Merge branch 'm19_MDL-26672_aiccfix' of git://github.com/danmarsden/moodle into MOODL...
[moodle.git] / install.php
blobbe3c28c6f5e086fc867a18e88eac66dd127e4328
1 <?php /// $Id$
2 /// install.php - helps admin user to create a config.php file
4 /// If config.php exists already then we are not needed.
6 if (file_exists('./config.php')) {
7 header('Location: index.php');
8 die;
9 } else {
10 $configfile = './config.php';
13 ///==========================================================================//
14 /// We are doing this in stages
15 define ('WELCOME', 0); /// 0. Welcome and language settings
16 define ('COMPATIBILITY', 1); /// 1. Compatibility
17 define ('DIRECTORY', 2); /// 2. Directory settings
18 define ('DATABASE', 3); /// 2. Database settings
19 define ('ADMIN', 4); /// 4. Administration directory name
20 define ('ENVIRONMENT', 5); /// 5. Administration directory name
21 define ('DOWNLOADLANG', 6); /// 6. Load complete lang from download.moodle.org
22 define ('SAVE', 7); /// 7. Save or display the settings
23 define ('REDIRECT', 8); /// 8. Redirect to index.php
24 ///==========================================================================//
27 /// This has to be defined to avoid a notice in current_language()
28 define('SITEID', 0);
30 /// Begin the session as we are holding all information in a session
31 /// variable until the end.
33 session_name('MoodleSession');
34 @session_start();
36 /// make sure PHP errors are displayed to help diagnose problems
37 @error_reporting(1023); //E_ALL not used because we do not want strict notices in PHP5 yet
38 @ini_set('display_errors', '1');
40 if (! isset($_SESSION['INSTALL'])) {
41 $_SESSION['INSTALL'] = array();
44 $INSTALL = &$_SESSION['INSTALL']; // Makes it easier to reference
46 /// detect if install was attempted from diferent directory, if yes reset session to prevent errors,
47 /// dirroot location now fixed in installer
48 if (!empty($INSTALL['dirroot']) and $INSTALL['dirroot'] != dirname(__FILE__)) {
49 $_SESSION['INSTALL'] = array();
52 /// If it's our first time through this script then we need to set some default values
54 if ( empty($INSTALL['language']) and empty($_POST['language']) ) {
56 /// set defaults
57 $INSTALL['language'] = 'en_utf8';
59 $INSTALL['dbhost'] = 'localhost';
60 $INSTALL['dbuser'] = '';
61 $INSTALL['dbpass'] = '';
62 $INSTALL['dbtype'] = 'mysql';
63 $INSTALL['dbname'] = 'moodle';
64 $INSTALL['prefix'] = 'mdl_';
66 $INSTALL['downloadlangpack'] = false;
67 $INSTALL['showdownloadlangpack'] = true;
68 $INSTALL['downloadlangpackerror'] = '';
70 /// To be used by the Installer
71 $INSTALL['wwwroot'] = '';
72 $INSTALL['dirroot'] = dirname(__FILE__);
73 $INSTALL['dataroot'] = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'moodledata';
75 /// To be configured in the Installer
76 $INSTALL['wwwrootform'] = '';
77 $INSTALL['dirrootform'] = dirname(__FILE__);
79 $INSTALL['admindirname'] = 'admin';
81 $INSTALL['stage'] = WELCOME;
84 //==========================================================================//
86 /// Set the page to Unicode always
88 header('Content-Type: text/html; charset=UTF-8');
90 /// Was data submitted?
92 if (isset($_POST['stage'])) {
94 /// Get the stage for which the form was set and the next stage we are going to
96 $gpc = ini_get('magic_quotes_gpc');
97 $gpc = ($gpc == '1' or strtolower($gpc) == 'on');
99 /// Store any posted data
100 foreach ($_POST as $setting=>$value) {
101 if ($gpc) {
102 $value = stripslashes($value);
105 $INSTALL[$setting] = $value;
108 if ( $goforward = (! empty( $_POST['next'] )) ) {
109 $nextstage = $_POST['stage'] + 1;
110 } else if (! empty( $_POST['prev'])) {
111 $nextstage = $_POST['stage'] - 1;
112 $INSTALL['stage'] = $_POST['stage'] - 1;
113 } else if (! empty( $_POST['same'] )) {
114 $nextstage = $_POST['stage'];
117 $nextstage = (int)$nextstage;
119 if ($nextstage < 0) {
120 $nextstage = WELCOME;
124 } else {
126 $goforward = true;
127 $nextstage = WELCOME;
131 //==========================================================================//
133 /// Fake some settings so that we can use selected functions from moodlelib.php and weblib.php
135 $SESSION->lang = (!empty($_POST['language'])) ? $_POST['language'] : $INSTALL['language'];
136 $CFG->dirroot = $INSTALL['dirroot'];
137 $CFG->libdir = $INSTALL['dirroot'].'/lib';
138 $CFG->dataroot = $INSTALL['dataroot'];
139 $CFG->admin = $INSTALL['admindirname'];
140 $CFG->directorypermissions = 00777;
141 $CFG->running_installer = true;
142 $CFG->docroot = 'http://docs.moodle.org';
143 $CFG->httpswwwroot = $INSTALL['wwwrootform']; // Needed by doc_link() in Server Checks page.
144 $COURSE->id = 0;
146 /// Include some moodle libraries
148 require_once($CFG->libdir.'/adminlib.php');
149 require_once($CFG->libdir.'/setuplib.php');
150 require_once($CFG->libdir.'/moodlelib.php');
151 require_once($CFG->libdir.'/weblib.php');
152 require_once($CFG->libdir.'/deprecatedlib.php');
153 require_once($CFG->libdir.'/adodb/adodb.inc.php');
154 require_once($CFG->libdir.'/environmentlib.php');
155 require_once($CFG->libdir.'/xmlize.php');
156 require_once($CFG->libdir.'/componentlib.class.php');
157 require_once($CFG->dirroot.'/version.php');
159 /// Set version and release
160 $INSTALL['version'] = $version;
161 $INSTALL['release'] = $release;
163 /// Have the $db object ready because we are going to use it often
164 define ('ADODB_ASSOC_CASE', 0); //Use lowercase fieldnames for ADODB_FETCH_ASSOC
165 $db = &ADONewConnection($INSTALL['dbtype']);
166 $db->SetFetchMode(ADODB_FETCH_ASSOC);
168 /// guess the www root
169 if ($INSTALL['wwwroot'] == '') {
170 list($INSTALL['wwwroot'], $xtra) = explode('/install.php', qualified_me());
171 $INSTALL['wwwrootform'] = $INSTALL['wwwroot'];
173 // now try to guess the correct dataroot not accessible via web
174 $CFG->wwwroot = $INSTALL['wwwroot'];
175 $i = 0; //safety check - dirname might return some unexpected results
176 while(is_dataroot_insecure()) {
177 $parrent = dirname($CFG->dataroot);
178 $i++;
179 if ($parrent == '/' or $parrent == '.' or preg_match('/^[a-z]:\\\?$/i', $parrent) or ($i > 100)) {
180 $CFG->dataroot = ''; //can not find secure location for dataroot
181 break;
183 $CFG->dataroot = dirname($parrent).'/moodledata';
185 $INSTALL['dataroot'] = $CFG->dataroot;
188 $headstagetext = array(WELCOME => get_string('chooselanguagehead', 'install'),
189 COMPATIBILITY => get_string('compatibilitysettingshead', 'install'),
190 DIRECTORY => get_string('directorysettingshead', 'install'),
191 DATABASE => get_string('databasesettingshead', 'install'),
192 ADMIN => get_string('admindirsettinghead', 'install'),
193 ENVIRONMENT => get_string('environmenthead', 'install'),
194 DOWNLOADLANG => get_string('downloadlanguagehead', 'install'),
195 SAVE => get_string('configurationcompletehead', 'install')
198 $substagetext = array(WELCOME => get_string('chooselanguagesub', 'install'),
199 COMPATIBILITY => get_string('compatibilitysettingssub', 'install'),
200 DIRECTORY => get_string('directorysettingssub', 'install'),
201 DATABASE => get_string('databasesettingssub', 'install'),
202 ADMIN => get_string('admindirsettingsub', 'install'),
203 ENVIRONMENT => get_string('environmentsub', 'install'),
204 DOWNLOADLANG => get_string('downloadlanguagesub', 'install'),
205 SAVE => get_string('configurationcompletesub', 'install')
210 //==========================================================================//
212 /// Are we in help mode?
214 if (isset($_GET['help'])) {
215 $nextstage = -1;
220 //==========================================================================//
222 /// Are we in config download mode?
224 if (isset($_GET['download'])) {
225 header("Content-Type: application/x-forcedownload\n");
226 header("Content-Disposition: attachment; filename=\"config.php\"");
227 echo $INSTALL['config'];
228 exit;
235 //==========================================================================//
237 /// Check the directory settings
239 if ($INSTALL['stage'] == DIRECTORY) {
241 error_reporting(0);
243 /// check wwwroot
244 if (ini_get('allow_url_fopen') && false) { /// This was not reliable
245 if (($fh = @fopen($INSTALL['wwwrootform'].'/install.php', 'r')) === false) {
246 $errormsg .= get_string('wwwrooterror', 'install').'<br />';
247 $INSTALL['wwwrootform'] = $INSTALL['wwwroot'];
250 if ($fh) fclose($fh);
252 /// check dirroot
253 if (($fh = @fopen($INSTALL['dirrootform'].'/install.php', 'r')) === false ) {
254 $errormsg .= get_string('dirrooterror', 'install').'<br />';
255 $INSTALL['dirrootform'] = $INSTALL['dirroot'];
257 if ($fh) fclose($fh);
259 /// check dataroot
260 $CFG->dataroot = $INSTALL['dataroot'];
261 $CFG->wwwroot = $INSTALL['wwwroot'];
262 if (make_upload_directory('sessions', false) === false ) {
263 $errormsg .= get_string('datarooterror', 'install').'<br />';
265 } else if (is_dataroot_insecure(true) == INSECURE_DATAROOT_ERROR) {
266 $errormsg .= get_string('datarootpublicerror', 'install').'<br />';
269 if (!empty($errormsg)) $nextstage = DIRECTORY;
271 error_reporting(7);
276 //==========================================================================//
278 /// Check database settings if stage 3 data submitted
279 /// Try to connect to the database. If that fails then try to create the database
281 if ($INSTALL['stage'] == DATABASE) {
283 /// different format for postgres7 by socket
284 if ($INSTALL['dbtype'] == 'postgres7' and ($INSTALL['dbhost'] == 'localhost' || $INSTALL['dbhost'] == '127.0.0.1')) {
285 $INSTALL['dbhost'] = "user='{$INSTALL['dbuser']}' password='{$INSTALL['dbpass']}' dbname='{$INSTALL['dbname']}'";
286 $INSTALL['dbuser'] = '';
287 $INSTALL['dbpass'] = '';
288 $INSTALL['dbname'] = '';
290 if ($INSTALL['prefix'] == '') { /// must have a prefix
291 $INSTALL['prefix'] = 'mdl_';
295 if ($INSTALL['dbtype'] == 'mysql') { /// Check MySQL extension is present
296 if (!extension_loaded('mysql')) {
297 $errormsg = get_string('mysqlextensionisnotpresentinphp', 'install');
298 $nextstage = DATABASE;
302 if ($INSTALL['dbtype'] == 'mysqli') { /// Check MySQLi extension is present
303 if (!extension_loaded('mysqli')) {
304 $errormsg = get_string('mysqliextensionisnotpresentinphp', 'install');
305 $nextstage = DATABASE;
309 if ($INSTALL['dbtype'] == 'postgres7') { /// Check PostgreSQL extension is present
310 if (!extension_loaded('pgsql')) {
311 $errormsg = get_string('pgsqlextensionisnotpresentinphp', 'install');
312 $nextstage = DATABASE;
316 if ($INSTALL['dbtype'] == 'mssql') { /// Check MSSQL extension is present
317 if (!function_exists('mssql_connect')) {
318 $errormsg = get_string('mssqlextensionisnotpresentinphp', 'install');
319 $nextstage = DATABASE;
323 if ($INSTALL['dbtype'] == 'mssql_n') { /// Check MSSQL extension is present
324 if (!function_exists('mssql_connect')) {
325 $errormsg = get_string('mssqlextensionisnotpresentinphp', 'install');
326 $nextstage = DATABASE;
330 if ($INSTALL['dbtype'] == 'odbc_mssql') { /// Check ODBC extension is present
331 if (!extension_loaded('odbc')) {
332 $errormsg = get_string('odbcextensionisnotpresentinphp', 'install');
333 $nextstage = DATABASE;
337 if ($INSTALL['dbtype'] == 'oci8po') { /// Check OCI extension is present
338 if (!extension_loaded('oci8')) {
339 $errormsg = get_string('ociextensionisnotpresentinphp', 'install');
340 $nextstage = DATABASE;
344 if (empty($INSTALL['prefix']) && $INSTALL['dbtype'] != 'mysql' && $INSTALL['dbtype'] != 'mysqli') { // All DBs but MySQL require prefix (reserv. words)
345 $errormsg = get_string('dbwrongprefix', 'install');
346 $nextstage = DATABASE;
349 if ($INSTALL['dbtype'] == 'oci8po' && strlen($INSTALL['prefix']) > 2) { // Oracle max prefix = 2cc (30cc limit)
350 $errormsg = get_string('dbwrongprefix', 'install');
351 $nextstage = DATABASE;
354 if ($INSTALL['dbtype'] == 'oci8po' && !empty($INSTALL['dbhost'])) { // Oracle host must be blank (tnsnames.ora has it)
355 $errormsg = get_string('dbwronghostserver', 'install');
356 $nextstage = DATABASE;
359 if (empty($errormsg)) {
361 error_reporting(0); // Hide errors
363 if (! $dbconnected = $db->Connect($INSTALL['dbhost'],$INSTALL['dbuser'],$INSTALL['dbpass'],$INSTALL['dbname'])) {
364 $db->database = ''; // reset database name cached by ADODB. Trick from MDL-9609
365 if ($dbconnected = $db->Connect($INSTALL['dbhost'],$INSTALL['dbuser'],$INSTALL['dbpass'])) { /// Try to connect without DB
366 switch ($INSTALL['dbtype']) { /// Try to create a database
367 case 'mysql':
368 case 'mysqli':
369 if ($db->Execute("CREATE DATABASE {$INSTALL['dbname']} DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;")) {
370 $dbconnected = $db->Connect($INSTALL['dbhost'],$INSTALL['dbuser'],$INSTALL['dbpass'],$INSTALL['dbname']);
371 } else {
372 $errormsg = get_string('dbcreationerror', 'install');
373 $nextstage = DATABASE;
375 break;
378 } else {
379 /// We have been able to connect properly, just test the database encoding now.
380 /// It must be Unicode for 1.8 installations.
381 $encoding = '';
382 switch ($INSTALL['dbtype']) {
383 case 'mysql':
384 case 'mysqli':
385 /// Get MySQL character_set_database value
386 $rs = $db->Execute("SHOW VARIABLES LIKE 'character_set_database'");
387 if ($rs && !$rs->EOF) {
388 $records = $rs->GetAssoc(true);
389 $encoding = $records['character_set_database']['Value'];
390 if (strtoupper($encoding) != 'UTF8') {
391 /// Try to set the encoding now!
392 if (! $db->Metatables()) { // We have no tables so go ahead
393 $db->Execute("ALTER DATABASE `".$INSTALL['dbname']."` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci");
394 $rs = $db->Execute("SHOW VARIABLES LIKE 'character_set_database'"); // this works
398 /// If conversion fails, skip, let environment testing do the job
400 break;
401 case 'postgres7':
402 /// Skip, let environment testing do the job
403 break;
404 case 'oci8po':
405 /// Skip, let environment testing do the job
406 break;
411 error_reporting(7);
413 if (($dbconnected === false) and (empty($errormsg)) ) {
414 $errormsg = get_string('dbconnectionerror', 'install');
415 $nextstage = DATABASE;
421 //==========================================================================//
423 /// If the next stage is admin directory settings OR we have just come from there then
424 /// check the admin directory.
425 /// If we can open a file then we know that the admin name is correct.
427 if ($nextstage == ADMIN or $INSTALL['stage'] == ADMIN) {
428 if (!ini_get('allow_url_fopen')) {
429 $nextstage = ($goforward) ? ENVIRONMENT : DATABASE;
430 } else if (($fh = @fopen($INSTALL['wwwrootform'].'/'.$INSTALL['admindirname'].'/environment.xml', 'r')) !== false) {
431 $nextstage = ($goforward) ? ENVIRONMENT : DATABASE;
432 fclose($fh);
433 } else {
434 $nextstage = ($goforward) ? ENVIRONMENT : DATABASE;
435 //if ($nextstage != ADMIN) {
436 // $errormsg = get_string('admindirerror', 'install');
437 // $nextstage = ADMIN;
438 // }
442 //==========================================================================//
444 // Check if we can navigate from the environemt page (because it's ok)
446 if ($INSTALL['stage'] == ENVIRONMENT) {
447 error_reporting(0); // Hide errors
448 $dbconnected = $db->Connect($INSTALL['dbhost'],$INSTALL['dbuser'],$INSTALL['dbpass'],$INSTALL['dbname']);
449 error_reporting(7); // Show errors
450 if ($dbconnected) {
451 /// Execute environment check, not printing results
452 @remove_dir($INSTALL['dataroot'] . '/environment'); /// Always delete downloaded env. info to force use of the released one. MDL-9796
453 if (!check_moodle_environment($INSTALL['release'], $environment_results, false)) {
454 $nextstage = ENVIRONMENT;
456 } else {
457 /// We never should reach this because DB has been tested before arriving here
458 $errormsg = get_string('dbconnectionerror', 'install');
459 $nextstage = DATABASE;
465 //==========================================================================//
467 // Try to download the lang pack if it has been selected
469 if ($INSTALL['stage'] == DOWNLOADLANG && $INSTALL['downloadlangpack']) {
471 $downloadsuccess = false;
472 $downloaderror = '';
474 error_reporting(0); // Hide errors
476 /// Create necessary lang dir
477 if (!make_upload_directory('lang', false)) {
478 $downloaderror = get_string('cannotcreatelangdir', 'error');
481 /// Download and install component
482 if (($cd = new component_installer('http://download.moodle.org', 'lang16',
483 $INSTALL['language'].'.zip', 'languages.md5', 'lang')) && empty($errormsg)) {
484 $status = $cd->install(); //returns COMPONENT_(ERROR | UPTODATE | INSTALLED)
485 switch ($status) {
486 case COMPONENT_ERROR:
487 if ($cd->get_error() == 'remotedownloaderror') {
488 $a = new stdClass();
489 $a->url = 'http://download.moodle.org/lang16/'.$INSTALL['language'].'.zip';
490 $a->dest= $CFG->dataroot.'/lang';
491 $downloaderror = get_string($cd->get_error(), 'error', $a);
492 } else {
493 $downloaderror = get_string($cd->get_error(), 'error');
495 break;
496 case COMPONENT_UPTODATE:
497 case COMPONENT_INSTALLED:
498 $downloadsuccess = true;
499 break;
500 default:
501 //We shouldn't reach this point
503 } else {
504 //We shouldn't reach this point
507 error_reporting(7); // Show errors
509 if ($downloadsuccess) {
510 $INSTALL['downloadlangpack'] = false;
511 $INSTALL['showdownloadlangpack'] = false;
512 $INSTALL['downloadlangpackerror'] = $downloaderror;
513 } else {
514 $INSTALL['downloadlangpack'] = false;
515 $INSTALL['showdownloadlangpack'] = false;
516 $INSTALL['downloadlangpackerror'] = $downloaderror;
522 //==========================================================================//
524 /// Display or print the data
525 /// Put the data into a string
526 /// Try to open config file for writing.
528 if ($nextstage == SAVE) {
530 $str = '<?php /// Moodle Configuration File '."\r\n";
531 $str .= "\r\n";
533 $str .= 'unset($CFG);'."\r\n";
534 $str .= "\r\n";
536 $str .= '$CFG = new stdClass();'."\r\n";
537 $str .= '$CFG->dbtype = \''.$INSTALL['dbtype']."';\r\n";
538 $str .= '$CFG->dbhost = \''.addslashes($INSTALL['dbhost'])."';\r\n";
539 if (!empty($INSTALL['dbname'])) {
540 $str .= '$CFG->dbname = \''.$INSTALL['dbname']."';\r\n";
541 // support single quotes in db user/passwords
542 $str .= '$CFG->dbuser = \''.addsingleslashes($INSTALL['dbuser'])."';\r\n";
543 $str .= '$CFG->dbpass = \''.addsingleslashes($INSTALL['dbpass'])."';\r\n";
545 $str .= '$CFG->dbpersist = false;'."\r\n";
546 $str .= '$CFG->prefix = \''.$INSTALL['prefix']."';\r\n";
547 $str .= "\r\n";
549 $str .= '$CFG->wwwroot = \''.s($INSTALL['wwwrootform'],true)."';\r\n";
550 $str .= '$CFG->dirroot = \''.s($INSTALL['dirrootform'],true)."';\r\n";
551 $str .= '$CFG->dataroot = \''.s($INSTALL['dataroot'],true)."';\r\n";
552 $str .= '$CFG->admin = \''.s($INSTALL['admindirname'],true)."';\r\n";
553 $str .= "\r\n";
555 $str .= '$CFG->directorypermissions = 00777; // try 02777 on a server in Safe Mode'."\r\n";
556 $str .= "\r\n";
558 $str .= '$CFG->passwordsaltmain = \''.addsingleslashes(complex_random_string()).'\';'."\r\n";
559 $str .= "\r\n";
561 $str .= 'require_once("$CFG->dirroot/lib/setup.php");'."\r\n";
562 $str .= '// MAKE SURE WHEN YOU EDIT THIS FILE THAT THERE ARE NO SPACES, BLANK LINES,'."\r\n";
563 $str .= '// RETURNS, OR ANYTHING ELSE AFTER THE TWO CHARACTERS ON THE NEXT LINE.'."\r\n";
564 $str .= '?>';
566 umask(0137);
568 if (( $configsuccess = ($fh = @fopen($configfile, 'w')) ) !== false) {
569 fwrite($fh, $str);
570 fclose($fh);
574 $INSTALL['config'] = $str;
579 //==========================================================================//
582 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
583 <html dir="<?php echo (right_to_left() ? 'rtl' : 'ltr'); ?>">
584 <head>
585 <link rel="shortcut icon" href="theme/standard/favicon.ico" />
586 <title>Moodle Install</title>
587 <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
588 <?php css_styles() ?>
589 <?php database_js() ?>
591 </head>
593 <body>
596 <?php
597 if (isset($_GET['help'])) {
598 print_install_help($_GET['help']);
599 close_window_button();
600 } else {
604 <table class="main" cellpadding="3" cellspacing="0">
605 <tr>
606 <td class="td_mainlogo">
607 <p class="p_mainlogo"><img src="pix/moodlelogo-med.gif" width="240" height="60" alt="Moodle logo"/></p>
608 </td>
609 <td class="td_mainlogo" valign="bottom">
610 <p class="p_mainheader"><?php print_string('installation', 'install') ?></p>
611 </td>
612 </tr>
614 <tr>
615 <td class="td_mainheading" colspan="2">
616 <p class="p_mainheading"><?php echo $headstagetext[$nextstage] ?></p>
617 <?php /// Exceptionaly, depending of the DB selected, we show some different text
618 /// from the standard one to show better instructions for each DB
619 if ($nextstage == DATABASE) {
620 echo '<script type="text/javascript" defer="defer">window.onload=toggledbinfo;</script>';
621 echo '<div id="mysql">' . get_string('databasesettingssub_mysql', 'install');
622 echo '<p style="text-align: center">' . get_string('databasesettingswillbecreated', 'install') . '</p>';
623 echo '</div>';
625 echo '<div id="mysqli">' . get_string('databasesettingssub_mysqli', 'install');
626 echo '<p style="text-align: center">' . get_string('databasesettingswillbecreated', 'install') . '</p>';
627 echo '</div>';
629 echo '<div id="postgres7">' . get_string('databasesettingssub_postgres7', 'install');
630 echo '<p style="text-align: left">' . get_string('postgresqlwarning', 'install') . '</p>';
631 echo '</div>';
633 echo '<div id="mssql">' . get_string('databasesettingssub_mssql', 'install');
634 /// Link to mssql installation page
635 echo "<p style='text-align:right'><a href=\"javascript:void(0)\" ";
636 echo "onclick=\"return window.open('http://docs.moodle.org/en/Installing_MSSQL_for_PHP')\"";
637 echo ">";
638 echo '<img src="pix/docs.gif' . '" alt="Docs" class="iconhelp" />';
639 echo get_string('moodledocslink', 'install') . '</a></p>';
640 echo '</div>';
642 echo '<div id="mssql_n">' . get_string('databasesettingssub_mssql_n', 'install');
643 /// Link to mssql installation page
644 echo "<p style='text-align:right'><a href=\"javascript:void(0)\" ";
645 echo "onclick=\"return window.open('http://docs.moodle.org/en/Installing_MSSQL_for_PHP')\"";
646 echo ">";
647 echo '<img src="pix/docs.gif' . '" alt="Docs" />';
648 echo get_string('moodledocslink', 'install') . '</a></p>';
649 echo '</div>';
651 echo '<div id="odbc_mssql">'. get_string('databasesettingssub_odbc_mssql', 'install');
652 /// Link to mssql installation page
653 echo "<p style='text-align:right'><a href=\"javascript:void(0)\" ";
654 echo "onclick=\"return window.open('http://docs.moodle.org/en/Installing_MSSQL_for_PHP')\"";
655 echo ">";
656 echo '<img src="pix/docs.gif' . '" alt="Docs" class="iconhelp" />';
657 echo get_string('moodledocslink', 'install') . '</a></p>';
658 echo '</div>';
660 echo '<div id="oci8po">' . get_string('databasesettingssub_oci8po', 'install');
661 /// Link to oracle installation page
662 echo "<p style='text-align:right'><a href=\"javascript:void(0)\" ";
663 echo "onclick=\"return window.open('http://docs.moodle.org/en/Installing_Oracle_for_PHP')\"";
664 echo ">";
665 echo '<img src="pix/docs.gif' . '" alt="Docs" class="iconhelp" />';
666 echo get_string('moodledocslink', 'install') . '</a></p>';
667 echo '</div>';
668 } else {
669 if (!empty($substagetext[$nextstage])) {
670 echo '<p class="p_subheading">' . $substagetext[$nextstage] . '</p>';
674 </td>
675 </tr>
677 <tr>
678 <td class="td_main" colspan="2">
680 <?php
682 if (!empty($errormsg)) echo "<p class=\"errormsg\" style=\"text-align:center\">$errormsg</p>\n";
685 if ($nextstage == SAVE) {
686 $INSTALL['stage'] = WELCOME;
687 $options = array();
688 $options['lang'] = $INSTALL['language'];
689 if ($configsuccess) {
690 echo "<p class=\"p_install\">".get_string('configfilewritten', 'install')."</p>\n";
692 echo "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"100%\">\n";
693 echo "<tr>\n";
694 echo "<td>&nbsp;</td>\n";
695 echo "<td>&nbsp;</td>\n";
696 echo "<td align=\"right\">\n";
697 print_single_button("index.php", $options, get_string('continue'));
698 echo "</td>\n";
699 echo "</tr>\n";
700 echo "</table>\n";
702 } else {
703 echo "<p class=\"errormsg\">".get_string('configfilenotwritten', 'install')."</p>";
705 echo "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"100%\">\n";
706 echo "<tr>\n";
707 echo "<td>&nbsp;</td>\n";
708 echo "<td align=\"center\">\n";
709 $installoptions = array();
710 $installoptions['download'] = 1;
711 print_single_button("install.php", $installoptions, get_string('download', 'install'));
712 echo "</td>\n";
713 echo "<td align=\"right\">\n";
714 print_single_button("index.php", $options, get_string('continue'));
715 echo "</td>\n";
716 echo "</tr>\n";
717 echo "</table>\n";
719 echo "<hr />\n";
720 echo "<div style=\"text-align: ".fix_align_rtl("left")."\">\n";
721 echo "<pre>\n";
722 print_r(s($str));
723 echo "</pre>\n";
724 echo "</div>\n";
726 } else {
727 $formaction = (isset($_GET['configfile'])) ? "install.php?configfile=".$_GET['configfile'] : "install.php";
728 form_table($nextstage, $formaction);
733 </td>
734 </tr>
735 </table>
737 <?php
741 </body>
742 </html>
753 <?php
756 //==========================================================================//
758 function form_table($nextstage = WELCOME, $formaction = "install.php") {
759 global $INSTALL, $db;
761 $enablenext = true;
763 /// Print the standard form if we aren't in the DOWNLOADLANG page
764 /// because it has its own form.
765 if ($nextstage != DOWNLOADLANG) {
766 $needtoopenform = false;
768 <form id="installform" method="post" action="<?php echo $formaction ?>">
769 <div><input type="hidden" name="stage" value="<?php echo $nextstage ?>" /></div>
771 <?php
772 } else {
773 $needtoopenform = true;
776 <table class="install_table" cellspacing="3" cellpadding="3">
778 <?php
779 /// what we do depends on the stage we're at
780 switch ($nextstage) {
781 case WELCOME: /// Welcome and language settings
783 <tr>
784 <td class="td_left"><p class="p_install"><?php print_string('language') ?></p></td>
785 <td class="td_right">
786 <?php choose_from_menu (get_installer_list_of_languages(), 'language', $INSTALL['language'], '') ?>
787 </td>
788 </tr>
790 <?php
791 break;
792 case COMPATIBILITY: /// Compatibilty check
793 $compatsuccess = true;
795 /// Check that PHP is of a sufficient version
796 print_compatibility_row(inst_check_php_version(), get_string('phpversion', 'install'), get_string('phpversionerror', 'install'), 'phpversionhelp');
797 $enablenext = $enablenext && inst_check_php_version();
798 /// Check session auto start
799 print_compatibility_row(!ini_get_bool('session.auto_start'), get_string('sessionautostart', 'install'), get_string('sessionautostarterror', 'install'), 'sessionautostarthelp');
800 $enablenext = $enablenext && !ini_get_bool('session.auto_start');
801 /// Check magic quotes
802 print_compatibility_row(!ini_get_bool('magic_quotes_runtime'), get_string('magicquotesruntime', 'install'), get_string('magicquotesruntimeerror', 'install'), 'magicquotesruntimehelp');
803 $enablenext = $enablenext && !ini_get_bool('magic_quotes_runtime');
804 /// Check unsupported PHP configuration
805 print_compatibility_row(!ini_get_bool('register_globals'), get_string('globalsquotes', 'install'), get_string('globalswarning', 'install'));
806 $enablenext = $enablenext && !ini_get_bool('register_globals');
807 /// Check safe mode
808 print_compatibility_row(!ini_get_bool('safe_mode'), get_string('safemode', 'install'), get_string('safemodeerror', 'install'), 'safemodehelp', true);
809 /// Check file uploads
810 print_compatibility_row(ini_get_bool('file_uploads'), get_string('fileuploads', 'install'), get_string('fileuploadserror', 'install'), 'fileuploadshelp', true);
811 /// Check GD version
812 print_compatibility_row(check_gd_version(), get_string('gdversion', 'install'), get_string('gdversionerror', 'install'), 'gdversionhelp', true);
813 /// Check memory limit
814 print_compatibility_row(check_memory_limit(), get_string('memorylimit', 'install'), get_string('memorylimiterror', 'install'), 'memorylimithelp', true);
817 break;
818 case DIRECTORY: /// Directory settings
821 <tr>
822 <td class="td_left"><p class="p_install"><?php print_string('wwwroot', 'install') ?></p></td>
823 <td class="td_right">
824 <input type="text" size="40"name="wwwrootform" value="<?php p($INSTALL['wwwrootform'],true) ?>" />
825 </td>
826 </tr>
827 <tr>
828 <td class="td_left"><p class="p_install"><?php print_string('dirroot', 'install') ?></p></td>
829 <td class="td_right">
830 <input type="text" size="40" name="dirrootform" disabled="disabled" value="<?php p($INSTALL['dirrootform'],true) ?>" />
831 </td>
832 </tr>
833 <tr>
834 <td class="td_left"><p class="p_install"><?php print_string('dataroot', 'install') ?></p></td>
835 <td class="td_right">
836 <input type="text" size="40" name="dataroot" value="<?php p($INSTALL['dataroot'],true) ?>" />
837 </td>
838 </tr>
840 <?php
841 break;
842 case DATABASE: /// Database settings
845 <tr>
846 <td class="td_left"><p class="p_install"><?php print_string('dbtype', 'install') ?></p></td>
847 <td class="td_right">
848 <?php choose_from_menu (array('mysql' => get_string('mysql', 'install'),
849 'mysqli' => get_string('mysqli', 'install'),
850 'oci8po' => get_string('oci8po', 'install'),
851 'postgres7' => get_string('postgres7', 'install'),
852 'mssql' => get_string('mssql', 'install'),
853 'mssql_n' => get_string('mssql_n', 'install'),
854 'odbc_mssql' => get_string('odbc_mssql', 'install')),
855 'dbtype', $INSTALL['dbtype'], '', 'toggledbinfo();') ?>
856 </td>
857 </tr>
858 <tr>
859 <td class="td_left"><p class="p_install"><?php print_string('dbhost', 'install') ?></p></td>
860 <td class="td_right">
861 <input type="text" class="input_database" name="dbhost" value="<?php p($INSTALL['dbhost']) ?>" />
862 </td>
863 </tr>
864 <tr>
865 <td class="td_left"><p class="p_install"><?php print_string('database', 'install') ?></p></td>
866 <td class="td_right">
867 <input type="text" class="input_database" name="dbname" value="<?php p($INSTALL['dbname']) ?>" />
868 </td>
869 </tr>
870 <tr>
871 <td class="td_left"><p class="p_install"><?php print_string('user') ?></p></td>
872 <td class="td_right">
873 <input type="text" class="input_database" name="dbuser" value="<?php p($INSTALL['dbuser']) ?>" />
874 </td>
875 </tr>
876 <tr>
877 <td class="td_left"><p class="p_install"><?php print_string('password') ?></p></td>
878 <td class="td_right">
879 <input type="password" class="input_database" name="dbpass" value="<?php p($INSTALL['dbpass']) ?>" />
880 </td>
881 </tr>
882 <tr>
883 <td class="td_left"><p class="p_install"><?php print_string('dbprefix', 'install') ?></p></td>
884 <td class="td_right">
885 <input type="text" class="input_database" name="prefix" value="<?php p($INSTALL['prefix']) ?>" />
886 </td>
887 </tr>
889 <?php
890 break;
891 case ADMIN: /// Administration directory setting
894 <tr>
895 <td class="td_left"><p class="p_install"><?php print_string('admindirname', 'install') ?></p></td>
896 <td class="td_right">
897 <input type="text" size="40" name="admindirname" value="<?php p($INSTALL['admindirname']) ?>" />
898 </td>
899 </tr>
902 <?php
903 break;
904 case ENVIRONMENT: /// Environment checks
907 <tr>
908 <td colspan="2">
909 <?php
910 error_reporting(0); // Hide errors
911 $dbconnected = $db->Connect($INSTALL['dbhost'],$INSTALL['dbuser'],$INSTALL['dbpass'],$INSTALL['dbname']);
912 error_reporting(7); // Show errors
913 if ($dbconnected) {
914 /// Execute environment check, printing results
915 @remove_dir($INSTALL['dataroot'] . '/environment'); /// Always delete downloaded env. info to force use of the released one. MDL-9796
916 check_moodle_environment($INSTALL['release'], $environment_results, true);
917 } else {
918 /// We never should reach this because DB has been tested before arriving here
919 $errormsg = get_string('dbconnectionerror', 'install');
920 $nextstage = DATABASE;
921 echo '<p class="errormsg" style="text-align:center">'.get_string('dbconnectionerror', 'install').'</p>';
924 </td>
925 </tr>
927 <?php
928 break;
929 case DOWNLOADLANG: /// Download language from download.moodle.org
932 <tr>
933 <td colspan="2">
934 <?php
935 /// Get array of languages, we are going to use it
936 $languages=get_installer_list_of_languages();
937 /// Print the download form (button) if necessary
938 if ($INSTALL['showdownloadlangpack'] == true && substr($INSTALL['language'],0,2) != 'en') {
939 $options = array();
940 $options['downloadlangpack'] = true;
941 $options['stage'] = DOWNLOADLANG;
942 $options['same'] = true;
943 print_simple_box_start('center');
944 print_single_button('install.php', $options, get_string('downloadlanguagebutton','install', $languages[$INSTALL['language']]), 'post');
945 print_simple_box_end();
946 } else {
947 /// Show result info
948 /// English lang packs aren't downloaded
949 if (substr($INSTALL['language'],0,2) == 'en') {
950 print_simple_box(get_string('downloadlanguagenotneeded', 'install', $languages[$INSTALL['language']]), 'center', '80%');
951 } else {
952 if ($INSTALL['downloadlangpackerror']) {
953 echo "<p class=\"errormsg\" align=\"center\">".$INSTALL['downloadlangpackerror']."</p>\n";
954 print_simple_box(get_string('langdownloaderror', 'install', $languages[$INSTALL['language']]), 'center', '80%');
955 } else {
956 print_simple_box(get_string('langdownloadok', 'install', $languages[$INSTALL['language']]), 'center', '80%');
961 </td>
962 </tr>
964 <?php
965 break;
966 default:
970 <tr>
971 <td colspan="<?php echo ($nextstage == COMPATIBILITY) ? 3 : 2; ?>">
973 <?php
974 if ($needtoopenform) {
976 <form id="installform" method="post" action="<?php echo $formaction ?>">
977 <div><input type="hidden" name="stage" value="<?php echo $nextstage ?>" /></div>
978 <?php
981 $disabled = $enablenext ? '' : 'disabled="disabled"';
984 <?php echo ($nextstage < SAVE) ? "<div><input $disabled type=\"submit\" name=\"next\" value=\"".get_string('next')." &raquo;\" style=\"float: ".fix_align_rtl("right")."\"/></div>\n" : "&nbsp;\n" ?>
985 <?php echo ($nextstage > WELCOME) ? "<div><input type=\"submit\" name=\"prev\" value=\"&laquo; ".get_string('previous')."\" style=\"float: ".fix_align_rtl("left")."\"/></div>\n" : "&nbsp;\n" ?>
987 <?php
988 if ($needtoopenform) {
990 </form>
991 <?php
996 </td>
998 </tr>
1000 </table>
1001 <?php
1002 if (!$needtoopenform) {
1004 </form>
1005 <?php
1009 <?php
1014 //==========================================================================//
1016 function print_compatibility_row($success, $testtext, $errormessage, $helpfield='', $caution=false) {
1017 echo "<tr>\n";
1018 echo "<td class=\"td_left_nowrap\" valign=\"top\"><p class=\"p_install\">$testtext</p></td>\n";
1019 if ($success) {
1020 echo "<td valign=\"top\"><p class=\"p_pass\">".get_string('pass', 'install')."</p></td>\n";
1021 echo "<td valign=\"top\">&nbsp;</td>\n";
1022 } else {
1023 echo "<td valign=\"top\">";
1024 echo ($caution) ? "<p class=\"p_caution\">".get_string('caution', 'install') : "<p class=\"p_fail\">".get_string('fail', 'install');
1025 echo "</p></td>\n";
1026 echo "<td valign=\"top\">";
1027 echo "<p class=\"p_install\">$errormessage ";
1028 if ($helpfield !== '') {
1029 install_helpbutton("install.php?help=$helpfield");
1031 echo "</p></td>\n";
1033 echo "</tr>\n";
1034 return $success;
1038 //==========================================================================//
1040 function install_helpbutton($url, $title='') {
1041 if ($title == '') {
1042 $title = get_string('help');
1044 echo "<a href=\"javascript:void(0)\" ";
1045 echo "onclick=\"return window.open('$url','Help','menubar=0,location=0,scrollbars,resizable,width=500,height=400')\"";
1046 echo ">";
1047 echo "<img src=\"pix/help.gif\" class=\"iconhelp\" alt=\"$title\" title=\"$title\"/>";
1048 echo "</a>\n";
1053 //==========================================================================//
1055 function print_install_help($help) {
1056 switch ($help) {
1057 case 'phpversionhelp':
1058 print_string($help, 'install', phpversion());
1059 break;
1060 case 'memorylimithelp':
1061 print_string($help, 'install', get_memory_limit());
1062 break;
1063 default:
1064 print_string($help, 'install');
1069 //==========================================================================//
1071 function get_memory_limit() {
1072 if ($limit = ini_get('memory_limit')) {
1073 return $limit;
1074 } else {
1075 return get_cfg_var('memory_limit');
1079 //==========================================================================//
1081 function check_memory_limit() {
1083 /// if limit is already 40 or more then we don't care if we can change it or not
1084 if ((int)str_replace('M', '', get_memory_limit()) >= 40) {
1085 return true;
1088 /// Otherwise, see if we can change it ourselves
1089 raise_memory_limit('40M');
1090 return ((int)str_replace('M', '', get_memory_limit()) >= 40);
1093 //==========================================================================//
1095 function inst_check_php_version() {
1096 if (!check_php_version("4.3.0")) {
1097 return false;
1098 } else if (check_php_version("5.0.0")) {
1099 return check_php_version("5.1.0"); // 5.0.x is too buggy
1101 return true; // 4.3.x or 4.4.x is fine
1103 //==========================================================================//
1105 /* This function returns a list of languages and their full names. The
1106 * list of available languages is fetched from install/lang/xx/installer.php
1107 * and it's used exclusively by the installation process
1108 * @return array An associative array with contents in the form of LanguageCode => LanguageName
1110 function get_installer_list_of_languages() {
1112 global $CFG;
1114 $languages = array();
1116 /// Get raw list of lang directories
1117 $langdirs = get_list_of_plugins('install/lang');
1118 asort($langdirs);
1119 /// Get some info from each lang
1120 foreach ($langdirs as $lang) {
1121 if (file_exists($CFG->dirroot .'/install/lang/'. $lang .'/installer.php')) {
1122 include($CFG->dirroot .'/install/lang/'. $lang .'/installer.php');
1123 if (substr($lang, -5) == '_utf8') { //Remove the _utf8 suffix from the lang to show
1124 $shortlang = substr($lang, 0, -5);
1125 } else {
1126 $shortlang = $lang;
1128 if ($lang == 'en') { //Explain this is non-utf8 en
1129 $shortlang = 'non-utf8 en';
1131 if (!empty($string['thislanguage'])) {
1132 $languages[$lang] = $string['thislanguage'] .' ('. $shortlang .')';
1134 unset($string);
1137 /// Return array
1138 return $languages;
1141 //==========================================================================//
1143 function css_styles() {
1146 <style type="text/css">
1148 body { background-color: #ffeece; }
1149 p, li, td {
1150 font-family: helvetica, arial, sans-serif;
1151 font-size: 10pt;
1153 a { text-decoration: none; color: blue; }
1154 a img {
1155 border: none;
1157 .errormsg {
1158 color: red;
1159 font-weight: bold;
1161 blockquote {
1162 font-family: courier, monospace;
1163 font-size: 10pt;
1165 .input_database {
1166 width: 270px;
1168 .install_table {
1169 width: 500px;
1170 margin-left:auto;
1171 margin-right:auto;
1173 .td_left {
1174 text-align: <?php echo fix_align_rtl("right") ?>;
1175 font-weight: bold;
1177 .td_left_nowrap{
1178 text-align: <?php echo fix_align_rtl("right") ?>;
1179 font-weight: bold;
1180 white-space: nowrap;
1181 width: 160px;
1182 padding-left: 10px;
1184 .td_right {
1185 text-align: <?php echo fix_align_rtl("left") ?>;
1187 .main {
1188 width: 80%;
1189 border-width: 1px;
1190 border-style: solid;
1191 border-color: #ffc85f;
1192 margin-left:auto;
1193 margin-right:auto;
1194 -moz-border-radius-bottomleft: 15px;
1195 -moz-border-radius-bottomright: 15px;
1197 .td_mainheading {
1198 background-color: #fee6b9;
1199 padding-left: 10px;
1201 .td_main {
1202 text-align: center;
1204 .td_mainlogo {
1205 vertical-align: middle;
1207 .p_mainlogo {
1208 margin-top: 0px;
1209 margin-bottom: 0px;
1211 .p_mainheading {
1212 font-size: 11pt;
1213 margin-top: 16px;
1214 margin-bottom: 16px;
1216 .p_subheading {
1217 font-size: 10pt;
1218 padding-left: 10px;
1219 margin-top: 16px;
1220 margin-bottom: 16px;
1222 .p_mainheader{
1223 text-align: right;
1224 font-size: 20pt;
1225 font-weight: bold;
1226 margin-top: 0px;
1227 margin-bottom: 0px;
1229 .p_pass {
1230 color: green;
1231 font-weight: bold;
1232 margin-top: 0px;
1233 margin-bottom: 0px;
1235 .p_fail {
1236 color: red;
1237 font-weight: bold;
1238 margin-top: 0px;
1239 margin-bottom: 0px;
1241 .p_caution {
1242 color: #ff6600;
1243 font-weight: bold;
1244 margin-top: 0px;
1245 margin-bottom: 0px;
1247 .p_help {
1248 text-align: center;
1249 font-family: helvetica, arial, sans-serif;
1250 font-size: 14pt;
1251 font-weight: bold;
1252 color: #333333;
1253 margin-top: 0px;
1254 margin-bottom: 0px;
1256 /* This override the p tag for every p tag in this installation script,
1257 but not in lang\xxx\installer.php
1259 .p_install {
1260 margin-top: 0px;
1261 margin-bottom: 0px;
1263 .environmenttable {
1264 font-size: 10pt;
1265 border-color: #ffc85f;
1267 table.environmenttable .error {
1268 background-color : red;
1269 color : inherit;
1272 table.environmenttable .warn {
1273 background-color : yellow;
1276 table.environmenttable .ok {
1277 background-color : lightgreen;
1279 .header {
1280 background-color: #fee6b9;
1281 font-size: 10pt;
1283 .cell {
1284 background-color: #ffeece;
1285 font-size: 10pt;
1287 .error {
1288 color: #ff0000;
1290 .errorboxcontent {
1291 text-align: center;
1292 font-weight: bold;
1293 padding-left: 20px;
1294 color: #ff0000;
1296 .invisiblefieldset {
1297 display:inline;
1298 border:0px;
1299 padding:0px;
1300 margin:0px;
1302 #mysql, #mysqli, #postgres7, #mssql, #mssql_n, #odbc_mssql, #oci8po {
1303 display: none;
1306 </style>
1308 <?php
1311 //==========================================================================//
1313 function database_js() {
1316 <script type="text/javascript" defer="defer">
1317 function toggledbinfo() {
1318 //Calculate selected value
1319 var showid = 'mysql';
1320 if (document.getElementById('installform').dbtype.value) {
1321 showid = document.getElementById('installform').dbtype.value;
1323 if (document.getElementById) {
1324 //Hide all the divs
1325 document.getElementById('mysql').style.display = '';
1326 document.getElementById('mysqli').style.display = '';
1327 document.getElementById('postgres7').style.display = '';
1328 document.getElementById('mssql').style.display = '';
1329 document.getElementById('mssql_n').style.display = '';
1330 document.getElementById('odbc_mssql').style.display = '';
1331 document.getElementById('oci8po').style.display = '';
1332 //Show the selected div
1333 document.getElementById(showid).style.display = 'block';
1334 } else if (document.all) {
1335 //This is the way old msie versions work
1336 //Hide all the divs
1337 document.all['mysql'].style.display = '';
1338 document.all['mysqli'].style.display = '';
1339 document.all['postgres7'].style.display = '';
1340 document.all['mssql'].style.display = '';
1341 document.all['mssql_n'].style.display = '';
1342 document.all['odbc_mssql'].style.display = '';
1343 document.all['oci8po'].style.display = '';
1344 //Show the selected div
1345 document.all[showid].style.display = 'block';
1346 } else if (document.layers) {
1347 //This is the way nn4 works
1348 //Hide all the divs
1349 document.layers['mysql'].style.display = '';
1350 document.layers['mysqli'].style.display = '';
1351 document.layers['postgres7'].style.display = '';
1352 document.layers['mssql'].style.display = '';
1353 document.layers['mssql_n'].style.display = '';
1354 document.layers['odbc_mssql'].style.display = '';
1355 document.layers['oci8po'].style.display = '';
1356 //Show the selected div
1357 document.layers[showid].style.display = 'block';
1360 </script>
1362 <?php
1366 * Add slashes for single quotes and backslashes
1367 * so they can be included in single quoted string
1368 * (for config.php)
1370 function addsingleslashes($input){
1371 return preg_replace("/(['\\\])/", "\\\\$1", $input);