MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / install.php
blob76157e04734e730eeb3100f153902beeec50ee09
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 if (! isset($_SESSION['INSTALL'])) {
37 $_SESSION['INSTALL'] = array();
40 $INSTALL = &$_SESSION['INSTALL']; // Makes it easier to reference
42 /// detect if install was attempted from diferent directory, if yes reset session to prevent errors,
43 /// dirroot location now fixed in installer
44 if (!empty($INSTALL['dirroot']) and $INSTALL['dirroot'] != dirname(__FILE__)) {
45 $_SESSION['INSTALL'] = array();
48 /// If it's our first time through this script then we need to set some default values
50 if ( empty($INSTALL['language']) and empty($_POST['language']) ) {
52 /// set defaults
53 $INSTALL['language'] = 'en_utf8';
55 $INSTALL['dbhost'] = 'localhost';
56 $INSTALL['dbuser'] = '';
57 $INSTALL['dbpass'] = '';
58 $INSTALL['dbtype'] = 'mysql';
59 $INSTALL['dbname'] = 'moodle';
60 $INSTALL['prefix'] = 'mdl_';
62 $INSTALL['downloadlangpack'] = false;
63 $INSTALL['showdownloadlangpack'] = true;
64 $INSTALL['downloadlangpackerror'] = '';
66 /// To be used by the Installer
67 $INSTALL['wwwroot'] = '';
68 $INSTALL['dirroot'] = dirname(__FILE__);
69 $INSTALL['dataroot'] = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'moodledata';
71 /// To be configured in the Installer
72 $INSTALL['wwwrootform'] = '';
73 $INSTALL['dirrootform'] = dirname(__FILE__);
75 $INSTALL['admindirname'] = 'admin';
77 $INSTALL['stage'] = WELCOME;
80 //==========================================================================//
82 /// Set the page to Unicode always
84 header('Content-Type: text/html; charset=UTF-8');
86 /// Was data submitted?
88 if (isset($_POST['stage'])) {
90 /// Get the stage for which the form was set and the next stage we are going to
92 /// Store any posted data
93 foreach ($_POST as $setting=>$value) {
94 $INSTALL[$setting] = $value;
97 if ( $goforward = (! empty( $_POST['next'] )) ) {
98 $nextstage = $_POST['stage'] + 1;
99 } else if (! empty( $_POST['prev'])) {
100 $nextstage = $_POST['stage'] - 1;
101 $INSTALL['stage'] = $_POST['stage'] - 1;
102 } else if (! empty( $_POST['same'] )) {
103 $nextstage = $_POST['stage'];
107 if ($nextstage < 0) {
108 $nextstage = WELCOME;
112 } else {
114 $goforward = true;
115 $nextstage = WELCOME;
119 //==========================================================================//
121 /// Fake some settings so that we can use selected functions from moodlelib.php and weblib.php
123 $SESSION->lang = (!empty($_POST['language'])) ? $_POST['language'] : $INSTALL['language'];
124 $CFG->dirroot = $INSTALL['dirroot'];
125 $CFG->libdir = $INSTALL['dirroot'].'/lib';
126 $CFG->dataroot = $INSTALL['dataroot'];
127 $CFG->admin = $INSTALL['admindirname'];
128 $CFG->directorypermissions = 00777;
129 $CFG->running_installer = true;
130 $COURSE->id = 0;
132 /// Include some moodle libraries
134 require_once($CFG->libdir.'/adminlib.php');
135 require_once($CFG->libdir.'/setuplib.php');
136 require_once($CFG->libdir.'/moodlelib.php');
137 require_once($CFG->libdir.'/weblib.php');
138 require_once($CFG->libdir.'/deprecatedlib.php');
139 require_once($CFG->libdir.'/adodb/adodb.inc.php');
140 require_once($CFG->libdir.'/environmentlib.php');
141 require_once($CFG->libdir.'/xmlize.php');
142 require_once($CFG->libdir.'/componentlib.class.php');
143 require_once($CFG->dirroot.'/version.php');
145 /// Set version and release
146 $INSTALL['version'] = $version;
147 $INSTALL['release'] = $release;
149 /// Have the $db object ready because we are going to use it often
150 define ('ADODB_ASSOC_CASE', 0); //Use lowercase fieldnames for ADODB_FETCH_ASSOC
151 $db = &ADONewConnection($INSTALL['dbtype']);
152 $db->SetFetchMode(ADODB_FETCH_ASSOC);
154 /// guess the www root
155 if ($INSTALL['wwwroot'] == '') {
156 list($INSTALL['wwwroot'], $xtra) = explode('/install.php', qualified_me());
157 $INSTALL['wwwrootform'] = $INSTALL['wwwroot'];
159 // now try to guess the correct dataroot not accessible via web
160 $CFG->wwwroot = $INSTALL['wwwroot'];
161 $i = 0; //safety check - dirname might return some unexpected results
162 while(is_dataroot_insecure()) {
163 $parrent = dirname($CFG->dataroot);
164 $i++;
165 if ($parrent == '/' or $parrent == '.' or preg_match('/^[a-z]:\\\?$/i', $parrent) or ($i > 100)) {
166 $CFG->dataroot = ''; //can not find secure location for dataroot
167 break;
169 $CFG->dataroot = dirname($parrent).'/moodledata';
171 $INSTALL['dataroot'] = $CFG->dataroot;
174 $headstagetext = array(WELCOME => get_string('chooselanguagehead', 'install'),
175 COMPATIBILITY => get_string('compatibilitysettingshead', 'install'),
176 DIRECTORY => get_string('directorysettingshead', 'install'),
177 DATABASE => get_string('databasesettingshead', 'install'),
178 ADMIN => get_string('admindirsettinghead', 'install'),
179 ENVIRONMENT => get_string('environmenthead', 'install'),
180 DOWNLOADLANG => get_string('downloadlanguagehead', 'install'),
181 SAVE => get_string('configurationcompletehead', 'install')
184 $substagetext = array(WELCOME => get_string('chooselanguagesub', 'install'),
185 COMPATIBILITY => get_string('compatibilitysettingssub', 'install'),
186 DIRECTORY => get_string('directorysettingssub', 'install'),
187 DATABASE => get_string('databasesettingssub', 'install'),
188 ADMIN => get_string('admindirsettingsub', 'install'),
189 ENVIRONMENT => get_string('environmentsub', 'install'),
190 DOWNLOADLANG => get_string('downloadlanguagesub', 'install'),
191 SAVE => get_string('configurationcompletesub', 'install')
196 //==========================================================================//
198 /// Are we in help mode?
200 if (isset($_GET['help'])) {
201 $nextstage = -1;
206 //==========================================================================//
208 /// Are we in config download mode?
210 if (isset($_GET['download'])) {
211 header("Content-Type: application/x-forcedownload\n");
212 header("Content-Disposition: attachment; filename=\"config.php\"");
213 echo $INSTALL['config'];
214 exit;
221 //==========================================================================//
223 /// Check the directory settings
225 if ($INSTALL['stage'] == DIRECTORY) {
227 error_reporting(0);
229 /// check wwwroot
230 if (ini_get('allow_url_fopen') && false) { /// This was not reliable
231 if (($fh = @fopen($INSTALL['wwwrootform'].'/install.php', 'r')) === false) {
232 $errormsg .= get_string('wwwrooterror', 'install').'<br />';
233 $INSTALL['wwwrootform'] = $INSTALL['wwwroot'];
236 if ($fh) fclose($fh);
238 /// check dirroot
239 if (($fh = @fopen($INSTALL['dirrootform'].'/install.php', 'r')) === false ) {
240 $errormsg .= get_string('dirrooterror', 'install').'<br />';
241 $INSTALL['dirrootform'] = $INSTALL['dirroot'];
243 if ($fh) fclose($fh);
245 /// check dataroot
246 $CFG->dataroot = $INSTALL['dataroot'];
247 if (make_upload_directory('sessions', false) === false ) {
248 $errormsg .= get_string('datarooterror', 'install').'<br />';
250 if ($fh) fclose($fh);
252 if (!empty($errormsg)) $nextstage = DIRECTORY;
254 error_reporting(7);
259 //==========================================================================//
261 /// Check database settings if stage 3 data submitted
262 /// Try to connect to the database. If that fails then try to create the database
264 if ($INSTALL['stage'] == DATABASE) {
266 /// different format for postgres7 by socket
267 if ($INSTALL['dbtype'] == 'postgres7' and ($INSTALL['dbhost'] == 'localhost' || $INSTALL['dbhost'] == '127.0.0.1')) {
268 $INSTALL['dbhost'] = "user='{$INSTALL['dbuser']}' password='{$INSTALL['dbpass']}' dbname='{$INSTALL['dbname']}'";
269 $INSTALL['dbuser'] = '';
270 $INSTALL['dbpass'] = '';
271 $INSTALL['dbname'] = '';
273 if ($INSTALL['prefix'] == '') { /// must have a prefix
274 $INSTALL['prefix'] = 'mdl_';
278 if ($INSTALL['dbtype'] == 'mysql') { /// Check MySQL extension is present
279 if (!extension_loaded('mysql')) {
280 $errormsg = get_string('mysqlextensionisnotpresentinphp', 'install');
281 $nextstage = DATABASE;
285 if ($INSTALL['dbtype'] == 'mysqli') { /// Check MySQLi extension is present
286 if (!extension_loaded('mysqli')) {
287 $errormsg = get_string('mysqliextensionisnotpresentinphp', 'install');
288 $nextstage = DATABASE;
292 if ($INSTALL['dbtype'] == 'postgres7') { /// Check PostgreSQL extension is present
293 if (!extension_loaded('pgsql')) {
294 $errormsg = get_string('pgsqlextensionisnotpresentinphp', 'install');
295 $nextstage = DATABASE;
299 if ($INSTALL['dbtype'] == 'mssql') { /// Check MSSQL extension is present
300 if (!function_exists('mssql_connect')) {
301 $errormsg = get_string('mssqlextensionisnotpresentinphp', 'install');
302 $nextstage = DATABASE;
306 if ($INSTALL['dbtype'] == 'mssql_n') { /// Check MSSQL extension is present
307 if (!function_exists('mssql_connect')) {
308 $errormsg = get_string('mssqlextensionisnotpresentinphp', 'install');
309 $nextstage = DATABASE;
313 if ($INSTALL['dbtype'] == 'odbc_mssql') { /// Check ODBC extension is present
314 if (!extension_loaded('odbc')) {
315 $errormsg = get_string('odbcextensionisnotpresentinphp', 'install');
316 $nextstage = DATABASE;
320 if ($INSTALL['dbtype'] == 'oci8po') { /// Check OCI extension is present
321 if (!extension_loaded('oci8')) {
322 $errormsg = get_string('ociextensionisnotpresentinphp', 'install');
323 $nextstage = DATABASE;
327 if (empty($INSTALL['prefix']) && $INSTALL['dbtype'] != 'mysql' && $INSTALL['dbtype'] != 'mysqli') { // All DBs but MySQL require prefix (reserv. words)
328 $errormsg = get_string('dbwrongprefix', 'install');
329 $nextstage = DATABASE;
332 if ($INSTALL['dbtype'] == 'oci8po' && strlen($INSTALL['prefix']) > 2) { // Oracle max prefix = 2cc (30cc limit)
333 $errormsg = get_string('dbwrongprefix', 'install');
334 $nextstage = DATABASE;
337 if ($INSTALL['dbtype'] == 'oci8po' && !empty($INSTALL['dbhost'])) { // Oracle host must be blank (tnsnames.ora has it)
338 $errormsg = get_string('dbwronghostserver', 'install');
339 $nextstage = DATABASE;
342 if (empty($errormsg)) {
344 error_reporting(0); // Hide errors
346 if (! $dbconnected = $db->Connect($INSTALL['dbhost'],$INSTALL['dbuser'],$INSTALL['dbpass'],$INSTALL['dbname'])) {
347 /// The following doesn't seem to work but we're working on it
348 /// If you come up with a solution for creating a database in MySQL
349 /// feel free to put it in and let us know
350 if ($dbconnected = $db->Connect($INSTALL['dbhost'],$INSTALL['dbuser'],$INSTALL['dbpass'])) {
351 switch ($INSTALL['dbtype']) { /// Try to create a database
352 case 'mysql':
353 case 'mysqli':
354 if ($db->Execute("CREATE DATABASE {$INSTALL['dbname']};")) {
355 $dbconnected = $db->Connect($INSTALL['dbhost'],$INSTALL['dbuser'],$INSTALL['dbpass'],$INSTALL['dbname']);
356 } else {
357 $errormsg = get_string('dbcreationerror', 'install');
358 $nextstage = DATABASE;
360 break;
363 } else {
364 /// We have been able to connect properly, just test the database encoding now.
365 /// It must be Unicode for 1.8 installations.
366 $encoding = '';
367 switch ($INSTALL['dbtype']) {
368 case 'mysql':
369 case 'mysqli':
370 /// Get MySQL character_set_database value
371 $rs = $db->Execute("SHOW VARIABLES LIKE 'character_set_database'");
372 if ($rs && $rs->RecordCount() > 0) {
373 $records = $rs->GetAssoc(true);
374 $encoding = $records['character_set_database']['Value'];
375 if (strtoupper($encoding) != 'UTF8') {
376 /// Try to set the encoding now!
377 if (! $db->Metatables()) { // We have no tables so go ahead
378 $db->Execute("ALTER DATABASE `".$INSTALL['dbname']."` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci");
379 $rs = $db->Execute("SHOW VARIABLES LIKE 'character_set_database'"); // this works
383 /// If conversion fails, skip, let environment testing do the job
385 break;
386 case 'postgres7':
387 /// Skip, let environment testing do the job
388 break;
389 case 'oci8po':
390 /// Skip, let environment testing do the job
391 break;
396 error_reporting(7);
398 if (($dbconnected === false) and (empty($errormsg)) ) {
399 $errormsg = get_string('dbconnectionerror', 'install');
400 $nextstage = DATABASE;
406 //==========================================================================//
408 /// If the next stage is admin directory settings OR we have just come from there then
409 /// check the admin directory.
410 /// If we can open a file then we know that the admin name is correct.
412 if ($nextstage == ADMIN or $INSTALL['stage'] == ADMIN) {
413 if (!ini_get('allow_url_fopen')) {
414 $nextstage = ($goforward) ? ENVIRONMENT : DATABASE;
415 } else if (($fh = @fopen($INSTALL['wwwrootform'].'/'.$INSTALL['admindirname'].'/environment.xml', 'r')) !== false) {
416 $nextstage = ($goforward) ? ENVIRONMENT : DATABASE;
417 fclose($fh);
418 } else {
419 $nextstage = ($goforward) ? ENVIRONMENT : DATABASE;
420 //if ($nextstage != ADMIN) {
421 // $errormsg = get_string('admindirerror', 'install');
422 // $nextstage = ADMIN;
423 // }
427 //==========================================================================//
429 // Check if we can navigate from the environemt page (because it's ok)
431 if ($INSTALL['stage'] == ENVIRONMENT) {
432 error_reporting(0); // Hide errors
433 $dbconnected = $db->Connect($INSTALL['dbhost'],$INSTALL['dbuser'],$INSTALL['dbpass'],$INSTALL['dbname']);
434 error_reporting(7); // Show errors
435 if ($dbconnected) {
436 /// Execute environment check, printing results
437 if (!check_moodle_environment($INSTALL['release'], $environment_results, false)) {
438 $nextstage = ENVIRONMENT;
440 } else {
441 /// We never should reach this because DB has been tested before arriving here
442 $errormsg = get_string('dbconnectionerror', 'install');
443 $nextstage = DATABASE;
449 //==========================================================================//
451 // Try to download the lang pack if it has been selected
453 if ($INSTALL['stage'] == DOWNLOADLANG && $INSTALL['downloadlangpack']) {
455 $downloadsuccess = false;
456 $downloaderror = '';
458 error_reporting(0); // Hide errors
460 /// Create necessary lang dir
461 if (!make_upload_directory('lang', false)) {
462 $downloaderror = get_string('cannotcreatelangdir', 'error');
465 /// Download and install component
466 if (($cd = new component_installer('http://download.moodle.org', 'lang16',
467 $INSTALL['language'].'.zip', 'languages.md5', 'lang')) && empty($errormsg)) {
468 $status = $cd->install(); //returns ERROR | UPTODATE | INSTALLED
469 switch ($status) {
470 case ERROR:
471 if ($cd->get_error() == 'remotedownloadnotallowed') {
472 $a = new stdClass();
473 $a->url = 'http://download.moodle.org/lang16/'.$pack.'.zip';
474 $a->dest= $CFG->dataroot.'/lang';
475 $downloaderror = get_string($cd->get_error(), 'error', $a);
476 } else {
477 $downloaderror = get_string($cd->get_error(), 'error');
479 break;
480 case UPTODATE:
481 case INSTALLED:
482 $downloadsuccess = true;
483 break;
484 default:
485 //We shouldn't reach this point
487 } else {
488 //We shouldn't reach this point
491 error_reporting(7); // Show errors
493 if ($downloadsuccess) {
494 $INSTALL['downloadlangpack'] = false;
495 $INSTALL['showdownloadlangpack'] = false;
496 $INSTALL['downloadlangpackerror'] = $downloaderror;
497 } else {
498 $INSTALL['downloadlangpack'] = false;
499 $INSTALL['showdownloadlangpack'] = false;
500 $INSTALL['downloadlangpackerror'] = $downloaderror;
506 //==========================================================================//
508 /// Display or print the data
509 /// Put the data into a string
510 /// Try to open config file for writing.
512 if ($nextstage == SAVE) {
514 $str = '<?php /// Moodle Configuration File '."\r\n";
515 $str .= "\r\n";
517 $str .= 'unset($CFG);'."\r\n";
518 $str .= "\r\n";
520 $str .= '$CFG->dbtype = \''.$INSTALL['dbtype']."';\r\n";
521 $str .= '$CFG->dbhost = \''.addslashes($INSTALL['dbhost'])."';\r\n";
522 if (!empty($INSTALL['dbname'])) {
523 $str .= '$CFG->dbname = \''.$INSTALL['dbname']."';\r\n";
524 $str .= '$CFG->dbuser = \''.$INSTALL['dbuser']."';\r\n";
525 $str .= '$CFG->dbpass = \''.$INSTALL['dbpass']."';\r\n";
527 $str .= '$CFG->dbpersist = false;'."\r\n";
528 $str .= '$CFG->prefix = \''.$INSTALL['prefix']."';\r\n";
529 $str .= "\r\n";
531 $str .= '$CFG->wwwroot = \''.s($INSTALL['wwwrootform'],true)."';\r\n";
532 $str .= '$CFG->dirroot = \''.s($INSTALL['dirrootform'],true)."';\r\n";
533 $str .= '$CFG->dataroot = \''.s($INSTALL['dataroot'],true)."';\r\n";
534 $str .= '$CFG->admin = \''.s($INSTALL['admindirname'],true)."';\r\n";
535 $str .= "\r\n";
537 $str .= '$CFG->directorypermissions = 00777; // try 02777 on a server in Safe Mode'."\r\n";
538 $str .= "\r\n";
540 $str .= 'require_once("$CFG->dirroot/lib/setup.php");'."\r\n";
541 $str .= '// MAKE SURE WHEN YOU EDIT THIS FILE THAT THERE ARE NO SPACES, BLANK LINES,'."\r\n";
542 $str .= '// RETURNS, OR ANYTHING ELSE AFTER THE TWO CHARACTERS ON THE NEXT LINE.'."\r\n";
543 $str .= '?>';
545 umask(0137);
547 if (( $configsuccess = ($fh = @fopen($configfile, 'w')) ) !== false) {
548 fwrite($fh, $str);
549 fclose($fh);
553 $INSTALL['config'] = $str;
558 //==========================================================================//
561 <html dir="<?php echo (right_to_left() ? 'rtl' : 'ltr'); ?>">
562 <head>
563 <link rel="shortcut icon" href="theme/standard/favicon.ico" />
564 <title>Moodle Install</title>
565 <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
566 <?php css_styles() ?>
567 <?php database_js() ?>
569 </head>
571 <body>
574 <?php
575 if (isset($_GET['help'])) {
576 print_install_help($_GET['help']);
577 close_window_button();
578 } else {
582 <table class="main" align="center" cellpadding="3" cellspacing="0">
583 <tr>
584 <td class="td_mainlogo">
585 <p class="p_mainlogo"><img src="pix/moodlelogo-med.gif" width="240" height="60" alt="Moodle logo"></p>
586 </td>
587 <td class="td_mainlogo" valign="bottom">
588 <p class="p_mainheader"><?php print_string('installation', 'install') ?></p>
589 </td>
590 </tr>
592 <tr>
593 <td class="td_mainheading" colspan="2">
594 <p class="p_mainheading"><?php echo $headstagetext[$nextstage] ?></p>
595 <?php /// Exceptionaly, depending of the DB selected, we show some different text
596 /// from the standard one to show better instructions for each DB
597 if ($nextstage == DATABASE) {
598 echo '<script type="text/javascript" defer="defer">window.onload=toggledbinfo;</script>';
599 echo '<div id="mysql" name="mysql">' . get_string('databasesettingssub_mysql', 'install') . '</div>';
601 echo '<div id="mysqli" name="mysqli">' . get_string('databasesettingssub_mysqli', 'install') . '</div>';
603 echo '<div id="postgres7" name="postgres7">' . get_string('databasesettingssub_postgres7', 'install') . '</div>';
605 echo '<div id="mssql" name="mssql">' . get_string('databasesettingssub_mssql', 'install');
606 /// Link to mssql installation page
607 echo '<p align="right"><a href="http://docs.moodle.org/en/Installing_MSSQL_for_PHP" target="_blank">';
608 echo '<img src="pix/docs.gif' . '" alt="Docs" class="iconhelp" />';
609 echo get_string('moodledocslink', 'install') . '</a></p>';
610 echo '</div>';
612 echo '<div id="mssql_n" name="mssql">' . get_string('databasesettingssub_mssql_n', 'install');
613 /// Link to mssql installation page
614 echo '<p align="right"><a href="http://docs.moodle.org/en/Installing_MSSQL_for_PHP" target="_blank">';
615 echo '<img src="pix/docs.gif' . '" alt="Docs" />';
616 echo get_string('moodledocslink', 'install') . '</a></p>';
617 echo '</div>';
619 echo '<div id="odbc_mssql" name="odbc_mssql">'. get_string('databasesettingssub_odbc_mssql', 'install');
620 /// Link to mssql installation page
621 echo '<p align="right"><a href="http://docs.moodle.org/en/Installing_MSSQL_for_PHP" target="_blank">';
622 echo '<img src="pix/docs.gif' . '" alt="Docs" class="iconhelp" />';
623 echo get_string('moodledocslink', 'install') . '</a></p>';
624 echo '</div>';
626 echo '<div id="oci8po" name="oci8po">' . get_string('databasesettingssub_oci8po', 'install');
627 /// Link to oracle installation page
628 echo '<p align="right"><a href="http://docs.moodle.org/en/Installing_Oracle_for_PHP" target="_blank">';
629 echo '<img src="pix/docs.gif' . '" alt="Docs" class="iconhelp" />';
630 echo get_string('moodledocslink', 'install') . '</a></p>';
631 echo '</div>';
632 } else {
633 if (!empty($substagetext[$nextstage])) {
634 echo '<p class="p_subheading">' . $substagetext[$nextstage] . '</p>';
638 </td>
639 </tr>
641 <tr>
642 <td class="td_main" colspan="2">
644 <?php
646 if (!empty($errormsg)) echo "<p class=\"errormsg\" align=\"center\">$errormsg</p>\n";
649 if ($nextstage == SAVE) {
650 $INSTALL['stage'] = WELCOME;
651 $options = array();
652 $options['lang'] = $INSTALL['language'];
653 if ($configsuccess) {
654 echo "<p>".get_string('configfilewritten', 'install')."</p>\n";
656 echo "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"100%\">\n";
657 echo "<tr>\n";
658 echo "<td width=\"33.3%\">&nbsp;</td>\n";
659 echo "<td width=\"33.3%\">&nbsp;</td>\n";
660 echo "<td width=\"33.3%\" align=\"right\">\n";
661 print_single_button("index.php", $options, get_string('continue'));
662 echo "</td>\n";
663 echo "</tr>\n";
664 echo "</table>\n";
666 } else {
667 echo "<p class=\"errormsg\">".get_string('configfilenotwritten', 'install')."</p>";
669 echo "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"100%\">\n";
670 echo "<tr>\n";
671 echo "<td width=\"33.3%\">&nbsp;</td>\n";
672 echo "<td width=\"33.3%\" align=\"center\">\n";
673 $installoptions = array();
674 $installoptions['download'] = 1;
675 print_single_button("install.php", $installoptions, get_string('download', 'install'));
676 echo "</td>\n";
677 echo "<td width=\"33.3%\" align=\"right\">\n";
678 print_single_button("index.php", $options, get_string('continue'));
679 echo "</td>\n";
680 echo "</tr>\n";
681 echo "</table>\n";
683 echo "<hr />\n";
684 echo "<div style=\"text-align: ".fix_align_rtl("left")."\">\n";
685 echo "<pre>\n";
686 print_r(s($str));
687 echo "</pre>\n";
688 echo "</div>\n";
690 } else {
691 $formaction = (isset($_GET['configfile'])) ? "install.php?configfile=".$_GET['configfile'] : "install.php";
692 form_table($nextstage, $formaction);
697 </td>
698 </tr>
699 </table>
701 <?php
705 </body>
706 </html>
717 <?php
720 //==========================================================================//
722 function form_table($nextstage = WELCOME, $formaction = "install.php") {
723 global $INSTALL, $db;
725 /// Print the standard form if we aren't in the DOWNLOADLANG page
726 /// because it has its own form.
727 if ($nextstage != DOWNLOADLANG) {
728 $needtoopenform = false;
730 <form id="installform" method="post" action="<?php echo $formaction ?>">
731 <input type="hidden" name="stage" value="<?php echo $nextstage ?>" />
733 <?php
734 } else {
735 $needtoopenform = true;
738 <table class="install_table" cellspacing="3" cellpadding="3" align="center">
740 <?php
741 /// what we do depends on the stage we're at
742 switch ($nextstage) {
743 case WELCOME: /// Welcome and language settings
745 <tr>
746 <td class="td_left"><p><?php print_string('language') ?></p></td>
747 <td class="td_right">
748 <?php choose_from_menu (get_installer_list_of_languages(), 'language', $INSTALL['language'], '') ?>
749 </td>
750 </tr>
752 <?php
753 break;
754 case COMPATIBILITY: /// Compatibilty check
755 $compatsuccess = true;
757 /// Check that PHP is of a sufficient version
758 print_compatibility_row(inst_check_php_version(), get_string('phpversion', 'install'), get_string('phpversionerror', 'install'), 'phpversionhelp');
759 /// Check session auto start
760 print_compatibility_row(!ini_get_bool('session.auto_start'), get_string('sessionautostart', 'install'), get_string('sessionautostarterror', 'install'), 'sessionautostarthelp');
761 /// Check magic quotes
762 print_compatibility_row(!ini_get_bool('magic_quotes_runtime'), get_string('magicquotesruntime', 'install'), get_string('magicquotesruntimeerror', 'install'), 'magicquotesruntimehelp');
763 /// Check unsupported PHP configuration
764 print_compatibility_row(ini_get_bool('magic_quotes_gpc') || (!ini_get_bool('register_globals')), get_string('globalsquotes', 'install'), get_string('globalsquoteserror', 'install'), 'globalsquoteshelp');
765 /// Check safe mode
766 print_compatibility_row(!ini_get_bool('safe_mode'), get_string('safemode', 'install'), get_string('safemodeerror', 'install'), 'safemodehelp', true);
767 /// Check file uploads
768 print_compatibility_row(ini_get_bool('file_uploads'), get_string('fileuploads', 'install'), get_string('fileuploadserror', 'install'), 'fileuploadshelp', true);
769 /// Check GD version
770 print_compatibility_row(check_gd_version(), get_string('gdversion', 'install'), get_string('gdversionerror', 'install'), 'gdversionhelp', true);
771 /// Check memory limit
772 print_compatibility_row(check_memory_limit(), get_string('memorylimit', 'install'), get_string('memorylimiterror', 'install'), 'memorylimithelp', true);
775 break;
776 case DIRECTORY: /// Directory settings
779 <tr>
780 <td class="td_left"><p><?php print_string('wwwroot', 'install') ?></p></td>
781 <td class="td_right">
782 <input type="text" size="40"name="wwwrootform" value="<?php p($INSTALL['wwwrootform'],true) ?>" />
783 </td>
784 </tr>
785 <tr>
786 <td class="td_left"><p><?php print_string('dirroot', 'install') ?></p></td>
787 <td class="td_right">
788 <input type="text" size="40" name="dirrootform" disabled="disabled" value="<?php p($INSTALL['dirrootform'],true) ?>" />
789 </td>
790 </tr>
791 <tr>
792 <td class="td_left"><p><?php print_string('dataroot', 'install') ?></p></td>
793 <td class="td_right">
794 <input type="text" size="40" name="dataroot" value="<?php p($INSTALL['dataroot'],true) ?>" />
795 </td>
796 </tr>
798 <?php
799 break;
800 case DATABASE: /// Database settings
803 <tr>
804 <td class="td_left"><p><?php print_string('dbtype', 'install') ?></p></td>
805 <td class="td_right">
806 <?php choose_from_menu (array('mysql' => get_string('mysql', 'install'),
807 'mysqli' => get_string('mysqli', 'install'),
808 'oci8po' => get_string('oci8po', 'install'),
809 'postgres7' => get_string('postgres7', 'install'),
810 'mssql' => get_string('mssql', 'install'),
811 'mssql_n' => get_string('mssql_n', 'install'),
812 'odbc_mssql' => get_string('odbc_mssql', 'install')),
813 'dbtype', $INSTALL['dbtype'], '', 'toggledbinfo();') ?>
814 </td>
815 </tr>
816 <tr>
817 <td class="td_left"><p><?php print_string('dbhost', 'install') ?></p></td>
818 <td class="td_right">
819 <input type="text" size="40" name="dbhost" value="<?php echo $INSTALL['dbhost'] ?>" />
820 </td>
821 </tr>
822 <tr>
823 <td class="td_left"><p><?php print_string('database', 'install') ?></p></td>
824 <td class="td_right">
825 <input type="text" size="40" name="dbname" value="<?php echo $INSTALL['dbname'] ?>" />
826 </td>
827 </tr>
828 <tr>
829 <td class="td_left"><p><?php print_string('user') ?></p></td>
830 <td class="td_right">
831 <input type="text" size="40" name="dbuser" value="<?php echo $INSTALL['dbuser'] ?>" />
832 </td>
833 </tr>
834 <tr>
835 <td class="td_left"><p><?php print_string('password') ?></p></td>
836 <td class="td_right">
837 <input type="password" size="40" name="dbpass" value="<?php echo $INSTALL['dbpass'] ?>" />
838 </td>
839 </tr>
840 <tr>
841 <td class="td_left"><p><?php print_string('dbprefix', 'install') ?></p></td>
842 <td class="td_right">
843 <input type="text" size="40" name="prefix" value="<?php echo $INSTALL['prefix'] ?>" />
844 </td>
845 </tr>
847 <?php
848 break;
849 case ADMIN: /// Administration directory setting
852 <tr>
853 <td class="td_left"><p><?php print_string('admindirname', 'install') ?></p></td>
854 <td class="td_right">
855 <input type="text" size="40" name="admindirname" value="<?php echo $INSTALL['admindirname'] ?>" />
856 </td>
857 </tr>
860 <?php
861 break;
862 case ENVIRONMENT: /// Environment checks
865 <tr>
866 <td colspan="2">
867 <?php
868 error_reporting(0); // Hide errors
869 $dbconnected = $db->Connect($INSTALL['dbhost'],$INSTALL['dbuser'],$INSTALL['dbpass'],$INSTALL['dbname']);
870 error_reporting(7); // Show errors
871 if ($dbconnected) {
872 /// Execute environment check, printing results
873 check_moodle_environment($INSTALL['release'], $environment_results, true);
874 } else {
875 /// We never should reach this because DB has been tested before arriving here
876 $errormsg = get_string('dbconnectionerror', 'install');
877 $nextstage = DATABASE;
878 echo '<p class="errormsg" align="center">'.get_string('dbconnectionerror', 'install').'</p>';
881 </td>
882 </tr>
884 <?php
885 break;
886 case DOWNLOADLANG: /// Download language from download.moodle.org
889 <tr>
890 <td colspan="2">
891 <?php
892 /// Get array of languages, we are going to use it
893 $languages=get_installer_list_of_languages();
894 /// Print the download form (button) if necessary
895 if ($INSTALL['showdownloadlangpack'] == true && substr($INSTALL['language'],0,2) != 'en') {
896 $options = array();
897 $options['downloadlangpack'] = true;
898 $options['stage'] = DOWNLOADLANG;
899 $options['same'] = true;
900 print_simple_box_start('center');
901 print_single_button('install.php', $options, get_string('downloadlanguagebutton','install', $languages[$INSTALL['language']]), 'POST');
902 print_simple_box_end();
903 } else {
904 /// Show result info
905 /// English lang packs aren't downloaded
906 if (substr($INSTALL['language'],0,2) == 'en') {
907 print_simple_box(get_string('downloadlanguagenotneeded', 'install', $languages[$INSTALL['language']]), 'center', '80%');
908 } else {
909 if ($INSTALL['downloadlangpackerror']) {
910 echo "<p class=\"errormsg\" align=\"center\">".$INSTALL['downloadlangpackerror']."</p>\n";
911 print_simple_box(get_string('langdownloaderror', 'install', $languages[$INSTALL['language']]), 'center', '80%');
912 } else {
913 print_simple_box(get_string('langdownloadok', 'install', $languages[$INSTALL['language']]), 'center', '80%');
918 </td>
919 </tr>
921 <?php
922 break;
923 default:
927 <tr>
928 <td colspan="<?php echo ($nextstage == COMPATIBILITY) ? 3 : 2; ?>">
930 <?php
931 if ($needtoopenform) {
933 <form id="installform" method="post" action="<?php echo $formaction ?>">
934 <input type="hidden" name="stage" value="<?php echo $nextstage ?>" />
935 <?php
939 <?php echo ($nextstage < SAVE) ? "<input type=\"submit\" name=\"next\" value=\"".get_string('next')." &raquo;\" style=\"float: ".fix_align_rtl("right")."\"/>\n" : "&nbsp;\n" ?>
940 <?php echo ($nextstage > WELCOME) ? "<input type=\"submit\" name=\"prev\" value=\"&laquo; ".get_string('previous')."\" style=\"float: ".fix_align_rtl("left")."\"/>\n" : "&nbsp;\n" ?>
942 <?php
943 if ($needtoopenform) {
945 </form>
946 <?php
951 </td>
953 </tr>
955 </table>
956 <?php
957 if (!$needtoopenform) {
959 </form>
960 <?php
964 <?php
969 //==========================================================================//
971 function print_compatibility_row($success, $testtext, $errormessage, $helpfield='', $caution=false) {
972 echo "<tr>\n";
973 echo "<td class=\"td_left\" valign=\"top\" nowrap width=\"160\"><p>$testtext</p></td>\n";
974 if ($success) {
975 echo "<td valign=\"top\"><p class=\"p_pass\">".get_string('pass', 'install')."</p></td>\n";
976 echo "<td valign=\"top\">&nbsp;</td>\n";
977 } else {
978 echo "<td valign=\"top\"";
979 echo ($caution) ? "<p class=\"p_caution\">".get_string('caution', 'install') : "<p class=\"p_fail\">".get_string('fail', 'install');
980 echo "</p></td>\n";
981 echo "<td valign=\"top\">";
982 echo "<p>$errormessage ";
983 install_helpbutton("install.php?help=$helpfield");
984 echo "</p></td>\n";
986 echo "</tr>\n";
987 return $success;
991 //==========================================================================//
993 function install_helpbutton($url, $title='') {
994 if ($title == '') {
995 $title = get_string('help');
997 echo "<a href=\"javascript: void(0)\">";
998 echo "<img src=\"pix/help.gif\" class=\"iconhelp\" alt=\"$title\" title=\"$title\" ";
999 echo "onClick=\"return window.open('$url', 'Help', 'menubar=0,location=0,scrollbars,resizable,width=500,height=400')\">";
1000 echo "</a>\n";
1005 //==========================================================================//
1007 function print_install_help($help) {
1008 switch ($help) {
1009 case 'phpversionhelp':
1010 print_string($help, 'install', phpversion());
1011 break;
1012 case 'memorylimithelp':
1013 print_string($help, 'install', get_memory_limit());
1014 break;
1015 default:
1016 print_string($help, 'install');
1021 //==========================================================================//
1023 function get_memory_limit() {
1024 if ($limit = ini_get('memory_limit')) {
1025 return $limit;
1026 } else {
1027 return get_cfg_var('memory_limit');
1031 //==========================================================================//
1033 function check_memory_limit() {
1035 /// if limit is already 40 or more then we don't care if we can change it or not
1036 if ((int)str_replace('M', '', get_memory_limit()) >= 40) {
1037 return true;
1040 /// Otherwise, see if we can change it ourselves
1041 @ini_set('memory_limit', '40M');
1042 return ((int)str_replace('M', '', get_memory_limit()) >= 40);
1045 //==========================================================================//
1047 function inst_check_php_version() {
1048 if (!check_php_version("4.3.0")) {
1049 return false;
1050 } else if (check_php_version("5.0.0")) {
1051 return check_php_version("5.1.0"); // 5.0.x is too buggy
1053 return true; // 4.3.x or 4.4.x is fine
1055 //==========================================================================//
1057 /* This function returns a list of languages and their full names. The
1058 * list of available languages is fetched from install/lang/xx/installer.php
1059 * and it's used exclusively by the installation process
1060 * @return array An associative array with contents in the form of LanguageCode => LanguageName
1062 function get_installer_list_of_languages() {
1064 global $CFG;
1066 $languages = array();
1068 /// Get raw list of lang directories
1069 $langdirs = get_list_of_plugins('install/lang');
1070 asort($langdirs);
1071 /// Get some info from each lang
1072 foreach ($langdirs as $lang) {
1073 if (file_exists($CFG->dirroot .'/install/lang/'. $lang .'/installer.php')) {
1074 include($CFG->dirroot .'/install/lang/'. $lang .'/installer.php');
1075 if (substr($lang, -5) == '_utf8') { //Remove the _utf8 suffix from the lang to show
1076 $shortlang = substr($lang, 0, -5);
1077 } else {
1078 $shortlang = $lang;
1080 if ($lang == 'en') { //Explain this is non-utf8 en
1081 $shortlang = 'non-utf8 en';
1083 if (!empty($string['thislanguage'])) {
1084 $languages[$lang] = $string['thislanguage'] .' ('. $shortlang .')';
1086 unset($string);
1089 /// Return array
1090 return $languages;
1093 //==========================================================================//
1095 function css_styles() {
1098 <style type="text/css">
1100 body { background-color: #ffeece; }
1101 p, li, td {
1102 font-family: helvetica, arial, sans-serif;
1103 font-size: 10pt;
1105 a { text-decoration: none; color: blue; }
1106 a img {
1107 border: none;
1109 .errormsg {
1110 color: red;
1111 font-weight: bold;
1113 blockquote {
1114 font-family: courier, monospace;
1115 font-size: 10pt;
1117 .install_table {
1118 width: 500px;
1120 .td_left {
1121 text-align: <?php echo fix_align_rtl("right") ?>;
1122 font-weight: bold;
1124 .td_right {
1125 text-align: <?php echo fix_align_rtl("left") ?>;
1127 .main {
1128 width: 500px;
1129 border-width: 1px;
1130 border-style: solid;
1131 border-color: #ffc85f;
1132 -moz-border-radius-bottomleft: 15px;
1133 -moz-border-radius-bottomright: 15px;
1135 .td_mainheading {
1136 background-color: #fee6b9;
1137 padding: 10px;
1139 .td_main {
1140 text-align: center;
1142 .td_mainlogo {
1144 .p_mainlogo {
1146 .p_mainheading {
1147 font-size: 11pt;
1149 .p_subheading {
1150 font-size: 10pt;
1151 padding: 10px;
1153 .p_mainheader{
1154 text-align: right;
1155 font-size: 20pt;
1156 font-weight: bold;
1158 .p_pass {
1159 color: green;
1160 font-weight: bold;
1162 .p_fail {
1163 color: red;
1164 font-weight: bold;
1166 .p_caution {
1167 color: #ff6600;
1168 font-weight: bold;
1170 .p_help {
1171 text-align: center;
1172 font-family: helvetica, arial, sans-serif;
1173 font-size: 14pt;
1174 font-weight: bold;
1175 color: #333333;
1177 .environmenttable {
1178 font-size: 10pt;
1179 border-color: #ffc85f;
1181 table.environmenttable .error {
1182 background-color : red;
1183 color : inherit;
1186 table.environmenttable .warn {
1187 background-color : yellow;
1190 table.environmenttable .ok {
1191 background-color : lightgreen;
1193 .header {
1194 background-color: #fee6b9;
1195 font-size: 10pt;
1197 .cell {
1198 background-color: #ffeece;
1199 font-size: 10pt;
1201 .error {
1202 color: #ff0000;
1204 .errorboxcontent {
1205 text-align: center;
1206 font-weight: bold;
1207 padding: 20px;
1208 color: #ff0000;
1210 .invisiblefieldset {
1211 display:inline;
1212 border:0px;
1213 padding:0px;
1214 margin:0px;
1216 #mysql, #mysqli, #postgres7, #mssql, #mssql_n, #odbc_mssql, #oci8po {
1217 display: none;
1220 </style>
1222 <?php
1225 //==========================================================================//
1227 function database_js() {
1230 <script type="text/javascript" defer="defer">
1231 function toggledbinfo() {
1232 //Calculate selected value
1233 var showid = 'mysql';
1234 if (document.getElementById('installform').dbtype.value) {
1235 showid = document.getElementById('installform').dbtype.value;
1237 if (document.getElementById) {
1238 //Hide all the divs
1239 document.getElementById('mysql').style.display = '';
1240 document.getElementById('mysqli').style.display = '';
1241 document.getElementById('postgres7').style.display = '';
1242 document.getElementById('mssql').style.display = '';
1243 document.getElementById('mssql_n').style.display = '';
1244 document.getElementById('odbc_mssql').style.display = '';
1245 document.getElementById('oci8po').style.display = '';
1246 //Show the selected div
1247 document.getElementById(showid).style.display = 'block';
1248 } else if (document.all) {
1249 //This is the way old msie versions work
1250 //Hide all the divs
1251 document.all['mysql'].style.display = '';
1252 document.all['mysqli'].style.display = '';
1253 document.all['postgres7'].style.display = '';
1254 document.all['mssql'].style.display = '';
1255 document.all['mssql_n'].style.display = '';
1256 document.all['odbc_mssql'].style.display = '';
1257 document.all['oci8po'].style.display = '';
1258 //Show the selected div
1259 document.all[showid].style.display = 'block';
1260 } else if (document.layers) {
1261 //This is the way nn4 works
1262 //Hide all the divs
1263 document.layers['mysql'].style.display = '';
1264 document.layers['mysqli'].style.display = '';
1265 document.layers['postgres7'].style.display = '';
1266 document.layers['mssql'].style.display = '';
1267 document.layers['mssql_n'].style.display = '';
1268 document.layers['odbc_mssql'].style.display = '';
1269 document.layers['oci8po'].style.display = '';
1270 //Show the selected div
1271 document.layers[showid].style.display = 'block';
1274 </script>
1276 <?php