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