Nullified courseimages.php, and made some of the security improvements
[moodle.git] / install.php
blob51d73f03ae785ead66a168674fbef43a5d2e9e16
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 = 0777;
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');
166 } else {
167 fclose($fh);
169 $CFG->dirroot = $INSTALL['dirroot'];
171 /// check wwwroot
172 if (($fh = @fopen($INSTALL['wwwroot'].'/install.php', 'r')) === false) {
173 $errormsg = get_string('wwwrooterror', 'install');
174 } else {
175 fclose($fh);
177 /// check dataroot
178 $CFG->dataroot = $INSTALL['dataroot'];
179 if (make_upload_directory('sessions', false) === false ) {
180 $errormsg = get_string('datarooterror', 'install');
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 $db = &ADONewConnection($INSTALL['dbtype']);
218 error_reporting(0); // Hide errors
220 if (! $dbconnected = $db->Connect($INSTALL['dbhost'],$INSTALL['dbuser'],$INSTALL['dbpass'],$INSTALL['dbname'])) {
221 /// The following doesn't seem to work but we're working on it
222 /// If you come up with a solution for creating a database in MySQL
223 /// feel free to put it in and let us know
224 if ($dbconnected = $db->Connect($INSTALL['dbhost'],$INSTALL['dbuser'],$INSTALL['dbpass'])) {
225 switch ($INSTALL['dbtype']) { /// Try to create a database
226 case 'mysql':
227 if ($db->Execute("CREATE DATABASE {$INSTALL['dbname']};")) {
228 $dbconnected = $db->Connect($INSTALL['dbhost'],$INSTALL['dbuser'],$INSTALL['dbpass'],$INSTALL['dbname']);
229 } else {
230 $errormsg = get_string('dbcreationerror', 'install');
231 $nextstage = 3;
233 break;
238 error_reporting(7);
240 if (($dbconnected === false) and (empty($errormsg)) ) {
241 $errormsg = get_string('dbconnectionerror', 'install');
242 $nextstage = 3;
248 //==========================================================================//
250 /// If the next stage is admin directory settings OR we have just come from there then
251 /// check the admin directory.
252 /// If we can open a file then we know that the admin name is correct.
254 if ($nextstage == 4 or $INSTALL['stage'] == 4) {
255 if (($fh = @fopen($INSTALL['wwwroot'].'/'.$INSTALL['admindirname'].'/site.html', 'r')) !== false) {
256 $nextstage = ($goforward) ? 5 : 3;
257 fclose($fh);
258 } else {
259 if ($nextstage != 4) {
260 $errormsg = get_string('admindirerror', 'install');
261 $nextstage = 4;
268 //==========================================================================//
270 /// Display or print the data
271 /// Put the data into a string
272 /// Try to open config file for writing.
274 if ($nextstage == 5) {
276 $str = '<?php /// Moodle Configuration File '."\r\n";
277 $str .= "\r\n";
279 $str .= 'unset($CFG);'."\r\n";
280 $str .= "\r\n";
282 $str .= '$CFG->dbtype = \''.$INSTALL['dbtype']."';\r\n";
283 $str .= '$CFG->dbhost = \''.addslashes($INSTALL['dbhost'])."';\r\n";
284 if (!empty($INSTALL['dbname'])) {
285 $str .= '$CFG->dbname = \''.$INSTALL['dbname']."';\r\n";
286 $str .= '$CFG->dbuser = \''.$INSTALL['dbuser']."';\r\n";
287 $str .= '$CFG->dbpass = \''.$INSTALL['dbpass']."';\r\n";
289 $str .= '$CFG->dbpersist = false;'."\r\n";
290 $str .= '$CFG->prefix = \''.$INSTALL['prefix']."';\r\n";
291 $str .= "\r\n";
293 $str .= '$CFG->wwwroot = \''.$INSTALL['wwwroot']."';\r\n";
294 $str .= '$CFG->dirroot = \''.$INSTALL['dirroot']."';\r\n";
295 $str .= '$CFG->dataroot = \''.$INSTALL['dataroot']."';\r\n";
296 $str .= "\r\n";
298 $str .= '$CFG->directorypermissions = 0777;'."\r\n";
299 $str .= "\r\n";
301 $str .= 'require_once("$CFG->dirroot/lib/setup.php");'."\r\n";
302 $str .= '// MAKE SURE WHEN YOU EDIT THIS FILE THAT THERE ARE NO SPACES, BLANK LINES,'."\r\n";
303 $str .= '// RETURNS, OR ANYTHING ELSE AFTER THE TWO CHARACTERS ON THE NEXT LINE.'."\r\n";
304 $str .= '?>';
306 umask(0137);
308 if (( $configsuccess = ($fh = @fopen($configfile, 'w')) ) !== false) {
309 fwrite($fh, $str);
310 fclose($fh);
314 $INSTALL['config'] = $str;
319 //==========================================================================//
325 <html dir="<?php echo (get_string('this_direction') == 'rtl') ? 'rtl' : 'ltr' ?>">
326 <head>
327 <link rel="shortcut icon" href="http://moodle.dougiamas.net/theme/standard/favicon.ico" />
328 <title>Moodle Install</title>
329 <meta http-equiv="content-type" content="text/html; charset=<?php print_string('thischarset') ?>" />
330 <?php css_styles() ?>
332 </head>
334 <body>
337 <?php
338 if (isset($_GET['help'])) {
339 print_install_help($_GET['help']);
340 close_window_button();
341 } else {
345 <table class="main" align="center" cellpadding="3" cellspacing="0">
346 <tr>
347 <td class="td_mainlogo">
348 <p class="p_mainlogo"><img src="pix/moodlelogo-med.gif" width="240" height="60"></p>
349 </td>
350 <td class="td_mainlogo" valign="bottom">
351 <p class="p_mainheader"><?php print_string('installation', 'install') ?></p>
352 </td>
353 </tr>
355 <tr>
356 <td class="td_mainheading" colspan="2">
357 <p class="p_mainheading"><?php echo $stagetext[$nextstage] ?></p>
358 </td>
359 </tr>
361 <tr>
362 <td class="td_main" colspan="2">
364 <?php
366 if (!empty($errormsg)) echo "<p class=\"errormsg\" align=\"center\">$errormsg</p>\n";
369 if ($nextstage == 5) {
370 $INSTALL['stage'] = 0;
371 $options = array();
372 $options['lang'] = $INSTALL['language'];
373 if ($configsuccess) {
374 echo "<p>".get_string('configfilewritten', 'install')."</p>\n";
376 echo "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"100%\">\n";
377 echo "<tr>\n";
378 echo "<td width=\"33.3%\">&nbsp;</td>\n";
379 echo "<td width=\"33.3%\">&nbsp;</td>\n";
380 echo "<td width=\"33.3%\" align=\"right\">\n";
381 print_single_button("index.php", $options, get_string('continue')." &raquo;");
382 echo "</td>\n";
383 echo "</tr>\n";
384 echo "</table>\n";
386 } else {
387 echo "<p class=\"errormsg\">".get_string('configfilenotwritten', 'install')."</p>";
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%\" align=\"center\">\n";
393 $installoptions = array();
394 $installoptions['download'] = 1;
395 print_single_button("install.php", $installoptions, get_string('download', 'install'));
396 echo "</td>\n";
397 echo "<td width=\"33.3%\" align=\"right\">\n";
398 print_single_button("index.php", $options, get_string('continue')." &raquo;");
399 echo "</td>\n";
400 echo "</tr>\n";
401 echo "</table>\n";
403 echo "<hr />\n";
404 echo "<div style=\"text-align: left\">\n";
405 print_object(htmlentities($str));
406 echo "</div>\n";
408 } else {
409 $formaction = (isset($_GET['configfile'])) ? "install.php?configfile=".$_GET['configfile'] : "install.php";
410 form_table($nextstage, $formaction);
415 </td>
416 </tr>
417 </table>
419 <?php
423 </body>
424 </html>
435 <?php
437 //==========================================================================//
440 function print_object($object) {
441 echo "<pre>\n";
442 print_r($object);
443 echo "</pre>\n";
448 //==========================================================================//
450 function form_table($nextstage = 0, $formaction = "install.php") {
451 global $INSTALL;
453 /// standard lines for all forms
456 <form name="installform" method="post" action="<?php echo $formaction ?>">
457 <input type="hidden" name="stage" value="<?php echo $nextstage ?>" />
458 <table class="install_table" cellspacing="3" cellpadding="3" align="center">
460 <?php
461 /// what we do depends on the stage we're at
462 switch ($nextstage) {
463 case 0: /// Language settings
465 <tr>
466 <td class="td_left"><p><?php print_string('language') ?></p></td>
467 <td class="td_right">
468 <?php choose_from_menu (get_list_of_languages(), 'language', $INSTALL['language'], '') ?>
469 </td>
470 </tr>
472 <?php
473 break;
474 case 1: /// Compatibilty check
475 $compatsuccess = true;
477 /// Check that PHP is of a sufficient version
478 print_compatibility_row(check_php_version("4.1.0"), get_string('phpversion', 'install'), get_string('phpversionerror', 'install'), 'phpversionhelp');
479 /// Check session auto start
480 print_compatibility_row(!ini_get_bool('session.auto_start'), get_string('sessionautostart', 'install'), get_string('sessionautostarterror', 'install'), 'sessionautostarthelp');
481 /// Check magic quotes
482 print_compatibility_row(!ini_get_bool('magic_quotes_runtime'), get_string('magicquotesruntime', 'install'), get_string('magicquotesruntimeerror', 'install'), 'magicquotesruntimehelp');
483 /// Check safe mode
484 print_compatibility_row(!ini_get_bool('safe_mode'), get_string('safemode', 'install'), get_string('safemodeerror', 'install'), 'safemodehelp', true);
485 /// Check file uploads
486 print_compatibility_row(ini_get_bool('file_uploads'), get_string('fileuploads', 'install'), get_string('fileuploadserror', 'install'), 'fileuploadshelp', true);
487 /// Check GD version
488 print_compatibility_row(check_gd_version(), get_string('gdversion', 'install'), get_string('gdversionerror', 'install'), 'gdversionhelp', true);
489 /// Check memory limit
490 print_compatibility_row(check_memory_limit(), get_string('memorylimit', 'install'), get_string('memorylimiterror', 'install'), 'memorylimithelp', true);
493 break;
494 case 2: /// Directory settings
497 <tr>
498 <td class="td_left"><p><?php print_string('wwwroot', 'install') ?></p></td>
499 <td class="td_right">
500 <input type="text" size="40"name="wwwroot" value="<?php echo $INSTALL['wwwroot'] ?>" />
501 </td>
502 </tr>
503 <tr>
504 <td class="td_left"><p><?php print_string('dirroot', 'install') ?></p></td>
505 <td class="td_right">
506 <input type="text" size="40" name="dirroot" value="<?php echo $INSTALL['dirroot'] ?>" />
507 </td>
508 </tr>
509 <tr>
510 <td class="td_left"><p><?php print_string('dataroot', 'install') ?></p></td>
511 <td class="td_right">
512 <input type="text" size="40" name="dataroot" value="<?php echo $INSTALL['dataroot'] ?>" />
513 </td>
514 </tr>
516 <?php
517 break;
518 case 3: /// Database settings
521 <tr>
522 <td class="td_left"><p><?php print_string('dbtype', 'install') ?></p></td>
523 <td class="td_right">
524 <?php choose_from_menu (array("mysql" => "mysql", "postgres7" => "postgres7"), 'dbtype', $INSTALL['dbtype'], '') ?>
525 </td>
526 </tr>
527 <tr>
528 <td class="td_left"><p><?php print_string('dbhost', 'install') ?></p></td>
529 <td class="td_right">
530 <input type="text" size="40" name="dbhost" value="<?php echo $INSTALL['dbhost'] ?>" />
531 </td>
532 </tr>
533 <tr>
534 <td class="td_left"><p><?php print_string('database', 'install') ?></p></td>
535 <td class="td_right">
536 <input type="text" size="40" name="dbname" value="<?php echo $INSTALL['dbname'] ?>" />
537 </td>
538 </tr>
539 <tr>
540 <td class="td_left"><p><?php print_string('user') ?></p></td>
541 <td class="td_right">
542 <input type="text" size="40" name="dbuser" value="<?php echo $INSTALL['dbuser'] ?>" />
543 </td>
544 </tr>
545 <tr>
546 <td class="td_left"><p><?php print_string('password') ?></p></td>
547 <td class="td_right">
548 <input type="text" size="40" name="dbpass" value="<?php echo $INSTALL['dbpass'] ?>" />
549 </td>
550 </tr>
551 <tr>
552 <td class="td_left"><p><?php print_string('dbprefix', 'install') ?></p></td>
553 <td class="td_right">
554 <input type="text" size="40" name="prefix" value="<?php echo $INSTALL['prefix'] ?>" />
555 </td>
556 </tr>
558 <?php
559 break;
560 case 4: /// Administration directory setting
563 <tr>
564 <td class="td_left"><p><?php print_string('admindirname', 'install') ?></p></td>
565 <td class="td_right">
566 <input type="text" size="40" name="admindirname" value="<?php echo $INSTALL['admindirname'] ?>" />
567 </td>
568 </tr>
571 <?php
572 break;
573 default:
577 <tr>
578 <td colspan="<?php echo ($nextstage == 1) ? '3' : '2'; ?>">
580 <?php echo ($nextstage < 5) ? "<input type=\"submit\" name=\"next\" value=\"".get_string('next')." &raquo;\" style=\"float: right\"/>\n" : "&nbsp;\n" ?>
581 <?php echo ($nextstage > 0) ? "<input type=\"submit\" name=\"prev\" value=\"&laquo; ".get_string('previous')."\" style=\"float: left\"/>\n" : "&nbsp;\n" ?>
584 </td>
586 </tr>
588 </table>
589 </form>
591 <?php
596 //==========================================================================//
598 function print_compatibility_row($success, $testtext, $errormessage, $helpfield='', $caution=false) {
599 echo "<tr>\n";
600 echo "<td class=\"td_left\" valign=\"top\" nowrap width=\"160\"><p>$testtext</p></td>\n";
601 if ($success) {
602 echo "<td valign=\"top\"><p class=\"p_pass\">".get_string('pass', 'install')."</p></td>\n";
603 echo "<td valign=\"top\">&nbsp;</td>\n";
604 } else {
605 echo "<td valign=\"top\"";
606 echo ($caution) ? "<p class=\"p_caution\">".get_string('caution', 'install') : "<p class=\"p_fail\">".get_string('fail', 'install');
607 echo "</p></td>\n";
608 echo "<td valign=\"top\">";
609 echo "<p>$errormessage ";
610 install_helpbutton("install.php?help=$helpfield");
611 echo "</p></td>\n";
613 echo "</tr>\n";
614 return $success;
618 //==========================================================================//
620 function install_helpbutton($url, $title='') {
621 if ($title == '') {
622 $title = get_string('help');
624 echo "<a href=\"javascript: void(0)\">";
625 echo "<img src=\"./pix/help.gif\" height=\"17\" width=\"22\" alt=\"$title\"";
626 echo "border=\"0\" align=\"absmiddle\" title=\"$title\" ";
627 echo "onClick=\"return window.open('$url', 'Help', 'menubar=0,location=0,scrollbars,resizable,width=500,height=400')\">";
628 echo "</a>\n";
633 //==========================================================================//
635 function print_install_help($help) {
636 switch ($help) {
637 case 'phpversionhelp':
638 print_string($help, 'install', phpversion());
639 break;
640 case 'memorylimithelp':
641 print_string($help, 'install', get_memory_limit());
642 break;
643 default:
644 print_string($help, 'install');
649 //==========================================================================//
651 function get_memory_limit() {
652 if ($limit = ini_get('memory_limit')) {
653 return $limit;
654 } else {
655 return get_cfg_var('memory_limit');
659 //==========================================================================//
661 function check_memory_limit() {
663 /// if limit is already 16M or more then we don't care if we can change it or not
664 if ((int)str_replace('M', '', get_memory_limit()) >= 16) {
665 return true;
668 /// Otherwise, see if we can change it ourselves
669 @ini_set('memory_limit', '16M');
670 return ((int)str_replace('M', '', get_memory_limit()) >= 16);
673 //==========================================================================//
675 function css_styles() {
678 <style type="text/css">
680 body { background-color: #ffeece; }
681 p, li {
682 font-family: helvetica, arial, sans-serif;
683 font-size: 10pt;
685 a { text-decoration: none; color: blue; }
686 .errormsg {
687 color: red;
688 font-weight: bold;
690 blockquote {
691 font-family: courier, monospace;
692 font-size: 10pt;
694 .install_table {
695 width: 500px;
697 .td_left {
698 text-align: right;
699 font-weight: bold;
701 .td_right {
702 text-align: left;
704 .main {
705 width: 500px;
706 border-width: 1px;
707 border-style: solid;
708 border-color: #ffc85f;
709 -moz-border-radius-bottomleft: 15px;
710 -moz-border-radius-bottomright: 15px;
712 .td_mainheading {
713 background-color: #fee6b9;
714 padding: 10px;
716 .td_main {
717 text-align: center;
719 .td_mainlogo {
721 .p_mainlogo {
723 .p_mainheading {
724 font-size: 11pt;
726 .p_mainheader{
727 text-align: right;
728 font-size: 20pt;
729 font-weight: bold;
731 .p_pass {
732 color: green;
733 font-weight: bold;
735 .p_fail {
736 color: red;
737 font-weight: bold;
739 .p_caution {
740 color: #ff6600;
741 font-weight: bold;
743 .p_help {
744 text-align: center;
745 font-family: helvetica, arial, sans-serif;
746 font-size: 14pt;
747 font-weight: bold;
748 color: #333333;
751 </style>
753 <?php