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