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