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