some refactoring
[dokuwiki.git] / install.php
blobf54c252073a181b14c274486ea06bf92528edf7a
1 <?php
2 /**
3 * Dokuwiki installation assistance
5 * @author Chris Smith <chris@jalakai.co.uk>
6 */
8 if(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/');
9 if(!defined('DOKU_CONF')) define('DOKU_CONF',DOKU_INC.'conf/');
10 if(!defined('DOKU_LOCAL')) define('DOKU_LOCAL',DOKU_INC.'conf/');
12 require_once(DOKU_INC.'inc/PassHash.class.php');
14 // check for error reporting override or set error reporting to sane values
15 if (!defined('DOKU_E_LEVEL')) { error_reporting(E_ALL ^ E_NOTICE); }
16 else { error_reporting(DOKU_E_LEVEL); }
18 // kill magic quotes
19 if (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);
27 if (function_exists('set_magic_quotes_runtime')) @set_magic_quotes_runtime(0);
28 @ini_set('magic_quotes_sybase',0);
30 // language strings
31 require_once(DOKU_INC.'inc/lang/en/lang.php');
32 if(isset($_REQUEST['l']) && !is_array($_REQUEST['l'])) {
33 $LC = preg_replace('/[^a-z\-]+/','',$_REQUEST['l']);
35 if(empty($LC)) $LC = 'en';
36 if($LC && $LC != 'en' ) {
37 require_once(DOKU_INC.'inc/lang/'.$LC.'/lang.php');
40 // initialise variables ...
41 $error = array();
43 $dokuwiki_hash = array(
44 '2005-09-22' => 'e33223e957b0b0a130d0520db08f8fb7',
45 '2006-03-05' => '51295727f79ab9af309a2fd9e0b61acc',
46 '2006-03-09' => '51295727f79ab9af309a2fd9e0b61acc',
47 '2006-11-06' => 'b3a8af76845977c2000d85d6990dd72b',
48 '2007-05-24' => 'd80f2740c84c4a6a791fd3c7a353536f',
49 '2007-06-26' => 'b3ca19c7a654823144119980be73cd77',
50 '2008-05-04' => '1e5c42eac3219d9e21927c39e3240aad',
51 '2009-02-14' => 'ec8c04210732a14fdfce0f7f6eead865',
52 '2009-12-25' => '993c4b2b385643efe5abf8e7010e11f4',
53 '2010-11-07' => '7921d48195f4db21b8ead6d9bea801b8',
54 '2011-05-25' => '4241865472edb6fa14a1227721008072',
55 '2011-11-10' => 'b46ff19a7587966ac4df61cbab1b8b31',
56 '2012-01-25' => '72c083c73608fc43c586901fd5dabb74',
57 '2012-09-10' => 'eb0b3fc90056fbc12bac6f49f7764df3'
61 // begin output
62 header('Content-Type: text/html; charset=utf-8');
64 <!DOCTYPE html>
65 <html lang="<?php echo $LC?>" dir="<?php echo $lang['direction']?>">
66 <head>
67 <meta charset="utf-8" />
68 <title><?php echo $lang['i_installer']?></title>
69 <style type="text/css">
70 body { width: 90%; margin: 0 auto; font: 84% Verdana, Helvetica, Arial, sans-serif; }
71 img { border: none }
72 br.cl { clear:both; }
73 code { font-size: 110%; color: #800000; }
74 fieldset { border: none }
75 label { display: block; margin-top: 0.5em; }
76 select.text, input.text { width: 30em; margin: 0 0.5em; }
77 a {text-decoration: none}
78 </style>
79 <script type="text/javascript">
80 function acltoggle(){
81 var cb = document.getElementById('acl');
82 var fs = document.getElementById('acldep');
83 if(!cb || !fs) return;
84 if(cb.checked){
85 fs.style.display = '';
86 }else{
87 fs.style.display = 'none';
90 window.onload = function(){
91 acltoggle();
92 var cb = document.getElementById('acl');
93 if(cb) cb.onchange = acltoggle;
95 </script>
96 </head>
97 <body style="">
98 <h1 style="float:left">
99 <img src="lib/exe/fetch.php?media=wiki:dokuwiki-128.png&amp;w=64"
100 style="vertical-align: middle;" alt="" />
101 <?php echo $lang['i_installer']?>
102 </h1>
103 <div style="float:right; margin: 1em;">
104 <?php langsel()?>
105 </div>
106 <br class="cl" />
108 <div style="float: right; width: 34%;">
109 <?php
110 if(@file_exists(DOKU_INC.'inc/lang/'.$LC.'/install.html')){
111 include(DOKU_INC.'inc/lang/'.$LC.'/install.html');
112 }else{
113 print "<div lang=\"en\" dir=\"ltr\">\n";
114 include(DOKU_INC.'inc/lang/en/install.html');
115 print "</div>\n";
118 <a style="background: transparent url(data/security.png) left top no-repeat;
119 display: block; width:380px; height:73px; border:none; clear:both;"
120 target="_blank"
121 href="http://www.dokuwiki.org/security#web_access_security"></a>
122 </div>
124 <div style="float: left; width: 58%;">
125 <?php
126 if(! (check_functions() && check_permissions()) ){
127 echo '<p>'.$lang['i_problems'].'</p>';
128 print_errors();
129 print_retry();
130 }elseif(!check_configs()){
131 echo '<p>'.$lang['i_modified'].'</p>';
132 print_errors();
133 }elseif(check_data($_REQUEST['d'])){
134 // check_data has sanitized all input parameters
135 if(!store_data($_REQUEST['d'])){
136 echo '<p>'.$lang['i_failure'].'</p>';
137 print_errors();
138 }else{
139 echo '<p>'.$lang['i_success'].'</p>';
141 }else{
142 print_errors();
143 print_form($_REQUEST['d']);
146 </div>
149 <div style="clear: both">
150 <a href="http://dokuwiki.org/"><img src="lib/tpl/default/images/button-dw.png" alt="driven by DokuWiki" /></a>
151 <a href="http://www.php.net"><img src="lib/tpl/default/images/button-php.gif" alt="powered by PHP" /></a>
152 </div>
153 </body>
154 </html>
155 <?php
158 * Print the input form
160 function print_form($d){
161 global $lang;
162 global $LC;
164 include(DOKU_CONF.'license.php');
166 if(!is_array($d)) $d = array();
167 $d = array_map('htmlspecialchars',$d);
169 if(!isset($d['acl'])) $d['acl']=1;
170 if(!isset($d['pop'])) $d['pop']=1;
173 <form action="" method="post">
174 <input type="hidden" name="l" value="<?php echo $LC ?>" />
175 <fieldset>
176 <label for="title"><?php echo $lang['i_wikiname']?>
177 <input type="text" name="d[title]" id="title" value="<?php echo $d['title'] ?>" style="width: 20em;" />
178 </label>
180 <fieldset style="margin-top: 1em;">
181 <label for="acl">
182 <input type="checkbox" name="d[acl]" id="acl" <?php echo(($d['acl'] ? ' checked="checked"' : ''));?> />
183 <?php echo $lang['i_enableacl']?></label>
185 <fieldset id="acldep">
186 <label for="superuser"><?php echo $lang['i_superuser']?></label>
187 <input class="text" type="text" name="d[superuser]" id="superuser" value="<?php echo $d['superuser'] ?>" />
189 <label for="fullname"><?php echo $lang['fullname']?></label>
190 <input class="text" type="text" name="d[fullname]" id="fullname" value="<?php echo $d['fullname'] ?>" />
192 <label for="email"><?php echo $lang['email']?></label>
193 <input class="text" type="text" name="d[email]" id="email" value="<?php echo $d['email'] ?>" />
195 <label for="password"><?php echo $lang['pass']?></label>
196 <input class="text" type="password" name="d[password]" id="password" />
198 <label for="confirm"><?php echo $lang['passchk']?></label>
199 <input class="text" type="password" name="d[confirm]" id="confirm" />
201 <label for="policy"><?php echo $lang['i_policy']?></label>
202 <select class="text" name="d[policy]" id="policy">
203 <option value="0" <?php echo ($d['policy'] == 0)?'selected="selected"':'' ?>><?php echo $lang['i_pol0']?></option>
204 <option value="1" <?php echo ($d['policy'] == 1)?'selected="selected"':'' ?>><?php echo $lang['i_pol1']?></option>
205 <option value="2" <?php echo ($d['policy'] == 2)?'selected="selected"':'' ?>><?php echo $lang['i_pol2']?></option>
206 </select>
208 </fieldset>
209 </fieldset>
211 <fieldset>
212 <p><?php echo $lang['i_license']?></p>
213 <?php
214 array_push($license,array('name' => $lang['i_license_none'], 'url'=>''));
215 if(empty($d['license'])) $d['license'] = 'cc-by-sa';
216 foreach($license as $key => $lic){
217 echo '<label for="lic_'.$key.'">';
218 echo '<input type="radio" name="d[license]" value="'.htmlspecialchars($key).'" id="lic_'.$key.'"'.
219 (($d['license'] === $key)?' checked="checked"':'').'>';
220 echo htmlspecialchars($lic['name']);
221 if($lic['url']) echo ' <a href="'.$lic['url'].'" target="_blank"><sup>[?]</sup></a>';
222 echo '</label>';
225 </fieldset>
227 <fieldset>
228 <p><?php echo $lang['i_pop_field']?></p>
229 <label for="pop">
230 <input type="checkbox" name="d[pop]" id="pop" <?php echo(($d['pop'] ? ' checked="checked"' : ''));?> />
231 <?php echo $lang['i_pop_label']?> <a href="http://www.dokuwiki.org/popularity" target="_blank"><sup>[?]</sup></a>
232 </label>
233 </fieldset>
235 </fieldset>
236 <fieldset id="process">
237 <input class="button" type="submit" name="submit" value="<?php echo $lang['btn_save']?>" />
238 </fieldset>
239 </form>
240 <?php
243 function print_retry() {
244 global $lang;
245 global $LC;
247 <form action="" method="get">
248 <fieldset>
249 <input type="hidden" name="l" value="<?php echo $LC ?>" />
250 <input class="button" type="submit" value="<?php echo $lang['i_retry'];?>" />
251 </fieldset>
252 </form>
253 <?php
257 * Check validity of data
259 * @author Andreas Gohr
261 function check_data(&$d){
262 static $form_default = array(
263 'title' => '',
264 'acl' => '1',
265 'superuser' => '',
266 'fullname' => '',
267 'email' => '',
268 'password' => '',
269 'confirm' => '',
270 'policy' => '0',
271 'license' => 'cc-by-sa'
273 global $lang;
274 global $error;
276 if(!is_array($d)) $d = array();
277 foreach($d as $k => $v) {
278 if(is_array($v))
279 unset($d[$k]);
280 else
281 $d[$k] = (string)$v;
284 //autolowercase the username
285 $d['superuser'] = isset($d['superuser']) ? strtolower($d['superuser']) : "";
287 $ok = false;
289 if(isset($_REQUEST['submit'])) {
290 $ok = true;
292 // check input
293 if(empty($d['title'])){
294 $error[] = sprintf($lang['i_badval'],$lang['i_wikiname']);
295 $ok = false;
297 if(isset($d['acl'])){
298 if(!preg_match('/^[a-z0-9_]+$/',$d['superuser'])){
299 $error[] = sprintf($lang['i_badval'],$lang['i_superuser']);
300 $ok = false;
302 if(empty($d['password'])){
303 $error[] = sprintf($lang['i_badval'],$lang['pass']);
304 $ok = false;
306 elseif(!isset($d['confirm']) || $d['confirm'] != $d['password']){
307 $error[] = sprintf($lang['i_badval'],$lang['passchk']);
308 $ok = false;
310 if(empty($d['fullname']) || strstr($d['fullname'],':')){
311 $error[] = sprintf($lang['i_badval'],$lang['fullname']);
312 $ok = false;
314 if(empty($d['email']) || strstr($d['email'],':') || !strstr($d['email'],'@')){
315 $error[] = sprintf($lang['i_badval'],$lang['email']);
316 $ok = false;
320 $d = array_merge($form_default, $d);
321 return $ok;
325 * Writes the data to the config files
327 * @author Chris Smith <chris@jalakai.co.uk>
329 function store_data($d){
330 global $LC;
331 $ok = true;
332 $d['policy'] = (int) $d['policy'];
334 // create local.php
335 $now = gmdate('r');
336 $output = <<<EOT
337 <?php
339 * Dokuwiki's Main Configuration File - Local Settings
340 * Auto-generated by install script
341 * Date: $now
344 EOT;
345 $output .= '$conf[\'title\'] = \''.addslashes($d['title'])."';\n";
346 $output .= '$conf[\'lang\'] = \''.addslashes($LC)."';\n";
347 $output .= '$conf[\'license\'] = \''.addslashes($d['license'])."';\n";
348 if($d['acl']){
349 $output .= '$conf[\'useacl\'] = 1'.";\n";
350 $output .= "\$conf['superuser'] = '@admin';\n";
352 $ok = $ok && fileWrite(DOKU_LOCAL.'local.php',$output);
354 if ($d['acl']) {
355 // hash the password
356 $phash = new PassHash();
357 $pass = $phash->hash_smd5($d['password']);
359 // create users.auth.php
360 // --- user:SMD5password:Real Name:email:groups,comma,seperated
361 $output = join(":",array($d['superuser'], $pass, $d['fullname'], $d['email'], 'admin,user'));
362 $output = @file_get_contents(DOKU_CONF.'users.auth.php.dist')."\n$output\n";
363 $ok = $ok && fileWrite(DOKU_LOCAL.'users.auth.php', $output);
365 // create acl.auth.php
366 $output = <<<EOT
367 # acl.auth.php
368 # <?php exit()?>
369 # Don't modify the lines above
371 # Access Control Lists
373 # Auto-generated by install script
374 # Date: $now
376 EOT;
377 if($d['policy'] == 2){
378 $output .= "* @ALL 0\n";
379 $output .= "* @user 8\n";
380 }elseif($d['policy'] == 1){
381 $output .= "* @ALL 1\n";
382 $output .= "* @user 8\n";
383 }else{
384 $output .= "* @ALL 8\n";
386 $ok = $ok && fileWrite(DOKU_LOCAL.'acl.auth.php', $output);
389 // enable popularity submission
390 if($d['pop']){
391 @touch(DOKU_INC.'data/cache/autosubmit.txt');
394 return $ok;
398 * Write the given content to a file
400 * @author Chris Smith <chris@jalakai.co.uk>
402 function fileWrite($filename, $data) {
403 global $error;
404 global $lang;
406 if (($fp = @fopen($filename, 'wb')) === false) {
407 $filename = str_replace($_SERVER['DOCUMENT_ROOT'],'{DOCUMENT_ROOT}/', $filename);
408 $error[] = sprintf($lang['i_writeerr'],$filename);
409 return false;
412 if (!empty($data)) { fwrite($fp, $data); }
413 fclose($fp);
414 return true;
419 * check installation dependent local config files and tests for a known
420 * unmodified main config file
422 * @author Chris Smith <chris@jalakai.co.uk>
424 function check_configs(){
425 global $error;
426 global $lang;
427 global $dokuwiki_hash;
429 $ok = true;
431 $config_files = array(
432 'local' => DOKU_LOCAL.'local.php',
433 'users' => DOKU_LOCAL.'users.auth.php',
434 'auth' => DOKU_LOCAL.'acl.auth.php'
437 // main dokuwiki config file (conf/dokuwiki.php) must not have been modified
438 $installation_hash = md5(preg_replace("/(\015\012)|(\015)/","\012",
439 @file_get_contents(DOKU_CONF.'dokuwiki.php')));
440 if (!in_array($installation_hash, $dokuwiki_hash)) {
441 $error[] = sprintf($lang['i_badhash'],$installation_hash);
442 $ok = false;
445 // configs shouldn't exist
446 foreach ($config_files as $file) {
447 if (@file_exists($file) && filesize($file)) {
448 $file = str_replace($_SERVER['DOCUMENT_ROOT'],'{DOCUMENT_ROOT}/', $file);
449 $error[] = sprintf($lang['i_confexists'],$file);
450 $ok = false;
453 return $ok;
458 * Check other installation dir/file permission requirements
460 * @author Chris Smith <chris@jalakai.co.uk>
462 function check_permissions(){
463 global $error;
464 global $lang;
466 $dirs = array(
467 'conf' => DOKU_LOCAL,
468 'data' => DOKU_INC.'data',
469 'pages' => DOKU_INC.'data/pages',
470 'attic' => DOKU_INC.'data/attic',
471 'media' => DOKU_INC.'data/media',
472 'media_attic' => DOKU_INC.'data/media_attic',
473 'media_meta' => DOKU_INC.'data/media_meta',
474 'meta' => DOKU_INC.'data/meta',
475 'cache' => DOKU_INC.'data/cache',
476 'locks' => DOKU_INC.'data/locks',
477 'index' => DOKU_INC.'data/index',
478 'tmp' => DOKU_INC.'data/tmp'
481 $ok = true;
482 foreach($dirs as $dir){
483 if(!@file_exists("$dir/.") || !@is_writable($dir)){
484 $dir = str_replace($_SERVER['DOCUMENT_ROOT'],'{DOCUMENT_ROOT}', $dir);
485 $error[] = sprintf($lang['i_permfail'],$dir);
486 $ok = false;
489 return $ok;
493 * Check the availability of functions used in DokuWiki and the PHP version
495 * @author Andreas Gohr <andi@splitbrain.org>
497 function check_functions(){
498 global $error;
499 global $lang;
500 $ok = true;
502 if(version_compare(phpversion(),'5.1.2','<')){
503 $error[] = sprintf($lang['i_phpver'],phpversion(),'5.1.2');
504 $ok = false;
507 $funcs = explode(' ','addslashes call_user_func chmod copy fgets '.
508 'file file_exists fseek flush filesize ftell fopen '.
509 'glob header ignore_user_abort ini_get mail mkdir '.
510 'ob_start opendir parse_ini_file readfile realpath '.
511 'rename rmdir serialize session_start unlink usleep '.
512 'preg_replace file_get_contents htmlspecialchars_decode '.
513 'spl_autoload_register stream_select fsockopen');
515 if (!function_exists('mb_substr')) {
516 $funcs[] = 'utf8_encode';
517 $funcs[] = 'utf8_decode';
520 foreach($funcs as $func){
521 if(!function_exists($func)){
522 $error[] = sprintf($lang['i_funcna'],$func);
523 $ok = false;
526 return $ok;
530 * Print language selection
532 * @author Andreas Gohr <andi@splitbrain.org>
534 function langsel(){
535 global $lang;
536 global $LC;
538 $dir = DOKU_INC.'inc/lang';
539 $dh = opendir($dir);
540 if(!$dh) return;
542 $langs = array();
543 while (($file = readdir($dh)) !== false) {
544 if(preg_match('/^[\._]/',$file)) continue;
545 if(is_dir($dir.'/'.$file) && @file_exists($dir.'/'.$file.'/lang.php')){
546 $langs[] = $file;
549 closedir($dh);
550 sort($langs);
552 echo '<form action="">';
553 echo $lang['i_chooselang'];
554 echo ': <select name="l" onchange="submit()">';
555 foreach($langs as $l){
556 $sel = ($l == $LC) ? 'selected="selected"' : '';
557 echo '<option value="'.$l.'" '.$sel.'>'.$l.'</option>';
559 echo '</select> ';
560 echo '<input type="submit" value="'.$lang['btn_update'].'" />';
561 echo '</form>';
565 * Print global error array
567 * @author Andreas Gohr <andi@splitbrain.org>
569 function print_errors(){
570 global $error;
571 if(!empty($error)) {
572 echo '<ul>';
573 foreach ($error as $err){
574 echo "<li>$err</li>";
576 echo '</ul>';
581 * remove magic quotes recursivly
583 * @author Andreas Gohr <andi@splitbrain.org>
585 function remove_magic_quotes(&$array) {
586 foreach (array_keys($array) as $key) {
587 if (is_array($array[$key])) {
588 remove_magic_quotes($array[$key]);
589 }else {
590 $array[$key] = stripslashes($array[$key]);