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
= 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'])) {
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').'<br />';
167 if ($fh) fclose($fh);
169 $CFG->dirroot
= $INSTALL['dirroot'];
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);
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;
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');
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
236 if ($db->Execute("CREATE DATABASE {$INSTALL['dbname']};")) {
237 $dbconnected = $db->Connect($INSTALL['dbhost'],$INSTALL['dbuser'],$INSTALL['dbpass'],$INSTALL['dbname']);
239 $errormsg = get_string('dbcreationerror', 'install');
250 if (($dbconnected === false) and (empty($errormsg)) ) {
251 $errormsg = get_string('dbconnectionerror', 'install');
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;
271 if ($nextstage != 4) {
272 $errormsg = get_string('admindirerror', 'install');
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";
291 $str .= 'unset($CFG);'."\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";
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";
311 $str .= '$CFG->directorypermissions = 00777; // try 02777 on a server in Safe Mode'."\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";
321 if (( $configsuccess = ($fh = @fopen
($configfile, 'w')) ) !== false) {
327 $INSTALL['config'] = $str;
332 //==========================================================================//
338 <html dir
="<?php echo (get_string('this_direction') == 'rtl') ? 'rtl' : 'ltr' ?>">
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() ?
>
351 if (isset($_GET['help'])) {
352 print_install_help($_GET['help']);
353 close_window_button();
358 <table
class="main" align
="center" cellpadding
="3" cellspacing
="0">
360 <td
class="td_mainlogo">
361 <p
class="p_mainlogo"><img src
="pix/moodlelogo-med.gif" width
="240" height
="60" alt
=\"\"></p
>
363 <td
class="td_mainlogo" valign
="bottom">
364 <p
class="p_mainheader"><?php
print_string('installation', 'install') ?
></p
>
369 <td
class="td_mainheading" colspan
="2">
370 <p
class="p_mainheading"><?php
echo $stagetext[$nextstage] ?
></p
>
375 <td
class="td_main" colspan
="2">
379 if (!empty($errormsg)) echo "<p class=\"errormsg\" align=\"center\">$errormsg</p>\n";
382 if ($nextstage == 5) {
383 $INSTALL['stage'] = 0;
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";
391 echo "<td width=\"33.3%\"> </td>\n";
392 echo "<td width=\"33.3%\"> </td>\n";
393 echo "<td width=\"33.3%\" align=\"right\">\n";
394 print_single_button("index.php", $options, get_string('continue')." »");
400 echo "<p class=\"errormsg\">".get_string('configfilenotwritten', 'install')."</p>";
402 echo "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"100%\">\n";
404 echo "<td width=\"33.3%\"> </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'));
410 echo "<td width=\"33.3%\" align=\"right\">\n";
411 print_single_button("index.php", $options, get_string('continue')." »");
417 echo "<div style=\"text-align: left\">\n";
418 print_object(htmlentities($str));
422 $formaction = (isset($_GET['configfile'])) ?
"install.php?configfile=".$_GET['configfile'] : "install.php";
423 form_table($nextstage, $formaction);
450 //==========================================================================//
453 function print_object($object) {
461 //==========================================================================//
463 function form_table($nextstage = 0, $formaction = "install.php") {
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">
474 /// what we do depends on the stage we're at
475 switch ($nextstage) {
476 case 0: /// Language settings
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'], '') ?
>
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');
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);
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);
507 case 2: /// Directory settings
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'] ?>" />
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'] ?>" />
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'] ?>" />
531 case 3: /// Database settings
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'], '') ?
>
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'] ?>" />
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'] ?>" />
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'] ?>" />
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'] ?>" />
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'] ?>" />
573 case 4: /// Administration directory setting
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'] ?>" />
591 <td colspan
="<?php echo ($nextstage == 1) ? '3' : '2'; ?>">
593 <?php
echo ($nextstage < 5) ?
"<input type=\"submit\" name=\"next\" value=\"".get_string('next')." »\" style=\"float: right\"/>\n" : " \n" ?
>
594 <?php
echo ($nextstage > 0) ?
"<input type=\"submit\" name=\"prev\" value=\"« ".get_string('previous')."\" style=\"float: left\"/>\n" : " \n" ?
>
609 //==========================================================================//
611 function print_compatibility_row($success, $testtext, $errormessage, $helpfield='', $caution=false) {
613 echo "<td class=\"td_left\" valign=\"top\" nowrap width=\"160\"><p>$testtext</p></td>\n";
615 echo "<td valign=\"top\"><p class=\"p_pass\">".get_string('pass', 'install')."</p></td>\n";
616 echo "<td valign=\"top\"> </td>\n";
618 echo "<td valign=\"top\"";
619 echo ($caution) ?
"<p class=\"p_caution\">".get_string('caution', 'install') : "<p class=\"p_fail\">".get_string('fail', 'install');
621 echo "<td valign=\"top\">";
622 echo "<p>$errormessage ";
623 install_helpbutton("install.php?help=$helpfield");
631 //==========================================================================//
633 function install_helpbutton($url, $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')\">";
646 //==========================================================================//
648 function print_install_help($help) {
650 case 'phpversionhelp':
651 print_string($help, 'install', phpversion());
653 case 'memorylimithelp':
654 print_string($help, 'install', get_memory_limit());
657 print_string($help, 'install');
662 //==========================================================================//
664 function get_memory_limit() {
665 if ($limit = ini_get('memory_limit')) {
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) {
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; }
695 font
-family
: helvetica
, arial
, sans
-serif
;
698 a
{ text
-decoration
: none
; color
: blue
; }
704 font
-family
: courier
, monospace
;
721 border
-color
: #ffc85f;
722 -moz
-border
-radius
-bottomleft
: 15px
;
723 -moz
-border
-radius
-bottomright
: 15px
;
726 background
-color
: #fee6b9;
758 font
-family
: helvetica
, arial
, sans
-serif
;