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
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 if (version_compare(phpversion(), '5.3.2') < 0) {
61 $phpversion = phpversion();
62 // do NOT localise - lang strings would not work here and we CAN not move it after installib
63 echo "Moodle 2.1 or later requires at least PHP 5.3.2 (currently using version $phpversion).<br />";
64 echo "Please upgrade your server software or install older Moodle version.";
68 // make sure iconv is available and actually works
69 if (!function_exists('iconv')) {
70 // this should not happen, this must be very borked install
71 echo 'Moodle requires the iconv PHP extension. Please install or enable the iconv extension.';
75 if (PHP_INT_SIZE
> 4) {
76 // most probably 64bit PHP - we need a lot more memory
77 $minrequiredmemory = '70M';
80 $minrequiredmemory = '40M';
82 // increase or decrease available memory - we need to make sure moodle
83 // installs even with low memory, otherwise developers would overlook
84 // sudden increases of memory needs ;-)
85 @ini_set
('memory_limit', $minrequiredmemory);
87 /** Used by library scripts to check they are being called by Moodle */
88 define('MOODLE_INTERNAL', true);
90 require dirname(__FILE__
).'/lib/installlib.php';
92 // TODO: add lang detection here if empty $_REQUEST['lang']
94 // distro specific customisation
96 if (file_exists('install/distrolib.php')) {
97 require_once('install/distrolib.php');
98 if (function_exists('distro_get_config')) {
99 $distro = distro_get_config();
103 $config = new stdClass();
104 $config->lang
= $lang;
106 if (!empty($_POST)) {
107 if (install_ini_get_bool('magic_quotes_gpc')) {
108 $_POST = array_map('stripslashes', $_POST);
111 $config->stage
= (int)$_POST['stage'];
113 if (isset($_POST['previous'])) {
115 if (INSTALL_DATABASETYPE
and !empty($distro->dbtype
)) {
118 if ($config->stage
== INSTALL_ENVIRONMENT
or $config->stage
== INSTALL_DOWNLOADLANG
) {
121 } else if (isset($_POST['next'])) {
125 $config->dbtype
= trim($_POST['dbtype']);
126 $config->dbhost
= trim($_POST['dbhost']);
127 $config->dbuser
= trim($_POST['dbuser']);
128 $config->dbpass
= trim($_POST['dbpass']);
129 $config->dbname
= trim($_POST['dbname']);
130 $config->prefix
= trim($_POST['prefix']);
131 $config->dbsocket
= (int)(!empty($_POST['dbsocket']));
133 $config->admin
= empty($_POST['admin']) ?
'admin' : trim($_POST['admin']);
135 $config->dataroot
= trim($_POST['dataroot']);
138 $config->stage
= INSTALL_WELCOME
;
140 $config->dbtype
= empty($distro->dbtype
) ?
'' : $distro->dbtype
; // let distro skip dbtype selection
141 $config->dbhost
= empty($distro->dbhost
) ?
'localhost' : $distro->dbhost
; // let distros set dbhost
142 $config->dbuser
= empty($distro->dbuser
) ?
'' : $distro->dbuser
; // let distros set dbuser
143 $config->dbpass
= '';
144 $config->dbname
= 'moodle';
145 $config->prefix
= 'mdl_';
146 $config->dbsocket
= 0;
148 $config->admin
= 'admin';
150 $config->dataroot
= empty($distro->dataroot
) ?
null : $distro->dataroot
; // initialised later after including libs or by distro
153 // Fake some settings so that we can use selected functions from moodlelib.php and weblib.php
154 $CFG = new stdClass();
155 $CFG->lang
= $config->lang
;
156 $CFG->dirroot
= dirname(__FILE__
);
157 $CFG->libdir
= "$CFG->dirroot/lib";
158 $CFG->wwwroot
= install_guess_wwwroot(); // can not be changed - ppl must use the real address when installing
159 $CFG->httpswwwroot
= $CFG->wwwroot
;
160 $CFG->dataroot
= $config->dataroot
;
161 $CFG->tempdir
= $CFG->dataroot
.'/temp';
162 $CFG->cachedir
= $CFG->dataroot
.'/cache';
163 $CFG->admin
= $config->admin
;
164 $CFG->docroot
= 'http://docs.moodle.org';
165 $CFG->langotherroot
= $CFG->dataroot
.'/lang';
166 $CFG->langlocalroot
= $CFG->dataroot
.'/lang';
167 $CFG->directorypermissions
= isset($distro->directorypermissions
) ?
$distro->directorypermissions
: 00777; // let distros set dir permissions
168 $CFG->running_installer
= true;
169 $CFG->early_install_lang
= true;
171 // Require all needed libs
172 require_once($CFG->libdir
.'/setuplib.php');
174 // we need to make sure we have enough memory to load all libraries
175 $memlimit = @ini_get
('memory_limit');
176 if (!empty($memlimit) and $memlimit != -1) {
177 if (get_real_size($memlimit) < get_real_size($minrequiredmemory)) {
178 // do NOT localise - lang strings would not work here and we CAN not move it to later place
179 echo "Moodle requires at least {$minrequiredmemory}B of PHP memory.<br />";
180 echo "Please contact server administrator to fix PHP.ini memory settings.";
185 // Continue with lib loading
186 require_once($CFG->libdir
.'/textlib.class.php');
187 require_once($CFG->libdir
.'/weblib.php');
188 require_once($CFG->libdir
.'/outputlib.php');
189 require_once($CFG->libdir
.'/dmllib.php');
190 require_once($CFG->libdir
.'/moodlelib.php');
191 require_once($CFG->libdir
.'/pagelib.php');
192 require_once($CFG->libdir
.'/deprecatedlib.php');
193 require_once($CFG->libdir
.'/adminlib.php');
194 require_once($CFG->libdir
.'/environmentlib.php');
195 require_once($CFG->libdir
.'/componentlib.class.php');
196 require_once($CFG->dirroot
.'/cache/lib.php');
198 //point pear include path to moodles lib/pear so that includes and requires will search there for files before anywhere else
199 //the problem is that we need specific version of quickforms and hacked excel files :-(
200 ini_set('include_path', $CFG->libdir
.'/pear' . PATH_SEPARATOR
. ini_get('include_path'));
201 //point zend include path to moodles lib/zend so that includes and requires will search there for files before anywhere else
202 ini_set('include_path', $CFG->libdir
.'/zend' . PATH_SEPARATOR
. ini_get('include_path'));
204 require('version.php');
205 $CFG->target_release
= $release;
207 $SESSION = new stdClass();
208 $SESSION->lang
= $CFG->lang
;
210 $USER = new stdClass();
213 $COURSE = new stdClass();
223 // Are we in help mode?
224 if (isset($_GET['help'])) {
225 install_print_help_page($_GET['help']);
228 //first time here? find out suitable dataroot
229 if (is_null($CFG->dataroot
)) {
230 $CFG->dataroot
= dirname(dirname(__FILE__
)).DIRECTORY_SEPARATOR
.'moodledata';
232 $i = 0; //safety check - dirname might return some unexpected results
233 while(is_dataroot_insecure()) {
234 $parrent = dirname($CFG->dataroot
);
236 if ($parrent == '/' or $parrent == '.' or preg_match('/^[a-z]:\\\?$/i', $parrent) or ($i > 100)) {
237 $CFG->dataroot
= ''; //can not find secure location for dataroot
240 $CFG->dataroot
= dirname($parrent).DIRECTORY_SEPARATOR
.'moodledata';
242 $config->dataroot
= $CFG->dataroot
;
243 $config->stage
= INSTALL_WELCOME
;
246 // now let's do the stage work
247 if ($config->stage
< INSTALL_WELCOME
) {
248 $config->stage
= INSTALL_WELCOME
;
250 if ($config->stage
> INSTALL_SAVE
) {
251 $config->stage
= INSTALL_SAVE
;
256 if ($config->stage
== INSTALL_SAVE
) {
257 $CFG->early_install_lang
= false;
259 $database = moodle_database
::get_driver_instance($config->dbtype
, 'native');
260 if (!$database->driver_installed()) {
261 $config->stage
= INSTALL_DATABASETYPE
;
263 if (function_exists('distro_pre_create_db')) { // Hook for distros needing to do something before DB creation
264 $distro = distro_pre_create_db($database, $config->dbhost
, $config->dbuser
, $config->dbpass
, $config->dbname
, $config->prefix
, array('dbpersist'=>0, 'dbsocket'=>$config->dbsocket
), $distro);
266 $hint_database = install_db_validate($database, $config->dbhost
, $config->dbuser
, $config->dbpass
, $config->dbname
, $config->prefix
, array('dbpersist'=>0, 'dbsocket'=>$config->dbsocket
));
268 if ($hint_database === '') {
269 $configphp = install_generate_configphp($database, $CFG);
272 if (($fh = @fopen
($configfile, 'w')) !== false) {
273 fwrite($fh, $configphp);
277 if (file_exists($configfile)) {
278 // config created, let's continue!
279 redirect("$CFG->wwwroot/$config->admin/index.php?lang=$config->lang");
282 install_print_header($config, 'config.php',
283 get_string('configurationcompletehead', 'install'),
284 get_string('configurationcompletesub', 'install').get_string('configfilenotwritten', 'install'));
285 echo '<div class="configphp"><pre>';
289 install_print_footer($config);
293 $config->stage
= INSTALL_DATABASE
;
300 if ($config->stage
== INSTALL_DOWNLOADLANG
) {
301 if (empty($CFG->dataroot
)) {
302 $config->stage
= INSTALL_PATHS
;
304 } else if (is_dataroot_insecure()) {
305 $hint_dataroot = get_string('pathsunsecuredataroot', 'install');
306 $config->stage
= INSTALL_PATHS
;
308 } else if (!file_exists($CFG->dataroot
)) {
310 $a->parent
= dirname($CFG->dataroot
);
311 $a->dataroot
= $CFG->dataroot
;
312 if (!is_writable($a->parent
)) {
313 $hint_dataroot = get_string('pathsroparentdataroot', 'install', $a);
314 $config->stage
= INSTALL_PATHS
;
316 if (!install_init_dataroot($CFG->dataroot
, $CFG->directorypermissions
)) {
317 $hint_dataroot = get_string('pathserrcreatedataroot', 'install', $a);
318 $config->stage
= INSTALL_PATHS
;
322 } else if (!install_init_dataroot($CFG->dataroot
, $CFG->directorypermissions
)) {
323 $hint_dataroot = get_string('pathserrcreatedataroot', 'install', array('dataroot' => $CFG->dataroot
));
324 $config->stage
= INSTALL_PATHS
;
327 if (empty($hint_dataroot) and !is_writable($CFG->dataroot
)) {
328 $hint_dataroot = get_string('pathsrodataroot', 'install');
329 $config->stage
= INSTALL_PATHS
;
332 if ($config->admin
=== '' or !file_exists($CFG->dirroot
.'/'.$config->admin
.'/environment.xml')) {
333 $hint_admindir = get_string('pathswrongadmindir', 'install');
334 $config->stage
= INSTALL_PATHS
;
340 if ($config->stage
== INSTALL_DOWNLOADLANG
) {
341 // no need to download anything if en lang selected
342 if ($CFG->lang
== 'en') {
343 $config->stage
= INSTALL_DATABASETYPE
;
349 if ($config->stage
== INSTALL_DATABASETYPE
) {
350 // skip db selection if distro package supports only one db
351 if (!empty($distro->dbtype
)) {
352 $config->stage
= INSTALL_DATABASE
;
357 if ($config->stage
== INSTALL_DOWNLOADLANG
) {
360 // download and install required lang packs, the lang dir has already been created in install_init_dataroot
361 $installer = new lang_installer($CFG->lang
);
362 $results = $installer->run();
363 foreach ($results as $langcode => $langstatus) {
364 if ($langstatus === lang_installer
::RESULT_DOWNLOADERROR
) {
366 $a->url
= $installer->lang_pack_url($langcode);
367 $a->dest
= $CFG->dataroot
.'/lang';
368 $downloaderror = get_string('remotedownloaderror', 'error', $a);
372 if ($downloaderror !== '') {
373 install_print_header($config, get_string('language'), get_string('langdownloaderror', 'install', $CFG->lang
), $downloaderror);
374 install_print_footer($config);
377 if (empty($distro->dbtype
)) {
378 $config->stage
= INSTALL_DATABASETYPE
;
380 $config->stage
= INSTALL_DATABASE
;
384 // switch the string_manager instance to stop using install/lang/
385 $CFG->early_install_lang
= false;
386 $CFG->langotherroot
= $CFG->dataroot
.'/lang';
387 $CFG->langlocalroot
= $CFG->dataroot
.'/lang';
388 get_string_manager(true);
392 if ($config->stage
== INSTALL_DATABASE
) {
393 $CFG->early_install_lang
= false;
395 $database = moodle_database
::get_driver_instance($config->dbtype
, 'native');
397 $sub = '<h3>'.$database->get_name().'</h3>'.$database->get_configuration_help();
399 install_print_header($config, get_string('database', 'install'), get_string('databasehead', 'install'), $sub);
401 $strdbhost = get_string('databasehost', 'install');
402 $strdbname = get_string('databasename', 'install');
403 $strdbuser = get_string('databaseuser', 'install');
404 $strdbpass = get_string('databasepass', 'install');
405 $strprefix = get_string('dbprefix', 'install');
406 $strdbsocket = get_string('databasesocket', 'install');
408 echo '<div class="userinput">';
410 $disabled = empty($distro->dbhost
) ?
'' : 'disabled="disabled';
411 echo '<div class="formrow"><label for="id_dbhost" class="formlabel">'.$strdbhost.'</label>';
412 echo '<input id="id_dbhost" name="dbhost" '.$disabled.' type="text" value="'.s($config->dbhost
).'" size="50" class="forminput" />';
415 echo '<div class="formrow"><label for="id_dbname" class="formlabel">'.$strdbname.'</label>';
416 echo '<input id="id_dbname" name="dbname" type="text" value="'.s($config->dbname
).'" size="50" class="forminput" />';
419 $disabled = empty($distro->dbuser
) ?
'' : 'disabled="disabled';
420 echo '<div class="formrow"><label for="id_dbuser" class="formlabel">'.$strdbuser.'</label>';
421 echo '<input id="id_dbuser" name="dbuser" '.$disabled.' type="text" value="'.s($config->dbuser
).'" size="50" class="forminput" />';
424 echo '<div class="formrow"><label for="id_dbpass" class="formlabel">'.$strdbpass.'</label>';
425 // no password field here, the password may be visible in config.php if we can not write it to disk
426 echo '<input id="id_dbpass" name="dbpass" type="text" value="'.s($config->dbpass
).'" size="50" class="forminput" />';
429 echo '<div class="formrow"><label for="id_prefix" class="formlabel">'.$strprefix.'</label>';
430 echo '<input id="id_prefix" name="prefix" type="text" value="'.s($config->prefix
).'" size="10" class="forminput" />';
433 if (!(stristr(PHP_OS
, 'win') && !stristr(PHP_OS
, 'darwin'))) {
434 $checked = $config->dbsocket ?
'checked="checked' : '';
435 echo '<div class="formrow"><label for="id_dbsocket" class="formlabel">'.$strdbsocket.'</label>';
436 echo '<input type="hidden" value="0" name="dbsocket" />';
437 echo '<input type="checkbox" id="id_dbsocket" value="1" name="dbsocket" '.$checked.' class="forminput" />';
441 echo '<div class="hint">'.$hint_database.'</div>';
443 install_print_footer($config);
448 if ($config->stage
== INSTALL_DATABASETYPE
) {
449 $CFG->early_install_lang
= false;
451 // Finally ask for DB type
452 install_print_header($config, get_string('database', 'install'),
453 get_string('databasetypehead', 'install'),
454 get_string('databasetypesub', 'install'));
456 $databases = array('mysqli' => moodle_database
::get_driver_instance('mysqli', 'native'),
457 'pgsql' => moodle_database
::get_driver_instance('pgsql', 'native'),
458 'oci' => moodle_database
::get_driver_instance('oci', 'native'),
459 'sqlsrv' => moodle_database
::get_driver_instance('sqlsrv', 'native'), // MS SQL*Server PHP driver
460 'mssql' => moodle_database
::get_driver_instance('mssql', 'native'), // FreeTDS driver
463 echo '<div class="userinput">';
464 echo '<div class="formrow"><label class="formlabel" for="dbtype">'.get_string('dbtype', 'install').'</label>';
465 echo '<select id="dbtype" name="dbtype" class="forminput">';
468 foreach ($databases as $type=>$database) {
469 if ($database->driver_installed() !== true) {
470 $disabled[$type] = $database;
473 echo '<option value="'.s($type).'">'.$database->get_name().'</option>';
476 echo '<optgroup label="'.s(get_string('notavailable')).'">';
477 foreach ($disabled as $type=>$database) {
478 echo '<option value="'.s($type).'" class="notavailable">'.$database->get_name().'</option>';
482 echo '</select></div>';
485 install_print_footer($config);
491 if ($config->stage
== INSTALL_ENVIRONMENT
or $config->stage
== INSTALL_PATHS
) {
492 $version_fail = (version_compare(phpversion(), "5.3.2") < 0);
493 $curl_fail = ($lang !== 'en' and !extension_loaded('curl')); // needed for lang pack download
494 $zip_fail = ($lang !== 'en' and !extension_loaded('zip')); // needed for lang pack download
496 if ($version_fail or $curl_fail or $zip_fail) {
497 $config->stage
= INSTALL_ENVIRONMENT
;
499 install_print_header($config, get_string('environmenthead', 'install'),
500 get_string('errorsinenvironment', 'install'),
501 get_string('environmentsub2', 'install'));
503 echo '<div id="envresult"><dl>';
505 $a = (object)array('needed'=>'5.3.2', 'current'=>phpversion());
506 echo '<dt>'.get_string('phpversion', 'install').'</dt><dd>'.get_string('environmentrequireversion', 'admin', $a).'</dd>';
509 echo '<dt>'.get_string('phpextension', 'install', 'cURL').'</dt><dd>'.get_string('environmentrequireinstall', 'admin').'</dd>';
512 echo '<dt>'.get_string('phpextension', 'install', 'Zip').'</dt><dd>'.get_string('environmentrequireinstall', 'admin').'</dd>';
516 install_print_footer($config, true);
520 $config->stage
= INSTALL_PATHS
;
526 if ($config->stage
== INSTALL_PATHS
) {
527 $paths = array('wwwroot' => get_string('wwwroot', 'install'),
528 'dirroot' => get_string('dirroot', 'install'),
529 'dataroot' => get_string('dataroot', 'install'));
532 foreach ($paths as $path=>$name) {
533 $sub .= '<dt>'.$name.'</dt><dd>'.get_string('pathssub'.$path, 'install').'</dd>';
535 if (!file_exists("$CFG->dirroot/admin/environment.xml")) {
536 $sub .= '<dt>'.get_string('admindirname', 'install').'</dt><dd>'.get_string('pathssubadmindir', 'install').'</dd>';
540 install_print_header($config, get_string('paths', 'install'), get_string('pathshead', 'install'), $sub);
542 $strwwwroot = get_string('wwwroot', 'install');
543 $strdirroot = get_string('dirroot', 'install');
544 $strdataroot = get_string('dataroot', 'install');
545 $stradmindirname = get_string('admindirname', 'install');
547 echo '<div class="userinput">';
548 echo '<div class="formrow"><label for="id_wwwroot" class="formlabel">'.$paths['wwwroot'].'</label>';
549 echo '<input id="id_wwwroot" name="wwwroot" type="text" value="'.s($CFG->wwwroot
).'" disabled="disabled" size="70" class="forminput" />';
552 echo '<div class="formrow"><label for="id_dirroot" class="formlabel">'.$paths['dirroot'].'</label>';
553 echo '<input id="id_dirroot" name="dirroot" type="text" value="'.s($CFG->dirroot
).'" disabled="disabled" size="70"class="forminput" />';
556 echo '<div class="formrow"><label for="id_dataroot" class="formlabel">'.$paths['dataroot'].'</label>';
557 echo '<input id="id_dataroot" name="dataroot" type="text" value="'.s($config->dataroot
).'" size="70" class="forminput" />';
558 if ($hint_dataroot !== '') {
559 echo '<div class="hint">'.$hint_dataroot.'</div>';
564 if (!file_exists("$CFG->dirroot/admin/environment.xml")) {
565 echo '<div class="formrow"><label for="id_admin" class="formlabel">'.$paths['admindir'].'</label>';
566 echo '<input id="id_admin" name="admin" type="text" value="'.s($config->admin
).'" size="10" class="forminput" />';
567 if ($hint_admindir !== '') {
568 echo '<div class="hint">'.$hint_admindir.'</div>';
575 install_print_footer($config);
581 $config->stage
= INSTALL_WELCOME
;
585 include('install/distribution.html');
586 $sub = ob_get_clean();
588 install_print_header($config, get_string('language'),
589 get_string('chooselanguagehead', 'install'),
593 install_print_header($config, get_string('language'),
594 get_string('chooselanguagehead', 'install'),
595 get_string('chooselanguagesub', 'install'));
598 $languages = get_string_manager()->get_list_of_translations();
599 echo '<div class="userinput">';
600 echo '<div class="formrow"><label class="formlabel" for="langselect">'.get_string('language').'</label>';
601 echo '<select id="langselect" name="lang" class="forminput" onchange="this.form.submit()">';
602 foreach ($languages as $name=>$value) {
603 $selected = ($name == $CFG->lang
) ?
'selected="selected"' : '';
604 echo '<option value="'.s($name).'" '.$selected.'>'.$value.'</option>';
606 echo '</select></div>';
609 install_print_footer($config);