xref: /dokuwiki/install.php (revision f95ecbbf8b1de8bc1270d3cf91dfdf055ea5c78c)
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
12require_once(DOKU_INC.'inc/PassHash.class.php');
13
14// check for error reporting override or set error reporting to sane values
15if (!defined('DOKU_E_LEVEL')) { error_reporting(E_ALL ^ E_NOTICE); }
16else { error_reporting(DOKU_E_LEVEL); }
17
18// kill magic quotes
19if (get_magic_quotes_gpc() && !defined('MAGIC_QUOTES_STRIPPED')) {
20    if (!empty($_GET))    remove_magic_quotes($_GET);
21    if (!empty($_POST))   remove_magic_quotes($_POST);
22    if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE);
23    if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST);
24    @ini_set('magic_quotes_gpc', 0);
25    define('MAGIC_QUOTES_STRIPPED',1);
26}
27if (function_exists('set_magic_quotes_runtime')) @set_magic_quotes_runtime(0);
28@ini_set('magic_quotes_sybase',0);
29
30// language strings
31require_once(DOKU_INC.'inc/lang/en/lang.php');
32if(isset($_REQUEST['l']) && !is_array($_REQUEST['l'])) {
33    $LC = preg_replace('/[^a-z\-]+/','',$_REQUEST['l']);
34}
35if(empty($LC)) $LC = 'en';
36if($LC && $LC != 'en' ) {
37    require_once(DOKU_INC.'inc/lang/'.$LC.'/lang.php');
38}
39
40// initialise variables ...
41$error = array();
42
43$dokuwiki_hash = array(
44    '2005-09-22'   => 'e33223e957b0b0a130d0520db08f8fb7',
45    '2006-03-05'   => '51295727f79ab9af309a2fd9e0b61acc',
46    '2006-03-09'   => '51295727f79ab9af309a2fd9e0b61acc',
47    '2006-11-06'   => 'b3a8af76845977c2000d85d6990dd72b',
48    '2007-05-24'   => 'd80f2740c84c4a6a791fd3c7a353536f',
49    '2007-06-26'   => 'b3ca19c7a654823144119980be73cd77',
50    '2008-05-04'   => '1e5c42eac3219d9e21927c39e3240aad',
51    '2009-02-14'   => 'ec8c04210732a14fdfce0f7f6eead865',
52    '2009-12-25'   => '993c4b2b385643efe5abf8e7010e11f4',
53    '2010-11-07'   => '7921d48195f4db21b8ead6d9bea801b8',
54    '2011-05-25'   => '4241865472edb6fa14a1227721008072',
55    '2011-11-10'   => 'b46ff19a7587966ac4df61cbab1b8b31',
56    '2012-01-25'   => '72c083c73608fc43c586901fd5dabb74',
57    '2012-09-10'   => 'eb0b3fc90056fbc12bac6f49f7764df3',
58    '2013-05-10'   => '7b62b75245f57f122d3e0f8ed7989623',
59    '2013-12-08'   => '263c76af309fbf083867c18a34ff5214',
60    '2014-05-05'   => '263c76af309fbf083867c18a34ff5214',
61    'devel'        => 'b3ddc3f793eac8c135176e535054c00a',
62);
63
64
65// begin output
66header('Content-Type: text/html; charset=utf-8');
67?>
68<!DOCTYPE html>
69<html lang="<?php echo $LC?>" dir="<?php echo $lang['direction']?>">
70<head>
71    <meta charset="utf-8" />
72    <title><?php echo $lang['i_installer']?></title>
73    <style type="text/css">
74        body { width: 90%; margin: 0 auto; font: 84% Verdana, Helvetica, Arial, sans-serif; }
75        img { border: none }
76        br.cl { clear:both; }
77        code { font-size: 110%; color: #800000; }
78        fieldset { border: none }
79        label { display: block; margin-top: 0.5em; }
80        select.text, input.text { width: 30em; margin: 0 0.5em; }
81        a {text-decoration: none}
82    </style>
83    <script type="text/javascript">
84        function acltoggle(){
85            var cb = document.getElementById('acl');
86            var fs = document.getElementById('acldep');
87            if(!cb || !fs) return;
88            if(cb.checked){
89                fs.style.display = '';
90            }else{
91                fs.style.display = 'none';
92            }
93        }
94        window.onload = function(){
95            acltoggle();
96            var cb = document.getElementById('acl');
97            if(cb) cb.onchange = acltoggle;
98        };
99    </script>
100</head>
101<body style="">
102    <h1 style="float:left">
103        <img src="lib/exe/fetch.php?media=wiki:dokuwiki-128.png"
104             style="vertical-align: middle;" alt="" height="64" width="64" />
105        <?php echo $lang['i_installer']?>
106    </h1>
107    <div style="float:right; margin: 1em;">
108        <?php langsel()?>
109    </div>
110    <br class="cl" />
111
112    <div style="float: right; width: 34%;">
113        <?php
114            if(@file_exists(DOKU_INC.'inc/lang/'.$LC.'/install.html')){
115                include(DOKU_INC.'inc/lang/'.$LC.'/install.html');
116            }else{
117                print "<div lang=\"en\" dir=\"ltr\">\n";
118                include(DOKU_INC.'inc/lang/en/install.html');
119                print "</div>\n";
120            }
121        ?>
122        <a style="background: transparent url(data/security.png) left top no-repeat;
123                  display: block; width:380px; height:73px; border:none; clear:both;"
124           target="_blank"
125           href="http://www.dokuwiki.org/security#web_access_security"></a>
126    </div>
127
128    <div style="float: left; width: 58%;">
129        <?php
130            if(! (check_functions() && check_permissions()) ){
131                echo '<p>'.$lang['i_problems'].'</p>';
132                print_errors();
133                print_retry();
134            }elseif(!check_configs()){
135                echo '<p>'.$lang['i_modified'].'</p>';
136                print_errors();
137            }elseif(check_data($_REQUEST['d'])){
138                // check_data has sanitized all input parameters
139                if(!store_data($_REQUEST['d'])){
140                    echo '<p>'.$lang['i_failure'].'</p>';
141                    print_errors();
142                }else{
143                    echo '<p>'.$lang['i_success'].'</p>';
144                }
145            }else{
146                print_errors();
147                print_form($_REQUEST['d']);
148            }
149        ?>
150    </div>
151
152
153<div style="clear: both">
154  <a href="http://dokuwiki.org/"><img src="lib/tpl/dokuwiki/images/button-dw.png" alt="driven by DokuWiki" /></a>
155  <a href="http://www.php.net"><img src="lib/tpl/dokuwiki/images/button-php.gif" alt="powered by PHP" /></a>
156</div>
157</body>
158</html>
159<?php
160
161/**
162 * Print the input form
163 */
164function print_form($d){
165    global $lang;
166    global $LC;
167
168    include(DOKU_CONF.'license.php');
169
170    if(!is_array($d)) $d = array();
171    $d = array_map('htmlspecialchars',$d);
172
173    if(!isset($d['acl'])) $d['acl']=1;
174    if(!isset($d['pop'])) $d['pop']=1;
175
176    ?>
177    <form action="" method="post">
178    <input type="hidden" name="l" value="<?php echo $LC ?>" />
179    <fieldset>
180        <label for="title"><?php echo $lang['i_wikiname']?>
181        <input type="text" name="d[title]" id="title" value="<?php echo $d['title'] ?>" style="width: 20em;" />
182        </label>
183
184        <fieldset style="margin-top: 1em;">
185            <label for="acl">
186            <input type="checkbox" name="d[acl]" id="acl" <?php echo(($d['acl'] ? ' checked="checked"' : ''));?> />
187            <?php echo $lang['i_enableacl']?></label>
188
189            <fieldset id="acldep">
190                <label for="superuser"><?php echo $lang['i_superuser']?></label>
191                <input class="text" type="text" name="d[superuser]" id="superuser" value="<?php echo $d['superuser'] ?>" />
192
193                <label for="fullname"><?php echo $lang['fullname']?></label>
194                <input class="text" type="text" name="d[fullname]" id="fullname" value="<?php echo $d['fullname'] ?>" />
195
196                <label for="email"><?php echo $lang['email']?></label>
197                <input class="text" type="text" name="d[email]" id="email" value="<?php echo $d['email'] ?>" />
198
199                <label for="password"><?php echo $lang['pass']?></label>
200                <input class="text" type="password" name="d[password]" id="password" />
201
202                <label for="confirm"><?php echo $lang['passchk']?></label>
203                <input class="text" type="password" name="d[confirm]" id="confirm" />
204
205                <label for="policy"><?php echo $lang['i_policy']?></label>
206                <select class="text" name="d[policy]" id="policy">
207                    <option value="0" <?php echo ($d['policy'] == 0)?'selected="selected"':'' ?>><?php echo $lang['i_pol0']?></option>
208                    <option value="1" <?php echo ($d['policy'] == 1)?'selected="selected"':'' ?>><?php echo $lang['i_pol1']?></option>
209                    <option value="2" <?php echo ($d['policy'] == 2)?'selected="selected"':'' ?>><?php echo $lang['i_pol2']?></option>
210                </select>
211
212                <label for="allowreg">
213                    <input type="checkbox" name="d[allowreg]" id="allowreg" <?php echo(($d['allowreg'] ? ' checked="checked"' : ''));?> />
214                    <?php echo $lang['i_allowreg']?>
215                </label>
216            </fieldset>
217        </fieldset>
218
219        <fieldset>
220            <p><?php echo $lang['i_license']?></p>
221            <?php
222            array_push($license,array('name' => $lang['i_license_none'], 'url'=>''));
223            if(empty($d['license'])) $d['license'] = 'cc-by-sa';
224            foreach($license as $key => $lic){
225                echo '<label for="lic_'.$key.'">';
226                echo '<input type="radio" name="d[license]" value="'.htmlspecialchars($key).'" id="lic_'.$key.'"'.
227                     (($d['license'] === $key)?' checked="checked"':'').'>';
228                echo htmlspecialchars($lic['name']);
229                if($lic['url']) echo ' <a href="'.$lic['url'].'" target="_blank"><sup>[?]</sup></a>';
230                echo '</label>';
231            }
232            ?>
233        </fieldset>
234
235        <fieldset>
236            <p><?php echo $lang['i_pop_field']?></p>
237            <label for="pop">
238                <input type="checkbox" name="d[pop]" id="pop" <?php echo(($d['pop'] ? ' checked="checked"' : ''));?> />
239                <?php echo $lang['i_pop_label']?> <a href="http://www.dokuwiki.org/popularity" target="_blank"><sup>[?]</sup></a>
240            </label>
241        </fieldset>
242
243    </fieldset>
244    <fieldset id="process">
245        <input class="button" type="submit" name="submit" value="<?php echo $lang['btn_save']?>" />
246    </fieldset>
247    </form>
248    <?php
249}
250
251function print_retry() {
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        <input class="button" type="submit" value="<?php echo $lang['i_retry'];?>" />
259      </fieldset>
260    </form>
261    <?php
262}
263
264/**
265 * Check validity of data
266 *
267 * @author Andreas Gohr
268 */
269function check_data(&$d){
270    static $form_default = array(
271        'title'     => '',
272        'acl'       => '1',
273        'superuser' => '',
274        'fullname'  => '',
275        'email'     => '',
276        'password'  => '',
277        'confirm'   => '',
278        'policy'    => '0',
279        'allowreg'  => '0',
280        'license'   => 'cc-by-sa'
281    );
282    global $lang;
283    global $error;
284
285    if(!is_array($d)) $d = array();
286    foreach($d as $k => $v) {
287        if(is_array($v))
288            unset($d[$k]);
289        else
290            $d[$k] = (string)$v;
291    }
292
293    //autolowercase the username
294    $d['superuser'] = isset($d['superuser']) ? strtolower($d['superuser']) : "";
295
296    $ok = false;
297
298    if(isset($_REQUEST['submit'])) {
299        $ok = true;
300
301        // check input
302        if(empty($d['title'])){
303            $error[] = sprintf($lang['i_badval'],$lang['i_wikiname']);
304            $ok      = false;
305        }
306        if(isset($d['acl'])){
307            if(!preg_match('/^[a-z0-9_]+$/',$d['superuser'])){
308                $error[] = sprintf($lang['i_badval'],$lang['i_superuser']);
309                $ok      = false;
310            }
311            if(empty($d['password'])){
312                $error[] = sprintf($lang['i_badval'],$lang['pass']);
313                $ok      = false;
314            }
315            elseif(!isset($d['confirm']) || $d['confirm'] != $d['password']){
316                $error[] = sprintf($lang['i_badval'],$lang['passchk']);
317                $ok      = false;
318            }
319            if(empty($d['fullname']) || strstr($d['fullname'],':')){
320                $error[] = sprintf($lang['i_badval'],$lang['fullname']);
321                $ok      = false;
322            }
323            if(empty($d['email']) || strstr($d['email'],':') || !strstr($d['email'],'@')){
324                $error[] = sprintf($lang['i_badval'],$lang['email']);
325                $ok      = false;
326            }
327        }
328    }
329    $d = array_merge($form_default, $d);
330    return $ok;
331}
332
333/**
334 * Writes the data to the config files
335 *
336 * @author  Chris Smith <chris@jalakai.co.uk>
337 */
338function store_data($d){
339    global $LC;
340    $ok = true;
341    $d['policy'] = (int) $d['policy'];
342
343    // create local.php
344    $now    = gmdate('r');
345    $output = <<<EOT
346<?php
347/**
348 * Dokuwiki's Main Configuration File - Local Settings
349 * Auto-generated by install script
350 * Date: $now
351 */
352
353EOT;
354    // add any config options set by a previous installer
355    $preset = __DIR__.'/install.conf';
356    if(file_exists($preset)){
357        $output .= "# preset config options\n";
358        $output .= file_get_contents($preset);
359        $output .= "\n\n";
360        $output .= "# options selected in installer\n";
361        @unlink($preset);
362    }
363
364    $output .= '$conf[\'title\'] = \''.addslashes($d['title'])."';\n";
365    $output .= '$conf[\'lang\'] = \''.addslashes($LC)."';\n";
366    $output .= '$conf[\'license\'] = \''.addslashes($d['license'])."';\n";
367    if($d['acl']){
368        $output .= '$conf[\'useacl\'] = 1'.";\n";
369        $output .= "\$conf['superuser'] = '@admin';\n";
370    }
371    if(!$d['allowreg']){
372        $output .= '$conf[\'disableactions\'] = \'register\''.";\n";
373    }
374    $ok = $ok && fileWrite(DOKU_LOCAL.'local.php',$output);
375
376    if ($d['acl']) {
377        // hash the password
378        $phash = new PassHash();
379        $pass = $phash->hash_smd5($d['password']);
380
381        // create users.auth.php
382        // --- user:SMD5password:Real Name:email:groups,comma,seperated
383        $output = join(":",array($d['superuser'], $pass, $d['fullname'], $d['email'], 'admin,user'));
384        $output = @file_get_contents(DOKU_CONF.'users.auth.php.dist')."\n$output\n";
385        $ok = $ok && fileWrite(DOKU_LOCAL.'users.auth.php', $output);
386
387        // create acl.auth.php
388        $output = <<<EOT
389# acl.auth.php
390# <?php exit()?>
391# Don't modify the lines above
392#
393# Access Control Lists
394#
395# Auto-generated by install script
396# Date: $now
397
398EOT;
399        if($d['policy'] == 2){
400            $output .=  "*               @ALL          0\n";
401            $output .=  "*               @user         8\n";
402        }elseif($d['policy'] == 1){
403            $output .=  "*               @ALL          1\n";
404            $output .=  "*               @user         8\n";
405        }else{
406            $output .=  "*               @ALL          8\n";
407        }
408        $ok = $ok && fileWrite(DOKU_LOCAL.'acl.auth.php', $output);
409    }
410
411    // enable popularity submission
412    if($d['pop']){
413        @touch(DOKU_INC.'data/cache/autosubmit.txt');
414    }
415
416    // disable auth plugins til needed
417    $output = <<<EOT
418<?php
419/*
420 * Local plugin enable/disable settings
421 *
422 * Auto-generated by install script
423 * Date: $now
424 */
425
426\$plugins['authad']    = 0;
427\$plugins['authldap']  = 0;
428\$plugins['authmysql'] = 0;
429\$plugins['authpgsql'] = 0;
430
431EOT;
432    $ok = $ok && fileWrite(DOKU_LOCAL.'plugins.local.php', $output);
433
434    return $ok;
435}
436
437/**
438 * Write the given content to a file
439 *
440 * @author  Chris Smith <chris@jalakai.co.uk>
441 */
442function fileWrite($filename, $data) {
443    global $error;
444    global $lang;
445
446    if (($fp = @fopen($filename, 'wb')) === false) {
447        $filename = str_replace($_SERVER['DOCUMENT_ROOT'],'{DOCUMENT_ROOT}/', $filename);
448        $error[]  = sprintf($lang['i_writeerr'],$filename);
449        return false;
450    }
451
452    if (!empty($data)) { fwrite($fp, $data);  }
453    fclose($fp);
454    return true;
455}
456
457
458/**
459 * check installation dependent local config files and tests for a known
460 * unmodified main config file
461 *
462 * @author      Chris Smith <chris@jalakai.co.uk>
463 */
464function check_configs(){
465    global $error;
466    global $lang;
467    global $dokuwiki_hash;
468
469    $ok = true;
470
471    $config_files = array(
472        'local' => DOKU_LOCAL.'local.php',
473        'users' => DOKU_LOCAL.'users.auth.php',
474        'auth'  => DOKU_LOCAL.'acl.auth.php'
475    );
476
477    // main dokuwiki config file (conf/dokuwiki.php) must not have been modified
478    $installation_hash = md5(preg_replace("/(\015\012)|(\015)/","\012",
479                             @file_get_contents(DOKU_CONF.'dokuwiki.php')));
480    if (!in_array($installation_hash, $dokuwiki_hash)) {
481        $error[] = sprintf($lang['i_badhash'],$installation_hash);
482        $ok = false;
483    }
484
485    // configs shouldn't exist
486    foreach ($config_files as $file) {
487        if (@file_exists($file) && filesize($file)) {
488            $file    = str_replace($_SERVER['DOCUMENT_ROOT'],'{DOCUMENT_ROOT}/', $file);
489            $error[] = sprintf($lang['i_confexists'],$file);
490            $ok      = false;
491        }
492    }
493    return $ok;
494}
495
496
497/**
498 * Check other installation dir/file permission requirements
499 *
500 * @author      Chris Smith <chris@jalakai.co.uk>
501 */
502function check_permissions(){
503    global $error;
504    global $lang;
505
506    $dirs = array(
507        'conf'        => DOKU_LOCAL,
508        'data'        => DOKU_INC.'data',
509        'pages'       => DOKU_INC.'data/pages',
510        'attic'       => DOKU_INC.'data/attic',
511        'media'       => DOKU_INC.'data/media',
512        'media_attic' => DOKU_INC.'data/media_attic',
513        'media_meta'  => DOKU_INC.'data/media_meta',
514        'meta'        => DOKU_INC.'data/meta',
515        'cache'       => DOKU_INC.'data/cache',
516        'locks'       => DOKU_INC.'data/locks',
517        'index'       => DOKU_INC.'data/index',
518        'tmp'         => DOKU_INC.'data/tmp'
519    );
520
521    $ok = true;
522    foreach($dirs as $dir){
523        if(!@file_exists("$dir/.") || !@is_writable($dir)){
524            $dir     = str_replace($_SERVER['DOCUMENT_ROOT'],'{DOCUMENT_ROOT}', $dir);
525            $error[] = sprintf($lang['i_permfail'],$dir);
526            $ok      = false;
527        }
528    }
529    return $ok;
530}
531
532/**
533 * Check the availability of functions used in DokuWiki and the PHP version
534 *
535 * @author Andreas Gohr <andi@splitbrain.org>
536 */
537function check_functions(){
538    global $error;
539    global $lang;
540    $ok = true;
541
542    if(version_compare(phpversion(),'5.2.0','<')){
543        $error[] = sprintf($lang['i_phpver'],phpversion(),'5.2.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 '<input type="submit" value="'.$lang['btn_update'].'" />';
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
625/**
626 * remove magic quotes recursivly
627 *
628 * @author Andreas Gohr <andi@splitbrain.org>
629 */
630function remove_magic_quotes(&$array) {
631    foreach (array_keys($array) as $key) {
632        if (is_array($array[$key])) {
633            remove_magic_quotes($array[$key]);
634        }else {
635            $array[$key] = stripslashes($array[$key]);
636        }
637    }
638}
639
640