MDL-14406
[moodle-linuxchix.git] / install.php
blobe10cf07feddfd8d7692e2c747aca5e94996d0f3e
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 $COURSE->id = 0;
145 /// Include some moodle libraries
147 require_once($CFG->libdir.'/adminlib.php');
148 require_once($CFG->libdir.'/setuplib.php');
149 require_once($CFG->libdir.'/moodlelib.php');
150 require_once($CFG->libdir.'/weblib.php');
151 require_once($CFG->libdir.'/deprecatedlib.php');
152 require_once($CFG->libdir.'/adodb/adodb.inc.php');
153 require_once($CFG->libdir.'/environmentlib.php');
154 require_once($CFG->libdir.'/xmlize.php');
155 require_once($CFG->libdir.'/componentlib.class.php');
156 require_once($CFG->dirroot.'/version.php');
158 /// Set version and release
159 $INSTALL['version'] = $version;
160 $INSTALL['release'] = $release;
162 /// Have the $db object ready because we are going to use it often
163 define ('ADODB_ASSOC_CASE', 0); //Use lowercase fieldnames for ADODB_FETCH_ASSOC
164 $db = &ADONewConnection($INSTALL['dbtype']);
165 $db->SetFetchMode(ADODB_FETCH_ASSOC);
167 /// guess the www root
168 if ($INSTALL['wwwroot'] == '') {
169 list($INSTALL['wwwroot'], $xtra) = explode('/install.php', qualified_me());
170 $INSTALL['wwwrootform'] = $INSTALL['wwwroot'];
172 // now try to guess the correct dataroot not accessible via web
173 $CFG->wwwroot = $INSTALL['wwwroot'];
174 $i = 0; //safety check - dirname might return some unexpected results
175 while(is_dataroot_insecure()) {
176 $parrent = dirname($CFG->dataroot);
177 $i++;
178 if ($parrent == '/' or $parrent == '.' or preg_match('/^[a-z]:\\\?$/i', $parrent) or ($i > 100)) {
179 $CFG->dataroot = ''; //can not find secure location for dataroot
180 break;
182 $CFG->dataroot = dirname($parrent).'/moodledata';
184 $INSTALL['dataroot'] = $CFG->dataroot;
187 $headstagetext = array(WELCOME => get_string('chooselanguagehead', 'install'),
188 COMPATIBILITY => get_string('compatibilitysettingshead', 'install'),
189 DIRECTORY => get_string('directorysettingshead', 'install'),
190 DATABASE => get_string('databasesettingshead', 'install'),
191 ADMIN => get_string('admindirsettinghead', 'install'),
192 ENVIRONMENT => get_string('environmenthead', 'install'),
193 DOWNLOADLANG => get_string('downloadlanguagehead', 'install'),
194 SAVE => get_string('configurationcompletehead', 'install')
197 $substagetext = array(WELCOME => get_string('chooselanguagesub', 'install'),
198 COMPATIBILITY => get_string('compatibilitysettingssub', 'install'),
199 DIRECTORY => get_string('directorysettingssub', 'install'),
200 DATABASE => get_string('databasesettingssub', 'install'),
201 ADMIN => get_string('admindirsettingsub', 'install'),
202 ENVIRONMENT => get_string('environmentsub', 'install'),
203 DOWNLOADLANG => get_string('downloadlanguagesub', 'install'),
204 SAVE => get_string('configurationcompletesub', 'install')
209 //==========================================================================//
211 /// Are we in help mode?
213 if (isset($_GET['help'])) {
214 $nextstage = -1;
219 //==========================================================================//
221 /// Are we in config download mode?
223 if (isset($_GET['download'])) {
224 header("Content-Type: application/x-forcedownload\n");
225 header("Content-Disposition: attachment; filename=\"config.php\"");
226 echo $INSTALL['config'];
227 exit;
234 //==========================================================================//
236 /// Check the directory settings
238 if ($INSTALL['stage'] == DIRECTORY) {
240 error_reporting(0);
242 /// check wwwroot
243 if (ini_get('allow_url_fopen') && false) { /// This was not reliable
244 if (($fh = @fopen($INSTALL['wwwrootform'].'/install.php', 'r')) === false) {
245 $errormsg .= get_string('wwwrooterror', 'install').'<br />';
246 $INSTALL['wwwrootform'] = $INSTALL['wwwroot'];
249 if ($fh) fclose($fh);
251 /// check dirroot
252 if (($fh = @fopen($INSTALL['dirrootform'].'/install.php', 'r')) === false ) {
253 $errormsg .= get_string('dirrooterror', 'install').'<br />';
254 $INSTALL['dirrootform'] = $INSTALL['dirroot'];
256 if ($fh) fclose($fh);
258 /// check dataroot
259 $CFG->dataroot = $INSTALL['dataroot'];
260 if (make_upload_directory('sessions', false) === false ) {
261 $errormsg .= get_string('datarooterror', 'install').'<br />';
263 if ($fh) fclose($fh);
265 if (!empty($errormsg)) $nextstage = DIRECTORY;
267 error_reporting(7);
272 //==========================================================================//
274 /// Check database settings if stage 3 data submitted
275 /// Try to connect to the database. If that fails then try to create the database
277 if ($INSTALL['stage'] == DATABASE) {
279 /// different format for postgres7 by socket
280 if ($INSTALL['dbtype'] == 'postgres7' and ($INSTALL['dbhost'] == 'localhost' || $INSTALL['dbhost'] == '127.0.0.1')) {
281 $INSTALL['dbhost'] = "user='{$INSTALL['dbuser']}' password='{$INSTALL['dbpass']}' dbname='{$INSTALL['dbname']}'";
282 $INSTALL['dbuser'] = '';
283 $INSTALL['dbpass'] = '';
284 $INSTALL['dbname'] = '';
286 if ($INSTALL['prefix'] == '') { /// must have a prefix
287 $INSTALL['prefix'] = 'mdl_';
291 if ($INSTALL['dbtype'] == 'mysql') { /// Check MySQL extension is present
292 if (!extension_loaded('mysql')) {
293 $errormsg = get_string('mysqlextensionisnotpresentinphp', 'install');
294 $nextstage = DATABASE;
298 if ($INSTALL['dbtype'] == 'mysqli') { /// Check MySQLi extension is present
299 if (!extension_loaded('mysqli')) {
300 $errormsg = get_string('mysqliextensionisnotpresentinphp', 'install');
301 $nextstage = DATABASE;
305 if ($INSTALL['dbtype'] == 'postgres7') { /// Check PostgreSQL extension is present
306 if (!extension_loaded('pgsql')) {
307 $errormsg = get_string('pgsqlextensionisnotpresentinphp', 'install');
308 $nextstage = DATABASE;
312 if ($INSTALL['dbtype'] == 'mssql') { /// Check MSSQL extension is present
313 if (!function_exists('mssql_connect')) {
314 $errormsg = get_string('mssqlextensionisnotpresentinphp', 'install');
315 $nextstage = DATABASE;
319 if ($INSTALL['dbtype'] == 'mssql_n') { /// Check MSSQL extension is present
320 if (!function_exists('mssql_connect')) {
321 $errormsg = get_string('mssqlextensionisnotpresentinphp', 'install');
322 $nextstage = DATABASE;
326 if ($INSTALL['dbtype'] == 'odbc_mssql') { /// Check ODBC extension is present
327 if (!extension_loaded('odbc')) {
328 $errormsg = get_string('odbcextensionisnotpresentinphp', 'install');
329 $nextstage = DATABASE;
333 if ($INSTALL['dbtype'] == 'oci8po') { /// Check OCI extension is present
334 if (!extension_loaded('oci8')) {
335 $errormsg = get_string('ociextensionisnotpresentinphp', 'install');
336 $nextstage = DATABASE;
340 if (empty($INSTALL['prefix']) && $INSTALL['dbtype'] != 'mysql' && $INSTALL['dbtype'] != 'mysqli') { // All DBs but MySQL require prefix (reserv. words)
341 $errormsg = get_string('dbwrongprefix', 'install');
342 $nextstage = DATABASE;
345 if ($INSTALL['dbtype'] == 'oci8po' && strlen($INSTALL['prefix']) > 2) { // Oracle max prefix = 2cc (30cc limit)
346 $errormsg = get_string('dbwrongprefix', 'install');
347 $nextstage = DATABASE;
350 if ($INSTALL['dbtype'] == 'oci8po' && !empty($INSTALL['dbhost'])) { // Oracle host must be blank (tnsnames.ora has it)
351 $errormsg = get_string('dbwronghostserver', 'install');
352 $nextstage = DATABASE;
355 if (empty($errormsg)) {
357 error_reporting(0); // Hide errors
359 if (! $dbconnected = $db->Connect($INSTALL['dbhost'],$INSTALL['dbuser'],$INSTALL['dbpass'],$INSTALL['dbname'])) {
360 $db->database = ''; // reset database name cached by ADODB. Trick from MDL-9609
361 if ($dbconnected = $db->Connect($INSTALL['dbhost'],$INSTALL['dbuser'],$INSTALL['dbpass'])) { /// Try to connect without DB
362 switch ($INSTALL['dbtype']) { /// Try to create a database
363 case 'mysql':
364 case 'mysqli':
365 if ($db->Execute("CREATE DATABASE {$INSTALL['dbname']} DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;")) {
366 $dbconnected = $db->Connect($INSTALL['dbhost'],$INSTALL['dbuser'],$INSTALL['dbpass'],$INSTALL['dbname']);
367 } else {
368 $errormsg = get_string('dbcreationerror', 'install');
369 $nextstage = DATABASE;
371 break;
374 } else {
375 /// We have been able to connect properly, just test the database encoding now.
376 /// It must be Unicode for 1.8 installations.
377 $encoding = '';
378 switch ($INSTALL['dbtype']) {
379 case 'mysql':
380 case 'mysqli':
381 /// Get MySQL character_set_database value
382 $rs = $db->Execute("SHOW VARIABLES LIKE 'character_set_database'");
383 if ($rs && !$rs->EOF) {
384 $records = $rs->GetAssoc(true);
385 $encoding = $records['character_set_database']['Value'];
386 if (strtoupper($encoding) != 'UTF8') {
387 /// Try to set the encoding now!
388 if (! $db->Metatables()) { // We have no tables so go ahead
389 $db->Execute("ALTER DATABASE `".$INSTALL['dbname']."` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci");
390 $rs = $db->Execute("SHOW VARIABLES LIKE 'character_set_database'"); // this works
394 /// If conversion fails, skip, let environment testing do the job
396 break;
397 case 'postgres7':
398 /// Skip, let environment testing do the job
399 break;
400 case 'oci8po':
401 /// Skip, let environment testing do the job
402 break;
407 error_reporting(7);
409 if (($dbconnected === false) and (empty($errormsg)) ) {
410 $errormsg = get_string('dbconnectionerror', 'install');
411 $nextstage = DATABASE;
417 //==========================================================================//
419 /// If the next stage is admin directory settings OR we have just come from there then
420 /// check the admin directory.
421 /// If we can open a file then we know that the admin name is correct.
423 if ($nextstage == ADMIN or $INSTALL['stage'] == ADMIN) {
424 if (!ini_get('allow_url_fopen')) {
425 $nextstage = ($goforward) ? ENVIRONMENT : DATABASE;
426 } else if (($fh = @fopen($INSTALL['wwwrootform'].'/'.$INSTALL['admindirname'].'/environment.xml', 'r')) !== false) {
427 $nextstage = ($goforward) ? ENVIRONMENT : DATABASE;
428 fclose($fh);
429 } else {
430 $nextstage = ($goforward) ? ENVIRONMENT : DATABASE;
431 //if ($nextstage != ADMIN) {
432 // $errormsg = get_string('admindirerror', 'install');
433 // $nextstage = ADMIN;
434 // }
438 //==========================================================================//
440 // Check if we can navigate from the environemt page (because it's ok)
442 if ($INSTALL['stage'] == ENVIRONMENT) {
443 error_reporting(0); // Hide errors
444 $dbconnected = $db->Connect($INSTALL['dbhost'],$INSTALL['dbuser'],$INSTALL['dbpass'],$INSTALL['dbname']);
445 error_reporting(7); // Show errors
446 if ($dbconnected) {
447 /// Execute environment check, printing results
448 if (!check_moodle_environment($INSTALL['release'], $environment_results, false)) {
449 $nextstage = ENVIRONMENT;
451 } else {
452 /// We never should reach this because DB has been tested before arriving here
453 $errormsg = get_string('dbconnectionerror', 'install');
454 $nextstage = DATABASE;
460 //==========================================================================//
462 // Try to download the lang pack if it has been selected
464 if ($INSTALL['stage'] == DOWNLOADLANG && $INSTALL['downloadlangpack']) {
466 $downloadsuccess = false;
467 $downloaderror = '';
469 error_reporting(0); // Hide errors
471 /// Create necessary lang dir
472 if (!make_upload_directory('lang', false)) {
473 $downloaderror = get_string('cannotcreatelangdir', 'error');
476 /// Download and install component
477 if (($cd = new component_installer('http://download.moodle.org', 'lang16',
478 $INSTALL['language'].'.zip', 'languages.md5', 'lang')) && empty($errormsg)) {
479 $status = $cd->install(); //returns COMPONENT_(ERROR | UPTODATE | INSTALLED)
480 switch ($status) {
481 case COMPONENT_ERROR:
482 if ($cd->get_error() == 'remotedownloaderror') {
483 $a = new stdClass();
484 $a->url = 'http://download.moodle.org/lang16/'.$INSTALL['language'].'.zip';
485 $a->dest= $CFG->dataroot.'/lang';
486 $downloaderror = get_string($cd->get_error(), 'error', $a);
487 } else {
488 $downloaderror = get_string($cd->get_error(), 'error');
490 break;
491 case COMPONENT_UPTODATE:
492 case COMPONENT_INSTALLED:
493 $downloadsuccess = true;
494 break;
495 default:
496 //We shouldn't reach this point
498 } else {
499 //We shouldn't reach this point
502 error_reporting(7); // Show errors
504 if ($downloadsuccess) {
505 $INSTALL['downloadlangpack'] = false;
506 $INSTALL['showdownloadlangpack'] = false;
507 $INSTALL['downloadlangpackerror'] = $downloaderror;
508 } else {
509 $INSTALL['downloadlangpack'] = false;
510 $INSTALL['showdownloadlangpack'] = false;
511 $INSTALL['downloadlangpackerror'] = $downloaderror;
517 //==========================================================================//
519 /// Display or print the data
520 /// Put the data into a string
521 /// Try to open config file for writing.
523 if ($nextstage == SAVE) {
525 $str = '<?php /// Moodle Configuration File '."\r\n";
526 $str .= "\r\n";
528 $str .= 'unset($CFG);'."\r\n";
529 $str .= "\r\n";
531 $str .= '$CFG->dbtype = \''.$INSTALL['dbtype']."';\r\n";
532 $str .= '$CFG->dbhost = \''.addslashes($INSTALL['dbhost'])."';\r\n";
533 if (!empty($INSTALL['dbname'])) {
534 $str .= '$CFG->dbname = \''.$INSTALL['dbname']."';\r\n";
535 // support single quotes in db user/passwords
536 $str .= '$CFG->dbuser = \''.addsingleslashes($INSTALL['dbuser'])."';\r\n";
537 $str .= '$CFG->dbpass = \''.addsingleslashes($INSTALL['dbpass'])."';\r\n";
539 $str .= '$CFG->dbpersist = false;'."\r\n";
540 $str .= '$CFG->prefix = \''.$INSTALL['prefix']."';\r\n";
541 $str .= "\r\n";
543 $str .= '$CFG->wwwroot = \''.s($INSTALL['wwwrootform'],true)."';\r\n";
544 $str .= '$CFG->dirroot = \''.s($INSTALL['dirrootform'],true)."';\r\n";
545 $str .= '$CFG->dataroot = \''.s($INSTALL['dataroot'],true)."';\r\n";
546 $str .= '$CFG->admin = \''.s($INSTALL['admindirname'],true)."';\r\n";
547 $str .= "\r\n";
549 $str .= '$CFG->directorypermissions = 00777; // try 02777 on a server in Safe Mode'."\r\n";
550 $str .= "\r\n";
552 $str .= 'require_once("$CFG->dirroot/lib/setup.php");'."\r\n";
553 $str .= '// MAKE SURE WHEN YOU EDIT THIS FILE THAT THERE ARE NO SPACES, BLANK LINES,'."\r\n";
554 $str .= '// RETURNS, OR ANYTHING ELSE AFTER THE TWO CHARACTERS ON THE NEXT LINE.'."\r\n";
555 $str .= '?>';
557 umask(0137);
559 if (( $configsuccess = ($fh = @fopen($configfile, 'w')) ) !== false) {
560 fwrite($fh, $str);
561 fclose($fh);
565 $INSTALL['config'] = $str;
570 //==========================================================================//
573 <html dir="<?php echo (right_to_left() ? 'rtl' : 'ltr'); ?>">
574 <head>
575 <link rel="shortcut icon" href="theme/standard/favicon.ico" />
576 <title>Moodle Install</title>
577 <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
578 <?php css_styles() ?>
579 <?php database_js() ?>
581 </head>
583 <body>
586 <?php
587 if (isset($_GET['help'])) {
588 print_install_help($_GET['help']);
589 close_window_button();
590 } else {
594 <table class="main" align="center" cellpadding="3" cellspacing="0">
595 <tr>
596 <td class="td_mainlogo">
597 <p class="p_mainlogo"><img src="pix/moodlelogo-med.gif" width="240" height="60" alt="Moodle logo"></p>
598 </td>
599 <td class="td_mainlogo" valign="bottom">
600 <p class="p_mainheader"><?php print_string('installation', 'install') ?></p>
601 </td>
602 </tr>
604 <tr>
605 <td class="td_mainheading" colspan="2">
606 <p class="p_mainheading"><?php echo $headstagetext[$nextstage] ?></p>
607 <?php /// Exceptionaly, depending of the DB selected, we show some different text
608 /// from the standard one to show better instructions for each DB
609 if ($nextstage == DATABASE) {
610 echo '<script type="text/javascript" defer="defer">window.onload=toggledbinfo;</script>';
611 echo '<div id="mysql" name="mysql">' . get_string('databasesettingssub_mysql', 'install');
612 echo '<p align="center">' . get_string('databasesettingswillbecreated', 'install') . '</p>';
613 echo '</div>';
615 echo '<div id="mysqli" name="mysqli">' . get_string('databasesettingssub_mysqli', 'install');
616 echo '<p align="center">' . get_string('databasesettingswillbecreated', 'install') . '</p>';
617 echo '</div>';
619 echo '<div id="postgres7" name="postgres7">' . get_string('databasesettingssub_postgres7', 'install') . '</div>';
621 echo '<div id="mssql" name="mssql">' . get_string('databasesettingssub_mssql', 'install');
622 /// Link to mssql installation page
623 echo '<p align="right"><a href="http://docs.moodle.org/en/Installing_MSSQL_for_PHP" target="_blank">';
624 echo '<img src="pix/docs.gif' . '" alt="Docs" class="iconhelp" />';
625 echo get_string('moodledocslink', 'install') . '</a></p>';
626 echo '</div>';
628 echo '<div id="mssql_n" name="mssql">' . get_string('databasesettingssub_mssql_n', 'install');
629 /// Link to mssql installation page
630 echo '<p align="right"><a href="http://docs.moodle.org/en/Installing_MSSQL_for_PHP" target="_blank">';
631 echo '<img src="pix/docs.gif' . '" alt="Docs" />';
632 echo get_string('moodledocslink', 'install') . '</a></p>';
633 echo '</div>';
635 echo '<div id="odbc_mssql" name="odbc_mssql">'. get_string('databasesettingssub_odbc_mssql', 'install');
636 /// Link to mssql installation page
637 echo '<p align="right"><a href="http://docs.moodle.org/en/Installing_MSSQL_for_PHP" target="_blank">';
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="oci8po" name="oci8po">' . get_string('databasesettingssub_oci8po', 'install');
643 /// Link to oracle installation page
644 echo '<p align="right"><a href="http://docs.moodle.org/en/Installing_Oracle_for_PHP" target="_blank">';
645 echo '<img src="pix/docs.gif' . '" alt="Docs" class="iconhelp" />';
646 echo get_string('moodledocslink', 'install') . '</a></p>';
647 echo '</div>';
648 } else {
649 if (!empty($substagetext[$nextstage])) {
650 echo '<p class="p_subheading">' . $substagetext[$nextstage] . '</p>';
654 </td>
655 </tr>
657 <tr>
658 <td class="td_main" colspan="2">
660 <?php
662 if (!empty($errormsg)) echo "<p class=\"errormsg\" align=\"center\">$errormsg</p>\n";
665 if ($nextstage == SAVE) {
666 $INSTALL['stage'] = WELCOME;
667 $options = array();
668 $options['lang'] = $INSTALL['language'];
669 if ($configsuccess) {
670 echo "<p>".get_string('configfilewritten', 'install')."</p>\n";
672 echo "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"100%\">\n";
673 echo "<tr>\n";
674 echo "<td width=\"33.3%\">&nbsp;</td>\n";
675 echo "<td width=\"33.3%\">&nbsp;</td>\n";
676 echo "<td width=\"33.3%\" align=\"right\">\n";
677 print_single_button("index.php", $options, get_string('continue'));
678 echo "</td>\n";
679 echo "</tr>\n";
680 echo "</table>\n";
682 } else {
683 echo "<p class=\"errormsg\">".get_string('configfilenotwritten', 'install')."</p>";
685 echo "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"100%\">\n";
686 echo "<tr>\n";
687 echo "<td width=\"33.3%\">&nbsp;</td>\n";
688 echo "<td width=\"33.3%\" align=\"center\">\n";
689 $installoptions = array();
690 $installoptions['download'] = 1;
691 print_single_button("install.php", $installoptions, get_string('download', 'install'));
692 echo "</td>\n";
693 echo "<td width=\"33.3%\" align=\"right\">\n";
694 print_single_button("index.php", $options, get_string('continue'));
695 echo "</td>\n";
696 echo "</tr>\n";
697 echo "</table>\n";
699 echo "<hr />\n";
700 echo "<div style=\"text-align: ".fix_align_rtl("left")."\">\n";
701 echo "<pre>\n";
702 print_r(s($str));
703 echo "</pre>\n";
704 echo "</div>\n";
706 } else {
707 $formaction = (isset($_GET['configfile'])) ? "install.php?configfile=".$_GET['configfile'] : "install.php";
708 form_table($nextstage, $formaction);
713 </td>
714 </tr>
715 </table>
717 <?php
721 </body>
722 </html>
733 <?php
736 //==========================================================================//
738 function form_table($nextstage = WELCOME, $formaction = "install.php") {
739 global $INSTALL, $db;
741 /// Print the standard form if we aren't in the DOWNLOADLANG page
742 /// because it has its own form.
743 if ($nextstage != DOWNLOADLANG) {
744 $needtoopenform = false;
746 <form id="installform" method="post" action="<?php echo $formaction ?>">
747 <input type="hidden" name="stage" value="<?php echo $nextstage ?>" />
749 <?php
750 } else {
751 $needtoopenform = true;
754 <table class="install_table" cellspacing="3" cellpadding="3" align="center">
756 <?php
757 /// what we do depends on the stage we're at
758 switch ($nextstage) {
759 case WELCOME: /// Welcome and language settings
761 <tr>
762 <td class="td_left"><p><?php print_string('language') ?></p></td>
763 <td class="td_right">
764 <?php choose_from_menu (get_installer_list_of_languages(), 'language', $INSTALL['language'], '') ?>
765 </td>
766 </tr>
768 <?php
769 break;
770 case COMPATIBILITY: /// Compatibilty check
771 $compatsuccess = true;
773 /// Check that PHP is of a sufficient version
774 print_compatibility_row(inst_check_php_version(), get_string('phpversion', 'install'), get_string('phpversionerror', 'install'), 'phpversionhelp');
775 /// Check session auto start
776 print_compatibility_row(!ini_get_bool('session.auto_start'), get_string('sessionautostart', 'install'), get_string('sessionautostarterror', 'install'), 'sessionautostarthelp');
777 /// Check magic quotes
778 print_compatibility_row(!ini_get_bool('magic_quotes_runtime'), get_string('magicquotesruntime', 'install'), get_string('magicquotesruntimeerror', 'install'), 'magicquotesruntimehelp');
779 /// Check unsupported PHP configuration
780 print_compatibility_row(ini_get_bool('magic_quotes_gpc') || (!ini_get_bool('register_globals')), get_string('globalsquotes', 'install'), get_string('globalsquoteserror', 'install'), 'globalsquoteshelp');
781 /// Check safe mode
782 print_compatibility_row(!ini_get_bool('safe_mode'), get_string('safemode', 'install'), get_string('safemodeerror', 'install'), 'safemodehelp', true);
783 /// Check file uploads
784 print_compatibility_row(ini_get_bool('file_uploads'), get_string('fileuploads', 'install'), get_string('fileuploadserror', 'install'), 'fileuploadshelp', true);
785 /// Check GD version
786 print_compatibility_row(check_gd_version(), get_string('gdversion', 'install'), get_string('gdversionerror', 'install'), 'gdversionhelp', true);
787 /// Check memory limit
788 print_compatibility_row(check_memory_limit(), get_string('memorylimit', 'install'), get_string('memorylimiterror', 'install'), 'memorylimithelp', true);
791 break;
792 case DIRECTORY: /// Directory settings
795 <tr>
796 <td class="td_left"><p><?php print_string('wwwroot', 'install') ?></p></td>
797 <td class="td_right">
798 <input type="text" size="40"name="wwwrootform" value="<?php p($INSTALL['wwwrootform'],true) ?>" />
799 </td>
800 </tr>
801 <tr>
802 <td class="td_left"><p><?php print_string('dirroot', 'install') ?></p></td>
803 <td class="td_right">
804 <input type="text" size="40" name="dirrootform" disabled="disabled" value="<?php p($INSTALL['dirrootform'],true) ?>" />
805 </td>
806 </tr>
807 <tr>
808 <td class="td_left"><p><?php print_string('dataroot', 'install') ?></p></td>
809 <td class="td_right">
810 <input type="text" size="40" name="dataroot" value="<?php p($INSTALL['dataroot'],true) ?>" />
811 </td>
812 </tr>
814 <?php
815 break;
816 case DATABASE: /// Database settings
819 <tr>
820 <td class="td_left"><p><?php print_string('dbtype', 'install') ?></p></td>
821 <td class="td_right">
822 <?php choose_from_menu (array('mysql' => get_string('mysql', 'install'),
823 'mysqli' => get_string('mysqli', 'install'),
824 'oci8po' => get_string('oci8po', 'install'),
825 'postgres7' => get_string('postgres7', 'install'),
826 'mssql' => get_string('mssql', 'install'),
827 'mssql_n' => get_string('mssql_n', 'install'),
828 'odbc_mssql' => get_string('odbc_mssql', 'install')),
829 'dbtype', $INSTALL['dbtype'], '', 'toggledbinfo();') ?>
830 </td>
831 </tr>
832 <tr>
833 <td class="td_left"><p><?php print_string('dbhost', 'install') ?></p></td>
834 <td class="td_right">
835 <input type="text" size="40" name="dbhost" value="<?php p($INSTALL['dbhost']) ?>" />
836 </td>
837 </tr>
838 <tr>
839 <td class="td_left"><p><?php print_string('database', 'install') ?></p></td>
840 <td class="td_right">
841 <input type="text" size="40" name="dbname" value="<?php p($INSTALL['dbname']) ?>" />
842 </td>
843 </tr>
844 <tr>
845 <td class="td_left"><p><?php print_string('user') ?></p></td>
846 <td class="td_right">
847 <input type="text" size="40" name="dbuser" value="<?php p($INSTALL['dbuser']) ?>" />
848 </td>
849 </tr>
850 <tr>
851 <td class="td_left"><p><?php print_string('password') ?></p></td>
852 <td class="td_right">
853 <input type="password" size="40" name="dbpass" value="<?php p($INSTALL['dbpass']) ?>" />
854 </td>
855 </tr>
856 <tr>
857 <td class="td_left"><p><?php print_string('dbprefix', 'install') ?></p></td>
858 <td class="td_right">
859 <input type="text" size="40" name="prefix" value="<?php p($INSTALL['prefix']) ?>" />
860 </td>
861 </tr>
863 <?php
864 break;
865 case ADMIN: /// Administration directory setting
868 <tr>
869 <td class="td_left"><p><?php print_string('admindirname', 'install') ?></p></td>
870 <td class="td_right">
871 <input type="text" size="40" name="admindirname" value="<?php p($INSTALL['admindirname']) ?>" />
872 </td>
873 </tr>
876 <?php
877 break;
878 case ENVIRONMENT: /// Environment checks
881 <tr>
882 <td colspan="2">
883 <?php
884 error_reporting(0); // Hide errors
885 $dbconnected = $db->Connect($INSTALL['dbhost'],$INSTALL['dbuser'],$INSTALL['dbpass'],$INSTALL['dbname']);
886 error_reporting(7); // Show errors
887 if ($dbconnected) {
888 /// Execute environment check, printing results
889 check_moodle_environment($INSTALL['release'], $environment_results, true);
890 } else {
891 /// We never should reach this because DB has been tested before arriving here
892 $errormsg = get_string('dbconnectionerror', 'install');
893 $nextstage = DATABASE;
894 echo '<p class="errormsg" align="center">'.get_string('dbconnectionerror', 'install').'</p>';
897 </td>
898 </tr>
900 <?php
901 break;
902 case DOWNLOADLANG: /// Download language from download.moodle.org
905 <tr>
906 <td colspan="2">
907 <?php
908 /// Get array of languages, we are going to use it
909 $languages=get_installer_list_of_languages();
910 /// Print the download form (button) if necessary
911 if ($INSTALL['showdownloadlangpack'] == true && substr($INSTALL['language'],0,2) != 'en') {
912 $options = array();
913 $options['downloadlangpack'] = true;
914 $options['stage'] = DOWNLOADLANG;
915 $options['same'] = true;
916 print_simple_box_start('center');
917 print_single_button('install.php', $options, get_string('downloadlanguagebutton','install', $languages[$INSTALL['language']]), 'POST');
918 print_simple_box_end();
919 } else {
920 /// Show result info
921 /// English lang packs aren't downloaded
922 if (substr($INSTALL['language'],0,2) == 'en') {
923 print_simple_box(get_string('downloadlanguagenotneeded', 'install', $languages[$INSTALL['language']]), 'center', '80%');
924 } else {
925 if ($INSTALL['downloadlangpackerror']) {
926 echo "<p class=\"errormsg\" align=\"center\">".$INSTALL['downloadlangpackerror']."</p>\n";
927 print_simple_box(get_string('langdownloaderror', 'install', $languages[$INSTALL['language']]), 'center', '80%');
928 } else {
929 print_simple_box(get_string('langdownloadok', 'install', $languages[$INSTALL['language']]), 'center', '80%');
934 </td>
935 </tr>
937 <?php
938 break;
939 default:
943 <tr>
944 <td colspan="<?php echo ($nextstage == COMPATIBILITY) ? 3 : 2; ?>">
946 <?php
947 if ($needtoopenform) {
949 <form id="installform" method="post" action="<?php echo $formaction ?>">
950 <input type="hidden" name="stage" value="<?php echo $nextstage ?>" />
951 <?php
955 <?php echo ($nextstage < SAVE) ? "<input type=\"submit\" name=\"next\" value=\"".get_string('next')." &raquo;\" style=\"float: ".fix_align_rtl("right")."\"/>\n" : "&nbsp;\n" ?>
956 <?php echo ($nextstage > WELCOME) ? "<input type=\"submit\" name=\"prev\" value=\"&laquo; ".get_string('previous')."\" style=\"float: ".fix_align_rtl("left")."\"/>\n" : "&nbsp;\n" ?>
958 <?php
959 if ($needtoopenform) {
961 </form>
962 <?php
967 </td>
969 </tr>
971 </table>
972 <?php
973 if (!$needtoopenform) {
975 </form>
976 <?php
980 <?php
985 //==========================================================================//
987 function print_compatibility_row($success, $testtext, $errormessage, $helpfield='', $caution=false) {
988 echo "<tr>\n";
989 echo "<td class=\"td_left\" valign=\"top\" nowrap width=\"160\"><p>$testtext</p></td>\n";
990 if ($success) {
991 echo "<td valign=\"top\"><p class=\"p_pass\">".get_string('pass', 'install')."</p></td>\n";
992 echo "<td valign=\"top\">&nbsp;</td>\n";
993 } else {
994 echo "<td valign=\"top\"";
995 echo ($caution) ? "<p class=\"p_caution\">".get_string('caution', 'install') : "<p class=\"p_fail\">".get_string('fail', 'install');
996 echo "</p></td>\n";
997 echo "<td valign=\"top\">";
998 echo "<p>$errormessage ";
999 install_helpbutton("install.php?help=$helpfield");
1000 echo "</p></td>\n";
1002 echo "</tr>\n";
1003 return $success;
1007 //==========================================================================//
1009 function install_helpbutton($url, $title='') {
1010 if ($title == '') {
1011 $title = get_string('help');
1013 echo "<a href=\"javascript: void(0)\">";
1014 echo "<img src=\"pix/help.gif\" class=\"iconhelp\" alt=\"$title\" title=\"$title\" ";
1015 echo "onClick=\"return window.open('$url', 'Help', 'menubar=0,location=0,scrollbars,resizable,width=500,height=400')\">";
1016 echo "</a>\n";
1021 //==========================================================================//
1023 function print_install_help($help) {
1024 switch ($help) {
1025 case 'phpversionhelp':
1026 print_string($help, 'install', phpversion());
1027 break;
1028 case 'memorylimithelp':
1029 print_string($help, 'install', get_memory_limit());
1030 break;
1031 default:
1032 print_string($help, 'install');
1037 //==========================================================================//
1039 function get_memory_limit() {
1040 if ($limit = ini_get('memory_limit')) {
1041 return $limit;
1042 } else {
1043 return get_cfg_var('memory_limit');
1047 //==========================================================================//
1049 function check_memory_limit() {
1051 /// if limit is already 40 or more then we don't care if we can change it or not
1052 if ((int)str_replace('M', '', get_memory_limit()) >= 40) {
1053 return true;
1056 /// Otherwise, see if we can change it ourselves
1057 @ini_set('memory_limit', '40M');
1058 return ((int)str_replace('M', '', get_memory_limit()) >= 40);
1061 //==========================================================================//
1063 function inst_check_php_version() {
1064 if (!check_php_version("4.3.0")) {
1065 return false;
1066 } else if (check_php_version("5.0.0")) {
1067 return check_php_version("5.1.0"); // 5.0.x is too buggy
1069 return true; // 4.3.x or 4.4.x is fine
1071 //==========================================================================//
1073 /* This function returns a list of languages and their full names. The
1074 * list of available languages is fetched from install/lang/xx/installer.php
1075 * and it's used exclusively by the installation process
1076 * @return array An associative array with contents in the form of LanguageCode => LanguageName
1078 function get_installer_list_of_languages() {
1080 global $CFG;
1082 $languages = array();
1084 /// Get raw list of lang directories
1085 $langdirs = get_list_of_plugins('install/lang');
1086 asort($langdirs);
1087 /// Get some info from each lang
1088 foreach ($langdirs as $lang) {
1089 if (file_exists($CFG->dirroot .'/install/lang/'. $lang .'/installer.php')) {
1090 include($CFG->dirroot .'/install/lang/'. $lang .'/installer.php');
1091 if (substr($lang, -5) == '_utf8') { //Remove the _utf8 suffix from the lang to show
1092 $shortlang = substr($lang, 0, -5);
1093 } else {
1094 $shortlang = $lang;
1096 if ($lang == 'en') { //Explain this is non-utf8 en
1097 $shortlang = 'non-utf8 en';
1099 if (!empty($string['thislanguage'])) {
1100 $languages[$lang] = $string['thislanguage'] .' ('. $shortlang .')';
1102 unset($string);
1105 /// Return array
1106 return $languages;
1109 //==========================================================================//
1111 function css_styles() {
1114 <style type="text/css">
1116 body { background-color: #ffeece; }
1117 p, li, td {
1118 font-family: helvetica, arial, sans-serif;
1119 font-size: 10pt;
1121 a { text-decoration: none; color: blue; }
1122 a img {
1123 border: none;
1125 .errormsg {
1126 color: red;
1127 font-weight: bold;
1129 blockquote {
1130 font-family: courier, monospace;
1131 font-size: 10pt;
1133 .install_table {
1134 width: 500px;
1136 .td_left {
1137 text-align: <?php echo fix_align_rtl("right") ?>;
1138 font-weight: bold;
1140 .td_right {
1141 text-align: <?php echo fix_align_rtl("left") ?>;
1143 .main {
1144 width: 500px;
1145 border-width: 1px;
1146 border-style: solid;
1147 border-color: #ffc85f;
1148 -moz-border-radius-bottomleft: 15px;
1149 -moz-border-radius-bottomright: 15px;
1151 .td_mainheading {
1152 background-color: #fee6b9;
1153 padding: 10px;
1155 .td_main {
1156 text-align: center;
1158 .td_mainlogo {
1160 .p_mainlogo {
1162 .p_mainheading {
1163 font-size: 11pt;
1165 .p_subheading {
1166 font-size: 10pt;
1167 padding: 10px;
1169 .p_mainheader{
1170 text-align: right;
1171 font-size: 20pt;
1172 font-weight: bold;
1174 .p_pass {
1175 color: green;
1176 font-weight: bold;
1178 .p_fail {
1179 color: red;
1180 font-weight: bold;
1182 .p_caution {
1183 color: #ff6600;
1184 font-weight: bold;
1186 .p_help {
1187 text-align: center;
1188 font-family: helvetica, arial, sans-serif;
1189 font-size: 14pt;
1190 font-weight: bold;
1191 color: #333333;
1193 .environmenttable {
1194 font-size: 10pt;
1195 border-color: #ffc85f;
1197 table.environmenttable .error {
1198 background-color : red;
1199 color : inherit;
1202 table.environmenttable .warn {
1203 background-color : yellow;
1206 table.environmenttable .ok {
1207 background-color : lightgreen;
1209 .header {
1210 background-color: #fee6b9;
1211 font-size: 10pt;
1213 .cell {
1214 background-color: #ffeece;
1215 font-size: 10pt;
1217 .error {
1218 color: #ff0000;
1220 .errorboxcontent {
1221 text-align: center;
1222 font-weight: bold;
1223 padding: 20px;
1224 color: #ff0000;
1226 .invisiblefieldset {
1227 display:inline;
1228 border:0px;
1229 padding:0px;
1230 margin:0px;
1232 #mysql, #mysqli, #postgres7, #mssql, #mssql_n, #odbc_mssql, #oci8po {
1233 display: none;
1236 </style>
1238 <?php
1241 //==========================================================================//
1243 function database_js() {
1246 <script type="text/javascript" defer="defer">
1247 function toggledbinfo() {
1248 //Calculate selected value
1249 var showid = 'mysql';
1250 if (document.getElementById('installform').dbtype.value) {
1251 showid = document.getElementById('installform').dbtype.value;
1253 if (document.getElementById) {
1254 //Hide all the divs
1255 document.getElementById('mysql').style.display = '';
1256 document.getElementById('mysqli').style.display = '';
1257 document.getElementById('postgres7').style.display = '';
1258 document.getElementById('mssql').style.display = '';
1259 document.getElementById('mssql_n').style.display = '';
1260 document.getElementById('odbc_mssql').style.display = '';
1261 document.getElementById('oci8po').style.display = '';
1262 //Show the selected div
1263 document.getElementById(showid).style.display = 'block';
1264 } else if (document.all) {
1265 //This is the way old msie versions work
1266 //Hide all the divs
1267 document.all['mysql'].style.display = '';
1268 document.all['mysqli'].style.display = '';
1269 document.all['postgres7'].style.display = '';
1270 document.all['mssql'].style.display = '';
1271 document.all['mssql_n'].style.display = '';
1272 document.all['odbc_mssql'].style.display = '';
1273 document.all['oci8po'].style.display = '';
1274 //Show the selected div
1275 document.all[showid].style.display = 'block';
1276 } else if (document.layers) {
1277 //This is the way nn4 works
1278 //Hide all the divs
1279 document.layers['mysql'].style.display = '';
1280 document.layers['mysqli'].style.display = '';
1281 document.layers['postgres7'].style.display = '';
1282 document.layers['mssql'].style.display = '';
1283 document.layers['mssql_n'].style.display = '';
1284 document.layers['odbc_mssql'].style.display = '';
1285 document.layers['oci8po'].style.display = '';
1286 //Show the selected div
1287 document.layers[showid].style.display = 'block';
1290 </script>
1292 <?php
1296 * Add slashes for single quotes and backslashes
1297 * so they can be included in single quoted string
1298 * (for config.php)
1300 function addsingleslashes($input){
1301 return preg_replace("/(['\\\])/", "\\\\$1", $input);