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 // 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.";
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.';
76 if (iconv('UTF-8', 'UTF-8//IGNORE', 'abc') !== 'abc') {
77 // known to be broken in mid-2011 MAMP installations
78 echo 'Broken iconv PHP extension detected, installation can not continue.';
82 if (PHP_INT_SIZE
> 4) {
83 // most probably 64bit PHP - we need a lot more memory
84 $minrequiredmemory = '70M';
87 $minrequiredmemory = '40M';
89 // increase or decrease available memory - we need to make sure moodle
90 // installs even with low memory, otherwise developers would overlook
91 // sudden increases of memory needs ;-)
92 @ini_set
('memory_limit', $minrequiredmemory);
94 /** Used by library scripts to check they are being called by Moodle */
95 define('MOODLE_INTERNAL', true);
97 require dirname(__FILE__
).'/lib/installlib.php';
99 // TODO: add lang detection here if empty $_REQUEST['lang']
101 // distro specific customisation
103 if (file_exists('install/distrolib.php')) {
104 require_once('install/distrolib.php');
105 if (function_exists('distro_get_config')) {
106 $distro = distro_get_config();
110 $config = new stdClass();
111 $config->lang
= $lang;
113 if (!empty($_POST)) {
114 if (install_ini_get_bool('magic_quotes_gpc')) {
115 $_POST = array_map('stripslashes', $_POST);
118 $config->stage
= (int)$_POST['stage'];
120 if (isset($_POST['previous'])) {
122 if (INSTALL_DATABASETYPE
and !empty($distro->dbtype
)) {
125 if ($config->stage
== INSTALL_ENVIRONMENT
or $config->stage
== INSTALL_DOWNLOADLANG
) {
128 } else if (isset($_POST['next'])) {
132 $config->dbtype
= trim($_POST['dbtype']);
133 $config->dbhost
= trim($_POST['dbhost']);
134 $config->dbuser
= trim($_POST['dbuser']);
135 $config->dbpass
= trim($_POST['dbpass']);
136 $config->dbname
= trim($_POST['dbname']);
137 $config->prefix
= trim($_POST['prefix']);
138 $config->dbsocket
= (int)(!empty($_POST['dbsocket']));
140 $config->admin
= empty($_POST['admin']) ?
'admin' : trim($_POST['admin']);
142 $config->dataroot
= trim($_POST['dataroot']);
145 $config->stage
= INSTALL_WELCOME
;
147 $config->dbtype
= empty($distro->dbtype
) ?
'' : $distro->dbtype
; // let distro skip dbtype selection
148 $config->dbhost
= empty($distro->dbhost
) ?
'localhost' : $distro->dbhost
; // let distros set dbhost
149 $config->dbuser
= empty($distro->dbuser
) ?
'' : $distro->dbuser
; // let distros set dbuser
150 $config->dbpass
= '';
151 $config->dbname
= 'moodle';
152 $config->prefix
= 'mdl_';
153 $config->dbsocket
= 0;
155 $config->admin
= 'admin';
157 $config->dataroot
= empty($distro->dataroot
) ?
null : $distro->dataroot
; // initialised later after including libs or by distro
160 // Fake some settings so that we can use selected functions from moodlelib.php and weblib.php
161 $CFG = new stdClass();
162 $CFG->lang
= $config->lang
;
163 $CFG->dirroot
= dirname(__FILE__
);
164 $CFG->libdir
= "$CFG->dirroot/lib";
165 $CFG->wwwroot
= install_guess_wwwroot(); // can not be changed - ppl must use the real address when installing
166 $CFG->httpswwwroot
= $CFG->wwwroot
;
167 $CFG->dataroot
= $config->dataroot
;
168 $CFG->tempdir
= $CFG->dataroot
.'/temp';
169 $CFG->cachedir
= $CFG->dataroot
.'/cache';
170 $CFG->admin
= $config->admin
;
171 $CFG->docroot
= 'http://docs.moodle.org';
172 $CFG->langotherroot
= $CFG->dataroot
.'/lang';
173 $CFG->langlocalroot
= $CFG->dataroot
.'/lang';
174 $CFG->directorypermissions
= isset($distro->directorypermissions
) ?
$distro->directorypermissions
: 00777; // let distros set dir permissions
175 $CFG->running_installer
= true;
176 $CFG->early_install_lang
= true;
178 // Require all needed libs
179 require_once($CFG->libdir
.'/setuplib.php');
181 // we need to make sure we have enough memory to load all libraries
182 $memlimit = @ini_get
('memory_limit');
183 if (!empty($memlimit) and $memlimit != -1) {
184 if (get_real_size($memlimit) < get_real_size($minrequiredmemory)) {
185 // do NOT localise - lang strings would not work here and we CAN not move it to later place
186 echo "Moodle requires at least {$minrequiredmemory}B of PHP memory.<br />";
187 echo "Please contact server administrator to fix PHP.ini memory settings.";
192 // Continue with lib loading
193 require_once($CFG->libdir
.'/textlib.class.php');
194 require_once($CFG->libdir
.'/weblib.php');
195 require_once($CFG->libdir
.'/outputlib.php');
196 require_once($CFG->libdir
.'/dmllib.php');
197 require_once($CFG->libdir
.'/moodlelib.php');
198 require_once($CFG->libdir
.'/pagelib.php');
199 require_once($CFG->libdir
.'/deprecatedlib.php');
200 require_once($CFG->libdir
.'/adminlib.php');
201 require_once($CFG->libdir
.'/environmentlib.php');
202 require_once($CFG->libdir
.'/componentlib.class.php');
204 //point pear include path to moodles lib/pear so that includes and requires will search there for files before anywhere else
205 //the problem is that we need specific version of quickforms and hacked excel files :-(
206 ini_set('include_path', $CFG->libdir
.'/pear' . PATH_SEPARATOR
. ini_get('include_path'));
207 //point zend include path to moodles lib/zend so that includes and requires will search there for files before anywhere else
208 ini_set('include_path', $CFG->libdir
.'/zend' . PATH_SEPARATOR
. ini_get('include_path'));
210 require('version.php');
211 $CFG->target_release
= $release;
213 $SESSION = new stdClass();
214 $SESSION->lang
= $CFG->lang
;
216 $USER = new stdClass();
219 $COURSE = new stdClass();
229 // Are we in help mode?
230 if (isset($_GET['help'])) {
231 install_print_help_page($_GET['help']);
234 //first time here? find out suitable dataroot
235 if (is_null($CFG->dataroot
)) {
236 $CFG->dataroot
= dirname(dirname(__FILE__
)).DIRECTORY_SEPARATOR
.'moodledata';
238 $i = 0; //safety check - dirname might return some unexpected results
239 while(is_dataroot_insecure()) {
240 $parrent = dirname($CFG->dataroot
);
242 if ($parrent == '/' or $parrent == '.' or preg_match('/^[a-z]:\\\?$/i', $parrent) or ($i > 100)) {
243 $CFG->dataroot
= ''; //can not find secure location for dataroot
246 $CFG->dataroot
= dirname($parrent).DIRECTORY_SEPARATOR
.'moodledata';
248 $config->dataroot
= $CFG->dataroot
;
249 $config->stage
= INSTALL_WELCOME
;
252 // now let's do the stage work
253 if ($config->stage
< INSTALL_WELCOME
) {
254 $config->stage
= INSTALL_WELCOME
;
256 if ($config->stage
> INSTALL_SAVE
) {
257 $config->stage
= INSTALL_SAVE
;
262 if ($config->stage
== INSTALL_SAVE
) {
263 $CFG->early_install_lang
= false;
265 $database = moodle_database
::get_driver_instance($config->dbtype
, 'native');
266 if (!$database->driver_installed()) {
267 $config->stage
= INSTALL_DATABASETYPE
;
269 if (function_exists('distro_pre_create_db')) { // Hook for distros needing to do something before DB creation
270 $distro = distro_pre_create_db($database, $config->dbhost
, $config->dbuser
, $config->dbpass
, $config->dbname
, $config->prefix
, array('dbpersist'=>0, 'dbsocket'=>$config->dbsocket
), $distro);
272 $hint_database = install_db_validate($database, $config->dbhost
, $config->dbuser
, $config->dbpass
, $config->dbname
, $config->prefix
, array('dbpersist'=>0, 'dbsocket'=>$config->dbsocket
));
274 if ($hint_database === '') {
275 $configphp = install_generate_configphp($database, $CFG);
278 if (($fh = @fopen
($configfile, 'w')) !== false) {
279 fwrite($fh, $configphp);
283 if (file_exists($configfile)) {
284 // config created, let's continue!
285 redirect("$CFG->wwwroot/$config->admin/index.php?lang=$config->lang");
288 install_print_header($config, 'config.php',
289 get_string('configurationcompletehead', 'install'),
290 get_string('configurationcompletesub', 'install').get_string('configfilenotwritten', 'install'));
291 echo '<div class="configphp"><pre>';
295 install_print_footer($config);
299 $config->stage
= INSTALL_DATABASE
;
306 if ($config->stage
== INSTALL_DOWNLOADLANG
) {
307 if (empty($CFG->dataroot
)) {
308 $config->stage
= INSTALL_PATHS
;
310 } else if (is_dataroot_insecure()) {
311 $hint_dataroot = get_string('pathsunsecuredataroot', 'install');
312 $config->stage
= INSTALL_PATHS
;
314 } else if (!file_exists($CFG->dataroot
)) {
316 $a->parent
= dirname($CFG->dataroot
);
317 $a->dataroot
= $CFG->dataroot
;
318 if (!is_writable($a->parent
)) {
319 $hint_dataroot = get_string('pathsroparentdataroot', 'install', $a);
320 $config->stage
= INSTALL_PATHS
;
322 if (!install_init_dataroot($CFG->dataroot
, $CFG->directorypermissions
)) {
323 $hint_dataroot = get_string('pathserrcreatedataroot', 'install', $a);
324 $config->stage
= INSTALL_PATHS
;
328 } else if (!install_init_dataroot($CFG->dataroot
, $CFG->directorypermissions
)) {
329 $hint_dataroot = get_string('pathserrcreatedataroot', 'install', array('dataroot' => $CFG->dataroot
));
330 $config->stage
= INSTALL_PATHS
;
333 if (empty($hint_dataroot) and !is_writable($CFG->dataroot
)) {
334 $hint_dataroot = get_string('pathsrodataroot', 'install');
335 $config->stage
= INSTALL_PATHS
;
338 if ($config->admin
=== '' or !file_exists($CFG->dirroot
.'/'.$config->admin
.'/environment.xml')) {
339 $hint_admindir = get_string('pathswrongadmindir', 'install');
340 $config->stage
= INSTALL_PATHS
;
346 if ($config->stage
== INSTALL_DOWNLOADLANG
) {
347 // no need to download anything if en lang selected
348 if ($CFG->lang
== 'en') {
349 $config->stage
= INSTALL_DATABASETYPE
;
355 if ($config->stage
== INSTALL_DATABASETYPE
) {
356 // skip db selection if distro package supports only one db
357 if (!empty($distro->dbtype
)) {
358 $config->stage
= INSTALL_DATABASE
;
363 if ($config->stage
== INSTALL_DOWNLOADLANG
) {
366 // download and install required lang packs, the lang dir has already been created in install_init_dataroot
367 $installer = new lang_installer($CFG->lang
);
368 $results = $installer->run();
369 foreach ($results as $langcode => $langstatus) {
370 if ($langstatus === lang_installer
::RESULT_DOWNLOADERROR
) {
372 $a->url
= $installer->lang_pack_url($langcode);
373 $a->dest
= $CFG->dataroot
.'/lang';
374 $downloaderror = get_string('remotedownloaderror', 'error', $a);
378 if ($downloaderror !== '') {
379 install_print_header($config, get_string('language'), get_string('langdownloaderror', 'install', $CFG->lang
), $downloaderror);
380 install_print_footer($config);
383 if (empty($distro->dbtype
)) {
384 $config->stage
= INSTALL_DATABASETYPE
;
386 $config->stage
= INSTALL_DATABASE
;
390 // switch the string_manager instance to stop using install/lang/
391 $CFG->early_install_lang
= false;
392 $CFG->langotherroot
= $CFG->dataroot
.'/lang';
393 $CFG->langlocalroot
= $CFG->dataroot
.'/lang';
394 get_string_manager(true);
398 if ($config->stage
== INSTALL_DATABASE
) {
399 $CFG->early_install_lang
= false;
401 $database = moodle_database
::get_driver_instance($config->dbtype
, 'native');
403 $sub = '<h3>'.$database->get_name().'</h3>'.$database->get_configuration_help();
405 install_print_header($config, get_string('database', 'install'), get_string('databasehead', 'install'), $sub);
407 $strdbhost = get_string('databasehost', 'install');
408 $strdbname = get_string('databasename', 'install');
409 $strdbuser = get_string('databaseuser', 'install');
410 $strdbpass = get_string('databasepass', 'install');
411 $strprefix = get_string('dbprefix', 'install');
412 $strdbsocket = get_string('databasesocket', 'install');
414 echo '<div class="userinput">';
416 $disabled = empty($distro->dbhost
) ?
'' : 'disabled="disabled';
417 echo '<div class="formrow"><label for="id_dbhost" class="formlabel">'.$strdbhost.'</label>';
418 echo '<input id="id_dbhost" name="dbhost" '.$disabled.' type="text" value="'.s($config->dbhost
).'" size="50" class="forminput" />';
421 echo '<div class="formrow"><label for="id_dbname" class="formlabel">'.$strdbname.'</label>';
422 echo '<input id="id_dbname" name="dbname" type="text" value="'.s($config->dbname
).'" size="30" class="forminput" />';
425 $disabled = empty($distro->dbuser
) ?
'' : 'disabled="disabled';
426 echo '<div class="formrow"><label for="id_dbuser" class="formlabel">'.$strdbuser.'</label>';
427 echo '<input id="id_dbuser" name="dbuser" '.$disabled.' type="text" value="'.s($config->dbuser
).'" size="30" class="forminput" />';
430 echo '<div class="formrow"><label for="id_dbpass" class="formlabel">'.$strdbpass.'</label>';
431 // no password field here, the password may be visible in config.php if we can not write it to disk
432 echo '<input id="id_dbpass" name="dbpass" type="text" value="'.s($config->dbpass
).'" size="30" class="forminput" />';
435 echo '<div class="formrow"><label for="id_prefix" class="formlabel">'.$strprefix.'</label>';
436 echo '<input id="id_prefix" name="prefix" type="text" value="'.s($config->prefix
).'" size="10" class="forminput" />';
439 if (!(stristr(PHP_OS
, 'win') && !stristr(PHP_OS
, 'darwin'))) {
440 $checked = $config->dbsocket ?
'checked="checked' : '';
441 echo '<div class="formrow"><label for="id_dbsocket" class="formlabel">'.$strdbsocket.'</label>';
442 echo '<input type="hidden" value="0" name="dbsocket" />';
443 echo '<input type="checkbox" id="id_dbsocket" value="1" name="dbsocket" '.$checked.' class="forminput" />';
447 echo '<div class="hint">'.$hint_database.'</div>';
449 install_print_footer($config);
454 if ($config->stage
== INSTALL_DATABASETYPE
) {
455 $CFG->early_install_lang
= false;
457 // Finally ask for DB type
458 install_print_header($config, get_string('database', 'install'),
459 get_string('databasetypehead', 'install'),
460 get_string('databasetypesub', 'install'));
462 $databases = array('mysqli' => moodle_database
::get_driver_instance('mysqli', 'native'),
463 'pgsql' => moodle_database
::get_driver_instance('pgsql', 'native'),
464 'oci' => moodle_database
::get_driver_instance('oci', 'native'),
465 'sqlsrv' => moodle_database
::get_driver_instance('sqlsrv', 'native'), // MS SQL*Server PHP driver
466 'mssql' => moodle_database
::get_driver_instance('mssql', 'native'), // FreeTDS driver
469 echo '<div class="userinput">';
470 echo '<div class="formrow"><label class="formlabel" for="dbtype">'.get_string('dbtype', 'install').'</label>';
471 echo '<select id="dbtype" name="dbtype" class="forminput">';
474 foreach ($databases as $type=>$database) {
475 if ($database->driver_installed() !== true) {
476 $disabled[$type] = $database;
479 echo '<option value="'.s($type).'">'.$database->get_name().'</option>';
482 echo '<optgroup label="'.s(get_string('notavailable')).'">';
483 foreach ($disabled as $type=>$database) {
484 echo '<option value="'.s($type).'" class="notavailable">'.$database->get_name().'</option>';
488 echo '</select></div>';
491 install_print_footer($config);
497 if ($config->stage
== INSTALL_ENVIRONMENT
or $config->stage
== INSTALL_PATHS
) {
498 $version_fail = (version_compare(phpversion(), "5.3.2") < 0);
499 $curl_fail = ($lang !== 'en' and !extension_loaded('curl')); // needed for lang pack download
500 $zip_fail = ($lang !== 'en' and !extension_loaded('zip')); // needed for lang pack download
502 if ($version_fail or $curl_fail or $zip_fail) {
503 $config->stage
= INSTALL_ENVIRONMENT
;
505 install_print_header($config, get_string('environmenthead', 'install'),
506 get_string('errorsinenvironment', 'install'),
507 get_string('environmentsub2', 'install'));
509 echo '<div id="envresult"><dl>';
511 $a = (object)array('needed'=>'5.3.2', 'current'=>phpversion());
512 echo '<dt>'.get_string('phpversion', 'install').'</dt><dd>'.get_string('environmentrequireversion', 'admin', $a).'</dd>';
515 echo '<dt>'.get_string('phpextension', 'install', 'cURL').'</dt><dd>'.get_string('environmentrequireinstall', 'admin').'</dd>';
518 echo '<dt>'.get_string('phpextension', 'install', 'Zip').'</dt><dd>'.get_string('environmentrequireinstall', 'admin').'</dd>';
522 install_print_footer($config, true);
526 $config->stage
= INSTALL_PATHS
;
532 if ($config->stage
== INSTALL_PATHS
) {
533 $paths = array('wwwroot' => get_string('wwwroot', 'install'),
534 'dirroot' => get_string('dirroot', 'install'),
535 'dataroot' => get_string('dataroot', 'install'));
538 foreach ($paths as $path=>$name) {
539 $sub .= '<dt>'.$name.'</dt><dd>'.get_string('pathssub'.$path, 'install').'</dd>';
541 if (!file_exists("$CFG->dirroot/admin/environment.xml")) {
542 $sub .= '<dt>'.get_string('admindirname', 'install').'</dt><dd>'.get_string('pathssubadmindir', 'install').'</dd>';
546 install_print_header($config, get_string('paths', 'install'), get_string('pathshead', 'install'), $sub);
548 $strwwwroot = get_string('wwwroot', 'install');
549 $strdirroot = get_string('dirroot', 'install');
550 $strdataroot = get_string('dataroot', 'install');
551 $stradmindirname = get_string('admindirname', 'install');
553 echo '<div class="userinput">';
554 echo '<div class="formrow"><label for="id_wwwroot" class="formlabel">'.$paths['wwwroot'].'</label>';
555 echo '<input id="id_wwwroot" name="wwwroot" type="text" value="'.s($CFG->wwwroot
).'" disabled="disabled" size="70" class="forminput" />';
558 echo '<div class="formrow"><label for="id_dirroot" class="formlabel">'.$paths['dirroot'].'</label>';
559 echo '<input id="id_dirroot" name="dirroot" type="text" value="'.s($CFG->dirroot
).'" disabled="disabled" size="70"class="forminput" />';
562 echo '<div class="formrow"><label for="id_dataroot" class="formlabel">'.$paths['dataroot'].'</label>';
563 echo '<input id="id_dataroot" name="dataroot" type="text" value="'.s($config->dataroot
).'" size="70" class="forminput" />';
564 if ($hint_dataroot !== '') {
565 echo '<div class="hint">'.$hint_dataroot.'</div>';
570 if (!file_exists("$CFG->dirroot/admin/environment.xml")) {
571 echo '<div class="formrow"><label for="id_admin" class="formlabel">'.$paths['admindir'].'</label>';
572 echo '<input id="id_admin" name="admin" type="text" value="'.s($config->admin
).'" size="10" class="forminput" />';
573 if ($hint_admindir !== '') {
574 echo '<div class="hint">'.$hint_admindir.'</div>';
581 install_print_footer($config);
587 $config->stage
= INSTALL_WELCOME
;
591 include('install/distribution.html');
592 $sub = ob_get_clean();
594 install_print_header($config, get_string('language'),
595 get_string('chooselanguagehead', 'install'),
599 install_print_header($config, get_string('language'),
600 get_string('chooselanguagehead', 'install'),
601 get_string('chooselanguagesub', 'install'));
604 $languages = get_string_manager()->get_list_of_translations();
605 echo '<div class="userinput">';
606 echo '<div class="formrow"><label class="formlabel" for="langselect">'.get_string('language').'</label>';
607 echo '<select id="langselect" name="lang" class="forminput" onchange="this.form.submit()">';
608 foreach ($languages as $name=>$value) {
609 $selected = ($name == $CFG->lang
) ?
'selected="selected"' : '';
610 echo '<option value="'.s($name).'" '.$selected.'>'.$value.'</option>';
612 echo '</select></div>';
615 install_print_footer($config);