xref: /dokuwiki/install.php (revision 70a6aa16e600e1ec2fc65fd0e84ece3b4a046fd7)
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-09-28' => 'c795599093bcde8765ed5df6c8dd7c4b',
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    $ok = true;
220
221    // check input
222    if(empty($d['title'])){
223        $error[] = sprintf($lang['i_badval'],$lang['i_wikiname']);
224        $ok      = false;
225    }
226    if($d['acl']){
227        if(!preg_match('/^[a-z1-9_]+$/',$d['superuser'])){
228            $error[] = sprintf($lang['i_badval'],$lang['i_superuser']);
229            $ok      = false;
230        }
231        if(empty($d['password'])){
232            $error[] = sprintf($lang['i_badval'],$lang['pass']);
233            $ok      = false;
234        }
235        if($d['confirm'] != $d['password']){
236            $error[] = sprintf($lang['i_badval'],$lang['passchk']);
237            $ok      = false;
238        }
239        if(empty($d['fullname']) || strstr($d['fullname'],':')){
240            $error[] = sprintf($lang['i_badval'],$lang['fullname']);
241            $ok      = false;
242        }
243        if(empty($d['email']) || strstr($d['fullname'],':')){
244            $error[] = sprintf($lang['i_badval'],$lang['email']);
245            $ok      = false;
246        }
247    }
248    return $ok;
249}
250
251/**
252 * Writes the data to the config files
253 *
254 * @author  Chris Smith <chris@jalakai.co.uk>
255 */
256function store_data($d){
257    global $LC;
258    $ok = true;
259    $d['policy'] = (int) $d['policy'];
260
261    // create local.php
262    $now    = date('r');
263    $output = <<<EOT
264<?php
265/**
266 * Dokuwiki's Main Configuration File - Local Settings
267 * Auto-generated by install script
268 * Date: $now
269 */
270
271EOT;
272    $output .= '$conf[\'title\'] = \''.addslashes($d['title'])."';\n";
273    $output .= '$conf[\'lang\'] = \''.addslashes($LC)."';\n";
274    if($d['acl']){
275        $output .= '$conf[\'useacl\'] = 1'.";\n";
276        $output .= "\$conf['superuser'] = '@admin';\n";
277    }
278    $ok = $ok && fileWrite(DOKU_LOCAL.'local.php',$output);
279
280
281    if ($d['acl']) {
282        // create users.auth.php
283        // --- user:MD5password:Real Name:email:groups,comma,seperated
284        $output = join(":",array($d['superuser'], md5($d['password']), $d['fullname'], $d['email'], 'admin,user'));
285        $output = @file_get_contents(DOKU_CONF.'users.auth.php.dist')."\n$output\n";
286        $ok = $ok && fileWrite(DOKU_LOCAL.'users.auth.php', $output);
287
288        // create acl.auth.php
289        $output = <<<EOT
290# acl.auth.php
291# <?php exit()?>
292# Don't modify the lines above
293#
294# Access Control Lists
295#
296# Auto-generated by install script
297# Date: $now
298
299EOT;
300        if($d['policy'] == 2){
301            $output .=  "*               @ALL          0\n";
302            $output .=  "*               @users        8\n";
303        }elseif($d['policy'] == 1){
304            $output .=  "*               @ALL          1\n";
305            $output .=  "*               @users        8\n";
306        }else{
307            $output .=  "*               @ALL          8\n";
308        }
309
310				$output .=  "*               @admin      255\n";
311        $ok = $ok && fileWrite(DOKU_LOCAL.'acl.auth.php', $output);
312    }
313    return $ok;
314}
315
316/**
317 * Write the given content to a file
318 *
319 * @author  Chris Smith <chris@jalakai.co.uk>
320 */
321function fileWrite($filename, $data) {
322    global $error;
323    global $lang;
324
325    if (($fp = @fopen($filename, 'wb')) === false) {
326        $filename = str_replace($_SERVER['DOCUMENT_ROOT'],'{DOCUMENT_ROOT}/', $filename);
327        $error[]  = sprintf($lang['i_writeerr'],$filename);
328        return false;
329    }
330
331    if (!empty($data)) { fwrite($fp, $data);  }
332    fclose($fp);
333    return true;
334}
335
336
337/**
338 * check installation dependent local config files and tests for a known
339 * unmodified main config file
340 *
341 * @author      Chris Smith <chris@jalakai.co.uk>
342 */
343function check_configs(){
344    global $error;
345    global $lang;
346    global $dokuwiki_hash;
347
348    $ok = true;
349
350    $config_files = array(
351        'local' => DOKU_LOCAL.'local.php',
352        'users' => DOKU_LOCAL.'users.auth.php',
353        'auth'  => DOKU_LOCAL.'acl.auth.php'
354    );
355
356
357    // main dokuwiki config file (conf/dokuwiki.php) must not have been modified
358    $installation_hash = md5(@file_get_contents(DOKU_CONF.'dokuwiki.php'));
359    if (!in_array($installation_hash, $dokuwiki_hash)) {
360        $error[] = sprintf($lang['i_badhash'],$installation_hash);
361        $ok = false;
362    }
363
364    // configs shouldn't exist
365    foreach ($config_files as $file) {
366        if (@file_exists($file)) {
367            $file    = str_replace($_SERVER['DOCUMENT_ROOT'],'{DOCUMENT_ROOT}/', $file);
368            $error[] = sprintf($lang['i_confexists'],$file);
369            $ok      = false;
370        }
371    }
372    return $ok;
373}
374
375
376/**
377 * Check other installation dir/file permission requirements
378 *
379 * @author      Chris Smith <chris@jalakai.co.uk>
380 */
381function check_permissions(){
382    global $error;
383    global $lang;
384
385    $dirs = array(
386        'conf'      => DOKU_LOCAL,
387        'data'      => DOKU_INC.'data',
388        'pages'     => DOKU_INC.'data/pages',
389        'attic'     => DOKU_INC.'data/attic',
390        'media'     => DOKU_INC.'data/media',
391        'meta'      => DOKU_INC.'data/meta',
392        'cache'     => DOKU_INC.'data/cache',
393        'locks'     => DOKU_INC.'data/locks',
394    );
395
396    $ok = true;
397    foreach($dirs as $dir){
398        if(!@file_exists("$dir/.") || !@is_writable($dir)){
399            $dir     = str_replace($_SERVER['DOCUMENT_ROOT'],'{DOCUMENT_ROOT}', $dir);
400            $error[] = sprintf($lang['i_permfail'],$dir);
401            $ok      = false;
402        }
403    }
404    return $ok;
405}
406
407/**
408 * Check the availability of functions used in DokuWiki
409 *
410 * @author Andreas Gohr <andi@splitbrain.org>
411 */
412function check_functions(){
413    global $error;
414    global $lang;
415    $funcs = explode(' ','addslashes basename call_user_func chmod copy fgets '.
416                         'file file_exists fseek flush filesize ftell fopen '.
417                         'glob header ignore_user_abort ini_get mail mkdir '.
418                         'ob_start opendir parse_ini_file readfile realpath '.
419                         'rename rmdir serialize session_start unlink usleep');
420
421    if (!function_exists('mb_substr')) {
422      $funcs[] = 'utf8_encode';
423      $funcs[] = 'utf8_decode';
424    }
425
426    $ok = true;
427    foreach($funcs as $func){
428        if(!function_exists($func)){
429            $error[] = sprintf($lang['i_funcna'],$func);
430            $ok = false;
431        }
432    }
433    return $ok;
434}
435
436/**
437 * Print language selection
438 *
439 * @author Andreas Gohr <andi@splitbrain.org>
440 */
441function langsel(){
442    global $lang;
443    global $LC;
444
445    $dir = DOKU_INC.'inc/lang';
446    $dh  = opendir($dir);
447    if(!$dh) return;
448
449    $langs = array();
450    while (($file = readdir($dh)) !== false) {
451        if(preg_match('/^[\._]/',$file)) continue;
452        if(is_dir($dir.'/'.$file) && @file_exists($dir.'/'.$file.'/lang.php')){
453            $langs[] = $file;
454        }
455    }
456    closedir($dh);
457    sort($langs);
458
459
460    echo '<form action="">';
461    echo $lang['i_chooselang'];
462    echo ': <select name="l" onchange="submit()">';
463    foreach($langs as $l){
464        $sel = ($l == $LC) ? 'selected="selected"' : '';
465        echo '<option value="'.$l.'" '.$sel.'>'.$l.'</option>';
466    }
467    echo '</select> ';
468    echo '<input type="submit" value="'.$lang['btn_update'].'" />';
469    echo '</form>';
470}
471
472/**
473 * Print gloabl error array
474 *
475 * @author Andreas Gohr <andi@splitbrain.org>
476 */
477function print_errors(){
478    global $error;
479    echo '<ul>';
480    foreach ($error as $err){
481        echo "<li>$err</li>";
482    }
483    echo '</ul>';
484}
485
486/**
487 * remove magic quotes recursivly
488 *
489 * @author Andreas Gohr <andi@splitbrain.org>
490 */
491function remove_magic_quotes(&$array) {
492  foreach (array_keys($array) as $key) {
493    if (is_array($array[$key])) {
494      remove_magic_quotes($array[$key]);
495    }else {
496      $array[$key] = stripslashes($array[$key]);
497    }
498  }
499}
500
501