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 | // // |
10 | // Copyright (C) 2001-3001 Martin Dougiamas http://dougiamas.com // |
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); |
f58b518f |
49 | |
50 | /** |
51 | * This function will perform the whole check, returning |
52 | * true or false as final result. Also, he full array of |
53 | * environment_result will be returned in the parameter list. |
54 | * The function looks for the best version to compare and |
55 | * everything. This is the only function that should be called |
56 | * ever from the rest of Moodle. |
57 | * @param string version version to check. |
58 | * @param array results array of results checked. |
59 | * @return boolean true/false, depending of results |
60 | */ |
049c0f4a |
61 | function check_moodle_environment($version, &$environment_results, $print_table=true) { |
62 | |
63 | $status = true; |
f58b518f |
64 | |
878d309c |
65 | /// This are cached per request |
66 | static $result = true; |
67 | static $env_results; |
68 | static $cache_exists = false; |
69 | |
70 | /// if we have results cached, use them |
71 | if ($cache_exists) { |
72 | $environment_results = $env_results; |
73 | /// No cache exists, calculate everything |
74 | } else { |
75 | /// Get the more recent version before the requested |
76 | if (!$version = get_latest_version_available($version)) { |
77 | $status = false; |
78 | } |
f58b518f |
79 | |
878d309c |
80 | /// Perform all the checks |
81 | if (!($environment_results = environment_check($version)) && $status) { |
82 | $status = false; |
83 | } |
f58b518f |
84 | |
878d309c |
85 | /// Iterate over all the results looking for some error in required items |
86 | /// or some error_code |
87 | if ($status) { |
88 | foreach ($environment_results as $environment_result) { |
89 | if ((!$environment_result->getStatus() && |
b0e2a189 |
90 | $environment_result->getLevel() == 'required') && !$environment_result->getBypassStr() || |
878d309c |
91 | $environment_result->getErrorCode()) { |
92 | $result = false; |
93 | } |
049c0f4a |
94 | } |
f58b518f |
95 | } |
878d309c |
96 | /// Going to end, we store environment_results to cache |
97 | $env_results = $environment_results; |
98 | $cache_exists = true; |
99 | } ///End of cache block |
f58b518f |
100 | |
049c0f4a |
101 | /// If we have decided to print all the information, just do it |
102 | if ($print_table) { |
e909788d |
103 | print_moodle_environment($result && $status, $environment_results); |
049c0f4a |
104 | } |
105 | |
106 | return ($result && $status); |
107 | } |
108 | |
109 | /** |
110 | * This function will print one beautiful table with all the environmental |
111 | * configuration and how it suits Moodle needs. |
112 | * @param boolean final result of the check (true/false) |
113 | * @param array environment_results array of results gathered |
114 | */ |
115 | function print_moodle_environment($result, $environment_results) { |
116 | |
117 | /// Get some strings |
118 | $strname = get_string('name'); |
119 | $strinfo = get_string('info'); |
120 | $strreport = get_string('report'); |
121 | $strstatus = get_string('status'); |
122 | $strok = get_string('ok'); |
123 | $strerror = get_string('error'); |
124 | $strcheck = get_string('check'); |
b0e2a189 |
125 | $strbypassed = get_string('bypassed'); |
e909788d |
126 | $strenvironmenterrortodo = get_string('environmenterrortodo', 'admin'); |
049c0f4a |
127 | |
9e2d15e5 |
128 | /// Here we'll store all the feedback found |
129 | $feedbacktext = ''; |
130 | |
049c0f4a |
131 | /// Table header |
132 | $table->head = array ($strname, $strinfo, $strreport, $strstatus); |
133 | $table->align = array ('center', 'center', 'left', 'center'); |
134 | $table->wrap = array ('nowrap', '', '', 'nowrap'); |
135 | $table->size = array ('10', 10, '100%', '10'); |
136 | $table->width = '90%'; |
edc13d62 |
137 | $table->class = 'environmenttable generaltable'; |
049c0f4a |
138 | |
139 | /// Iterate over each environment_result |
140 | $continue = true; |
141 | foreach ($environment_results as $environment_result) { |
142 | $errorline = false; |
143 | if ($continue) { |
144 | $type = $environment_result->getPart(); |
145 | $info = $environment_result->getInfo(); |
146 | $status = $environment_result->getStatus(); |
147 | $error_code = $environment_result->getErrorCode(); |
148 | /// Process Report field |
878d309c |
149 | $rec = new stdClass(); |
049c0f4a |
150 | /// Something has gone wrong at parsing time |
151 | if ($error_code) { |
152 | $stringtouse = 'environmentxmlerror'; |
153 | $rec->error_code = $error_code; |
154 | $status = $strerror; |
155 | $errorline = true; |
156 | $continue = false; |
157 | } |
158 | |
159 | if ($continue) { |
160 | /// We are comparing versions |
161 | if ($rec->needed = $environment_result->getNeededVersion()) { |
162 | $rec->current = $environment_result->getCurrentVersion(); |
163 | if ($environment_result->getLevel() == 'required') { |
164 | $stringtouse = 'environmentrequireversion'; |
165 | } else { |
166 | $stringtouse = 'environmentrecommendversion'; |
167 | } |
168 | /// We are checking installed & enabled things |
169 | } else { |
170 | if ($environment_result->getLevel() == 'required') { |
171 | $stringtouse = 'environmentrequireinstall'; |
172 | } else { |
173 | $stringtouse = 'environmentrecommendinstall'; |
174 | } |
175 | } |
176 | /// Calculate the status value |
b0e2a189 |
177 | if ($environment_result->getBypassStr() == '') { |
178 | if (!$status and $environment_result->getLevel() == 'required') { |
179 | $status = $strerror; |
180 | $errorline = true; |
181 | } else if (!$status && $environment_result->getLevel() == 'optional') { |
182 | $status = $strcheck; |
183 | } else { |
184 | $status = $strok; |
185 | } |
049c0f4a |
186 | } else { |
b0e2a189 |
187 | $status = $strbypassed; |
188 | $errorline = true; |
049c0f4a |
189 | } |
190 | } |
191 | |
192 | /// Build the text |
193 | $report = get_string($stringtouse, 'admin', $rec); |
194 | /// Format error line |
195 | if ($errorline) { |
196 | $type = '<span class="error">'.$type.'</span>'; |
197 | $info = '<span class="error">'.$info.'</span>'; |
198 | $report = '<span class="error">'.$report.'</span>'; |
199 | $status = '<span class="error">'.$status.'</span>'; |
200 | } |
201 | /// Add the row to the table |
202 | $table->data[] = array ($type, $info, $report, $status); |
9e2d15e5 |
203 | ///Process the feedback if necessary |
204 | if ($feedbackstr = $environment_result->getFeedbackStr()) { |
205 | $feedbacktext .= '<li class="environmenttable">'.get_string($feedbackstr, 'admin').'</li>'; |
206 | } |
b0e2a189 |
207 | ///Process the bypass if necessary |
208 | if ($bypassstr = $environment_result->getBypassStr()) { |
209 | $feedbacktext .= '<li class="environmenttable">'.get_string($bypassstr, 'admin').'</li>'; |
210 | } |
049c0f4a |
211 | } |
212 | } |
213 | |
214 | /// Print table |
215 | print_table($table); |
e909788d |
216 | |
9e2d15e5 |
217 | /// And feedback accumulated text |
218 | if ($feedbacktext) { |
219 | print_simple_box('<ul>'.$feedbacktext.'</ul>', 'center', '90%'); |
220 | } |
221 | |
e909788d |
222 | /// Finally, if any error has happened, print the summary box |
223 | if (!$result) { |
224 | print_simple_box($strenvironmenterrortodo, 'center', '', '', '', 'errorbox'); |
225 | } |
f58b518f |
226 | } |
227 | |
228 | |
229 | /** |
230 | * This function will normalize any version to just a serie of numbers |
231 | * separated by dots. Everything else will be removed. |
232 | * @param string $version the original version |
233 | * @return string the normalized version |
234 | */ |
235 | function normalize_version($version) { |
236 | /// Replace everything but numbers and dots by dots |
237 | $version = preg_replace('/[^\.\d]/', '.', $version); |
238 | /// Combine multiple dots in one |
239 | $version = preg_replace('/(\.{2,})/', '.', $version); |
240 | /// Trim possible leading and trailing dots |
241 | $version = trim($version, '.'); |
242 | |
243 | return $version; |
244 | } |
245 | |
246 | |
247 | /** |
248 | * This function will load the environment.xml file and xmlize it |
249 | * @return mixed the xmlized structure or false on error |
250 | */ |
251 | function load_environment_xml() { |
252 | |
253 | global $CFG; |
254 | |
255 | static $data; //Only load and xmlize once by request |
256 | |
257 | if (!empty($data)) { |
258 | return $data; |
259 | } |
260 | |
00d3a0fd |
261 | /// First of all, take a look inside $CFG->dataroot/environment/environment.xml |
262 | $file = $CFG->dataroot.'/environment/environment.xml'; |
f58b518f |
263 | if (!is_file($file) || !is_readable($file) || !$contents = file_get_contents($file)) { |
d83f8373 |
264 | /// Fallback to fixed $CFG->admin/environment.xml |
265 | $file = $CFG->dirroot.'/'.$CFG->admin.'/environment.xml'; |
f58b518f |
266 | if (!is_file($file) || !is_readable($file) || !$contents = file_get_contents($file)) { |
267 | return false; |
268 | } |
269 | } |
270 | /// XML the whole file |
271 | $data = xmlize($contents); |
272 | |
273 | return $data; |
274 | } |
275 | |
276 | |
277 | /** |
278 | * This function will return the list of Moodle versions available |
279 | * @return mixed array of versions. False on error. |
280 | */ |
281 | function get_list_of_environment_versions ($contents) { |
282 | |
283 | static $versions = array(); |
284 | |
285 | if (!empty($versions)) { |
286 | return $versions; |
287 | } |
288 | |
289 | if (isset($contents['COMPATIBILITY_MATRIX']['#']['MOODLE'])) { |
290 | foreach ($contents['COMPATIBILITY_MATRIX']['#']['MOODLE'] as $version) { |
291 | $versions[] = $version['@']['version']; |
292 | } |
293 | } |
294 | |
295 | return $versions; |
296 | } |
297 | |
298 | |
299 | /** |
300 | * This function will return the most recent version in the environment.xml |
301 | * file previous or equal to the version requested |
302 | * @param string version top version from which we start to look backwards |
303 | * @return string more recent version or false if not found |
304 | */ |
305 | function get_latest_version_available ($version) { |
306 | |
307 | /// Normalize the version requested |
308 | $version = normalize_version($version); |
309 | |
310 | /// Load xml file |
311 | if (!$contents = load_environment_xml()) { |
312 | return false; |
313 | } |
314 | |
315 | /// Detect available versions |
316 | if (!$versions = get_list_of_environment_versions($contents)) { |
317 | return false; |
318 | } |
319 | /// First we look for exact version |
320 | if (in_array($version, $versions)) { |
321 | return $version; |
322 | } else { |
323 | $found_version = false; |
324 | /// Not exact match, so we are going to iterate over the list searching |
325 | /// for the latest version before the requested one |
326 | foreach ($versions as $arrversion) { |
327 | if (version_compare($arrversion, $version, '<')) { |
328 | $found_version = $arrversion; |
329 | } |
330 | } |
331 | } |
332 | |
333 | return $found_version; |
334 | } |
335 | |
336 | |
337 | /** |
338 | * This function will return the xmlized data belonging to one Moodle version |
339 | * @return mixed the xmlized structure or false on error |
340 | */ |
341 | function get_environment_for_version($version) { |
342 | |
343 | /// Normalize the version requested |
344 | $version = normalize_version($version); |
345 | |
346 | /// Load xml file |
347 | if (!$contents = load_environment_xml()) { |
348 | return false; |
349 | } |
350 | |
351 | /// Detect available versions |
352 | if (!$versions = get_list_of_environment_versions($contents)) { |
353 | return false; |
354 | } |
355 | |
356 | /// If the version requested is available |
357 | if (!in_array($version, $versions)) { |
358 | return false; |
359 | } |
360 | |
361 | /// We now we have it. Extract from full contents. |
362 | $fl_arr = array_flip($versions); |
363 | |
364 | return $contents['COMPATIBILITY_MATRIX']['#']['MOODLE'][$fl_arr[$version]]; |
365 | } |
366 | |
367 | |
368 | /** |
369 | * This function will check for everything (DB, PHP and PHP extensions for now) |
370 | * returning an array of environment_result objects. |
371 | * @param string $version xml version we are going to use to test this server |
372 | * @return array array of results encapsulated in one environment_result object |
373 | */ |
374 | function environment_check($version) { |
375 | |
376 | /// Normalize the version requested |
377 | $version = normalize_version($version); |
378 | |
379 | $results = array(); //To store all the results |
380 | |
381 | $results[] = environment_check_database($version); |
382 | $results[] = environment_check_php($version); |
383 | |
384 | $phpext_results = environment_check_php_extensions($version); |
385 | |
386 | $results = array_merge ($results, $phpext_results); |
387 | |
388 | return $results; |
389 | } |
390 | |
391 | |
392 | /** |
393 | * This function will check if php extensions requirements are satisfied |
394 | * @param string $version xml version we are going to use to test this server |
395 | * @return array array of results encapsulated in one environment_result object |
396 | */ |
397 | function environment_check_php_extensions($version) { |
398 | |
399 | $results = array(); |
400 | |
401 | /// Get the enviroment version we need |
402 | if (!$data = get_environment_for_version($version)) { |
403 | /// Error. No version data found |
049c0f4a |
404 | $result = new environment_results('php_extension'); |
f58b518f |
405 | $result->setStatus(false); |
406 | $result->setErrorCode(NO_VERSION_DATA_FOUND); |
407 | return $result; |
408 | } |
409 | |
410 | /// Extract the php_extension part |
411 | if (!isset($data['#']['PHP_EXTENSIONS']['0']['#']['PHP_EXTENSION'])) { |
412 | /// Error. No PHP section found |
049c0f4a |
413 | $result = new environment_results('php_extension'); |
f58b518f |
414 | $result->setStatus(false); |
415 | $result->setErrorCode(NO_PHP_EXTENSIONS_SECTION_FOUND); |
416 | return $result; |
9e2d15e5 |
417 | } |
418 | /// Iterate over extensions checking them and creating the needed environment_results |
419 | foreach($data['#']['PHP_EXTENSIONS']['0']['#']['PHP_EXTENSION'] as $extension) { |
420 | $result = new environment_results('php_extension'); |
421 | /// Check for level |
422 | if (isset($extension['@']['level'])) { |
423 | $level = $extension['@']['level']; |
424 | if ($level != 'optional') { |
425 | $level = 'required'; |
f58b518f |
426 | } |
9e2d15e5 |
427 | } |
428 | /// Check for extension name |
429 | if (!isset($extension['@']['name'])) { |
430 | $result->setStatus(false); |
431 | $result->setErrorCode(NO_PHP_EXTENSIONS_NAME_FOUND); |
432 | } else { |
433 | $extension_name = $extension['@']['name']; |
434 | /// The name exists. Just check if it's an installed extension |
435 | if (!extension_loaded($extension_name)) { |
f58b518f |
436 | $result->setStatus(false); |
f58b518f |
437 | } else { |
9e2d15e5 |
438 | $result->setStatus(true); |
f58b518f |
439 | } |
9e2d15e5 |
440 | $result->setLevel($level); |
441 | $result->setInfo($extension_name); |
f58b518f |
442 | } |
9e2d15e5 |
443 | /// Process messages, modifying the $result if needed. |
444 | process_environment_messages($extension, $result); |
b0e2a189 |
445 | /// Process bypass, modifying $result if needed. |
446 | process_environment_bypass($extension, $result); |
447 | |
9e2d15e5 |
448 | /// Add the result to the array of results |
449 | $results[] = $result; |
f58b518f |
450 | } |
451 | |
9e2d15e5 |
452 | |
f58b518f |
453 | return $results; |
454 | } |
455 | |
456 | |
457 | /** |
458 | * This function will check if php requirements are satisfied |
459 | * @param string $version xml version we are going to use to test this server |
460 | * @return object results encapsulated in one environment_result object |
461 | */ |
462 | function environment_check_php($version) { |
463 | |
464 | $result = new environment_results('php'); |
465 | |
466 | /// Get the enviroment version we need |
467 | if (!$data = get_environment_for_version($version)) { |
468 | /// Error. No version data found |
469 | $result->setStatus(false); |
470 | $result->setErrorCode(NO_VERSION_DATA_FOUND); |
471 | return $result; |
472 | } |
473 | |
474 | /// Extract the php part |
475 | if (!isset($data['#']['PHP'])) { |
476 | /// Error. No PHP section found |
477 | $result->setStatus(false); |
478 | $result->setErrorCode(NO_PHP_SECTION_FOUND); |
479 | return $result; |
480 | } else { |
481 | /// Extract level and version |
482 | if (isset($data['#']['PHP']['0']['@']['level'])) { |
00d3a0fd |
483 | $level = $data['#']['PHP']['0']['@']['level']; |
f58b518f |
484 | if ($level != 'optional') { |
485 | $level = 'required'; |
486 | } |
487 | } |
488 | if (!isset($data['#']['PHP']['0']['@']['version'])) { |
489 | $result->setStatus(false); |
490 | $result->setErrorCode(NO_PHP_VERSION_FOUND); |
491 | return $result; |
492 | } else { |
493 | $needed_version = $data['#']['PHP']['0']['@']['version']; |
494 | } |
495 | } |
496 | |
497 | /// Now search the version we are using |
498 | $current_version = normalize_version(phpversion()); |
499 | |
500 | /// And finally compare them, saving results |
501 | if (version_compare($current_version, $needed_version, '>=')) { |
502 | $result->setStatus(true); |
503 | } else { |
504 | $result->setStatus(false); |
f58b518f |
505 | } |
506 | $result->setLevel($level); |
507 | $result->setCurrentVersion($current_version); |
508 | $result->setNeededVersion($needed_version); |
9e2d15e5 |
509 | /// Process messages, modifying the $result if needed. |
510 | process_environment_messages($data['#']['PHP'][0], $result); |
b0e2a189 |
511 | /// Process bypass, modifying $result if needed. |
512 | process_environment_bypass($data['#']['PHP'][0], $result); |
f58b518f |
513 | |
514 | return $result; |
515 | } |
516 | |
517 | |
518 | /** |
519 | * This function will check if database requirements are satisfied |
520 | * @param string $version xml version we are going to use to test this server |
521 | * @return object results encapsulated in one environment_result object |
522 | */ |
523 | function environment_check_database($version) { |
524 | |
525 | global $db; |
526 | |
527 | $result = new environment_results('database'); |
528 | |
529 | $vendors = array(); //Array of vendors in version |
530 | |
531 | /// Get the enviroment version we need |
532 | if (!$data = get_environment_for_version($version)) { |
533 | /// Error. No version data found |
534 | $result->setStatus(false); |
535 | $result->setErrorCode(NO_VERSION_DATA_FOUND); |
536 | return $result; |
537 | } |
538 | |
539 | /// Extract the database part |
540 | if (!isset($data['#']['DATABASE'])) { |
541 | /// Error. No DATABASE section found |
542 | $result->setStatus(false); |
543 | $result->setErrorCode(NO_DATABASE_SECTION_FOUND); |
544 | return $result; |
545 | } else { |
546 | /// Extract level |
547 | if (isset($data['#']['DATABASE']['0']['@']['level'])) { |
00d3a0fd |
548 | $level = $data['#']['DATABASE']['0']['@']['level']; |
f58b518f |
549 | if ($level != 'optional') { |
550 | $level = 'required'; |
551 | } |
552 | } |
553 | } |
554 | |
555 | /// Extract DB vendors. At least 2 are mandatory (mysql & postgres) |
556 | if (!isset($data['#']['DATABASE']['0']['#']['VENDOR'])) { |
557 | /// Error. No VENDORS found |
558 | $result->setStatus(false); |
559 | $result->setErrorCode(NO_DATABASE_VENDORS_FOUND); |
560 | return $result; |
561 | } else { |
562 | /// Extract vendors |
563 | foreach ($data['#']['DATABASE']['0']['#']['VENDOR'] as $vendor) { |
564 | if (isset($vendor['@']['name']) && isset($vendor['@']['version'])) { |
565 | $vendors[$vendor['@']['name']] = $vendor['@']['version']; |
9e2d15e5 |
566 | $vendorsxml[$vendor['@']['name']] = $vendor; |
f58b518f |
567 | } |
568 | } |
569 | } |
570 | /// Check we have the mysql vendor version |
571 | if (empty($vendors['mysql'])) { |
572 | $result->setStatus(false); |
573 | $result->setErrorCode(NO_DATABASE_VENDOR_MYSQL_FOUND); |
574 | return $result; |
575 | } |
576 | /// Check we have the postgres vendor version |
577 | if (empty($vendors['postgres'])) { |
578 | $result->setStatus(false); |
579 | $result->setErrorCode(NO_DATABASE_VENDOR_POSTGRES_FOUND); |
580 | return $result; |
581 | } |
582 | |
583 | /// Now search the version we are using (depending of vendor) |
584 | $current_vendor = $db->databaseType; |
e3058eb3 |
585 | if ($current_vendor == 'postgres7') { //Normalize a bit postgresql |
586 | $current_vendor ='postgres'; |
587 | } |
cfba5440 |
588 | if ($current_vendor == 'oci8po') { //Normalize a bit oracle |
589 | $current_vendor ='oracle'; |
590 | } |
f58b518f |
591 | $dbinfo = $db->ServerInfo(); |
592 | $current_version = normalize_version($dbinfo['version']); |
593 | $needed_version = $vendors[$current_vendor]; |
594 | |
e3058eb3 |
595 | /// Check we have a needed version |
596 | if (!$needed_version) { |
597 | $result->setStatus(false); |
598 | $result->setErrorCode(NO_DATABASE_VENDOR_VERSION_FOUND); |
599 | return $result; |
600 | } |
601 | |
f58b518f |
602 | /// And finally compare them, saving results |
603 | if (version_compare($current_version, $needed_version, '>=')) { |
604 | $result->setStatus(true); |
605 | } else { |
606 | $result->setStatus(false); |
f58b518f |
607 | } |
608 | $result->setLevel($level); |
609 | $result->setCurrentVersion($current_version); |
610 | $result->setNeededVersion($needed_version); |
611 | $result->setInfo($current_vendor); |
612 | |
9e2d15e5 |
613 | /// Process messages, modifying the $result if needed. |
614 | process_environment_messages($vendorsxml[$current_vendor], $result); |
b0e2a189 |
615 | /// Process bypass, modifying $result if needed. |
616 | process_environment_bypass($vendorsxml[$current_vendor], $result); |
9e2d15e5 |
617 | |
f58b518f |
618 | return $result; |
619 | |
620 | } |
621 | |
b0e2a189 |
622 | /** |
623 | * This function will post-process the result record by executing the specified |
624 | * function, modifying it as necessary, also a custom message will be added |
625 | * to the result object to be printed by the display layer. |
626 | * Every bypass function must be defined in this file and it'll return |
627 | * true/false to decide if the original test is bypassed or no. Also |
628 | * such bypass functions are able to directly handling the result object |
629 | * although it should be only under exceptional conditions. |
630 | * |
631 | * @param string xmldata containing the bypass data |
632 | * @param object reult object to be updated |
633 | */ |
634 | function process_environment_bypass($xml, &$result) { |
635 | |
76bb0d20 |
636 | /// Only try to bypass if we were in error and it was required |
637 | if ($result->getStatus() || $result->getLevel() == 'optional') { |
b0e2a189 |
638 | return; |
639 | } |
640 | |
641 | /// It there is bypass info (function and message) |
74506a51 |
642 | if (is_array($xml['#']) && isset($xml['#']['BYPASS'][0]['@']['function']) && isset($xml['#']['BYPASS'][0]['@']['message'])) { |
b0e2a189 |
643 | $function = $xml['#']['BYPASS'][0]['@']['function']; |
644 | $message = $xml['#']['BYPASS'][0]['@']['message']; |
645 | /// Look for the function |
646 | if (function_exists($function)) { |
647 | /// Call it, and if bypass = true is returned, apply meesage |
648 | if ($function($result)) { |
649 | /// We only set the bypass message if the function itself hasn't defined it before |
650 | if (empty($result->getBypassStr)) { |
651 | $result->setBypassStr($message); |
652 | } |
653 | } |
654 | } |
655 | } |
656 | } |
657 | |
9e2d15e5 |
658 | /** |
659 | * This function will detect if there is some message available to be added to the |
660 | * result in order to clarify enviromental details. |
b0e2a189 |
661 | * @param string xmldata containing the feedback data |
9e2d15e5 |
662 | * @param object reult object to be updated |
663 | */ |
664 | function process_environment_messages($xml, &$result) { |
665 | |
666 | /// If there is feedback info |
74506a51 |
667 | if (is_array($xml['#']) && isset($xml['#']['FEEDBACK'][0]['#'])) { |
9e2d15e5 |
668 | $feedbackxml = $xml['#']['FEEDBACK'][0]['#']; |
669 | |
670 | if (!$result->status and $result->getLevel() == 'required') { |
671 | if (isset($feedbackxml['ON_ERROR'][0]['@']['message'])) { |
672 | $result->setFeedbackStr($feedbackxml['ON_ERROR'][0]['@']['message']); |
673 | } |
674 | } else if (!$result->status and $result->getLevel() == 'optional') { |
675 | if (isset($feedbackxml['ON_CHECK'][0]['@']['message'])) { |
676 | $result->setFeedbackStr($feedbackxml['ON_CHECK'][0]['@']['message']); |
677 | } |
678 | } else { |
679 | if (isset($feedbackxml['ON_OK'][0]['@']['message'])) { |
680 | $result->setFeedbackStr($feedbackxml['ON_OK'][0]['@']['message']); |
681 | } |
682 | } |
683 | } |
684 | } |
685 | |
f58b518f |
686 | |
687 | //--- Helper Class to return results to caller ---// |
688 | |
689 | |
690 | /** |
691 | * This class is used to return the results of the environment |
692 | * main functions (environment_check_xxxx) |
693 | */ |
694 | class environment_results { |
695 | |
049c0f4a |
696 | var $part; //which are we checking (database, php, php_extension) |
f58b518f |
697 | var $status; //true/false |
698 | var $error_code; //integer. See constants at the beginning of the file |
699 | var $level; //required/optional |
700 | var $current_version; //current version detected |
701 | var $needed_version; //version needed |
702 | var $info; //Aux. info (DB vendor, library...) |
9e2d15e5 |
703 | var $feedback_str; //String to show on error|on check|on ok |
704 | var $bypass_str; //String to show if some bypass has happened |
f58b518f |
705 | |
706 | /** |
707 | * Constructor of the environment_result class. Just set default values |
708 | */ |
709 | function environment_results($part) { |
710 | $this->part=$part; |
711 | $this->status=false; |
049c0f4a |
712 | $this->error_code=NO_ERROR; |
f58b518f |
713 | $this->level='required'; |
714 | $this->current_version=''; |
715 | $this->needed_version=''; |
716 | $this->info=''; |
9e2d15e5 |
717 | $this->feedback_str=''; |
718 | $this->bypass_str=''; |
f58b518f |
719 | } |
720 | |
721 | /** |
722 | * Set the status |
723 | * @param boolean the status (true/false) |
724 | */ |
725 | function setStatus($status) { |
726 | $this->status=$status; |
727 | if ($status) { |
728 | $this->setErrorCode(NO_ERROR); |
729 | } |
730 | } |
731 | |
732 | /** |
733 | * Set the error_code |
734 | * @param integer the error code (see constants above) |
735 | */ |
736 | function setErrorCode($error_code) { |
737 | $this->error_code=$error_code; |
738 | } |
739 | |
740 | /** |
741 | * Set the level |
742 | * @param string the level (required, optional) |
743 | */ |
744 | function setLevel($level) { |
745 | $this->level=$level; |
746 | } |
747 | |
748 | /** |
749 | * Set the current version |
750 | * @param string the current version |
751 | */ |
752 | function setCurrentVersion($current_version) { |
753 | $this->current_version=$current_version; |
754 | } |
755 | |
756 | /** |
757 | * Set the needed version |
758 | * @param string the needed version |
759 | */ |
760 | function setNeededVersion($needed_version) { |
761 | $this->needed_version=$needed_version; |
762 | } |
763 | |
764 | /** |
765 | * Set the auxiliary info |
766 | * @param string the auxiliary info |
767 | */ |
9e2d15e5 |
768 | function setInfo($info) { |
769 | $this->info=$info; |
770 | } |
771 | |
772 | /** |
773 | * Set the feedback string |
774 | * @param string the feedback string |
775 | */ |
776 | function setFeedbackStr($str) { |
777 | $this->feedback_str=$str; |
778 | } |
f58b518f |
779 | |
b0e2a189 |
780 | /** |
781 | * Set the bypass string |
782 | * @param string the bypass string |
783 | */ |
784 | function setBypassStr($str) { |
785 | $this->bypass_str=$str; |
786 | } |
787 | |
f58b518f |
788 | /** |
789 | * Get the status |
790 | * @return boolean result |
791 | */ |
792 | function getStatus() { |
793 | return $this->status; |
794 | } |
795 | |
796 | /** |
797 | * Get the error code |
798 | * @return integer error code |
799 | */ |
800 | function getErrorCode() { |
801 | return $this->error_code; |
802 | } |
803 | |
804 | /** |
805 | * Get the level |
806 | * @return string level |
807 | */ |
808 | function getLevel() { |
809 | return $this->level; |
810 | } |
811 | |
812 | /** |
813 | * Get the current version |
814 | * @return string current version |
815 | */ |
816 | function getCurrentVersion() { |
817 | return $this->current_version; |
818 | } |
819 | |
820 | /** |
821 | * Get the needed version |
822 | * @return string needed version |
823 | */ |
824 | function getNeededVersion() { |
825 | return $this->needed_version; |
826 | } |
827 | |
828 | /** |
829 | * Get the aux info |
830 | * @return string info |
831 | */ |
832 | function getInfo() { |
833 | return $this->info; |
834 | } |
835 | |
836 | /** |
837 | * Get the part this result belongs to |
838 | * @return string part |
839 | */ |
840 | function getPart() { |
841 | return $this->part; |
842 | } |
9e2d15e5 |
843 | |
844 | /** |
845 | * Get the feedback string |
846 | * @return string feedback string |
847 | */ |
848 | function getFeedbackStr() { |
849 | return $this->feedback_str; |
850 | } |
b0e2a189 |
851 | |
852 | /** |
853 | * Get the bypass string |
854 | * @return string bypass string |
855 | */ |
856 | function getBypassStr() { |
857 | return $this->bypass_str; |
858 | } |
f58b518f |
859 | } |
860 | |
9e2d15e5 |
861 | /// Here all the bypass functions are coded to be used by the environment |
862 | /// checker. All those functions will receive the result object and will |
863 | /// return it modified as needed (status and bypass string) |
864 | |
b0e2a189 |
865 | /** |
866 | * This function will bypass MySQL 4.1.16 reqs if: |
867 | * - We are using MySQL > 4.1.12, informing about problems with non latin chars in the future |
868 | * |
869 | * @param object result object to handle |
870 | * @return boolean true/false to the terminate if the bypass has to be performed (true) or no (false) |
871 | */ |
872 | function bypass_mysql416_reqs ($result) { |
873 | /// See if we are running MySQL >= 4.1.12 |
874 | if (version_compare($result->getCurrentVersion(), '4.1.12', '>=')) { |
875 | return true; |
876 | } |
877 | |
878 | return false; |
879 | } |
880 | |
f58b518f |
881 | ?> |