f58b518f |
1 | <?php //$Id$ |
2 | |
3 | /////////////////////////////////////////////////////////////////////////// |
4 | // // |
5 | // NOTICE OF COPYRIGHT // |
6 | // // |
7 | // Moodle - Modular Object-Oriented Dynamic Learning Environment // |
8 | // http://moodle.com // |
9 | // // |
b08a7225 |
10 | // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com // |
f58b518f |
11 | // (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com // |
12 | // // |
13 | // This program is free software; you can redistribute it and/or modify // |
14 | // it under the terms of the GNU General Public License as published by // |
15 | // the Free Software Foundation; either version 2 of the License, or // |
16 | // (at your option) any later version. // |
17 | // // |
18 | // This program is distributed in the hope that it will be useful, // |
19 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // |
20 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // |
21 | // GNU General Public License for more details: // |
22 | // // |
23 | // http://www.gnu.org/copyleft/gpl.html // |
24 | // // |
25 | /////////////////////////////////////////////////////////////////////////// |
26 | |
27 | // This library includes all the necessary stuff to execute some standard |
28 | // tests of required versions and libraries to run Moodle. It can be |
29 | // used from the admin interface, and both at install and upgrade. |
30 | // |
31 | // All the info is stored in the admin/environment.xml file, |
00d3a0fd |
32 | // supporting to have an updated version in dataroot/environment |
f58b518f |
33 | |
049c0f4a |
34 | /// Add required files |
35 | require_once($CFG->libdir.'/xmlize.php'); |
36 | |
37 | /// Define a buch of XML processing errors |
00d3a0fd |
38 | define('NO_ERROR', 0); |
39 | define('NO_VERSION_DATA_FOUND', 1); |
40 | define('NO_DATABASE_SECTION_FOUND', 2); |
41 | define('NO_DATABASE_VENDORS_FOUND', 3); |
42 | define('NO_DATABASE_VENDOR_MYSQL_FOUND', 4); |
43 | define('NO_DATABASE_VENDOR_POSTGRES_FOUND', 5); |
44 | define('NO_PHP_SECTION_FOUND', 6); |
45 | define('NO_PHP_VERSION_FOUND', 7); |
46 | define('NO_PHP_EXTENSIONS_SECTION_FOUND', 8); |
47 | define('NO_PHP_EXTENSIONS_NAME_FOUND', 9); |
48 | define('NO_DATABASE_VENDOR_VERSION_FOUND', 10); |
a392be33 |
49 | define('NO_UNICODE_SECTION_FOUND', 11); |
bac40536 |
50 | define('NO_CUSTOM_CHECK_FOUND', 12); |
51 | define('CUSTOM_CHECK_FILE_MISSING', 13); |
52 | define('CUSTOM_CHECK_FUNCTION_MISSING', 14); |
c808379e |
53 | define('NO_PHP_SETTINGS_NAME_FOUND', 15); |
f58b518f |
54 | |
829fa074 |
55 | /// Define algorithm used to select the xml file |
56 | define('ENV_SELECT_NEWER', 0); /// To select the newer file available to perform checks |
57 | define('ENV_SELECT_DATAROOT', 1); /// To enforce the use of the file under dataroot |
58 | define('ENV_SELECT_RELEASE', 2); /// To enforce the use of the file under admin (release) |
59 | |
f58b518f |
60 | /** |
61 | * This function will perform the whole check, returning |
62 | * true or false as final result. Also, he full array of |
63 | * environment_result will be returned in the parameter list. |
64 | * The function looks for the best version to compare and |
65 | * everything. This is the only function that should be called |
66 | * ever from the rest of Moodle. |
770fef0a |
67 | * @param string version version to check. |
f58b518f |
68 | * @param array results array of results checked. |
770fef0a |
69 | * @param boolean true/false, whether to print the table or just return results array |
829fa074 |
70 | * @param int one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use. Default ENV_SELECT_NEWER (BC) |
f58b518f |
71 | * @return boolean true/false, depending of results |
72 | */ |
829fa074 |
73 | function check_moodle_environment($version, &$environment_results, $print_table=true, $env_select=ENV_SELECT_NEWER) { |
049c0f4a |
74 | |
75 | $status = true; |
f58b518f |
76 | |
878d309c |
77 | /// This are cached per request |
78 | static $result = true; |
79 | static $env_results; |
80 | static $cache_exists = false; |
81 | |
82 | /// if we have results cached, use them |
83 | if ($cache_exists) { |
84 | $environment_results = $env_results; |
85 | /// No cache exists, calculate everything |
86 | } else { |
87 | /// Get the more recent version before the requested |
829fa074 |
88 | if (!$version = get_latest_version_available($version, $env_select)) { |
878d309c |
89 | $status = false; |
90 | } |
f58b518f |
91 | |
878d309c |
92 | /// Perform all the checks |
829fa074 |
93 | if (!($environment_results = environment_check($version, $env_select)) && $status) { |
878d309c |
94 | $status = false; |
95 | } |
f58b518f |
96 | |
878d309c |
97 | /// Iterate over all the results looking for some error in required items |
98 | /// or some error_code |
99 | if ($status) { |
100 | foreach ($environment_results as $environment_result) { |
95a39282 |
101 | if (!$environment_result->getStatus() && $environment_result->getLevel() == 'required' |
102 | && !$environment_result->getBypassStr()) { |
103 | $result = false; // required item that is not bypased |
104 | } else if ($environment_result->getStatus() && $environment_result->getLevel() == 'required' |
105 | && $environment_result->getRestrictStr()) { |
106 | $result = false; // required item that is restricted |
107 | } else if ($environment_result->getErrorCode()) { |
878d309c |
108 | $result = false; |
109 | } |
049c0f4a |
110 | } |
f58b518f |
111 | } |
878d309c |
112 | /// Going to end, we store environment_results to cache |
113 | $env_results = $environment_results; |
114 | $cache_exists = true; |
115 | } ///End of cache block |
f58b518f |
116 | |
049c0f4a |
117 | /// If we have decided to print all the information, just do it |
118 | if ($print_table) { |
e909788d |
119 | print_moodle_environment($result && $status, $environment_results); |
049c0f4a |
120 | } |
049c0f4a |
121 | return ($result && $status); |
122 | } |
123 | |
770fef0a |
124 | /** |
049c0f4a |
125 | * This function will print one beautiful table with all the environmental |
126 | * configuration and how it suits Moodle needs. |
127 | * @param boolean final result of the check (true/false) |
128 | * @param array environment_results array of results gathered |
129 | */ |
130 | function print_moodle_environment($result, $environment_results) { |
90509582 |
131 | global $CFG; |
132 | |
049c0f4a |
133 | /// Get some strings |
134 | $strname = get_string('name'); |
135 | $strinfo = get_string('info'); |
136 | $strreport = get_string('report'); |
137 | $strstatus = get_string('status'); |
138 | $strok = get_string('ok'); |
139 | $strerror = get_string('error'); |
140 | $strcheck = get_string('check'); |
b0e2a189 |
141 | $strbypassed = get_string('bypassed'); |
95a39282 |
142 | $strrestricted = get_string('restricted'); |
e909788d |
143 | $strenvironmenterrortodo = get_string('environmenterrortodo', 'admin'); |
770fef0a |
144 | /// Table headers |
145 | $servertable = new stdClass;//table for server checks |
146 | $servertable->head = array ($strname, $strinfo, $strreport, $strstatus); |
147 | $servertable->align = array ('center', 'center', 'left', 'center'); |
148 | $servertable->wrap = array ('nowrap', '', '', 'nowrap'); |
149 | $servertable->size = array ('10', 10, '100%', '10'); |
150 | $servertable->width = '90%'; |
151 | $servertable->class = 'environmenttable generaltable'; |
152 | |
153 | $serverdata = array('ok'=>array(), 'warn'=>array(), 'error'=>array()); |
154 | |
155 | $othertable = new stdClass;//table for custom checks |
156 | $othertable->head = array ($strinfo, $strreport, $strstatus); |
157 | $othertable->align = array ('center', 'left', 'center'); |
158 | $othertable->wrap = array ('', '', 'nowrap'); |
159 | $othertable->size = array (10, '100%', '10'); |
160 | $othertable->width = '90%'; |
161 | $othertable->class = 'environmenttable generaltable'; |
162 | |
163 | $otherdata = array('ok'=>array(), 'warn'=>array(), 'error'=>array()); |
049c0f4a |
164 | |
165 | /// Iterate over each environment_result |
166 | $continue = true; |
167 | foreach ($environment_results as $environment_result) { |
95a39282 |
168 | $errorline = false; |
169 | $warningline = false; |
90509582 |
170 | $stringtouse = ''; |
049c0f4a |
171 | if ($continue) { |
172 | $type = $environment_result->getPart(); |
173 | $info = $environment_result->getInfo(); |
174 | $status = $environment_result->getStatus(); |
175 | $error_code = $environment_result->getErrorCode(); |
176 | /// Process Report field |
878d309c |
177 | $rec = new stdClass(); |
049c0f4a |
178 | /// Something has gone wrong at parsing time |
179 | if ($error_code) { |
180 | $stringtouse = 'environmentxmlerror'; |
181 | $rec->error_code = $error_code; |
182 | $status = $strerror; |
183 | $errorline = true; |
184 | $continue = false; |
185 | } |
186 | |
187 | if ($continue) { |
188 | /// We are comparing versions |
189 | if ($rec->needed = $environment_result->getNeededVersion()) { |
190 | $rec->current = $environment_result->getCurrentVersion(); |
191 | if ($environment_result->getLevel() == 'required') { |
192 | $stringtouse = 'environmentrequireversion'; |
193 | } else { |
194 | $stringtouse = 'environmentrecommendversion'; |
195 | } |
196 | /// We are checking installed & enabled things |
ce5f4578 |
197 | } else if ($environment_result->getPart() == 'custom_check') { |
198 | if ($environment_result->getLevel() == 'required') { |
199 | $stringtouse = 'environmentrequirecustomcheck'; |
200 | } else { |
201 | $stringtouse = 'environmentrecommendcustomcheck'; |
202 | } |
c808379e |
203 | } else if ($environment_result->getPart() == 'php_setting') { |
204 | if ($status) { |
205 | $stringtouse = 'environmentsettingok'; |
206 | } else if ($environment_result->getLevel() == 'required') { |
f404df92 |
207 | $stringtouse = 'environmentmustfixsetting'; |
c808379e |
208 | } else { |
f404df92 |
209 | $stringtouse = 'environmentshouldfixsetting'; |
c808379e |
210 | } |
049c0f4a |
211 | } else { |
212 | if ($environment_result->getLevel() == 'required') { |
213 | $stringtouse = 'environmentrequireinstall'; |
214 | } else { |
215 | $stringtouse = 'environmentrecommendinstall'; |
216 | } |
217 | } |
218 | /// Calculate the status value |
95a39282 |
219 | if ($environment_result->getBypassStr() != '') { //Handle bypassed result (warning) |
b0e2a189 |
220 | $status = $strbypassed; |
95a39282 |
221 | $warningline = true; |
222 | } else if ($environment_result->getRestrictStr() != '') { //Handle restricted result (error) |
223 | $status = $strrestricted; |
b0e2a189 |
224 | $errorline = true; |
95a39282 |
225 | } else { |
226 | if ($status) { //Handle ok result (ok) |
227 | $status = $strok; |
228 | } else { |
229 | if ($environment_result->getLevel() == 'optional') {//Handle check result (warning) |
230 | $status = $strcheck; |
231 | $warningline = true; |
232 | } else { //Handle error result (error) |
770fef0a |
233 | $status = $strcheck; |
95a39282 |
234 | $errorline = true; |
235 | } |
236 | } |
049c0f4a |
237 | } |
238 | } |
770fef0a |
239 | |
049c0f4a |
240 | /// Build the text |
770fef0a |
241 | $linkparts = array(); |
242 | $linkparts[] = 'admin/environment'; |
243 | $linkparts[] = $type; |
244 | if (!empty($info)){ |
245 | $linkparts[] = $info; |
246 | } |
90509582 |
247 | if (empty($CFG->docroot)) { |
248 | $report = get_string($stringtouse, 'admin', $rec); |
249 | } else { |
250 | $report = doc_link(join($linkparts, '/'), get_string($stringtouse, 'admin', $rec)); |
251 | } |
770fef0a |
252 | |
253 | |
95a39282 |
254 | /// Format error or warning line |
255 | if ($errorline || $warningline) { |
770fef0a |
256 | $messagetype = $errorline? 'error':'warn'; |
257 | } else { |
258 | $messagetype = 'ok'; |
049c0f4a |
259 | } |
770fef0a |
260 | $status = '<span class="'.$messagetype.'">'.$status.'</span>'; |
261 | /// Here we'll store all the feedback found |
262 | $feedbacktext = ''; |
afb36bca |
263 | ///Append the feedback if there is some |
264 | $feedbacktext .= $environment_result->strToReport($environment_result->getFeedbackStr(), $messagetype); |
265 | ///Append the bypass if there is some |
266 | $feedbacktext .= $environment_result->strToReport($environment_result->getBypassStr(), 'warn'); |
267 | ///Append the restrict if there is some |
268 | $feedbacktext .= $environment_result->strToReport($environment_result->getRestrictStr(), 'error'); |
269 | |
270 | $report .= $feedbacktext; |
770fef0a |
271 | /// Add the row to the table |
272 | |
273 | if ($environment_result->getPart() == 'custom_check'){ |
274 | $otherdata[$messagetype][] = array ($info, $report, $status); |
770fef0a |
275 | } else { |
276 | $serverdata[$messagetype][] = array ($type, $info, $report, $status); |
95a39282 |
277 | } |
049c0f4a |
278 | } |
279 | } |
770fef0a |
280 | //put errors first in |
281 | $servertable->data = array_merge($serverdata['error'], $serverdata['warn'], $serverdata['ok']); |
282 | $othertable->data = array_merge($otherdata['error'], $otherdata['warn'], $otherdata['ok']); |
e909788d |
283 | |
770fef0a |
284 | /// Print table |
285 | print_heading(get_string('serverchecks', 'admin')); |
286 | print_table($servertable); |
287 | if (count($othertable->data)){ |
288 | print_heading(get_string('customcheck', 'admin')); |
289 | print_table($othertable); |
9e2d15e5 |
290 | } |
291 | |
e909788d |
292 | /// Finally, if any error has happened, print the summary box |
293 | if (!$result) { |
cc60cd9b |
294 | print_simple_box($strenvironmenterrortodo, 'center', '', '', '', 'environmentbox errorbox'); |
e909788d |
295 | } |
f58b518f |
296 | } |
297 | |
298 | |
299 | /** |
300 | * This function will normalize any version to just a serie of numbers |
301 | * separated by dots. Everything else will be removed. |
302 | * @param string $version the original version |
303 | * @return string the normalized version |
304 | */ |
305 | function normalize_version($version) { |
4fa6cb47 |
306 | |
307 | /// 1.9 Beta 2 should be read 1.9 on enviromental checks, not 1.9.2 |
308 | /// we can discard everything after the first space |
309 | $version = trim($version); |
310 | $versionarr = explode(" ",$version); |
311 | if (!empty($versionarr)) { |
312 | $version = $versionarr[0]; |
313 | } |
f58b518f |
314 | /// Replace everything but numbers and dots by dots |
315 | $version = preg_replace('/[^\.\d]/', '.', $version); |
316 | /// Combine multiple dots in one |
317 | $version = preg_replace('/(\.{2,})/', '.', $version); |
318 | /// Trim possible leading and trailing dots |
319 | $version = trim($version, '.'); |
320 | |
321 | return $version; |
322 | } |
323 | |
324 | |
325 | /** |
326 | * This function will load the environment.xml file and xmlize it |
829fa074 |
327 | * @param int one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use. Default ENV_SELECT_NEWER (BC) |
f58b518f |
328 | * @return mixed the xmlized structure or false on error |
329 | */ |
829fa074 |
330 | function load_environment_xml($env_select=ENV_SELECT_NEWER) { |
770fef0a |
331 | |
f58b518f |
332 | global $CFG; |
333 | |
334 | static $data; //Only load and xmlize once by request |
335 | |
336 | if (!empty($data)) { |
337 | return $data; |
338 | } |
339 | |
00d3a0fd |
340 | /// First of all, take a look inside $CFG->dataroot/environment/environment.xml |
341 | $file = $CFG->dataroot.'/environment/environment.xml'; |
38fca5d7 |
342 | $internalfile = $CFG->dirroot.'/'.$CFG->admin.'/environment.xml'; |
829fa074 |
343 | switch ($env_select) { |
344 | case ENV_SELECT_NEWER: |
345 | if (!is_file($file) || !is_readable($file) || filemtime($file) < filemtime($internalfile) || |
346 | !$contents = file_get_contents($file)) { |
347 | /// Fallback to fixed $CFG->admin/environment.xml |
348 | if (!is_file($internalfile) || !is_readable($internalfile) || !$contents = file_get_contents($internalfile)) { |
349 | return false; |
350 | } |
351 | } |
352 | break; |
353 | case ENV_SELECT_DATAROOT: |
354 | if (!is_file($file) || !is_readable($file) || !$contents = file_get_contents($file)) { |
355 | return false; |
356 | } |
357 | break; |
358 | case ENV_SELECT_RELEASE: |
359 | if (!is_file($internalfile) || !is_readable($internalfile) || !$contents = file_get_contents($internalfile)) { |
360 | return false; |
361 | } |
362 | break; |
f58b518f |
363 | } |
364 | /// XML the whole file |
365 | $data = xmlize($contents); |
366 | |
367 | return $data; |
368 | } |
369 | |
370 | |
371 | /** |
372 | * This function will return the list of Moodle versions available |
373 | * @return mixed array of versions. False on error. |
374 | */ |
375 | function get_list_of_environment_versions ($contents) { |
376 | |
377 | static $versions = array(); |
378 | |
379 | if (!empty($versions)) { |
380 | return $versions; |
381 | } |
382 | |
383 | if (isset($contents['COMPATIBILITY_MATRIX']['#']['MOODLE'])) { |
384 | foreach ($contents['COMPATIBILITY_MATRIX']['#']['MOODLE'] as $version) { |
385 | $versions[] = $version['@']['version']; |
386 | } |
387 | } |
388 | |
389 | return $versions; |
390 | } |
391 | |
392 | |
393 | /** |
394 | * This function will return the most recent version in the environment.xml |
395 | * file previous or equal to the version requested |
396 | * @param string version top version from which we start to look backwards |
829fa074 |
397 | * @param int one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use. |
f58b518f |
398 | * @return string more recent version or false if not found |
399 | */ |
829fa074 |
400 | function get_latest_version_available ($version, $env_select) { |
f58b518f |
401 | |
402 | /// Normalize the version requested |
403 | $version = normalize_version($version); |
404 | |
405 | /// Load xml file |
829fa074 |
406 | if (!$contents = load_environment_xml($env_select)) { |
f58b518f |
407 | return false; |
408 | } |
409 | |
410 | /// Detect available versions |
411 | if (!$versions = get_list_of_environment_versions($contents)) { |
412 | return false; |
413 | } |
414 | /// First we look for exact version |
415 | if (in_array($version, $versions)) { |
416 | return $version; |
417 | } else { |
418 | $found_version = false; |
419 | /// Not exact match, so we are going to iterate over the list searching |
420 | /// for the latest version before the requested one |
421 | foreach ($versions as $arrversion) { |
422 | if (version_compare($arrversion, $version, '<')) { |
423 | $found_version = $arrversion; |
424 | } |
425 | } |
426 | } |
427 | |
428 | return $found_version; |
429 | } |
430 | |
431 | |
770fef0a |
432 | /** |
f58b518f |
433 | * This function will return the xmlized data belonging to one Moodle version |
829fa074 |
434 | * @param int one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use. |
f58b518f |
435 | * @return mixed the xmlized structure or false on error |
436 | */ |
829fa074 |
437 | function get_environment_for_version($version, $env_select) { |
770fef0a |
438 | |
f58b518f |
439 | /// Normalize the version requested |
440 | $version = normalize_version($version); |
441 | |
442 | /// Load xml file |
829fa074 |
443 | if (!$contents = load_environment_xml($env_select)) { |
f58b518f |
444 | return false; |
445 | } |
446 | |
447 | /// Detect available versions |
448 | if (!$versions = get_list_of_environment_versions($contents)) { |
449 | return false; |
450 | } |
451 | |
452 | /// If the version requested is available |
453 | if (!in_array($version, $versions)) { |
454 | return false; |
455 | } |
456 | |
457 | /// We now we have it. Extract from full contents. |
458 | $fl_arr = array_flip($versions); |
770fef0a |
459 | |
f58b518f |
460 | return $contents['COMPATIBILITY_MATRIX']['#']['MOODLE'][$fl_arr[$version]]; |
461 | } |
462 | |
463 | |
770fef0a |
464 | /** |
f58b518f |
465 | * This function will check for everything (DB, PHP and PHP extensions for now) |
466 | * returning an array of environment_result objects. |
467 | * @param string $version xml version we are going to use to test this server |
829fa074 |
468 | * @param int one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use. |
f58b518f |
469 | * @return array array of results encapsulated in one environment_result object |
470 | */ |
829fa074 |
471 | function environment_check($version, $env_select) { |
7f2d3ec0 |
472 | global $CFG; |
473 | |
f58b518f |
474 | /// Normalize the version requested |
475 | $version = normalize_version($version); |
476 | |
477 | $results = array(); //To store all the results |
478 | |
7f2d3ec0 |
479 | /// Only run the moodle versions checker on upgrade, not on install |
90509582 |
480 | if (!empty($CFG->version)) { |
829fa074 |
481 | $results[] = environment_check_moodle($version, $env_select); |
7f2d3ec0 |
482 | } |
829fa074 |
483 | $results[] = environment_check_unicode($version, $env_select); |
484 | $results[] = environment_check_database($version, $env_select); |
485 | $results[] = environment_check_php($version, $env_select); |
f58b518f |
486 | |
829fa074 |
487 | $phpext_results = environment_check_php_extensions($version, $env_select); |
bac40536 |
488 | $results = array_merge($results, $phpext_results); |
f58b518f |
489 | |
829fa074 |
490 | $phpsetting_results = environment_check_php_settings($version, $env_select); |
c808379e |
491 | $results = array_merge($results, $phpsetting_results); |
492 | |
829fa074 |
493 | $custom_results = environment_custom_checks($version, $env_select); |
bac40536 |
494 | $results = array_merge($results, $custom_results); |
f58b518f |
495 | |
496 | return $results; |
497 | } |
498 | |
499 | |
500 | /** |
501 | * This function will check if php extensions requirements are satisfied |
502 | * @param string $version xml version we are going to use to test this server |
829fa074 |
503 | * @param int one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use. |
f58b518f |
504 | * @return array array of results encapsulated in one environment_result object |
505 | */ |
829fa074 |
506 | function environment_check_php_extensions($version, $env_select) { |
f58b518f |
507 | |
508 | $results = array(); |
509 | |
510 | /// Get the enviroment version we need |
829fa074 |
511 | if (!$data = get_environment_for_version($version, $env_select)) { |
f58b518f |
512 | /// Error. No version data found |
049c0f4a |
513 | $result = new environment_results('php_extension'); |
f58b518f |
514 | $result->setStatus(false); |
515 | $result->setErrorCode(NO_VERSION_DATA_FOUND); |
c33b83e7 |
516 | return array($result); |
f58b518f |
517 | } |
518 | |
519 | /// Extract the php_extension part |
520 | if (!isset($data['#']['PHP_EXTENSIONS']['0']['#']['PHP_EXTENSION'])) { |
521 | /// Error. No PHP section found |
049c0f4a |
522 | $result = new environment_results('php_extension'); |
f58b518f |
523 | $result->setStatus(false); |
524 | $result->setErrorCode(NO_PHP_EXTENSIONS_SECTION_FOUND); |
c33b83e7 |
525 | return array($result); |
9e2d15e5 |
526 | } |
527 | /// Iterate over extensions checking them and creating the needed environment_results |
528 | foreach($data['#']['PHP_EXTENSIONS']['0']['#']['PHP_EXTENSION'] as $extension) { |
529 | $result = new environment_results('php_extension'); |
530 | /// Check for level |
bac40536 |
531 | $level = get_level($extension); |
9e2d15e5 |
532 | /// Check for extension name |
533 | if (!isset($extension['@']['name'])) { |
534 | $result->setStatus(false); |
535 | $result->setErrorCode(NO_PHP_EXTENSIONS_NAME_FOUND); |
536 | } else { |
537 | $extension_name = $extension['@']['name']; |
538 | /// The name exists. Just check if it's an installed extension |
539 | if (!extension_loaded($extension_name)) { |
f58b518f |
540 | $result->setStatus(false); |
f58b518f |
541 | } else { |
9e2d15e5 |
542 | $result->setStatus(true); |
f58b518f |
543 | } |
9e2d15e5 |
544 | $result->setLevel($level); |
545 | $result->setInfo($extension_name); |
f58b518f |
546 | } |
bac40536 |
547 | |
548 | /// Do any actions defined in the XML file. |
549 | process_environment_result($extension, $result); |
b0e2a189 |
550 | |
9e2d15e5 |
551 | /// Add the result to the array of results |
552 | $results[] = $result; |
f58b518f |
553 | } |
554 | |
9e2d15e5 |
555 | |
f58b518f |
556 | return $results; |
557 | } |
558 | |
c808379e |
559 | /** |
560 | * This function will check if php extensions requirements are satisfied |
561 | * @param string $version xml version we are going to use to test this server |
829fa074 |
562 | * @param int one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use. |
c808379e |
563 | * @return array array of results encapsulated in one environment_result object |
564 | */ |
829fa074 |
565 | function environment_check_php_settings($version, $env_select) { |
c808379e |
566 | |
567 | $results = array(); |
568 | |
569 | /// Get the enviroment version we need |
829fa074 |
570 | if (!$data = get_environment_for_version($version, $env_select)) { |
c808379e |
571 | /// Error. No version data found |
572 | $result = new environment_results('php_setting'); |
573 | $result->setStatus(false); |
574 | $result->setErrorCode(NO_VERSION_DATA_FOUND); |
575 | return $result; |
576 | } |
577 | |
578 | /// Extract the php_setting part |
579 | if (!isset($data['#']['PHP_SETTINGS']['0']['#']['PHP_SETTING'])) { |
580 | /// No PHP section found - ignore |
581 | return $results; |
582 | } |
583 | /// Iterate over settings checking them and creating the needed environment_results |
584 | foreach($data['#']['PHP_SETTINGS']['0']['#']['PHP_SETTING'] as $setting) { |
585 | $result = new environment_results('php_setting'); |
586 | /// Check for level |
587 | $level = get_level($setting); |
588 | $result->setLevel($level); |
589 | /// Check for extension name |
590 | if (!isset($setting['@']['name'])) { |
591 | $result->setStatus(false); |
592 | $result->setErrorCode(NO_PHP_SETTINGS_NAME_FOUND); |
593 | } else { |
594 | $setting_name = $setting['@']['name']; |
595 | $setting_value = $setting['@']['value']; |
596 | $result->setInfo($setting_name); |
597 | |
598 | if ($setting_name == 'memory_limit') { |
599 | $current = ini_get('memory_limit'); |
600 | if ($current == -1) { |
601 | $result->setStatus(true); |
602 | } else { |
603 | $current = get_real_size($current); |
604 | $minlimit = get_real_size($setting_value); |
605 | if ($current < $minlimit) { |
606 | @ini_set('memory_limit', $setting_value); |
607 | $current = ini_get('memory_limit'); |
608 | $current = get_real_size($current); |
609 | } |
610 | $result->setStatus($current >= $minlimit); |
611 | } |
612 | |
613 | } else { |
614 | $current = ini_get_bool($setting_name); |
615 | /// The name exists. Just check if it's an installed extension |
616 | if ($current == $setting_value) { |
617 | $result->setStatus(true); |
618 | } else { |
619 | $result->setStatus(false); |
620 | } |
621 | } |
622 | } |
623 | |
624 | /// Do any actions defined in the XML file. |
625 | process_environment_result($setting, $result); |
626 | |
627 | /// Add the result to the array of results |
628 | $results[] = $result; |
629 | } |
630 | |
631 | |
632 | return $results; |
633 | } |
634 | |
bac40536 |
635 | /** |
636 | * This function will do the custom checks. |
637 | * @param string $version xml version we are going to use to test this server. |
829fa074 |
638 | * @param int one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use. |
bac40536 |
639 | * @return array array of results encapsulated in environment_result objects. |
640 | */ |
829fa074 |
641 | function environment_custom_checks($version, $env_select) { |
bac40536 |
642 | global $CFG; |
643 | |
644 | $results = array(); |
645 | |
eae02f59 |
646 | /// Get current Moodle version (release) for later compare |
b08a7225 |
647 | $release = isset($CFG->release) ? $CFG->release : $version; /// In case $CFG fails (at install) use $version |
648 | $current_version = normalize_version($release); |
eae02f59 |
649 | |
bac40536 |
650 | /// Get the enviroment version we need |
829fa074 |
651 | if (!$data = get_environment_for_version($version, $env_select)) { |
bac40536 |
652 | /// Error. No version data found - but this will already have been reported. |
653 | return $results; |
654 | } |
655 | |
656 | /// Extract the CUSTOM_CHECKS part |
657 | if (!isset($data['#']['CUSTOM_CHECKS']['0']['#']['CUSTOM_CHECK'])) { |
658 | /// No custom checks found - not a problem |
659 | return $results; |
660 | } |
661 | |
662 | /// Iterate over extensions checking them and creating the needed environment_results |
663 | foreach($data['#']['CUSTOM_CHECKS']['0']['#']['CUSTOM_CHECK'] as $check) { |
664 | $result = new environment_results('custom_check'); |
665 | |
666 | /// Check for level |
667 | $level = get_level($check); |
668 | |
669 | /// Check for extension name |
670 | if (isset($check['@']['file']) && isset($check['@']['function'])) { |
671 | $file = $CFG->dirroot . '/' . $check['@']['file']; |
bac40536 |
672 | $function = $check['@']['function']; |
673 | if (is_readable($file)) { |
674 | include_once($file); |
675 | if (function_exists($function)) { |
676 | $result->setLevel($level); |
677 | $result->setInfo($function); |
678 | $result = $function($result); |
679 | } else { |
eae02f59 |
680 | /// Only show error for current version (where function MUST exist) |
681 | /// else, we are performing custom checks against future versiosn |
682 | /// and function MAY not exist, so it doesn't cause error, just skip |
683 | /// custom check by returning null. MDL-15939 |
684 | if (version_compare($current_version, $version, '>=')) { |
685 | $result->setStatus(false); |
686 | $result->setInfo($function); |
687 | $result->setErrorCode(CUSTOM_CHECK_FUNCTION_MISSING); |
688 | } else { |
689 | $result = null; |
690 | } |
bac40536 |
691 | } |
692 | } else { |
eae02f59 |
693 | /// Only show error for current version (where function MUST exist) |
694 | /// else, we are performing custom checks against future versiosn |
695 | /// and function MAY not exist, so it doesn't cause error, just skip |
696 | /// custom check by returning null. MDL-15939 |
697 | if (version_compare($current_version, $version, '>=')) { |
698 | $result->setStatus(false); |
699 | $result->setInfo($function); |
700 | $result->setErrorCode(CUSTOM_CHECK_FILE_MISSING); |
701 | } else { |
702 | $result = null; |
703 | } |
bac40536 |
704 | } |
705 | } else { |
706 | $result->setStatus(false); |
707 | $result->setErrorCode(NO_CUSTOM_CHECK_FOUND); |
708 | } |
709 | |
710 | if (!is_null($result)) { |
711 | /// Do any actions defined in the XML file. |
712 | process_environment_result($check, $result); |
770fef0a |
713 | |
bac40536 |
714 | /// Add the result to the array of results |
715 | $results[] = $result; |
716 | } |
717 | } |
718 | |
719 | return $results; |
720 | } |
f58b518f |
721 | |
7f2d3ec0 |
722 | /** |
723 | * This function will check if Moodle requirements are satisfied |
724 | * @param string $version xml version we are going to use to test this server |
829fa074 |
725 | * @param int one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use. |
7f2d3ec0 |
726 | * @return object results encapsulated in one environment_result object |
727 | */ |
829fa074 |
728 | function environment_check_moodle($version, $env_select) { |
7f2d3ec0 |
729 | |
730 | $result = new environment_results('moodle'); |
731 | |
732 | /// Get the enviroment version we need |
829fa074 |
733 | if (!$data = get_environment_for_version($version, $env_select)) { |
7f2d3ec0 |
734 | /// Error. No version data found |
735 | $result->setStatus(false); |
736 | $result->setErrorCode(NO_VERSION_DATA_FOUND); |
737 | return $result; |
738 | } |
739 | |
740 | /// Extract the moodle part |
741 | if (!isset($data['@']['requires'])) { |
742 | $needed_version = '1.0'; /// Default to 1.0 if no moodle requires is found |
743 | } else { |
744 | /// Extract required moodle version |
745 | $needed_version = $data['@']['requires']; |
746 | } |
747 | |
748 | /// Now search the version we are using |
749 | $current_version = normalize_version(get_config('', 'release')); |
750 | |
751 | /// And finally compare them, saving results |
752 | if (version_compare($current_version, $needed_version, '>=')) { |
753 | $result->setStatus(true); |
754 | } else { |
755 | $result->setStatus(false); |
756 | } |
757 | $result->setLevel('required'); |
758 | $result->setCurrentVersion($current_version); |
759 | $result->setNeededVersion($needed_version); |
760 | |
761 | return $result; |
762 | } |
763 | |
f58b518f |
764 | /** |
765 | * This function will check if php requirements are satisfied |
766 | * @param string $version xml version we are going to use to test this server |
829fa074 |
767 | * @param int one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use. |
f58b518f |
768 | * @return object results encapsulated in one environment_result object |
769 | */ |
829fa074 |
770 | function environment_check_php($version, $env_select) { |
f58b518f |
771 | |
772 | $result = new environment_results('php'); |
773 | |
774 | /// Get the enviroment version we need |
829fa074 |
775 | if (!$data = get_environment_for_version($version, $env_select)) { |
f58b518f |
776 | /// Error. No version data found |
777 | $result->setStatus(false); |
778 | $result->setErrorCode(NO_VERSION_DATA_FOUND); |
779 | return $result; |
780 | } |
781 | |
782 | /// Extract the php part |
783 | if (!isset($data['#']['PHP'])) { |
784 | /// Error. No PHP section found |
785 | $result->setStatus(false); |
786 | $result->setErrorCode(NO_PHP_SECTION_FOUND); |
787 | return $result; |
788 | } else { |
789 | /// Extract level and version |
bac40536 |
790 | $level = get_level($data['#']['PHP']['0']); |
f58b518f |
791 | if (!isset($data['#']['PHP']['0']['@']['version'])) { |
792 | $result->setStatus(false); |
793 | $result->setErrorCode(NO_PHP_VERSION_FOUND); |
794 | return $result; |
795 | } else { |
796 | $needed_version = $data['#']['PHP']['0']['@']['version']; |
797 | } |
798 | } |
799 | |
800 | /// Now search the version we are using |
801 | $current_version = normalize_version(phpversion()); |
802 | |
803 | /// And finally compare them, saving results |
804 | if (version_compare($current_version, $needed_version, '>=')) { |
805 | $result->setStatus(true); |
806 | } else { |
807 | $result->setStatus(false); |
f58b518f |
808 | } |
770fef0a |
809 | $result->setLevel($level); |
f58b518f |
810 | $result->setCurrentVersion($current_version); |
811 | $result->setNeededVersion($needed_version); |
bac40536 |
812 | |
813 | /// Do any actions defined in the XML file. |
814 | process_environment_result($data['#']['PHP'][0], $result); |
f58b518f |
815 | |
816 | return $result; |
817 | } |
818 | |
819 | |
a392be33 |
820 | /** |
821 | * This function will check if unicode database requirements are satisfied |
822 | * @param string $version xml version we are going to use to test this server |
829fa074 |
823 | * @param int one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use. |
a392be33 |
824 | * @return object results encapsulated in one environment_result object |
825 | */ |
829fa074 |
826 | function environment_check_unicode($version, $env_select) { |
fd0e6640 |
827 | global $DB; |
a392be33 |
828 | |
829 | $result = new environment_results('unicode'); |
830 | |
831 | /// Get the enviroment version we need |
829fa074 |
832 | if (!$data = get_environment_for_version($version, $env_select)) { |
a392be33 |
833 | /// Error. No version data found |
834 | $result->setStatus(false); |
835 | $result->setErrorCode(NO_VERSION_DATA_FOUND); |
836 | return $result; |
837 | } |
838 | |
839 | /// Extract the unicode part |
840 | |
841 | if (!isset($data['#']['UNICODE'])) { |
7f2d3ec0 |
842 | /// Error. No UNICODE section found |
a392be33 |
843 | $result->setStatus(false); |
844 | $result->setErrorCode(NO_UNICODE_SECTION_FOUND); |
845 | return $result; |
846 | } else { |
847 | /// Extract level |
bac40536 |
848 | $level = get_level($data['#']['UNICODE']['0']); |
a392be33 |
849 | } |
850 | |
f33e1ed4 |
851 | if (!$unicodedb = $DB->setup_is_unicodedb()) { |
a392be33 |
852 | $result->setStatus(false); |
853 | } else { |
854 | $result->setStatus(true); |
855 | } |
856 | |
857 | $result->setLevel($level); |
858 | |
bac40536 |
859 | /// Do any actions defined in the XML file. |
860 | process_environment_result($data['#']['UNICODE'][0], $result); |
a392be33 |
861 | |
862 | return $result; |
863 | } |
864 | |
f58b518f |
865 | /** |
866 | * This function will check if database requirements are satisfied |
867 | * @param string $version xml version we are going to use to test this server |
829fa074 |
868 | * @param int one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use. |
f58b518f |
869 | * @return object results encapsulated in one environment_result object |
870 | */ |
829fa074 |
871 | function environment_check_database($version, $env_select) { |
f58b518f |
872 | |
f33e1ed4 |
873 | global $DB; |
f58b518f |
874 | |
875 | $result = new environment_results('database'); |
876 | |
877 | $vendors = array(); //Array of vendors in version |
878 | |
879 | /// Get the enviroment version we need |
829fa074 |
880 | if (!$data = get_environment_for_version($version, $env_select)) { |
f58b518f |
881 | /// Error. No version data found |
882 | $result->setStatus(false); |
883 | $result->setErrorCode(NO_VERSION_DATA_FOUND); |
884 | return $result; |
885 | } |
886 | |
887 | /// Extract the database part |
888 | if (!isset($data['#']['DATABASE'])) { |
889 | /// Error. No DATABASE section found |
890 | $result->setStatus(false); |
891 | $result->setErrorCode(NO_DATABASE_SECTION_FOUND); |
892 | return $result; |
893 | } else { |
894 | /// Extract level |
bac40536 |
895 | $level = get_level($data['#']['DATABASE']['0']); |
f58b518f |
896 | } |
897 | |
898 | /// Extract DB vendors. At least 2 are mandatory (mysql & postgres) |
899 | if (!isset($data['#']['DATABASE']['0']['#']['VENDOR'])) { |
900 | /// Error. No VENDORS found |
901 | $result->setStatus(false); |
902 | $result->setErrorCode(NO_DATABASE_VENDORS_FOUND); |
903 | return $result; |
904 | } else { |
905 | /// Extract vendors |
906 | foreach ($data['#']['DATABASE']['0']['#']['VENDOR'] as $vendor) { |
907 | if (isset($vendor['@']['name']) && isset($vendor['@']['version'])) { |
908 | $vendors[$vendor['@']['name']] = $vendor['@']['version']; |
9e2d15e5 |
909 | $vendorsxml[$vendor['@']['name']] = $vendor; |
f58b518f |
910 | } |
911 | } |
912 | } |
913 | /// Check we have the mysql vendor version |
914 | if (empty($vendors['mysql'])) { |
915 | $result->setStatus(false); |
916 | $result->setErrorCode(NO_DATABASE_VENDOR_MYSQL_FOUND); |
917 | return $result; |
918 | } |
919 | /// Check we have the postgres vendor version |
920 | if (empty($vendors['postgres'])) { |
921 | $result->setStatus(false); |
922 | $result->setErrorCode(NO_DATABASE_VENDOR_POSTGRES_FOUND); |
923 | return $result; |
924 | } |
925 | |
926 | /// Now search the version we are using (depending of vendor) |
f33e1ed4 |
927 | $current_vendor = $DB->get_dbfamily(); |
ed7656bf |
928 | |
f33e1ed4 |
929 | $dbinfo = $DB->get_server_info(); |
f58b518f |
930 | $current_version = normalize_version($dbinfo['version']); |
931 | $needed_version = $vendors[$current_vendor]; |
932 | |
e3058eb3 |
933 | /// Check we have a needed version |
934 | if (!$needed_version) { |
935 | $result->setStatus(false); |
936 | $result->setErrorCode(NO_DATABASE_VENDOR_VERSION_FOUND); |
937 | return $result; |
938 | } |
939 | |
f58b518f |
940 | /// And finally compare them, saving results |
941 | if (version_compare($current_version, $needed_version, '>=')) { |
942 | $result->setStatus(true); |
943 | } else { |
944 | $result->setStatus(false); |
f58b518f |
945 | } |
770fef0a |
946 | $result->setLevel($level); |
f58b518f |
947 | $result->setCurrentVersion($current_version); |
948 | $result->setNeededVersion($needed_version); |
949 | $result->setInfo($current_vendor); |
950 | |
bac40536 |
951 | /// Do any actions defined in the XML file. |
952 | process_environment_result($vendorsxml[$current_vendor], $result); |
9e2d15e5 |
953 | |
f58b518f |
954 | return $result; |
955 | |
956 | } |
957 | |
b0e2a189 |
958 | /** |
959 | * This function will post-process the result record by executing the specified |
960 | * function, modifying it as necessary, also a custom message will be added |
961 | * to the result object to be printed by the display layer. |
962 | * Every bypass function must be defined in this file and it'll return |
963 | * true/false to decide if the original test is bypassed or no. Also |
964 | * such bypass functions are able to directly handling the result object |
965 | * although it should be only under exceptional conditions. |
966 | * |
967 | * @param string xmldata containing the bypass data |
95a39282 |
968 | * @param object result object to be updated |
b0e2a189 |
969 | */ |
970 | function process_environment_bypass($xml, &$result) { |
971 | |
76bb0d20 |
972 | /// Only try to bypass if we were in error and it was required |
973 | if ($result->getStatus() || $result->getLevel() == 'optional') { |
b0e2a189 |
974 | return; |
975 | } |
976 | |
977 | /// It there is bypass info (function and message) |
74506a51 |
978 | if (is_array($xml['#']) && isset($xml['#']['BYPASS'][0]['@']['function']) && isset($xml['#']['BYPASS'][0]['@']['message'])) { |
b0e2a189 |
979 | $function = $xml['#']['BYPASS'][0]['@']['function']; |
980 | $message = $xml['#']['BYPASS'][0]['@']['message']; |
981 | /// Look for the function |
982 | if (function_exists($function)) { |
983 | /// Call it, and if bypass = true is returned, apply meesage |
984 | if ($function($result)) { |
985 | /// We only set the bypass message if the function itself hasn't defined it before |
986 | if (empty($result->getBypassStr)) { |
987 | $result->setBypassStr($message); |
988 | } |
989 | } |
990 | } |
991 | } |
992 | } |
993 | |
95a39282 |
994 | /** |
995 | * This function will post-process the result record by executing the specified |
996 | * function, modifying it as necessary, also a custom message will be added |
997 | * to the result object to be printed by the display layer. |
998 | * Every restrict function must be defined in this file and it'll return |
999 | * true/false to decide if the original test is restricted or no. Also |
1000 | * such restrict functions are able to directly handling the result object |
1001 | * although it should be only under exceptional conditions. |
1002 | * |
1003 | * @param string xmldata containing the restrict data |
1004 | * @param object result object to be updated |
1005 | */ |
1006 | function process_environment_restrict($xml, &$result) { |
1007 | |
1008 | /// Only try to restrict if we were not in error and it was required |
1009 | if (!$result->getStatus() || $result->getLevel() == 'optional') { |
1010 | return; |
1011 | } |
1012 | /// It there is restrict info (function and message) |
1013 | if (is_array($xml['#']) && isset($xml['#']['RESTRICT'][0]['@']['function']) && isset($xml['#']['RESTRICT'][0]['@']['message'])) { |
1014 | $function = $xml['#']['RESTRICT'][0]['@']['function']; |
1015 | $message = $xml['#']['RESTRICT'][0]['@']['message']; |
1016 | /// Look for the function |
1017 | if (function_exists($function)) { |
1018 | /// Call it, and if restrict = true is returned, apply meesage |
1019 | if ($function($result)) { |
1020 | /// We only set the restrict message if the function itself hasn't defined it before |
1021 | if (empty($result->getRestrictStr)) { |
1022 | $result->setRestrictStr($message); |
1023 | } |
1024 | } |
1025 | } |
1026 | } |
1027 | } |
1028 | |
9e2d15e5 |
1029 | /** |
1030 | * This function will detect if there is some message available to be added to the |
1031 | * result in order to clarify enviromental details. |
b0e2a189 |
1032 | * @param string xmldata containing the feedback data |
9e2d15e5 |
1033 | * @param object reult object to be updated |
1034 | */ |
1035 | function process_environment_messages($xml, &$result) { |
1036 | |
1037 | /// If there is feedback info |
74506a51 |
1038 | if (is_array($xml['#']) && isset($xml['#']['FEEDBACK'][0]['#'])) { |
9e2d15e5 |
1039 | $feedbackxml = $xml['#']['FEEDBACK'][0]['#']; |
1040 | |
1041 | if (!$result->status and $result->getLevel() == 'required') { |
1042 | if (isset($feedbackxml['ON_ERROR'][0]['@']['message'])) { |
1043 | $result->setFeedbackStr($feedbackxml['ON_ERROR'][0]['@']['message']); |
1044 | } |
1045 | } else if (!$result->status and $result->getLevel() == 'optional') { |
1046 | if (isset($feedbackxml['ON_CHECK'][0]['@']['message'])) { |
1047 | $result->setFeedbackStr($feedbackxml['ON_CHECK'][0]['@']['message']); |
1048 | } |
1049 | } else { |
1050 | if (isset($feedbackxml['ON_OK'][0]['@']['message'])) { |
1051 | $result->setFeedbackStr($feedbackxml['ON_OK'][0]['@']['message']); |
1052 | } |
1053 | } |
1054 | } |
1055 | } |
1056 | |
f58b518f |
1057 | |
1058 | //--- Helper Class to return results to caller ---// |
1059 | |
1060 | |
770fef0a |
1061 | /** |
f58b518f |
1062 | * This class is used to return the results of the environment |
1063 | * main functions (environment_check_xxxx) |
1064 | */ |
1065 | class environment_results { |
1066 | |
c808379e |
1067 | var $part; //which are we checking (database, php, php_extension, php_extension) |
f58b518f |
1068 | var $status; //true/false |
1069 | var $error_code; //integer. See constants at the beginning of the file |
1070 | var $level; //required/optional |
1071 | var $current_version; //current version detected |
1072 | var $needed_version; //version needed |
1073 | var $info; //Aux. info (DB vendor, library...) |
9e2d15e5 |
1074 | var $feedback_str; //String to show on error|on check|on ok |
1075 | var $bypass_str; //String to show if some bypass has happened |
95a39282 |
1076 | var $restrict_str; //String to show if some restrict has happened |
f58b518f |
1077 | |
1078 | /** |
1079 | * Constructor of the environment_result class. Just set default values |
1080 | */ |
1081 | function environment_results($part) { |
1082 | $this->part=$part; |
1083 | $this->status=false; |
049c0f4a |
1084 | $this->error_code=NO_ERROR; |
f58b518f |
1085 | $this->level='required'; |
1086 | $this->current_version=''; |
1087 | $this->needed_version=''; |
1088 | $this->info=''; |
9e2d15e5 |
1089 | $this->feedback_str=''; |
1090 | $this->bypass_str=''; |
95a39282 |
1091 | $this->restrict_str=''; |
f58b518f |
1092 | } |
1093 | |
1094 | /** |
1095 | * Set the status |
1096 | * @param boolean the status (true/false) |
1097 | */ |
1098 | function setStatus($status) { |
1099 | $this->status=$status; |
1100 | if ($status) { |
1101 | $this->setErrorCode(NO_ERROR); |
1102 | } |
1103 | } |
1104 | |
1105 | /** |
1106 | * Set the error_code |
1107 | * @param integer the error code (see constants above) |
1108 | */ |
1109 | function setErrorCode($error_code) { |
1110 | $this->error_code=$error_code; |
1111 | } |
1112 | |
1113 | /** |
1114 | * Set the level |
1115 | * @param string the level (required, optional) |
1116 | */ |
1117 | function setLevel($level) { |
1118 | $this->level=$level; |
1119 | } |
1120 | |
1121 | /** |
1122 | * Set the current version |
1123 | * @param string the current version |
1124 | */ |
1125 | function setCurrentVersion($current_version) { |
1126 | $this->current_version=$current_version; |
1127 | } |
1128 | |
1129 | /** |
1130 | * Set the needed version |
1131 | * @param string the needed version |
1132 | */ |
1133 | function setNeededVersion($needed_version) { |
1134 | $this->needed_version=$needed_version; |
1135 | } |
1136 | |
1137 | /** |
1138 | * Set the auxiliary info |
1139 | * @param string the auxiliary info |
1140 | */ |
9e2d15e5 |
1141 | function setInfo($info) { |
1142 | $this->info=$info; |
1143 | } |
770fef0a |
1144 | |
9e2d15e5 |
1145 | /** |
1146 | * Set the feedback string |
afb36bca |
1147 | * @param mixed the feedback string that will be fetched from the admin lang file. |
1148 | * pass just the string or pass an array of params for get_string |
1149 | * You always should put your string in admin.php but a third param is useful |
1150 | * to pass an $a object / string to get_string |
9e2d15e5 |
1151 | */ |
1152 | function setFeedbackStr($str) { |
1153 | $this->feedback_str=$str; |
1154 | } |
f58b518f |
1155 | |
afb36bca |
1156 | |
b0e2a189 |
1157 | /** |
1158 | * Set the bypass string |
afb36bca |
1159 | * @param string the bypass string that will be fetched from the admin lang file. |
1160 | * pass just the string or pass an array of params for get_string |
1161 | * You always should put your string in admin.php but a third param is useful |
1162 | * to pass an $a object / string to get_string |
b0e2a189 |
1163 | */ |
1164 | function setBypassStr($str) { |
1165 | $this->bypass_str=$str; |
1166 | } |
1167 | |
95a39282 |
1168 | /** |
1169 | * Set the restrict string |
afb36bca |
1170 | * @param string the restrict string that will be fetched from the admin lang file. |
1171 | * pass just the string or pass an array of params for get_string |
1172 | * You always should put your string in admin.php but a third param is useful |
1173 | * to pass an $a object / string to get_string |
95a39282 |
1174 | */ |
1175 | function setRestrictStr($str) { |
1176 | $this->restrict_str=$str; |
1177 | } |
1178 | |
f58b518f |
1179 | /** |
1180 | * Get the status |
1181 | * @return boolean result |
1182 | */ |
1183 | function getStatus() { |
1184 | return $this->status; |
1185 | } |
1186 | |
1187 | /** |
1188 | * Get the error code |
1189 | * @return integer error code |
1190 | */ |
1191 | function getErrorCode() { |
1192 | return $this->error_code; |
1193 | } |
1194 | |
1195 | /** |
1196 | * Get the level |
1197 | * @return string level |
1198 | */ |
1199 | function getLevel() { |
1200 | return $this->level; |
1201 | } |
1202 | |
1203 | /** |
770fef0a |
1204 | * Get the current version |
f58b518f |
1205 | * @return string current version |
1206 | */ |
1207 | function getCurrentVersion() { |
1208 | return $this->current_version; |
1209 | } |
1210 | |
1211 | /** |
1212 | * Get the needed version |
1213 | * @return string needed version |
1214 | */ |
1215 | function getNeededVersion() { |
1216 | return $this->needed_version; |
1217 | } |
1218 | |
1219 | /** |
1220 | * Get the aux info |
1221 | * @return string info |
1222 | */ |
1223 | function getInfo() { |
1224 | return $this->info; |
1225 | } |
1226 | |
1227 | /** |
1228 | * Get the part this result belongs to |
1229 | * @return string part |
1230 | */ |
1231 | function getPart() { |
1232 | return $this->part; |
1233 | } |
9e2d15e5 |
1234 | |
1235 | /** |
1236 | * Get the feedback string |
afb36bca |
1237 | * @return mixed feedback string (can be an array of params for get_string or a single string to fetch from |
1238 | * admin.php lang file). |
9e2d15e5 |
1239 | */ |
1240 | function getFeedbackStr() { |
1241 | return $this->feedback_str; |
1242 | } |
b0e2a189 |
1243 | |
1244 | /** |
1245 | * Get the bypass string |
afb36bca |
1246 | * @return mixed bypass string (can be an array of params for get_string or a single string to fetch from |
1247 | * admin.php lang file). |
b0e2a189 |
1248 | */ |
1249 | function getBypassStr() { |
1250 | return $this->bypass_str; |
1251 | } |
95a39282 |
1252 | |
1253 | /** |
1254 | * Get the restrict string |
afb36bca |
1255 | * @return mixed restrict string (can be an array of params for get_string or a single string to fetch from |
1256 | * admin.php lang file). |
95a39282 |
1257 | */ |
1258 | function getRestrictStr() { |
1259 | return $this->restrict_str; |
1260 | } |
afb36bca |
1261 | |
1262 | /** |
1263 | * @param mixed $string params for get_string, either a string to fetch from admin.php or an array of |
1264 | * params for get_string. |
1265 | * @param string $class css class(es) for message. |
1266 | * @return string feedback string fetched from lang file wrapped in p tag with class $class or returns |
1267 | * empty string if $string is empty. |
1268 | */ |
1269 | function strToReport($string, $class){ |
1270 | if (!empty($string)){ |
1271 | if (is_array($string)){ |
1272 | $str = call_user_func_array('get_string', $string); |
1273 | } else { |
1274 | $str = get_string($string, 'admin'); |
1275 | } |
1276 | return '<p class="'.$class.'">'.$str.'</p>'; |
1277 | } else { |
1278 | return ''; |
1279 | } |
1280 | } |
f58b518f |
1281 | } |
1282 | |
9e2d15e5 |
1283 | /// Here all the bypass functions are coded to be used by the environment |
1284 | /// checker. All those functions will receive the result object and will |
1285 | /// return it modified as needed (status and bypass string) |
1286 | |
b0e2a189 |
1287 | /** |
1288 | * This function will bypass MySQL 4.1.16 reqs if: |
1289 | * - We are using MySQL > 4.1.12, informing about problems with non latin chars in the future |
1290 | * |
1291 | * @param object result object to handle |
95a39282 |
1292 | * @return boolean true/false to determinate if the bypass has to be performed (true) or no (false) |
b0e2a189 |
1293 | */ |
1294 | function bypass_mysql416_reqs ($result) { |
1295 | /// See if we are running MySQL >= 4.1.12 |
1296 | if (version_compare($result->getCurrentVersion(), '4.1.12', '>=')) { |
1297 | return true; |
1298 | } |
1299 | |
1300 | return false; |
1301 | } |
1302 | |
95a39282 |
1303 | /// Here all the restrict functions are coded to be used by the environment |
1304 | /// checker. All those functions will receive the result object and will |
1305 | /// return it modified as needed (status and bypass string) |
1306 | |
770fef0a |
1307 | /** |
95a39282 |
1308 | * This function will restrict PHP reqs if: |
1309 | * - We are using PHP 5.0.x, informing about the buggy version |
1310 | * |
1311 | * @param object result object to handle |
1312 | * @return boolean true/false to determinate if the restrict has to be performed (true) or no (false) |
1313 | */ |
1314 | function restrict_php50_version($result) { |
1315 | if (version_compare($result->getCurrentVersion(), '5.0.0', '>=') |
1316 | and version_compare($result->getCurrentVersion(), '5.0.99', '<')) { |
1317 | return true; |
1318 | } |
1319 | return false; |
1320 | } |
bac40536 |
1321 | |
1322 | /** |
1323 | * @param array $element the element from the environment.xml file that should have |
1324 | * either a level="required" or level="optional" attribute. |
1325 | * @read string "required" or "optional". |
1326 | */ |
1327 | function get_level($element) { |
1328 | $level = 'required'; |
1329 | if (isset($element['@']['level'])) { |
1330 | $level = $element['@']['level']; |
1331 | if (!in_array($level, array('required', 'optional'))) { |
7f2d3ec0 |
1332 | debugging('The level of a check in the environment.xml file must be "required" or "optional".', DEBUG_DEVELOPER); |
bac40536 |
1333 | $level = 'required'; |
1334 | } |
1335 | } else { |
1336 | debugging('Checks in the environment.xml file must have a level="required" or level="optional" attribute.', DEBUG_DEVELOPER); |
1337 | } |
1338 | return $level; |
1339 | } |
1340 | |
1341 | /** |
1342 | * Once the result has been determined, look in the XML for any |
1343 | * messages, or other things that should be done depending on the outcome. |
1344 | * @param array $element the element from the environment.xml file which |
1345 | * may have children defining what should be done with the outcome. |
1346 | * @param object $result the result of the test, which may be modified by |
1347 | * this function as specified in the XML. |
1348 | */ |
1349 | function process_environment_result($element, &$result) { |
1350 | /// Process messages, modifying the $result if needed. |
1351 | process_environment_messages($element, $result); |
1352 | /// Process bypass, modifying $result if needed. |
1353 | process_environment_bypass($element, $result); |
1354 | /// Process restrict, modifying $result if needed. |
1355 | process_environment_restrict($element, $result); |
1356 | } |
f58b518f |
1357 | ?> |