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