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