root/releases/0.9rc2/install.php

Revision 1474, 22.2 kB (checked in by misja, 9 months ago)

Misja Hoebe <misja@curverider.co.uk> Merge r1473 in 0.9rc2 tag

Line 
1 <?php
2 /**
3  * Elgg configuration/install script
4  * @author Rolando Espinoza La Fuente <rho@prosoftpeople.com>
5  */
6     global $CFG;
7
8     require(dirname(__FILE__).'/lib/config-defaults.php');
9
10     require($CFG->dirroot.'lib/elgglib.php');
11     require($CFG->dirroot.'mod/gettext/lib.php');
12
13     global $db, $CFG;
14
15     $version = '0.9';
16     $messages = array();
17     $configs = array();
18     $config_ready = false;
19
20     // Prevent access on already running configuration
21     if (file_exists($CFG->dirroot.'config.php') && config_check_db()) {
22         $config_ready = true;
23         $messages[] = __gettext('You have already configured Elgg and can safely remove the file <code>install.php</code> from your installation.');
24     }
25     
26     config_init();
27
28     // workaround for backward compatibility
29     if (!function_exists('__gettext')) {
30         function __gettext($s) {
31             return $s;
32         }
33     }
34 ?>
35 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
36 <html xmlns="http://www.w3.org/1999/xhtml">
37 <head>
38 <title>Elgg <?php echo $version ?> <?php echo __gettext('installation') ?></title>
39 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
40 <style type="text/css" media="all">
41     /* globals */
42
43     body {
44         padding: 0;
45         margin:0;
46         text-align:center;
47         font-family: "Lucida Grande", verdana, arial, helvetica, sans-serif;
48         color: #666; /*333;*/
49         background: #eee url(mod/template/images/bg2.gif);
50         width:100%;
51         font-size:90%;
52         line-height:1.6em;
53         margin: 0 auto;
54         }
55
56     a {
57             color: #7289AF;
58             font-family: "Lucida Grande", verdana, arial, helvetica, sans-serif;
59             font-size:100%;
60             text-decoration: none;
61         }
62
63     a:hover {
64         text-decoration: underline;
65     }
66        
67     p {
68         font-size: 100%;
69         color: #5e5d5c;
70         margin: 0;
71         padding: 5px;
72     }
73
74     h1 {
75            margin:0px 0px 15px 0px;
76         padding:0px;
77         font-size:120%;
78         font-weight:900;
79     }
80
81
82     h2 {
83         margin:0px 0px 5px 0px;
84         padding:0px;
85         font-size:100%
86     }
87
88
89     h3 {
90         margin:0px 0px 5px 0px;
91         padding:0px;
92         font-size:100%
93     }
94
95     h4 {
96         margin:0px 0px 5px 0px;
97         padding:0px;
98         font-size:100%
99     }
100
101     h5 {
102         margin:0px 0px 5px 0px;
103         padding:0px;
104         color:#1181AA;
105            background:#fff;
106         font-size:100%
107     }
108
109     td p {
110        
111         font-size: 0.8em;
112         margin-top: 15px;
113         padding-top: 15px;
114        
115     }
116
117     blockquote {
118         padding: 0 1pc 1pc 1pc;
119         border: 1px solid #ddd;
120         background-color: #F0F0F0;
121            color:#000;
122         background-image: url("{$CFG->wwwroot}_templates/double-quotes.png");
123         background-repeat: no-repeat;
124         background-position: -10px -7px;
125     }
126
127     .checkbox {
128      background: #fff;
129      border: none;
130      margin: 0;
131      padding: 0;
132     }
133
134     label {
135      margin: 0;
136      padding: 0;
137     }
138
139     input {
140        width: auto;
141        font-size: 1em;
142         line-height: 1.2em;
143        font-weight:normal;
144         padding: 2px 4px;
145     }
146
147     textarea {
148        background-color: #FFF;
149        color: #000;
150        border: 1px solid #CCC;
151        font-size: 1em;
152        padding: 3px;
153         margin: 0 0 5px;
154         width:100%;
155         height:360px;
156       }
157
158     .button {
159       padding:2px;
160       font-size:16px;
161       width:50px;
162    }
163
164     .config-form {
165         border: 1px #ddd dotted;
166         margin: 0 auto;
167     }
168
169     .form-item {
170         padding: 10px;
171         border-bottom: 1px #ddd dotted;
172         clear:both;
173     }
174
175     .form-item label {
176         float: left;
177         text-align: right;
178         font-weight: bold;
179         width: 220px;
180     }
181
182     .form-item input {
183         width: 150px;
184     }
185
186     .form-error {
187         background: #f7dad8;
188     }
189
190     code {
191         font-size: 1.2em;
192     }
193
194     .form-item input,
195     .form-item select {
196         margin-left: 10px;
197         border: 1px #ccc solid;
198         background: #eee;
199         color: #333;
200     }
201
202     .form-item input:focus,
203     .form-tiem select:focus {
204         background: #fff;
205         color: #000;
206     }
207
208     .form-item span {
209         display: block;
210         font-size: 0.8em;
211         color: #666;
212         padding-top: 5px;
213         padding-left: 230px;
214         line-height: 1.2em;
215     }
216
217     /*
218     Wraps the entire page
219     */
220
221     #container {
222         width: 780px;
223         min-width: 750px;
224          padding:0px;
225          margin:0px;
226          border:1px solid #ccc;
227          background:#fff;
228          margin: 10px auto;
229         }
230
231     /*
232     HEADER
233     */
234
235     #header {
236         width: 100%;
237         background: #fff url(mod/template/images/newelgg.gif) top left no-repeat;
238         color:#1181AA;
239         padding: 0px;
240         margin: 0px;
241         text-align: center;
242      height:80px;
243         }
244
245     #header h1 {
246         padding: 40px 0 0 0;
247         margin: 0;
248         color:#017FBC;
249      font-size:1.4em;
250         text-align: center;
251         font-weight:bold;
252         }   
253
254     /*
255     SITE CONTENT WRAPPER
256     */
257
258     #content {
259         margin: 0;
260         padding: 20px;
261         text-align: left;
262         border: 0px solid #ccc;
263         border-top: none;
264         background-color: #fff;
265         color:#000;
266      font-size:90%;
267      min-height:300px;
268         width: 600px;
269         margin: 0 auto;
270     }
271
272     #content h1 {
273       border-bottom: 1px solid #666;
274       padding:0px;
275       margin:0px;
276      }
277
278     /*
279       FOOTER
280     */
281
282     #footer {
283         text-align: right;
284         padding: 2px 10px;
285          border-top:1px solid #ccc;
286          border-bottom:1px solid #ddd;
287          font-size:0.8em;
288         color: #555;
289     }
290
291     #footer a:hover {
292        text-decoration:underline;
293      }
294
295     #footer a:link, #footer a:visited {
296         text-align:right;
297     }
298
299     /*
300       Important warning messages
301     */
302
303     .important{
304         background:#F7DAD8;
305         color:#000;
306     }
307
308     .important p{
309      }
310
311
312     /*
313      System messages
314     */
315
316     #messages {
317      text-align:left;
318      background:#E7EDF3;
319      border:1px solid #87B2E1;
320      padding:10px;
321      margin:10px;
322     }
323
324     #messages ul {
325       margin:0;
326       padding:0;
327     }
328
329     #messages li {
330       list-style:none;
331     }
332
333     /*
334        MISC
335     */
336
337     .clearall {
338         padding: 0px;
339         clear: both;
340         font-size: 0px;
341         }
342
343     .continuebutton {
344         text-align: center;
345         margin-top: 20px;
346         font-size: 1em;
347     }
348
349     .notifyproblem {
350         color: #900;
351         font-size: 1.2em;
352     }
353
354     .notifysuccess {
355         color: #0b0;
356         font-size: 1.2em;
357     }
358
359     .setup {
360         color: #ccc;
361         font-size: 0.8em;
362     }
363
364     hr {
365         background: #ccc;
366         color: #ccc;
367         height: 1px;
368         margin: 0;
369         border: 0;
370     }
371 </style>
372 </head>
373 <body>
374 <div id="container">
375
376 <div id="header">
377     <h1><?php echo __gettext('Welcome to elgg installation') ?></h1>
378 </div><!-- #header -->
379
380 <div id="content">
381
382 <?php show_messages() ?>
383
384 <?php
385  if (!$config_ready) {
386
387     if (config_submitted() && config_validate()) {
388         if (config_write_file()) {
389             config_copy_htaccess();
390 ?>
391             <p><?php echo __gettext('Your configuration file has been saved, you can now proceed with the installation.') ?></p>
392
393 <?php if (is_writable(dirname(__FILE__))) { ?>
394             <p><?php echo __gettext('Please revert your permissions on your <em>elgg directory</em> for more security.') ?></p>
395 <?php } else { ?>
396             <p><?php echo __gettext('Please revert your permissions on <code>config.php</code> for more security.') ?></p>
397 <?php } ?>
398             <div class="setup">
399 <?php
400                 if (!config_check_db()) {
401                     echo '<div style="color:#900;">';
402                     echo __gettext('<strong>Error:</strong> Database connection failed, please edit <code>config.php</code> to include the correct values. ');
403                     echo '<br />&raquo; ' . __gettext('<a href="install.php">Try again installation wizard</a>.');
404                     echo '</div>';
405                 } else {
406                     //FIXME: workaround, does not work config_check_db global db
407                     //
408                     // reload config
409                     include($CFG->dirroot.'config.php');
410                     // setup database
411                     require_once($CFG->dirroot.'lib/adodb/adodb.inc.php');
412
413                     $db = &ADONewConnection($CFG->dbtype);
414                     $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
415                     @$db->Connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname);
416                     //
417
418                     if ($db->databaseType == 'mysql') {
419                         $db->Execute('SET NAMES "utf8"');
420                         $db->Execute('SET CHARSET "utf8"');
421                     } elseif ($db->databaseType == 'postgres7') {
422                         $db->Execute('SET NAMES "utf8"');
423                     }
424
425                     $METATABLES = $db->Metatables();
426                     $_SESSION['userid'] = -1; // workaround
427
428                     // set initial administrator username & password
429                     $CFG->newsinitialusername = $_POST['admin'];
430                     $CFG->newsinitialpassword = $_POST['adminpw'];
431                     $CFG->sysadminemail = $_POST['adminemail'];
432
433                     //To display message Success!
434                     $CFG->debug = 8;
435
436                     require_once($CFG->dirroot.'lib/cache/lib.php');
437                     require_once($CFG->dirroot.'lib/constants.php');
438                     require_once($CFG->dirroot.'lib/elgglib.php'); // already included
439                     require_once($CFG->dirroot.'lib/datalib.php');
440                     require_once($CFG->dirroot.'lib/userlib.php');
441                     require_once($CFG->dirroot.'lib/dbsetup.php');
442                 }
443 ?>
444             </div>
445
446 <?php       } else { ?>
447 <p><?php echo __gettext('Your configuration file is not writable, please copy the following  content and save it as <code>config.php</code> inside your elgg installation.') ?></p>
448
449     <textarea id="config-file" onclick="this.select()"><?php echo config_file() ?></textarea>
450
451 <?php
452         }
453         print_continue('index.php');
454     } else {
455         echo config_form();
456     }
457  } else {
458     print_continue('index.php');
459  }
460 ?>
461
462 </div><!-- #content -->
463
464 <div id="footer">
465 <p>elgg <?php echo $version ?></p>
466 </div>
467
468 </div><!-- #container -->
469 </body>
470 </html>
471 <?php
472
473 /**
474  * Display messages in html block if there is any
475  */
476 function show_messages() {
477     global $messages;
478     if (!empty($messages)) {
479         echo '<div id="messages">';
480         echo implode('<br />', $messages);
481         echo '</div>';
482     }
483 }
484
485 /**
486  * Initial checks
487  */
488 function config_init() {
489     global $config_ready;
490     global $configs;
491
492     // trim all post values and remove slashes
493     foreach ($_POST as $k => $v) { $_POST[$k] = stripslashes(trim($v)); }
494
495     // default values
496     if (!isset($_POST['wwwroot'])) { $_POST['wwwroot'] = "http://" . preg_replace("#install\.php.*#","", $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);}
497     if (!isset($_POST['admin'])) { $_POST['admin'] = 'admin'; }
498     if (!isset($_POST['dbtype'])) { $_POST['dbtype'] = 'mysql'; }
499     if (!isset($_POST['dbhost'])) { $_POST['dbhost'] = 'localhost'; }
500     if (!isset($_POST['dbuser'])) { $_POST['dbuser'] = 'elgg'; }
501     if (!isset($_POST['dbpass'])) { $_POST['dbpass'] = ''; }
502     if (!isset($_POST['dbname'])) { $_POST['dbname'] = 'elgg'; }
503     if (!isset($_POST['prefix'])) { $_POST['prefix'] = 'elgg_'; }
504
505     //FIXME: force admin username
506     $_POST['admin'] = 'news';
507
508     $configs = array();
509     $configs['wwwroot']->name = __gettext('Web root');
510     $configs['wwwroot']->desc = __gettext('External URL to the site (eg: http://elgg.boston.edu/). *MUST* have a final slash at end.');
511     $configs['admin']->name = __gettext('Admin username');
512     $configs['admin']->desc = __gettext('Initial administrator username');
513     $configs['adminpw']->name = __gettext('Admin password');
514     $configs['adminpw']->desc = __gettext('Initial administrator password, at least 6 chars.');
515     $configs['adminemail']->name = __gettext('System email');
516     $configs['adminemail']->desc = __gettext('Email address for system notifications.');
517     $configs['dbtype']->name = __gettext('Database type');
518     $configs['dbtype']->desc = __gettext('PostgreSQL and MySQL supported. But is highly recommended MySQL');
519     $configs['dbhost']->name = __gettext('Database host');
520     $configs['dbuser']->name = __gettext('Database username');
521     $configs['dbpass']->name = __gettext('Database password');
522     $configs['dbpass']->not_required = true;
523     $configs['dbname']->name = __gettext('Database name');
524     $configs['prefix']->name = __gettext('Database table prefix');
525
526     if (config_submitted()) { config_validate(); }
527     elseif (!$config_ready) { config_check_requirements(); }
528 }
529
530 /**
531  * Check database connection
532  *
533  * @return bool
534  */
535 function config_check_db() {
536     global $db, $CFG;
537
538     // reload config
539     include($CFG->dirroot.'config.php');
540     // setup database
541     require_once($CFG->dirroot.'lib/adodb/adodb.inc.php');
542
543     $db = &ADONewConnection($CFG->dbtype);
544     $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
545
546     $result = @$db->Connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname);
547
548     if (!$result) {
549         unset($db);
550     }
551
552     return $result;
553 }
554
555 /**
556  * Check if system fills minimal requirements
557  * @return bool
558  * @todo check all needed requirements
559  */
560 function config_check_requirements() {
561     global $messages, $CFG;
562
563     if (!extension_loaded('mysql')) {
564         $messages[] = __gettext('<strong>Warning:</strong> Your system does not support <code>mysql</code>.');
565     }
566
567     if (!extension_loaded('pgsql')) {
568         $messages[] = __gettext('<strong>Warning:</strong> Your system does not support <code>pgsql</code>.');
569     }
570
571     if (file_exists($CFG->dirroot . 'config.php')) {
572         if (!is_writable($CFG->dirroot . 'config.php')) {
573             $messages[] = __gettext("<strong>Error:</strong> please make sure the file <code>{$CFG->dirroot}config.php</code> is writable in order to save your configuration.");
574         }
575     } elseif (!is_writable($CFG->dirroot)) {
576         $messages[] = __gettext("<strong>Error:</strong> please make sure the install directory <code>{$CFG->dirroot}</code> is writable in order to save your configuration.");
577     }
578
579     if (!is_writable($CFG->dataroot)) {
580         $messages[] = __gettext("<strong>Error:</strong> please make sure the elgg data directory <code>{$CFG->dataroot}</code> is writable.");
581     }
582
583     if (!empty($messages)) {
584         // add reload
585         $messages[] = '&raquo; <a href="install.php">Check again</a>';
586     }
587 }
588
589 /**
590  * Displays html input for given config name
591  * @param string $name
592  *      Configuration name
593  * @return string
594  *      Html output
595  */
596 function config_input($name) {
597     switch ($name) {
598         case 'dbtype':
599             $output = "<select name=\"{$name}\">";
600             foreach (array('mysql', 'postgres7') as $dbtype) {
601                 if ($dbtype == $_POST['dbtype']) $selected = 'selected="selected"';
602                 else $selected = '';
603
604                 $output .= "<option value=\"{$dbtype}\" {$selected}>{$dbtype}</option>";
605             }
606             $output .= "</select>";
607             break;
608
609         case 'admin':
610             $output = "<input type=\"text\" name=\"{$name}\" value=\"{$_POST[$name]}\" disabled=\"disabled\" />";
611             break;
612
613         case 'adminpw':
614         case 'dbpass':
615             $output = "<input type=\"password\" name=\"{$name}\" value=\"\" />";
616             break;
617
618         default:
619             $output = "<input type=\"text\" name=\"{$name}\" value=\"{$_POST[$name]}\" />";
620             break;
621     }
622
623     return $output;
624 }
625
626 /**
627  * Display html form for config values
628  * @return string
629  *      Html form
630  */
631 function config_form() {
632     global $configs;
633
634     $output = '<form class="config-form" action="" method="post">';
635
636     foreach ($configs as $k => $c) {
637         $label = $c->name . ': ';
638         $label .= empty($c->not_required) ? '*' : '&nbsp;';
639
640         $extraclass = empty($c->error) ? '' : ' form-error';
641
642         $output .= "<div class=\"form-item{$extraclass}\">";
643         $output .= "<label>{$label}</label>";
644         $output .= config_input($k);
645         $output .= "<span>{$c->desc}</span>";
646         $output .= '</div>';
647     }
648
649     $output .= '<p class="continuebutton"><input type="submit" name="submit" value="' . __gettext('Write config') .'" /></p>';
650     $output .= '</form>';
651
652     return $output;
653 }
654
655 /**
656  * True if form submitted
657  * @return bool
658  */
659 function config_submitted() {
660     return isset($_POST['submit']);
661 }
662
663 /**
664  * Check for valide config values
665  * @return bool
666  */
667 function config_validate() {
668     global $messages;
669     global $configs;
670     // only run once
671     static $result;
672
673     if (isset($result)) {
674         return $result;
675     }
676
677     //$urlregex = '#https?://([\w\.-]+)+(:\d+)?(/([\w-_\./]*(\?\S+)?)?)?#'; //extended url regex
678     $urlregex = '#^https?://[\w\.-]+(:\d+)?([\w-_~\./()\#@%;$\+=\\\|&]*)?/$#i';
679     $hostregex = '#^[\w\.-]+(:\d+)?$#';
680
681     if (!preg_match($urlregex, $_POST['wwwroot'])) {
682         $messages[] = __gettext('<strong>Error:</strong> Web root incorrect, please enter a valid url.');
683         $configs['wwwroot']->error = true;
684     }
685
686     if (!validate_username($_POST['admin'])) {
687         $messages[] = __gettext('<strong>Error:</strong> Invalid administrator username, please try again.');
688         $configs['admin']->error = true;
689     }
690
691     if (empty($_POST['adminpw']) || strlen($_POST['adminpw']) < 6) {
692         $messages[] = __gettext('<strong>Error:</strong> Invalid administrator password. Please enter at least 6 chars.');
693         $configs['adminpw']->error = true;
694     }
695
696     if (!validate_email($_POST['adminemail'])) {
697         $messages[] = __gettext('<strong>Error:</strong> Invalid administrator email, please enter a valid email address.');
698         $configs['adminemail']->error = true;
699     }
700
701     if (!in_array($_POST['dbtype'], array('mysql', 'postgres7'))) {
702         $messages[] = __gettext('<strong>Error:</strong> Database type unknown.');
703         $configs['dbtype']->error = true;
704     } elseif (function_exists('extension_loaded')) {
705         if ($_POST['dbtype'] == 'mysql' && !extension_loaded('mysql')) {
706             $messages[] = __gettext('<strong>Error:</strong> Database type <em>(mysql)</em> not supported on your system.');
707             $configs['dbtype']->error = true;
708         } elseif ($_POST['dbtype'] == 'postgres7' && !extension_loaded('pgsql')) {
709             $messages[] = __gettext('<strong>Error:</strong> Database type <em>(postgresql)</em> not supported on your system.');
710             $configs['dbtype']->error = true;
711         }
712     }
713
714     if (!preg_match($hostregex, $_POST['dbhost'])) {
715         $messages[] = __gettext('<strong>Error:</strong> Database host invalid.');
716         $configs['dbhost']->error = true;
717     }
718
719     if (empty($_POST['dbuser'])) {
720         $messages[] = __gettext('<strong>Error:</strong> Database username empty.');
721         $configs['dbuser']->error = true;
722     }
723
724     if (empty($_POST['dbpass'])) {
725         //$messages[] = __gettext('<strong>Error:</strong> Database password empty.');
726         // prefix could be empty
727     }
728
729     if (empty($_POST['dbname'])) {
730         $messages