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