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