3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * This script creates config.php file during installation.
23 * @copyright 2009 Petr Skoda (http://skodak.org)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 if (isset($_REQUEST['lang'])) {
28 $lang = preg_replace('/[^A-Za-z0-9_-]/i', '', $_REQUEST['lang']);
33 if (isset($_REQUEST['admin'])) {
34 $admin = preg_replace('/[^A-Za-z0-9_-]/i', '', $_REQUEST['admin']);
39 // If config.php exists we just created config.php and need to redirect to continue installation
40 $configfile = './config.php';
41 if (file_exists($configfile)) {
42 header("Location: $admin/index.php?lang=$lang");
46 define('CLI_SCRIPT', false); // prevents some warnings later
47 define('AJAX_SCRIPT', false); // prevents some warnings later
48 define('CACHE_DISABLE_ALL', true); // Disables caching.. just in case.
49 define('PHPUNIT_TEST', false);
51 // Servers should define a default timezone in php.ini, but if they don't then make sure something is defined.
52 // This is a quick hack. Ideally we should ask the admin for a value. See MDL-22625 for more on this.
53 if (function_exists('date_default_timezone_set') and function_exists('date_default_timezone_get')) {
54 @date_default_timezone_set
(@date_default_timezone_get
());
57 // make sure PHP errors are displayed - helps with diagnosing of problems
58 @error_reporting
(E_ALL
);
59 @ini_set
('display_errors', '1');
61 // Check that PHP is of a sufficient version.
62 if (version_compare(phpversion(), '5.3.3') < 0) {
63 $phpversion = phpversion();
64 // do NOT localise - lang strings would not work here and we CAN not move it after installib
65 echo "Moodle 2.5 or later requires at least PHP 5.3.3 (currently using version $phpversion).<br />";
66 echo "Please upgrade your server software or install older Moodle version.";
70 // make sure iconv is available and actually works
71 if (!function_exists('iconv')) {
72 // this should not happen, this must be very borked install
73 echo 'Moodle requires the iconv PHP extension. Please install or enable the iconv extension.';
77 if (PHP_INT_SIZE
> 4) {
78 // most probably 64bit PHP - we need a lot more memory
79 $minrequiredmemory = '70M';
82 $minrequiredmemory = '40M';
84 // increase or decrease available memory - we need to make sure moodle
85 // installs even with low memory, otherwise developers would overlook
86 // sudden increases of memory needs ;-)
87 @ini_set
('memory_limit', $minrequiredmemory);
89 /** Used by library scripts to check they are being called by Moodle */
90 define('MOODLE_INTERNAL', true);
92 require_once(__DIR__
.'/lib/classes/component.php');
93 require_once(__DIR__
.'/lib/installlib.php');
95 // TODO: add lang detection here if empty $_REQUEST['lang']
97 // distro specific customisation
99 if (file_exists('install/distrolib.php')) {
100 require_once('install/distrolib.php');
101 if (function_exists('distro_get_config')) {
102 $distro = distro_get_config();
106 $config = new stdClass();
107 $config->lang
= $lang;
109 if (!empty($_POST)) {
110 if (install_ini_get_bool('magic_quotes_gpc')) {
111 $_POST = array_map('stripslashes', $_POST);
114 $config->stage
= (int)$_POST['stage'];
116 if (isset($_POST['previous'])) {
118 if (INSTALL_DATABASETYPE
and !empty($distro->dbtype
)) {
121 if ($config->stage
== INSTALL_ENVIRONMENT
or $config->stage
== INSTALL_DOWNLOADLANG
) {
124 } else if (isset($_POST['next'])) {
128 $config->dbtype
= trim($_POST['dbtype']);
129 $config->dbhost
= trim($_POST['dbhost']);
130 $config->dbuser
= trim($_POST['dbuser']);
131 $config->dbpass
= trim($_POST['dbpass']);
132 $config->dbname
= trim($_POST['dbname']);
133 $config->prefix
= trim($_POST['prefix']);
134 $config->dbport
= (int)trim($_POST['dbport']);
135 $config->dbsocket
= trim($_POST['dbsocket']);
137 if ($config->dbport
<= 0) {
138 $config->dbport
= '';
141 $config->admin
= empty($_POST['admin']) ?
'admin' : trim($_POST['admin']);
143 $config->dataroot
= trim($_POST['dataroot']);
146 $config->stage
= INSTALL_WELCOME
;
148 $config->dbtype
= empty($distro->dbtype
) ?
'' : $distro->dbtype
; // let distro skip dbtype selection
149 $config->dbhost
= empty($distro->dbhost
) ?
'localhost' : $distro->dbhost
; // let distros set dbhost
150 $config->dbuser
= empty($distro->dbuser
) ?
'' : $distro->dbuser
; // let distros set dbuser
151 $config->dbpass
= '';
152 $config->dbname
= 'moodle';
153 $config->prefix
= 'mdl_';
154 $config->dbport
= empty($distro->dbport
) ?
'' : $distro->dbport
;
155 $config->dbsocket
= empty($distro->dbsocket
) ?
'' : $distro->dbsocket
;
157 $config->admin
= 'admin';
159 $config->dataroot
= empty($distro->dataroot
) ?
null : $distro->dataroot
; // initialised later after including libs or by distro
162 // Fake some settings so that we can use selected functions from moodlelib.php, weblib.php and filelib.php.
163 $CFG = new stdClass();
164 $CFG->lang
= $config->lang
;
165 $CFG->dirroot
= dirname(__FILE__
);
166 $CFG->libdir
= "$CFG->dirroot/lib";
167 $CFG->wwwroot
= install_guess_wwwroot(); // can not be changed - ppl must use the real address when installing
168 $CFG->httpswwwroot
= $CFG->wwwroot
;
169 $CFG->dataroot
= $config->dataroot
;
170 $CFG->tempdir
= $CFG->dataroot
.'/temp';
171 $CFG->cachedir
= $CFG->dataroot
.'/cache';
172 $CFG->admin
= $config->admin
;
173 $CFG->docroot
= 'http://docs.moodle.org';
174 $CFG->langotherroot
= $CFG->dataroot
.'/lang';
175 $CFG->langlocalroot
= $CFG->dataroot
.'/lang';
176 $CFG->directorypermissions
= isset($distro->directorypermissions
) ?
$distro->directorypermissions
: 00777; // let distros set dir permissions
177 $CFG->running_installer
= true;
178 $CFG->early_install_lang
= true;
179 $CFG->ostype
= (stristr(PHP_OS
, 'win') && !stristr(PHP_OS
, 'darwin')) ?
'WINDOWS' : 'UNIX';
181 // Require all needed libs
182 require_once($CFG->libdir
.'/setuplib.php');
184 // we need to make sure we have enough memory to load all libraries
185 $memlimit = @ini_get
('memory_limit');
186 if (!empty($memlimit) and $memlimit != -1) {
187 if (get_real_size($memlimit) < get_real_size($minrequiredmemory)) {
188 // do NOT localise - lang strings would not work here and we CAN not move it to later place
189 echo "Moodle requires at least {$minrequiredmemory}B of PHP memory.<br />";
190 echo "Please contact server administrator to fix PHP.ini memory settings.";
195 // Continue with lib loading
196 require_once($CFG->libdir
.'/textlib.class.php');
197 require_once($CFG->libdir
.'/weblib.php');
198 require_once($CFG->libdir
.'/outputlib.php');
199 require_once($CFG->libdir
.'/dmllib.php');
200 require_once($CFG->libdir
.'/moodlelib.php');
201 require_once($CFG->libdir
.'/pagelib.php');
202 require_once($CFG->libdir
.'/deprecatedlib.php');
203 require_once($CFG->libdir
.'/adminlib.php');
204 require_once($CFG->libdir
.'/environmentlib.php');
205 require_once($CFG->libdir
.'/componentlib.class.php');
206 require_once($CFG->dirroot
.'/cache/lib.php');
208 //point pear include path to moodles lib/pear so that includes and requires will search there for files before anywhere else
209 //the problem is that we need specific version of quickforms and hacked excel files :-(
210 ini_set('include_path', $CFG->libdir
.'/pear' . PATH_SEPARATOR
. ini_get('include_path'));
211 //point zend include path to moodles lib/zend so that includes and requires will search there for files before anywhere else
212 ini_set('include_path', $CFG->libdir
.'/zend' . PATH_SEPARATOR
. ini_get('include_path'));
214 require('version.php');
215 $CFG->target_release
= $release;
217 $SESSION = new stdClass();
218 $SESSION->lang
= $CFG->lang
;
220 $USER = new stdClass();
223 $COURSE = new stdClass();
233 // Are we in help mode?
234 if (isset($_GET['help'])) {
235 install_print_help_page($_GET['help']);
238 //first time here? find out suitable dataroot
239 if (is_null($CFG->dataroot
)) {
240 $CFG->dataroot
= dirname(dirname(__FILE__
)).DIRECTORY_SEPARATOR
.'moodledata';
242 $i = 0; //safety check - dirname might return some unexpected results
243 while(is_dataroot_insecure()) {
244 $parrent = dirname($CFG->dataroot
);
246 if ($parrent == '/' or $parrent == '.' or preg_match('/^[a-z]:\\\?$/i', $parrent) or ($i > 100)) {
247 $CFG->dataroot
= ''; //can not find secure location for dataroot
250 $CFG->dataroot
= dirname($parrent).DIRECTORY_SEPARATOR
.'moodledata';
252 $config->dataroot
= $CFG->dataroot
;
253 $config->stage
= INSTALL_WELCOME
;
256 // now let's do the stage work
257 if ($config->stage
< INSTALL_WELCOME
) {
258 $config->stage
= INSTALL_WELCOME
;
260 if ($config->stage
> INSTALL_SAVE
) {
261 $config->stage
= INSTALL_SAVE
;
266 if ($config->stage
== INSTALL_SAVE
) {
267 $CFG->early_install_lang
= false;
269 $database = moodle_database
::get_driver_instance($config->dbtype
, 'native');
270 if (!$database->driver_installed()) {
271 $config->stage
= INSTALL_DATABASETYPE
;
273 if (function_exists('distro_pre_create_db')) { // Hook for distros needing to do something before DB creation
274 $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);
276 $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
));
278 if ($hint_database === '') {
279 $configphp = install_generate_configphp($database, $CFG);
282 if (($fh = @fopen
($configfile, 'w')) !== false) {
283 fwrite($fh, $configphp);
287 if (file_exists($configfile)) {
288 // config created, let's continue!
289 redirect("$CFG->wwwroot/$config->admin/index.php?lang=$config->lang");
292 install_print_header($config, 'config.php',
293 get_string('configurationcompletehead', 'install'),
294 get_string('configurationcompletesub', 'install').get_string('configfilenotwritten', 'install'));
295 echo '<div class="configphp"><pre>';
299 install_print_footer($config);
303 $config->stage
= INSTALL_DATABASE
;
310 if ($config->stage
== INSTALL_DOWNLOADLANG
) {
311 if (empty($CFG->dataroot
)) {
312 $config->stage
= INSTALL_PATHS
;
314 } else if (is_dataroot_insecure()) {
315 $hint_dataroot = get_string('pathsunsecuredataroot', 'install');
316 $config->stage
= INSTALL_PATHS
;
318 } else if (!file_exists($CFG->dataroot
)) {
320 $a->parent
= dirname($CFG->dataroot
);
321 $a->dataroot
= $CFG->dataroot
;
322 if (!is_writable($a->parent
)) {
323 $hint_dataroot = get_string('pathsroparentdataroot', 'install', $a);
324 $config->stage
= INSTALL_PATHS
;
326 if (!install_init_dataroot($CFG->dataroot
, $CFG->directorypermissions
)) {
327 $hint_dataroot = get_string('pathserrcreatedataroot', 'install', $a);
328 $config->stage
= INSTALL_PATHS
;
332 } else if (!install_init_dataroot($CFG->dataroot
, $CFG->directorypermissions
)) {
333 $hint_dataroot = get_string('pathserrcreatedataroot', 'install', array('dataroot' => $CFG->dataroot
));
334 $config->stage
= INSTALL_PATHS
;
337 if (empty($hint_dataroot) and !is_writable($CFG->dataroot
)) {
338 $hint_dataroot = get_string('pathsrodataroot', 'install');
339 $config->stage
= INSTALL_PATHS
;
342 if ($config->admin
=== '' or !file_exists($CFG->dirroot
.'/'.$config->admin
.'/environment.xml')) {
343 $hint_admindir = get_string('pathswrongadmindir', 'install');
344 $config->stage
= INSTALL_PATHS
;
350 if ($config->stage
== INSTALL_DOWNLOADLANG
) {
351 // no need to download anything if en lang selected
352 if ($CFG->lang
== 'en') {
353 $config->stage
= INSTALL_DATABASETYPE
;
359 if ($config->stage
== INSTALL_DATABASETYPE
) {
360 // skip db selection if distro package supports only one db
361 if (!empty($distro->dbtype
)) {
362 $config->stage
= INSTALL_DATABASE
;
367 if ($config->stage
== INSTALL_DOWNLOADLANG
) {
370 // download and install required lang packs, the lang dir has already been created in install_init_dataroot
371 $installer = new lang_installer($CFG->lang
);
372 $results = $installer->run();
373 foreach ($results as $langcode => $langstatus) {
374 if ($langstatus === lang_installer
::RESULT_DOWNLOADERROR
) {
376 $a->url
= $installer->lang_pack_url($langcode);
377 $a->dest
= $CFG->dataroot
.'/lang';
378 $downloaderror = get_string('remotedownloaderror', 'error', $a);
382 if ($downloaderror !== '') {
383 install_print_header($config, get_string('language'), get_string('langdownloaderror', 'install', $CFG->lang
), $downloaderror);
384 install_print_footer($config);
387 if (empty($distro->dbtype
)) {
388 $config->stage
= INSTALL_DATABASETYPE
;
390 $config->stage
= INSTALL_DATABASE
;
394 // switch the string_manager instance to stop using install/lang/
395 $CFG->early_install_lang
= false;
396 $CFG->langotherroot
= $CFG->dataroot
.'/lang';
397 $CFG->langlocalroot
= $CFG->dataroot
.'/lang';
398 get_string_manager(true);
402 if ($config->stage
== INSTALL_DATABASE
) {
403 $CFG->early_install_lang
= false;
405 $database = moodle_database
::get_driver_instance($config->dbtype
, 'native');
407 $sub = '<h3>'.$database->get_name().'</h3>'.$database->get_configuration_help();
409 install_print_header($config, get_string('database', 'install'), get_string('databasehead', 'install'), $sub);
411 $strdbhost = get_string('databasehost', 'install');
412 $strdbname = get_string('databasename', 'install');
413 $strdbuser = get_string('databaseuser', 'install');
414 $strdbpass = get_string('databasepass', 'install');
415 $strprefix = get_string('dbprefix', 'install');
416 $strdbport = get_string('databaseport', 'install');
417 $strdbsocket = get_string('databasesocket', 'install');
419 echo '<div class="userinput">';
421 $disabled = empty($distro->dbhost
) ?
'' : 'disabled="disabled';
422 echo '<div class="formrow"><label for="id_dbhost" class="formlabel">'.$strdbhost.'</label>';
423 echo '<input id="id_dbhost" name="dbhost" '.$disabled.' type="text" value="'.s($config->dbhost
).'" size="50" class="forminput" />';
426 echo '<div class="formrow"><label for="id_dbname" class="formlabel">'.$strdbname.'</label>';
427 echo '<input id="id_dbname" name="dbname" type="text" value="'.s($config->dbname
).'" size="50" class="forminput" />';
430 $disabled = empty($distro->dbuser
) ?
'' : 'disabled="disabled';
431 echo '<div class="formrow"><label for="id_dbuser" class="formlabel">'.$strdbuser.'</label>';
432 echo '<input id="id_dbuser" name="dbuser" '.$disabled.' type="text" value="'.s($config->dbuser
).'" size="50" class="forminput" />';
435 echo '<div class="formrow"><label for="id_dbpass" class="formlabel">'.$strdbpass.'</label>';
436 // no password field here, the password may be visible in config.php if we can not write it to disk
437 echo '<input id="id_dbpass" name="dbpass" type="text" value="'.s($config->dbpass
).'" size="50" class="forminput" />';
440 echo '<div class="formrow"><label for="id_prefix" class="formlabel">'.$strprefix.'</label>';
441 echo '<input id="id_prefix" name="prefix" type="text" value="'.s($config->prefix
).'" size="10" class="forminput" />';
444 echo '<div class="formrow"><label for="id_prefix" class="formlabel">'.$strdbport.'</label>';
445 echo '<input id="id_dbport" name="dbport" type="text" value="'.s($config->dbport
).'" size="10" class="forminput" />';
448 if (!(stristr(PHP_OS
, 'win') && !stristr(PHP_OS
, 'darwin'))) {
449 echo '<div class="formrow"><label for="id_dbsocket" class="formlabel">'.$strdbsocket.'</label>';
450 echo '<input id="id_dbsocket" name="dbsocket" type="text" value="'.s($config->dbsocket
).'" size="50" class="forminput" />';
454 echo '<div class="hint">'.$hint_database.'</div>';
456 install_print_footer($config);
461 if ($config->stage
== INSTALL_DATABASETYPE
) {
462 $CFG->early_install_lang
= false;
464 // Finally ask for DB type
465 install_print_header($config, get_string('database', 'install'),
466 get_string('databasetypehead', 'install'),
467 get_string('databasetypesub', 'install'));
469 $databases = array('mysqli' => moodle_database
::get_driver_instance('mysqli', 'native'),
470 'pgsql' => moodle_database
::get_driver_instance('pgsql', 'native'),
471 'oci' => moodle_database
::get_driver_instance('oci', 'native'),
472 'sqlsrv' => moodle_database
::get_driver_instance('sqlsrv', 'native'), // MS SQL*Server PHP driver
473 'mssql' => moodle_database
::get_driver_instance('mssql', 'native'), // FreeTDS driver
476 echo '<div class="userinput">';
477 echo '<div class="formrow"><label class="formlabel" for="dbtype">'.get_string('dbtype', 'install').'</label>';
478 echo '<select id="dbtype" name="dbtype" class="forminput">';
481 foreach ($databases as $type=>$database) {
482 if ($database->driver_installed() !== true) {
483 $disabled[$type] = $database;
486 echo '<option value="'.s($type).'">'.$database->get_name().'</option>';
489 echo '<optgroup label="'.s(get_string('notavailable')).'">';
490 foreach ($disabled as $type=>$database) {
491 echo '<option value="'.s($type).'" class="notavailable">'.$database->get_name().'</option>';
495 echo '</select></div>';
498 install_print_footer($config);
504 if ($config->stage
== INSTALL_ENVIRONMENT
or $config->stage
== INSTALL_PATHS
) {
505 $version_fail = (version_compare(phpversion(), "5.3.3") < 0);
506 $curl_fail = ($lang !== 'en' and !extension_loaded('curl')); // needed for lang pack download
507 $zip_fail = ($lang !== 'en' and !extension_loaded('zip')); // needed for lang pack download
509 if ($version_fail or $curl_fail or $zip_fail) {
510 $config->stage
= INSTALL_ENVIRONMENT
;
512 install_print_header($config, get_string('environmenthead', 'install'),
513 get_string('errorsinenvironment', 'install'),
514 get_string('environmentsub2', 'install'));
516 echo '<div id="envresult"><dl>';
518 $a = (object)array('needed'=>'5.3.3', 'current'=>phpversion());
519 echo '<dt>'.get_string('phpversion', 'install').'</dt><dd>'.get_string('environmentrequireversion', 'admin', $a).'</dd>';
522 echo '<dt>'.get_string('phpextension', 'install', 'cURL').'</dt><dd>'.get_string('environmentrequireinstall', 'admin').'</dd>';
525 echo '<dt>'.get_string('phpextension', 'install', 'Zip').'</dt><dd>'.get_string('environmentrequireinstall', 'admin').'</dd>';
529 install_print_footer($config, true);
533 $config->stage
= INSTALL_PATHS
;
539 if ($config->stage
== INSTALL_PATHS
) {
540 $paths = array('wwwroot' => get_string('wwwroot', 'install'),
541 'dirroot' => get_string('dirroot', 'install'),
542 'dataroot' => get_string('dataroot', 'install'));
545 foreach ($paths as $path=>$name) {
546 $sub .= '<dt>'.$name.'</dt><dd>'.get_string('pathssub'.$path, 'install').'</dd>';
548 if (!file_exists("$CFG->dirroot/admin/environment.xml")) {
549 $sub .= '<dt>'.get_string('admindirname', 'install').'</dt><dd>'.get_string('pathssubadmindir', 'install').'</dd>';
553 install_print_header($config, get_string('paths', 'install'), get_string('pathshead', 'install'), $sub);
555 $strwwwroot = get_string('wwwroot', 'install');
556 $strdirroot = get_string('dirroot', 'install');
557 $strdataroot = get_string('dataroot', 'install');
558 $stradmindirname = get_string('admindirname', 'install');
560 echo '<div class="userinput">';
561 echo '<div class="formrow"><label for="id_wwwroot" class="formlabel">'.$paths['wwwroot'].'</label>';
562 echo '<input id="id_wwwroot" name="wwwroot" type="text" value="'.s($CFG->wwwroot
).'" disabled="disabled" size="70" class="forminput" />';
565 echo '<div class="formrow"><label for="id_dirroot" class="formlabel">'.$paths['dirroot'].'</label>';
566 echo '<input id="id_dirroot" name="dirroot" type="text" value="'.s($CFG->dirroot
).'" disabled="disabled" size="70"class="forminput" />';
569 echo '<div class="formrow"><label for="id_dataroot" class="formlabel">'.$paths['dataroot'].'</label>';
570 echo '<input id="id_dataroot" name="dataroot" type="text" value="'.s($config->dataroot
).'" size="70" class="forminput" />';
571 if ($hint_dataroot !== '') {
572 echo '<div class="hint">'.$hint_dataroot.'</div>';
577 if (!file_exists("$CFG->dirroot/admin/environment.xml")) {
578 echo '<div class="formrow"><label for="id_admin" class="formlabel">'.$paths['admindir'].'</label>';
579 echo '<input id="id_admin" name="admin" type="text" value="'.s($config->admin
).'" size="10" class="forminput" />';
580 if ($hint_admindir !== '') {
581 echo '<div class="hint">'.$hint_admindir.'</div>';
588 install_print_footer($config);
594 $config->stage
= INSTALL_WELCOME
;
598 include('install/distribution.html');
599 $sub = ob_get_clean();
601 install_print_header($config, get_string('language'),
602 get_string('chooselanguagehead', 'install'),
606 install_print_header($config, get_string('language'),
607 get_string('chooselanguagehead', 'install'),
608 get_string('chooselanguagesub', 'install'));
611 $languages = get_string_manager()->get_list_of_translations();
612 echo '<div class="userinput">';
613 echo '<div class="formrow"><label class="formlabel" for="langselect">'.get_string('language').'</label>';
614 echo '<select id="langselect" name="lang" class="forminput" onchange="this.form.submit()">';
615 foreach ($languages as $name=>$value) {
616 $selected = ($name == $CFG->lang
) ?
'selected="selected"' : '';
617 echo '<option value="'.s($name).'" '.$selected.'>'.$value.'</option>';
619 echo '</select></div>';
622 install_print_footer($config);