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