xref: /dokuwiki/install.php (revision 0fdcfd96106295c8aac3f83b36be0ac4e3b21d46)
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-10-28'   => '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            </fieldset>
211        </fieldset>
212
213        <fieldset>
214            <p><?php echo $lang['i_license']?></p>
215            <?php
216            array_push($license,array('name' => $lang['i_license_none'], 'url'=>''));
217            if(empty($d['license'])) $d['license'] = 'cc-by-sa';
218            foreach($license as $key => $lic){
219                echo '<label for="lic_'.$key.'">';
220                echo '<input type="radio" name="d[license]" value="'.htmlspecialchars($key).'" id="lic_'.$key.'"'.
221                     (($d['license'] === $key)?' checked="checked"':'').'>';
222                echo htmlspecialchars($lic['name']);
223                if($lic['url']) echo ' <a href="'.$lic['url'].'" target="_blank"><sup>[?]</sup></a>';
224                echo '</label>';
225            }
226            ?>
227        </fieldset>
228
229        <fieldset>
230            <p><?php echo $lang['i_pop_field']?></p>
231            <label for="pop">
232                <input type="checkbox" name="d[pop]" id="pop" <?php echo(($d['pop'] ? ' checked="checked"' : ''));?> />
233                <?php echo $lang['i_pop_label']?> <a href="http://www.dokuwiki.org/popularity" target="_blank"><sup>[?]</sup></a>
234            </label>
235        </fieldset>
236
237    </fieldset>
238    <fieldset id="process">
239        <input class="button" type="submit" name="submit" value="<?php echo $lang['btn_save']?>" />
240    </fieldset>
241    </form>
242    <?php
243}
244
245function print_retry() {
246    global $lang;
247    global $LC;
248    ?>
249    <form action="" method="get">
250      <fieldset>
251        <input type="hidden" name="l" value="<?php echo $LC ?>" />
252        <input class="button" type="submit" value="<?php echo $lang['i_retry'];?>" />
253      </fieldset>
254    </form>
255    <?php
256}
257
258/**
259 * Check validity of data
260 *
261 * @author Andreas Gohr
262 */
263function check_data(&$d){
264    static $form_default = array(
265        'title'     => '',
266        'acl'       => '1',
267        'superuser' => '',
268        'fullname'  => '',
269        'email'     => '',
270        'password'  => '',
271        'confirm'   => '',
272        'policy'    => '0',
273        'license'   => 'cc-by-sa'
274    );
275    global $lang;
276    global $error;
277
278    if(!is_array($d)) $d = array();
279    foreach($d as $k => $v) {
280        if(is_array($v))
281            unset($d[$k]);
282        else
283            $d[$k] = (string)$v;
284    }
285
286    //autolowercase the username
287    $d['superuser'] = isset($d['superuser']) ? strtolower($d['superuser']) : "";
288
289    $ok = false;
290
291    if(isset($_REQUEST['submit'])) {
292        $ok = true;
293
294        // check input
295        if(empty($d['title'])){
296            $error[] = sprintf($lang['i_badval'],$lang['i_wikiname']);
297            $ok      = false;
298        }
299        if(isset($d['acl'])){
300            if(!preg_match('/^[a-z0-9_]+$/',$d['superuser'])){
301                $error[] = sprintf($lang['i_badval'],$lang['i_superuser']);
302                $ok      = false;
303            }
304            if(empty($d['password'])){
305                $error[] = sprintf($lang['i_badval'],$lang['pass']);
306                $ok      = false;
307            }
308            elseif(!isset($d['confirm']) || $d['confirm'] != $d['password']){
309                $error[] = sprintf($lang['i_badval'],$lang['passchk']);
310                $ok      = false;
311            }
312            if(empty($d['fullname']) || strstr($d['fullname'],':')){
313                $error[] = sprintf($lang['i_badval'],$lang['fullname']);
314                $ok      = false;
315            }
316            if(empty($d['email']) || strstr($d['email'],':') || !strstr($d['email'],'@')){
317                $error[] = sprintf($lang['i_badval'],$lang['email']);
318                $ok      = false;
319            }
320        }
321    }
322    $d = array_merge($form_default, $d);
323    return $ok;
324}
325
326/**
327 * Writes the data to the config files
328 *
329 * @author  Chris Smith <chris@jalakai.co.uk>
330 */
331function store_data($d){
332    global $LC;
333    $ok = true;
334    $d['policy'] = (int) $d['policy'];
335
336    // create local.php
337    $now    = gmdate('r');
338    $output = <<<EOT
339<?php
340/**
341 * Dokuwiki's Main Configuration File - Local Settings
342 * Auto-generated by install script
343 * Date: $now
344 */
345
346EOT;
347    $output .= '$conf[\'title\'] = \''.addslashes($d['title'])."';\n";
348    $output .= '$conf[\'lang\'] = \''.addslashes($LC)."';\n";
349    $output .= '$conf[\'license\'] = \''.addslashes($d['license'])."';\n";
350    if($d['acl']){
351        $output .= '$conf[\'useacl\'] = 1'.";\n";
352        $output .= "\$conf['superuser'] = '@admin';\n";
353    }
354    $ok = $ok && fileWrite(DOKU_LOCAL.'local.php',$output);
355
356    if ($d['acl']) {
357        // hash the password
358        $phash = new PassHash();
359        $pass = $phash->hash_smd5($d['password']);
360
361        // create users.auth.php
362        // --- user:SMD5password:Real Name:email:groups,comma,seperated
363        $output = join(":",array($d['superuser'], $pass, $d['fullname'], $d['email'], 'admin,user'));
364        $output = @file_get_contents(DOKU_CONF.'users.auth.php.dist')."\n$output\n";
365        $ok = $ok && fileWrite(DOKU_LOCAL.'users.auth.php', $output);
366
367        // create acl.auth.php
368        $output = <<<EOT
369# acl.auth.php
370# <?php exit()?>
371# Don't modify the lines above
372#
373# Access Control Lists
374#
375# Auto-generated by install script
376# Date: $now
377
378EOT;
379        if($d['policy'] == 2){
380            $output .=  "*               @ALL          0\n";
381            $output .=  "*               @user         8\n";
382        }elseif($d['policy'] == 1){
383            $output .=  "*               @ALL          1\n";
384            $output .=  "*               @user         8\n";
385        }else{
386            $output .=  "*               @ALL          8\n";
387        }
388        $ok = $ok && fileWrite(DOKU_LOCAL.'acl.auth.php', $output);
389    }
390
391    // enable popularity submission
392    if($d['pop']){
393        @touch(DOKU_INC.'data/cache/autosubmit.txt');
394    }
395
396    // disable auth plugins til needed
397    $output = <<<EOT
398<?php
399/*
400 * Local plugin enable/disable settings
401 *
402 * Auto-generated by install script
403 * Date: $now
404 */
405
406\$plugins['authad']    = 0;
407\$plugins['authldap']  = 0;
408\$plugins['authmysql'] = 0;
409\$plugins['authpgsql'] = 0;
410
411EOT;
412    $ok = $ok && fileWrite(DOKU_LOCAL.'plugins.local.php', $output);
413
414    return $ok;
415}
416
417/**
418 * Write the given content to a file
419 *
420 * @author  Chris Smith <chris@jalakai.co.uk>
421 */
422function fileWrite($filename, $data) {
423    global $error;
424    global $lang;
425
426    if (($fp = @fopen($filename, 'wb')) === false) {
427        $filename = str_replace($_SERVER['DOCUMENT_ROOT'],'{DOCUMENT_ROOT}/', $filename);
428        $error[]  = sprintf($lang['i_writeerr'],$filename);
429        return false;
430    }
431
432    if (!empty($data)) { fwrite($fp, $data);  }
433    fclose($fp);
434    return true;
435}
436
437
438/**
439 * check installation dependent local config files and tests for a known
440 * unmodified main config file
441 *
442 * @author      Chris Smith <chris@jalakai.co.uk>
443 */
444function check_configs(){
445    global $error;
446    global $lang;
447    global $dokuwiki_hash;
448
449    $ok = true;
450
451    $config_files = array(
452        'local' => DOKU_LOCAL.'local.php',
453        'users' => DOKU_LOCAL.'users.auth.php',
454        'auth'  => DOKU_LOCAL.'acl.auth.php'
455    );
456
457    // main dokuwiki config file (conf/dokuwiki.php) must not have been modified
458    $installation_hash = md5(preg_replace("/(\015\012)|(\015)/","\012",
459                             @file_get_contents(DOKU_CONF.'dokuwiki.php')));
460    if (!in_array($installation_hash, $dokuwiki_hash)) {
461        $error[] = sprintf($lang['i_badhash'],$installation_hash);
462        $ok = false;
463    }
464
465    // configs shouldn't exist
466    foreach ($config_files as $file) {
467        if (@file_exists($file) && filesize($file)) {
468            $file    = str_replace($_SERVER['DOCUMENT_ROOT'],'{DOCUMENT_ROOT}/', $file);
469            $error[] = sprintf($lang['i_confexists'],$file);
470            $ok      = false;
471        }
472    }
473    return $ok;
474}
475
476
477/**
478 * Check other installation dir/file permission requirements
479 *
480 * @author      Chris Smith <chris@jalakai.co.uk>
481 */
482function check_permissions(){
483    global $error;
484    global $lang;
485
486    $dirs = array(
487        'conf'        => DOKU_LOCAL,
488        'data'        => DOKU_INC.'data',
489        'pages'       => DOKU_INC.'data/pages',
490        'attic'       => DOKU_INC.'data/attic',
491        'media'       => DOKU_INC.'data/media',
492        'media_attic' => DOKU_INC.'data/media_attic',
493        'media_meta'  => DOKU_INC.'data/media_meta',
494        'meta'        => DOKU_INC.'data/meta',
495        'cache'       => DOKU_INC.'data/cache',
496        'locks'       => DOKU_INC.'data/locks',
497        'index'       => DOKU_INC.'data/index',
498        'tmp'         => DOKU_INC.'data/tmp'
499    );
500
501    $ok = true;
502    foreach($dirs as $dir){
503        if(!@file_exists("$dir/.") || !@is_writable($dir)){
504            $dir     = str_replace($_SERVER['DOCUMENT_ROOT'],'{DOCUMENT_ROOT}', $dir);
505            $error[] = sprintf($lang['i_permfail'],$dir);
506            $ok      = false;
507        }
508    }
509    return $ok;
510}
511
512/**
513 * Check the availability of functions used in DokuWiki and the PHP version
514 *
515 * @author Andreas Gohr <andi@splitbrain.org>
516 */
517function check_functions(){
518    global $error;
519    global $lang;
520    $ok = true;
521
522    if(version_compare(phpversion(),'5.2.0','<')){
523        $error[] = sprintf($lang['i_phpver'],phpversion(),'5.2.0');
524        $ok = false;
525    }
526
527    $funcs = explode(' ','addslashes call_user_func chmod copy fgets '.
528                         'file file_exists fseek flush filesize ftell fopen '.
529                         'glob header ignore_user_abort ini_get mail mkdir '.
530                         'ob_start opendir parse_ini_file readfile realpath '.
531                         'rename rmdir serialize session_start unlink usleep '.
532                         'preg_replace file_get_contents htmlspecialchars_decode '.
533                         'spl_autoload_register stream_select fsockopen pack');
534
535    if (!function_exists('mb_substr')) {
536        $funcs[] = 'utf8_encode';
537        $funcs[] = 'utf8_decode';
538    }
539
540    foreach($funcs as $func){
541        if(!function_exists($func)){
542            $error[] = sprintf($lang['i_funcna'],$func);
543            $ok = false;
544        }
545    }
546    return $ok;
547}
548
549/**
550 * Print language selection
551 *
552 * @author Andreas Gohr <andi@splitbrain.org>
553 */
554function langsel(){
555    global $lang;
556    global $LC;
557
558    $dir = DOKU_INC.'inc/lang';
559    $dh  = opendir($dir);
560    if(!$dh) return;
561
562    $langs = array();
563    while (($file = readdir($dh)) !== false) {
564        if(preg_match('/^[\._]/',$file)) continue;
565        if(is_dir($dir.'/'.$file) && @file_exists($dir.'/'.$file.'/lang.php')){
566            $langs[] = $file;
567        }
568    }
569    closedir($dh);
570    sort($langs);
571
572    echo '<form action="">';
573    echo $lang['i_chooselang'];
574    echo ': <select name="l" onchange="submit()">';
575    foreach($langs as $l){
576        $sel = ($l == $LC) ? 'selected="selected"' : '';
577        echo '<option value="'.$l.'" '.$sel.'>'.$l.'</option>';
578    }
579    echo '</select> ';
580    echo '<input type="submit" value="'.$lang['btn_update'].'" />';
581    echo '</form>';
582}
583
584/**
585 * Print global error array
586 *
587 * @author Andreas Gohr <andi@splitbrain.org>
588 */
589function print_errors(){
590    global $error;
591    if(!empty($error)) {
592        echo '<ul>';
593        foreach ($error as $err){
594            echo "<li>$err</li>";
595        }
596        echo '</ul>';
597    }
598}
599
600/**
601 * remove magic quotes recursivly
602 *
603 * @author Andreas Gohr <andi@splitbrain.org>
604 */
605function remove_magic_quotes(&$array) {
606    foreach (array_keys($array) as $key) {
607        if (is_array($array[$key])) {
608            remove_magic_quotes($array[$key]);
609        }else {
610            $array[$key] = stripslashes($array[$key]);
611        }
612    }
613}
614
615