weekly release 2.3dev
[moodle.git] / lib / environmentlib.php
CommitLineData
c030b5ee 1<?php
2
16ae0853 3// This file is part of Moodle - http://moodle.org/
4//
c030b5ee 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.
f58b518f 9//
c030b5ee 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.
16ae0853 14//
c030b5ee 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/>.
17
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.
22 *
23 * All the info is stored in the admin/environment.xml file,
24 * supporting to have an updated version in dataroot/environment
25 *
78bfb562
PS
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
c030b5ee 30 */
f58b518f 31
78bfb562
PS
32defined('MOODLE_INTERNAL') || die();
33
049c0f4a 34/// Add required files
c030b5ee 35/**
78bfb562 36 * Include the necessary
c030b5ee 37 */
049c0f4a 38 require_once($CFG->libdir.'/xmlize.php');
39
78bfb562 40/// Define a bunch of XML processing errors
c030b5ee 41 /** XML Processing Error */
00d3a0fd 42 define('NO_ERROR', 0);
c030b5ee 43 /** XML Processing Error */
00d3a0fd 44 define('NO_VERSION_DATA_FOUND', 1);
c030b5ee 45 /** XML Processing Error */
00d3a0fd 46 define('NO_DATABASE_SECTION_FOUND', 2);
c030b5ee 47 /** XML Processing Error */
00d3a0fd 48 define('NO_DATABASE_VENDORS_FOUND', 3);
c030b5ee 49 /** XML Processing Error */
00d3a0fd 50 define('NO_DATABASE_VENDOR_MYSQL_FOUND', 4);
c030b5ee 51 /** XML Processing Error */
00d3a0fd 52 define('NO_DATABASE_VENDOR_POSTGRES_FOUND', 5);
c030b5ee 53 /** XML Processing Error */
00d3a0fd 54 define('NO_PHP_SECTION_FOUND', 6);
c030b5ee 55 /** XML Processing Error */
00d3a0fd 56 define('NO_PHP_VERSION_FOUND', 7);
c030b5ee 57 /** XML Processing Error */
00d3a0fd 58 define('NO_PHP_EXTENSIONS_SECTION_FOUND', 8);
c030b5ee 59 /** XML Processing Error */
00d3a0fd 60 define('NO_PHP_EXTENSIONS_NAME_FOUND', 9);
c030b5ee 61 /** XML Processing Error */
00d3a0fd 62 define('NO_DATABASE_VENDOR_VERSION_FOUND', 10);
c030b5ee 63 /** XML Processing Error */
a392be33 64 define('NO_UNICODE_SECTION_FOUND', 11);
c030b5ee 65 /** XML Processing Error */
bac40536 66 define('NO_CUSTOM_CHECK_FOUND', 12);
c030b5ee 67 /** XML Processing Error */
bac40536 68 define('CUSTOM_CHECK_FILE_MISSING', 13);
c030b5ee 69 /** XML Processing Error */
bac40536 70 define('CUSTOM_CHECK_FUNCTION_MISSING', 14);
c030b5ee 71 /** XML Processing Error */
c808379e 72 define('NO_PHP_SETTINGS_NAME_FOUND', 15);
f58b518f 73
829fa074 74/// Define algorithm used to select the xml file
c030b5ee 75 /** To select the newer file available to perform checks */
16ae0853 76 define('ENV_SELECT_NEWER', 0);
c030b5ee 77 /** To enforce the use of the file under dataroot */
16ae0853 78 define('ENV_SELECT_DATAROOT', 1);
c030b5ee 79 /** To enforce the use of the file under admin (release) */
16ae0853 80 define('ENV_SELECT_RELEASE', 2);
829fa074 81
f58b518f 82/**
cc359566 83 * This function checks all the requirements defined in environment.xml.
c030b5ee 84 *
85 * @staticvar bool $result
86 * @staticvar array $env_results
87 * @staticvar bool $cache_exists
cc359566 88 *
c030b5ee 89 * @param string $version version to check.
c030b5ee 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)
cc359566
TH
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.
f58b518f 94 */
cc359566 95function check_moodle_environment($version, $env_select = ENV_SELECT_NEWER) {
049c0f4a 96
97 $status = true;
f58b518f 98
878d309c 99/// This are cached per request
100 static $result = true;
101 static $env_results;
102 static $cache_exists = false;
103
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
829fa074 110 if (!$version = get_latest_version_available($version, $env_select)) {
878d309c 111 $status = false;
112 }
f58b518f 113
878d309c 114 /// Perform all the checks
829fa074 115 if (!($environment_results = environment_check($version, $env_select)) && $status) {
878d309c 116 $status = false;
117 }
f58b518f 118
878d309c 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) {
95a39282 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()) {
878d309c 130 $result = false;
131 }
049c0f4a 132 }
f58b518f 133 }
878d309c 134 /// Going to end, we store environment_results to cache
135 $env_results = $environment_results;
136 $cache_exists = true;
137 } ///End of cache block
f58b518f 138
cc359566 139 return array($result && $status, $environment_results);
f58b518f 140}
141
142
16ae0853 143/**
144 * Returns array of critical errors in plain text format
145 * @param array $environment_results array of results gathered
146 * @return array errors
147 */
148function environment_get_errors($environment_results) {
149 global $CFG;
150 $errors = array();
151
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();
158
365a5941 159 $a = new stdClass();
16ae0853 160 if ($error_code) {
161 $a->error_code = $error_code;
162 $errors[] = array($info, get_string('environmentxmlerror', 'admin', $a));
163 return $errors;
164 }
165
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
182 }
183 }
184 }
185
186 // We are comparing versions
365a5941 187 $rec = new stdClass();
16ae0853 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';
194 }
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';
201 }
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';
209 }
210 } else {
211 if ($environment_result->getLevel() == 'required') {
212 $stringtouse = 'environmentrequireinstall';
213 } else {
214 $stringtouse = 'environmentrecommendinstall';
215 }
216 }
217 $report = get_string($stringtouse, 'admin', $rec);
218
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');
225
226 $report .= html_to_text($feedbacktext);
227
228 if ($environment_result->getPart() == 'custom_check'){
229 $errors[] = array($info, $report);
230 } else {
231 $errors[] = array(($info !== '' ? "$type $info" : $type), $report);
232 }
233 }
234
235 return $errors;
236}
237
238
f58b518f 239/**
240 * This function will normalize any version to just a serie of numbers
241 * separated by dots. Everything else will be removed.
c030b5ee 242 *
f58b518f 243 * @param string $version the original version
244 * @return string the normalized version
245 */
246function normalize_version($version) {
16ae0853 247
4fa6cb47 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];
254 }
f58b518f 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, '.');
261
262 return $version;
263}
264
265
266/**
267 * This function will load the environment.xml file and xmlize it
c030b5ee 268 *
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)
f58b518f 275 * @return mixed the xmlized structure or false on error
276 */
829fa074 277function load_environment_xml($env_select=ENV_SELECT_NEWER) {
770fef0a 278
f58b518f 279 global $CFG;
280
281 static $data; //Only load and xmlize once by request
282
283 if (!empty($data)) {
284 return $data;
285 }
286
00d3a0fd 287/// First of all, take a look inside $CFG->dataroot/environment/environment.xml
288 $file = $CFG->dataroot.'/environment/environment.xml';
38fca5d7 289 $internalfile = $CFG->dirroot.'/'.$CFG->admin.'/environment.xml';
829fa074 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;
297 }
298 }
299 break;
300 case ENV_SELECT_DATAROOT:
301 if (!is_file($file) || !is_readable($file) || !$contents = file_get_contents($file)) {
302 return false;
303 }
304 break;
305 case ENV_SELECT_RELEASE:
306 if (!is_file($internalfile) || !is_readable($internalfile) || !$contents = file_get_contents($internalfile)) {
307 return false;
308 }
309 break;
f58b518f 310 }
311/// XML the whole file
312 $data = xmlize($contents);
313
314 return $data;
315}
316
317
318/**
319 * This function will return the list of Moodle versions available
c030b5ee 320 *
321 * @staticvar array $versions
f58b518f 322 * @return mixed array of versions. False on error.
323 */
324function get_list_of_environment_versions ($contents) {
325
326 static $versions = array();
327
328 if (!empty($versions)) {
329 return $versions;
330 }
331
332 if (isset($contents['COMPATIBILITY_MATRIX']['#']['MOODLE'])) {
333 foreach ($contents['COMPATIBILITY_MATRIX']['#']['MOODLE'] as $version) {
334 $versions[] = $version['@']['version'];
335 }
336 }
337
338 return $versions;
339}
340
341
342/**
343 * This function will return the most recent version in the environment.xml
344 * file previous or equal to the version requested
c030b5ee 345 *
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
f58b518f 349 */
829fa074 350function get_latest_version_available ($version, $env_select) {
f58b518f 351
352/// Normalize the version requested
353 $version = normalize_version($version);
354
355/// Load xml file
829fa074 356 if (!$contents = load_environment_xml($env_select)) {
f58b518f 357 return false;
358 }
359
360/// Detect available versions
361 if (!$versions = get_list_of_environment_versions($contents)) {
362 return false;
363 }
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;
374 }
375 }
376 }
377
378 return $found_version;
379}
380
381
770fef0a 382/**
f58b518f 383 * This function will return the xmlized data belonging to one Moodle version
c030b5ee 384 *
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.
f58b518f 387 * @return mixed the xmlized structure or false on error
388 */
829fa074 389function get_environment_for_version($version, $env_select) {
770fef0a 390
f58b518f 391/// Normalize the version requested
392 $version = normalize_version($version);
393
394/// Load xml file
829fa074 395 if (!$contents = load_environment_xml($env_select)) {
f58b518f 396 return false;
397 }
398
399/// Detect available versions
400 if (!$versions = get_list_of_environment_versions($contents)) {
401 return false;
402 }
403
404/// If the version requested is available
405 if (!in_array($version, $versions)) {
406 return false;
407 }
408
409/// We now we have it. Extract from full contents.
410 $fl_arr = array_flip($versions);
770fef0a 411
f58b518f 412 return $contents['COMPATIBILITY_MATRIX']['#']['MOODLE'][$fl_arr[$version]];
413}
414
415
770fef0a 416/**
f58b518f 417 * This function will check for everything (DB, PHP and PHP extensions for now)
418 * returning an array of environment_result objects.
c030b5ee 419 *
420 * @global object
f58b518f 421 * @param string $version xml version we are going to use to test this server
c030b5ee 422 * @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
f58b518f 423 * @return array array of results encapsulated in one environment_result object
424 */
829fa074 425function environment_check($version, $env_select) {
7f2d3ec0 426 global $CFG;
427
f58b518f 428/// Normalize the version requested
429 $version = normalize_version($version);
430
431 $results = array(); //To store all the results
432
7f2d3ec0 433/// Only run the moodle versions checker on upgrade, not on install
90509582 434 if (!empty($CFG->version)) {
829fa074 435 $results[] = environment_check_moodle($version, $env_select);
7f2d3ec0 436 }
829fa074 437 $results[] = environment_check_unicode($version, $env_select);
438 $results[] = environment_check_database($version, $env_select);
439 $results[] = environment_check_php($version, $env_select);
f58b518f 440
829fa074 441 $phpext_results = environment_check_php_extensions($version, $env_select);
bac40536 442 $results = array_merge($results, $phpext_results);
f58b518f 443
829fa074 444 $phpsetting_results = environment_check_php_settings($version, $env_select);
c808379e 445 $results = array_merge($results, $phpsetting_results);
446
829fa074 447 $custom_results = environment_custom_checks($version, $env_select);
bac40536 448 $results = array_merge($results, $custom_results);
f58b518f 449
450 return $results;
451}
452
453
454/**
455 * This function will check if php extensions requirements are satisfied
c030b5ee 456 *
457 * @uses NO_VERSION_DATA_FOUND
458 * @uses NO_PHP_EXTENSIONS_SECTION_FOUND
459 * @uses NO_PHP_EXTENSIONS_NAME_FOUND
f58b518f 460 * @param string $version xml version we are going to use to test this server
c030b5ee 461 * @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
f58b518f 462 * @return array array of results encapsulated in one environment_result object
463 */
829fa074 464function environment_check_php_extensions($version, $env_select) {
f58b518f 465
466 $results = array();
467
468/// Get the enviroment version we need
829fa074 469 if (!$data = get_environment_for_version($version, $env_select)) {
f58b518f 470 /// Error. No version data found
049c0f4a 471 $result = new environment_results('php_extension');
f58b518f 472 $result->setStatus(false);
473 $result->setErrorCode(NO_VERSION_DATA_FOUND);
c33b83e7 474 return array($result);
f58b518f 475 }
476
477/// Extract the php_extension part
478 if (!isset($data['#']['PHP_EXTENSIONS']['0']['#']['PHP_EXTENSION'])) {
479 /// Error. No PHP section found
049c0f4a 480 $result = new environment_results('php_extension');
f58b518f 481 $result->setStatus(false);
482 $result->setErrorCode(NO_PHP_EXTENSIONS_SECTION_FOUND);
c33b83e7 483 return array($result);
9e2d15e5 484 }
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
bac40536 489 $level = get_level($extension);
9e2d15e5 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)) {
f58b518f 498 $result->setStatus(false);
f58b518f 499 } else {
9e2d15e5 500 $result->setStatus(true);
f58b518f 501 }
9e2d15e5 502 $result->setLevel($level);
503 $result->setInfo($extension_name);
f58b518f 504 }
bac40536 505
506 /// Do any actions defined in the XML file.
507 process_environment_result($extension, $result);
b0e2a189 508
9e2d15e5 509 /// Add the result to the array of results
510 $results[] = $result;
f58b518f 511 }
512
9e2d15e5 513
f58b518f 514 return $results;
515}
516
c808379e 517/**
518 * This function will check if php extensions requirements are satisfied
c030b5ee 519 *
520 * @uses NO_VERSION_DATA_FOUND
521 * @uses NO_PHP_SETTINGS_NAME_FOUND
c808379e 522 * @param string $version xml version we are going to use to test this server
c030b5ee 523 * @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
c808379e 524 * @return array array of results encapsulated in one environment_result object
525 */
829fa074 526function environment_check_php_settings($version, $env_select) {
c808379e 527
528 $results = array();
529
530/// Get the enviroment version we need
829fa074 531 if (!$data = get_environment_for_version($version, $env_select)) {
c808379e 532 /// Error. No version data found
533 $result = new environment_results('php_setting');
534 $result->setStatus(false);
535 $result->setErrorCode(NO_VERSION_DATA_FOUND);
5f014173
PS
536 $results[] = $result;
537 return $results;
c808379e 538 }
539
540/// Extract the php_setting part
541 if (!isset($data['#']['PHP_SETTINGS']['0']['#']['PHP_SETTING'])) {
542 /// No PHP section found - ignore
543 return $results;
544 }
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);
559
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);
571 }
572 $result->setStatus($current >= $minlimit);
573 }
574
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);
582 }
583 }
584 }
585
586 /// Do any actions defined in the XML file.
587 process_environment_result($setting, $result);
588
589 /// Add the result to the array of results
590 $results[] = $result;
591 }
592
593
594 return $results;
595}
596
bac40536 597/**
598 * This function will do the custom checks.
c030b5ee 599 *
600 * @global object
601 * @uses CUSTOM_CHECK_FUNCTION_MISSING
602 * @uses CUSTOM_CHECK_FILE_MISSING
603 * @uses NO_CUSTOM_CHECK_FOUND
bac40536 604 * @param string $version xml version we are going to use to test this server.
c030b5ee 605 * @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
bac40536 606 * @return array array of results encapsulated in environment_result objects.
607 */
829fa074 608function environment_custom_checks($version, $env_select) {
bac40536 609 global $CFG;
610
611 $results = array();
612
eae02f59 613/// Get current Moodle version (release) for later compare
b08a7225 614 $release = isset($CFG->release) ? $CFG->release : $version; /// In case $CFG fails (at install) use $version
615 $current_version = normalize_version($release);
eae02f59 616
bac40536 617/// Get the enviroment version we need
829fa074 618 if (!$data = get_environment_for_version($version, $env_select)) {
bac40536 619 /// Error. No version data found - but this will already have been reported.
620 return $results;
621 }
622
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;
627 }
628
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');
632
633 /// Check for level
634 $level = get_level($check);
635
636 /// Check for extension name
637 if (isset($check['@']['file']) && isset($check['@']['function'])) {
638 $file = $CFG->dirroot . '/' . $check['@']['file'];
bac40536 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 {
eae02f59 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;
657 }
bac40536 658 }
659 } else {
eae02f59 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;
670 }
bac40536 671 }
672 } else {
673 $result->setStatus(false);
674 $result->setErrorCode(NO_CUSTOM_CHECK_FOUND);
675 }
676
677 if (!is_null($result)) {
678 /// Do any actions defined in the XML file.
679 process_environment_result($check, $result);
770fef0a 680
bac40536 681 /// Add the result to the array of results
682 $results[] = $result;
683 }
684 }
685
686 return $results;
687}
f58b518f 688
7f2d3ec0 689/**
690 * This function will check if Moodle requirements are satisfied
c030b5ee 691 *
692 * @uses NO_VERSION_DATA_FOUND
7f2d3ec0 693 * @param string $version xml version we are going to use to test this server
c030b5ee 694 * @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
7f2d3ec0 695 * @return object results encapsulated in one environment_result object
696 */
829fa074 697function environment_check_moodle($version, $env_select) {
7f2d3ec0 698
699 $result = new environment_results('moodle');
700
701/// Get the enviroment version we need
829fa074 702 if (!$data = get_environment_for_version($version, $env_select)) {
7f2d3ec0 703 /// Error. No version data found
704 $result->setStatus(false);
705 $result->setErrorCode(NO_VERSION_DATA_FOUND);
706 return $result;
707 }
708
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'];
715 }
716
717/// Now search the version we are using
01264131
PS
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;
723 }
7f2d3ec0 724
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);
730 }
731 $result->setLevel('required');
01264131 732 $result->setCurrentVersion($release);
7f2d3ec0 733 $result->setNeededVersion($needed_version);
734
735 return $result;
736}
737
f58b518f 738/**
739 * This function will check if php requirements are satisfied
c030b5ee 740 *
741 * @uses NO_VERSION_DATA_FOUND
742 * @uses NO_PHP_SECTION_FOUND
743 * @uses NO_PHP_VERSION_FOUND
f58b518f 744 * @param string $version xml version we are going to use to test this server
c030b5ee 745 * @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
f58b518f 746 * @return object results encapsulated in one environment_result object
747 */
829fa074 748function environment_check_php($version, $env_select) {
f58b518f 749
750 $result = new environment_results('php');
751
752/// Get the enviroment version we need
829fa074 753 if (!$data = get_environment_for_version($version, $env_select)) {
f58b518f 754 /// Error. No version data found
755 $result->setStatus(false);
756 $result->setErrorCode(NO_VERSION_DATA_FOUND);
757 return $result;
758 }
759
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
bac40536 768 $level = get_level($data['#']['PHP']['0']);
f58b518f 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'];
775 }
776 }
777
778/// Now search the version we are using
779 $current_version = normalize_version(phpversion());
780
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);
f58b518f 786 }
770fef0a 787 $result->setLevel($level);
f58b518f 788 $result->setCurrentVersion($current_version);
789 $result->setNeededVersion($needed_version);
bac40536 790
791/// Do any actions defined in the XML file.
792 process_environment_result($data['#']['PHP'][0], $result);
f58b518f 793
794 return $result;
795}
796
797
a392be33 798/**
799 * This function will check if unicode database requirements are satisfied
c030b5ee 800 *
801 * @global object
802 * @uses NO_VERSION_DATA_FOUND
803 * @uses NO_UNICODE_SECTION_FOUND
a392be33 804 * @param string $version xml version we are going to use to test this server
c030b5ee 805 * @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
a392be33 806 * @return object results encapsulated in one environment_result object
807 */
829fa074 808function environment_check_unicode($version, $env_select) {
fd0e6640 809 global $DB;
a392be33 810
811 $result = new environment_results('unicode');
812
813 /// Get the enviroment version we need
829fa074 814 if (!$data = get_environment_for_version($version, $env_select)) {
a392be33 815 /// Error. No version data found
816 $result->setStatus(false);
817 $result->setErrorCode(NO_VERSION_DATA_FOUND);
818 return $result;
819 }
820
821 /// Extract the unicode part
822
823 if (!isset($data['#']['UNICODE'])) {
7f2d3ec0 824 /// Error. No UNICODE section found
a392be33 825 $result->setStatus(false);
826 $result->setErrorCode(NO_UNICODE_SECTION_FOUND);
827 return $result;
828 } else {
829 /// Extract level
bac40536 830 $level = get_level($data['#']['UNICODE']['0']);
a392be33 831 }
832
f33e1ed4 833 if (!$unicodedb = $DB->setup_is_unicodedb()) {
a392be33 834 $result->setStatus(false);
835 } else {
836 $result->setStatus(true);
837 }
838
839 $result->setLevel($level);
840
bac40536 841/// Do any actions defined in the XML file.
842 process_environment_result($data['#']['UNICODE'][0], $result);
a392be33 843
844 return $result;
845}
846
f58b518f 847/**
848 * This function will check if database requirements are satisfied
c030b5ee 849 *
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
f58b518f 857 * @param string $version xml version we are going to use to test this server
c030b5ee 858 * @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
f58b518f 859 * @return object results encapsulated in one environment_result object
860 */
829fa074 861function environment_check_database($version, $env_select) {
f58b518f 862
f33e1ed4 863 global $DB;
f58b518f 864
865 $result = new environment_results('database');
866
867 $vendors = array(); //Array of vendors in version
868
869/// Get the enviroment version we need
829fa074 870 if (!$data = get_environment_for_version($version, $env_select)) {
f58b518f 871 /// Error. No version data found
872 $result->setStatus(false);
873 $result->setErrorCode(NO_VERSION_DATA_FOUND);
874 return $result;
875 }
876
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
bac40536 885 $level = get_level($data['#']['DATABASE']['0']);
f58b518f 886 }
887
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'];
9e2d15e5 899 $vendorsxml[$vendor['@']['name']] = $vendor;
f58b518f 900 }
901 }
902 }
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;
908 }
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;
914 }
915
916/// Now search the version we are using (depending of vendor)
f33e1ed4 917 $current_vendor = $DB->get_dbfamily();
ed7656bf 918
f33e1ed4 919 $dbinfo = $DB->get_server_info();
f58b518f 920 $current_version = normalize_version($dbinfo['version']);
921 $needed_version = $vendors[$current_vendor];
922
e3058eb3 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;
928 }
929
f58b518f 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);
f58b518f 935 }
770fef0a 936 $result->setLevel($level);
f58b518f 937 $result->setCurrentVersion($current_version);
938 $result->setNeededVersion($needed_version);
939 $result->setInfo($current_vendor);
940
bac40536 941/// Do any actions defined in the XML file.
942 process_environment_result($vendorsxml[$current_vendor], $result);
9e2d15e5 943
f58b518f 944 return $result;
945
946}
947
b0e2a189 948/**
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.
956 *
957 * @param string xmldata containing the bypass data
95a39282 958 * @param object result object to be updated
c030b5ee 959 * @return void
b0e2a189 960 */
961function process_environment_bypass($xml, &$result) {
962
76bb0d20 963/// Only try to bypass if we were in error and it was required
964 if ($result->getStatus() || $result->getLevel() == 'optional') {
b0e2a189 965 return;
966 }
967
968/// It there is bypass info (function and message)
74506a51 969 if (is_array($xml['#']) && isset($xml['#']['BYPASS'][0]['@']['function']) && isset($xml['#']['BYPASS'][0]['@']['message'])) {
b0e2a189 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);
979 }
980 }
981 }
982 }
983}
984
95a39282 985/**
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.
993 *
994 * @param string xmldata containing the restrict data
995 * @param object result object to be updated
c030b5ee 996 * @return void
95a39282 997 */
998function process_environment_restrict($xml, &$result) {
999
1000/// Only try to restrict if we were not in error and it was required
1001 if (!$result->getStatus() || $result->getLevel() == 'optional') {
1002 return;
1003 }
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);
1015 }
1016 }
1017 }
1018 }
1019}
1020
9e2d15e5 1021/**
1022 * This function will detect if there is some message available to be added to the
1023 * result in order to clarify enviromental details.
c030b5ee 1024 *
b0e2a189 1025 * @param string xmldata containing the feedback data
9e2d15e5 1026 * @param object reult object to be updated
1027 */
1028function process_environment_messages($xml, &$result) {
1029
1030/// If there is feedback info
74506a51 1031 if (is_array($xml['#']) && isset($xml['#']['FEEDBACK'][0]['#'])) {
9e2d15e5 1032 $feedbackxml = $xml['#']['FEEDBACK'][0]['#'];
1033
1034 if (!$result->status and $result->getLevel() == 'required') {
1035 if (isset($feedbackxml['ON_ERROR'][0]['@']['message'])) {
1036 $result->setFeedbackStr($feedbackxml['ON_ERROR'][0]['@']['message']);
1037 }
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']);
1041 }
1042 } else {
1043 if (isset($feedbackxml['ON_OK'][0]['@']['message'])) {
1044 $result->setFeedbackStr($feedbackxml['ON_OK'][0]['@']['message']);
1045 }
1046 }
1047 }
1048}
1049
f58b518f 1050
1051//--- Helper Class to return results to caller ---//
1052
1053
770fef0a 1054/**
c030b5ee 1055 * Helper Class to return results to caller
1056 *
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
f58b518f 1060 */
1061class environment_results {
c030b5ee 1062 /**
1063 * @var string Which are we checking (database, php, php_extension, php_extension)
1064 */
1065 var $part;
1066 /**
1067 * @var bool
1068 */
1069 var $status;
1070 /**
1071 * @var integer See constants at the beginning of the file
1072 */
1073 var $error_code;
1074 /**
1075 * @var string required/optional
1076 */
1077 var $level;
1078 /**
1079 * @var string current version detected
1080 */
1081 var $current_version;
1082 /**
1083 * @var string version needed
1084 */
1085 var $needed_version;
1086 /**
1087 * @var string Aux. info (DB vendor, library...)
1088 */
1089 var $info;
1090 /**
1091 * @var string String to show on error|on check|on ok
1092 */
1093 var $feedback_str;
1094 /**
1095 * @var string String to show if some bypass has happened
1096 */
1097 var $bypass_str;
1098 /**
1099 * @var string String to show if some restrict has happened
1100 */
1101 var $restrict_str;
f58b518f 1102
1103 /**
1104 * Constructor of the environment_result class. Just set default values
c030b5ee 1105 *
1106 * @param string $part
f58b518f 1107 */
1108 function environment_results($part) {
1109 $this->part=$part;
1110 $this->status=false;
049c0f4a 1111 $this->error_code=NO_ERROR;
f58b518f 1112 $this->level='required';
1113 $this->current_version='';
1114 $this->needed_version='';
1115 $this->info='';
9e2d15e5 1116 $this->feedback_str='';
1117 $this->bypass_str='';
95a39282 1118 $this->restrict_str='';
f58b518f 1119 }
1120
1121 /**
1122 * Set the status
c030b5ee 1123 *
1124 * @param boolean $status the status (true/false)
f58b518f 1125 */
1126 function setStatus($status) {
1127 $this->status=$status;
1128 if ($status) {
1129 $this->setErrorCode(NO_ERROR);
1130 }
1131 }
1132
1133 /**
1134 * Set the error_code
c030b5ee 1135 *
1136 * @param integer $error_code the error code (see constants above)
f58b518f 1137 */
1138 function setErrorCode($error_code) {
1139 $this->error_code=$error_code;
1140 }
1141
1142 /**
1143 * Set the level
c030b5ee 1144 *
1145 * @param string $level the level (required, optional)
f58b518f 1146 */
1147 function setLevel($level) {
1148 $this->level=$level;
1149 }
1150
1151 /**
1152 * Set the current version
c030b5ee 1153 *
1154 * @param string $current_version the current version
f58b518f 1155 */
1156 function setCurrentVersion($current_version) {
1157 $this->current_version=$current_version;
1158 }
1159
1160 /**
1161 * Set the needed version
c030b5ee 1162 *
1163 * @param string $needed_version the needed version
f58b518f 1164 */
1165 function setNeededVersion($needed_version) {
1166 $this->needed_version=$needed_version;
1167 }
1168
1169 /**
1170 * Set the auxiliary info
c030b5ee 1171 *
1172 * @param string $info the auxiliary info
f58b518f 1173 */
9e2d15e5 1174 function setInfo($info) {
1175 $this->info=$info;
1176 }
770fef0a 1177
9e2d15e5 1178 /**
1179 * Set the feedback string
c030b5ee 1180 *
1181 * @param mixed $str the feedback string that will be fetched from the admin lang file.
afb36bca 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
9e2d15e5 1185 */
1186 function setFeedbackStr($str) {
1187 $this->feedback_str=$str;
1188 }
f58b518f 1189
afb36bca 1190
b0e2a189 1191 /**
1192 * Set the bypass string
c030b5ee 1193 *
1194 * @param string $str the bypass string that will be fetched from the admin lang file.
afb36bca 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
b0e2a189 1198 */
1199 function setBypassStr($str) {
1200 $this->bypass_str=$str;
1201 }
1202
95a39282 1203 /**
1204 * Set the restrict string
c030b5ee 1205 *
1206 * @param string $str the restrict string that will be fetched from the admin lang file.
afb36bca 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
95a39282 1210 */
1211 function setRestrictStr($str) {
1212 $this->restrict_str=$str;
1213 }
1214
f58b518f 1215 /**
1216 * Get the status
c030b5ee 1217 *
f58b518f 1218 * @return boolean result
1219 */
1220 function getStatus() {
1221 return $this->status;
1222 }
1223
1224 /**
1225 * Get the error code
c030b5ee 1226 *
f58b518f 1227 * @return integer error code
1228 */
1229 function getErrorCode() {
1230 return $this->error_code;
1231 }
1232
1233 /**
1234 * Get the level
c030b5ee 1235 *
f58b518f 1236 * @return string level
1237 */
1238 function getLevel() {
1239 return $this->level;
1240 }
1241
1242 /**
770fef0a 1243 * Get the current version
c030b5ee 1244 *
f58b518f 1245 * @return string current version
1246 */
1247 function getCurrentVersion() {
1248 return $this->current_version;
1249 }
1250
1251 /**
1252 * Get the needed version
c030b5ee 1253 *
f58b518f 1254 * @return string needed version
1255 */
1256 function getNeededVersion() {
1257 return $this->needed_version;
1258 }
1259
1260 /**
1261 * Get the aux info
c030b5ee 1262 *
f58b518f 1263 * @return string info
1264 */
1265 function getInfo() {
1266 return $this->info;
1267 }
1268
1269 /**
1270 * Get the part this result belongs to
c030b5ee 1271 *
f58b518f 1272 * @return string part
1273 */
1274 function getPart() {
1275 return $this->part;
1276 }
9e2d15e5 1277
1278 /**
1279 * Get the feedback string
c030b5ee 1280 *
afb36bca 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).
9e2d15e5 1283 */
1284 function getFeedbackStr() {
1285 return $this->feedback_str;
1286 }
b0e2a189 1287
1288 /**
1289 * Get the bypass string
c030b5ee 1290 *
afb36bca 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).
b0e2a189 1293 */
1294 function getBypassStr() {
1295 return $this->bypass_str;
1296 }
95a39282 1297
1298 /**
1299 * Get the restrict string
c030b5ee 1300 *
afb36bca 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).
95a39282 1303 */
1304 function getRestrictStr() {
1305 return $this->restrict_str;
1306 }
afb36bca 1307
1308 /**
16ae0853 1309 * @todo Document this function
c030b5ee 1310 *
afb36bca 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.
1316 */
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');
1323 }
1324 return '<p class="'.$class.'">'.$str.'</p>';
1325 } else {
1326 return '';
1327 }
1328 }
f58b518f 1329}
1330
9e2d15e5 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)
1334
b0e2a189 1335/**
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
1338 *
1339 * @param object result object to handle
95a39282 1340 * @return boolean true/false to determinate if the bypass has to be performed (true) or no (false)
b0e2a189 1341 */
1342function 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;
1346 }
1347
1348 return false;
1349}
1350
95a39282 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)
1354
770fef0a 1355/**
95a39282 1356 * This function will restrict PHP reqs if:
1357 * - We are using PHP 5.0.x, informing about the buggy version
1358 *
c030b5ee 1359 * @param object $result object to handle
95a39282 1360 * @return boolean true/false to determinate if the restrict has to be performed (true) or no (false)
1361 */
1362function 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;
1366 }
1367 return false;
1368}
bac40536 1369
1370/**
1371 * @param array $element the element from the environment.xml file that should have
1372 * either a level="required" or level="optional" attribute.
c030b5ee 1373 * @return string "required" or "optional".
bac40536 1374 */
1375function get_level($element) {
1376 $level = 'required';
1377 if (isset($element['@']['level'])) {
1378 $level = $element['@']['level'];
1379 if (!in_array($level, array('required', 'optional'))) {
7f2d3ec0 1380 debugging('The level of a check in the environment.xml file must be "required" or "optional".', DEBUG_DEVELOPER);
bac40536 1381 $level = 'required';
1382 }
1383 } else {
1384 debugging('Checks in the environment.xml file must have a level="required" or level="optional" attribute.', DEBUG_DEVELOPER);
1385 }
1386 return $level;
1387}
1388
1389/**
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.
c030b5ee 1392 *
bac40536 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.
1397 */
1398function 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);
1405}