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