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