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