forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
environmentlib.php
1581 lines (1394 loc) · 54.4 KB
/
environmentlib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This library includes all the necessary stuff to execute some standard
* tests of required versions and libraries to run Moodle. It can be
* used from the admin interface, and both at install and upgrade.
*
* All the info is stored in the admin/environment.xml file,
* supporting to have an updated version in dataroot/environment
*
* @copyright (C) 2001-3001 Eloy Lafuente (stronk7) {@link http://contiento.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package core
* @subpackage admin
*/
defined('MOODLE_INTERNAL') || die();
/// Add required files
/**
* Include the necessary
*/
require_once($CFG->libdir.'/xmlize.php');
/// Define a bunch of XML processing errors
/** XML Processing Error */
define('NO_ERROR', 0);
/** XML Processing Error */
define('NO_VERSION_DATA_FOUND', 1);
/** XML Processing Error */
define('NO_DATABASE_SECTION_FOUND', 2);
/** XML Processing Error */
define('NO_DATABASE_VENDORS_FOUND', 3);
/** XML Processing Error */
define('NO_DATABASE_VENDOR_MYSQL_FOUND', 4);
/** XML Processing Error */
define('NO_DATABASE_VENDOR_POSTGRES_FOUND', 5);
/** XML Processing Error */
define('NO_PHP_SECTION_FOUND', 6);
/** XML Processing Error */
define('NO_PHP_VERSION_FOUND', 7);
/** XML Processing Error */
define('NO_PHP_EXTENSIONS_SECTION_FOUND', 8);
/** XML Processing Error */
define('NO_PHP_EXTENSIONS_NAME_FOUND', 9);
/** XML Processing Error */
define('NO_DATABASE_VENDOR_VERSION_FOUND', 10);
/** XML Processing Error */
define('NO_UNICODE_SECTION_FOUND', 11);
/** XML Processing Error */
define('NO_CUSTOM_CHECK_FOUND', 12);
/** XML Processing Error */
define('CUSTOM_CHECK_FILE_MISSING', 13);
/** XML Processing Error */
define('CUSTOM_CHECK_FUNCTION_MISSING', 14);
/** XML Processing Error */
define('NO_PHP_SETTINGS_NAME_FOUND', 15);
/// Define algorithm used to select the xml file
/** To select the newer file available to perform checks */
define('ENV_SELECT_NEWER', 0);
/** To enforce the use of the file under dataroot */
define('ENV_SELECT_DATAROOT', 1);
/** To enforce the use of the file under admin (release) */
define('ENV_SELECT_RELEASE', 2);
/**
* This function will perform the whole check, returning
* true or false as final result. Also, he full array of
* environment_result will be returned in the parameter list.
* The function looks for the best version to compare and
* everything. This is the only function that should be called
* ever from the rest of Moodle.
*
* @staticvar bool $result
* @staticvar array $env_results
* @staticvar bool $cache_exists
* @param string $version version to check.
* @param array $environment_results results array of results checked.
* @param boolean $print_table true/false, whether to print the table or just return results array
* @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use. Default ENV_SELECT_NEWER (BC)
* @return boolean true/false, depending of results
*/
function check_moodle_environment($version, &$environment_results, $print_table=true, $env_select=ENV_SELECT_NEWER) {
$status = true;
/// This are cached per request
static $result = true;
static $env_results;
static $cache_exists = false;
/// if we have results cached, use them
if ($cache_exists) {
$environment_results = $env_results;
/// No cache exists, calculate everything
} else {
/// Get the more recent version before the requested
if (!$version = get_latest_version_available($version, $env_select)) {
$status = false;
}
/// Perform all the checks
if (!($environment_results = environment_check($version, $env_select)) && $status) {
$status = false;
}
/// Iterate over all the results looking for some error in required items
/// or some error_code
if ($status) {
foreach ($environment_results as $environment_result) {
if (!$environment_result->getStatus() && $environment_result->getLevel() == 'required'
&& !$environment_result->getBypassStr()) {
$result = false; // required item that is not bypased
} else if ($environment_result->getStatus() && $environment_result->getLevel() == 'required'
&& $environment_result->getRestrictStr()) {
$result = false; // required item that is restricted
} else if ($environment_result->getErrorCode()) {
$result = false;
}
}
}
/// Going to end, we store environment_results to cache
$env_results = $environment_results;
$cache_exists = true;
} ///End of cache block
/// If we have decided to print all the information, just do it
if ($print_table) {
print_moodle_environment($result && $status, $environment_results);
}
return ($result && $status);
}
/**
* This function will print one beautiful table with all the environmental
* configuration and how it suits Moodle needs.
*
* @global object
* @param boolean $result final result of the check (true/false)
* @param array $environment_results array of results gathered
* @return void
*/
function print_moodle_environment($result, $environment_results) {
global $CFG, $OUTPUT;
/// Get some strings
$strname = get_string('name');
$strinfo = get_string('info');
$strreport = get_string('report');
$strstatus = get_string('status');
$strok = get_string('ok');
$strerror = get_string('error');
$strcheck = get_string('check');
$strbypassed = get_string('bypassed');
$strrestricted = get_string('restricted');
$strenvironmenterrortodo = get_string('environmenterrortodo', 'admin');
/// Table headers
$servertable = new html_table();//table for server checks
$servertable->head = array ($strname, $strinfo, $strreport, $strstatus);
$servertable->align = array ('center', 'center', 'left', 'center');
$servertable->wrap = array ('nowrap', '', '', 'nowrap');
$servertable->size = array ('10', 10, '100%', '10');
$servertable->attributes['class'] = 'environmenttable generaltable';
$serverdata = array('ok'=>array(), 'warn'=>array(), 'error'=>array());
$othertable = new html_table();//table for custom checks
$othertable->head = array ($strinfo, $strreport, $strstatus);
$othertable->align = array ('center', 'left', 'center');
$othertable->wrap = array ('', '', 'nowrap');
$othertable->size = array (10, '100%', '10');
$othertable->attributes['class'] = 'environmenttable generaltable';
$otherdata = array('ok'=>array(), 'warn'=>array(), 'error'=>array());
/// Iterate over each environment_result
$continue = true;
foreach ($environment_results as $environment_result) {
$errorline = false;
$warningline = false;
$stringtouse = '';
if ($continue) {
$type = $environment_result->getPart();
$info = $environment_result->getInfo();
$status = $environment_result->getStatus();
$error_code = $environment_result->getErrorCode();
/// Process Report field
$rec = new stdClass();
/// Something has gone wrong at parsing time
if ($error_code) {
$stringtouse = 'environmentxmlerror';
$rec->error_code = $error_code;
$status = $strerror;
$errorline = true;
$continue = false;
}
if ($continue) {
/// We are comparing versions
if ($rec->needed = $environment_result->getNeededVersion()) {
$rec->current = $environment_result->getCurrentVersion();
if ($environment_result->getLevel() == 'required') {
$stringtouse = 'environmentrequireversion';
} else {
$stringtouse = 'environmentrecommendversion';
}
/// We are checking installed & enabled things
} else if ($environment_result->getPart() == 'custom_check') {
if ($environment_result->getLevel() == 'required') {
$stringtouse = 'environmentrequirecustomcheck';
} else {
$stringtouse = 'environmentrecommendcustomcheck';
}
} else if ($environment_result->getPart() == 'php_setting') {
if ($status) {
$stringtouse = 'environmentsettingok';
} else if ($environment_result->getLevel() == 'required') {
$stringtouse = 'environmentmustfixsetting';
} else {
$stringtouse = 'environmentshouldfixsetting';
}
} else {
if ($environment_result->getLevel() == 'required') {
$stringtouse = 'environmentrequireinstall';
} else {
$stringtouse = 'environmentrecommendinstall';
}
}
/// Calculate the status value
if ($environment_result->getBypassStr() != '') { //Handle bypassed result (warning)
$status = $strbypassed;
$warningline = true;
} else if ($environment_result->getRestrictStr() != '') { //Handle restricted result (error)
$status = $strrestricted;
$errorline = true;
} else {
if ($status) { //Handle ok result (ok)
$status = $strok;
} else {
if ($environment_result->getLevel() == 'optional') {//Handle check result (warning)
$status = $strcheck;
$warningline = true;
} else { //Handle error result (error)
$status = $strcheck;
$errorline = true;
}
}
}
}
/// Build the text
$linkparts = array();
$linkparts[] = 'admin/environment';
$linkparts[] = $type;
if (!empty($info)){
$linkparts[] = $info;
}
if (empty($CFG->docroot)) {
$report = get_string($stringtouse, 'admin', $rec);
} else {
$report = $OUTPUT->doc_link(join($linkparts, '/'), get_string($stringtouse, 'admin', $rec));
}
/// Format error or warning line
if ($errorline || $warningline) {
$messagetype = $errorline? 'error':'warn';
} else {
$messagetype = 'ok';
}
$status = '<span class="'.$messagetype.'">'.$status.'</span>';
/// Here we'll store all the feedback found
$feedbacktext = '';
///Append the feedback if there is some
$feedbacktext .= $environment_result->strToReport($environment_result->getFeedbackStr(), $messagetype);
///Append the bypass if there is some
$feedbacktext .= $environment_result->strToReport($environment_result->getBypassStr(), 'warn');
///Append the restrict if there is some
$feedbacktext .= $environment_result->strToReport($environment_result->getRestrictStr(), 'error');
$report .= $feedbacktext;
/// Add the row to the table
if ($environment_result->getPart() == 'custom_check'){
$otherdata[$messagetype][] = array ($info, $report, $status);
} else {
$serverdata[$messagetype][] = array ($type, $info, $report, $status);
}
}
}
//put errors first in
$servertable->data = array_merge($serverdata['error'], $serverdata['warn'], $serverdata['ok']);
$othertable->data = array_merge($otherdata['error'], $otherdata['warn'], $otherdata['ok']);
/// Print table
echo $OUTPUT->heading(get_string('serverchecks', 'admin'));
echo html_writer::table($servertable);
if (count($othertable->data)){
echo $OUTPUT->heading(get_string('customcheck', 'admin'));
echo html_writer::table($othertable);
}
/// Finally, if any error has happened, print the summary box
if (!$result) {
echo $OUTPUT->box($strenvironmenterrortodo, 'environmentbox errorbox');
}
}
/**
* Returns array of critical errors in plain text format
* @param array $environment_results array of results gathered
* @return array errors
*/
function environment_get_errors($environment_results) {
global $CFG;
$errors = array();
// Iterate over each environment_result
foreach ($environment_results as $environment_result) {
$type = $environment_result->getPart();
$info = $environment_result->getInfo();
$status = $environment_result->getStatus();
$error_code = $environment_result->getErrorCode();
$a = new object();
if ($error_code) {
$a->error_code = $error_code;
$errors[] = array($info, get_string('environmentxmlerror', 'admin', $a));
return $errors;
}
/// Calculate the status value
if ($environment_result->getBypassStr() != '') {
// not interesting
continue;
} else if ($environment_result->getRestrictStr() != '') {
// error
} else {
if ($status) {
// ok
continue;
} else {
if ($environment_result->getLevel() == 'optional') {
// just a warning
continue;
} else {
// error
}
}
}
// We are comparing versions
if ($rec->needed = $environment_result->getNeededVersion()) {
$rec->current = $environment_result->getCurrentVersion();
if ($environment_result->getLevel() == 'required') {
$stringtouse = 'environmentrequireversion';
} else {
$stringtouse = 'environmentrecommendversion';
}
// We are checking installed & enabled things
} else if ($environment_result->getPart() == 'custom_check') {
if ($environment_result->getLevel() == 'required') {
$stringtouse = 'environmentrequirecustomcheck';
} else {
$stringtouse = 'environmentrecommendcustomcheck';
}
} else if ($environment_result->getPart() == 'php_setting') {
if ($status) {
$stringtouse = 'environmentsettingok';
} else if ($environment_result->getLevel() == 'required') {
$stringtouse = 'environmentmustfixsetting';
} else {
$stringtouse = 'environmentshouldfixsetting';
}
} else {
if ($environment_result->getLevel() == 'required') {
$stringtouse = 'environmentrequireinstall';
} else {
$stringtouse = 'environmentrecommendinstall';
}
}
$report = get_string($stringtouse, 'admin', $rec);
// Here we'll store all the feedback found
$feedbacktext = '';
// Append the feedback if there is some
$feedbacktext .= $environment_result->strToReport($environment_result->getFeedbackStr(), 'error');
// Append the restrict if there is some
$feedbacktext .= $environment_result->strToReport($environment_result->getRestrictStr(), 'error');
$report .= html_to_text($feedbacktext);
if ($environment_result->getPart() == 'custom_check'){
$errors[] = array($info, $report);
} else {
$errors[] = array(($info !== '' ? "$type $info" : $type), $report);
}
}
return $errors;
}
/**
* This function will normalize any version to just a serie of numbers
* separated by dots. Everything else will be removed.
*
* @param string $version the original version
* @return string the normalized version
*/
function normalize_version($version) {
/// 1.9 Beta 2 should be read 1.9 on enviromental checks, not 1.9.2
/// we can discard everything after the first space
$version = trim($version);
$versionarr = explode(" ",$version);
if (!empty($versionarr)) {
$version = $versionarr[0];
}
/// Replace everything but numbers and dots by dots
$version = preg_replace('/[^\.\d]/', '.', $version);
/// Combine multiple dots in one
$version = preg_replace('/(\.{2,})/', '.', $version);
/// Trim possible leading and trailing dots
$version = trim($version, '.');
return $version;
}
/**
* This function will load the environment.xml file and xmlize it
*
* @global object
* @staticvar mixed $data
* @uses ENV_SELECT_NEWER
* @uses ENV_SELECT_DATAROOT
* @uses ENV_SELECT_RELEASE
* @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use. Default ENV_SELECT_NEWER (BC)
* @return mixed the xmlized structure or false on error
*/
function load_environment_xml($env_select=ENV_SELECT_NEWER) {
global $CFG;
static $data; //Only load and xmlize once by request
if (!empty($data)) {
return $data;
}
/// First of all, take a look inside $CFG->dataroot/environment/environment.xml
$file = $CFG->dataroot.'/environment/environment.xml';
$internalfile = $CFG->dirroot.'/'.$CFG->admin.'/environment.xml';
switch ($env_select) {
case ENV_SELECT_NEWER:
if (!is_file($file) || !is_readable($file) || filemtime($file) < filemtime($internalfile) ||
!$contents = file_get_contents($file)) {
/// Fallback to fixed $CFG->admin/environment.xml
if (!is_file($internalfile) || !is_readable($internalfile) || !$contents = file_get_contents($internalfile)) {
return false;
}
}
break;
case ENV_SELECT_DATAROOT:
if (!is_file($file) || !is_readable($file) || !$contents = file_get_contents($file)) {
return false;
}
break;
case ENV_SELECT_RELEASE:
if (!is_file($internalfile) || !is_readable($internalfile) || !$contents = file_get_contents($internalfile)) {
return false;
}
break;
}
/// XML the whole file
$data = xmlize($contents);
return $data;
}
/**
* This function will return the list of Moodle versions available
*
* @staticvar array $versions
* @return mixed array of versions. False on error.
*/
function get_list_of_environment_versions ($contents) {
static $versions = array();
if (!empty($versions)) {
return $versions;
}
if (isset($contents['COMPATIBILITY_MATRIX']['#']['MOODLE'])) {
foreach ($contents['COMPATIBILITY_MATRIX']['#']['MOODLE'] as $version) {
$versions[] = $version['@']['version'];
}
}
return $versions;
}
/**
* This function will return the most recent version in the environment.xml
* file previous or equal to the version requested
*
* @param string $version top version from which we start to look backwards
* @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
* @return string|bool string more recent version or false if not found
*/
function get_latest_version_available ($version, $env_select) {
/// Normalize the version requested
$version = normalize_version($version);
/// Load xml file
if (!$contents = load_environment_xml($env_select)) {
return false;
}
/// Detect available versions
if (!$versions = get_list_of_environment_versions($contents)) {
return false;
}
/// First we look for exact version
if (in_array($version, $versions)) {
return $version;
} else {
$found_version = false;
/// Not exact match, so we are going to iterate over the list searching
/// for the latest version before the requested one
foreach ($versions as $arrversion) {
if (version_compare($arrversion, $version, '<')) {
$found_version = $arrversion;
}
}
}
return $found_version;
}
/**
* This function will return the xmlized data belonging to one Moodle version
*
* @param string $version top version from which we start to look backwards
* @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
* @return mixed the xmlized structure or false on error
*/
function get_environment_for_version($version, $env_select) {
/// Normalize the version requested
$version = normalize_version($version);
/// Load xml file
if (!$contents = load_environment_xml($env_select)) {
return false;
}
/// Detect available versions
if (!$versions = get_list_of_environment_versions($contents)) {
return false;
}
/// If the version requested is available
if (!in_array($version, $versions)) {
return false;
}
/// We now we have it. Extract from full contents.
$fl_arr = array_flip($versions);
return $contents['COMPATIBILITY_MATRIX']['#']['MOODLE'][$fl_arr[$version]];
}
/**
* This function will check for everything (DB, PHP and PHP extensions for now)
* returning an array of environment_result objects.
*
* @global object
* @param string $version xml version we are going to use to test this server
* @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
* @return array array of results encapsulated in one environment_result object
*/
function environment_check($version, $env_select) {
global $CFG;
/// Normalize the version requested
$version = normalize_version($version);
$results = array(); //To store all the results
/// Only run the moodle versions checker on upgrade, not on install
if (!empty($CFG->version)) {
$results[] = environment_check_moodle($version, $env_select);
}
$results[] = environment_check_unicode($version, $env_select);
$results[] = environment_check_database($version, $env_select);
$results[] = environment_check_php($version, $env_select);
$phpext_results = environment_check_php_extensions($version, $env_select);
$results = array_merge($results, $phpext_results);
$phpsetting_results = environment_check_php_settings($version, $env_select);
$results = array_merge($results, $phpsetting_results);
$custom_results = environment_custom_checks($version, $env_select);
$results = array_merge($results, $custom_results);
return $results;
}
/**
* This function will check if php extensions requirements are satisfied
*
* @uses NO_VERSION_DATA_FOUND
* @uses NO_PHP_EXTENSIONS_SECTION_FOUND
* @uses NO_PHP_EXTENSIONS_NAME_FOUND
* @param string $version xml version we are going to use to test this server
* @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
* @return array array of results encapsulated in one environment_result object
*/
function environment_check_php_extensions($version, $env_select) {
$results = array();
/// Get the enviroment version we need
if (!$data = get_environment_for_version($version, $env_select)) {
/// Error. No version data found
$result = new environment_results('php_extension');
$result->setStatus(false);
$result->setErrorCode(NO_VERSION_DATA_FOUND);
return array($result);
}
/// Extract the php_extension part
if (!isset($data['#']['PHP_EXTENSIONS']['0']['#']['PHP_EXTENSION'])) {
/// Error. No PHP section found
$result = new environment_results('php_extension');
$result->setStatus(false);
$result->setErrorCode(NO_PHP_EXTENSIONS_SECTION_FOUND);
return array($result);
}
/// Iterate over extensions checking them and creating the needed environment_results
foreach($data['#']['PHP_EXTENSIONS']['0']['#']['PHP_EXTENSION'] as $extension) {
$result = new environment_results('php_extension');
/// Check for level
$level = get_level($extension);
/// Check for extension name
if (!isset($extension['@']['name'])) {
$result->setStatus(false);
$result->setErrorCode(NO_PHP_EXTENSIONS_NAME_FOUND);
} else {
$extension_name = $extension['@']['name'];
/// The name exists. Just check if it's an installed extension
if (!extension_loaded($extension_name)) {
$result->setStatus(false);
} else {
$result->setStatus(true);
}
$result->setLevel($level);
$result->setInfo($extension_name);
}
/// Do any actions defined in the XML file.
process_environment_result($extension, $result);
/// Add the result to the array of results
$results[] = $result;
}
return $results;
}
/**
* This function will check if php extensions requirements are satisfied
*
* @uses NO_VERSION_DATA_FOUND
* @uses NO_PHP_SETTINGS_NAME_FOUND
* @param string $version xml version we are going to use to test this server
* @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
* @return array array of results encapsulated in one environment_result object
*/
function environment_check_php_settings($version, $env_select) {
$results = array();
/// Get the enviroment version we need
if (!$data = get_environment_for_version($version, $env_select)) {
/// Error. No version data found
$result = new environment_results('php_setting');
$result->setStatus(false);
$result->setErrorCode(NO_VERSION_DATA_FOUND);
return $result;
}
/// Extract the php_setting part
if (!isset($data['#']['PHP_SETTINGS']['0']['#']['PHP_SETTING'])) {
/// No PHP section found - ignore
return $results;
}
/// Iterate over settings checking them and creating the needed environment_results
foreach($data['#']['PHP_SETTINGS']['0']['#']['PHP_SETTING'] as $setting) {
$result = new environment_results('php_setting');
/// Check for level
$level = get_level($setting);
$result->setLevel($level);
/// Check for extension name
if (!isset($setting['@']['name'])) {
$result->setStatus(false);
$result->setErrorCode(NO_PHP_SETTINGS_NAME_FOUND);
} else {
$setting_name = $setting['@']['name'];
$setting_value = $setting['@']['value'];
$result->setInfo($setting_name);
if ($setting_name == 'memory_limit') {
$current = ini_get('memory_limit');
if ($current == -1) {
$result->setStatus(true);
} else {
$current = get_real_size($current);
$minlimit = get_real_size($setting_value);
if ($current < $minlimit) {
@ini_set('memory_limit', $setting_value);
$current = ini_get('memory_limit');
$current = get_real_size($current);
}
$result->setStatus($current >= $minlimit);
}
} else {
$current = ini_get_bool($setting_name);
/// The name exists. Just check if it's an installed extension
if ($current == $setting_value) {
$result->setStatus(true);
} else {
$result->setStatus(false);
}
}
}
/// Do any actions defined in the XML file.
process_environment_result($setting, $result);
/// Add the result to the array of results
$results[] = $result;
}
return $results;
}
/**
* This function will do the custom checks.
*
* @global object
* @uses CUSTOM_CHECK_FUNCTION_MISSING
* @uses CUSTOM_CHECK_FILE_MISSING
* @uses NO_CUSTOM_CHECK_FOUND
* @param string $version xml version we are going to use to test this server.
* @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
* @return array array of results encapsulated in environment_result objects.
*/
function environment_custom_checks($version, $env_select) {
global $CFG;
$results = array();
/// Get current Moodle version (release) for later compare
$release = isset($CFG->release) ? $CFG->release : $version; /// In case $CFG fails (at install) use $version
$current_version = normalize_version($release);
/// Get the enviroment version we need
if (!$data = get_environment_for_version($version, $env_select)) {
/// Error. No version data found - but this will already have been reported.
return $results;
}
/// Extract the CUSTOM_CHECKS part
if (!isset($data['#']['CUSTOM_CHECKS']['0']['#']['CUSTOM_CHECK'])) {
/// No custom checks found - not a problem
return $results;
}
/// Iterate over extensions checking them and creating the needed environment_results
foreach($data['#']['CUSTOM_CHECKS']['0']['#']['CUSTOM_CHECK'] as $check) {
$result = new environment_results('custom_check');
/// Check for level
$level = get_level($check);
/// Check for extension name
if (isset($check['@']['file']) && isset($check['@']['function'])) {
$file = $CFG->dirroot . '/' . $check['@']['file'];
$function = $check['@']['function'];
if (is_readable($file)) {
include_once($file);
if (function_exists($function)) {
$result->setLevel($level);
$result->setInfo($function);
$result = $function($result);
} else {
/// Only show error for current version (where function MUST exist)
/// else, we are performing custom checks against future versiosn
/// and function MAY not exist, so it doesn't cause error, just skip
/// custom check by returning null. MDL-15939
if (version_compare($current_version, $version, '>=')) {
$result->setStatus(false);
$result->setInfo($function);
$result->setErrorCode(CUSTOM_CHECK_FUNCTION_MISSING);
} else {
$result = null;
}
}
} else {
/// Only show error for current version (where function MUST exist)
/// else, we are performing custom checks against future versiosn
/// and function MAY not exist, so it doesn't cause error, just skip
/// custom check by returning null. MDL-15939
if (version_compare($current_version, $version, '>=')) {
$result->setStatus(false);
$result->setInfo($function);
$result->setErrorCode(CUSTOM_CHECK_FILE_MISSING);
} else {
$result = null;
}
}
} else {
$result->setStatus(false);
$result->setErrorCode(NO_CUSTOM_CHECK_FOUND);
}
if (!is_null($result)) {
/// Do any actions defined in the XML file.
process_environment_result($check, $result);
/// Add the result to the array of results
$results[] = $result;
}
}
return $results;
}
/**
* This function will check if Moodle requirements are satisfied
*
* @uses NO_VERSION_DATA_FOUND
* @param string $version xml version we are going to use to test this server
* @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
* @return object results encapsulated in one environment_result object
*/
function environment_check_moodle($version, $env_select) {
$result = new environment_results('moodle');
/// Get the enviroment version we need
if (!$data = get_environment_for_version($version, $env_select)) {
/// Error. No version data found
$result->setStatus(false);
$result->setErrorCode(NO_VERSION_DATA_FOUND);
return $result;
}
/// Extract the moodle part
if (!isset($data['@']['requires'])) {
$needed_version = '1.0'; /// Default to 1.0 if no moodle requires is found
} else {
/// Extract required moodle version
$needed_version = $data['@']['requires'];
}
/// Now search the version we are using
$current_version = normalize_version(get_config('', 'release'));
/// And finally compare them, saving results
if (version_compare($current_version, $needed_version, '>=')) {
$result->setStatus(true);
} else {
$result->setStatus(false);
}
$result->setLevel('required');
$result->setCurrentVersion($current_version);
$result->setNeededVersion($needed_version);
return $result;
}
/**
* This function will check if php requirements are satisfied
*
* @uses NO_VERSION_DATA_FOUND
* @uses NO_PHP_SECTION_FOUND
* @uses NO_PHP_VERSION_FOUND
* @param string $version xml version we are going to use to test this server
* @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
* @return object results encapsulated in one environment_result object
*/
function environment_check_php($version, $env_select) {
$result = new environment_results('php');
/// Get the enviroment version we need
if (!$data = get_environment_for_version($version, $env_select)) {
/// Error. No version data found
$result->setStatus(false);
$result->setErrorCode(NO_VERSION_DATA_FOUND);
return $result;
}
/// Extract the php part
if (!isset($data['#']['PHP'])) {
/// Error. No PHP section found
$result->setStatus(false);
$result->setErrorCode(NO_PHP_SECTION_FOUND);
return $result;
} else {
/// Extract level and version
$level = get_level($data['#']['PHP']['0']);
if (!isset($data['#']['PHP']['0']['@']['version'])) {
$result->setStatus(false);
$result->setErrorCode(NO_PHP_VERSION_FOUND);
return $result;
} else {
$needed_version = $data['#']['PHP']['0']['@']['version'];
}
}
/// Now search the version we are using
$current_version = normalize_version(phpversion());
/// And finally compare them, saving results
if (version_compare($current_version, $needed_version, '>=')) {
$result->setStatus(true);
} else {
$result->setStatus(false);
}
$result->setLevel($level);
$result->setCurrentVersion($current_version);
$result->setNeededVersion($needed_version);
/// Do any actions defined in the XML file.
process_environment_result($data['#']['PHP'][0], $result);
return $result;
}
/**
* This function will check if unicode database requirements are satisfied
*
* @global object
* @uses NO_VERSION_DATA_FOUND
* @uses NO_UNICODE_SECTION_FOUND
* @param string $version xml version we are going to use to test this server
* @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
* @return object results encapsulated in one environment_result object
*/
function environment_check_unicode($version, $env_select) {
global $DB;
$result = new environment_results('unicode');
/// Get the enviroment version we need
if (!$data = get_environment_for_version($version, $env_select)) {
/// Error. No version data found
$result->setStatus(false);
$result->setErrorCode(NO_VERSION_DATA_FOUND);
return $result;
}
/// Extract the unicode part
if (!isset($data['#']['UNICODE'])) {
/// Error. No UNICODE section found