Merge branch 'install_21_STABLE' of git://git.moodle.cz/moodle-install into MOODLE_21...
[moodle.git] / install.php
blobd5de87c1d30e77acd818cf89e9fe785e9a7df11f
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
9 //
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/>.
18 /**
19 * This script creates config.php file during installation.
21 * @package core
22 * @subpackage install
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']);
29 } else {
30 $lang = 'en';
33 if (isset($_REQUEST['admin'])) {
34 $admin = preg_replace('/[^A-Za-z0-9_-]/i', '', $_REQUEST['admin']);
35 } else {
36 $admin = '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");
43 die;
46 define('CLI_SCRIPT', false); // prevents some warnings later
47 define('AJAX_SCRIPT', false); // prevents some warnings later
49 // Servers should define a default timezone in php.ini, but if they don't then make sure something is defined.
50 // This is a quick hack. Ideally we should ask the admin for a value. See MDL-22625 for more on this.
51 if (function_exists('date_default_timezone_set') and function_exists('date_default_timezone_get')) {
52 @date_default_timezone_set(@date_default_timezone_get());
55 // make sure PHP errors are displayed - helps with diagnosing of problems
56 @error_reporting(E_ALL);
57 @ini_set('display_errors', '1');
59 // Check that PHP is of a sufficient version
60 // PHP 5.2.0 is intentionally checked here even though a higher version is required by the environment
61 // check. This is not a typo - see MDL-18112
62 if (version_compare(phpversion(), "5.2.0") < 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.1 or later requires at least PHP 5.3.2 (currently using version $phpversion).<br />";
66 echo "Please upgrade your server software or install older Moodle version.";
67 die;
70 if (PHP_INT_SIZE > 4) {
71 // most probably 64bit PHP - we need a lot more memory
72 $minrequiredmemory = '70M';
73 } else {
74 // 32bit PHP
75 $minrequiredmemory = '40M';
77 // increase or decrease available memory - we need to make sure moodle
78 // installs even with low memory, otherwise developers would overlook
79 // sudden increases of memory needs ;-)
80 @ini_set('memory_limit', $minrequiredmemory);
82 /** Used by library scripts to check they are being called by Moodle */
83 define('MOODLE_INTERNAL', true);
85 require dirname(__FILE__).'/lib/installlib.php';
87 // TODO: add lang detection here if empty $_REQUEST['lang']
89 // distro specific customisation
90 $distro = null;
91 if (file_exists('install/distrolib.php')) {
92 require_once('install/distrolib.php');
93 if (function_exists('distro_get_config')) {
94 $distro = distro_get_config();
98 $config = new stdClass();
99 $config->lang = $lang;
101 if (!empty($_POST)) {
102 if (install_ini_get_bool('magic_quotes_gpc')) {
103 $_POST = array_map('stripslashes', $_POST);
106 $config->stage = (int)$_POST['stage'];
108 if (isset($_POST['previous'])) {
109 $config->stage--;
110 if (INSTALL_DATABASETYPE and !empty($distro->dbtype)) {
111 $config->stage--;
113 if ($config->stage == INSTALL_ENVIRONMENT or $config->stage == INSTALL_DOWNLOADLANG) {
114 $config->stage--;
116 } else if (isset($_POST['next'])) {
117 $config->stage++;
120 $config->dbtype = trim($_POST['dbtype']);
121 $config->dbhost = trim($_POST['dbhost']);
122 $config->dbuser = trim($_POST['dbuser']);
123 $config->dbpass = trim($_POST['dbpass']);
124 $config->dbname = trim($_POST['dbname']);
125 $config->prefix = trim($_POST['prefix']);
126 $config->dbsocket = (int)(!empty($_POST['dbsocket']));
128 $config->admin = empty($_POST['admin']) ? 'admin' : trim($_POST['admin']);
130 $config->dataroot = trim($_POST['dataroot']);
132 } else {
133 $config->stage = INSTALL_WELCOME;
135 $config->dbtype = empty($distro->dbtype) ? '' : $distro->dbtype; // let distro skip dbtype selection
136 $config->dbhost = empty($distro->dbhost) ? 'localhost' : $distro->dbhost; // let distros set dbhost
137 $config->dbuser = empty($distro->dbuser) ? '' : $distro->dbuser; // let distros set dbuser
138 $config->dbpass = '';
139 $config->dbname = 'moodle';
140 $config->prefix = 'mdl_';
141 $config->dbsocket = 0;
143 $config->admin = 'admin';
145 $config->dataroot = empty($distro->dataroot) ? null : $distro->dataroot; // initialised later after including libs or by distro
148 // Fake some settings so that we can use selected functions from moodlelib.php and weblib.php
149 $CFG = new stdClass();
150 $CFG->lang = $config->lang;
151 $CFG->dirroot = dirname(__FILE__);
152 $CFG->libdir = "$CFG->dirroot/lib";
153 $CFG->wwwroot = install_guess_wwwroot(); // can not be changed - ppl must use the real address when installing
154 $CFG->httpswwwroot = $CFG->wwwroot;
155 $CFG->dataroot = $config->dataroot;
156 $CFG->admin = $config->admin;
157 $CFG->docroot = 'http://docs.moodle.org';
158 $CFG->langotherroot = $CFG->dataroot.'/lang';
159 $CFG->langlocalroot = $CFG->dataroot.'/lang';
160 $CFG->directorypermissions = isset($distro->directorypermissions) ? $distro->directorypermissions : 00777; // let distros set dir permissions
161 $CFG->running_installer = true;
162 $CFG->early_install_lang = true;
164 // Require all needed libs
165 require_once($CFG->libdir.'/setuplib.php');
167 // we need to make sure we have enough memory to load all libraries
168 $memlimit = @ini_get('memory_limit');
169 if (!empty($memlimit) and $memlimit != -1) {
170 if (get_real_size($memlimit) < get_real_size($minrequiredmemory)) {
171 // do NOT localise - lang strings would not work here and we CAN not move it to later place
172 echo "Moodle requires at least {$minrequiredmemory}B of PHP memory.<br />";
173 echo "Please contact server administrator to fix PHP.ini memory settings.";
174 die;
178 // Continue with lib loading
179 require_once($CFG->libdir.'/textlib.class.php');
180 require_once($CFG->libdir.'/weblib.php');
181 require_once($CFG->libdir.'/outputlib.php');
182 require_once($CFG->libdir.'/dmllib.php');
183 require_once($CFG->libdir.'/moodlelib.php');
184 require_once($CFG->libdir .'/pagelib.php');
185 require_once($CFG->libdir.'/deprecatedlib.php');
186 require_once($CFG->libdir.'/adminlib.php');
187 require_once($CFG->libdir.'/environmentlib.php');
188 require_once($CFG->libdir.'/componentlib.class.php');
190 //point pear include path to moodles lib/pear so that includes and requires will search there for files before anywhere else
191 //the problem is that we need specific version of quickforms and hacked excel files :-(
192 ini_set('include_path', $CFG->libdir.'/pear' . PATH_SEPARATOR . ini_get('include_path'));
193 //point zend include path to moodles lib/zend so that includes and requires will search there for files before anywhere else
194 ini_set('include_path', $CFG->libdir.'/zend' . PATH_SEPARATOR . ini_get('include_path'));
196 require('version.php');
197 $CFG->target_release = $release;
199 $SESSION = new stdClass();
200 $SESSION->lang = $CFG->lang;
202 $USER = new stdClass();
203 $USER->id = 0;
205 $COURSE = new stdClass();
206 $COURSE->id = 0;
208 $SITE = $COURSE;
209 define('SITEID', 0);
211 $hint_dataroot = '';
212 $hint_admindir = '';
213 $hint_database = '';
215 // Are we in help mode?
216 if (isset($_GET['help'])) {
217 install_print_help_page($_GET['help']);
220 // send css?
221 if (isset($_GET['css'])) {
222 install_css_styles();
225 //first time here? find out suitable dataroot
226 if (is_null($CFG->dataroot)) {
227 $CFG->dataroot = dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'moodledata';
229 $i = 0; //safety check - dirname might return some unexpected results
230 while(is_dataroot_insecure()) {
231 $parrent = dirname($CFG->dataroot);
232 $i++;
233 if ($parrent == '/' or $parrent == '.' or preg_match('/^[a-z]:\\\?$/i', $parrent) or ($i > 100)) {
234 $CFG->dataroot = ''; //can not find secure location for dataroot
235 break;
237 $CFG->dataroot = dirname($parrent).DIRECTORY_SEPARATOR.'moodledata';
239 $config->dataroot = $CFG->dataroot;
240 $config->stage = INSTALL_WELCOME;
243 // now let's do the stage work
244 if ($config->stage < INSTALL_WELCOME) {
245 $config->stage = INSTALL_WELCOME;
247 if ($config->stage > INSTALL_SAVE) {
248 $config->stage = INSTALL_SAVE;
253 if ($config->stage == INSTALL_SAVE) {
254 $CFG->early_install_lang = false;
256 $database = moodle_database::get_driver_instance($config->dbtype, 'native');
257 if (!$database->driver_installed()) {
258 $config->stage = INSTALL_DATABASETYPE;
259 } else {
260 if (function_exists('distro_pre_create_db')) { // Hook for distros needing to do something before DB creation
261 $distro = distro_pre_create_db($database, $config->dbhost, $config->dbuser, $config->dbpass, $config->dbname, $config->prefix, array('dbpersist'=>0, 'dbsocket'=>$config->dbsocket), $distro);
263 $hint_database = install_db_validate($database, $config->dbhost, $config->dbuser, $config->dbpass, $config->dbname, $config->prefix, array('dbpersist'=>0, 'dbsocket'=>$config->dbsocket));
265 if ($hint_database === '') {
266 $configphp = install_generate_configphp($database, $CFG);
268 umask(0137);
269 if (($fh = @fopen($configfile, 'w')) !== false) {
270 fwrite($fh, $configphp);
271 fclose($fh);
274 if (file_exists($configfile)) {
275 // config created, let's continue!
276 redirect("$CFG->wwwroot/$config->admin/index.php?lang=$config->lang");
279 install_print_header($config, 'config.php',
280 get_string('configurationcompletehead', 'install'),
281 get_string('configurationcompletesub', 'install').get_string('configfilenotwritten', 'install'));
282 echo '<div class="configphp"><pre>';
283 echo p($configphp);
284 echo '</pre></div>';
286 install_print_footer($config);
287 die;
289 } else {
290 $config->stage = INSTALL_DATABASE;
297 if ($config->stage == INSTALL_DOWNLOADLANG) {
298 if (empty($CFG->dataroot)) {
299 $config->stage = INSTALL_PATHS;
301 } else if (is_dataroot_insecure()) {
302 $hint_dataroot = get_string('pathsunsecuredataroot', 'install');
303 $config->stage = INSTALL_PATHS;
305 } else if (!file_exists($CFG->dataroot)) {
306 $a = new stdClass();
307 $a->parent = dirname($CFG->dataroot);
308 $a->dataroot = $CFG->dataroot;
309 if (!is_writable($a->parent)) {
310 $hint_dataroot = get_string('pathsroparentdataroot', 'install', $a);
311 $config->stage = INSTALL_PATHS;
312 } else {
313 if (!install_init_dataroot($CFG->dataroot, $CFG->directorypermissions)) {
314 $hint_dataroot = get_string('pathserrcreatedataroot', 'install', $a);
315 $config->stage = INSTALL_PATHS;
319 } else if (!install_init_dataroot($CFG->dataroot, $CFG->directorypermissions)) {
320 $hint_dataroot = get_string('pathserrcreatedataroot', 'install', array('dataroot' => $CFG->dataroot));
321 $config->stage = INSTALL_PATHS;
324 if (empty($hint_dataroot) and !is_writable($CFG->dataroot)) {
325 $hint_dataroot = get_string('pathsrodataroot', 'install');
326 $config->stage = INSTALL_PATHS;
329 if ($config->admin === '' or !file_exists($CFG->dirroot.'/'.$config->admin.'/environment.xml')) {
330 $hint_admindir = get_string('pathswrongadmindir', 'install');
331 $config->stage = INSTALL_PATHS;
337 if ($config->stage == INSTALL_DOWNLOADLANG) {
338 // no need to download anything if en lang selected
339 if ($CFG->lang == 'en') {
340 $config->stage = INSTALL_DATABASETYPE;
346 if ($config->stage == INSTALL_DATABASETYPE) {
347 // skip db selection if distro package supports only one db
348 if (!empty($distro->dbtype)) {
349 $config->stage = INSTALL_DATABASE;
354 if ($config->stage == INSTALL_DOWNLOADLANG) {
355 $downloaderror = '';
357 // download and install required lang packs, the lang dir has already been created in install_init_dataroot
358 $installer = new lang_installer($CFG->lang);
359 $results = $installer->run();
360 foreach ($results as $langcode => $langstatus) {
361 if ($langstatus === lang_installer::RESULT_DOWNLOADERROR) {
362 $a = new stdClass();
363 $a->url = $installer->lang_pack_url($langcode);
364 $a->dest = $CFG->dataroot.'/lang';
365 $downloaderror = get_string('remotedownloaderror', 'error', $a);
369 if ($downloaderror !== '') {
370 install_print_header($config, get_string('language'), get_string('langdownloaderror', 'install', $CFG->lang), $downloaderror);
371 install_print_footer($config);
372 die;
373 } else {
374 if (empty($distro->dbtype)) {
375 $config->stage = INSTALL_DATABASETYPE;
376 } else {
377 $config->stage = INSTALL_DATABASE;
381 // switch the string_manager instance to stop using install/lang/
382 $CFG->early_install_lang = false;
383 $CFG->langotherroot = $CFG->dataroot.'/lang';
384 $CFG->langlocalroot = $CFG->dataroot.'/lang';
385 get_string_manager(true);
389 if ($config->stage == INSTALL_DATABASE) {
390 $CFG->early_install_lang = false;
392 $database = moodle_database::get_driver_instance($config->dbtype, 'native');
394 $sub = '<h3>'.$database->get_name().'</h3>'.$database->get_configuration_help();
396 install_print_header($config, get_string('database', 'install'), get_string('databasehead', 'install'), $sub);
398 $strdbhost = get_string('databasehost', 'install');
399 $strdbname = get_string('databasename', 'install');
400 $strdbuser = get_string('databaseuser', 'install');
401 $strdbpass = get_string('databasepass', 'install');
402 $strprefix = get_string('dbprefix', 'install');
403 $strdbsocket = get_string('databasesocket', 'install');
405 echo '<div class="userinput">';
407 $disabled = empty($distro->dbhost) ? '' : 'disabled="disabled';
408 echo '<div class="formrow"><label for="id_dbhost" class="formlabel">'.$strdbhost.'</label>';
409 echo '<input id="id_dbhost" name="dbhost" '.$disabled.' type="text" value="'.s($config->dbhost).'" size="50" class="forminput" />';
410 echo '</div>';
412 echo '<div class="formrow"><label for="id_dbname" class="formlabel">'.$strdbname.'</label>';
413 echo '<input id="id_dbname" name="dbname" type="text" value="'.s($config->dbname).'" size="50" class="forminput" />';
414 echo '</div>';
416 $disabled = empty($distro->dbuser) ? '' : 'disabled="disabled';
417 echo '<div class="formrow"><label for="id_dbuser" class="formlabel">'.$strdbuser.'</label>';
418 echo '<input id="id_dbuser" name="dbuser" '.$disabled.' type="text" value="'.s($config->dbuser).'" size="50" class="forminput" />';
419 echo '</div>';
421 echo '<div class="formrow"><label for="id_dbpass" class="formlabel">'.$strdbpass.'</label>';
422 // no password field here, the password may be visible in config.php if we can not write it to disk
423 echo '<input id="id_dbpass" name="dbpass" type="text" value="'.s($config->dbpass).'" size="50" class="forminput" />';
424 echo '</div>';
426 echo '<div class="formrow"><label for="id_prefix" class="formlabel">'.$strprefix.'</label>';
427 echo '<input id="id_prefix" name="prefix" type="text" value="'.s($config->prefix).'" size="10" class="forminput" />';
428 echo '</div>';
430 if (!(stristr(PHP_OS, 'win') && !stristr(PHP_OS, 'darwin'))) {
431 $checked = $config->dbsocket ? 'checked="checked' : '';
432 echo '<div class="formrow"><label for="id_dbsocket" class="formlabel">'.$strdbsocket.'</label>';
433 echo '<input type="hidden" value="0" name="dbsocket" />';
434 echo '<input type="checkbox" id="id_dbsocket" value="1" name="dbsocket" '.$checked.' class="forminput" />';
435 echo '</div>';
438 echo '<div class="hint">'.$hint_database.'</div>';
439 echo '</div>';
440 install_print_footer($config);
441 die;
445 if ($config->stage == INSTALL_DATABASETYPE) {
446 $CFG->early_install_lang = false;
448 // Finally ask for DB type
449 install_print_header($config, get_string('database', 'install'),
450 get_string('databasetypehead', 'install'),
451 get_string('databasetypesub', 'install'));
453 $databases = array('mysqli' => moodle_database::get_driver_instance('mysqli', 'native'),
454 'pgsql' => moodle_database::get_driver_instance('pgsql', 'native'),
455 'oci' => moodle_database::get_driver_instance('oci', 'native'),
456 'sqlsrv' => moodle_database::get_driver_instance('sqlsrv', 'native'), // MS SQL*Server PHP driver
457 'mssql' => moodle_database::get_driver_instance('mssql', 'native'), // FreeTDS driver
460 echo '<div class="userinput">';
461 echo '<div class="formrow"><label class="formlabel" for="dbtype">'.get_string('dbtype', 'install').'</label>';
462 echo '<select id="dbtype" name="dbtype" class="forminput">';
463 $disabled = array();
464 $options = array();
465 foreach ($databases as $type=>$database) {
466 if ($database->driver_installed() !== true) {
467 $disabled[$type] = $database;
468 continue;
470 echo '<option value="'.s($type).'">'.$database->get_name().'</option>';
472 if ($disabled) {
473 echo '<optgroup label="'.s(get_string('notavailable')).'">';
474 foreach ($disabled as $type=>$database) {
475 echo '<option value="'.s($type).'" class="notavailable">'.$database->get_name().'</option>';
477 echo '</optgroup>';
479 echo '</select></div>';
480 echo '</div>';
482 install_print_footer($config);
483 die;
488 if ($config->stage == INSTALL_ENVIRONMENT or $config->stage == INSTALL_PATHS) {
489 $version_fail = (version_compare(phpversion(), "5.3.2") < 0);
490 $curl_fail = ($lang !== 'en' and !extension_loaded('curl')); // needed for lang pack download
491 $zip_fail = ($lang !== 'en' and !extension_loaded('zip')); // needed for lang pack download
493 if ($version_fail or $curl_fail or $zip_fail) {
494 $config->stage = INSTALL_ENVIRONMENT;
496 install_print_header($config, get_string('environmenthead', 'install'),
497 get_string('errorsinenvironment', 'install'),
498 get_string('environmentsub2', 'install'));
500 echo '<div id="envresult"><dl>';
501 if ($version_fail) {
502 $a = (object)array('needed'=>'5.3.2', 'current'=>phpversion());
503 echo '<dt>'.get_string('phpversion', 'install').'</dt><dd>'.get_string('environmentrequireversion', 'admin', $a).'</dd>';
505 if ($curl_fail) {
506 echo '<dt>'.get_string('phpextension', 'install', 'cURL').'</dt><dd>'.get_string('environmentrequireinstall', 'admin').'</dd>';
508 if ($zip_fail) {
509 echo '<dt>'.get_string('phpextension', 'install', 'Zip').'</dt><dd>'.get_string('environmentrequireinstall', 'admin').'</dd>';
511 echo '</dl></div>';
513 install_print_footer($config, true);
514 die;
516 } else {
517 $config->stage = INSTALL_PATHS;
523 if ($config->stage == INSTALL_PATHS) {
524 $paths = array('wwwroot' => get_string('wwwroot', 'install'),
525 'dirroot' => get_string('dirroot', 'install'),
526 'dataroot' => get_string('dataroot', 'install'));
528 $sub = '<dl>';
529 foreach ($paths as $path=>$name) {
530 $sub .= '<dt>'.$name.'</dt><dd>'.get_string('pathssub'.$path, 'install').'</dd>';
532 if (!file_exists("$CFG->dirroot/admin/environment.xml")) {
533 $sub .= '<dt>'.get_string('admindirname', 'install').'</dt><dd>'.get_string('pathssubadmindir', 'install').'</dd>';
535 $sub .= '</dl>';
537 install_print_header($config, get_string('paths', 'install'), get_string('pathshead', 'install'), $sub);
539 $strwwwroot = get_string('wwwroot', 'install');
540 $strdirroot = get_string('dirroot', 'install');
541 $strdataroot = get_string('dataroot', 'install');
542 $stradmindirname = get_string('admindirname', 'install');
544 echo '<div class="userinput">';
545 echo '<div class="formrow"><label for="id_wwwroot" class="formlabel">'.$paths['wwwroot'].'</label>';
546 echo '<input id="id_wwwroot" name="wwwroot" type="text" value="'.s($CFG->wwwroot).'" disabled="disabled" size="70" class="forminput" />';
547 echo '</div>';
549 echo '<div class="formrow"><label for="id_dirroot" class="formlabel">'.$paths['dirroot'].'</label>';
550 echo '<input id="id_dirroot" name="dirroot" type="text" value="'.s($CFG->dirroot).'" disabled="disabled" size="70"class="forminput" />';
551 echo '</div>';
553 echo '<div class="formrow"><label for="id_dataroot" class="formlabel">'.$paths['dataroot'].'</label>';
554 echo '<input id="id_dataroot" name="dataroot" type="text" value="'.s($config->dataroot).'" size="70" class="forminput" />';
555 if ($hint_dataroot !== '') {
556 echo '<div class="hint">'.$hint_dataroot.'</div>';
558 echo '</div>';
561 if (!file_exists("$CFG->dirroot/admin/environment.xml")) {
562 echo '<div class="formrow"><label for="id_admin" class="formlabel">'.$paths['admindir'].'</label>';
563 echo '<input id="id_admin" name="admin" type="text" value="'.s($config->admin).'" size="10" class="forminput" />';
564 if ($hint_admindir !== '') {
565 echo '<div class="hint">'.$hint_admindir.'</div>';
567 echo '</div>';
570 echo '</div>';
572 install_print_footer($config);
573 die;
578 $config->stage = INSTALL_WELCOME;
580 if ($distro) {
581 ob_start();
582 include('install/distribution.html');
583 $sub = ob_get_clean();
585 install_print_header($config, get_string('language'),
586 get_string('chooselanguagehead', 'install'),
587 $sub);
589 } else {
590 install_print_header($config, get_string('language'),
591 get_string('chooselanguagehead', 'install'),
592 get_string('chooselanguagesub', 'install'));
595 $languages = get_string_manager()->get_list_of_translations();
596 echo '<div class="userinput">';
597 echo '<div class="formrow"><label class="formlabel" for="langselect">'.get_string('language').'</label>';
598 echo '<select id="langselect" name="lang" class="forminput" onchange="this.form.submit()">';
599 foreach ($languages as $name=>$value) {
600 $selected = ($name == $CFG->lang) ? 'selected="selected"' : '';
601 echo '<option value="'.s($name).'" '.$selected.'>'.$value.'</option>';
603 echo '</select></div>';
604 echo '</div>';
606 install_print_footer($config);
607 die;