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