MDL-30995 Completion Fixedup some more PHP DOC issues
[moodle.git] / lib / environmentlib.php
blob607bcfb76b0c9d259be7a992a5c2835570a21fa5
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * This library includes all the necessary stuff to execute some standard
20 * tests of required versions and libraries to run Moodle. It can be
21 * used from the admin interface, and both at install and upgrade.
23 * All the info is stored in the admin/environment.xml file,
24 * supporting to have an updated version in dataroot/environment
26 * @copyright (C) 2001-3001 Eloy Lafuente (stronk7) {@link http://contiento.com}
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 * @package core
29 * @subpackage admin
32 defined('MOODLE_INTERNAL') || die();
34 /// Add required files
35 /**
36 * Include the necessary
38 require_once($CFG->libdir.'/xmlize.php');
40 /// Define a bunch of XML processing errors
41 /** XML Processing Error */
42 define('NO_ERROR', 0);
43 /** XML Processing Error */
44 define('NO_VERSION_DATA_FOUND', 1);
45 /** XML Processing Error */
46 define('NO_DATABASE_SECTION_FOUND', 2);
47 /** XML Processing Error */
48 define('NO_DATABASE_VENDORS_FOUND', 3);
49 /** XML Processing Error */
50 define('NO_DATABASE_VENDOR_MYSQL_FOUND', 4);
51 /** XML Processing Error */
52 define('NO_DATABASE_VENDOR_POSTGRES_FOUND', 5);
53 /** XML Processing Error */
54 define('NO_PHP_SECTION_FOUND', 6);
55 /** XML Processing Error */
56 define('NO_PHP_VERSION_FOUND', 7);
57 /** XML Processing Error */
58 define('NO_PHP_EXTENSIONS_SECTION_FOUND', 8);
59 /** XML Processing Error */
60 define('NO_PHP_EXTENSIONS_NAME_FOUND', 9);
61 /** XML Processing Error */
62 define('NO_DATABASE_VENDOR_VERSION_FOUND', 10);
63 /** XML Processing Error */
64 define('NO_UNICODE_SECTION_FOUND', 11);
65 /** XML Processing Error */
66 define('NO_CUSTOM_CHECK_FOUND', 12);
67 /** XML Processing Error */
68 define('CUSTOM_CHECK_FILE_MISSING', 13);
69 /** XML Processing Error */
70 define('CUSTOM_CHECK_FUNCTION_MISSING', 14);
71 /** XML Processing Error */
72 define('NO_PHP_SETTINGS_NAME_FOUND', 15);
74 /// Define algorithm used to select the xml file
75 /** To select the newer file available to perform checks */
76 define('ENV_SELECT_NEWER', 0);
77 /** To enforce the use of the file under dataroot */
78 define('ENV_SELECT_DATAROOT', 1);
79 /** To enforce the use of the file under admin (release) */
80 define('ENV_SELECT_RELEASE', 2);
82 /**
83 * This function checks all the requirements defined in environment.xml.
85 * @staticvar bool $result
86 * @staticvar array $env_results
87 * @staticvar bool $cache_exists
89 * @param string $version version to check.
90 * @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use. Default ENV_SELECT_NEWER (BC)
91 * @return array with two elements. The first element true/false, depending on
92 * on whether the check passed. The second element is an array of environment_results
93 * objects that has detailed information about the checks and which ones passed.
95 function check_moodle_environment($version, $env_select = ENV_SELECT_NEWER) {
97 $status = true;
99 /// This are cached per request
100 static $result = true;
101 static $env_results;
102 static $cache_exists = false;
104 /// if we have results cached, use them
105 if ($cache_exists) {
106 $environment_results = $env_results;
107 /// No cache exists, calculate everything
108 } else {
109 /// Get the more recent version before the requested
110 if (!$version = get_latest_version_available($version, $env_select)) {
111 $status = false;
114 /// Perform all the checks
115 if (!($environment_results = environment_check($version, $env_select)) && $status) {
116 $status = false;
119 /// Iterate over all the results looking for some error in required items
120 /// or some error_code
121 if ($status) {
122 foreach ($environment_results as $environment_result) {
123 if (!$environment_result->getStatus() && $environment_result->getLevel() == 'required'
124 && !$environment_result->getBypassStr()) {
125 $result = false; // required item that is not bypased
126 } else if ($environment_result->getStatus() && $environment_result->getLevel() == 'required'
127 && $environment_result->getRestrictStr()) {
128 $result = false; // required item that is restricted
129 } else if ($environment_result->getErrorCode()) {
130 $result = false;
134 /// Going to end, we store environment_results to cache
135 $env_results = $environment_results;
136 $cache_exists = true;
137 } ///End of cache block
139 return array($result && $status, $environment_results);
144 * Returns array of critical errors in plain text format
145 * @param array $environment_results array of results gathered
146 * @return array errors
148 function environment_get_errors($environment_results) {
149 global $CFG;
150 $errors = array();
152 // Iterate over each environment_result
153 foreach ($environment_results as $environment_result) {
154 $type = $environment_result->getPart();
155 $info = $environment_result->getInfo();
156 $status = $environment_result->getStatus();
157 $error_code = $environment_result->getErrorCode();
159 $a = new stdClass();
160 if ($error_code) {
161 $a->error_code = $error_code;
162 $errors[] = array($info, get_string('environmentxmlerror', 'admin', $a));
163 return $errors;
166 /// Calculate the status value
167 if ($environment_result->getBypassStr() != '') {
168 // not interesting
169 continue;
170 } else if ($environment_result->getRestrictStr() != '') {
171 // error
172 } else {
173 if ($status) {
174 // ok
175 continue;
176 } else {
177 if ($environment_result->getLevel() == 'optional') {
178 // just a warning
179 continue;
180 } else {
181 // error
186 // We are comparing versions
187 $rec = new stdClass();
188 if ($rec->needed = $environment_result->getNeededVersion()) {
189 $rec->current = $environment_result->getCurrentVersion();
190 if ($environment_result->getLevel() == 'required') {
191 $stringtouse = 'environmentrequireversion';
192 } else {
193 $stringtouse = 'environmentrecommendversion';
195 // We are checking installed & enabled things
196 } else if ($environment_result->getPart() == 'custom_check') {
197 if ($environment_result->getLevel() == 'required') {
198 $stringtouse = 'environmentrequirecustomcheck';
199 } else {
200 $stringtouse = 'environmentrecommendcustomcheck';
202 } else if ($environment_result->getPart() == 'php_setting') {
203 if ($status) {
204 $stringtouse = 'environmentsettingok';
205 } else if ($environment_result->getLevel() == 'required') {
206 $stringtouse = 'environmentmustfixsetting';
207 } else {
208 $stringtouse = 'environmentshouldfixsetting';
210 } else {
211 if ($environment_result->getLevel() == 'required') {
212 $stringtouse = 'environmentrequireinstall';
213 } else {
214 $stringtouse = 'environmentrecommendinstall';
217 $report = get_string($stringtouse, 'admin', $rec);
219 // Here we'll store all the feedback found
220 $feedbacktext = '';
221 // Append the feedback if there is some
222 $feedbacktext .= $environment_result->strToReport($environment_result->getFeedbackStr(), 'error');
223 // Append the restrict if there is some
224 $feedbacktext .= $environment_result->strToReport($environment_result->getRestrictStr(), 'error');
226 $report .= html_to_text($feedbacktext);
228 if ($environment_result->getPart() == 'custom_check'){
229 $errors[] = array($info, $report);
230 } else {
231 $errors[] = array(($info !== '' ? "$type $info" : $type), $report);
235 return $errors;
240 * This function will normalize any version to just a serie of numbers
241 * separated by dots. Everything else will be removed.
243 * @param string $version the original version
244 * @return string the normalized version
246 function normalize_version($version) {
248 /// 1.9 Beta 2 should be read 1.9 on enviromental checks, not 1.9.2
249 /// we can discard everything after the first space
250 $version = trim($version);
251 $versionarr = explode(" ",$version);
252 if (!empty($versionarr)) {
253 $version = $versionarr[0];
255 /// Replace everything but numbers and dots by dots
256 $version = preg_replace('/[^\.\d]/', '.', $version);
257 /// Combine multiple dots in one
258 $version = preg_replace('/(\.{2,})/', '.', $version);
259 /// Trim possible leading and trailing dots
260 $version = trim($version, '.');
262 return $version;
267 * This function will load the environment.xml file and xmlize it
269 * @global object
270 * @staticvar mixed $data
271 * @uses ENV_SELECT_NEWER
272 * @uses ENV_SELECT_DATAROOT
273 * @uses ENV_SELECT_RELEASE
274 * @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use. Default ENV_SELECT_NEWER (BC)
275 * @return mixed the xmlized structure or false on error
277 function load_environment_xml($env_select=ENV_SELECT_NEWER) {
279 global $CFG;
281 static $data; //Only load and xmlize once by request
283 if (!empty($data)) {
284 return $data;
287 /// First of all, take a look inside $CFG->dataroot/environment/environment.xml
288 $file = $CFG->dataroot.'/environment/environment.xml';
289 $internalfile = $CFG->dirroot.'/'.$CFG->admin.'/environment.xml';
290 switch ($env_select) {
291 case ENV_SELECT_NEWER:
292 if (!is_file($file) || !is_readable($file) || filemtime($file) < filemtime($internalfile) ||
293 !$contents = file_get_contents($file)) {
294 /// Fallback to fixed $CFG->admin/environment.xml
295 if (!is_file($internalfile) || !is_readable($internalfile) || !$contents = file_get_contents($internalfile)) {
296 return false;
299 break;
300 case ENV_SELECT_DATAROOT:
301 if (!is_file($file) || !is_readable($file) || !$contents = file_get_contents($file)) {
302 return false;
304 break;
305 case ENV_SELECT_RELEASE:
306 if (!is_file($internalfile) || !is_readable($internalfile) || !$contents = file_get_contents($internalfile)) {
307 return false;
309 break;
311 /// XML the whole file
312 $data = xmlize($contents);
314 return $data;
319 * This function will return the list of Moodle versions available
321 * @staticvar array $versions
322 * @return mixed array of versions. False on error.
324 function get_list_of_environment_versions ($contents) {
326 static $versions = array();
328 if (!empty($versions)) {
329 return $versions;
332 if (isset($contents['COMPATIBILITY_MATRIX']['#']['MOODLE'])) {
333 foreach ($contents['COMPATIBILITY_MATRIX']['#']['MOODLE'] as $version) {
334 $versions[] = $version['@']['version'];
338 return $versions;
343 * This function will return the most recent version in the environment.xml
344 * file previous or equal to the version requested
346 * @param string $version top version from which we start to look backwards
347 * @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
348 * @return string|bool string more recent version or false if not found
350 function get_latest_version_available ($version, $env_select) {
352 /// Normalize the version requested
353 $version = normalize_version($version);
355 /// Load xml file
356 if (!$contents = load_environment_xml($env_select)) {
357 return false;
360 /// Detect available versions
361 if (!$versions = get_list_of_environment_versions($contents)) {
362 return false;
364 /// First we look for exact version
365 if (in_array($version, $versions)) {
366 return $version;
367 } else {
368 $found_version = false;
369 /// Not exact match, so we are going to iterate over the list searching
370 /// for the latest version before the requested one
371 foreach ($versions as $arrversion) {
372 if (version_compare($arrversion, $version, '<')) {
373 $found_version = $arrversion;
378 return $found_version;
383 * This function will return the xmlized data belonging to one Moodle version
385 * @param string $version top version from which we start to look backwards
386 * @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
387 * @return mixed the xmlized structure or false on error
389 function get_environment_for_version($version, $env_select) {
391 /// Normalize the version requested
392 $version = normalize_version($version);
394 /// Load xml file
395 if (!$contents = load_environment_xml($env_select)) {
396 return false;
399 /// Detect available versions
400 if (!$versions = get_list_of_environment_versions($contents)) {
401 return false;
404 /// If the version requested is available
405 if (!in_array($version, $versions)) {
406 return false;
409 /// We now we have it. Extract from full contents.
410 $fl_arr = array_flip($versions);
412 return $contents['COMPATIBILITY_MATRIX']['#']['MOODLE'][$fl_arr[$version]];
417 * This function will check for everything (DB, PHP and PHP extensions for now)
418 * returning an array of environment_result objects.
420 * @global object
421 * @param string $version xml version we are going to use to test this server
422 * @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
423 * @return array array of results encapsulated in one environment_result object
425 function environment_check($version, $env_select) {
426 global $CFG;
428 /// Normalize the version requested
429 $version = normalize_version($version);
431 $results = array(); //To store all the results
433 /// Only run the moodle versions checker on upgrade, not on install
434 if (!empty($CFG->version)) {
435 $results[] = environment_check_moodle($version, $env_select);
437 $results[] = environment_check_unicode($version, $env_select);
438 $results[] = environment_check_database($version, $env_select);
439 $results[] = environment_check_php($version, $env_select);
441 $phpext_results = environment_check_php_extensions($version, $env_select);
442 $results = array_merge($results, $phpext_results);
444 $phpsetting_results = environment_check_php_settings($version, $env_select);
445 $results = array_merge($results, $phpsetting_results);
447 $custom_results = environment_custom_checks($version, $env_select);
448 $results = array_merge($results, $custom_results);
450 return $results;
455 * This function will check if php extensions requirements are satisfied
457 * @uses NO_VERSION_DATA_FOUND
458 * @uses NO_PHP_EXTENSIONS_SECTION_FOUND
459 * @uses NO_PHP_EXTENSIONS_NAME_FOUND
460 * @param string $version xml version we are going to use to test this server
461 * @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
462 * @return array array of results encapsulated in one environment_result object
464 function environment_check_php_extensions($version, $env_select) {
466 $results = array();
468 /// Get the enviroment version we need
469 if (!$data = get_environment_for_version($version, $env_select)) {
470 /// Error. No version data found
471 $result = new environment_results('php_extension');
472 $result->setStatus(false);
473 $result->setErrorCode(NO_VERSION_DATA_FOUND);
474 return array($result);
477 /// Extract the php_extension part
478 if (!isset($data['#']['PHP_EXTENSIONS']['0']['#']['PHP_EXTENSION'])) {
479 /// Error. No PHP section found
480 $result = new environment_results('php_extension');
481 $result->setStatus(false);
482 $result->setErrorCode(NO_PHP_EXTENSIONS_SECTION_FOUND);
483 return array($result);
485 /// Iterate over extensions checking them and creating the needed environment_results
486 foreach($data['#']['PHP_EXTENSIONS']['0']['#']['PHP_EXTENSION'] as $extension) {
487 $result = new environment_results('php_extension');
488 /// Check for level
489 $level = get_level($extension);
490 /// Check for extension name
491 if (!isset($extension['@']['name'])) {
492 $result->setStatus(false);
493 $result->setErrorCode(NO_PHP_EXTENSIONS_NAME_FOUND);
494 } else {
495 $extension_name = $extension['@']['name'];
496 /// The name exists. Just check if it's an installed extension
497 if (!extension_loaded($extension_name)) {
498 $result->setStatus(false);
499 } else {
500 $result->setStatus(true);
502 $result->setLevel($level);
503 $result->setInfo($extension_name);
506 /// Do any actions defined in the XML file.
507 process_environment_result($extension, $result);
509 /// Add the result to the array of results
510 $results[] = $result;
514 return $results;
518 * This function will check if php extensions requirements are satisfied
520 * @uses NO_VERSION_DATA_FOUND
521 * @uses NO_PHP_SETTINGS_NAME_FOUND
522 * @param string $version xml version we are going to use to test this server
523 * @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
524 * @return array array of results encapsulated in one environment_result object
526 function environment_check_php_settings($version, $env_select) {
528 $results = array();
530 /// Get the enviroment version we need
531 if (!$data = get_environment_for_version($version, $env_select)) {
532 /// Error. No version data found
533 $result = new environment_results('php_setting');
534 $result->setStatus(false);
535 $result->setErrorCode(NO_VERSION_DATA_FOUND);
536 $results[] = $result;
537 return $results;
540 /// Extract the php_setting part
541 if (!isset($data['#']['PHP_SETTINGS']['0']['#']['PHP_SETTING'])) {
542 /// No PHP section found - ignore
543 return $results;
545 /// Iterate over settings checking them and creating the needed environment_results
546 foreach($data['#']['PHP_SETTINGS']['0']['#']['PHP_SETTING'] as $setting) {
547 $result = new environment_results('php_setting');
548 /// Check for level
549 $level = get_level($setting);
550 $result->setLevel($level);
551 /// Check for extension name
552 if (!isset($setting['@']['name'])) {
553 $result->setStatus(false);
554 $result->setErrorCode(NO_PHP_SETTINGS_NAME_FOUND);
555 } else {
556 $setting_name = $setting['@']['name'];
557 $setting_value = $setting['@']['value'];
558 $result->setInfo($setting_name);
560 if ($setting_name == 'memory_limit') {
561 $current = ini_get('memory_limit');
562 if ($current == -1) {
563 $result->setStatus(true);
564 } else {
565 $current = get_real_size($current);
566 $minlimit = get_real_size($setting_value);
567 if ($current < $minlimit) {
568 @ini_set('memory_limit', $setting_value);
569 $current = ini_get('memory_limit');
570 $current = get_real_size($current);
572 $result->setStatus($current >= $minlimit);
575 } else {
576 $current = ini_get_bool($setting_name);
577 /// The name exists. Just check if it's an installed extension
578 if ($current == $setting_value) {
579 $result->setStatus(true);
580 } else {
581 $result->setStatus(false);
586 /// Do any actions defined in the XML file.
587 process_environment_result($setting, $result);
589 /// Add the result to the array of results
590 $results[] = $result;
594 return $results;
598 * This function will do the custom checks.
600 * @global object
601 * @uses CUSTOM_CHECK_FUNCTION_MISSING
602 * @uses CUSTOM_CHECK_FILE_MISSING
603 * @uses NO_CUSTOM_CHECK_FOUND
604 * @param string $version xml version we are going to use to test this server.
605 * @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
606 * @return array array of results encapsulated in environment_result objects.
608 function environment_custom_checks($version, $env_select) {
609 global $CFG;
611 $results = array();
613 /// Get current Moodle version (release) for later compare
614 $release = isset($CFG->release) ? $CFG->release : $version; /// In case $CFG fails (at install) use $version
615 $current_version = normalize_version($release);
617 /// Get the enviroment version we need
618 if (!$data = get_environment_for_version($version, $env_select)) {
619 /// Error. No version data found - but this will already have been reported.
620 return $results;
623 /// Extract the CUSTOM_CHECKS part
624 if (!isset($data['#']['CUSTOM_CHECKS']['0']['#']['CUSTOM_CHECK'])) {
625 /// No custom checks found - not a problem
626 return $results;
629 /// Iterate over extensions checking them and creating the needed environment_results
630 foreach($data['#']['CUSTOM_CHECKS']['0']['#']['CUSTOM_CHECK'] as $check) {
631 $result = new environment_results('custom_check');
633 /// Check for level
634 $level = get_level($check);
636 /// Check for extension name
637 if (isset($check['@']['file']) && isset($check['@']['function'])) {
638 $file = $CFG->dirroot . '/' . $check['@']['file'];
639 $function = $check['@']['function'];
640 if (is_readable($file)) {
641 include_once($file);
642 if (function_exists($function)) {
643 $result->setLevel($level);
644 $result->setInfo($function);
645 $result = $function($result);
646 } else {
647 /// Only show error for current version (where function MUST exist)
648 /// else, we are performing custom checks against future versiosn
649 /// and function MAY not exist, so it doesn't cause error, just skip
650 /// custom check by returning null. MDL-15939
651 if (version_compare($current_version, $version, '>=')) {
652 $result->setStatus(false);
653 $result->setInfo($function);
654 $result->setErrorCode(CUSTOM_CHECK_FUNCTION_MISSING);
655 } else {
656 $result = null;
659 } else {
660 /// Only show error for current version (where function MUST exist)
661 /// else, we are performing custom checks against future versiosn
662 /// and function MAY not exist, so it doesn't cause error, just skip
663 /// custom check by returning null. MDL-15939
664 if (version_compare($current_version, $version, '>=')) {
665 $result->setStatus(false);
666 $result->setInfo($function);
667 $result->setErrorCode(CUSTOM_CHECK_FILE_MISSING);
668 } else {
669 $result = null;
672 } else {
673 $result->setStatus(false);
674 $result->setErrorCode(NO_CUSTOM_CHECK_FOUND);
677 if (!is_null($result)) {
678 /// Do any actions defined in the XML file.
679 process_environment_result($check, $result);
681 /// Add the result to the array of results
682 $results[] = $result;
686 return $results;
690 * This function will check if Moodle requirements are satisfied
692 * @uses NO_VERSION_DATA_FOUND
693 * @param string $version xml version we are going to use to test this server
694 * @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
695 * @return object results encapsulated in one environment_result object
697 function environment_check_moodle($version, $env_select) {
699 $result = new environment_results('moodle');
701 /// Get the enviroment version we need
702 if (!$data = get_environment_for_version($version, $env_select)) {
703 /// Error. No version data found
704 $result->setStatus(false);
705 $result->setErrorCode(NO_VERSION_DATA_FOUND);
706 return $result;
709 /// Extract the moodle part
710 if (!isset($data['@']['requires'])) {
711 $needed_version = '1.0'; /// Default to 1.0 if no moodle requires is found
712 } else {
713 /// Extract required moodle version
714 $needed_version = $data['@']['requires'];
717 /// Now search the version we are using
718 $release = get_config('', 'release');
719 $current_version = normalize_version($release);
720 if (strpos($release, 'dev') !== false) {
721 // when final version is required, dev is NOT enough!
722 $current_version = $current_version - 0.1;
725 /// And finally compare them, saving results
726 if (version_compare($current_version, $needed_version, '>=')) {
727 $result->setStatus(true);
728 } else {
729 $result->setStatus(false);
731 $result->setLevel('required');
732 $result->setCurrentVersion($release);
733 $result->setNeededVersion($needed_version);
735 return $result;
739 * This function will check if php requirements are satisfied
741 * @uses NO_VERSION_DATA_FOUND
742 * @uses NO_PHP_SECTION_FOUND
743 * @uses NO_PHP_VERSION_FOUND
744 * @param string $version xml version we are going to use to test this server
745 * @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
746 * @return object results encapsulated in one environment_result object
748 function environment_check_php($version, $env_select) {
750 $result = new environment_results('php');
752 /// Get the enviroment version we need
753 if (!$data = get_environment_for_version($version, $env_select)) {
754 /// Error. No version data found
755 $result->setStatus(false);
756 $result->setErrorCode(NO_VERSION_DATA_FOUND);
757 return $result;
760 /// Extract the php part
761 if (!isset($data['#']['PHP'])) {
762 /// Error. No PHP section found
763 $result->setStatus(false);
764 $result->setErrorCode(NO_PHP_SECTION_FOUND);
765 return $result;
766 } else {
767 /// Extract level and version
768 $level = get_level($data['#']['PHP']['0']);
769 if (!isset($data['#']['PHP']['0']['@']['version'])) {
770 $result->setStatus(false);
771 $result->setErrorCode(NO_PHP_VERSION_FOUND);
772 return $result;
773 } else {
774 $needed_version = $data['#']['PHP']['0']['@']['version'];
778 /// Now search the version we are using
779 $current_version = normalize_version(phpversion());
781 /// And finally compare them, saving results
782 if (version_compare($current_version, $needed_version, '>=')) {
783 $result->setStatus(true);
784 } else {
785 $result->setStatus(false);
787 $result->setLevel($level);
788 $result->setCurrentVersion($current_version);
789 $result->setNeededVersion($needed_version);
791 /// Do any actions defined in the XML file.
792 process_environment_result($data['#']['PHP'][0], $result);
794 return $result;
799 * This function will check if unicode database requirements are satisfied
801 * @global object
802 * @uses NO_VERSION_DATA_FOUND
803 * @uses NO_UNICODE_SECTION_FOUND
804 * @param string $version xml version we are going to use to test this server
805 * @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
806 * @return object results encapsulated in one environment_result object
808 function environment_check_unicode($version, $env_select) {
809 global $DB;
811 $result = new environment_results('unicode');
813 /// Get the enviroment version we need
814 if (!$data = get_environment_for_version($version, $env_select)) {
815 /// Error. No version data found
816 $result->setStatus(false);
817 $result->setErrorCode(NO_VERSION_DATA_FOUND);
818 return $result;
821 /// Extract the unicode part
823 if (!isset($data['#']['UNICODE'])) {
824 /// Error. No UNICODE section found
825 $result->setStatus(false);
826 $result->setErrorCode(NO_UNICODE_SECTION_FOUND);
827 return $result;
828 } else {
829 /// Extract level
830 $level = get_level($data['#']['UNICODE']['0']);
833 if (!$unicodedb = $DB->setup_is_unicodedb()) {
834 $result->setStatus(false);
835 } else {
836 $result->setStatus(true);
839 $result->setLevel($level);
841 /// Do any actions defined in the XML file.
842 process_environment_result($data['#']['UNICODE'][0], $result);
844 return $result;
848 * This function will check if database requirements are satisfied
850 * @global object
851 * @uses NO_VERSION_DATA_FOUND
852 * @uses NO_DATABASE_SECTION_FOUND
853 * @uses NO_DATABASE_VENDORS_FOUND
854 * @uses NO_DATABASE_VENDOR_MYSQL_FOUND
855 * @uses NO_DATABASE_VENDOR_POSTGRES_FOUND
856 * @uses NO_DATABASE_VENDOR_VERSION_FOUND
857 * @param string $version xml version we are going to use to test this server
858 * @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
859 * @return object results encapsulated in one environment_result object
861 function environment_check_database($version, $env_select) {
863 global $DB;
865 $result = new environment_results('database');
867 $vendors = array(); //Array of vendors in version
869 /// Get the enviroment version we need
870 if (!$data = get_environment_for_version($version, $env_select)) {
871 /// Error. No version data found
872 $result->setStatus(false);
873 $result->setErrorCode(NO_VERSION_DATA_FOUND);
874 return $result;
877 /// Extract the database part
878 if (!isset($data['#']['DATABASE'])) {
879 /// Error. No DATABASE section found
880 $result->setStatus(false);
881 $result->setErrorCode(NO_DATABASE_SECTION_FOUND);
882 return $result;
883 } else {
884 /// Extract level
885 $level = get_level($data['#']['DATABASE']['0']);
888 /// Extract DB vendors. At least 2 are mandatory (mysql & postgres)
889 if (!isset($data['#']['DATABASE']['0']['#']['VENDOR'])) {
890 /// Error. No VENDORS found
891 $result->setStatus(false);
892 $result->setErrorCode(NO_DATABASE_VENDORS_FOUND);
893 return $result;
894 } else {
895 /// Extract vendors
896 foreach ($data['#']['DATABASE']['0']['#']['VENDOR'] as $vendor) {
897 if (isset($vendor['@']['name']) && isset($vendor['@']['version'])) {
898 $vendors[$vendor['@']['name']] = $vendor['@']['version'];
899 $vendorsxml[$vendor['@']['name']] = $vendor;
903 /// Check we have the mysql vendor version
904 if (empty($vendors['mysql'])) {
905 $result->setStatus(false);
906 $result->setErrorCode(NO_DATABASE_VENDOR_MYSQL_FOUND);
907 return $result;
909 /// Check we have the postgres vendor version
910 if (empty($vendors['postgres'])) {
911 $result->setStatus(false);
912 $result->setErrorCode(NO_DATABASE_VENDOR_POSTGRES_FOUND);
913 return $result;
916 /// Now search the version we are using (depending of vendor)
917 $current_vendor = $DB->get_dbfamily();
919 $dbinfo = $DB->get_server_info();
920 $current_version = normalize_version($dbinfo['version']);
921 $needed_version = $vendors[$current_vendor];
923 /// Check we have a needed version
924 if (!$needed_version) {
925 $result->setStatus(false);
926 $result->setErrorCode(NO_DATABASE_VENDOR_VERSION_FOUND);
927 return $result;
930 /// And finally compare them, saving results
931 if (version_compare($current_version, $needed_version, '>=')) {
932 $result->setStatus(true);
933 } else {
934 $result->setStatus(false);
936 $result->setLevel($level);
937 $result->setCurrentVersion($current_version);
938 $result->setNeededVersion($needed_version);
939 $result->setInfo($current_vendor);
941 /// Do any actions defined in the XML file.
942 process_environment_result($vendorsxml[$current_vendor], $result);
944 return $result;
949 * This function will post-process the result record by executing the specified
950 * function, modifying it as necessary, also a custom message will be added
951 * to the result object to be printed by the display layer.
952 * Every bypass function must be defined in this file and it'll return
953 * true/false to decide if the original test is bypassed or no. Also
954 * such bypass functions are able to directly handling the result object
955 * although it should be only under exceptional conditions.
957 * @param string xmldata containing the bypass data
958 * @param object result object to be updated
959 * @return void
961 function process_environment_bypass($xml, &$result) {
963 /// Only try to bypass if we were in error and it was required
964 if ($result->getStatus() || $result->getLevel() == 'optional') {
965 return;
968 /// It there is bypass info (function and message)
969 if (is_array($xml['#']) && isset($xml['#']['BYPASS'][0]['@']['function']) && isset($xml['#']['BYPASS'][0]['@']['message'])) {
970 $function = $xml['#']['BYPASS'][0]['@']['function'];
971 $message = $xml['#']['BYPASS'][0]['@']['message'];
972 /// Look for the function
973 if (function_exists($function)) {
974 /// Call it, and if bypass = true is returned, apply meesage
975 if ($function($result)) {
976 /// We only set the bypass message if the function itself hasn't defined it before
977 if (empty($result->getBypassStr)) {
978 $result->setBypassStr($message);
986 * This function will post-process the result record by executing the specified
987 * function, modifying it as necessary, also a custom message will be added
988 * to the result object to be printed by the display layer.
989 * Every restrict function must be defined in this file and it'll return
990 * true/false to decide if the original test is restricted or no. Also
991 * such restrict functions are able to directly handling the result object
992 * although it should be only under exceptional conditions.
994 * @param string xmldata containing the restrict data
995 * @param object result object to be updated
996 * @return void
998 function process_environment_restrict($xml, &$result) {
1000 /// Only try to restrict if we were not in error and it was required
1001 if (!$result->getStatus() || $result->getLevel() == 'optional') {
1002 return;
1004 /// It there is restrict info (function and message)
1005 if (is_array($xml['#']) && isset($xml['#']['RESTRICT'][0]['@']['function']) && isset($xml['#']['RESTRICT'][0]['@']['message'])) {
1006 $function = $xml['#']['RESTRICT'][0]['@']['function'];
1007 $message = $xml['#']['RESTRICT'][0]['@']['message'];
1008 /// Look for the function
1009 if (function_exists($function)) {
1010 /// Call it, and if restrict = true is returned, apply meesage
1011 if ($function($result)) {
1012 /// We only set the restrict message if the function itself hasn't defined it before
1013 if (empty($result->getRestrictStr)) {
1014 $result->setRestrictStr($message);
1022 * This function will detect if there is some message available to be added to the
1023 * result in order to clarify enviromental details.
1025 * @param string xmldata containing the feedback data
1026 * @param object reult object to be updated
1028 function process_environment_messages($xml, &$result) {
1030 /// If there is feedback info
1031 if (is_array($xml['#']) && isset($xml['#']['FEEDBACK'][0]['#'])) {
1032 $feedbackxml = $xml['#']['FEEDBACK'][0]['#'];
1034 if (!$result->status and $result->getLevel() == 'required') {
1035 if (isset($feedbackxml['ON_ERROR'][0]['@']['message'])) {
1036 $result->setFeedbackStr($feedbackxml['ON_ERROR'][0]['@']['message']);
1038 } else if (!$result->status and $result->getLevel() == 'optional') {
1039 if (isset($feedbackxml['ON_CHECK'][0]['@']['message'])) {
1040 $result->setFeedbackStr($feedbackxml['ON_CHECK'][0]['@']['message']);
1042 } else {
1043 if (isset($feedbackxml['ON_OK'][0]['@']['message'])) {
1044 $result->setFeedbackStr($feedbackxml['ON_OK'][0]['@']['message']);
1051 //--- Helper Class to return results to caller ---//
1055 * Helper Class to return results to caller
1057 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1058 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1059 * @package moodlecore
1061 class environment_results {
1063 * @var string Which are we checking (database, php, php_extension, php_extension)
1065 var $part;
1067 * @var bool
1069 var $status;
1071 * @var integer See constants at the beginning of the file
1073 var $error_code;
1075 * @var string required/optional
1077 var $level;
1079 * @var string current version detected
1081 var $current_version;
1083 * @var string version needed
1085 var $needed_version;
1087 * @var string Aux. info (DB vendor, library...)
1089 var $info;
1091 * @var string String to show on error|on check|on ok
1093 var $feedback_str;
1095 * @var string String to show if some bypass has happened
1097 var $bypass_str;
1099 * @var string String to show if some restrict has happened
1101 var $restrict_str;
1104 * Constructor of the environment_result class. Just set default values
1106 * @param string $part
1108 function environment_results($part) {
1109 $this->part=$part;
1110 $this->status=false;
1111 $this->error_code=NO_ERROR;
1112 $this->level='required';
1113 $this->current_version='';
1114 $this->needed_version='';
1115 $this->info='';
1116 $this->feedback_str='';
1117 $this->bypass_str='';
1118 $this->restrict_str='';
1122 * Set the status
1124 * @param boolean $status the status (true/false)
1126 function setStatus($status) {
1127 $this->status=$status;
1128 if ($status) {
1129 $this->setErrorCode(NO_ERROR);
1134 * Set the error_code
1136 * @param integer $error_code the error code (see constants above)
1138 function setErrorCode($error_code) {
1139 $this->error_code=$error_code;
1143 * Set the level
1145 * @param string $level the level (required, optional)
1147 function setLevel($level) {
1148 $this->level=$level;
1152 * Set the current version
1154 * @param string $current_version the current version
1156 function setCurrentVersion($current_version) {
1157 $this->current_version=$current_version;
1161 * Set the needed version
1163 * @param string $needed_version the needed version
1165 function setNeededVersion($needed_version) {
1166 $this->needed_version=$needed_version;
1170 * Set the auxiliary info
1172 * @param string $info the auxiliary info
1174 function setInfo($info) {
1175 $this->info=$info;
1179 * Set the feedback string
1181 * @param mixed $str the feedback string that will be fetched from the admin lang file.
1182 * pass just the string or pass an array of params for get_string
1183 * You always should put your string in admin.php but a third param is useful
1184 * to pass an $a object / string to get_string
1186 function setFeedbackStr($str) {
1187 $this->feedback_str=$str;
1192 * Set the bypass string
1194 * @param string $str the bypass string that will be fetched from the admin lang file.
1195 * pass just the string or pass an array of params for get_string
1196 * You always should put your string in admin.php but a third param is useful
1197 * to pass an $a object / string to get_string
1199 function setBypassStr($str) {
1200 $this->bypass_str=$str;
1204 * Set the restrict string
1206 * @param string $str the restrict string that will be fetched from the admin lang file.
1207 * pass just the string or pass an array of params for get_string
1208 * You always should put your string in admin.php but a third param is useful
1209 * to pass an $a object / string to get_string
1211 function setRestrictStr($str) {
1212 $this->restrict_str=$str;
1216 * Get the status
1218 * @return boolean result
1220 function getStatus() {
1221 return $this->status;
1225 * Get the error code
1227 * @return integer error code
1229 function getErrorCode() {
1230 return $this->error_code;
1234 * Get the level
1236 * @return string level
1238 function getLevel() {
1239 return $this->level;
1243 * Get the current version
1245 * @return string current version
1247 function getCurrentVersion() {
1248 return $this->current_version;
1252 * Get the needed version
1254 * @return string needed version
1256 function getNeededVersion() {
1257 return $this->needed_version;
1261 * Get the aux info
1263 * @return string info
1265 function getInfo() {
1266 return $this->info;
1270 * Get the part this result belongs to
1272 * @return string part
1274 function getPart() {
1275 return $this->part;
1279 * Get the feedback string
1281 * @return mixed feedback string (can be an array of params for get_string or a single string to fetch from
1282 * admin.php lang file).
1284 function getFeedbackStr() {
1285 return $this->feedback_str;
1289 * Get the bypass string
1291 * @return mixed bypass string (can be an array of params for get_string or a single string to fetch from
1292 * admin.php lang file).
1294 function getBypassStr() {
1295 return $this->bypass_str;
1299 * Get the restrict string
1301 * @return mixed restrict string (can be an array of params for get_string or a single string to fetch from
1302 * admin.php lang file).
1304 function getRestrictStr() {
1305 return $this->restrict_str;
1309 * @todo Document this function
1311 * @param mixed $string params for get_string, either a string to fetch from admin.php or an array of
1312 * params for get_string.
1313 * @param string $class css class(es) for message.
1314 * @return string feedback string fetched from lang file wrapped in p tag with class $class or returns
1315 * empty string if $string is empty.
1317 function strToReport($string, $class){
1318 if (!empty($string)){
1319 if (is_array($string)){
1320 $str = call_user_func_array('get_string', $string);
1321 } else {
1322 $str = get_string($string, 'admin');
1324 return '<p class="'.$class.'">'.$str.'</p>';
1325 } else {
1326 return '';
1331 /// Here all the bypass functions are coded to be used by the environment
1332 /// checker. All those functions will receive the result object and will
1333 /// return it modified as needed (status and bypass string)
1336 * This function will bypass MySQL 4.1.16 reqs if:
1337 * - We are using MySQL > 4.1.12, informing about problems with non latin chars in the future
1339 * @param object result object to handle
1340 * @return boolean true/false to determinate if the bypass has to be performed (true) or no (false)
1342 function bypass_mysql416_reqs ($result) {
1343 /// See if we are running MySQL >= 4.1.12
1344 if (version_compare($result->getCurrentVersion(), '4.1.12', '>=')) {
1345 return true;
1348 return false;
1351 /// Here all the restrict functions are coded to be used by the environment
1352 /// checker. All those functions will receive the result object and will
1353 /// return it modified as needed (status and bypass string)
1356 * This function will restrict PHP reqs if:
1357 * - We are using PHP 5.0.x, informing about the buggy version
1359 * @param object $result object to handle
1360 * @return boolean true/false to determinate if the restrict has to be performed (true) or no (false)
1362 function restrict_php50_version($result) {
1363 if (version_compare($result->getCurrentVersion(), '5.0.0', '>=')
1364 and version_compare($result->getCurrentVersion(), '5.0.99', '<')) {
1365 return true;
1367 return false;
1371 * @param array $element the element from the environment.xml file that should have
1372 * either a level="required" or level="optional" attribute.
1373 * @return string "required" or "optional".
1375 function get_level($element) {
1376 $level = 'required';
1377 if (isset($element['@']['level'])) {
1378 $level = $element['@']['level'];
1379 if (!in_array($level, array('required', 'optional'))) {
1380 debugging('The level of a check in the environment.xml file must be "required" or "optional".', DEBUG_DEVELOPER);
1381 $level = 'required';
1383 } else {
1384 debugging('Checks in the environment.xml file must have a level="required" or level="optional" attribute.', DEBUG_DEVELOPER);
1386 return $level;
1390 * Once the result has been determined, look in the XML for any
1391 * messages, or other things that should be done depending on the outcome.
1393 * @param array $element the element from the environment.xml file which
1394 * may have children defining what should be done with the outcome.
1395 * @param object $result the result of the test, which may be modified by
1396 * this function as specified in the XML.
1398 function process_environment_result($element, &$result) {
1399 /// Process messages, modifying the $result if needed.
1400 process_environment_messages($element, $result);
1401 /// Process bypass, modifying $result if needed.
1402 process_environment_bypass($element, $result);
1403 /// Process restrict, modifying $result if needed.
1404 process_environment_restrict($element, $result);