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