Ăšltimos cambios incorporados
[moodle.git] / install.php
CommitLineData
8b5b1028 1<?php /// $Id$
2 /// install.php - helps admin user to create a config.php file
3
4/// If config.php exists already then we are not needed.
5
6if (file_exists('./config.php')) {
7 header('Location: index.php');
8} else {
9 $configfile = './config.php';
10}
11
12///==========================================================================//
13/// We are doing this in stages
14/// 1. Welcome and language settings
15/// 2. Database settings
16/// 3. Host settings
17/// 4. Administration directory name
18/// 5. Save or display the settings
19/// 6. Redirect to index.php
20///==========================================================================//
21
22
23/// Begin the session as we are holding all information in a session
24/// variable until the end.
25
26session_name('MoodleSession');
27@session_start();
28
29if (! isset($_SESSION['INSTALL'])) {
30 $_SESSION['INSTALL'] = array();
31}
32
33$INSTALL = &$_SESSION['INSTALL']; // Makes it easier to reference
34
35if ( empty($INSTALL['language']) and empty($_POST['language']) ) { // First time through this script
36
37 /// set defaults
38 $INSTALL['language'] = 'en';
39
40 $INSTALL['dbhost'] = 'localhost';
41 $INSTALL['dbuser'] = '';
42 $INSTALL['dbpass'] = '';
43 $INSTALL['dbtype'] = 'mysql';
44 $INSTALL['dbname'] = 'moodle';
45 $INSTALL['prefix'] = 'mdl_';
46
47 $INSTALL['wwwroot'] = '';
48 $INSTALL['dirroot'] = dirname(__FILE__);
49 $INSTALL['dataroot'] = dirname(dirname(__FILE__)) . '/moodledata';
50
51 $INSTALL['admindirname'] = 'admin';
52
53 $INSTALL['stage'] = 1;
54}
55
56
57
58//==========================================================================//
59
60/// Fake some settings so that we can use selected functions from moodlelib.php and weblib.php
61$SESSION->lang = (!empty($_POST['language'])) ? $_POST['language'] : $INSTALL['language'];
62$CFG->dirroot = $INSTALL['dirroot'];
63$CFG->dataroot = $INSTALL['dataroot'];
64$CFG->directorypermissions = 0777;
65
66
67require_once('./lib/moodlelib.php');
68require_once('./lib/weblib.php');
69require_once('./lib/adodb/adodb.inc.php');
70
71
72/// guess the www root
73if ($INSTALL['wwwroot'] == '') {
74 list($INSTALL['wwwroot'], $xtra) = explode('/install.php', qualified_me());
75}
76
77$stagetext = array(1 => get_string('chooselanguage', 'install'),
78 get_string('directorysettings', 'install'),
79 get_string('databasesettings', 'install'),
80 get_string('admindirsetting', 'install'),
81 get_string('configurationcomplete', 'install')
82 );
83
84
85//==========================================================================//
86
87/// Was data submitted?
88if (!empty($_POST['stage'])) {
89
90 /// Get the stage for which the form was set and the next stage we are going to
91
92
93 if ( $goforward = (! empty( $_POST['next'] )) ) {
94 $nextstage = $_POST['stage'] + 1;
95 } else {
96 $nextstage = $_POST['stage'] - 1;
97 }
98
99 if ($nextstage < 1) $nextstage = 1;
100
101
102 /// Store any posted data
103 foreach ($_POST as $setting=>$value) {
104 $INSTALL[$setting] = $value;
105 }
106
107} else {
108
109 $goforward = true;
110 $nextstage = 1;
111
112}
113
114
115
116//==========================================================================//
117
118/// Check the directory settings
119if ($INSTALL['stage'] == 2) {
120
121 error_reporting(0);
122
123 /// check wwwroot
124 if (($fh = @fopen($INSTALL['wwwroot'].'/install.php', 'r')) === false) {
125 $errormsg = get_string('wwwrooterror', 'install');
126 } else {
127 fclose($fh);
128
129 /// check dirroot
130 if (($fh = @fopen($INSTALL['dirroot'].'/install.php', 'r')) === false ) {
131 $CFG->dirroot = dirname(__FILE__);
132 $INSTALL['dirroot'] = dirname(__FILE__);
133 $errormsg = get_string('dirrooterror', 'install');
134 } else {
135 fclose($fh);
136
137 $CFG->dirroot = $INSTALL['dirroot'];
138
139 /// check dataroot
140 $CFG->dataroot = $INSTALL['dataroot'];
141 if (make_upload_directory('sessions', false) === false ) {
142 $errormsg = get_string('datarooterror', 'install');
143 }
144 }
145 }
146
147
148 if (!empty($errormsg)) $nextstage = 2;
149
150 error_reporting(7);
151}
152
153
154
155//==========================================================================//
156
157/// Check database settings if stage 3 data submitted
158/// Try to connect to the database. If that fails then try to create the database
159if ($INSTALL['stage'] == 3) {
160
161 if (empty($INSTALL['dbname'])) {
162 $INSTALL['dbname'] = 'moodle';
163 }
164
165 /// different format for postgres7
166 if ($INSTALL['dbtype'] == 'postgres7') {
167 $INSTALL['dbhost'] = "user='{$INSTALL['dbuser']}' password='{$INSTALL['dbpass']}' dbname='{$INSTALL['dbname']}'";
168 if ($INSTALL['prefix'] == '') { /// must have a prefix
169 $INSTALL['prefix'] = 'mdl_';
170 }
171 }
172
173 $db = &ADONewConnection($INSTALL['dbtype']);
174
175 error_reporting(0); // Hide errors
176
177 if (!($dbconnected = $db->Connect($INSTALL['dbhost'],$INSTALL['dbuser'],$INSTALL['dbpass'],$INSTALL['dbname']))) {
178 /// The following doesn't seem to work but we're working on it
179 /// If you come up with a solution for creating a database in MySQL
180 /// feel free to put it in and let us know
181 if ($dbconnected = $db->Connect($INSTALL['dbhost'],$INSTALL['dbuser'],$INSTALL['dbpass'])) {
182 switch ($INSTALL['dbtype']) { /// Try to create a database
183 case 'mysql':
184 if ($db->Execute("CREATE DATABASE {$INSTALL['dbname']};")) {
185 $dbconnected = $db->Connect($INSTALL['dbhost'],$INSTALL['dbuser'],$INSTALL['dbpass'],$INSTALL['dbname']);
186 } else {
187 $errormsg = get_string('dbcreationerror', 'install');
188 $nextstage = 3;
189 }
190 break;
191 }
192 }
193 }
194
195 error_reporting(7);
196
197 if (($dbconnected === false) and (empty($errormsg)) ) {
198 $errormsg = get_string('dbconnectionerror', 'install');
199 $nextstage = 3;
200 }
201}
202
203
204
205//==========================================================================//
206
207/// If the next stage is admin directory settings OR we have just come from there then
208/// check the admin directory.
209/// If we can open a file then we know that the admin name is correct.
210
211if ($nextstage == 4 or $INSTALL['stage'] == 4) {
212 if (($fh = @fopen($INSTALL['wwwroot'].'/'.$INSTALL['admindirname'].'/site.html', 'r')) !== false) {
213 $nextstage = ($goforward) ? 5 : 3;
214 fclose($fh);
215 } else {
216 if ($nextstage != 4) {
217 $errormsg = get_string('admindirerror', 'install');
218 $nextstage = 4;
219 }
220 }
221}
222
223
224
225//==========================================================================//
226
227/// Display or print the data
228/// Put the data into a string
229/// Try to open config file for writing.
230if ($nextstage == 5) {
231
232 $str = '<?php /// Moodle Configuration File '."\n";
233 $str .= "\n";
234
235 $str .= 'unset($CFG);'."\n";
236 $str .= "\n";
237
238 $str .= '$CFG->dbtype = \''.$INSTALL['dbtype']."';\n";
239 $str .= '$CFG->dbhost = \''.$INSTALL['dbhost']."';\n";
240 if ($INSTALL['dbtype'] == 'mysql') {
241 $str .= '$CFG->dbname = \''.$INSTALL['dbname']."';\n";
242 $str .= '$CFG->dbuser = \''.$INSTALL['dbuser']."';\n";
243 $str .= '$CFG->dbpass = \''.$INSTALL['dbpass']."';\n";
244 }
245 $str .= '$CFG->dbpersist = false;'."\n";
246 $str .= '$CFG->prefix = \''.$INSTALL['prefix']."';\n";
247 $str .= "\n";
248
249 $str .= '$CFG->wwwroot = \''.$INSTALL['wwwroot']."';\n";
250 $str .= '$CFG->dirroot = \''.$INSTALL['dirroot']."';\n";
251 $str .= '$CFG->dataroot = \''.$INSTALL['dataroot']."';\n";
252 $str .= "\n";
253
254 $str .= '$CFG->directorypermissions = 0777;'."\n";
255 $str .= "\n";
256
257 $str .= 'require_once("$CFG->dirroot/lib/setup.php");'."\n";
258 $str .= '// MAKE SURE WHEN YOU EDIT THIS FILE THAT THERE ARE NO SPACES, BLANK LINES,'."\n";
259 $str .= '// RETURNS, OR ANYTHING ELSE AFTER THE TWO CHARACTERS ON THE NEXT LINE.'."\n";
260 $str .= '?>';
261
262 if (( $configsuccess = ($fh = @fopen($configfile, 'w')) ) !== false) {
263 fwrite($fh, $str);
264 fclose($fh);
265 }
266}
267
268
269
270//==========================================================================//
271
272?>
273
274
275
276<html dir="<?php echo (get_string('this_direction') == 'rtl') ? 'rtl' : 'ltr' ?>">
277<head>
278<link rel="shortcut icon" href="http://moodle.dougiamas.net/theme/standard/favicon.ico" />
279<title>Moodle Install</title>
280<meta http-equiv="content-type" content="text/html; charset=<?php print_string('thischarset') ?>" />
281<?php css_styles() ?>
282
283</head>
284
285<body>
286
287<h1 align="center">MOODLE INSTALL</h1>
288
289<table class="main" align="center" cellpadding="3" cellspacing="0">
290 <tr>
291 <td class="td_mainheading"><p class="p_mainheading"><?php echo $stagetext[$nextstage] ?></p></td>
292 </tr>
293
294 <tr>
295 <td class="td_main">
296
297<?php
298
299if (!empty($errormsg)) echo "<p class=\"errormsg\" align=\"center\">$errormsg</p>\n";
300
301
302if ($nextstage == 5) {
303 if ($configsuccess) {
304 echo "<p>".get_string('configfilewritten', 'install')."</p>\n";
305 } else {
306 echo "<p>".get_string('configfilenotwritten', 'install')."</p>";
307 echo "<hr />\n";
308 echo "<div style=\"text-align: left\">\n";
309 print_object(htmlentities($str));
310 echo "</div>\n";
311 echo "<hr />\n";
312 }
313 echo "<p>(<a href=\"index.php?lang={$INSTALL['language']}\">Continue</a>)</p>\n";
314} else {
315 $formaction = (isset($_GET['configfile'])) ? "install.php?configfile=".$_GET['configfile'] : "install.php";
316 form_table($nextstage, $formaction);
317}
318
319?>
320
321 </td>
322 </tr>
323</table>
324
325</body>
326</html>
327
328
329
330
331
332
333
334
335
336
337<?php
338
339//==========================================================================//
340
341
342function print_object($object) {
343 echo "<pre>\n";
344 print_r($object);
345 echo "</pre>\n";
346}
347
348
349
350//==========================================================================//
351
352function form_table($nextstage = 1, $formaction = "install.php") {
353 global $INSTALL;
354
355 /// standard lines for all forms
356?>
357
358 <form name="installform" method="post" action="<?php echo $formaction ?>">
359 <input type="hidden" name="stage" value="<?php echo $nextstage ?>" />
360 <table class="install_table" cellspacing="3" cellpadding="3" align="center">
361
362<?php
363 /// what we do depends on the stage we're at
364 switch ($nextstage) {
365 case 1: /// Language settings
366?>
367 <tr>
368 <td class="td_left"><p><?php print_string('language') ?></p></td>
369 <td class="td_right">
370 <?php choose_from_menu (get_list_of_languages(), 'language', $INSTALL['language'], '') ?>
371 </td>
372 </tr>
373
374<?php
375 break;
376 case 2: /// Directory settings
377?>
378
379 <tr>
380 <td class="td_left"><p><?php print_string('wwwroot', 'install') ?></p></td>
381 <td class="td_right">
382 <input type="text" size="40"name="wwwroot" value="<?php echo $INSTALL['wwwroot'] ?>" />
383 </td>
384 </tr>
385 <tr>
386 <td class="td_left"><p><?php print_string('dirroot', 'install') ?></p></td>
387 <td class="td_right">
388 <input type="text" size="40" name="dirroot" value="<?php echo $INSTALL['dirroot'] ?>" />
389 </td>
390 </tr>
391 <tr>
392 <td class="td_left"><p><?php print_string('dataroot', 'install') ?></p></td>
393 <td class="td_right">
394 <input type="text" size="40" name="dataroot" value="<?php echo $INSTALL['dataroot'] ?>" />
395 </td>
396 </tr>
397
398<?php
399 break;
400 case 3: /// Database settings
401?>
402
403 <tr>
404 <td class="td_left"><p><?php print_string('dbtype', 'install') ?></p></td>
405 <td class="td_right">
406 <?php choose_from_menu (array("mysql" => "mysql", "postgres7" => "postgres7"), 'dbtype', $INSTALL['dbtype'], '') ?>
407 </td>
408 </tr>
409 <tr>
410 <td class="td_left"><p><?php print_string('dbhost', 'install') ?></p></td>
411 <td class="td_right">
412 <input type="text" size="40" name="dbhost" value="<?php echo $INSTALL['dbhost'] ?>" />
413 </td>
414 </tr>
415 <tr>
416 <td class="td_left"><p><?php print_string('database', 'install') ?></p></td>
417 <td class="td_right">
418 <input type="text" size="40" name="dbname" value="<?php echo $INSTALL['dbname'] ?>" />
419 </td>
420 </tr>
421 <tr>
422 <td class="td_left"><p><?php print_string('user') ?></p></td>
423 <td class="td_right">
424 <input type="text" size="40" name="dbuser" value="<?php echo $INSTALL['dbuser'] ?>" />
425 </td>
426 </tr>
427 <tr>
428 <td class="td_left"><p><?php print_string('password') ?></p></td>
429 <td class="td_right">
430 <input type="text" size="40" name="dbpass" value="<?php echo $INSTALL['dbpass'] ?>" />
431 </td>
432 </tr>
433 <tr>
434 <td class="td_left"><p><?php print_string('dbprefix', 'install') ?></p></td>
435 <td class="td_right">
436 <input type="text" size="40" name="prefix" value="<?php echo $INSTALL['prefix'] ?>" />
437 </td>
438 </tr>
439
440<?php
441 break;
442 case 4: /// Administration directory setting
443?>
444
445 <tr>
446 <td class="td_left"><p><?php print_string('admindirname', 'install') ?></p></td>
447 <td class="td_right">
448 <input type="text" size="40" name="admindirname" value="<?php echo $INSTALL['admindirname'] ?>" />
449 </td>
450 </tr>
451
452
453<?php
454 break;
455 default:
456 }
457?>
458
459 <tr>
460 <td align="left">
461 <?php echo ($nextstage > 1) ? "<input type=\"submit\" name=\"prev\" value=\"".get_string('previous')."\" />\n" : "&nbsp;\n" ?>
462 </td>
463 <td align="right">
464 <?php echo ($nextstage < 5) ? "<input type=\"submit\" name=\"next\" value=\"".get_string('next')."\" />\n" : "&nbsp;\n" ?>
465 </td>
466 </tr>
467
468 </table>
469 </form>
470
471<?php
472}
473
474
475
476//==========================================================================//
477
478function css_styles() {
479?>
480
481<style type="text/css">
482
483 body { background-color: #ffeece; }
484 p { font-family: helvetica, arial, sans-serif; size: normal; }
485 a { text-decoration: none; color: blue; }
486 .errormsg {
487 color: red;
488 font-weight: bold;
489 }
490 .install_table {
491 width: 500px;
492 }
493 .td_left {
494 text-align: right;
495 font-weight: bold;
496 }
497 .td_right {
498 text-align: left;
499 }
500 .main {
501 width: 500px;
502 border-width: 1px;
503 border-style: solid;
504 border-color: #ffc85f;
505 -moz-border-radius-bottomleft: 15px;
506 -moz-border-radius-bottomright: 15px;
507 }
508 .td_mainheading {
509 background-color: #fee6b9;
510 }
511 .td_main {
512 text-align: center;
513 }
514 .p_mainheading {
515 font-size: 10pt;
516 }
517
518
519</style>
520
521<?php
522}
523?>