xref: /dokuwiki/install.php (revision c5b85cc545ded3ffcb9cc6079486ea7b50eebdc3)
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    'rc2006-10-19' => '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
313				$output .=  "*               @admin      255\n";
314        $ok = $ok && fileWrite(DOKU_LOCAL.'acl.auth.php', $output);
315    }
316    return $ok;
317}
318
319/**
320 * Write the given content to a file
321 *
322 * @author  Chris Smith <chris@jalakai.co.uk>
323 */
324function fileWrite($filename, $data) {
325    global $error;
326    global $lang;
327
328    if (($fp = @fopen($filename, 'wb')) === false) {
329        $filename = str_replace($_SERVER['DOCUMENT_ROOT'],'{DOCUMENT_ROOT}/', $filename);
330        $error[]  = sprintf($lang['i_writeerr'],$filename);
331        return false;
332    }
333
334    if (!empty($data)) { fwrite($fp, $data);  }
335    fclose($fp);
336    return true;
337}
338
339
340/**
341 * check installation dependent local config files and tests for a known
342 * unmodified main config file
343 *
344 * @author      Chris Smith <chris@jalakai.co.uk>
345 */
346function check_configs(){
347    global $error;
348    global $lang;
349    global $dokuwiki_hash;
350
351    $ok = true;
352
353    $config_files = array(
354        'local' => DOKU_LOCAL.'local.php',
355        'users' => DOKU_LOCAL.'users.auth.php',
356        'auth'  => DOKU_LOCAL.'acl.auth.php'
357    );
358
359
360    // main dokuwiki config file (conf/dokuwiki.php) must not have been modified
361    $installation_hash = md5(preg_replace("/(\015\012)|(\015)/","\012",
362                             @file_get_contents(DOKU_CONF.'dokuwiki.php')));
363    if (!in_array($installation_hash, $dokuwiki_hash)) {
364        $error[] = sprintf($lang['i_badhash'],$installation_hash);
365        $ok = false;
366    }
367
368    // configs shouldn't exist
369    foreach ($config_files as $file) {
370        if (@file_exists($file)) {
371            $file    = str_replace($_SERVER['DOCUMENT_ROOT'],'{DOCUMENT_ROOT}/', $file);
372            $error[] = sprintf($lang['i_confexists'],$file);
373            $ok      = false;
374        }
375    }
376    return $ok;
377}
378
379
380/**
381 * Check other installation dir/file permission requirements
382 *
383 * @author      Chris Smith <chris@jalakai.co.uk>
384 */
385function check_permissions(){
386    global $error;
387    global $lang;
388
389    $dirs = array(
390        'conf'      => DOKU_LOCAL,
391        'data'      => DOKU_INC.'data',
392        'pages'     => DOKU_INC.'data/pages',
393        'attic'     => DOKU_INC.'data/attic',
394        'media'     => DOKU_INC.'data/media',
395        'meta'      => DOKU_INC.'data/meta',
396        'cache'     => DOKU_INC.'data/cache',
397        'locks'     => DOKU_INC.'data/locks',
398    );
399
400    $ok = true;
401    foreach($dirs as $dir){
402        if(!@file_exists("$dir/.") || !@is_writable($dir)){
403            $dir     = str_replace($_SERVER['DOCUMENT_ROOT'],'{DOCUMENT_ROOT}', $dir);
404            $error[] = sprintf($lang['i_permfail'],$dir);
405            $ok      = false;
406        }
407    }
408    return $ok;
409}
410
411/**
412 * Check the availability of functions used in DokuWiki
413 *
414 * @author Andreas Gohr <andi@splitbrain.org>
415 */
416function check_functions(){
417    global $error;
418    global $lang;
419    $funcs = explode(' ','addslashes basename call_user_func chmod copy fgets '.
420                         'file file_exists fseek flush filesize ftell fopen '.
421                         'glob header ignore_user_abort ini_get mail mkdir '.
422                         'ob_start opendir parse_ini_file readfile realpath '.
423                         'rename rmdir serialize session_start unlink usleep');
424
425    if (!function_exists('mb_substr')) {
426      $funcs[] = 'utf8_encode';
427      $funcs[] = 'utf8_decode';
428    }
429
430    $ok = true;
431    foreach($funcs as $func){
432        if(!function_exists($func)){
433            $error[] = sprintf($lang['i_funcna'],$func);
434            $ok = false;
435        }
436    }
437    return $ok;
438}
439
440/**
441 * Print language selection
442 *
443 * @author Andreas Gohr <andi@splitbrain.org>
444 */
445function langsel(){
446    global $lang;
447    global $LC;
448
449    $dir = DOKU_INC.'inc/lang';
450    $dh  = opendir($dir);
451    if(!$dh) return;
452
453    $langs = array();
454    while (($file = readdir($dh)) !== false) {
455        if(preg_match('/^[\._]/',$file)) continue;
456        if(is_dir($dir.'/'.$file) && @file_exists($dir.'/'.$file.'/lang.php')){
457            $langs[] = $file;
458        }
459    }
460    closedir($dh);
461    sort($langs);
462
463
464    echo '<form action="">';
465    echo $lang['i_chooselang'];
466    echo ': <select name="l" onchange="submit()">';
467    foreach($langs as $l){
468        $sel = ($l == $LC) ? 'selected="selected"' : '';
469        echo '<option value="'.$l.'" '.$sel.'>'.$l.'</option>';
470    }
471    echo '</select> ';
472    echo '<input type="submit" value="'.$lang['btn_update'].'" />';
473    echo '</form>';
474}
475
476/**
477 * Print gloabl error array
478 *
479 * @author Andreas Gohr <andi@splitbrain.org>
480 */
481function print_errors(){
482    global $error;
483    echo '<ul>';
484    foreach ($error as $err){
485        echo "<li>$err</li>";
486    }
487    echo '</ul>';
488}
489
490/**
491 * remove magic quotes recursivly
492 *
493 * @author Andreas Gohr <andi@splitbrain.org>
494 */
495function remove_magic_quotes(&$array) {
496  foreach (array_keys($array) as $key) {
497    if (is_array($array[$key])) {
498      remove_magic_quotes($array[$key]);
499    }else {
500      $array[$key] = stripslashes($array[$key]);
501    }
502  }
503}
504
505