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.
50 // Servers should define a default timezone in php.ini, but if they don't then make sure something is defined.
51 // This is a quick hack. Ideally we should ask the admin for a value. See MDL-22625 for more on this.
52 if (function_exists('date_default_timezone_set') and function_exists('date_default_timezone_get')) {
53 @date_default_timezone_set
(@date_default_timezone_get
());
56 // make sure PHP errors are displayed - helps with diagnosing of problems
57 @error_reporting
(E_ALL
);
58 @ini_set
('display_errors', '1');
60 // Check that PHP is of a sufficient version.
61 if (version_compare(phpversion(), '5.3.2') < 0) {
62 $phpversion = phpversion();
63 // do NOT localise - lang strings would not work here and we CAN not move it after installib
64 echo "Moodle 2.1 or later requires at least PHP 5.3.2 (currently using version $phpversion).<br />";
65 echo "Please upgrade your server software or install older Moodle version.";
69 // make sure iconv is available and actually works
70 if (!function_exists('iconv')) {
71 // this should not happen, this must be very borked install
72 echo 'Moodle requires the iconv PHP extension. Please install or enable the iconv extension.';
76 if (PHP_INT_SIZE
> 4) {
77 // most probably 64bit PHP - we need a lot more memory
78 $minrequiredmemory = '70M';
81 $minrequiredmemory = '40M';
83 // increase or decrease available memory - we need to make sure moodle
84 // installs even with low memory, otherwise developers would overlook
85 // sudden increases of memory needs ;-)
86 @ini_set
('memory_limit', $minrequiredmemory);
88 /** Used by library scripts to check they are being called by Moodle */
89 define('MOODLE_INTERNAL', true);
91 require dirname(__FILE__
).'/lib/installlib.php';
93 // TODO: add lang detection here if empty $_REQUEST['lang']
95 // distro specific customisation
97 if (file_exists('install/distrolib.php')) {
98 require_once('install/distrolib.php');
99 if (function_exists('distro_get_config')) {
100 $distro = distro_get_config();
104 $config = new stdClass();
105 $config->lang
= $lang;
107 if (!empty($_POST)) {
108 if (install_ini_get_bool('magic_quotes_gpc')) {
109 $_POST = array_map('stripslashes', $_POST);
112 $config->stage
= (int)$_POST['stage'];
114 if (isset($_POST['previous'])) {
116 if (INSTALL_DATABASETYPE
and !empty($distro->dbtype
)) {
119 if ($config->stage
== INSTALL_ENVIRONMENT
or $config->stage
== INSTALL_DOWNLOADLANG
) {
122 } else if (isset($_POST['next'])) {
126 $config->dbtype
= trim($_POST['dbtype']);
127 $config->dbhost
= trim($_POST['dbhost']);
128 $config->dbuser
= trim($_POST['dbuser']);
129 $config->dbpass
= trim($_POST['dbpass']);
130 $config->dbname
= trim($_POST['dbname']);
131 $config->prefix
= trim($_POST['prefix']);
132 $config->dbsocket
= (int)(!empty($_POST['dbsocket']));
134 $config->admin
= empty($_POST['admin']) ?
'admin' : trim($_POST['admin']);
136 $config->dataroot
= trim($_POST['dataroot']);
139 $config->stage
= INSTALL_WELCOME
;
141 $config->dbtype
= empty($distro->dbtype
) ?
'' : $distro->dbtype
; // let distro skip dbtype selection
142 $config->dbhost
= empty($distro->dbhost
) ?
'localhost' : $distro->dbhost
; // let distros set dbhost
143 $config->dbuser
= empty($distro->dbuser
) ?
'' : $distro->dbuser
; // let distros set dbuser
144 $config->dbpass
= '';
145 $config->dbname
= 'moodle';
146 $config->prefix
= 'mdl_';
147 $config->dbsocket
= 0;
149 $config->admin
= 'admin';
151 $config->dataroot
= empty($distro->dataroot
) ?
null : $distro->dataroot
; // initialised later after including libs or by distro
154 // Fake some settings so that we can use selected functions from moodlelib.php and weblib.php
155 $CFG = new stdClass();
156 $CFG->lang
= $config->lang
;
157 $CFG->dirroot
= dirname(__FILE__
);
158 $CFG->libdir
= "$CFG->dirroot/lib";
159 $CFG->wwwroot
= install_guess_wwwroot(); // can not be changed - ppl must use the real address when installing
160 $CFG->httpswwwroot
= $CFG->wwwroot
;
161 $CFG->dataroot
= $config->dataroot
;
162 $CFG->tempdir
= $CFG->dataroot
.'/temp';
163 $CFG->cachedir
= $CFG->dataroot
.'/cache';
164 $CFG->admin
= $config->admin
;
165 $CFG->docroot
= 'http://docs.moodle.org';
166 $CFG->langotherroot
= $CFG->dataroot
.'/lang';
167 $CFG->langlocalroot
= $CFG->dataroot
.'/lang';
168 $CFG->directorypermissions
= isset($distro->directorypermissions
) ?
$distro->directorypermissions
: 00777; // let distros set dir permissions
169 $CFG->running_installer
= true;
170 $CFG->early_install_lang
= true;
172 // Require all needed libs
173 require_once($CFG->libdir
.'/setuplib.php');
175 // we need to make sure we have enough memory to load all libraries
176 $memlimit = @ini_get
('memory_limit');
177 if (!empty($memlimit) and $memlimit != -1) {
178 if (get_real_size($memlimit) < get_real_size($minrequiredmemory)) {
179 // do NOT localise - lang strings would not work here and we CAN not move it to later place
180 echo "Moodle requires at least {$minrequiredmemory}B of PHP memory.<br />";
181 echo "Please contact server administrator to fix PHP.ini memory settings.";
186 // Continue with lib loading
187 require_once($CFG->libdir
.'/textlib.class.php');
188 require_once($CFG->libdir
.'/weblib.php');
189 require_once($CFG->libdir
.'/outputlib.php');
190 require_once($CFG->libdir
.'/dmllib.php');
191 require_once($CFG->libdir
.'/moodlelib.php');
192 require_once($CFG->libdir
.'/pagelib.php');
193 require_once($CFG->libdir
.'/deprecatedlib.php');
194 require_once($CFG->libdir
.'/adminlib.php');
195 require_once($CFG->libdir
.'/environmentlib.php');
196 require_once($CFG->libdir
.'/componentlib.class.php');
197 require_once($CFG->dirroot
.'/cache/lib.php');
199 //point pear include path to moodles lib/pear so that includes and requires will search there for files before anywhere else
200 //the problem is that we need specific version of quickforms and hacked excel files :-(
201 ini_set('include_path', $CFG->libdir
.'/pear' . PATH_SEPARATOR
. ini_get('include_path'));
202 //point zend include path to moodles lib/zend so that includes and requires will search there for files before anywhere else
203 ini_set('include_path', $CFG->libdir
.'/zend' . PATH_SEPARATOR
. ini_get('include_path'));
205 require('version.php');
206 $CFG->target_release
= $release;
208 $SESSION = new stdClass();
209 $SESSION->lang
= $CFG->lang
;
211 $USER = new stdClass();
214 $COURSE = new stdClass();
224 // Are we in help mode?
225 if (isset($_GET['help'])) {
226 install_print_help_page($_GET['help']);
229 //first time here? find out suitable dataroot
230 if (is_null($CFG->dataroot
)) {
231 $CFG->dataroot
= dirname(dirname(__FILE__
)).DIRECTORY_SEPARATOR
.'moodledata';
233 $i = 0; //safety check - dirname might return some unexpected results
234 while(is_dataroot_insecure()) {
235 $parrent = dirname($CFG->dataroot
);
237 if ($parrent == '/' or $parrent == '.' or preg_match('/^[a-z]:\\\?$/i', $parrent) or ($i > 100)) {
238 $CFG->dataroot
= ''; //can not find secure location for dataroot
241 $CFG->dataroot
= dirname($parrent).DIRECTORY_SEPARATOR
.'moodledata';
243 $config->dataroot
= $CFG->dataroot
;
244 $config->stage
= INSTALL_WELCOME
;
247 // now let's do the stage work
248 if ($config->stage
< INSTALL_WELCOME
) {
249 $config->stage
= INSTALL_WELCOME
;
251 if ($config->stage
> INSTALL_SAVE
) {
252 $config->stage
= INSTALL_SAVE
;
257 if ($config->stage
== INSTALL_SAVE
) {
258 $CFG->early_install_lang
= false;
260 $database = moodle_database
::get_driver_instance($config->dbtype
, 'native');
261 if (!$database->driver_installed()) {
262 $config->stage
= INSTALL_DATABASETYPE
;
264 if (function_exists('distro_pre_create_db')) { // Hook for distros needing to do something before DB creation
265 $distro = distro_pre_create_db($database, $config->dbhost
, $config->dbuser
, $config->dbpass
, $config->dbname
, $config->prefix
, array('dbpersist'=>0, 'dbsocket'=>$config->dbsocket
), $distro);
267 $hint_database = install_db_validate($database, $config->dbhost
, $config->dbuser
, $config->dbpass
, $config->dbname
, $config->prefix
, array('dbpersist'=>0, 'dbsocket'=>$config->dbsocket
));
269 if ($hint_database === '') {
270 $configphp = install_generate_configphp($database, $CFG);
273 if (($fh = @fopen
($configfile, 'w')) !== false) {
274 fwrite($fh, $configphp);
278 if (file_exists($configfile)) {
279 // config created, let's continue!
280 redirect("$CFG->wwwroot/$config->admin/index.php?lang=$config->lang");
283 install_print_header($config, 'config.php',
284 get_string('configurationcompletehead', 'install'),
285 get_string('configurationcompletesub', 'install').get_string('configfilenotwritten', 'install'));
286 echo '<div class="configphp"><pre>';
290 install_print_footer($config);
294 $config->stage
= INSTALL_DATABASE
;
301 if ($config->stage
== INSTALL_DOWNLOADLANG
) {
302 if (empty($CFG->dataroot
)) {
303 $config->stage
= INSTALL_PATHS
;
305 } else if (is_dataroot_insecure()) {
306 $hint_dataroot = get_string('pathsunsecuredataroot', 'install');
307 $config->stage
= INSTALL_PATHS
;
309 } else if (!file_exists($CFG->dataroot
)) {
311 $a->parent
= dirname($CFG->dataroot
);
312 $a->dataroot
= $CFG->dataroot
;
313 if (!is_writable($a->parent
)) {
314 $hint_dataroot = get_string('pathsroparentdataroot', 'install', $a);
315 $config->stage
= INSTALL_PATHS
;
317 if (!install_init_dataroot($CFG->dataroot
, $CFG->directorypermissions
)) {
318 $hint_dataroot = get_string('pathserrcreatedataroot', 'install', $a);
319 $config->stage
= INSTALL_PATHS
;
323 } else if (!install_init_dataroot($CFG->dataroot
, $CFG->directorypermissions
)) {
324 $hint_dataroot = get_string('pathserrcreatedataroot', 'install', array('dataroot' => $CFG->dataroot
));
325 $config->stage
= INSTALL_PATHS
;
328 if (empty($hint_dataroot) and !is_writable($CFG->dataroot
)) {
329 $hint_dataroot = get_string('pathsrodataroot', 'install');
330 $config->stage
= INSTALL_PATHS
;
333 if ($config->admin
=== '' or !file_exists($CFG->dirroot
.'/'.$config->admin
.'/environment.xml')) {
334 $hint_admindir = get_string('pathswrongadmindir', 'install');
335 $config->stage
= INSTALL_PATHS
;
341 if ($config->stage
== INSTALL_DOWNLOADLANG
) {
342 // no need to download anything if en lang selected
343 if ($CFG->lang
== 'en') {
344 $config->stage
= INSTALL_DATABASETYPE
;
350 if ($config->stage
== INSTALL_DATABASETYPE
) {
351 // skip db selection if distro package supports only one db
352 if (!empty($distro->dbtype
)) {
353 $config->stage
= INSTALL_DATABASE
;
358 if ($config->stage
== INSTALL_DOWNLOADLANG
) {
361 // download and install required lang packs, the lang dir has already been created in install_init_dataroot
362 $installer = new lang_installer($CFG->lang
);
363 $results = $installer->run();
364 foreach ($results as $langcode => $langstatus) {
365 if ($langstatus === lang_installer
::RESULT_DOWNLOADERROR
) {
367 $a->url
= $installer->lang_pack_url($langcode);
368 $a->dest
= $CFG->dataroot
.'/lang';
369 $downloaderror = get_string('remotedownloaderror', 'error', $a);
373 if ($downloaderror !== '') {
374 install_print_header($config, get_string('language'), get_string('langdownloaderror', 'install', $CFG->lang
), $downloaderror);
375 install_print_footer($config);
378 if (empty($distro->dbtype
)) {
379 $config->stage
= INSTALL_DATABASETYPE
;
381 $config->stage
= INSTALL_DATABASE
;
385 // switch the string_manager instance to stop using install/lang/
386 $CFG->early_install_lang
= false;
387 $CFG->langotherroot
= $CFG->dataroot
.'/lang';
388 $CFG->langlocalroot
= $CFG->dataroot
.'/lang';
389 get_string_manager(true);
393 if ($config->stage
== INSTALL_DATABASE
) {
394 $CFG->early_install_lang
= false;
396 $database = moodle_database
::get_driver_instance($config->dbtype
, 'native');
398 $sub = '<h3>'.$database->get_name().'</h3>'.$database->get_configuration_help();
400 install_print_header($config, get_string('database', 'install'), get_string('databasehead', 'install'), $sub);
402 $strdbhost = get_string('databasehost', 'install');
403 $strdbname = get_string('databasename', 'install');
404 $strdbuser = get_string('databaseuser', 'install');
405 $strdbpass = get_string('databasepass', 'install');
406 $strprefix = get_string('dbprefix', 'install');
407 $strdbsocket = get_string('databasesocket', 'install');
409 echo '<div class="userinput">';
411 $disabled = empty($distro->dbhost
) ?
'' : 'disabled="disabled';
412 echo '<div class="formrow"><label for="id_dbhost" class="formlabel">'.$strdbhost.'</label>';
413 echo '<input id="id_dbhost" name="dbhost" '.$disabled.' type="text" value="'.s($config->dbhost
).'" size="50" class="forminput" />';
416 echo '<div class="formrow"><label for="id_dbname" class="formlabel">'.$strdbname.'</label>';
417 echo '<input id="id_dbname" name="dbname" type="text" value="'.s($config->dbname
).'" size="50" class="forminput" />';
420 $disabled = empty($distro->dbuser
) ?
'' : 'disabled="disabled';
421 echo '<div class="formrow"><label for="id_dbuser" class="formlabel">'.$strdbuser.'</label>';
422 echo '<input id="id_dbuser" name="dbuser" '.$disabled.' type="text" value="'.s($config->dbuser
).'" size="50" class="forminput" />';
425 echo '<div class="formrow"><label for="id_dbpass" class="formlabel">'.$strdbpass.'</label>';
426 // no password field here, the password may be visible in config.php if we can not write it to disk
427 echo '<input id="id_dbpass" name="dbpass" type="text" value="'.s($config->dbpass
).'" size="50" class="forminput" />';
430 echo '<div class="formrow"><label for="id_prefix" class="formlabel">'.$strprefix.'</label>';
431 echo '<input id="id_prefix" name="prefix" type="text" value="'.s($config->prefix
).'" size="10" class="forminput" />';
434 if (!(stristr(PHP_OS
, 'win') && !stristr(PHP_OS
, 'darwin'))) {
435 $checked = $config->dbsocket ?
'checked="checked' : '';
436 echo '<div class="formrow"><label for="id_dbsocket" class="formlabel">'.$strdbsocket.'</label>';
437 echo '<input type="hidden" value="0" name="dbsocket" />';
438 echo '<input type="checkbox" id="id_dbsocket" value="1" name="dbsocket" '.$checked.' class="forminput" />';
442 echo '<div class="hint">'.$hint_database.'</div>';
444 install_print_footer($config);
449 if ($config->stage
== INSTALL_DATABASETYPE
) {
450 $CFG->early_install_lang
= false;
452 // Finally ask for DB type
453 install_print_header($config, get_string('database', 'install'),
454 get_string('databasetypehead', 'install'),
455 get_string('databasetypesub', 'install'));
457 $databases = array('mysqli' => moodle_database
::get_driver_instance('mysqli', 'native'),
458 'pgsql' => moodle_database
::get_driver_instance('pgsql', 'native'),
459 'oci' => moodle_database
::get_driver_instance('oci', 'native'),
460 'sqlsrv' => moodle_database
::get_driver_instance('sqlsrv', 'native'), // MS SQL*Server PHP driver
461 'mssql' => moodle_database
::get_driver_instance('mssql', 'native'), // FreeTDS driver
464 echo '<div class="userinput">';
465 echo '<div class="formrow"><label class="formlabel" for="dbtype">'.get_string('dbtype', 'install').'</label>';
466 echo '<select id="dbtype" name="dbtype" class="forminput">';
469 foreach ($databases as $type=>$database) {
470 if ($database->driver_installed() !== true) {
471 $disabled[$type] = $database;
474 echo '<option value="'.s($type).'">'.$database->get_name().'</option>';
477 echo '<optgroup label="'.s(get_string('notavailable')).'">';
478 foreach ($disabled as $type=>$database) {
479 echo '<option value="'.s($type).'" class="notavailable">'.$database->get_name().'</option>';
483 echo '</select></div>';
486 install_print_footer($config);
492 if ($config->stage
== INSTALL_ENVIRONMENT
or $config->stage
== INSTALL_PATHS
) {
493 $version_fail = (version_compare(phpversion(), "5.3.2") < 0);
494 $curl_fail = ($lang !== 'en' and !extension_loaded('curl')); // needed for lang pack download
495 $zip_fail = ($lang !== 'en' and !extension_loaded('zip')); // needed for lang pack download
497 if ($version_fail or $curl_fail or $zip_fail) {
498 $config->stage
= INSTALL_ENVIRONMENT
;
500 install_print_header($config, get_string('environmenthead', 'install'),
501 get_string('errorsinenvironment', 'install'),
502 get_string('environmentsub2', 'install'));
504 echo '<div id="envresult"><dl>';
506 $a = (object)array('needed'=>'5.3.2', 'current'=>phpversion());
507 echo '<dt>'.get_string('phpversion', 'install').'</dt><dd>'.get_string('environmentrequireversion', 'admin', $a).'</dd>';
510 echo '<dt>'.get_string('phpextension', 'install', 'cURL').'</dt><dd>'.get_string('environmentrequireinstall', 'admin').'</dd>';
513 echo '<dt>'.get_string('phpextension', 'install', 'Zip').'</dt><dd>'.get_string('environmentrequireinstall', 'admin').'</dd>';
517 install_print_footer($config, true);
521 $config->stage
= INSTALL_PATHS
;
527 if ($config->stage
== INSTALL_PATHS
) {
528 $paths = array('wwwroot' => get_string('wwwroot', 'install'),
529 'dirroot' => get_string('dirroot', 'install'),
530 'dataroot' => get_string('dataroot', 'install'));
533 foreach ($paths as $path=>$name) {
534 $sub .= '<dt>'.$name.'</dt><dd>'.get_string('pathssub'.$path, 'install').'</dd>';
536 if (!file_exists("$CFG->dirroot/admin/environment.xml")) {
537 $sub .= '<dt>'.get_string('admindirname', 'install').'</dt><dd>'.get_string('pathssubadmindir', 'install').'</dd>';
541 install_print_header($config, get_string('paths', 'install'), get_string('pathshead', 'install'), $sub);
543 $strwwwroot = get_string('wwwroot', 'install');
544 $strdirroot = get_string('dirroot', 'install');
545 $strdataroot = get_string('dataroot', 'install');
546 $stradmindirname = get_string('admindirname', 'install');
548 echo '<div class="userinput">';
549 echo '<div class="formrow"><label for="id_wwwroot" class="formlabel">'.$paths['wwwroot'].'</label>';
550 echo '<input id="id_wwwroot" name="wwwroot" type="text" value="'.s($CFG->wwwroot
).'" disabled="disabled" size="70" class="forminput" />';
553 echo '<div class="formrow"><label for="id_dirroot" class="formlabel">'.$paths['dirroot'].'</label>';
554 echo '<input id="id_dirroot" name="dirroot" type="text" value="'.s($CFG->dirroot
).'" disabled="disabled" size="70"class="forminput" />';
557 echo '<div class="formrow"><label for="id_dataroot" class="formlabel">'.$paths['dataroot'].'</label>';
558 echo '<input id="id_dataroot" name="dataroot" type="text" value="'.s($config->dataroot
).'" size="70" class="forminput" />';
559 if ($hint_dataroot !== '') {
560 echo '<div class="hint">'.$hint_dataroot.'</div>';
565 if (!file_exists("$CFG->dirroot/admin/environment.xml")) {
566 echo '<div class="formrow"><label for="id_admin" class="formlabel">'.$paths['admindir'].'</label>';
567 echo '<input id="id_admin" name="admin" type="text" value="'.s($config->admin
).'" size="10" class="forminput" />';
568 if ($hint_admindir !== '') {
569 echo '<div class="hint">'.$hint_admindir.'</div>';
576 install_print_footer($config);
582 $config->stage
= INSTALL_WELCOME
;
586 include('install/distribution.html');
587 $sub = ob_get_clean();
589 install_print_header($config, get_string('language'),
590 get_string('chooselanguagehead', 'install'),
594 install_print_header($config, get_string('language'),
595 get_string('chooselanguagehead', 'install'),
596 get_string('chooselanguagesub', 'install'));
599 $languages = get_string_manager()->get_list_of_translations();
600 echo '<div class="userinput">';
601 echo '<div class="formrow"><label class="formlabel" for="langselect">'.get_string('language').'</label>';
602 echo '<select id="langselect" name="lang" class="forminput" onchange="this.form.submit()">';
603 foreach ($languages as $name=>$value) {
604 $selected = ($name == $CFG->lang
) ?
'selected="selected"' : '';
605 echo '<option value="'.s($name).'" '.$selected.'>'.$value.'</option>';
607 echo '</select></div>';
610 install_print_footer($config);