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