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');
10 $configfile = './config.php';
13 ///==========================================================================//
14 /// We are doing this in stages
15 /// 0. Welcome and language settings
17 /// 2. Database 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');
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']) ) {
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'])) {
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'];
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;
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;
152 //==========================================================================//
154 /// Check the directory settings
156 if ($INSTALL['stage'] == 2) {
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');
169 $CFG->dirroot
= $INSTALL['dirroot'];
172 if (($fh = @fopen
($INSTALL['wwwroot'].'/install.php', 'r')) === false) {
173 $errormsg = get_string('wwwrooterror', 'install');
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;
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
227 if ($db->Execute("CREATE DATABASE {$INSTALL['dbname']};")) {
228 $dbconnected = $db->Connect($INSTALL['dbhost'],$INSTALL['dbuser'],$INSTALL['dbpass'],$INSTALL['dbname']);
230 $errormsg = get_string('dbcreationerror', 'install');
240 if (($dbconnected === false) and (empty($errormsg)) ) {
241 $errormsg = get_string('dbconnectionerror', 'install');
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;
259 if ($nextstage != 4) {
260 $errormsg = get_string('admindirerror', 'install');
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";
279 $str .= 'unset($CFG);'."\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";
293 $str .= '$CFG->wwwroot = \''.$INSTALL['wwwroot']."';\r\n";
294 $str .= '$CFG->dirroot = \''.$INSTALL['dirroot']."';\r\n";
295 $str .= '$CFG->dataroot = \''.$INSTALL['dataroot']."';\r\n";
298 $str .= '$CFG->directorypermissions = 0777;'."\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";
308 if (( $configsuccess = ($fh = @fopen
($configfile, 'w')) ) !== false) {
314 $INSTALL['config'] = $str;
319 //==========================================================================//
325 <html dir
="<?php echo (get_string('this_direction') == 'rtl') ? 'rtl' : 'ltr' ?>">
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() ?
>
338 if (isset($_GET['help'])) {
339 print_install_help($_GET['help']);
340 close_window_button();
345 <table
class="main" align
="center" cellpadding
="3" cellspacing
="0">
347 <td
class="td_mainlogo">
348 <p
class="p_mainlogo"><img src
="pix/moodlelogo-med.gif" width
="240" height
="60"></p
>
350 <td
class="td_mainlogo" valign
="bottom">
351 <p
class="p_mainheader"><?php
print_string('installation', 'install') ?
></p
>
356 <td
class="td_mainheading" colspan
="2">
357 <p
class="p_mainheading"><?php
echo $stagetext[$nextstage] ?
></p
>
362 <td
class="td_main" colspan
="2">
366 if (!empty($errormsg)) echo "<p class=\"errormsg\" align=\"center\">$errormsg</p>\n";
369 if ($nextstage == 5) {
370 $INSTALL['stage'] = 0;
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";
378 echo "<td width=\"33.3%\"> </td>\n";
379 echo "<td width=\"33.3%\"> </td>\n";
380 echo "<td width=\"33.3%\" align=\"right\">\n";
381 print_single_button("index.php", $options, get_string('continue')." »");
387 echo "<p class=\"errormsg\">".get_string('configfilenotwritten', 'install')."</p>";
389 echo "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"100%\">\n";
391 echo "<td width=\"33.3%\"> </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'));
397 echo "<td width=\"33.3%\" align=\"right\">\n";
398 print_single_button("index.php", $options, get_string('continue')." »");
404 echo "<div style=\"text-align: left\">\n";
405 print_object(htmlentities($str));
409 $formaction = (isset($_GET['configfile'])) ?
"install.php?configfile=".$_GET['configfile'] : "install.php";
410 form_table($nextstage, $formaction);
437 //==========================================================================//
440 function print_object($object) {
448 //==========================================================================//
450 function form_table($nextstage = 0, $formaction = "install.php") {
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">
461 /// what we do depends on the stage we're at
462 switch ($nextstage) {
463 case 0: /// Language settings
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'], '') ?
>
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');
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);
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);
494 case 2: /// Directory settings
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'] ?>" />
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'] ?>" />
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'] ?>" />
518 case 3: /// Database settings
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'], '') ?
>
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'] ?>" />
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'] ?>" />
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'] ?>" />
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'] ?>" />
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'] ?>" />
560 case 4: /// Administration directory setting
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'] ?>" />
578 <td colspan
="<?php echo ($nextstage == 1) ? '3' : '2'; ?>">
580 <?php
echo ($nextstage < 5) ?
"<input type=\"submit\" name=\"next\" value=\"".get_string('next')." »\" style=\"float: right\"/>\n" : " \n" ?
>
581 <?php
echo ($nextstage > 0) ?
"<input type=\"submit\" name=\"prev\" value=\"« ".get_string('previous')."\" style=\"float: left\"/>\n" : " \n" ?
>
596 //==========================================================================//
598 function print_compatibility_row($success, $testtext, $errormessage, $helpfield='', $caution=false) {
600 echo "<td class=\"td_left\" valign=\"top\" nowrap width=\"160\"><p>$testtext</p></td>\n";
602 echo "<td valign=\"top\"><p class=\"p_pass\">".get_string('pass', 'install')."</p></td>\n";
603 echo "<td valign=\"top\"> </td>\n";
605 echo "<td valign=\"top\"";
606 echo ($caution) ?
"<p class=\"p_caution\">".get_string('caution', 'install') : "<p class=\"p_fail\">".get_string('fail', 'install');
608 echo "<td valign=\"top\">";
609 echo "<p>$errormessage ";
610 install_helpbutton("install.php?help=$helpfield");
618 //==========================================================================//
620 function install_helpbutton($url, $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')\">";
633 //==========================================================================//
635 function print_install_help($help) {
637 case 'phpversionhelp':
638 print_string($help, 'install', phpversion());
640 case 'memorylimithelp':
641 print_string($help, 'install', get_memory_limit());
644 print_string($help, 'install');
649 //==========================================================================//
651 function get_memory_limit() {
652 if ($limit = ini_get('memory_limit')) {
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) {
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; }
682 font
-family
: helvetica
, arial
, sans
-serif
;
685 a
{ text
-decoration
: none
; color
: blue
; }
691 font
-family
: courier
, monospace
;
708 border
-color
: #ffc85f;
709 -moz
-border
-radius
-bottomleft
: 15px
;
710 -moz
-border
-radius
-bottomright
: 15px
;
713 background
-color
: #fee6b9;
745 font
-family
: helvetica
, arial
, sans
-serif
;