Added new string for duplicate emails in lost password recovery.
[moodle-linuxchix.git] / install.php
blobcebba41791acecd29b1102c71041ec1f7cc79586
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 /// 0. Welcome and language settings
16 /// 1. Compatibility
17 /// 2. Database settings
18 /// 3. Host settings
19 /// 4. Administration directory name
20 /// 5. Save or display the settings
21 /// 6. Redirect to index.php
22 ///==========================================================================//
26 /// Begin the session as we are holding all information in a session
27 /// variable until the end.
29 session_name('MoodleSession');
30 @session_start();
32 if (! isset($_SESSION['INSTALL'])) {
33 $_SESSION['INSTALL'] = array();
36 $INSTALL = &$_SESSION['INSTALL']; // Makes it easier to reference
39 /// If it's our first time through this script then we need to set some default values
41 if ( empty($INSTALL['language']) and empty($_POST['language']) ) {
43 /// set defaults
44 $INSTALL['language'] = 'en';
46 $INSTALL['dbhost'] = 'localhost';
47 $INSTALL['dbuser'] = '';
48 $INSTALL['dbpass'] = '';
49 $INSTALL['dbtype'] = 'mysql';
50 $INSTALL['dbname'] = 'moodle';
51 $INSTALL['prefix'] = 'mdl_';
53 $INSTALL['wwwroot'] = '';
54 $INSTALL['dirroot'] = dirname(__FILE__);
55 $INSTALL['dataroot'] = dirname(dirname(__FILE__)) . '/moodledata';
57 $INSTALL['admindirname'] = 'admin';
59 $INSTALL['stage'] = 0;
64 //==========================================================================//
66 /// Fake some settings so that we can use selected functions from moodlelib.php and weblib.php
68 $SESSION->lang = (!empty($_POST['language'])) ? $_POST['language'] : $INSTALL['language'];
69 $CFG->dirroot = $INSTALL['dirroot'];
70 $CFG->dataroot = $INSTALL['dataroot'];
71 $CFG->directorypermissions = 00777;
74 /// Include some moodle libraries
76 require_once('./lib/moodlelib.php');
77 require_once('./lib/weblib.php');
78 require_once('./lib/adodb/adodb.inc.php');
81 /// guess the www root
82 if ($INSTALL['wwwroot'] == '') {
83 list($INSTALL['wwwroot'], $xtra) = explode('/install.php', qualified_me());
86 $stagetext = array(0 => get_string('chooselanguage', 'install'),
87 get_string('compatibilitysettings', 'install'),
88 get_string('directorysettings', 'install'),
89 get_string('databasesettings', 'install'),
90 get_string('admindirsetting', 'install'),
91 get_string('configurationcomplete', 'install')
97 //==========================================================================//
99 /// Are we in help mode?
101 if (isset($_GET['help'])) {
102 $nextstage = -1;
107 //==========================================================================//
109 /// Are we in config download mode?
111 if (isset($_GET['download'])) {
112 header("Content-Type: application/download\n");
113 header("Content-Disposition: attachment; filename=\"config.php\"");
114 echo $INSTALL['config'];
115 exit;
120 //==========================================================================//
122 /// Was data submitted?
124 if (isset($_POST['stage'])) {
126 /// Get the stage for which the form was set and the next stage we are going to
129 if ( $goforward = (! empty( $_POST['next'] )) ) {
130 $nextstage = $_POST['stage'] + 1;
131 } else {
132 $nextstage = $_POST['stage'] - 1;
135 if ($nextstage < 0) $nextstage = 0;
138 /// Store any posted data
139 foreach ($_POST as $setting=>$value) {
140 $INSTALL[$setting] = $value;
143 } else {
145 $goforward = true;
146 $nextstage = 0;
152 //==========================================================================//
154 /// Check the directory settings
156 if ($INSTALL['stage'] == 2) {
158 error_reporting(0);
161 /// check dirroot
162 if (($fh = @fopen($INSTALL['dirroot'].'/install.php', 'r')) === false ) {
163 $CFG->dirroot = dirname(__FILE__);
164 $INSTALL['dirroot'] = dirname(__FILE__);
165 $errormsg .= get_string('dirrooterror', 'install').'<br />';
167 if ($fh) fclose($fh);
169 $CFG->dirroot = $INSTALL['dirroot'];
171 /// check wwwroot
172 if (ini_get('allow_url_fopen')) {
173 if (($fh = @fopen($INSTALL['wwwroot'].'/install.php', 'r')) === false) {
174 $errormsg .= get_string('wwwrooterror', 'install').'<br />';
177 if ($fh) fclose($fh);
179 /// check dataroot
180 $CFG->dataroot = $INSTALL['dataroot'];
181 if (make_upload_directory('sessions', false) === false ) {
182 $errormsg = get_string('datarooterror', 'install').'<br />';
184 if ($fh) fclose($fh);
186 if (!empty($errormsg)) $nextstage = 2;
188 error_reporting(7);
193 //==========================================================================//
195 /// Check database settings if stage 3 data submitted
196 /// Try to connect to the database. If that fails then try to create the database
198 if ($INSTALL['stage'] == 3) {
200 if (empty($INSTALL['dbname'])) {
201 $INSTALL['dbname'] = 'moodle';
204 /// different format for postgres7 by socket
205 if ($INSTALL['dbtype'] == 'postgres7' and ($INSTALL['dbhost'] == 'localhost' || $INSTALL['dbhost'] == '127.0.0.1')) {
206 $INSTALL['dbhost'] = "user='{$INSTALL['dbuser']}' password='{$INSTALL['dbpass']}' dbname='{$INSTALL['dbname']}'";
207 $INSTALL['dbuser'] = '';
208 $INSTALL['dbpass'] = '';
209 $INSTALL['dbname'] = '';
211 if ($INSTALL['prefix'] == '') { /// must have a prefix
212 $INSTALL['prefix'] = 'mdl_';
216 if ($INSTALL['dbtype'] == 'mysql') { /// Check MySQL extension is present
217 if (!extension_loaded('mysql')) {
218 $errormsg = get_string('mysqlextensionisnotpresentinphp', 'install');
219 $nextstage = 3;
223 if (empty($errormsg)) {
225 $db = &ADONewConnection($INSTALL['dbtype']);
227 error_reporting(0); // Hide errors
229 if (! $dbconnected = $db->Connect($INSTALL['dbhost'],$INSTALL['dbuser'],$INSTALL['dbpass'],$INSTALL['dbname'])) {
230 /// The following doesn't seem to work but we're working on it
231 /// If you come up with a solution for creating a database in MySQL
232 /// feel free to put it in and let us know
233 if ($dbconnected = $db->Connect($INSTALL['dbhost'],$INSTALL['dbuser'],$INSTALL['dbpass'])) {
234 switch ($INSTALL['dbtype']) { /// Try to create a database
235 case 'mysql':
236 if ($db->Execute("CREATE DATABASE {$INSTALL['dbname']};")) {
237 $dbconnected = $db->Connect($INSTALL['dbhost'],$INSTALL['dbuser'],$INSTALL['dbpass'],$INSTALL['dbname']);
238 } else {
239 $errormsg = get_string('dbcreationerror', 'install');
240 $nextstage = 3;
242 break;
248 error_reporting(7);
250 if (($dbconnected === false) and (empty($errormsg)) ) {
251 $errormsg = get_string('dbconnectionerror', 'install');
252 $nextstage = 3;
258 //==========================================================================//
260 /// If the next stage is admin directory settings OR we have just come from there then
261 /// check the admin directory.
262 /// If we can open a file then we know that the admin name is correct.
264 if ($nextstage == 4 or $INSTALL['stage'] == 4) {
265 if (!ini_get('allow_url_fopen')) {
266 $nextstage = ($goforward) ? 5 : 3;
267 } else if (($fh = @fopen($INSTALL['wwwroot'].'/'.$INSTALL['admindirname'].'/site.html', 'r')) !== false) {
268 $nextstage = ($goforward) ? 5 : 3;
269 fclose($fh);
270 } else {
271 if ($nextstage != 4) {
272 $errormsg = get_string('admindirerror', 'install');
273 $nextstage = 4;
280 //==========================================================================//
282 /// Display or print the data
283 /// Put the data into a string
284 /// Try to open config file for writing.
286 if ($nextstage == 5) {
288 $str = '<?php /// Moodle Configuration File '."\r\n";
289 $str .= "\r\n";
291 $str .= 'unset($CFG);'."\r\n";
292 $str .= "\r\n";
294 $str .= '$CFG->dbtype = \''.$INSTALL['dbtype']."';\r\n";
295 $str .= '$CFG->dbhost = \''.addslashes($INSTALL['dbhost'])."';\r\n";
296 if (!empty($INSTALL['dbname'])) {
297 $str .= '$CFG->dbname = \''.$INSTALL['dbname']."';\r\n";
298 $str .= '$CFG->dbuser = \''.$INSTALL['dbuser']."';\r\n";
299 $str .= '$CFG->dbpass = \''.$INSTALL['dbpass']."';\r\n";
301 $str .= '$CFG->dbpersist = false;'."\r\n";
302 $str .= '$CFG->prefix = \''.$INSTALL['prefix']."';\r\n";
303 $str .= "\r\n";
305 $str .= '$CFG->wwwroot = \''.$INSTALL['wwwroot']."';\r\n";
306 $str .= '$CFG->dirroot = \''.$INSTALL['dirroot']."';\r\n";
307 $str .= '$CFG->dataroot = \''.$INSTALL['dataroot']."';\r\n";
308 $str .= '$CFG->admin = \''.$INSTALL['admindirname']."';\r\n";
309 $str .= "\r\n";
311 $str .= '$CFG->directorypermissions = 00777; // try 02777 on a server in Safe Mode'."\r\n";
312 $str .= "\r\n";
314 $str .= 'require_once("$CFG->dirroot/lib/setup.php");'."\r\n";
315 $str .= '// MAKE SURE WHEN YOU EDIT THIS FILE THAT THERE ARE NO SPACES, BLANK LINES,'."\r\n";
316 $str .= '// RETURNS, OR ANYTHING ELSE AFTER THE TWO CHARACTERS ON THE NEXT LINE.'."\r\n";
317 $str .= '?>';
319 umask(0137);
321 if (( $configsuccess = ($fh = @fopen($configfile, 'w')) ) !== false) {
322 fwrite($fh, $str);
323 fclose($fh);
327 $INSTALL['config'] = $str;
332 //==========================================================================//
338 <html dir="<?php echo (get_string('this_direction') == 'rtl') ? 'rtl' : 'ltr' ?>">
339 <head>
340 <link rel="shortcut icon" href="theme/standard/favicon.ico" />
341 <title>Moodle Install</title>
342 <meta http-equiv="content-type" content="text/html; charset=<?php print_string('thischarset') ?>" />
343 <?php css_styles() ?>
345 </head>
347 <body>
350 <?php
351 if (isset($_GET['help'])) {
352 print_install_help($_GET['help']);
353 close_window_button();
354 } else {
358 <table class="main" align="center" cellpadding="3" cellspacing="0">
359 <tr>
360 <td class="td_mainlogo">
361 <p class="p_mainlogo"><img src="pix/moodlelogo-med.gif" width="240" height="60" alt=\"\"></p>
362 </td>
363 <td class="td_mainlogo" valign="bottom">
364 <p class="p_mainheader"><?php print_string('installation', 'install') ?></p>
365 </td>
366 </tr>
368 <tr>
369 <td class="td_mainheading" colspan="2">
370 <p class="p_mainheading"><?php echo $stagetext[$nextstage] ?></p>
371 </td>
372 </tr>
374 <tr>
375 <td class="td_main" colspan="2">
377 <?php
379 if (!empty($errormsg)) echo "<p class=\"errormsg\" align=\"center\">$errormsg</p>\n";
382 if ($nextstage == 5) {
383 $INSTALL['stage'] = 0;
384 $options = array();
385 $options['lang'] = $INSTALL['language'];
386 if ($configsuccess) {
387 echo "<p>".get_string('configfilewritten', 'install')."</p>\n";
389 echo "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"100%\">\n";
390 echo "<tr>\n";
391 echo "<td width=\"33.3%\">&nbsp;</td>\n";
392 echo "<td width=\"33.3%\">&nbsp;</td>\n";
393 echo "<td width=\"33.3%\" align=\"right\">\n";
394 print_single_button("index.php", $options, get_string('continue')." &raquo;");
395 echo "</td>\n";
396 echo "</tr>\n";
397 echo "</table>\n";
399 } else {
400 echo "<p class=\"errormsg\">".get_string('configfilenotwritten', 'install')."</p>";
402 echo "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"100%\">\n";
403 echo "<tr>\n";
404 echo "<td width=\"33.3%\">&nbsp;</td>\n";
405 echo "<td width=\"33.3%\" align=\"center\">\n";
406 $installoptions = array();
407 $installoptions['download'] = 1;
408 print_single_button("install.php", $installoptions, get_string('download', 'install'));
409 echo "</td>\n";
410 echo "<td width=\"33.3%\" align=\"right\">\n";
411 print_single_button("index.php", $options, get_string('continue')." &raquo;");
412 echo "</td>\n";
413 echo "</tr>\n";
414 echo "</table>\n";
416 echo "<hr />\n";
417 echo "<div style=\"text-align: left\">\n";
418 print_object(htmlentities($str));
419 echo "</div>\n";
421 } else {
422 $formaction = (isset($_GET['configfile'])) ? "install.php?configfile=".$_GET['configfile'] : "install.php";
423 form_table($nextstage, $formaction);
428 </td>
429 </tr>
430 </table>
432 <?php
436 </body>
437 </html>
448 <?php
450 //==========================================================================//
453 function print_object($object) {
454 echo "<pre>\n";
455 print_r($object);
456 echo "</pre>\n";
461 //==========================================================================//
463 function form_table($nextstage = 0, $formaction = "install.php") {
464 global $INSTALL;
466 /// standard lines for all forms
469 <form name="installform" method="post" action="<?php echo $formaction ?>">
470 <input type="hidden" name="stage" value="<?php echo $nextstage ?>" />
471 <table class="install_table" cellspacing="3" cellpadding="3" align="center">
473 <?php
474 /// what we do depends on the stage we're at
475 switch ($nextstage) {
476 case 0: /// Language settings
478 <tr>
479 <td class="td_left"><p><?php print_string('language') ?></p></td>
480 <td class="td_right">
481 <?php choose_from_menu (get_list_of_languages(), 'language', $INSTALL['language'], '') ?>
482 </td>
483 </tr>
485 <?php
486 break;
487 case 1: /// Compatibilty check
488 $compatsuccess = true;
490 /// Check that PHP is of a sufficient version
491 print_compatibility_row(check_php_version("4.1.0"), get_string('phpversion', 'install'), get_string('phpversionerror', 'install'), 'phpversionhelp');
492 /// Check session auto start
493 print_compatibility_row(!ini_get_bool('session.auto_start'), get_string('sessionautostart', 'install'), get_string('sessionautostarterror', 'install'), 'sessionautostarthelp');
494 /// Check magic quotes
495 print_compatibility_row(!ini_get_bool('magic_quotes_runtime'), get_string('magicquotesruntime', 'install'), get_string('magicquotesruntimeerror', 'install'), 'magicquotesruntimehelp');
496 /// Check safe mode
497 print_compatibility_row(!ini_get_bool('safe_mode'), get_string('safemode', 'install'), get_string('safemodeerror', 'install'), 'safemodehelp', true);
498 /// Check file uploads
499 print_compatibility_row(ini_get_bool('file_uploads'), get_string('fileuploads', 'install'), get_string('fileuploadserror', 'install'), 'fileuploadshelp', true);
500 /// Check GD version
501 print_compatibility_row(check_gd_version(), get_string('gdversion', 'install'), get_string('gdversionerror', 'install'), 'gdversionhelp', true);
502 /// Check memory limit
503 print_compatibility_row(check_memory_limit(), get_string('memorylimit', 'install'), get_string('memorylimiterror', 'install'), 'memorylimithelp', true);
506 break;
507 case 2: /// Directory settings
510 <tr>
511 <td class="td_left"><p><?php print_string('wwwroot', 'install') ?></p></td>
512 <td class="td_right">
513 <input type="text" size="40"name="wwwroot" value="<?php echo $INSTALL['wwwroot'] ?>" />
514 </td>
515 </tr>
516 <tr>
517 <td class="td_left"><p><?php print_string('dirroot', 'install') ?></p></td>
518 <td class="td_right">
519 <input type="text" size="40" name="dirroot" value="<?php echo $INSTALL['dirroot'] ?>" />
520 </td>
521 </tr>
522 <tr>
523 <td class="td_left"><p><?php print_string('dataroot', 'install') ?></p></td>
524 <td class="td_right">
525 <input type="text" size="40" name="dataroot" value="<?php echo $INSTALL['dataroot'] ?>" />
526 </td>
527 </tr>
529 <?php
530 break;
531 case 3: /// Database settings
534 <tr>
535 <td class="td_left"><p><?php print_string('dbtype', 'install') ?></p></td>
536 <td class="td_right">
537 <?php choose_from_menu (array("mysql" => "mysql", "postgres7" => "postgres7"), 'dbtype', $INSTALL['dbtype'], '') ?>
538 </td>
539 </tr>
540 <tr>
541 <td class="td_left"><p><?php print_string('dbhost', 'install') ?></p></td>
542 <td class="td_right">
543 <input type="text" size="40" name="dbhost" value="<?php echo $INSTALL['dbhost'] ?>" />
544 </td>
545 </tr>
546 <tr>
547 <td class="td_left"><p><?php print_string('database', 'install') ?></p></td>
548 <td class="td_right">
549 <input type="text" size="40" name="dbname" value="<?php echo $INSTALL['dbname'] ?>" />
550 </td>
551 </tr>
552 <tr>
553 <td class="td_left"><p><?php print_string('user') ?></p></td>
554 <td class="td_right">
555 <input type="text" size="40" name="dbuser" value="<?php echo $INSTALL['dbuser'] ?>" />
556 </td>
557 </tr>
558 <tr>
559 <td class="td_left"><p><?php print_string('password') ?></p></td>
560 <td class="td_right">
561 <input type="password" size="40" name="dbpass" value="<?php echo $INSTALL['dbpass'] ?>" />
562 </td>
563 </tr>
564 <tr>
565 <td class="td_left"><p><?php print_string('dbprefix', 'install') ?></p></td>
566 <td class="td_right">
567 <input type="text" size="40" name="prefix" value="<?php echo $INSTALL['prefix'] ?>" />
568 </td>
569 </tr>
571 <?php
572 break;
573 case 4: /// Administration directory setting
576 <tr>
577 <td class="td_left"><p><?php print_string('admindirname', 'install') ?></p></td>
578 <td class="td_right">
579 <input type="text" size="40" name="admindirname" value="<?php echo $INSTALL['admindirname'] ?>" />
580 </td>
581 </tr>
584 <?php
585 break;
586 default:
590 <tr>
591 <td colspan="<?php echo ($nextstage == 1) ? '3' : '2'; ?>">
593 <?php echo ($nextstage < 5) ? "<input type=\"submit\" name=\"next\" value=\"".get_string('next')." &raquo;\" style=\"float: right\"/>\n" : "&nbsp;\n" ?>
594 <?php echo ($nextstage > 0) ? "<input type=\"submit\" name=\"prev\" value=\"&laquo; ".get_string('previous')."\" style=\"float: left\"/>\n" : "&nbsp;\n" ?>
597 </td>
599 </tr>
601 </table>
602 </form>
604 <?php
609 //==========================================================================//
611 function print_compatibility_row($success, $testtext, $errormessage, $helpfield='', $caution=false) {
612 echo "<tr>\n";
613 echo "<td class=\"td_left\" valign=\"top\" nowrap width=\"160\"><p>$testtext</p></td>\n";
614 if ($success) {
615 echo "<td valign=\"top\"><p class=\"p_pass\">".get_string('pass', 'install')."</p></td>\n";
616 echo "<td valign=\"top\">&nbsp;</td>\n";
617 } else {
618 echo "<td valign=\"top\"";
619 echo ($caution) ? "<p class=\"p_caution\">".get_string('caution', 'install') : "<p class=\"p_fail\">".get_string('fail', 'install');
620 echo "</p></td>\n";
621 echo "<td valign=\"top\">";
622 echo "<p>$errormessage ";
623 install_helpbutton("install.php?help=$helpfield");
624 echo "</p></td>\n";
626 echo "</tr>\n";
627 return $success;
631 //==========================================================================//
633 function install_helpbutton($url, $title='') {
634 if ($title == '') {
635 $title = get_string('help');
637 echo "<a href=\"javascript: void(0)\">";
638 echo "<img src=\"./pix/help.gif\" height=\"17\" width=\"17\" alt=\"$title\"";
639 echo "border=\"0\" align=\"middle\" title=\"$title\" ";
640 echo "onClick=\"return window.open('$url', 'Help', 'menubar=0,location=0,scrollbars,resizable,width=500,height=400')\">";
641 echo "</a>\n";
646 //==========================================================================//
648 function print_install_help($help) {
649 switch ($help) {
650 case 'phpversionhelp':
651 print_string($help, 'install', phpversion());
652 break;
653 case 'memorylimithelp':
654 print_string($help, 'install', get_memory_limit());
655 break;
656 default:
657 print_string($help, 'install');
662 //==========================================================================//
664 function get_memory_limit() {
665 if ($limit = ini_get('memory_limit')) {
666 return $limit;
667 } else {
668 return get_cfg_var('memory_limit');
672 //==========================================================================//
674 function check_memory_limit() {
676 /// if limit is already 16M or more then we don't care if we can change it or not
677 if ((int)str_replace('M', '', get_memory_limit()) >= 16) {
678 return true;
681 /// Otherwise, see if we can change it ourselves
682 @ini_set('memory_limit', '16M');
683 return ((int)str_replace('M', '', get_memory_limit()) >= 16);
686 //==========================================================================//
688 function css_styles() {
691 <style type="text/css">
693 body { background-color: #ffeece; }
694 p, li {
695 font-family: helvetica, arial, sans-serif;
696 font-size: 10pt;
698 a { text-decoration: none; color: blue; }
699 .errormsg {
700 color: red;
701 font-weight: bold;
703 blockquote {
704 font-family: courier, monospace;
705 font-size: 10pt;
707 .install_table {
708 width: 500px;
710 .td_left {
711 text-align: right;
712 font-weight: bold;
714 .td_right {
715 text-align: left;
717 .main {
718 width: 500px;
719 border-width: 1px;
720 border-style: solid;
721 border-color: #ffc85f;
722 -moz-border-radius-bottomleft: 15px;
723 -moz-border-radius-bottomright: 15px;
725 .td_mainheading {
726 background-color: #fee6b9;
727 padding: 10px;
729 .td_main {
730 text-align: center;
732 .td_mainlogo {
734 .p_mainlogo {
736 .p_mainheading {
737 font-size: 11pt;
739 .p_mainheader{
740 text-align: right;
741 font-size: 20pt;
742 font-weight: bold;
744 .p_pass {
745 color: green;
746 font-weight: bold;
748 .p_fail {
749 color: red;
750 font-weight: bold;
752 .p_caution {
753 color: #ff6600;
754 font-weight: bold;
756 .p_help {
757 text-align: center;
758 font-family: helvetica, arial, sans-serif;
759 font-size: 14pt;
760 font-weight: bold;
761 color: #333333;
764 </style>
766 <?php