xref: /dokuwiki/install.php (revision a672ef756673fe699e0bacd528ef2a880da4c20b)
1<?php
2/*><div style="width:60%; margin: auto; background-color: #fcc;
3                border: 1px solid #faa; padding: 0.5em 1em;">
4    <h1 style="font-size: 120%">No PHP Support</h1>
5
6    It seems this server has no PHP support enabled. You will need to
7    enable PHP before you can install and run DokuWiki. Contact your hosting
8    provider if you're unsure what this means.
9
10</div>*/
11/**
12 * Dokuwiki installation assistance
13 *
14 * @author      Chris Smith <chris@jalakai.co.uk>
15 */
16
17if(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/');
18if(!defined('DOKU_CONF')) define('DOKU_CONF',DOKU_INC.'conf/');
19if(!defined('DOKU_LOCAL')) define('DOKU_LOCAL',DOKU_INC.'conf/');
20
21// load and initialize the core system
22require_once(DOKU_INC.'inc/init.php');
23
24// check for error reporting override or set error reporting to sane values
25if (!defined('DOKU_E_LEVEL')) { error_reporting(E_ALL ^ E_NOTICE); }
26else { error_reporting(DOKU_E_LEVEL); }
27
28// language strings
29require_once(DOKU_INC.'inc/lang/en/lang.php');
30if(isset($_REQUEST['l']) && !is_array($_REQUEST['l'])) {
31    $LC = preg_replace('/[^a-z\-]+/','',$_REQUEST['l']);
32}
33if(empty($LC)) $LC = 'en';
34if($LC && $LC != 'en' ) {
35    require_once(DOKU_INC.'inc/lang/'.$LC.'/lang.php');
36}
37
38// initialise variables ...
39$error = array();
40
41// begin output
42header('Content-Type: text/html; charset=utf-8');
43?>
44<!DOCTYPE html>
45<html lang="<?php echo $LC?>" dir="<?php echo $lang['direction']?>">
46<head>
47    <meta charset="utf-8" />
48    <title><?php echo $lang['i_installer']?></title>
49    <style type="text/css">
50        body { width: 90%; margin: 0 auto; font: 84% Verdana, Helvetica, Arial, sans-serif; }
51        img { border: none }
52        br.cl { clear:both; }
53        code { font-size: 110%; color: #800000; }
54        fieldset { border: none }
55        label { display: block; margin-top: 0.5em; }
56        select.text, input.text { width: 30em; margin: 0 0.5em; }
57        a {text-decoration: none}
58    </style>
59    <script type="text/javascript">
60        function acltoggle(){
61            var cb = document.getElementById('acl');
62            var fs = document.getElementById('acldep');
63            if(!cb || !fs) return;
64            if(cb.checked){
65                fs.style.display = '';
66            }else{
67                fs.style.display = 'none';
68            }
69        }
70        window.onload = function(){
71            acltoggle();
72            var cb = document.getElementById('acl');
73            if(cb) cb.onchange = acltoggle;
74        };
75    </script>
76</head>
77<body style="">
78    <h1 style="float:left">
79        <img src="lib/exe/fetch.php?media=wiki:dokuwiki-128.png"
80             style="vertical-align: middle;" alt="" height="64" width="64" />
81        <?php echo $lang['i_installer']?>
82    </h1>
83    <div style="float:right; margin: 1em;">
84        <?php langsel()?>
85    </div>
86    <br class="cl" />
87
88    <div style="float: right; width: 34%;">
89        <?php
90            if(file_exists(DOKU_INC.'inc/lang/'.$LC.'/install.html')){
91                include(DOKU_INC.'inc/lang/'.$LC.'/install.html');
92            }else{
93                print "<div lang=\"en\" dir=\"ltr\">\n";
94                include(DOKU_INC.'inc/lang/en/install.html');
95                print "</div>\n";
96            }
97        ?>
98        <a style="background: transparent url(data/dont-panic-if-you-see-this-in-your-logs-it-means-your-directory-permissions-are-correct.png) left top no-repeat;
99                  display: block; width:380px; height:73px; border:none; clear:both;"
100           target="_blank"
101           href="http://www.dokuwiki.org/security#web_access_security"></a>
102    </div>
103
104    <div style="float: left; width: 58%;">
105        <?php
106            try {
107                if(! (check_functions() && check_permissions()) ){
108                    echo '<p>'.$lang['i_problems'].'</p>';
109                    print_errors();
110                    print_retry();
111                }elseif(!check_configs()){
112                    echo '<p>'.$lang['i_modified'].'</p>';
113                    print_errors();
114                }elseif(check_data($_REQUEST['d'])){
115                    // check_data has sanitized all input parameters
116                    if(!store_data($_REQUEST['d'])){
117                        echo '<p>'.$lang['i_failure'].'</p>';
118                        print_errors();
119                    }else{
120                        echo '<p>'.$lang['i_success'].'</p>';
121                    }
122                }else{
123                    print_errors();
124                    print_form($_REQUEST['d']);
125                }
126            } catch (Exception $e) {
127                echo 'Caught exception: ',  $e->getMessage(), "\n";
128            }
129        ?>
130    </div>
131
132
133<div style="clear: both">
134  <a href="http://dokuwiki.org/"><img src="lib/tpl/dokuwiki/images/button-dw.png" alt="driven by DokuWiki" /></a>
135  <a href="http://php.net"><img src="lib/tpl/dokuwiki/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 *
144 * @param array $d submitted entry 'd' of request data
145 */
146function print_form($d){
147    global $lang;
148    global $LC;
149
150    include(DOKU_CONF.'license.php');
151
152    if(!is_array($d)) $d = array();
153    $d = array_map('hsc',$d);
154
155    if(!isset($d['acl'])) $d['acl']=1;
156    if(!isset($d['pop'])) $d['pop']=1;
157
158    ?>
159    <form action="" method="post">
160    <input type="hidden" name="l" value="<?php echo $LC ?>" />
161    <fieldset>
162        <label for="title"><?php echo $lang['i_wikiname']?>
163        <input type="text" name="d[title]" id="title" value="<?php echo $d['title'] ?>" style="width: 20em;" />
164        </label>
165
166        <fieldset style="margin-top: 1em;">
167            <label for="acl">
168            <input type="checkbox" name="d[acl]" id="acl" <?php echo(($d['acl'] ? ' checked="checked"' : ''));?> />
169            <?php echo $lang['i_enableacl']?></label>
170
171            <fieldset id="acldep">
172                <label for="superuser"><?php echo $lang['i_superuser']?></label>
173                <input class="text" type="text" name="d[superuser]" id="superuser" value="<?php echo $d['superuser'] ?>" />
174
175                <label for="fullname"><?php echo $lang['fullname']?></label>
176                <input class="text" type="text" name="d[fullname]" id="fullname" value="<?php echo $d['fullname'] ?>" />
177
178                <label for="email"><?php echo $lang['email']?></label>
179                <input class="text" type="text" name="d[email]" id="email" value="<?php echo $d['email'] ?>" />
180
181                <label for="password"><?php echo $lang['pass']?></label>
182                <input class="text" type="password" name="d[password]" id="password" />
183
184                <label for="confirm"><?php echo $lang['passchk']?></label>
185                <input class="text" type="password" name="d[confirm]" id="confirm" />
186
187                <label for="policy"><?php echo $lang['i_policy']?></label>
188                <select class="text" name="d[policy]" id="policy">
189                    <option value="0" <?php echo ($d['policy'] == 0)?'selected="selected"':'' ?>><?php echo $lang['i_pol0']?></option>
190                    <option value="1" <?php echo ($d['policy'] == 1)?'selected="selected"':'' ?>><?php echo $lang['i_pol1']?></option>
191                    <option value="2" <?php echo ($d['policy'] == 2)?'selected="selected"':'' ?>><?php echo $lang['i_pol2']?></option>
192                </select>
193
194                <label for="allowreg">
195                    <input type="checkbox" name="d[allowreg]" id="allowreg" <?php echo(($d['allowreg'] ? ' checked="checked"' : ''));?> />
196                    <?php echo $lang['i_allowreg']?>
197                </label>
198            </fieldset>
199        </fieldset>
200
201        <fieldset>
202            <p><?php echo $lang['i_license']?></p>
203            <?php
204            array_push($license,array('name' => $lang['i_license_none'], 'url'=>''));
205            if(empty($d['license'])) $d['license'] = 'cc-by-sa';
206            foreach($license as $key => $lic){
207                echo '<label for="lic_'.$key.'">';
208                echo '<input type="radio" name="d[license]" value="'.hsc($key).'" id="lic_'.$key.'"'.
209                     (($d['license'] === $key)?' checked="checked"':'').'>';
210                echo hsc($lic['name']);
211                if($lic['url']) echo ' <a href="'.$lic['url'].'" target="_blank"><sup>[?]</sup></a>';
212                echo '</label>';
213            }
214            ?>
215        </fieldset>
216
217        <fieldset>
218            <p><?php echo $lang['i_pop_field']?></p>
219            <label for="pop">
220                <input type="checkbox" name="d[pop]" id="pop" <?php echo(($d['pop'] ? ' checked="checked"' : ''));?> />
221                <?php echo $lang['i_pop_label']?> <a href="http://www.dokuwiki.org/popularity" target="_blank"><sup>[?]</sup></a>
222            </label>
223        </fieldset>
224
225    </fieldset>
226    <fieldset id="process">
227        <button type="submit" name="submit"><?php echo $lang['btn_save']?></button>
228    </fieldset>
229    </form>
230    <?php
231}
232
233function print_retry() {
234    global $lang;
235    global $LC;
236    ?>
237    <form action="" method="get">
238      <fieldset>
239        <input type="hidden" name="l" value="<?php echo $LC ?>" />
240        <button type="submit"><?php echo $lang['i_retry'];?></button>
241      </fieldset>
242    </form>
243    <?php
244}
245
246/**
247 * Check validity of data
248 *
249 * @author Andreas Gohr
250 *
251 * @param array $d
252 * @return bool ok?
253 */
254function check_data(&$d){
255    static $form_default = array(
256        'title'     => '',
257        'acl'       => '1',
258        'superuser' => '',
259        'fullname'  => '',
260        'email'     => '',
261        'password'  => '',
262        'confirm'   => '',
263        'policy'    => '0',
264        'allowreg'  => '0',
265        'license'   => 'cc-by-sa'
266    );
267    global $lang;
268    global $error;
269
270    if(!is_array($d)) $d = array();
271    foreach($d as $k => $v) {
272        if(is_array($v))
273            unset($d[$k]);
274        else
275            $d[$k] = (string)$v;
276    }
277
278    //autolowercase the username
279    $d['superuser'] = isset($d['superuser']) ? strtolower($d['superuser']) : "";
280
281    $ok = false;
282
283    if(isset($_REQUEST['submit'])) {
284        $ok = true;
285
286        // check input
287        if(empty($d['title'])){
288            $error[] = sprintf($lang['i_badval'],$lang['i_wikiname']);
289            $ok      = false;
290        }
291        if(isset($d['acl'])){
292            if(!preg_match('/^[a-z0-9_]+$/',$d['superuser'])){
293                $error[] = sprintf($lang['i_badval'],$lang['i_superuser']);
294                $ok      = false;
295            }
296            if(empty($d['password'])){
297                $error[] = sprintf($lang['i_badval'],$lang['pass']);
298                $ok      = false;
299            }
300            elseif(!isset($d['confirm']) || $d['confirm'] != $d['password']){
301                $error[] = sprintf($lang['i_badval'],$lang['passchk']);
302                $ok      = false;
303            }
304            if(empty($d['fullname']) || strstr($d['fullname'],':')){
305                $error[] = sprintf($lang['i_badval'],$lang['fullname']);
306                $ok      = false;
307            }
308            if(empty($d['email']) || strstr($d['email'],':') || !strstr($d['email'],'@')){
309                $error[] = sprintf($lang['i_badval'],$lang['email']);
310                $ok      = false;
311            }
312        }else{
313            // Since default = 1, browser won't send acl=0 when user untick acl
314            $d['acl'] = '0';
315        }
316    }
317    $d = array_merge($form_default, $d);
318    return $ok;
319}
320
321/**
322 * Writes the data to the config files
323 *
324 * @author  Chris Smith <chris@jalakai.co.uk>
325 *
326 * @param array $d
327 * @return bool
328 */
329function store_data($d){
330    global $LC;
331    $ok = true;
332    $d['policy'] = (int) $d['policy'];
333
334    // create local.php
335    $now    = gmdate('r');
336    $output = <<<EOT
337<?php
338/**
339 * Dokuwiki's Main Configuration File - Local Settings
340 * Auto-generated by install script
341 * Date: $now
342 */
343
344EOT;
345    // add any config options set by a previous installer
346    $preset = __DIR__.'/install.conf';
347    if(file_exists($preset)){
348        $output .= "# preset config options\n";
349        $output .= file_get_contents($preset);
350        $output .= "\n\n";
351        $output .= "# options selected in installer\n";
352        @unlink($preset);
353    }
354
355    $output .= '$conf[\'title\'] = \''.addslashes($d['title'])."';\n";
356    $output .= '$conf[\'lang\'] = \''.addslashes($LC)."';\n";
357    $output .= '$conf[\'license\'] = \''.addslashes($d['license'])."';\n";
358    if($d['acl']){
359        $output .= '$conf[\'useacl\'] = 1'.";\n";
360        $output .= "\$conf['superuser'] = '@admin';\n";
361    }
362    if(!$d['allowreg']){
363        $output .= '$conf[\'disableactions\'] = \'register\''.";\n";
364    }
365    $ok = $ok && fileWrite(DOKU_LOCAL.'local.php',$output);
366
367    if ($d['acl']) {
368        // hash the password
369        $phash = new PassHash();
370        $pass = $phash->hash_smd5($d['password']);
371
372        // create users.auth.php
373        $output = <<<EOT
374# users.auth.php
375# <?php exit()?>
376# Don't modify the lines above
377#
378# Userfile
379#
380# Auto-generated by install script
381# Date: $now
382#
383# Format:
384# login:passwordhash:Real Name:email:groups,comma,separated
385
386EOT;
387        // --- user:SMD5password:Real Name:email:groups,comma,seperated
388        $output = $output."\n".join(":",array($d['superuser'], $pass, $d['fullname'], $d['email'], 'admin,user'))."\n";
389        $ok = $ok && fileWrite(DOKU_LOCAL.'users.auth.php', $output);
390
391        // create acl.auth.php
392        $output = <<<EOT
393# acl.auth.php
394# <?php exit()?>
395# Don't modify the lines above
396#
397# Access Control Lists
398#
399# Auto-generated by install script
400# Date: $now
401
402EOT;
403        if($d['policy'] == 2){
404            $output .=  "*               @ALL          0\n";
405            $output .=  "*               @user         8\n";
406        }elseif($d['policy'] == 1){
407            $output .=  "*               @ALL          1\n";
408            $output .=  "*               @user         8\n";
409        }else{
410            $output .=  "*               @ALL          8\n";
411        }
412        $ok = $ok && fileWrite(DOKU_LOCAL.'acl.auth.php', $output);
413    }
414
415    // enable popularity submission
416    if($d['pop']){
417        @touch(DOKU_INC.'data/cache/autosubmit.txt');
418    }
419
420    // disable auth plugins til needed
421    $output = <<<EOT
422<?php
423/*
424 * Local plugin enable/disable settings
425 *
426 * Auto-generated by install script
427 * Date: $now
428 */
429
430\$plugins['authad']    = 0;
431\$plugins['authldap']  = 0;
432\$plugins['authmysql'] = 0;
433\$plugins['authpgsql'] = 0;
434
435EOT;
436    $ok = $ok && fileWrite(DOKU_LOCAL.'plugins.local.php', $output);
437
438    return $ok;
439}
440
441/**
442 * Write the given content to a file
443 *
444 * @author  Chris Smith <chris@jalakai.co.uk>
445 *
446 * @param string $filename
447 * @param string $data
448 * @return bool
449 */
450function fileWrite($filename, $data) {
451    global $error;
452    global $lang;
453
454    if (($fp = @fopen($filename, 'wb')) === false) {
455        $filename = str_replace($_SERVER['DOCUMENT_ROOT'],'{DOCUMENT_ROOT}/', $filename);
456        $error[]  = sprintf($lang['i_writeerr'],$filename);
457        return false;
458    }
459
460    if (!empty($data)) { fwrite($fp, $data);  }
461    fclose($fp);
462    return true;
463}
464
465
466/**
467 * check installation dependent local config files and tests for a known
468 * unmodified main config file
469 *
470 * @author      Chris Smith <chris@jalakai.co.uk>
471 *
472 * @return bool
473 */
474function check_configs(){
475    global $error;
476    global $lang;
477
478    $ok = true;
479
480    $config_files = array(
481        'local' => DOKU_LOCAL.'local.php',
482        'users' => DOKU_LOCAL.'users.auth.php',
483        'auth'  => DOKU_LOCAL.'acl.auth.php'
484    );
485
486    // configs shouldn't exist
487    foreach ($config_files as $file) {
488        if (file_exists($file) && filesize($file)) {
489            $file    = str_replace($_SERVER['DOCUMENT_ROOT'],'{DOCUMENT_ROOT}/', $file);
490            $error[] = sprintf($lang['i_confexists'],$file);
491            $ok      = false;
492        }
493    }
494    return $ok;
495}
496
497
498/**
499 * Check other installation dir/file permission requirements
500 *
501 * @author      Chris Smith <chris@jalakai.co.uk>
502 *
503 * @return bool
504 */
505function check_permissions(){
506    global $error;
507    global $lang;
508
509    $dirs = array(
510        'conf'        => DOKU_LOCAL,
511        'data'        => DOKU_INC.'data',
512        'pages'       => DOKU_INC.'data/pages',
513        'attic'       => DOKU_INC.'data/attic',
514        'media'       => DOKU_INC.'data/media',
515        'media_attic' => DOKU_INC.'data/media_attic',
516        'media_meta'  => DOKU_INC.'data/media_meta',
517        'meta'        => DOKU_INC.'data/meta',
518        'cache'       => DOKU_INC.'data/cache',
519        'locks'       => DOKU_INC.'data/locks',
520        'index'       => DOKU_INC.'data/index',
521        'tmp'         => DOKU_INC.'data/tmp'
522    );
523
524    $ok = true;
525    foreach($dirs as $dir){
526        if(!file_exists("$dir/.") || !is_writable($dir)){
527            $dir     = str_replace($_SERVER['DOCUMENT_ROOT'],'{DOCUMENT_ROOT}', $dir);
528            $error[] = sprintf($lang['i_permfail'],$dir);
529            $ok      = false;
530        }
531    }
532    return $ok;
533}
534
535/**
536 * Check the availability of functions used in DokuWiki and the PHP version
537 *
538 * @author Andreas Gohr <andi@splitbrain.org>
539 *
540 * @return bool
541 */
542function check_functions(){
543    global $error;
544    global $lang;
545    $ok = true;
546
547    if(version_compare(phpversion(),'5.6.0','<')){
548        $error[] = sprintf($lang['i_phpver'],phpversion(),'5.6.0');
549        $ok = false;
550    }
551
552    if(ini_get('mbstring.func_overload') != 0){
553        $error[] = $lang['i_mbfuncoverload'];
554        $ok = false;
555    }
556
557    $funcs = explode(' ','addslashes call_user_func chmod copy fgets '.
558                         'file file_exists fseek flush filesize ftell fopen '.
559                         'glob header ignore_user_abort ini_get mail mkdir '.
560                         'ob_start opendir parse_ini_file readfile realpath '.
561                         'rename rmdir serialize session_start unlink usleep '.
562                         'preg_replace file_get_contents htmlspecialchars_decode '.
563                         'spl_autoload_register stream_select fsockopen pack');
564
565    if (!function_exists('mb_substr')) {
566        $funcs[] = 'utf8_encode';
567        $funcs[] = 'utf8_decode';
568    }
569
570    foreach($funcs as $func){
571        if(!function_exists($func)){
572            $error[] = sprintf($lang['i_funcna'],$func);
573            $ok = false;
574        }
575    }
576    return $ok;
577}
578
579/**
580 * Print language selection
581 *
582 * @author Andreas Gohr <andi@splitbrain.org>
583 */
584function langsel(){
585    global $lang;
586    global $LC;
587
588    $dir = DOKU_INC.'inc/lang';
589    $dh  = opendir($dir);
590    if(!$dh) return;
591
592    $langs = array();
593    while (($file = readdir($dh)) !== false) {
594        if(preg_match('/^[\._]/',$file)) continue;
595        if(is_dir($dir.'/'.$file) && file_exists($dir.'/'.$file.'/lang.php')){
596            $langs[] = $file;
597        }
598    }
599    closedir($dh);
600    sort($langs);
601
602    echo '<form action="">';
603    echo $lang['i_chooselang'];
604    echo ': <select name="l" onchange="submit()">';
605    foreach($langs as $l){
606        $sel = ($l == $LC) ? 'selected="selected"' : '';
607        echo '<option value="'.$l.'" '.$sel.'>'.$l.'</option>';
608    }
609    echo '</select> ';
610    echo '<button type="submit">'.$lang['btn_update'].'</button>';
611    echo '</form>';
612}
613
614/**
615 * Print global error array
616 *
617 * @author Andreas Gohr <andi@splitbrain.org>
618 */
619function print_errors(){
620    global $error;
621    if(!empty($error)) {
622        echo '<ul>';
623        foreach ($error as $err){
624            echo "<li>$err</li>";
625        }
626        echo '</ul>';
627    }
628}
629