xref: /dokuwiki/install.php (revision d613051ae9b5db0528293dbe6d918df6dbd7dc4e)
1<?php
2/**
3 *  Dokuwiki installation assistance
4 *
5 *  @author      Chris Smith <chris@jalakai.co.uk>
6 */
7
8if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__)).'/');
9if(!defined('DOKU_CONF')) define('DOKU_CONF',DOKU_INC.'conf/');
10if(!defined('DOKU_LOCAL')) define('DOKU_LOCAL',DOKU_INC.'conf/');
11
12// check for error reporting override or set error reporting to sane values
13if (!defined('DOKU_E_LEVEL')) { error_reporting(E_ALL ^ E_NOTICE); }
14else { error_reporting(DOKU_E_LEVEL); }
15
16// kill magic quotes
17if (get_magic_quotes_gpc() && !defined('MAGIC_QUOTES_STRIPPED')) {
18    if (!empty($_GET))    remove_magic_quotes($_GET);
19    if (!empty($_POST))   remove_magic_quotes($_POST);
20    if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE);
21    if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST);
22    @ini_set('magic_quotes_gpc', 0);
23    define('MAGIC_QUOTES_STRIPPED',1);
24}
25@set_magic_quotes_runtime(0);
26@ini_set('magic_quotes_sybase',0);
27
28// language strings
29require_once(DOKU_INC.'inc/lang/en/lang.php');
30$LC = preg_replace('/[^a-z\-]+/','',$_REQUEST['l']);
31if(!$LC) $LC = 'en';
32if($LC && $LC != 'en' ) {
33    require_once(DOKU_INC.'inc/lang/'.$LC.'/lang.php');
34}
35
36// initialise variables ...
37$error = array();
38
39$dokuwiki_hash = array(
40    '2005-09-22'   => 'e33223e957b0b0a130d0520db08f8fb7',
41    '2006-03-05'   => '51295727f79ab9af309a2fd9e0b61acc',
42    '2006-03-09'   => '51295727f79ab9af309a2fd9e0b61acc',
43    '2006-11-06'   => 'b3a8af76845977c2000d85d6990dd72b',
44);
45
46
47
48// begin output
49header('Content-Type: text/html; charset=utf-8');
50?>
51<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
52 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
53<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php echo $LC?>"
54 lang="<?php echo $LC?>" dir="<?php echo $lang['direction']?>">
55<head>
56    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
57    <title><?php echo $lang['i_installer']?></title>
58    <style type="text/css">
59        body { width: 90%; margin: 0 auto; font: 84% Verdana, Helvetica, Arial, sans-serif; }
60        img { border: none }
61        br.cl { clear:both; }
62        code { font-size: 110%; color: #800000; }
63        fieldset { border: none }
64        label { display: block; margin-top: 0.5em; }
65        select.text, input.text { width: 30em; margin: 0 0.5em; }
66    </style>
67    <script type="text/javascript" language="javascript">
68        function acltoggle(){
69            var cb = document.getElementById('acl');
70            var fs = document.getElementById('acldep');
71            if(!cb || !fs) return;
72            if(cb.checked){
73                fs.style.display = '';
74            }else{
75                fs.style.display = 'none';
76            }
77        }
78        window.onload = function(){
79            acltoggle();
80            var cb = document.getElementById('acl');
81            if(cb) cb.onchange = acltoggle;
82        };
83    </script>
84</head>
85<body style="">
86    <h1 style="float:left">
87        <img src="http://wiki.splitbrain.org/_media/wiki:dokuwiki-64.png"
88             style="vertical-align: middle;" alt="" />
89        <?php echo $lang['i_installer']?>
90    </h1>
91    <div style="float:right; margin: 1em;">
92        <?php langsel()?>
93    </div>
94    <br class="cl" />
95
96    <div style="float: right; width: 34%;">
97        <?php
98            if(@file_exists(DOKU_INC.'inc/lang/'.$LC.'/install.html')){
99                include(DOKU_INC.'inc/lang/'.$LC.'/install.html');
100            }else{
101                print "<div lang=\"en\" dir=\"ltr\">\n";
102                include(DOKU_INC.'inc/lang/en/install.html');
103                print "</div>\n";
104            }
105        ?>
106    </div>
107
108    <div style="float: left; width: 58%;">
109        <?php
110            if(! (check_functions() && check_permissions()) ){
111                echo '<p>'.$lang['i_problems'].'</p>';
112                print_errors();
113                print_retry();
114            }elseif(!check_configs()){
115                echo '<p>'.$lang['i_modified'].'</p>';
116                print_errors();
117            }elseif($_REQUEST['submit']){
118                if(!check_data($_REQUEST['d'])){
119                    print_errors();
120                    print_form($_REQUEST['d']);
121                }elseif(!store_data($_REQUEST['d'])){
122                    echo '<p>'.$lang['i_failure'].'</p>';
123                    print_errors();
124                }else{
125                    echo '<p>'.$lang['i_success'].'</p>';
126                }
127            }else{
128                print_form($_REQUEST['d']);
129            }
130        ?>
131    </div>
132
133<div style="clear: both">
134  <a href="http://wiki.splitbrain.org/wiki:dokuwiki"><img src="lib/tpl/default/images/button-dw.png" alt="driven by DokuWiki" /></a>
135  <a href="http://www.php.net"><img src="lib/tpl/default/images/button-php.gif" alt="powered by PHP" /></a>
136</div>
137</body>
138</html>
139<?php
140
141/**
142 * Print the input form
143 */
144function print_form($d){
145    global $lang;
146    global $LC;
147
148    if(!is_array($d)) $d = array();
149    $d = array_map('htmlspecialchars',$d);
150
151    if(!isset($d['acl'])) $d['acl']=1;
152
153    ?>
154    <form action="" method="post">
155    <input type="hidden" name="l" value="<?php echo $LC ?>" />
156    <fieldset>
157        <label for="title"><?php echo $lang['i_wikiname']?>
158        <input type="text" name="d[title]" id="title" value="<?php echo $d['title'] ?>" style="width: 20em;" />
159        </label>
160
161        <fieldset style="margin-top: 1em;">
162            <label for="acl">
163            <input type="checkbox" name="d[acl]" id="acl" <?php echo(($d['acl'] ? 'checked="checked"' : ''));?> />
164            <?php echo $lang['i_enableacl']?></label>
165
166            <fieldset id="acldep">
167                <label for="superuser"><?php echo $lang['i_superuser']?></label>
168                <input class="text" type="text" name="d[superuser]" id="superuser" value="<?php echo $d['superuser'] ?>" />
169
170                <label for="fullname"><?php echo $lang['fullname']?></label>
171                <input class="text" type="text" name="d[fullname]" id="fullname" value="<?php echo $d['fullname'] ?>" />
172
173                <label for="email"><?php echo $lang['email']?></label>
174                <input class="text" type="text" name="d[email]" id="email" value="<?php echo $d['email'] ?>" />
175
176                <label for="password"><?php echo $lang['pass']?></label>
177                <input class="text" type="password" name="d[password]" id="password" />
178
179                <label for="confirm"><?php echo $lang['passchk']?></label>
180                <input class="text" type="password" name="d[confirm]" id="confirm" />
181
182                <label for="policy"><?php echo $lang['i_policy']?></label>
183                <select class="text" name="d[policy]" id="policy">
184                    <option value="0" <?php echo ($d['policy'] == 0)?'selected="selected"':'' ?>><?php echo $lang['i_pol0']?></option>
185                    <option value="1" <?php echo ($d['policy'] == 1)?'selected="selected"':'' ?>><?php echo $lang['i_pol1']?></option>
186                    <option value="2" <?php echo ($d['policy'] == 2)?'selected="selected"':'' ?>><?php echo $lang['i_pol2']?></option>
187                </select>
188            </fieldset>
189        </fieldset>
190
191    </fieldset>
192    <fieldset id="process">
193        <input class="button" type="submit" name="submit" value="<?php echo $lang['btn_save']?>" />
194    </fieldset>
195    </form>
196    <?php
197}
198
199function print_retry() {
200  global $lang;
201?>
202    <form action="" method="get">
203      <fieldset>
204        <input class="button" type="submit" value="<?php echo $lang['i_retry'];?>" />
205      </fieldset>
206    </form>
207<?php
208}
209
210/**
211 * Check validity of data
212 *
213 * @author Andreas Gohr
214 */
215function check_data(&$d){
216    global $lang;
217    global $error;
218
219    //autolowercase the username
220    $d['superuser'] = strtolower($d['superuser']);
221
222    $ok = true;
223
224    // check input
225    if(empty($d['title'])){
226        $error[] = sprintf($lang['i_badval'],$lang['i_wikiname']);
227        $ok      = false;
228    }
229    if($d['acl']){
230        if(!preg_match('/^[a-z1-9_]+$/',$d['superuser'])){
231            $error[] = sprintf($lang['i_badval'],$lang['i_superuser']);
232            $ok      = false;
233        }
234        if(empty($d['password'])){
235            $error[] = sprintf($lang['i_badval'],$lang['pass']);
236            $ok      = false;
237        }
238        if($d['confirm'] != $d['password']){
239            $error[] = sprintf($lang['i_badval'],$lang['passchk']);
240            $ok      = false;
241        }
242        if(empty($d['fullname']) || strstr($d['fullname'],':')){
243            $error[] = sprintf($lang['i_badval'],$lang['fullname']);
244            $ok      = false;
245        }
246        if(empty($d['email']) || strstr($d['email'],':') || !strstr($d['email'],'@')){
247            $error[] = sprintf($lang['i_badval'],$lang['email']);
248            $ok      = false;
249        }
250    }
251    return $ok;
252}
253
254/**
255 * Writes the data to the config files
256 *
257 * @author  Chris Smith <chris@jalakai.co.uk>
258 */
259function store_data($d){
260    global $LC;
261    $ok = true;
262    $d['policy'] = (int) $d['policy'];
263
264    // create local.php
265    $now    = date('r');
266    $output = <<<EOT
267<?php
268/**
269 * Dokuwiki's Main Configuration File - Local Settings
270 * Auto-generated by install script
271 * Date: $now
272 */
273
274EOT;
275    $output .= '$conf[\'title\'] = \''.addslashes($d['title'])."';\n";
276    $output .= '$conf[\'lang\'] = \''.addslashes($LC)."';\n";
277    if($d['acl']){
278        $output .= '$conf[\'useacl\'] = 1'.";\n";
279        $output .= "\$conf['superuser'] = '@admin';\n";
280    }
281    $ok = $ok && fileWrite(DOKU_LOCAL.'local.php',$output);
282
283
284    if ($d['acl']) {
285        // create users.auth.php
286        // --- user:MD5password:Real Name:email:groups,comma,seperated
287        $output = join(":",array($d['superuser'], md5($d['password']), $d['fullname'], $d['email'], 'admin,user'));
288        $output = @file_get_contents(DOKU_CONF.'users.auth.php.dist')."\n$output\n";
289        $ok = $ok && fileWrite(DOKU_LOCAL.'users.auth.php', $output);
290
291        // create acl.auth.php
292        $output = <<<EOT
293# acl.auth.php
294# <?php exit()?>
295# Don't modify the lines above
296#
297# Access Control Lists
298#
299# Auto-generated by install script
300# Date: $now
301
302EOT;
303        if($d['policy'] == 2){
304            $output .=  "*               @ALL          0\n";
305            $output .=  "*               @user         8\n";
306        }elseif($d['policy'] == 1){
307            $output .=  "*               @ALL          1\n";
308            $output .=  "*               @user         8\n";
309        }else{
310            $output .=  "*               @ALL          8\n";
311        }
312        $ok = $ok && fileWrite(DOKU_LOCAL.'acl.auth.php', $output);
313    }
314    return $ok;
315}
316
317/**
318 * Write the given content to a file
319 *
320 * @author  Chris Smith <chris@jalakai.co.uk>
321 */
322function fileWrite($filename, $data) {
323    global $error;
324    global $lang;
325
326    if (($fp = @fopen($filename, 'wb')) === false) {
327        $filename = str_replace($_SERVER['DOCUMENT_ROOT'],'{DOCUMENT_ROOT}/', $filename);
328        $error[]  = sprintf($lang['i_writeerr'],$filename);
329        return false;
330    }
331
332    if (!empty($data)) { fwrite($fp, $data);  }
333    fclose($fp);
334    return true;
335}
336
337
338/**
339 * check installation dependent local config files and tests for a known
340 * unmodified main config file
341 *
342 * @author      Chris Smith <chris@jalakai.co.uk>
343 */
344function check_configs(){
345    global $error;
346    global $lang;
347    global $dokuwiki_hash;
348
349    $ok = true;
350
351    $config_files = array(
352        'local' => DOKU_LOCAL.'local.php',
353        'users' => DOKU_LOCAL.'users.auth.php',
354        'auth'  => DOKU_LOCAL.'acl.auth.php'
355    );
356
357
358    // main dokuwiki config file (conf/dokuwiki.php) must not have been modified
359    $installation_hash = md5(preg_replace("/(\015\012)|(\015)/","\012",
360                             @file_get_contents(DOKU_CONF.'dokuwiki.php')));
361    if (!in_array($installation_hash, $dokuwiki_hash)) {
362        $error[] = sprintf($lang['i_badhash'],$installation_hash);
363        $ok = false;
364    }
365
366    // configs shouldn't exist
367    foreach ($config_files as $file) {
368        if (@file_exists($file)) {
369            $file    = str_replace($_SERVER['DOCUMENT_ROOT'],'{DOCUMENT_ROOT}/', $file);
370            $error[] = sprintf($lang['i_confexists'],$file);
371            $ok      = false;
372        }
373    }
374    return $ok;
375}
376
377
378/**
379 * Check other installation dir/file permission requirements
380 *
381 * @author      Chris Smith <chris@jalakai.co.uk>
382 */
383function check_permissions(){
384    global $error;
385    global $lang;
386
387    $dirs = array(
388        'conf'      => DOKU_LOCAL,
389        'data'      => DOKU_INC.'data',
390        'pages'     => DOKU_INC.'data/pages',
391        'attic'     => DOKU_INC.'data/attic',
392        'media'     => DOKU_INC.'data/media',
393        'meta'      => DOKU_INC.'data/meta',
394        'cache'     => DOKU_INC.'data/cache',
395        'locks'     => DOKU_INC.'data/locks',
396    );
397
398    $ok = true;
399    foreach($dirs as $dir){
400        if(!@file_exists("$dir/.") || !@is_writable($dir)){
401            $dir     = str_replace($_SERVER['DOCUMENT_ROOT'],'{DOCUMENT_ROOT}', $dir);
402            $error[] = sprintf($lang['i_permfail'],$dir);
403            $ok      = false;
404        }
405    }
406    return $ok;
407}
408
409/**
410 * Check the availability of functions used in DokuWiki and the PHP version
411 *
412 * @author Andreas Gohr <andi@splitbrain.org>
413 */
414function check_functions(){
415    global $error;
416    global $lang;
417    $ok = true;
418
419    if(version_compare(phpversion(),'4.3.3','<')){
420        $error[] = sprintf($lang['i_phpver'],phpversion(),'4.3.3');
421        $ok = false;
422    }
423
424    $funcs = explode(' ','addslashes basename call_user_func chmod copy fgets '.
425                         'file file_exists fseek flush filesize ftell fopen '.
426                         'glob header ignore_user_abort ini_get mail mkdir '.
427                         'ob_start opendir parse_ini_file readfile realpath '.
428                         'rename rmdir serialize session_start unlink usleep '.
429                         'preg_replace');
430
431    if (!function_exists('mb_substr')) {
432      $funcs[] = 'utf8_encode';
433      $funcs[] = 'utf8_decode';
434    }
435
436    foreach($funcs as $func){
437        if(!function_exists($func)){
438            $error[] = sprintf($lang['i_funcna'],$func);
439            $ok = false;
440        }
441    }
442    return $ok;
443}
444
445/**
446 * Print language selection
447 *
448 * @author Andreas Gohr <andi@splitbrain.org>
449 */
450function langsel(){
451    global $lang;
452    global $LC;
453
454    $dir = DOKU_INC.'inc/lang';
455    $dh  = opendir($dir);
456    if(!$dh) return;
457
458    $langs = array();
459    while (($file = readdir($dh)) !== false) {
460        if(preg_match('/^[\._]/',$file)) continue;
461        if(is_dir($dir.'/'.$file) && @file_exists($dir.'/'.$file.'/lang.php')){
462            $langs[] = $file;
463        }
464    }
465    closedir($dh);
466    sort($langs);
467
468
469    echo '<form action="">';
470    echo $lang['i_chooselang'];
471    echo ': <select name="l" onchange="submit()">';
472    foreach($langs as $l){
473        $sel = ($l == $LC) ? 'selected="selected"' : '';
474        echo '<option value="'.$l.'" '.$sel.'>'.$l.'</option>';
475    }
476    echo '</select> ';
477    echo '<input type="submit" value="'.$lang['btn_update'].'" />';
478    echo '</form>';
479}
480
481/**
482 * Print gloabl error array
483 *
484 * @author Andreas Gohr <andi@splitbrain.org>
485 */
486function print_errors(){
487    global $error;
488    echo '<ul>';
489    foreach ($error as $err){
490        echo "<li>$err</li>";
491    }
492    echo '</ul>';
493}
494
495/**
496 * remove magic quotes recursivly
497 *
498 * @author Andreas Gohr <andi@splitbrain.org>
499 */
500function remove_magic_quotes(&$array) {
501  foreach (array_keys($array) as $key) {
502    if (is_array($array[$key])) {
503      remove_magic_quotes($array[$key]);
504    }else {
505      $array[$key] = stripslashes($array[$key]);
506    }
507  }
508}
509
510