Merge branch 'MDL-70075-39-2' of git://github.com/andrewnicols/moodle into MOODLE_39_...
[moodle.git] / lib / phpminimumversionlib.php
blob0c3e0d179cc3a5094d1c809babda9b9a094b6084
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 // MOODLE_INTERNAL check intentionally missing to allow this to be used more widely!
19 /**
20 * A set of PHP-compatible convenience functions to check Moodle minimum PHP version in
21 * a unified place.
23 * PLEASE NOTE: This file is made to be both php-version compatible and without requirement on
24 * any moodle functions or installation so it can be used in installer or incompatible PHP versions.
26 * @package core
27 * @copyright 2017 Dan Poltawski <dan@moodle.com>
28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 /**
32 * Require our minimum php version or halt execution if requirement not met.
33 * @return void Execution is halted if version is not met.
35 function moodle_require_minimum_php_version() {
36 // PLEASE NOTE THIS FUNCTION MUST BE COMPATIBLE WITH OLD UNSUPPORTED VERSIONS OF PHP!
37 moodle_minimum_php_version_is_met(true);
40 /**
41 * Tests the current PHP version against Moodle's minimum requirement. When requirement
42 * is not met returns false or halts execution depending $haltexecution param.
44 * @param bool $haltexecution Should execution be halted when requirement not met? Defaults to false.
45 * @return bool returns true if requirement is met (false if not)
47 function moodle_minimum_php_version_is_met($haltexecution = false) {
48 // PLEASE NOTE THIS FUNCTION MUST BE COMPATIBLE WITH OLD UNSUPPORTED VERSIONS OF PHP.
49 // Do not use modern php features or Moodle convenience functions (e.g. localised strings).
51 $minimumversion = '7.1.0';
52 $moodlerequirementchanged = '3.7';
54 if (version_compare(PHP_VERSION, $minimumversion) < 0) {
55 if ($haltexecution) {
56 $error = "Moodle ${moodlerequirementchanged} or later requires at least PHP ${minimumversion} "
57 . "(currently using version " . PHP_VERSION .").\n"
58 . "Some servers may have multiple PHP versions installed, are you using the correct executable?\n";
60 // Our CLI scripts define CLI_SCRIPT before running this test, so make use of
61 // to send error on STDERR.
62 if (defined('CLI_SCRIPT') && defined('STDERR')) {
63 fwrite(STDERR, $error);
64 } else {
65 echo $error;
67 exit(1);
68 } else {
69 return false;
72 return true;
75 // DO NOT ADD EXTRA FUNCTIONS TO THIS FILE!!
76 // This file must be functioning on all versions of PHP, extra functions belong elsewhere.