MDL-22276 polishing lang strings
[moodle.git] / install.php
blob164d0539eac4540f14c84ecb42e854889bdd5835
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
9 //
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/>.
18 /**
19 * This script creates config.php file during installation.
21 * @package moodlecore
22 * @subpackage install
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']);
29 } else {
30 $lang = 'en';
33 if (isset($_REQUEST['admin'])) {
34 $admin = preg_replace('/[^A-Za-z0-9_-]/i', '', $_REQUEST['admin']);
35 } else {
36 $admin = '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");
43 die;
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 // PHP 5.2.0 is intentionally checked here even though a higher version is required by the environment
55 // check. This is not a typo - see MDL-18112
56 if (version_compare(phpversion(), "5.2.0") < 0) {
57 $phpversion = phpversion();
58 // do NOT localise - lang strings would not work here and we CAN not move it after installib
59 echo "Sorry, Moodle 2.0 requires PHP 5.2.8 or later (currently using version $phpversion).<br />";
60 echo "Please upgrade your server software or install latest Moodle 1.9.x instead.";
61 die;
64 if (PHP_INT_SIZE > 4) {
65 // most probably 64bit PHP - we need a lot more memory
66 $minrequiredmemory = '70M';
67 } else {
68 // 32bit PHP
69 $minrequiredmemory = '40M';
71 // increase or decrease available memory - we need to make sure moodle
72 // installs even with low memory, otherwise developers would overlook
73 // sudden increases of memory needs ;-)
74 @ini_set('memory_limit', $minrequiredmemory);
76 require dirname(__FILE__).'/lib/installlib.php';
78 // TODO: add lang detection here if empty $_REQUEST['lang']
80 // distro specific customisation
81 $distro = null;
82 if (file_exists('install/distrolib.php')) {
83 require_once('install/distrolib.php');
84 if (function_exists('distro_get_config')) {
85 $distro = distro_get_config();
89 $config = new stdClass();
90 $config->lang = $lang;
92 if (!empty($_POST)) {
93 if (install_ini_get_bool('magic_quotes_gpc')) {
94 $_POST = array_map('stripslashes', $_POST);
97 $config->stage = (int)$_POST['stage'];
99 if (isset($_POST['previous'])) {
100 $config->stage--;
101 if (INSTALL_DATABASETYPE and !empty($distro->dbtype)) {
102 $config->stage--;
104 if ($config->stage == INSTALL_ENVIRONMENT or $config->stage == INSTALL_DOWNLOADLANG) {
105 $config->stage--;
107 } else if (isset($_POST['next'])) {
108 $config->stage++;
111 $config->dbtype = trim($_POST['dbtype']);
112 $config->dbhost = trim($_POST['dbhost']);
113 $config->dbuser = trim($_POST['dbuser']);
114 $config->dbpass = trim($_POST['dbpass']);
115 $config->dbname = trim($_POST['dbname']);
116 $config->prefix = trim($_POST['prefix']);
117 $config->dbsocket = (int)(!empty($_POST['dbsocket']));
119 $config->admin = empty($_POST['admin']) ? 'admin' : trim($_POST['admin']);
121 $config->dataroot = trim($_POST['dataroot']);
123 } else {
124 $config->stage = INSTALL_WELCOME;
126 $config->dbtype = empty($distro->dbtype) ? '' : $distro->dbtype; // let distro skip dbtype selection
127 $config->dbhost = empty($distro->dbhost) ? 'localhost' : $distro->dbhost; // let distros set dbhost
128 $config->dbuser = empty($distro->dbuser) ? '' : $distro->dbuser; // let distros set dbuser
129 $config->dbpass = '';
130 $config->dbname = 'moodle';
131 $config->prefix = 'mdl_';
132 $config->dbsocket = 0;
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 = dirname(__FILE__);
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->langotherroot = $CFG->dataroot.'/lang';
150 $CFG->langlocalroot = $CFG->dataroot.'/lang';
151 $CFG->directorypermissions = 00777;
152 $CFG->running_installer = true;
153 $CFG->early_install_lang = true;
155 // Require all needed libs
156 require_once($CFG->libdir.'/setuplib.php');
158 // we need to make sure we have enough memory to load all libraries
159 $memlimit = @ini_get('memory_limit');
160 if (!empty($memlimit) and $memlimit != -1) {
161 if (get_real_size($memlimit) < get_real_size($minrequiredmemory)) {
162 // do NOT localise - lang strings would not work here and we CAN not move it to later place
163 echo "Sorry, Moodle 2.0 requires at least {$minrequiredmemory}B of PHP memory.<br />";
164 echo "Please contact server administrator to fix PHP.ini memory settings.";
165 die;
169 // Continue with lib loading
170 require_once($CFG->libdir.'/textlib.class.php');
171 require_once($CFG->libdir.'/weblib.php');
172 require_once($CFG->libdir.'/outputlib.php');
173 require_once($CFG->libdir.'/dmllib.php');
174 require_once($CFG->libdir.'/moodlelib.php');
175 require_once($CFG->libdir .'/pagelib.php');
176 require_once($CFG->libdir.'/deprecatedlib.php');
177 require_once($CFG->libdir.'/adminlib.php');
178 require_once($CFG->libdir.'/environmentlib.php');
179 require_once($CFG->libdir.'/xmlize.php');
180 require_once($CFG->libdir.'/componentlib.class.php');
182 //point pear include path to moodles lib/pear so that includes and requires will search there for files before anywhere else
183 //the problem is that we need specific version of quickforms and hacked excel files :-(
184 ini_set('include_path', $CFG->libdir.'/pear' . PATH_SEPARATOR . ini_get('include_path'));
185 //point zend include path to moodles lib/zend so that includes and requires will search there for files before anywhere else
186 ini_set('include_path', $CFG->libdir.'/zend' . PATH_SEPARATOR . ini_get('include_path'));
188 require('version.php');
189 $CFG->target_release = $release;
191 $SESSION = new object();
192 $SESSION->lang = $CFG->lang;
194 $USER = new object();
195 $USER->id = 0;
197 $COURSE = new object();
198 $COURSE->id = 0;
200 $SITE = $COURSE;
201 define('SITEID', 0);
203 $hint_dataroot = '';
204 $hint_admindir = '';
205 $hint_database = '';
207 // Are we in help mode?
208 if (isset($_GET['help'])) {
209 install_print_help_page($_GET['help']);
212 // send css?
213 if (isset($_GET['css'])) {
214 install_css_styles();
217 //first time here? find out suitable dataroot
218 if (is_null($CFG->dataroot)) {
219 $CFG->dataroot = dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'moodledata';
221 $i = 0; //safety check - dirname might return some unexpected results
222 while(is_dataroot_insecure()) {
223 $parrent = dirname($CFG->dataroot);
224 $i++;
225 if ($parrent == '/' or $parrent == '.' or preg_match('/^[a-z]:\\\?$/i', $parrent) or ($i > 100)) {
226 $CFG->dataroot = ''; //can not find secure location for dataroot
227 break;
229 $CFG->dataroot = dirname($parrent).DIRECTORY_SEPARATOR.'moodledata';
231 $config->dataroot = $CFG->dataroot;
232 $config->stage = INSTALL_WELCOME;
235 // now let's do the stage work
236 if ($config->stage < INSTALL_WELCOME) {
237 $config->stage = INSTALL_WELCOME;
239 if ($config->stage > INSTALL_SAVE) {
240 $config->stage = INSTALL_SAVE;
245 if ($config->stage == INSTALL_SAVE) {
246 $CFG->early_install_lang = false;
248 $database = moodle_database::get_driver_instance($config->dbtype, 'native');
249 if (!$database->driver_installed()) {
250 $config->stage = INSTALL_DATABASETYPE;
251 } else {
252 if (function_exists('distro_pre_create_db')) { // Hook for distros needing to do something before DB creation
253 $distro = distro_pre_create_db($database, $config->dbhost, $config->dbuser, $config->dbpass, $config->dbname, $config->prefix, array('dbpersist'=>0, 'dbsocket'=>$config->dbsocket), $distro);
255 $hint_database = install_db_validate($database, $config->dbhost, $config->dbuser, $config->dbpass, $config->dbname, $config->prefix, array('dbpersist'=>0, 'dbsocket'=>$config->dbsocket));
257 if ($hint_database === '') {
258 $configphp = install_generate_configphp($database, $CFG);
260 umask(0137);
261 if (($fh = @fopen($configfile, 'w')) !== false) {
262 fwrite($fh, $configphp);
263 fclose($fh);
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>';
275 echo p($configphp);
276 echo '</pre></div>';
278 install_print_footer($config);
279 die;
281 } else {
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)) {
298 $a = new stdClass();
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;
304 } else {
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->admin === '' or !file_exists($CFG->dirroot.'/'.$config->admin.'/environment.xml')) {
318 $hint_admindir = get_string('pathswrongadmindir', 'install');
319 $config->stage = INSTALL_PATHS;
325 if ($config->stage == INSTALL_DOWNLOADLANG) {
326 // no need to download anything if en lang selected
327 if ($CFG->lang == 'en') {
328 $config->stage = INSTALL_DATABASETYPE;
334 if ($config->stage == INSTALL_DATABASETYPE) {
335 // skip db selection if distro package supports only one db
336 if (!empty($distro->dbtype)) {
337 $config->stage = INSTALL_DATABASE;
342 if ($config->stage == INSTALL_DOWNLOADLANG) {
343 $downloaderror = '';
345 // Create necessary lang dir
346 if (!make_upload_directory('lang', false)) {
347 $downloaderror = get_string('cannotcreatelangdir', 'error');
349 // Download and install lang component
350 } else if ($cd = new component_installer('http://download.moodle.org', 'langpack/2.0', $CFG->lang.'.zip', 'languages.md5', 'lang')) {
351 if ($cd->install() == COMPONENT_ERROR) {
352 if ($cd->get_error() == 'remotedownloaderror') {
353 $a = new stdClass();
354 $a->url = 'http://download.moodle.org/langpack/2.0/'.$config->lang.'.zip';
355 $a->dest = $CFG->dataroot.'/lang';
356 $downloaderror = get_string($cd->get_error(), 'error', $a);
357 } else {
358 $downloaderror = get_string($cd->get_error(), 'error');
360 } else {
361 // install parent lang if defined
362 if ($parentlang = get_parent_language()) {
363 if ($cd = new component_installer('http://download.moodle.org', 'langpack/2.0', $parentlang.'.zip', 'languages.md5', 'lang')) {
364 $cd->install();
369 // switch the string_manager instance to stop using install/lang/
370 $CFG->early_install_lang = false;
371 $CFG->langotherroot = $CFG->dataroot.'/lang';
372 $CFG->langlocalroot = $CFG->dataroot.'/lang';
373 get_string_manager(true);
375 if ($downloaderror !== '') {
376 install_print_header($config, get_string('language'), get_string('langdownloaderror', 'install', $CFG->lang), $downloaderror);
377 install_print_footer($config);
378 die;
379 } else {
380 if (empty($distro->dbtype)) {
381 $config->stage = INSTALL_DATABASETYPE;
382 } else {
383 $config->stage = INSTALL_DATABASE;
389 if ($config->stage == INSTALL_DATABASE) {
390 $CFG->early_install_lang = false;
392 $database = moodle_database::get_driver_instance($config->dbtype, 'native');
394 $sub = '<h3>'.$database->get_name().'</h3>'.$database->get_configuration_help();
396 install_print_header($config, get_string('database', 'install'), get_string('databasehead', 'install'), $sub);
398 $strdbhost = get_string('databasehost', 'install');
399 $strdbname = get_string('databasename', 'install');
400 $strdbuser = get_string('databaseuser', 'install');
401 $strdbpass = get_string('databasepass', 'install');
402 $strprefix = get_string('dbprefix', 'install');
403 $strdbsocket = get_string('databasesocket', 'install');
405 echo '<div class="userinput">';
407 $disabled = empty($distro->dbhost) ? '' : 'disabled="disabled';
408 echo '<div class="formrow"><label for="id_dbhost" class="formlabel">'.$strdbhost.'</label>';
409 echo '<input id="id_dbhost" name="dbhost" '.$disabled.' type="text" value="'.s($config->dbhost).'" size="30" class="forminput" />';
410 echo '</div>';
412 echo '<div class="formrow"><label for="id_dbname" class="formlabel">'.$strdbname.'</label>';
413 echo '<input id="id_dbname" name="dbname" type="text" value="'.s($config->dbname).'" size="30" class="forminput" />';
414 echo '</div>';
416 $disabled = empty($distro->dbuser) ? '' : 'disabled="disabled';
417 echo '<div class="formrow"><label for="id_dbuser" class="formlabel">'.$strdbuser.'</label>';
418 echo '<input id="id_dbuser" name="dbuser" '.$disabled.' type="text" value="'.s($config->dbuser).'" size="30" class="forminput" />';
419 echo '</div>';
421 echo '<div class="formrow"><label for="id_dbpass" class="formlabel">'.$strdbpass.'</label>';
422 // no password field here, the password may be visible in config.php if we can not write it to disk
423 echo '<input id="id_dbpass" name="dbpass" type="text" value="'.s($config->dbpass).'" size="30" class="forminput" />';
424 echo '</div>';
426 echo '<div class="formrow"><label for="id_prefix" class="formlabel">'.$strprefix.'</label>';
427 echo '<input id="id_prefix" name="prefix" type="text" value="'.s($config->prefix).'" size="10" class="forminput" />';
428 echo '</div>';
430 if (!(stristr(PHP_OS, 'win') && !stristr(PHP_OS, 'darwin'))) {
431 $checked = $config->dbsocket ? 'checked="checked' : '';
432 echo '<div class="formrow"><label for="id_dbsocket" class="formlabel">'.$strdbsocket.'</label>';
433 echo '<input type="hidden" value="0" name="dbsocket" />';
434 echo '<input type="checkbox" id="id_dbsocket" value="1" name="dbsocket" '.$checked.' class="forminput" />';
435 echo '</div>';
438 echo '<div class="hint">'.$hint_database.'</div>';
439 echo '</div>';
440 install_print_footer($config);
441 die;
445 if ($config->stage == INSTALL_DATABASETYPE) {
446 $CFG->early_install_lang = false;
448 // Finally ask for DB type
449 install_print_header($config, get_string('database', 'install'),
450 get_string('databasetypehead', 'install'),
451 get_string('databasetypesub', 'install'));
453 // TODO: move this PHP5 code to lib/installib.php so that this file parses in PHP4
454 $databases = array('mysqli' => moodle_database::get_driver_instance('mysqli', 'native'),
455 'pgsql' => moodle_database::get_driver_instance('pgsql', 'native'),
456 'oci' => moodle_database::get_driver_instance('oci', 'native'),
457 //'sqlsrv' => moodle_database::get_driver_instance('sqlsrv', 'native'), // new MS sql driver - win32 only
458 'mssql' => moodle_database::get_driver_instance('mssql', 'native'), // FreeTDS driver
461 echo '<div class="userinput">';
462 echo '<div class="formrow"><label class="formlabel" for="dbtype">'.get_string('dbtype', 'install').'</label>';
463 echo '<select id="dbtype" name="dbtype" class="forminput">';
464 $disabled = array();
465 $options = array();
466 foreach ($databases as $type=>$database) {
467 if ($database->driver_installed() !== true) {
468 $disabled[$type] = $database;
469 continue;
471 echo '<option value="'.s($type).'">'.$database->get_name().'</option>';
473 if ($disabled) {
474 echo '<optgroup label="'.s(get_string('notavailable')).'">';
475 foreach ($disabled as $type=>$database) {
476 echo '<option value="'.s($type).'" class="notavailable">'.$database->get_name().'</option>';
478 echo '</optgroup>';
480 echo '</select></div>';
481 echo '</div>';
483 install_print_footer($config);
484 die;
489 if ($config->stage == INSTALL_ENVIRONMENT or $config->stage == INSTALL_PATHS) {
490 $version_fail = (version_compare(phpversion(), "5.2.8") < 0);
491 $curl_fail = ($lang !== 'en' and !extension_loaded('curl')); // needed for lang pack download
492 $zip_fail = ($lang !== 'en' and !extension_loaded('zip')); // needed for lang pack download
494 if ($version_fail or $curl_fail or $zip_fail) {
495 $config->stage = INSTALL_ENVIRONMENT;
497 install_print_header($config, get_string('environment', 'install'),
498 get_string('errorsinenvironment', 'install'),
499 get_string('environmentsub2', 'install'));
501 echo '<div id="envresult"><dl>';
502 if ($version_fail) {
503 $a = (object)array('needed'=>'5.2.8', 'current'=>phpversion());
504 echo '<dt>'.get_string('phpversion', 'install').'</dt><dd>'.get_string('environmentrequireversion', 'admin', $a).'</dd>';
506 if ($curl_fail) {
507 echo '<dt>'.get_string('phpextension', 'install', 'cURL').'</dt><dd>'.get_string('environmentrequireinstall', 'admin').'</dd>';
509 if ($zip_fail) {
510 echo '<dt>'.get_string('phpextension', 'install', 'Zip').'</dt><dd>'.get_string('environmentrequireinstall', 'admin').'</dd>';
512 echo '</dl></div>';
514 install_print_footer($config, true);
515 die;
517 } else {
518 $config->stage = INSTALL_PATHS;
524 if ($config->stage == INSTALL_PATHS) {
525 $paths = array('wwwroot' => get_string('wwwroot', 'install'),
526 'dirroot' => get_string('dirroot', 'install'),
527 'dataroot' => get_string('dataroot', 'install'));
529 $sub = '<dl>';
530 foreach ($paths as $path=>$name) {
531 $sub .= '<dt>'.$name.'</dt><dd>'.get_string('pathssub'.$path, 'install').'</dd>';
533 if (!file_exists("$CFG->dirroot/admin/environment.xml")) {
534 $sub .= '<dt>'.get_string('admindirname', 'install').'</dt><dd>'.get_string('pathssubadmindir', 'install').'</dd>';
536 $sub .= '</dl>';
538 install_print_header($config, get_string('paths', 'install'), get_string('pathshead', 'install'), $sub);
540 $strwwwroot = get_string('wwwroot', 'install');
541 $strdirroot = get_string('dirroot', 'install');
542 $strdataroot = get_string('dataroot', 'install');
543 $stradmindirname = get_string('admindirname', 'install');
545 echo '<div class="userinput">';
546 echo '<div class="formrow"><label for="id_wwwroot" class="formlabel">'.$paths['wwwroot'].'</label>';
547 echo '<input id="id_wwwroot" name="wwwroot" type="text" value="'.s($CFG->wwwroot).'" disabled="disabled" size="45" class="forminput" />';
548 echo '</div>';
550 echo '<div class="formrow"><label for="id_dirroot" class="formlabel">'.$paths['dirroot'].'</label>';
551 echo '<input id="id_dirroot" name="dirroot" type="text" value="'.s($CFG->dirroot).'" disabled="disabled" size="45"class="forminput" />';
552 echo '</div>';
554 echo '<div class="formrow"><label for="id_dataroot" class="formlabel">'.$paths['dataroot'].'</label>';
555 echo '<input id="id_dataroot" name="dataroot" type="text" value="'.s($config->dataroot).'" size="45" class="forminput" />';
556 if ($hint_dataroot !== '') {
557 echo '<div class="hint">'.$hint_dataroot.'</div>';
559 echo '</div>';
562 if (!file_exists("$CFG->dirroot/admin/environment.xml")) {
563 echo '<div class="formrow"><label for="id_admin" class="formlabel">'.$paths['admindir'].'</label>';
564 echo '<input id="id_admin" name="admin" type="text" value="'.s($config->admin).'" size="10" class="forminput" />';
565 if ($hint_admindir !== '') {
566 echo '<div class="hint">'.$hint_admindir.'</div>';
568 echo '</div>';
571 echo '</div>';
573 install_print_footer($config);
574 die;
579 $config->stage = INSTALL_WELCOME;
581 if ($distro) {
582 ob_start();
583 include('install/distribution.html');
584 $sub = ob_get_clean();
586 install_print_header($config, get_string('language'),
587 get_string('chooselanguagehead', 'install'),
588 $sub);
590 } else {
591 install_print_header($config, get_string('language'),
592 get_string('chooselanguagehead', 'install'),
593 get_string('chooselanguagesub', 'install'));
596 $languages = get_string_manager()->get_list_of_translations();
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>';
605 echo '</div>';
607 install_print_footer($config);
608 die;