2 /// install.php - helps admin user to create a config.php file
5 if (isset($_REQUEST['lang'])) {
6 $lang = eregi_replace('[^A-Za-z0-9_-]', '', $_REQUEST['lang']);
11 if (isset($_REQUEST['admin'])) {
12 $admin = eregi_replace('[^A-Za-z0-9_-]', '', $_REQUEST['admin']);
17 /// If config.php exists we just created config.php and need to redirect to continue installation
18 $configfile = './config.php';
19 if (file_exists($configfile)) {
20 header("Location: $admin/index.php?lang=$lang");
24 /// make sure PHP errors are displayed - helps with diagnosing of problems
25 @error_reporting(E_ALL);
26 @ini_set('display_errors', '1');
27 // we need a lot of memory
28 @ini_set('memory_limit', '40M');
30 /// Check that PHP is of a sufficient version
31 if (version_compare(phpversion(), "5.2.0") < 0) {
32 $phpversion = phpversion();
33 // do NOT localise - lang strings would not work here and we CAN not move it after installib
34 echo "Sorry, Moodle 2.0 requires PHP 5.2.8 or later (currently using version $phpversion). ";
35 echo "Please upgrade your server software or install latest Moodle 1.9.x instead.";
39 require dirname(__FILE__).'/lib/installlib.php';
41 // TODO: add lang detection here if empty $_REQUEST['lang']
43 $config = new stdClass();
44 $config->lang = $lang;
47 if (install_ini_get_bool('magic_quotes_gpc')) {
48 $_POST = array_map('stripslashes', $_POST);
51 $config->stage = (int)$_POST['stage'];
53 if (isset($_POST['previous'])) {
55 if ($config->stage == INSTALL_ENVIRONMENT or $config->stage == INSTALL_DOWNLOADLANG) {
58 } else if (isset($_POST['next'])) {
62 $config->dbtype = trim($_POST['dbtype']);
63 $config->dbhost = trim($_POST['dbhost']);
64 $config->dbuser = trim($_POST['dbuser']);
65 $config->dbpass = trim($_POST['dbpass']);
66 $config->dbname = trim($_POST['dbname']);
67 $config->prefix = trim($_POST['prefix']);
68 $config->dbsocket = (int)(!empty($_POST['dbsocket']));
70 $config->dirroot = trim($_POST['dirroot']);
71 $config->admin = empty($_POST['admin']) ? 'admin' : trim($_POST['admin']);
73 $config->dataroot = trim($_POST['dataroot']);
76 $config->stage = INSTALL_WELCOME;
79 $config->dbhost = 'localhost';
82 $config->dbname = 'moodle';
83 $config->prefix = 'mdl_';
84 $config->dbsocket = 0;
86 $config->dirroot = dirname(__FILE__);
87 $config->admin = 'admin';
89 $config->dataroot = null; // initialised later after including libs
92 /// Fake some settings so that we can use selected functions from moodlelib.php and weblib.php
93 $CFG = new stdClass();
94 $CFG->lang = $config->lang;
95 $CFG->dirroot = dirname(__FILE__);
96 $CFG->libdir = "$CFG->dirroot/lib";
97 $CFG->wwwroot = install_guess_wwwroot(); // can not be changed - ppl must use the real address when installing
98 $CFG->httpswwwroot = $CFG->wwwroot;
99 $CFG->httpsthemewww = $CFG->wwwroot;
100 $CFG->dataroot = $config->dataroot;
101 $CFG->admin = $config->admin;
102 $CFG->docroot = 'http://docs.moodle.org';
103 $CFG->directorypermissions = 00777;
104 $CFG->running_installer = true;
106 /// Require all needed libs
107 require_once($CFG->libdir.'/setuplib.php');
108 require_once($CFG->libdir.'/textlib.class.php');
109 require_once($CFG->libdir.'/weblib.php');
110 require_once($CFG->libdir.'/dmllib.php');
111 require_once($CFG->libdir.'/moodlelib.php');
112 require_once($CFG->libdir.'/deprecatedlib.php');
113 require_once($CFG->libdir.'/adminlib.php');
114 require_once($CFG->libdir.'/environmentlib.php');
115 require_once($CFG->libdir.'/xmlize.php');
116 require_once($CFG->libdir.'/componentlib.class.php');
118 require('version.php');
119 $CFG->release = $release;
121 $SESSION = new object();
122 $SESSION->lang = $CFG->lang;
124 $USER = new object();
127 $COURSE = new object();
138 /// Are we in help mode?
139 if (isset($_GET['help'])) {
140 install_print_help_page($_GET['help']);
144 if (isset($_GET['css'])) {
145 install_css_styles();
148 ///first time here? find out suitable dataroot
149 if (is_null($CFG->dataroot)) {
150 $CFG->dataroot = str_replace('\\', '/', dirname(dirname(__FILE__)).'/moodledata');
152 $i = 0; //safety check - dirname might return some unexpected results
153 while(is_dataroot_insecure()) {
154 $parrent = dirname($CFG->dataroot);
156 if ($parrent == '/' or $parrent == '.' or preg_match('/^[a-z]:\\\?$/i', $parrent) or ($i > 100)) {
157 $CFG->dataroot = ''; //can not find secure location for dataroot
160 $CFG->dataroot = dirname($parrent).'/moodledata';
162 $config->dataroot = $CFG->dataroot;
163 $config->stage = INSTALL_WELCOME;
166 // now let's do the stage work
167 if ($config->stage < INSTALL_WELCOME) {
168 $config->stage = INSTALL_WELCOME;
170 if ($config->stage > INSTALL_SAVE) {
171 $config->stage = INSTALL_SAVE;
176 if ($config->stage == INSTALL_SAVE) {
177 $database = moodle_database::get_driver_instance($config->dbtype, 'native');
178 if (!$database->driver_installed()) {
179 $config->stage = INSTALL_DATABASETYPE;
181 $hint_database = install_db_validate($database, $config->dbhost, $config->dbuser, $config->dbpass, $config->dbname, $config->prefix, array('dbpersit'=>0, 'dbsocket'=>$config->dbsocket));
183 if ($hint_database === '') {
184 $configphp = '<?php /// Moodle Configuration File ' . "\r\n\r\n";
186 $configphp .= 'unset($CFG);'."\r\n";
187 $configphp .= '$CFG = new stdClass();'."\r\n\r\n"; // prevent PHP5 strict warnings
189 $dbconfig = $database->export_dbconfig();
191 foreach ($dbconfig as $key=>$value) {
192 $key = str_pad($key, 9);
193 $configphp .= '$CFG->'.$key.' = '.var_export($value, true).";\r\n";
195 $configphp .= "\r\n";
197 $configphp .= '$CFG->wwwroot = '.var_export($CFG->wwwroot, true).";\r\n";
199 if ($CFG->dirroot !== $config->dirroot) {
200 $configphp .= '$CFG->dirroot = realpath('.var_export($config->dirroot, true).");\r\n"; // fix for sym links
202 $dirroot = str_replace('\\', '/', $CFG->dirroot); // win32 fix
203 $dirroot = rtrim($dirroot, '/'); // no trailing /
204 $configphp .= '$CFG->dirroot = '.var_export($dirroot, true).";\r\n";
207 $dataroot = str_replace('\\', '/', $config->dataroot); // win32 fix
208 $dataroot = rtrim($dataroot, '/'); // no trailing /
209 $configphp .= '$CFG->dataroot = '.var_export($dataroot, true).";\r\n";
211 $configphp .= '$CFG->admin = '.var_export($config->admin, true).";\r\n\r\n";
213 $configphp .= '$CFG->directorypermissions = 00777; // try 02777 on a server in Safe Mode'."\r\n";
214 $configphp .= "\r\n";
216 $configphp .= 'require_once("$CFG->dirroot/lib/setup.php");'."\r\n\r\n";
217 $configphp .= '// There is no php closing tag in this file,'."\r\n";
218 $configphp .= '// it is intentional because it prevents trailing whitespace problems!'."\r\n";
222 if (($fh = @fopen($configfile, 'w')) !== false) {
223 fwrite($fh, $configphp);
227 if (file_exists($configfile)) {
228 // config created, let's continue!
229 redirect("$CFG->wwwroot/$config->admin/index.php?lang=$config->lang");
232 install_print_header($config, 'config.php',
233 get_string('configurationcompletehead', 'install'),
234 get_string('configurationcompletesub', 'install').get_string('configfilenotwritten', 'install'));
235 echo '<div class="configphp"><pre>';
239 install_print_footer($config);
243 $config->stage = INSTALL_DATABASE;
250 if ($config->stage == INSTALL_DATABASE) {
251 $database = moodle_database::get_driver_instance($config->dbtype, 'native');
253 $sub = '<h3>'.$database->get_name().'</h3>'.$database->get_configuration_help();
255 install_print_header($config, get_string('database', 'install'), get_string('databasehead', 'install'), $sub);
257 $strdbhost = get_string('databasehost', 'install');
258 $strdbname = get_string('databasename', 'install');
259 $strdbuser = get_string('databaseuser', 'install');
260 $strdbpass = get_string('databasepass', 'install');
261 $strprefix = get_string('dbprefix', 'install');
262 $strdbsocket = get_string('databasesocket', 'install');
264 echo '<div class="userinput">';
265 echo '<div class="formrow"><label for="id_dbhost" class="formlabel">'.$strdbhost.'</label>';
266 echo '<input id="id_dbhost" name="dbhost" type="text" value="'.s($config->dbhost).'" size="30" class="forminput" />';
269 echo '<div class="formrow"><label for="id_dbname" class="formlabel">'.$strdbname.'</label>';
270 echo '<input id="id_dbname" name="dbname" type="text" value="'.s($config->dbname).'" size="30" class="forminput" />';
273 echo '<div class="formrow"><label for="id_dbuser" class="formlabel">'.$strdbuser.'</label>';
274 echo '<input id="id_dbuser" name="dbuser" type="text" value="'.s($config->dbuser).'" size="30" class="forminput" />';
277 echo '<div class="formrow"><label for="id_dbpass" class="formlabel">'.$strdbpass.'</label>';
278 // no password field here, the password may be visible in config.php if we can not write it to disk
279 echo '<input id="id_dbpass" name="dbpass" type="text" value="'.s($config->dbpass).'" size="30" class="forminput" />';
282 echo '<div class="formrow"><label for="id_prefix" class="formlabel">'.$strprefix.'</label>';
283 echo '<input id="id_prefix" name="prefix" type="text" value="'.s($config->prefix).'" size="10" class="forminput" />';
286 if (!(stristr(PHP_OS, 'win') && !stristr(PHP_OS, 'darwin'))) {
287 $checked = $config->dbsocket ? 'checked="checked' : '';
288 echo '<div class="formrow"><label for="id_dbsocket" class="formlabel">'.$strdbsocket.'</label>';
289 echo '<input type="hidden" value="0" name="dbsocket" />';
290 echo '<input type="checkbox" id="id_dbsocket" value="1" name="dbsocket" '.$checked.' class="forminput" />';
294 echo '<div class="hint">'.$hint_database.'</div>';
296 install_print_footer($config);
302 if ($config->stage == INSTALL_DOWNLOADLANG) {
303 if (empty($CFG->dataroot)) {
304 $config->stage = INSTALL_PATHS;
306 } else if (is_dataroot_insecure()) {
307 $hint_dataroot = get_string('pathsunsecuredataroot', 'install');
308 $config->stage = INSTALL_PATHS;
310 } else if (!is_writable($CFG->dataroot)) {
311 $hint_dataroot = get_string('pathsrodataroot', 'install');
312 $config->stage = INSTALL_PATHS;
315 if ($config->dirroot === '' or !file_exists($config->dirroot)) {
316 $hint_dirroot = get_string('pathswrongdirroot', 'install');
317 $config->stage = INSTALL_PATHS;
320 if ($config->admin === '' or !file_exists($config->dirroot.'/'.$config->admin.'/environment.xml')) {
321 $hint_admindir = get_string('pathswrongadmindir', 'install');
322 $config->stage = INSTALL_PATHS;
328 if ($config->stage == INSTALL_DOWNLOADLANG) {
329 if ($CFG->lang == 'en_utf8') {
330 $config->stage = INSTALL_DATABASETYPE;
336 if ($config->stage == INSTALL_DOWNLOADLANG) {
339 /// Create necessary lang dir
340 if (!make_upload_directory('lang', false)) {
341 $downloaderror = get_string('cannotcreatelangdir', 'error');
343 /// Download and install lang component
344 } else if ($cd = new component_installer('http://download.moodle.org', 'lang16', $CFG->lang.'.zip', 'languages.md5', 'lang')) {
345 if ($cd->install() == COMPONENT_ERROR) {
346 if ($cd->get_error() == 'remotedownloaderror') {
348 $a->url = 'http://download.moodle.org/lang16/'.$INSTALL['language'].'.zip';
349 $a->dest = $CFG->dataroot.'/lang';
350 $downloaderror = get_string($cd->get_error(), 'error', $a);
352 $downloaderror = get_string($cd->get_error(), 'error');
357 if ($downloaderror !== '') {
358 install_print_header($config, get_string('language'), get_string('langdownloaderror', 'install', $CFG->lang), $downloaderror);
359 install_print_footer($config);
362 $config->stage = INSTALL_DATABASETYPE;
368 if ($config->stage == INSTALL_DATABASETYPE) {
369 /// Finally ask for DB type
370 install_print_header($config, get_string('database', 'install'),
371 get_string('databasetypehead', 'install'),
372 get_string('databasetypesub', 'install'));
374 // TODO: move this PHP5 code to lib/installib.php so that this file parses in PHP4
375 $databases = array('mysqli' => moodle_database::get_driver_instance('mysqli', 'native'),
376 'pgsql' => moodle_database::get_driver_instance('pgsql', 'native'),
377 'oci' => moodle_database::get_driver_instance('oci', 'native'),
378 //'sqlsrv' => moodle_database::get_driver_instance('sqlsrv', 'native'), // new MS sql driver - win32 only
379 //'mssql' => moodle_database::get_driver_instance('mssql', 'native'), // FreeTDS driver
382 echo '<div class="userinput">';
383 echo '<div class="formrow"><label class="formlabel" for="dbtype">'.get_string('dbtype', 'install').'</label>';
384 echo '<select id="dbtype" name="dbtype" class="forminput">';
387 foreach ($databases as $type=>$database) {
388 if ($database->driver_installed() !== true) {
389 $disabled[$type] = $database;
392 echo '<option value="'.s($type).'">'.$database->get_name().'</option>';
395 echo '<optgroup label="'.s(get_string('notavailable')).'">';
396 foreach ($disabled as $type=>$database) {
397 echo '<option value="'.s($type).'" class="notavailable">'.$database->get_name().'</option>';
401 echo '</select></div>';
404 install_print_footer($config);
410 if ($config->stage == INSTALL_ENVIRONMENT or $config->stage == INSTALL_PATHS) {
411 $version_fail = (version_compare(phpversion(), "5.2.8") < 0);
412 $curl_fail = ($lang !== 'en_utf8' and !extension_loaded('curl')); // needed for lang pack download
413 $zip_fail = ($lang !== 'en_utf8' and !extension_loaded('zip')); // needed for lang pack download
415 if ($version_fail or $curl_fail or $zip_fail) {
416 $config->stage = INSTALL_ENVIRONMENT;
418 install_print_header($config, get_string('environment', 'install'),
419 get_string('errorsinenvironment', 'install'),
420 get_string('environmentsub2', 'install'));
422 echo '<div id="envresult"><dl>';
424 $a = (object)array('needed'=>'5.2.8', 'current'=>phpversion());
425 echo '<dt>'.get_string('phpversion', 'install').'</dt><dd>'.get_string('environmentrequireversion', 'admin', $a).'</dd>';
428 echo '<dt>'.get_string('phpextension', 'install', 'cURL').'</dt><dd>'.get_string('environmentrequireinstall', 'admin').'</dd>';
431 echo '<dt>'.get_string('phpextension', 'install', 'Zip').'</dt><dd>'.get_string('environmentrequireinstall', 'admin').'</dd>';
435 install_print_footer($config, true);
439 $config->stage = INSTALL_PATHS;
445 if ($config->stage == INSTALL_DISTRIBUTION) {
446 // TODO: reimplement welcome.html support for win installer
447 $config->stage = INSTALL_PATHS;
452 if ($config->stage == INSTALL_PATHS) {
453 $paths = array('wwwroot' => get_string('wwwroot', 'install'),
454 'dirroot' => get_string('dirroot', 'install'),
455 'dataroot' => get_string('dataroot', 'install'),
456 'admindir' => get_string('admindirname', 'install'));
459 foreach ($paths as $path=>$name) {
460 $sub .= '<dt>'.$name.'</dt><dd>'.get_string('pathssub'.$path, 'install').'</dd>';
464 install_print_header($config, get_string('paths', 'install'), get_string('pathshead', 'install'), $sub);
466 $strwwwroot = get_string('wwwroot', 'install');
467 $strdirroot = get_string('dirroot', 'install');
468 $strdataroot = get_string('dataroot', 'install');
469 $stradmindirname = get_string('admindirname', 'install');
471 echo '<div class="userinput">';
472 echo '<div class="formrow"><label for="id_wwwroot" class="formlabel">'.$paths['wwwroot'].'</label>';
473 echo '<input id="id_wwwroot" name="wwwroot" type="text" value="'.s($CFG->wwwroot).'" disabled="disabled" size="45" class="forminput" />';
476 echo '<div class="formrow"><label for="id_dirroot" class="formlabel">'.$paths['dirroot'].'</label>';
477 echo '<input id="id_dirroot" name="dirroot" type="text" value="'.s($config->dirroot).'" size="45"class="forminput" />';
478 if ($hint_dirroot !== '') {
479 echo '<div class="hint">'.$hint_dirroot.'</div>';
483 echo '<div class="formrow"><label for="id_dataroot" class="formlabel">'.$paths['dataroot'].'</label>';
484 echo '<input id="id_dataroot" name="dataroot" type="text" value="'.s($config->dataroot).'" size="45" class="forminput" />';
485 if ($hint_dataroot !== '') {
486 echo '<div class="hint">'.$hint_dataroot.'</div>';
491 if (file_exists("$CFG->dirroot/admin/environment.xml")) {
492 $disabled = 'disabled="disabled"';
496 echo '<div class="formrow"><label for="id_admin" class="formlabel">'.$paths['admindir'].'</label>';
497 echo '<input id="id_admin" name="admin" type="text" value="'.s($config->admin).'" '.$disabled.' size="10" class="forminput" />';
498 if ($hint_admindir !== '') {
499 echo '<div class="hint">'.$hint_admindir.'</div>';
505 install_print_footer($config);
511 $config->stage = INSTALL_WELCOME;
512 install_print_header($config, get_string('language'),
513 get_string('chooselanguagehead', 'install'),
514 get_string('chooselanguagesub', 'install'));
516 $languages = install_get_list_of_languages();
517 echo '<div class="userinput">';
518 echo '<div class="formrow"><label class="formlabel" for="langselect">'.get_string('language').'</label>';
519 echo '<select id="langselect" name="lang" class="forminput" onchange="this.form.submit()">';
520 foreach ($languages as $name=>$value) {
521 $selected = ($name == $CFG->lang) ? 'selected="selected"' : '';
522 echo '<option value="'.s($name).'" '.$selected.'>'.$value.'</option>';
524 echo '</select></div>';
527 install_print_footer($config);