.
/**
* This script creates config.php file during installation.
*
* @package core
* @subpackage install
* @copyright 2009 Petr Skoda (http://skodak.org)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
if (isset($_REQUEST['lang'])) {
$lang = preg_replace('/[^A-Za-z0-9_-]/i', '', $_REQUEST['lang']);
} else {
$lang = 'en';
}
if (isset($_REQUEST['admin'])) {
$admin = preg_replace('/[^A-Za-z0-9_-]/i', '', $_REQUEST['admin']);
} else {
$admin = 'admin';
}
// If config.php exists we just created config.php and need to redirect to continue installation
$configfile = './config.php';
if (file_exists($configfile)) {
header("Location: $admin/index.php?lang=$lang");
die;
}
define('CLI_SCRIPT', false); // prevents some warnings later
define('AJAX_SCRIPT', false); // prevents some warnings later
define('CACHE_DISABLE_ALL', true); // Disables caching.. just in case.
define('PHPUNIT_TEST', false);
define('IGNORE_COMPONENT_CACHE', true);
// Servers should define a default timezone in php.ini, but if they don't then make sure something is defined.
// This is a quick hack. Ideally we should ask the admin for a value. See MDL-22625 for more on this.
if (function_exists('date_default_timezone_set') and function_exists('date_default_timezone_get')) {
@date_default_timezone_set(@date_default_timezone_get());
}
// make sure PHP errors are displayed - helps with diagnosing of problems
@error_reporting(E_ALL);
@ini_set('display_errors', '1');
// Check that PHP is of a sufficient version.
if (version_compare(phpversion(), '5.4.4') < 0) {
$phpversion = phpversion();
// do NOT localise - lang strings would not work here and we CAN not move it after installib
echo "Moodle 2.7 or later requires at least PHP 5.4.4 (currently using version $phpversion).
";
echo "Please upgrade your server software or install older Moodle version.";
die;
}
// make sure iconv is available and actually works
if (!function_exists('iconv')) {
// this should not happen, this must be very borked install
echo 'Moodle requires the iconv PHP extension. Please install or enable the iconv extension.';
die();
}
if (PHP_INT_SIZE > 4) {
// most probably 64bit PHP - we need a lot more memory
$minrequiredmemory = '70M';
} else {
// 32bit PHP
$minrequiredmemory = '40M';
}
// increase or decrease available memory - we need to make sure moodle
// installs even with low memory, otherwise developers would overlook
// sudden increases of memory needs ;-)
@ini_set('memory_limit', $minrequiredmemory);
/** Used by library scripts to check they are being called by Moodle */
define('MOODLE_INTERNAL', true);
require_once(__DIR__.'/lib/classes/component.php');
require_once(__DIR__.'/lib/installlib.php');
// TODO: add lang detection here if empty $_REQUEST['lang']
// distro specific customisation
$distro = null;
if (file_exists('install/distrolib.php')) {
require_once('install/distrolib.php');
if (function_exists('distro_get_config')) {
$distro = distro_get_config();
}
}
$config = new stdClass();
$config->lang = $lang;
if (!empty($_POST)) {
$config->stage = (int)$_POST['stage'];
if (isset($_POST['previous'])) {
$config->stage--;
if (INSTALL_DATABASETYPE and !empty($distro->dbtype)) {
$config->stage--;
}
if ($config->stage == INSTALL_ENVIRONMENT or $config->stage == INSTALL_DOWNLOADLANG) {
$config->stage--;
}
} else if (isset($_POST['next'])) {
$config->stage++;
}
$config->dbtype = trim($_POST['dbtype']);
$config->dbhost = trim($_POST['dbhost']);
$config->dbuser = trim($_POST['dbuser']);
$config->dbpass = trim($_POST['dbpass']);
$config->dbname = trim($_POST['dbname']);
$config->prefix = trim($_POST['prefix']);
$config->dbport = (int)trim($_POST['dbport']);
$config->dbsocket = trim($_POST['dbsocket']);
if ($config->dbport <= 0) {
$config->dbport = '';
}
$config->admin = empty($_POST['admin']) ? 'admin' : trim($_POST['admin']);
$config->dataroot = trim($_POST['dataroot']);
} else {
$config->stage = INSTALL_WELCOME;
$config->dbtype = empty($distro->dbtype) ? '' : $distro->dbtype; // let distro skip dbtype selection
$config->dbhost = empty($distro->dbhost) ? 'localhost' : $distro->dbhost; // let distros set dbhost
$config->dbuser = empty($distro->dbuser) ? '' : $distro->dbuser; // let distros set dbuser
$config->dbpass = '';
$config->dbname = 'moodle';
$config->prefix = 'mdl_';
$config->dbport = empty($distro->dbport) ? '' : $distro->dbport;
$config->dbsocket = empty($distro->dbsocket) ? '' : $distro->dbsocket;
$config->admin = 'admin';
$config->dataroot = empty($distro->dataroot) ? null : $distro->dataroot; // initialised later after including libs or by distro
}
// Fake some settings so that we can use selected functions from moodlelib.php, weblib.php and filelib.php.
global $CFG;
$CFG = new stdClass();
$CFG->lang = $config->lang;
$CFG->dirroot = dirname(__FILE__);
$CFG->libdir = "$CFG->dirroot/lib";
$CFG->wwwroot = install_guess_wwwroot(); // can not be changed - ppl must use the real address when installing
$CFG->httpswwwroot = $CFG->wwwroot;
$CFG->dataroot = $config->dataroot;
$CFG->tempdir = $CFG->dataroot.'/temp';
$CFG->cachedir = $CFG->dataroot.'/cache';
$CFG->localcachedir = $CFG->dataroot.'/localcache';
$CFG->admin = $config->admin;
$CFG->docroot = 'http://docs.moodle.org';
$CFG->langotherroot = $CFG->dataroot.'/lang';
$CFG->langlocalroot = $CFG->dataroot.'/lang';
$CFG->directorypermissions = isset($distro->directorypermissions) ? $distro->directorypermissions : 00777; // let distros set dir permissions
$CFG->filepermissions = ($CFG->directorypermissions & 0666);
$CFG->umaskpermissions = (($CFG->directorypermissions & 0777) ^ 0777);
$CFG->running_installer = true;
$CFG->early_install_lang = true;
$CFG->ostype = (stristr(PHP_OS, 'win') && !stristr(PHP_OS, 'darwin')) ? 'WINDOWS' : 'UNIX';
$CFG->debug = (E_ALL | E_STRICT);
$CFG->debugdisplay = true;
$CFG->debugdeveloper = true;
// Require all needed libs
require_once($CFG->libdir.'/setuplib.php');
// we need to make sure we have enough memory to load all libraries
$memlimit = @ini_get('memory_limit');
if (!empty($memlimit) and $memlimit != -1) {
if (get_real_size($memlimit) < get_real_size($minrequiredmemory)) {
// do NOT localise - lang strings would not work here and we CAN not move it to later place
echo "Moodle requires at least {$minrequiredmemory}B of PHP memory.
";
echo "Please contact server administrator to fix PHP.ini memory settings.";
die;
}
}
// Continue with lib loading
require_once($CFG->libdir.'/classes/text.php');
require_once($CFG->libdir.'/classes/string_manager.php');
require_once($CFG->libdir.'/classes/string_manager_install.php');
require_once($CFG->libdir.'/classes/string_manager_standard.php');
require_once($CFG->libdir.'/weblib.php');
require_once($CFG->libdir.'/outputlib.php');
require_once($CFG->libdir.'/dmllib.php');
require_once($CFG->libdir.'/moodlelib.php');
require_once($CFG->libdir .'/pagelib.php');
require_once($CFG->libdir.'/deprecatedlib.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->libdir.'/environmentlib.php');
require_once($CFG->libdir.'/componentlib.class.php');
require_once($CFG->dirroot.'/cache/lib.php');
//point pear include path to moodles lib/pear so that includes and requires will search there for files before anywhere else
//the problem is that we need specific version of quickforms and hacked excel files :-(
ini_set('include_path', $CFG->libdir.'/pear' . PATH_SEPARATOR . ini_get('include_path'));
//point zend include path to moodles lib/zend so that includes and requires will search there for files before anywhere else
ini_set('include_path', $CFG->libdir.'/zend' . PATH_SEPARATOR . ini_get('include_path'));
// Register our classloader, in theory somebody might want to replace it to load other hacked core classes.
// Required because the database checks below lead to session interaction which is going to lead us to requiring autoloaded classes.
if (defined('COMPONENT_CLASSLOADER')) {
spl_autoload_register(COMPONENT_CLASSLOADER);
} else {
spl_autoload_register('core_component::classloader');
}
require('version.php');
$CFG->target_release = $release;
\core\session\manager::init_empty_session();
global $SESSION;
global $USER;
global $COURSE;
$COURSE = new stdClass();
$COURSE->id = 1;
global $SITE;
$SITE = $COURSE;
define('SITEID', 1);
$hint_dataroot = '';
$hint_admindir = '';
$hint_database = '';
// Are we in help mode?
if (isset($_GET['help'])) {
install_print_help_page($_GET['help']);
}
//first time here? find out suitable dataroot
if (is_null($CFG->dataroot)) {
$CFG->dataroot = dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'moodledata';
$i = 0; //safety check - dirname might return some unexpected results
while(is_dataroot_insecure()) {
$parrent = dirname($CFG->dataroot);
$i++;
if ($parrent == '/' or $parrent == '.' or preg_match('/^[a-z]:\\\?$/i', $parrent) or ($i > 100)) {
$CFG->dataroot = ''; //can not find secure location for dataroot
break;
}
$CFG->dataroot = dirname($parrent).DIRECTORY_SEPARATOR.'moodledata';
}
$config->dataroot = $CFG->dataroot;
$config->stage = INSTALL_WELCOME;
}
// now let's do the stage work
if ($config->stage < INSTALL_WELCOME) {
$config->stage = INSTALL_WELCOME;
}
if ($config->stage > INSTALL_SAVE) {
$config->stage = INSTALL_SAVE;
}
if ($config->stage == INSTALL_SAVE) {
$CFG->early_install_lang = false;
$database = moodle_database::get_driver_instance($config->dbtype, 'native');
if (!$database->driver_installed()) {
$config->stage = INSTALL_DATABASETYPE;
} else {
if (function_exists('distro_pre_create_db')) { // Hook for distros needing to do something before DB creation
$distro = distro_pre_create_db($database, $config->dbhost, $config->dbuser, $config->dbpass, $config->dbname, $config->prefix, array('dbpersist'=>0, 'dbport'=>$config->dbport, 'dbsocket'=>$config->dbsocket), $distro);
}
$hint_database = install_db_validate($database, $config->dbhost, $config->dbuser, $config->dbpass, $config->dbname, $config->prefix, array('dbpersist'=>0, 'dbport'=>$config->dbport, 'dbsocket'=>$config->dbsocket));
if ($hint_database === '') {
$configphp = install_generate_configphp($database, $CFG);
umask(0137);
if (($fh = @fopen($configfile, 'w')) !== false) {
fwrite($fh, $configphp);
fclose($fh);
}
if (file_exists($configfile)) {
// config created, let's continue!
redirect("$CFG->wwwroot/$config->admin/index.php?lang=$config->lang");
}
install_print_header($config, 'config.php',
get_string('configurationcompletehead', 'install'),
get_string('configurationcompletesub', 'install').get_string('configfilenotwritten', 'install'), 'alert-error');
echo '
'; echo p($configphp); echo '