G2EmbedTestUtilities.class 15 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
// $Id$

/**
 * @version 
 * @package GalleryCore
 * @author Kieran Parsons
 */

/**
 * A collection of useful G2Embed related utilities to test that all the required 
 * parameters and modules are correctly configures
 *
 * @package GalleryCore
 * @subpackage GalleryEmbed
 * @static
 */
class G2EmbedTestUtilities {
  
  /**
   * Validate the Gallery2 Embed variables
   *
   * Returns false if any test failed
   * @param $orig, $autodetect
   * @return array($gallery_valid, $new, $results)
   */
    function checkLocations($orig, $autodetect=true) {
        $gallery_valid = 1;
        // Normalise the G2 URI 
        $g2Uri = G2EmbedDiscoveryUtilities::normalizeG2Uri($orig['g2Uri']); 
        $new['g2Uri'] = $g2Uri;
        if ($g2Uri != $orig['g2Uri']) {
            $results['g2Uri']['title'] = G2EmbedTestUtilities::_trans('\'URI of Gallery2\' variable:');
            $results['g2Uri']['advise'] = true;
            $results['g2Uri']['notice'] = G2EmbedTestUtilities::_trans('%origG2Uri automatically updated to %g2Uri to adhere to Gallery2 requirements.',
              array('%origG2Uri' => $orig['g2Uri'], '%g2Uri' => $g2Uri));
        }
        
        // Find the Gallery Directory and Embed URI
        if ($autodetect) {
        // Auto-Detect the G2 embed path 
        list ($success, $embedPhpPath, $errorString) = 
            G2EmbedDiscoveryUtilities::getG2EmbedPathByG2Uri($g2Uri); 
        if (!$success) {
            // Something is wrong with the G2 URI given as the G2 embed path is not found
            $results['g2Uri2']['title'] = G2EmbedTestUtilities::_trans('\'URI of Gallery2\' variable:');
            $results['g2Uri2']['error'] = true;
            $results['g2Uri2']['notice'] = $errorString;
            $new['g2EmbedPath'] = rtrim($embedPhpPath, 'embed.php'); 
            $gallery_valid = 0;
        } else {
            // G2 Embed Path is found OK
            $results['g2Uri2']['title'] = G2EmbedTestUtilities::_trans('\'URI of Gallery2\' variable:');
            $results['g2Uri2']['success'] = true;
            $new['g2EmbedPath'] = rtrim($embedPhpPath, 'embed.php'); 
        }
        
        // Auto-Detect the Embed Application URI
        $embedUri =  G2EmbedTestUtilities::_autodetectEmbedUri();
        // Normalise the Embed Application URI
        $embedUri = G2EmbedDiscoveryUtilities::normalizeEmbedUri($embedUri); 
        $new['embedUri'] = $embedUri; 
        
        } else {
            // Do not autodetect the variables, but check the manually entered ones.
            $results['g2Uri2']['title'] = G2EmbedTestUtilities::_trans('\'URI of Gallery2\' variable:');
            $results['g2Uri2']['advise'] = true;
            $results['g2Uri2']['notice'] = 
                G2EmbedTestUtilities::_trans('Cannot fully check this when not in auto-detect mode.');
    
            $embedPhpPath = $orig['g2EmbedPath'];
            // Check path can be read, adding embed.php if needed
            if (substr($embedPhpPath, -1) != '/') {	   
                $embedPhpPath = $embedPhpPath . '/';
                $results['g2EmbedPath']['title'] = 
                    G2EmbedTestUtilities::_trans('\'Location of Gallery2\' variable:');
                $results['g2EmbedPath']['advise'] = true;
                $results['g2EmbedPath']['notice'] = 
                    G2EmbedTestUtilities::_trans('%g2EmbedPath automatically updated to %embedPhpPath to adhere to Gallery2 requirements.',
                    array('%g2EmbedPath' => $orig['g2EmbedPath'],
                          '%embedPhpPath' => $embedPhpPath));
            }
            list ($ok, $errorString) = 
                G2EmbedDiscoveryUtilities::isFileReadable($embedPhpPath.'embed.php');
            $new['g2EmbedPath'] = $embedPhpPath;
            if (!$ok) {
                // Something is wrong with the G2 embed path
                $results['g2EmbedPath2']['title'] = 
                    G2EmbedTestUtilities::_trans('\'Location of Gallery2\' variable:');
                $results['g2EmbedPath2']['error'] = true;
                $results['g2EmbedPath2']['notice'] = $errorString;
                $gallery_valid = 0;
            } else {
                // G2 Embed Path is found OK
                $results['g2EmbedPath2']['title'] = 
                    G2EmbedTestUtilities::_trans('\'Location of Gallery2\' variable:');
                $results['g2EmbedPath2']['success'] = true;
            }
            $embedUri = $orig['embedUri'];
            $embedUri = G2EmbedDiscoveryUtilities::normalizeEmbedUri($embedUri); 
            $new['embedUri'] = $embedUri;
            if ($embedUri != $orig['embedUri']) {
                $results['embedUri']['title'] = 
                    G2EmbedTestUtilities::_trans('\'Embed URI\' variable:');
                $results['embedUri']['advise'] = true;
                $results['embedUri']['notice'] = 
                    G2EmbedTestUtilities::_trans('%origEmbedUri automatically updated to %embedUri to adhere to Gallery2 requirements.',
                        array('%origEmbedUri' => $orig['embedUri'], 
                              '%embedUri' => $embedUri));
            }
            $results['embedUri2']['title'] =
                                G2EmbedTestUtilities::_trans('\'Embed URI\' variable:');
            $results['embedUri2']['advise'] = true;
            $results['embedUri2']['notice'] = 
                G2EmbedTestUtilities::_trans('Cannot fully check this when not in auto-detect mode.');
        }

    list ($numErrors, $numWarnings) = G2EmbedTestUtilities::_getNumErrorsAndWarnings($results);
    return array($numErrors, $numWarnings, $new, $results); 
 //   return array($gallery_valid, $new, $results);
    }

	/**
	 * Check the status of Gallery2 Modules before Gallery Init
	 *
	 * @param $testsToRun
	 * @return array results
	 */
    function preInitTests($testsToRun = null) {
        if (!$testsToRun) {
            $testsToRun['php_memory']['errorlevel'] = 'error';
            $testsToRun['php_memory']['minimumMemoryLimit'] = 24;
        }
        /* Check for adequate PHP Memory Limit */
        if (isset($testsToRun['php_memory'])) {
            $results['php_memory'] = G2EmbedTestUtilities::phpMemoryCheck(
                $testsToRun['php_memory']['minimumMemoryLimit']); 
        }
        list ($numErrors, $numWarnings) = G2EmbedTestUtilities::_getNumErrorsAndWarnings($results);
        return array($numErrors, $numWarnings, $results); 
    }

	/**
	 * Check the status of Gallery2 Modules after Gallery Init
	 *
	 * @param $testsToRun
	 * @return array results
	 */
    function postInitTests($testsToRun = null) {
  
        if (!$testsToRun) {
            $testsToRun['imageblock']['errorlevel'] = 'error';
            $testsToRun['imageframe']['errorlevel'] = 'warning';	  
            $testsToRun['urlrewrite']['errorlevel'] = 'advise';		 
        }
	
        list ($ret, $allModulesStatus) = GalleryCoreApi::fetchPluginStatus('module');
        /* Check for ImageBlock Module */
        if (isset($testsToRun['imageblock'])) {
            $results['imageblock'] = 
                G2EmbedTestUtilities::moduleCheck('imageblock', 'Image Block', 
                    $testsToRun['imageblock']['errorlevel'], $allModulesStatus); 
        }
        /* Check for ImageFrame Module */
        if (isset($testsToRun['imageframe'])) {
            $results['imageframe'] = 
                G2EmbedTestUtilities::moduleCheck('imageframe', 'Image Frame', 
                    $testsToRun['imageframe']['errorlevel'], $allModulesStatus); 
        }	
        if (isset($testsToRun['urlrewrite'])) {
            $results['urlrewrite'] = 
                G2EmbedTestUtilities::urlRewriteCheck($testsToRun['urlrewrite']['errorlevel']); 
        }
        list ($numErrors, $numWarnings) = G2EmbedTestUtilities::_getNumErrorsAndWarnings($results);
        return array($numErrors, $numWarnings, $results); 
    }

    /**
     * Warn if memory_limit is set and is too low
     * (from Gallery2 install code)
     *
     * @param minimumMemoryLimit
     * @return array results
     */
	function phpMemoryCheck($minimumMemoryLimit = 24) {
        $memoryLimit = ini_get('memory_limit');		 
        $title = G2EmbedTestUtilities::_trans('PHP Memory Limit:');
kiz_0987's avatar
kiz_0987 committed
188
        if ($memoryLimit != '' && (G2EmbedTestUtilities::_getBytes($memoryLimit) / (1024 * 1024)) < $minimumMemoryLimit) {
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
            $results = array(
					'title' => $title,
					'error' => true,
				    'notice' => G2EmbedTestUtilities::_trans(
                        'Your PHP is configured to limit the memory to %memLimit (<b> 
                         memory_limit</b> parameter in php.ini). You should raise this limit 
                         to at least <b>%minMemLimitMB</b> for proper Embedded Gallery2 operation',
                        array('%memLimit' => $memoryLimit, '%minMemLimit' => $minimumMemoryLimit)),
					);			
        } else {
            $results = array(
					'title' => $title,
					'success' => true,
					);		
        }
        return $results;
    }
    
	/**
	 * Check the status of Gallery2 URL Rewrite Module
	 *
	 * @param $errorLevel
	 * @return array results
	 */
	function urlRewriteCheck($errorLevel) {
        /* Check for URL Rewrite Module */
        $title = 'Gallery2 URL Rewrite Module Status:';
        list ($ret, $rewriteApi) = GalleryCoreApi::newFactoryInstance('RewriteApi');
        if ($ret) {
        /* G2 Error handling */
            $results = array(
                'title' => $title,
                'installed' => false,
                'active' => false,
                'available' => false,
                $errorLevel => true,
                'notice' => G2EmbedTestUtilities::_trans(
                        'Gallery2 Error when trying to access URL Rewrite Module status. %ret',
                        array('%ret' => $ret->getAsHtml())),
                );
        }
        if (empty($rewriteApi)) {
            $results = array(
                'title' => $title,
                'installed' => false,
                'active' => false,
                'available' => false,
                $errorLevel => true,
				'notice' =>  G2EmbedTestUtilities::_trans(
 			        'Either the URL rewrite module is not installed or the version is too old.'),
                );
        } else {
            /* Check compatibility */
            $required = array(1, 0);
            list ($ret, $isCompatible) = $rewriteApi->isCompatibleWithApi($required);
            if ($ret) {
                /* Error handeling */
                $results = array(
                    'title' => $title,
                    'installed' => false,
                    'active' => false,
                    'available' => false,
                    $errorLevel => true,
                    'notice' => G2EmbedTestUtilities::_trans(
                        'Gallery2 Error when trying to access URL Rewrite Module status. %ret',
                        array('%ret' => $ret->getAsHtml())),
                    );		  
            } else {
                if (!$isCompatible) {
                    $results = array(
                        'title' => $title,
                        'installed' => false,
                        'active' => false,
                        'available' => false,
                        $errorLevel => true,
 				        'notice' =>  G2EmbedTestUtilities::_trans(
 				           'The installed URL Rewrite module version is not compatible.'),
                        );
                } else {
                /* Add rules checking etc */
                $results = array(
                    'title' => $title,
                    'success' => true,
                    );
                }
            }
        }
        return $results;
    }
    
	/**
	 * Check the status of a Gallery2 Module
	 *
	 * @param $moduleId, $moduleName, $errorLevel, $allModulesStatus
	 * @return array moduleStatus
	 */
	function moduleCheck($moduleId, $moduleName, $errorLevel, $allModulesStatus) {
		$moduleStatus = array();
		if (isset($allModulesStatus[$moduleId])) {
			$moduleStatus['installed'] = true;
			$moduleStatus['active'] = $allModulesStatus[$moduleId]['active'];
			$moduleStatus['available'] = $allModulesStatus[$moduleId]['available'];		   
		} else {
			$moduleStatus['installed'] = false;
			$moduleStatus['active'] = false;
			$moduleStatus['available'] = false;
		}  
		$moduleStatus['title'] = 'Gallery2 ' . $moduleName .' Module Status:';
		if (!$moduleStatus['installed']) {
				$moduleStatus[$errorLevel] = true;
				$moduleStatus['notice'] = G2EmbedTestUtilities::_trans(
				    'The %moduleName Module is not installed.', 
				    array('%moduleName' => $moduleName));
		} else {
			if ($moduleStatus['active'] && $moduleStatus['available']) {
				$moduleStatus['success'] = true;
			} else {
				$moduleStatus[$errorLevel] = true;
				$moduleStatus['notice'] =  G2EmbedTestUtilities::_trans(
				    'The %moduleName Module is installed but not active or configured correctly.', 
				    array('%moduleName' => $moduleName));
		   }
		} 
		return $moduleStatus;
	}
	
kiz_0987's avatar
kiz_0987 committed
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
	    /*
     * Returns the exact bytes value from a php.ini setting
     *     * Copied from PHP.net's manual entry for ini_get()
     */
    function _getBytes($val) {
        $val = trim($val);
        $last = $val{strlen($val)-1};
        switch($last) {
        case 'k':
        case 'K':
            return (int) $val * 1024;
            break;
        case 'm':
        case 'M':
            return (int) $val * 1048576;
            break;
        default:
            return $val;
        }
    }
    
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
	/**
	 * Calculate the number of errors and warnings
	 *
	 * @param $results 
	 * @return array($numErrors, $numWarnings)
	 */
    function _getNumErrorsAndWarnings($results) {
        $numErrors = 0;
        $numWarnings = 0;
        foreach ($results as $key=>$result) {
            if ($result['error']) {
              $numErrors++;
            }
            if ($result['warning']) {
              $numWarnings++;
            }
        }
        return array($numErrors, $numWarnings);       
    }
	
	/**
	 * Translate the message string
	 * This needs to be implemented by each embed application individually.
	 *
	 * Returns the translated string.
	 * @param $string A string containing the English string to translate.
     *        $args An associative array of replacements to make after translation. 
     *              Incidences of any key in this array are replaced with the corresponding value.
	 * @return string Translated String
	 */
	function _trans($string, $args = 0) {
	   return t($string, $args);
	}
	
    /**
	 * Autodetect the Embed URI
	 * This needs to be implemented by each embed application individually.
	 *
	 * @param none
	 * @return string autodetected URI
	 */
	function _autodetectEmbedUri() {
     $uri = url('gallery', null, null, false);
     if (substr($uri, -8) == '/gallery') {
        $uri = substr($uri, 0, strlen($string)-7) . 'index.php?q=gallery';
     }
     // strip the end /gallery if present and replace with index.php?q=gallery to avoid any 
     // url rewrite issues. See http://gallery.menalto.com/node/46181
     // Note the use of index.php in all cases. Not needed for Apache, but is for IIS
     // see drupaldocs documentation for url.
      return $uri;
    }
}
?>