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