Skip to content

Commit

Permalink
MDL-57432 lib: unify PHP version checks
Browse files Browse the repository at this point in the history
* Introduce a new php compatible library which can be used
  early in execution, without Moodle dependencies
* Use it where we previously had hardcoded checks

This was originally suggested in MDL-39007.
  • Loading branch information
danpoltawski committed Jul 10, 2017
1 parent 350700b commit 64e5793
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 32 deletions.
11 changes: 3 additions & 8 deletions admin/cli/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,9 @@

define('IGNORE_COMPONENT_CACHE', true);

// Check that PHP is of a sufficient version
if (version_compare(phpversion(), "5.6.5") < 0) {
$phpversion = phpversion();
// do NOT localise - lang strings would not work here and we CAN NOT move it after installib
fwrite(STDERR, "Moodle 3.2 or later requires at least PHP 5.6.5 (currently using version $phpversion).\n");
fwrite(STDERR, "Please upgrade your server software or install older Moodle version.\n");
exit(1);
}
// Check that PHP is of a sufficient version as soon as possible.
require_once(__DIR__.'/../../lib/phpminimumversionlib.php');
moodle_require_minimum_php_version();

// set up configuration
global $CFG;
Expand Down
11 changes: 3 additions & 8 deletions admin/cli/install_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,9 @@
\$sudo -u www-data /usr/bin/php admin/cli/install_database.php --lang=cs --adminpass=soMePass123 --agree-license
";

// Check that PHP is of a sufficient version
if (version_compare(phpversion(), "5.6.5") < 0) {
$phpversion = phpversion();
// do NOT localise - lang strings would not work here and we CAN NOT move it after installib
fwrite(STDERR, "Moodle 3.2 or later requires at least PHP 5.6.5 (currently using version $phpversion).\n");
fwrite(STDERR, "Please upgrade your server software or install older Moodle version.\n");
exit(1);
}
// Check that PHP is of a sufficient version as soon as possible.
require_once(__DIR__.'/../../lib/phpminimumversionlib.php');
moodle_require_minimum_php_version();

// Nothing to do if config.php does not exist
$configfile = __DIR__.'/../../config.php';
Expand Down
11 changes: 3 additions & 8 deletions admin/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,9 @@
die();
}

// Check that PHP is of a sufficient version as soon as possible
if (version_compare(phpversion(), '5.6.5') < 0) {
$phpversion = phpversion();
// do NOT localise - lang strings would not work here and we CAN NOT move it to later place
echo "Moodle 3.2 or later requires at least PHP 5.6.5 (currently using version $phpversion).<br />";
echo "Please upgrade your server software or install older Moodle version.";
die();
}
// Check that PHP is of a sufficient version as soon as possible.
require_once(__DIR__.'/../lib/phpminimumversionlib.php');
moodle_require_minimum_php_version();

// make sure iconv is available and actually works
if (!function_exists('iconv')) {
Expand Down
11 changes: 3 additions & 8 deletions install.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,9 @@
@error_reporting(E_ALL);
@ini_set('display_errors', '1');

// Check that PHP is of a sufficient version.
if (version_compare(phpversion(), '5.6.5') < 0) {
$phpversion = phpversion();
// do NOT localise - lang strings would not work here and we CAN not move it after installib
echo "Moodle 3.2 or later requires at least PHP 5.6.5 (currently using version $phpversion).<br />";
echo "Please upgrade your server software or install older Moodle version.";
die;
}
// Check that PHP is of a sufficient version as soon as possible.
require_once(__DIR__.'/../../lib/phpminimumversionlib.php');
moodle_require_minimum_php_version();

// make sure iconv is available and actually works
if (!function_exists('iconv')) {
Expand Down
76 changes: 76 additions & 0 deletions lib/phpminimumversionlib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?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/>.

// MOODLE_INTERNAL check intentionally missing to allow this to be used more widely!

/**
* A set of PHP-compatible convenience functions to check Moodle minimum PHP version in
* a unified place.
*
* PLEASE NOTE: This file is made to be both php-version compatible and without requirement on
* any moodle functions or installation so it can be used in installer or incompatible PHP versions.
*
* @package core
* @copyright 2017 Dan Poltawski <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

/**
* Require our minimum php version or halt execution if requirement not met.
* @return void Execution is halted if version is not met.
*/
function moodle_require_minimum_php_version() {
// PLEASE NOTE THIS FUNCTION MUST BE COMPATIBLE WITH OLD UNSUPPORTED VERSIONS OF PHP!
moodle_minimum_php_version_is_met(true);
}

/**
* Tests the current PHP version against Moodle's minimum requirement. When requirement
* is not met returns false or halts execution depending $haltexecution param.
*
* @param bool $haltexecution Should execution be halted when requirement not met? Defaults to false.
* @return bool returns true if requirement is met (false if not)
*/
function moodle_minimum_php_version_is_met($haltexecution = false) {
// PLEASE NOTE THIS FUNCTION MUST BE COMPATIBLE WITH OLD UNSUPPORTED VERSIONS OF PHP.
// Do not use modern php features or Moodle convenience functions (e.g. localised strings).

$minimumversion = '5.6.5';
$moodlerequirementchanged = '3.2';

if (version_compare(PHP_VERSION, $minimumversion) < 0) {
if ($haltexecution) {
$error = "Moodle ${moodlerequirementchanged} or later requires at least PHP ${minimumversion} "
. "(currently using version " . PHP_VERSION .").\n"
. "Some servers may have multiple PHP versions installed, are you using the correct executable?\n";

// Our CLI scripts define CLI_SCRIPT before running this test, so make use of
// to send error on STDERR.
if (defined('CLI_SCRIPT') && defined('STDERR')) {
fwrite(STDERR, $error);
} else {
echo $error;
}
exit(1);
} else {
return false;
}
}
return true;
}

// DO NOT ADD EXTRA FUNCTIONS TO THIS FILE!!
// This file must be functioning on all versions of PHP, extra functions belong elsewhere.

0 comments on commit 64e5793

Please sign in to comment.