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 // make sure PHP errors are displayed - helps with diagnosing of problems
50 @error_reporting(E_ALL);
51 @ini_set('display_errors', '1');
53 // Check that PHP is of a sufficient version
54 if (version_compare(phpversion(), "5.2.0") < 0) {
55 $phpversion = phpversion();
56 // do NOT localise - lang strings would not work here and we CAN not move it after installib
57 echo "Sorry, Moodle 2.0 requires PHP 5.2.8 or later (currently using version $phpversion).<br />";
58 echo "Please upgrade your server software or install latest Moodle 1.9.x instead.";
62 if (PHP_INT_SIZE > 4) {
63 // most probably 64bit PHP - we need a lot more memory
64 $minrequiredmemory = '70M';
67 $minrequiredmemory = '40M';
69 // increase or decrease available memory - we need to make sure moodle
70 // installs even with low memory, otherwise developers would overlook
71 // sudden increases of memory needs ;-)
72 @ini_set('memory_limit', $minrequiredmemory);
74 require dirname(__FILE__).'/lib/installlib.php';
76 // TODO: add lang detection here if empty $_REQUEST['lang']
78 // distro specific customisation
80 if (file_exists('install/distrolib.php')) {
81 require_once('install/distrolib.php');
82 if (function_exists('distro_get_config')) {
83 $distro = distro_get_config();
87 $config = new stdClass();
88 $config->lang = $lang;
91 if (install_ini_get_bool('magic_quotes_gpc')) {
92 $_POST = array_map('stripslashes', $_POST);
95 $config->stage = (int)$_POST['stage'];
97 if (isset($_POST['previous'])) {
99 if (INSTALL_DATABASETYPE and !empty($distro->dbtype)) {
102 if ($config->stage == INSTALL_ENVIRONMENT or $config->stage == INSTALL_DOWNLOADLANG) {
105 } else if (isset($_POST['next'])) {
109 $config->dbtype = trim($_POST['dbtype']);
110 $config->dbhost = trim($_POST['dbhost']);
111 $config->dbuser = trim($_POST['dbuser']);
112 $config->dbpass = trim($_POST['dbpass']);
113 $config->dbname = trim($_POST['dbname']);
114 $config->prefix = trim($_POST['prefix']);
115 $config->dbsocket = (int)(!empty($_POST['dbsocket']));
117 $config->dirroot = trim($_POST['dirroot']);
118 $config->admin = empty($_POST['admin']) ? 'admin' : trim($_POST['admin']);
120 $config->dataroot = trim($_POST['dataroot']);
123 $config->stage = INSTALL_WELCOME;
125 $config->dbtype = empty($distro->dbtype) ? '' : $distro->dbtype; // let distro skip dbtype selection
126 $config->dbhost = empty($distro->dbhost) ? 'localhost' : $distro->dbhost; // let distros set dbhost
127 $config->dbuser = empty($distro->dbuser) ? '' : $distro->dbuser; // let distros set dbuser
128 $config->dbpass = '';
129 $config->dbname = 'moodle';
130 $config->prefix = 'mdl_';
131 $config->dbsocket = 0;
133 $config->dirroot = str_replace('\\', '/', dirname(__FILE__)); // Fix for win32
134 $config->admin = 'admin';
136 $config->dataroot = empty($distro->dataroot) ? null : $distro->dataroot; // initialised later after including libs or by distro
139 // Fake some settings so that we can use selected functions from moodlelib.php and weblib.php
140 $CFG = new stdClass();
141 $CFG->lang = $config->lang;
142 $CFG->dirroot = str_replace('\\', '/', dirname(__FILE__)); // Fix for win32
143 $CFG->libdir = "$CFG->dirroot/lib";
144 $CFG->wwwroot = install_guess_wwwroot(); // can not be changed - ppl must use the real address when installing
145 $CFG->httpswwwroot = $CFG->wwwroot;
146 $CFG->dataroot = $config->dataroot;
147 $CFG->admin = $config->admin;
148 $CFG->docroot = 'http://docs.moodle.org';
149 $CFG->directorypermissions = 00777;
150 $CFG->running_installer = true;
151 $CFG->early_install_lang = true;
153 // Require all needed libs
154 require_once($CFG->libdir.'/setuplib.php');
156 // we need to make sure we have enough memory to load all libraries
157 $memlimit = @ini_get('memory_limit');
158 if (!empty($memlimit) and $memlimit != -1) {
159 if (get_real_size($memlimit) < get_real_size($minrequiredmemory)) {
160 // do NOT localise - lang strings would not work here and we CAN not move it to later place
161 echo "Sorry, Moodle 2.0 requires at least {$minrequiredmemory}B of PHP memory.<br />";
162 echo "Please contact server administrator to fix PHP.ini memory settings.";
167 // Continue with lib loading
168 require_once($CFG->libdir.'/textlib.class.php');
169 require_once($CFG->libdir.'/weblib.php');
170 require_once($CFG->libdir.'/outputlib.php');
171 require_once($CFG->libdir.'/dmllib.php');
172 require_once($CFG->libdir.'/moodlelib.php');
173 require_once($CFG->libdir .'/pagelib.php');
174 require_once($CFG->libdir.'/deprecatedlib.php');
175 require_once($CFG->libdir.'/adminlib.php');
176 require_once($CFG->libdir.'/environmentlib.php');
177 require_once($CFG->libdir.'/xmlize.php');
178 require_once($CFG->libdir.'/componentlib.class.php');
180 //point pear include path to moodles lib/pear so that includes and requires will search there for files before anywhere else
181 //the problem is that we need specific version of quickforms and hacked excel files :-(
182 ini_set('include_path', $CFG->libdir.'/pear' . PATH_SEPARATOR . ini_get('include_path'));
183 //point zend include path to moodles lib/zend so that includes and requires will search there for files before anywhere else
184 ini_set('include_path', $CFG->libdir.'/zend' . PATH_SEPARATOR . ini_get('include_path'));
186 require('version.php');
187 $CFG->target_release = $release;
189 $SESSION = new object();
190 $SESSION->lang = $CFG->lang;
192 $USER = new object();
195 $COURSE = new object();
206 // Are we in help mode?
207 if (isset($_GET['help'])) {
208 install_print_help_page($_GET['help']);
212 if (isset($_GET['css'])) {
213 install_css_styles();
216 //first time here? find out suitable dataroot
217 if (is_null($CFG->dataroot)) {
218 $CFG->dataroot = str_replace('\\', '/', dirname(dirname(__FILE__)).'/moodledata');
220 $i = 0; //safety check - dirname might return some unexpected results
221 while(is_dataroot_insecure()) {
222 $parrent = dirname($CFG->dataroot);
224 if ($parrent == '/' or $parrent == '.' or preg_match('/^[a-z]:\\\?$/i', $parrent) or ($i > 100)) {
225 $CFG->dataroot = ''; //can not find secure location for dataroot
228 $CFG->dataroot = dirname($parrent).'/moodledata';
230 $config->dataroot = $CFG->dataroot;
231 $config->stage = INSTALL_WELCOME;
234 // now let's do the stage work
235 if ($config->stage < INSTALL_WELCOME) {
236 $config->stage = INSTALL_WELCOME;
238 if ($config->stage > INSTALL_SAVE) {
239 $config->stage = INSTALL_SAVE;
244 if ($config->stage == INSTALL_SAVE) {
245 $CFG->early_install_lang = false;
247 $database = moodle_database::get_driver_instance($config->dbtype, 'native');
248 if (!$database->driver_installed()) {
249 $config->stage = INSTALL_DATABASETYPE;
251 if (function_exists('distro_pre_create_db')) { // Hook for distros needing to do something before DB creation
252 $distro = distro_pre_create_db($database, $config->dbhost, $config->dbuser, $config->dbpass, $config->dbname, $config->prefix, array('dbpersist'=>0, 'dbsocket'=>$config->dbsocket), $distro);
254 $hint_database = install_db_validate($database, $config->dbhost, $config->dbuser, $config->dbpass, $config->dbname, $config->prefix, array('dbpersist'=>0, 'dbsocket'=>$config->dbsocket));
256 if ($hint_database === '') {
257 // extra hackery needed for symbolic link support
258 if ($CFG->dirroot !== $config->dirroot) {
259 $CFG->dirroot = $config->dirroot;
262 $userealpath = false;
264 $configphp = install_generate_configphp($database, $CFG, $userealpath);
267 if (($fh = @fopen($configfile, 'w')) !== false) {
268 fwrite($fh, $configphp);
272 if (file_exists($configfile)) {
273 // config created, let's continue!
274 redirect("$CFG->wwwroot/$config->admin/index.php?lang=$config->lang");
277 install_print_header($config, 'config.php',
278 get_string('configurationcompletehead', 'install'),
279 get_string('configurationcompletesub', 'install').get_string('configfilenotwritten', 'install'));
280 echo '<div class="configphp"><pre>';
284 install_print_footer($config);
288 $config->stage = INSTALL_DATABASE;
295 if ($config->stage == INSTALL_DOWNLOADLANG) {
296 if (empty($CFG->dataroot)) {
297 $config->stage = INSTALL_PATHS;
299 } else if (is_dataroot_insecure()) {
300 $hint_dataroot = get_string('pathsunsecuredataroot', 'install');
301 $config->stage = INSTALL_PATHS;
303 } else if (!file_exists($CFG->dataroot)) {
305 $a->parent = dirname($CFG->dataroot);
306 $a->dataroot = $CFG->dataroot;
307 if (!is_writable(dirname($CFG->dataroot))) {
308 $hint_dataroot = get_string('pathsroparentdataroot', 'install', $a);
309 $config->stage = INSTALL_PATHS;
311 if (!make_upload_directory('lang', false)) {
312 $hint_dataroot = get_string('pathserrcreatedataroot', 'install', $a);
313 $config->stage = INSTALL_PATHS;
318 if (empty($hint_dataroot) and !is_writable($CFG->dataroot)) {
319 $hint_dataroot = get_string('pathsrodataroot', 'install');
320 $config->stage = INSTALL_PATHS;
323 if ($config->dirroot === '' or !file_exists($config->dirroot)) {
324 $hint_dirroot = get_string('pathswrongdirroot', 'install');
325 $config->stage = INSTALL_PATHS;
328 if ($config->admin === '' or !file_exists($config->dirroot.'/'.$config->admin.'/environment.xml')) {
329 $hint_admindir = get_string('pathswrongadmindir', 'install');
330 $config->stage = INSTALL_PATHS;
336 if ($config->stage == INSTALL_DOWNLOADLANG) {
337 // no need to download anything if en lang selected
338 if ($CFG->lang == 'en') {
339 $config->stage = INSTALL_DATABASETYPE;
345 if ($config->stage == INSTALL_DATABASETYPE) {
346 // skip db selection if distro package supports only one db
347 if (!empty($distro->dbtype)) {
348 $config->stage = INSTALL_DATABASE;
353 if ($config->stage == INSTALL_DOWNLOADLANG) {
356 // Create necessary lang dir
357 if (!make_upload_directory('lang', false)) {
358 $downloaderror = get_string('cannotcreatelangdir', 'error');
360 // Download and install lang component
361 } else if ($cd = new component_installer('http://download.moodle.org', 'lang20', $CFG->lang.'.zip', 'languages.md5', 'lang')) {
362 if ($cd->install() == COMPONENT_ERROR) {
363 if ($cd->get_error() == 'remotedownloaderror') {
365 $a->url = 'http://download.moodle.org/lang20/'.$INSTALL['language'].'.zip';
366 $a->dest = $CFG->dataroot.'/lang';
367 $downloaderror = get_string($cd->get_error(), 'error', $a);
369 $downloaderror = get_string($cd->get_error(), 'error');
372 // install parent lang if defined
373 if ($parentlang = get_parent_language()) {
374 if ($cd = new component_installer('http://download.moodle.org', 'lang20', $parentlang.'.zip', 'languages.md5', 'lang')) {
381 if ($downloaderror !== '') {
382 install_print_header($config, get_string('language'), get_string('langdownloaderror', 'install', $CFG->lang), $downloaderror);
383 install_print_footer($config);
386 if (empty($distro->dbtype)) {
387 $config->stage = INSTALL_DATABASETYPE;
389 $config->stage = INSTALL_DATABASE;
395 if ($config->stage == INSTALL_DATABASE) {
396 $CFG->early_install_lang = false;
398 $database = moodle_database::get_driver_instance($config->dbtype, 'native');
400 $sub = '<h3>'.$database->get_name().'</h3>'.$database->get_configuration_help();
402 install_print_header($config, get_string('database', 'install'), get_string('databasehead', 'install'), $sub);
404 $strdbhost = get_string('databasehost', 'install');
405 $strdbname = get_string('databasename', 'install');
406 $strdbuser = get_string('databaseuser', 'install');
407 $strdbpass = get_string('databasepass', 'install');
408 $strprefix = get_string('dbprefix', 'install');
409 $strdbsocket = get_string('databasesocket', 'install');
411 echo '<div class="userinput">';
413 $disabled = empty($distro->dbhost) ? '' : 'disabled="disabled';
414 echo '<div class="formrow"><label for="id_dbhost" class="formlabel">'.$strdbhost.'</label>';
415 echo '<input id="id_dbhost" name="dbhost" '.$disabled.' type="text" value="'.s($config->dbhost).'" size="30" class="forminput" />';
418 echo '<div class="formrow"><label for="id_dbname" class="formlabel">'.$strdbname.'</label>';
419 echo '<input id="id_dbname" name="dbname" type="text" value="'.s($config->dbname).'" size="30" class="forminput" />';
422 $disabled = empty($distro->dbuser) ? '' : 'disabled="disabled';
423 echo '<div class="formrow"><label for="id_dbuser" class="formlabel">'.$strdbuser.'</label>';
424 echo '<input id="id_dbuser" name="dbuser" '.$disabled.' type="text" value="'.s($config->dbuser).'" size="30" class="forminput" />';
427 echo '<div class="formrow"><label for="id_dbpass" class="formlabel">'.$strdbpass.'</label>';
428 // no password field here, the password may be visible in config.php if we can not write it to disk
429 echo '<input id="id_dbpass" name="dbpass" type="text" value="'.s($config->dbpass).'" size="30" class="forminput" />';
432 echo '<div class="formrow"><label for="id_prefix" class="formlabel">'.$strprefix.'</label>';
433 echo '<input id="id_prefix" name="prefix" type="text" value="'.s($config->prefix).'" size="10" class="forminput" />';
436 if (!(stristr(PHP_OS, 'win') && !stristr(PHP_OS, 'darwin'))) {
437 $checked = $config->dbsocket ? 'checked="checked' : '';
438 echo '<div class="formrow"><label for="id_dbsocket" class="formlabel">'.$strdbsocket.'</label>';
439 echo '<input type="hidden" value="0" name="dbsocket" />';
440 echo '<input type="checkbox" id="id_dbsocket" value="1" name="dbsocket" '.$checked.' class="forminput" />';
444 echo '<div class="hint">'.$hint_database.'</div>';
446 install_print_footer($config);
451 if ($config->stage == INSTALL_DATABASETYPE) {
452 $CFG->early_install_lang = false;
454 // Finally ask for DB type
455 install_print_header($config, get_string('database', 'install'),
456 get_string('databasetypehead', 'install'),
457 get_string('databasetypesub', 'install'));
459 // TODO: move this PHP5 code to lib/installib.php so that this file parses in PHP4
460 $databases = array('mysqli' => moodle_database::get_driver_instance('mysqli', 'native'),
461 'pgsql' => moodle_database::get_driver_instance('pgsql', 'native'),
462 'oci' => moodle_database::get_driver_instance('oci', 'native'),
463 //'sqlsrv' => moodle_database::get_driver_instance('sqlsrv', 'native'), // new MS sql driver - win32 only
464 //'mssql' => moodle_database::get_driver_instance('mssql', 'native'), // FreeTDS driver
467 echo '<div class="userinput">';
468 echo '<div class="formrow"><label class="formlabel" for="dbtype">'.get_string('dbtype', 'install').'</label>';
469 echo '<select id="dbtype" name="dbtype" class="forminput">';
472 foreach ($databases as $type=>$database) {
473 if ($database->driver_installed() !== true) {
474 $disabled[$type] = $database;
477 echo '<option value="'.s($type).'">'.$database->get_name().'</option>';
480 echo '<optgroup label="'.s(get_string('notavailable')).'">';
481 foreach ($disabled as $type=>$database) {
482 echo '<option value="'.s($type).'" class="notavailable">'.$database->get_name().'</option>';
486 echo '</select></div>';
489 install_print_footer($config);
495 if ($config->stage == INSTALL_ENVIRONMENT or $config->stage == INSTALL_PATHS) {
496 $version_fail = (version_compare(phpversion(), "5.2.8") < 0);
497 $curl_fail = ($lang !== 'en' and !extension_loaded('curl')); // needed for lang pack download
498 $zip_fail = ($lang !== 'en' and !extension_loaded('zip')); // needed for lang pack download
500 if ($version_fail or $curl_fail or $zip_fail) {
501 $config->stage = INSTALL_ENVIRONMENT;
503 install_print_header($config, get_string('environment', 'install'),
504 get_string('errorsinenvironment', 'install'),
505 get_string('environmentsub2', 'install'));
507 echo '<div id="envresult"><dl>';
509 $a = (object)array('needed'=>'5.2.8', 'current'=>phpversion());
510 echo '<dt>'.get_string('phpversion', 'install').'</dt><dd>'.get_string('environmentrequireversion', 'admin', $a).'</dd>';
513 echo '<dt>'.get_string('phpextension', 'install', 'cURL').'</dt><dd>'.get_string('environmentrequireinstall', 'admin').'</dd>';
516 echo '<dt>'.get_string('phpextension', 'install', 'Zip').'</dt><dd>'.get_string('environmentrequireinstall', 'admin').'</dd>';
520 install_print_footer($config, true);
524 $config->stage = INSTALL_PATHS;
530 if ($config->stage == INSTALL_PATHS) {
531 $paths = array('wwwroot' => get_string('wwwroot', 'install'),
532 'dirroot' => get_string('dirroot', 'install'),
533 'dataroot' => get_string('dataroot', 'install'),
534 'admindir' => get_string('admindirname', 'install'));
537 foreach ($paths as $path=>$name) {
538 $sub .= '<dt>'.$name.'</dt><dd>'.get_string('pathssub'.$path, 'install').'</dd>';
542 install_print_header($config, get_string('paths', 'install'), get_string('pathshead', 'install'), $sub);
544 $strwwwroot = get_string('wwwroot', 'install');
545 $strdirroot = get_string('dirroot', 'install');
546 $strdataroot = get_string('dataroot', 'install');
547 $stradmindirname = get_string('admindirname', 'install');
549 echo '<div class="userinput">';
550 echo '<div class="formrow"><label for="id_wwwroot" class="formlabel">'.$paths['wwwroot'].'</label>';
551 echo '<input id="id_wwwroot" name="wwwroot" type="text" value="'.s($CFG->wwwroot).'" disabled="disabled" size="45" class="forminput" />';
554 echo '<div class="formrow"><label for="id_dirroot" class="formlabel">'.$paths['dirroot'].'</label>';
555 echo '<input id="id_dirroot" name="dirroot" type="text" value="'.s($config->dirroot).'" size="45"class="forminput" />';
556 if ($hint_dirroot !== '') {
557 echo '<div class="hint">'.$hint_dirroot.'</div>';
561 echo '<div class="formrow"><label for="id_dataroot" class="formlabel">'.$paths['dataroot'].'</label>';
562 echo '<input id="id_dataroot" name="dataroot" type="text" value="'.s($config->dataroot).'" size="45" class="forminput" />';
563 if ($hint_dataroot !== '') {
564 echo '<div class="hint">'.$hint_dataroot.'</div>';
569 if (file_exists("$CFG->dirroot/admin/environment.xml")) {
570 $disabled = 'disabled="disabled"';
574 echo '<div class="formrow"><label for="id_admin" class="formlabel">'.$paths['admindir'].'</label>';
575 echo '<input id="id_admin" name="admin" type="text" value="'.s($config->admin).'" '.$disabled.' size="10" class="forminput" />';
576 if ($hint_admindir !== '') {
577 echo '<div class="hint">'.$hint_admindir.'</div>';
583 install_print_footer($config);
589 $config->stage = INSTALL_WELCOME;
593 include('install/distribution.html');
594 $sub = ob_get_clean();
596 install_print_header($config, get_string('language'),
597 get_string('chooselanguagehead', 'install'),
601 install_print_header($config, get_string('language'),
602 get_string('chooselanguagehead', 'install'),
603 get_string('chooselanguagesub', 'install'));
606 $languages = get_list_of_languages();
607 echo '<div class="userinput">';
608 echo '<div class="formrow"><label class="formlabel" for="langselect">'.get_string('language').'</label>';
609 echo '<select id="langselect" name="lang" class="forminput" onchange="this.form.submit()">';
610 foreach ($languages as $name=>$value) {
611 $selected = ($name == $CFG->lang) ? 'selected="selected"' : '';
612 echo '<option value="'.s($name).'" '.$selected.'>'.$value.'</option>';
614 echo '</select></div>';
617 install_print_footer($config);