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