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 // make sure PHP errors are displayed - helps with diagnosing of problems
47 @error_reporting
(E_ALL
);
48 @ini_set
('display_errors', '1');
50 // Check that PHP is of a sufficient version
51 if (version_compare(phpversion(), "5.2.0") < 0) {
52 $phpversion = phpversion();
53 // do NOT localise - lang strings would not work here and we CAN not move it after installib
54 echo "Sorry, Moodle 2.0 requires PHP 5.2.8 or later (currently using version $phpversion).<br />";
55 echo "Please upgrade your server software or install latest Moodle 1.9.x instead.";
59 if (PHP_INT_SIZE
> 4) {
60 // most probably 64bit PHP - we need a lot more memory
61 $minrequiredmemory = '70M';
64 $minrequiredmemory = '40M';
66 // increase or decrease available memory - we need to make sure moodle
67 // installs even with low memory, otherwise developers would overlook
68 // sudden increases of memory needs ;-)
69 @ini_set
('memory_limit', $minrequiredmemory);
71 require dirname(__FILE__
).'/lib/installlib.php';
73 // TODO: add lang detection here if empty $_REQUEST['lang']
75 // distro specific customisation
77 if (file_exists('install/distrolib.php')) {
78 require_once('install/distrolib.php');
79 if (function_exists('distro_get_config')) {
80 $distro = distro_get_config();
84 $config = new stdClass();
85 $config->lang
= $lang;
88 if (install_ini_get_bool('magic_quotes_gpc')) {
89 $_POST = array_map('stripslashes', $_POST);
92 $config->stage
= (int)$_POST['stage'];
94 if (isset($_POST['previous'])) {
96 if (INSTALL_DATABASETYPE
and !empty($distro->dbtype
)) {
99 if ($config->stage
== INSTALL_ENVIRONMENT
or $config->stage
== INSTALL_DOWNLOADLANG
) {
102 } else if (isset($_POST['next'])) {
106 $config->dbtype
= trim($_POST['dbtype']);
107 $config->dbhost
= trim($_POST['dbhost']);
108 $config->dbuser
= trim($_POST['dbuser']);
109 $config->dbpass
= trim($_POST['dbpass']);
110 $config->dbname
= trim($_POST['dbname']);
111 $config->prefix
= trim($_POST['prefix']);
112 $config->dbsocket
= (int)(!empty($_POST['dbsocket']));
114 $config->dirroot
= trim($_POST['dirroot']);
115 $config->admin
= empty($_POST['admin']) ?
'admin' : trim($_POST['admin']);
117 $config->dataroot
= trim($_POST['dataroot']);
120 $config->stage
= INSTALL_WELCOME
;
122 $config->dbtype
= empty($distro->dbtype
) ?
'' : $distro->dbtype
; // let distro skip dbtype selection
123 $config->dbhost
= empty($distro->dbhost
) ?
'localhost' : $distro->dbhost
; // let distros set dbhost
124 $config->dbuser
= empty($distro->dbuser
) ?
'' : $distro->dbuser
; // let distros set dbuser
125 $config->dbpass
= '';
126 $config->dbname
= 'moodle';
127 $config->prefix
= 'mdl_';
128 $config->dbsocket
= 0;
130 $config->dirroot
= str_replace('\\', '/', dirname(__FILE__
)); // Fix for win32
131 $config->admin
= 'admin';
133 $config->dataroot
= empty($distro->dataroot
) ?
null : $distro->dataroot
; // initialised later after including libs or by distro
136 // Fake some settings so that we can use selected functions from moodlelib.php and weblib.php
137 $CFG = new stdClass();
138 $CFG->lang
= $config->lang
;
139 $CFG->dirroot
= str_replace('\\', '/', dirname(__FILE__
)); // Fix for win32
140 $CFG->libdir
= "$CFG->dirroot/lib";
141 $CFG->wwwroot
= install_guess_wwwroot(); // can not be changed - ppl must use the real address when installing
142 $CFG->httpswwwroot
= $CFG->wwwroot
;
143 $CFG->dataroot
= $config->dataroot
;
144 $CFG->admin
= $config->admin
;
145 $CFG->docroot
= 'http://docs.moodle.org';
146 $CFG->directorypermissions
= 00777;
147 $CFG->running_installer
= true;
149 // Require all needed libs
150 require_once($CFG->libdir
.'/setuplib.php');
152 // we need to make sure we have enough memory to load all libraries
153 $memlimit = @ini_get
('memory_limit');
154 if (!empty($memlimit) and $memlimit != -1) {
155 if (get_real_size($memlimit) < get_real_size($minrequiredmemory)) {
156 // do NOT localise - lang strings would not work here and we CAN not move it to later place
157 echo "Sorry, Moodle 2.0 requires at least {$minrequiredmemory}B of PHP memory.<br />";
158 echo "Please contact server administrator to fix PHP.ini memory settings.";
163 // Continue with lib loading
164 require_once($CFG->libdir
.'/textlib.class.php');
165 require_once($CFG->libdir
.'/weblib.php');
166 require_once($CFG->libdir
.'/outputlib.php');
167 require_once($CFG->libdir
.'/dmllib.php');
168 require_once($CFG->libdir
.'/moodlelib.php');
169 require_once($CFG->libdir
.'/pagelib.php');
170 require_once($CFG->libdir
.'/deprecatedlib.php');
171 require_once($CFG->libdir
.'/adminlib.php');
172 require_once($CFG->libdir
.'/environmentlib.php');
173 require_once($CFG->libdir
.'/xmlize.php');
174 require_once($CFG->libdir
.'/componentlib.class.php');
176 //point pear include path to moodles lib/pear so that includes and requires will search there for files before anywhere else
177 //the problem is that we need specific version of quickforms and hacked excel files :-(
178 ini_set('include_path', $CFG->libdir
.'/pear' . PATH_SEPARATOR
. ini_get('include_path'));
179 //point zend include path to moodles lib/zend so that includes and requires will search there for files before anywhere else
180 ini_set('include_path', $CFG->libdir
.'/zend' . PATH_SEPARATOR
. ini_get('include_path'));
182 require('version.php');
183 $CFG->target_release
= $release;
185 $SESSION = new object();
186 $SESSION->lang
= $CFG->lang
;
188 $USER = new object();
191 $COURSE = new object();
202 // Are we in help mode?
203 if (isset($_GET['help'])) {
204 install_print_help_page($_GET['help']);
208 if (isset($_GET['css'])) {
209 install_css_styles();
212 //first time here? find out suitable dataroot
213 if (is_null($CFG->dataroot
)) {
214 $CFG->dataroot
= str_replace('\\', '/', dirname(dirname(__FILE__
)).'/moodledata');
216 $i = 0; //safety check - dirname might return some unexpected results
217 while(is_dataroot_insecure()) {
218 $parrent = dirname($CFG->dataroot
);
220 if ($parrent == '/' or $parrent == '.' or preg_match('/^[a-z]:\\\?$/i', $parrent) or ($i > 100)) {
221 $CFG->dataroot
= ''; //can not find secure location for dataroot
224 $CFG->dataroot
= dirname($parrent).'/moodledata';
226 $config->dataroot
= $CFG->dataroot
;
227 $config->stage
= INSTALL_WELCOME
;
230 // now let's do the stage work
231 if ($config->stage
< INSTALL_WELCOME
) {
232 $config->stage
= INSTALL_WELCOME
;
234 if ($config->stage
> INSTALL_SAVE
) {
235 $config->stage
= INSTALL_SAVE
;
240 if ($config->stage
== INSTALL_SAVE
) {
241 $database = moodle_database
::get_driver_instance($config->dbtype
, 'native');
242 if (!$database->driver_installed()) {
243 $config->stage
= INSTALL_DATABASETYPE
;
245 if (function_exists('distro_pre_create_db')) { // Hook for distros needing to do something before DB creation
246 $distro = distro_pre_create_db($database, $config->dbhost
, $config->dbuser
, $config->dbpass
, $config->dbname
, $config->prefix
, array('dbpersit'=>0, 'dbsocket'=>$config->dbsocket
), $distro);
248 $hint_database = install_db_validate($database, $config->dbhost
, $config->dbuser
, $config->dbpass
, $config->dbname
, $config->prefix
, array('dbpersit'=>0, 'dbsocket'=>$config->dbsocket
));
250 if ($hint_database === '') {
251 // extra hackery needed for symbolic link support
252 if ($CFG->dirroot
!== $config->dirroot
) {
253 $CFG->dirroot
= $config->dirroot
;
256 $userealpath = false;
258 $configphp = install_generate_configphp($database, $CFG, $userealpath);
261 if (($fh = @fopen
($configfile, 'w')) !== false) {
262 fwrite($fh, $configphp);
266 if (file_exists($configfile)) {
267 // config created, let's continue!
268 redirect("$CFG->wwwroot/$config->admin/index.php?lang=$config->lang");
271 install_print_header($config, 'config.php',
272 get_string('configurationcompletehead', 'install'),
273 get_string('configurationcompletesub', 'install').get_string('configfilenotwritten', 'install'));
274 echo '<div class="configphp"><pre>';
278 install_print_footer($config);
282 $config->stage
= INSTALL_DATABASE
;
289 if ($config->stage
== INSTALL_DOWNLOADLANG
) {
290 if (empty($CFG->dataroot
)) {
291 $config->stage
= INSTALL_PATHS
;
293 } else if (is_dataroot_insecure()) {
294 $hint_dataroot = get_string('pathsunsecuredataroot', 'install');
295 $config->stage
= INSTALL_PATHS
;
297 } else if (!file_exists($CFG->dataroot
)) {
299 $a->parent
= dirname($CFG->dataroot
);
300 $a->dataroot
= $CFG->dataroot
;
301 if (!is_writable(dirname($CFG->dataroot
))) {
302 $hint_dataroot = get_string('pathsroparentdataroot', 'install', $a);
303 $config->stage
= INSTALL_PATHS
;
305 if (!make_upload_directory('lang', false)) {
306 $hint_dataroot = get_string('pathserrcreatedataroot', 'install', $a);
307 $config->stage
= INSTALL_PATHS
;
312 if (empty($hint_dataroot) and !is_writable($CFG->dataroot
)) {
313 $hint_dataroot = get_string('pathsrodataroot', 'install');
314 $config->stage
= INSTALL_PATHS
;
317 if ($config->dirroot
=== '' or !file_exists($config->dirroot
)) {
318 $hint_dirroot = get_string('pathswrongdirroot', 'install');
319 $config->stage
= INSTALL_PATHS
;
322 if ($config->admin
=== '' or !file_exists($config->dirroot
.'/'.$config->admin
.'/environment.xml')) {
323 $hint_admindir = get_string('pathswrongadmindir', 'install');
324 $config->stage
= INSTALL_PATHS
;
330 if ($config->stage
== INSTALL_DOWNLOADLANG
) {
331 // no need to download anything if en lang selected
332 if ($CFG->lang
== 'en_utf8') {
333 $config->stage
= INSTALL_DATABASETYPE
;
339 if ($config->stage
== INSTALL_DATABASETYPE
) {
340 // skip db selection if distro package supports only one db
341 if (!empty($distro->dbtype
)) {
342 $config->stage
= INSTALL_DATABASE
;
347 if ($config->stage
== INSTALL_DOWNLOADLANG
) {
350 // Create necessary lang dir
351 if (!make_upload_directory('lang', false)) {
352 $downloaderror = get_string('cannotcreatelangdir', 'error');
354 // Download and install lang component
355 } else if ($cd = new component_installer('http://download.moodle.org', 'lang16', $CFG->lang
.'.zip', 'languages.md5', 'lang')) {
356 if ($cd->install() == COMPONENT_ERROR
) {
357 if ($cd->get_error() == 'remotedownloaderror') {
359 $a->url
= 'http://download.moodle.org/lang16/'.$INSTALL['language'].'.zip';
360 $a->dest
= $CFG->dataroot
.'/lang';
361 $downloaderror = get_string($cd->get_error(), 'error', $a);
363 $downloaderror = get_string($cd->get_error(), 'error');
366 // install parent lang if defined
367 if ($parentlang = get_parent_language()) {
368 if ($cd = new component_installer('http://download.moodle.org', 'lang16', $parentlang.'.zip', 'languages.md5', 'lang')) {
375 if ($downloaderror !== '') {
376 install_print_header($config, get_string('language'), get_string('langdownloaderror', 'install', $CFG->lang
), $downloaderror);
377 install_print_footer($config);
380 if (empty($distro->dbtype
)) {
381 $config->stage
= INSTALL_DATABASETYPE
;
383 $config->stage
= INSTALL_DATABASE
;
389 if ($config->stage
== INSTALL_DATABASE
) {
390 $database = moodle_database
::get_driver_instance($config->dbtype
, 'native');
392 $sub = '<h3>'.$database->get_name().'</h3>'.$database->get_configuration_help();
394 install_print_header($config, get_string('database', 'install'), get_string('databasehead', 'install'), $sub);
396 $strdbhost = get_string('databasehost', 'install');
397 $strdbname = get_string('databasename', 'install');
398 $strdbuser = get_string('databaseuser', 'install');
399 $strdbpass = get_string('databasepass', 'install');
400 $strprefix = get_string('dbprefix', 'install');
401 $strdbsocket = get_string('databasesocket', 'install');
403 echo '<div class="userinput">';
405 $disabled = empty($distro->dbhost
) ?
'' : 'disabled="disabled';
406 echo '<div class="formrow"><label for="id_dbhost" class="formlabel">'.$strdbhost.'</label>';
407 echo '<input id="id_dbhost" name="dbhost" '.$disabled.' type="text" value="'.s($config->dbhost
).'" size="30" class="forminput" />';
410 echo '<div class="formrow"><label for="id_dbname" class="formlabel">'.$strdbname.'</label>';
411 echo '<input id="id_dbname" name="dbname" type="text" value="'.s($config->dbname
).'" size="30" class="forminput" />';
414 $disabled = empty($distro->dbuser
) ?
'' : 'disabled="disabled';
415 echo '<div class="formrow"><label for="id_dbuser" class="formlabel">'.$strdbuser.'</label>';
416 echo '<input id="id_dbuser" name="dbuser" '.$disabled.' type="text" value="'.s($config->dbuser
).'" size="30" class="forminput" />';
419 echo '<div class="formrow"><label for="id_dbpass" class="formlabel">'.$strdbpass.'</label>';
420 // no password field here, the password may be visible in config.php if we can not write it to disk
421 echo '<input id="id_dbpass" name="dbpass" type="text" value="'.s($config->dbpass
).'" size="30" class="forminput" />';
424 echo '<div class="formrow"><label for="id_prefix" class="formlabel">'.$strprefix.'</label>';
425 echo '<input id="id_prefix" name="prefix" type="text" value="'.s($config->prefix
).'" size="10" class="forminput" />';
428 if (!(stristr(PHP_OS
, 'win') && !stristr(PHP_OS
, 'darwin'))) {
429 $checked = $config->dbsocket ?
'checked="checked' : '';
430 echo '<div class="formrow"><label for="id_dbsocket" class="formlabel">'.$strdbsocket.'</label>';
431 echo '<input type="hidden" value="0" name="dbsocket" />';
432 echo '<input type="checkbox" id="id_dbsocket" value="1" name="dbsocket" '.$checked.' class="forminput" />';
436 echo '<div class="hint">'.$hint_database.'</div>';
438 install_print_footer($config);
443 if ($config->stage
== INSTALL_DATABASETYPE
) {
444 // Finally ask for DB type
445 install_print_header($config, get_string('database', 'install'),
446 get_string('databasetypehead', 'install'),
447 get_string('databasetypesub', 'install'));
449 // TODO: move this PHP5 code to lib/installib.php so that this file parses in PHP4
450 $databases = array('mysqli' => moodle_database
::get_driver_instance('mysqli', 'native'),
451 'pgsql' => moodle_database
::get_driver_instance('pgsql', 'native'),
452 'oci' => moodle_database
::get_driver_instance('oci', 'native'),
453 //'sqlsrv' => moodle_database::get_driver_instance('sqlsrv', 'native'), // new MS sql driver - win32 only
454 //'mssql' => moodle_database::get_driver_instance('mssql', 'native'), // FreeTDS driver
457 echo '<div class="userinput">';
458 echo '<div class="formrow"><label class="formlabel" for="dbtype">'.get_string('dbtype', 'install').'</label>';
459 echo '<select id="dbtype" name="dbtype" class="forminput">';
462 foreach ($databases as $type=>$database) {
463 if ($database->driver_installed() !== true) {
464 $disabled[$type] = $database;
467 echo '<option value="'.s($type).'">'.$database->get_name().'</option>';
470 echo '<optgroup label="'.s(get_string('notavailable')).'">';
471 foreach ($disabled as $type=>$database) {
472 echo '<option value="'.s($type).'" class="notavailable">'.$database->get_name().'</option>';
476 echo '</select></div>';
479 install_print_footer($config);
485 if ($config->stage
== INSTALL_ENVIRONMENT
or $config->stage
== INSTALL_PATHS
) {
486 $version_fail = (version_compare(phpversion(), "5.2.8") < 0);
487 $curl_fail = ($lang !== 'en_utf8' and !extension_loaded('curl')); // needed for lang pack download
488 $zip_fail = ($lang !== 'en_utf8' and !extension_loaded('zip')); // needed for lang pack download
490 if ($version_fail or $curl_fail or $zip_fail) {
491 $config->stage
= INSTALL_ENVIRONMENT
;
493 install_print_header($config, get_string('environment', 'install'),
494 get_string('errorsinenvironment', 'install'),
495 get_string('environmentsub2', 'install'));
497 echo '<div id="envresult"><dl>';
499 $a = (object)array('needed'=>'5.2.8', 'current'=>phpversion());
500 echo '<dt>'.get_string('phpversion', 'install').'</dt><dd>'.get_string('environmentrequireversion', 'admin', $a).'</dd>';
503 echo '<dt>'.get_string('phpextension', 'install', 'cURL').'</dt><dd>'.get_string('environmentrequireinstall', 'admin').'</dd>';
506 echo '<dt>'.get_string('phpextension', 'install', 'Zip').'</dt><dd>'.get_string('environmentrequireinstall', 'admin').'</dd>';
510 install_print_footer($config, true);
514 $config->stage
= INSTALL_PATHS
;
520 if ($config->stage
== INSTALL_PATHS
) {
521 $paths = array('wwwroot' => get_string('wwwroot', 'install'),
522 'dirroot' => get_string('dirroot', 'install'),
523 'dataroot' => get_string('dataroot', 'install'),
524 'admindir' => get_string('admindirname', 'install'));
527 foreach ($paths as $path=>$name) {
528 $sub .= '<dt>'.$name.'</dt><dd>'.get_string('pathssub'.$path, 'install').'</dd>';
532 install_print_header($config, get_string('paths', 'install'), get_string('pathshead', 'install'), $sub);
534 $strwwwroot = get_string('wwwroot', 'install');
535 $strdirroot = get_string('dirroot', 'install');
536 $strdataroot = get_string('dataroot', 'install');
537 $stradmindirname = get_string('admindirname', 'install');
539 echo '<div class="userinput">';
540 echo '<div class="formrow"><label for="id_wwwroot" class="formlabel">'.$paths['wwwroot'].'</label>';
541 echo '<input id="id_wwwroot" name="wwwroot" type="text" value="'.s($CFG->wwwroot
).'" disabled="disabled" size="45" class="forminput" />';
544 echo '<div class="formrow"><label for="id_dirroot" class="formlabel">'.$paths['dirroot'].'</label>';
545 echo '<input id="id_dirroot" name="dirroot" type="text" value="'.s($config->dirroot
).'" size="45"class="forminput" />';
546 if ($hint_dirroot !== '') {
547 echo '<div class="hint">'.$hint_dirroot.'</div>';
551 echo '<div class="formrow"><label for="id_dataroot" class="formlabel">'.$paths['dataroot'].'</label>';
552 echo '<input id="id_dataroot" name="dataroot" type="text" value="'.s($config->dataroot
).'" size="45" class="forminput" />';
553 if ($hint_dataroot !== '') {
554 echo '<div class="hint">'.$hint_dataroot.'</div>';
559 if (file_exists("$CFG->dirroot/admin/environment.xml")) {
560 $disabled = 'disabled="disabled"';
564 echo '<div class="formrow"><label for="id_admin" class="formlabel">'.$paths['admindir'].'</label>';
565 echo '<input id="id_admin" name="admin" type="text" value="'.s($config->admin
).'" '.$disabled.' size="10" class="forminput" />';
566 if ($hint_admindir !== '') {
567 echo '<div class="hint">'.$hint_admindir.'</div>';
573 install_print_footer($config);
579 $config->stage
= INSTALL_WELCOME
;
583 include('install/distribution.html');
584 $sub = ob_get_clean();
586 install_print_header($config, get_string('language'),
587 get_string('chooselanguagehead', 'install'),
591 install_print_header($config, get_string('language'),
592 get_string('chooselanguagehead', 'install'),
593 get_string('chooselanguagesub', 'install'));
596 $languages = install_get_list_of_languages();
597 echo '<div class="userinput">';
598 echo '<div class="formrow"><label class="formlabel" for="langselect">'.get_string('language').'</label>';
599 echo '<select id="langselect" name="lang" class="forminput" onchange="this.form.submit()">';
600 foreach ($languages as $name=>$value) {
601 $selected = ($name == $CFG->lang
) ?
'selected="selected"' : '';
602 echo '<option value="'.s($name).'" '.$selected.'>'.$value.'</option>';
604 echo '</select></div>';
607 install_print_footer($config);