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