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