root/releases/0.9rc1/install.php

Revision 1424, 21.8 kB (checked in by rho, 10 months ago)

minor tweak

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