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