1<?php 2/** 3 * Initialize some defaults needed for DokuWiki 4 */ 5 6 // start timing Dokuwiki execution 7 function delta_time($start=0) { 8 list($usec, $sec) = explode(" ", microtime()); 9 return ((float)$usec+(float)$sec)-((float)$start); 10 } 11 define('DOKU_START_TIME', delta_time()); 12 13 // if available load a preload config file 14 @include(fullpath(dirname(__FILE__)).'/preload.php'); 15 16 // define the include path 17 if(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../').'/'); 18 19 // define config path (packagers may want to change this to /etc/dokuwiki/) 20 if(!defined('DOKU_CONF')) define('DOKU_CONF',DOKU_INC.'conf/'); 21 22 // check for error reporting override or set error reporting to sane values 23 if (!defined('DOKU_E_LEVEL') && @file_exists(DOKU_CONF.'report_e_all')) { 24 define('DOKU_E_LEVEL', E_ALL); 25 } 26 if (!defined('DOKU_E_LEVEL')) { 27 if(defined('E_DEPRECATED')){ // since php 5.3 28 error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED); 29 }else{ 30 error_reporting(E_ALL ^ E_NOTICE); 31 } 32 } else { 33 error_reporting(DOKU_E_LEVEL); 34 } 35 36 // init memory caches 37 global $cache_revinfo; $cache_revinfo = array(); 38 global $cache_wikifn; $cache_wikifn = array(); 39 global $cache_cleanid; $cache_cleanid = array(); 40 global $cache_authname; $cache_authname = array(); 41 global $cache_metadata; $cache_metadata = array(); 42 43 //prepare config array() 44 global $conf; 45 if (!defined('DOKU_UNITTEST')) { 46 $conf = array(); 47 48 // load the config file(s) 49 require_once(DOKU_CONF.'dokuwiki.php'); 50 if(@file_exists(DOKU_CONF.'local.php')){ 51 require_once(DOKU_CONF.'local.php'); 52 } 53 } 54 55 //prepare language array 56 global $lang; 57 $lang = array(); 58 59 //load the language files 60 require_once(DOKU_INC.'inc/lang/en/lang.php'); 61 if ( $conf['lang'] && $conf['lang'] != 'en' ) { 62 require_once(DOKU_INC.'inc/lang/'.$conf['lang'].'/lang.php'); 63 } 64 65 // define baseURL 66 if(!defined('DOKU_REL')) define('DOKU_REL',getBaseURL(false)); 67 if(!defined('DOKU_URL')) define('DOKU_URL',getBaseURL(true)); 68 if(!defined('DOKU_BASE')){ 69 if($conf['canonical']){ 70 define('DOKU_BASE',DOKU_URL); 71 }else{ 72 define('DOKU_BASE',DOKU_REL); 73 } 74 } 75 76 // define whitespace 77 if(!defined('DOKU_LF')) define ('DOKU_LF',"\n"); 78 if(!defined('DOKU_TAB')) define ('DOKU_TAB',"\t"); 79 80 // define cookie and session id 81 if (!defined('DOKU_COOKIE')) define('DOKU_COOKIE', 'DW'.md5(DOKU_REL)); 82 83 // define Plugin dir 84 if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 85 86 // define main script 87 if(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php'); 88 89 // define Template baseURL 90 if(!defined('DOKU_TPL')) define('DOKU_TPL', 91 DOKU_BASE.'lib/tpl/'.$conf['template'].'/'); 92 93 // define real Template directory 94 if(!defined('DOKU_TPLINC')) define('DOKU_TPLINC', 95 DOKU_INC.'lib/tpl/'.$conf['template'].'/'); 96 97 // make session rewrites XHTML compliant 98 @ini_set('arg_separator.output', '&'); 99 100 // make sure global zlib does not interfere FS#1132 101 @ini_set('zlib.output_compression', 'off'); 102 103 // increase PCRE backtrack limit 104 @ini_set('pcre.backtrack_limit', '20971520'); 105 106 // enable gzip compression 107 if ($conf['gzip_output'] && 108 !defined('DOKU_DISABLE_GZIP_OUTPUT') && 109 function_exists('ob_gzhandler') && 110 preg_match('/gzip|deflate/', $_SERVER['HTTP_ACCEPT_ENCODING'])) { 111 ob_start('ob_gzhandler'); 112 } 113 114 // init session 115 if (!headers_sent() && !defined('NOSESSION')){ 116 session_name("DokuWiki"); 117 if (version_compare(PHP_VERSION, '5.2.0', '>')) { 118 session_set_cookie_params(0,DOKU_REL,'',($conf['securecookie'] && is_ssl()),true); 119 }else{ 120 session_set_cookie_params(0,DOKU_REL,'',($conf['securecookie'] && is_ssl())); 121 } 122 session_start(); 123 } 124 125 // kill magic quotes 126 if (get_magic_quotes_gpc() && !defined('MAGIC_QUOTES_STRIPPED')) { 127 if (!empty($_GET)) remove_magic_quotes($_GET); 128 if (!empty($_POST)) remove_magic_quotes($_POST); 129 if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE); 130 if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST); 131 @ini_set('magic_quotes_gpc', 0); 132 define('MAGIC_QUOTES_STRIPPED',1); 133 } 134 @set_magic_quotes_runtime(0); 135 @ini_set('magic_quotes_sybase',0); 136 137 // don't let cookies ever interfere with request vars 138 $_REQUEST = array_merge($_GET,$_POST); 139 140 // disable gzip if not available 141 if($conf['compression'] == 'bz2' && !function_exists('bzopen')){ 142 $conf['compression'] = 'gz'; 143 } 144 if($conf['compression'] == 'gz' && !function_exists('gzopen')){ 145 $conf['compression'] = 0; 146 } 147 148 // fix dateformat for upgraders 149 if(strpos($conf['dformat'],'%') === false){ 150 $conf['dformat'] = '%Y/%m/%d %H:%M'; 151 } 152 153 // precalculate file creation modes 154 init_creationmodes(); 155 156 // make real paths and check them 157 init_paths(); 158 init_files(); 159 160 // automatic upgrade to script versions of certain files 161 scriptify(DOKU_CONF.'users.auth'); 162 scriptify(DOKU_CONF.'acl.auth'); 163 164 165/** 166 * Checks paths from config file 167 */ 168function init_paths(){ 169 global $conf; 170 171 $paths = array('datadir' => 'pages', 172 'olddir' => 'attic', 173 'mediadir' => 'media', 174 'metadir' => 'meta', 175 'cachedir' => 'cache', 176 'indexdir' => 'index', 177 'lockdir' => 'locks', 178 'tmpdir' => 'tmp'); 179 180 foreach($paths as $c => $p){ 181 if(empty($conf[$c])) $conf[$c] = $conf['savedir'].'/'.$p; 182 $conf[$c] = init_path($conf[$c]); 183 if(empty($conf[$c])) nice_die("The $c ('$p') does not exist, isn't accessible or writable. 184 You should check your config and permission settings. 185 Or maybe you want to <a href=\"install.php\">run the 186 installer</a>?"); 187 } 188 189 // path to old changelog only needed for upgrading 190 $conf['changelog_old'] = init_path((isset($conf['changelog']))?($conf['changelog']):($conf['savedir'].'/changes.log')); 191 if ($conf['changelog_old']=='') { unset($conf['changelog_old']); } 192 // hardcoded changelog because it is now a cache that lives in meta 193 $conf['changelog'] = $conf['metadir'].'/_dokuwiki.changes'; 194} 195 196/** 197 * Checks the existance of certain files and creates them if missing. 198 */ 199function init_files(){ 200 global $conf; 201 202 $files = array( $conf['indexdir'].'/page.idx'); 203 204 foreach($files as $file){ 205 if(!@file_exists($file)){ 206 $fh = @fopen($file,'a'); 207 if($fh){ 208 fclose($fh); 209 if($conf['fperm']) chmod($file, $conf['fperm']); 210 }else{ 211 nice_die("$file is not writable. Check your permissions settings!"); 212 } 213 } 214 } 215} 216 217/** 218 * Returns absolute path 219 * 220 * This tries the given path first, then checks in DOKU_INC. 221 * Check for accessability on directories as well. 222 * 223 * @author Andreas Gohr <andi@splitbrain.org> 224 */ 225function init_path($path){ 226 // check existance 227 $p = fullpath($path); 228 if(!@file_exists($p)){ 229 $p = fullpath(DOKU_INC.$path); 230 if(!@file_exists($p)){ 231 return ''; 232 } 233 } 234 235 // check writability 236 if(!@is_writable($p)){ 237 return ''; 238 } 239 240 // check accessability (execute bit) for directories 241 if(@is_dir($p) && !@file_exists("$p/.")){ 242 return ''; 243 } 244 245 return $p; 246} 247 248/** 249 * Sets the internal config values fperm and dperm which, when set, 250 * will be used to change the permission of a newly created dir or 251 * file with chmod. Considers the influence of the system's umask 252 * setting the values only if needed. 253 */ 254function init_creationmodes(){ 255 global $conf; 256 257 // Legacy support for old umask/dmask scheme 258 unset($conf['dmask']); 259 unset($conf['fmask']); 260 unset($conf['umask']); 261 unset($conf['fperm']); 262 unset($conf['dperm']); 263 264 // get system umask, fallback to 0 if none available 265 $umask = @umask(); 266 if(!$umask) $umask = 0000; 267 268 // check what is set automatically by the system on file creation 269 // and set the fperm param if it's not what we want 270 $auto_fmode = 0666 & ~$umask; 271 if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode']; 272 273 // check what is set automatically by the system on file creation 274 // and set the dperm param if it's not what we want 275 $auto_dmode = $conf['dmode'] & ~$umask; 276 if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode']; 277} 278 279/** 280 * remove magic quotes recursivly 281 * 282 * @author Andreas Gohr <andi@splitbrain.org> 283 */ 284function remove_magic_quotes(&$array) { 285 foreach (array_keys($array) as $key) { 286 // handle magic quotes in keynames (breaks order) 287 $sk = stripslashes($key); 288 if($sk != $key){ 289 $array[$sk] = $array[$key]; 290 unset($array[$key]); 291 $key = $sk; 292 } 293 294 // do recursion if needed 295 if (is_array($array[$key])) { 296 remove_magic_quotes($array[$key]); 297 }else { 298 $array[$key] = stripslashes($array[$key]); 299 } 300 } 301} 302 303/** 304 * Returns the full absolute URL to the directory where 305 * DokuWiki is installed in (includes a trailing slash) 306 * 307 * @author Andreas Gohr <andi@splitbrain.org> 308 */ 309function getBaseURL($abs=null){ 310 global $conf; 311 //if canonical url enabled always return absolute 312 if(is_null($abs)) $abs = $conf['canonical']; 313 314 if($conf['basedir']){ 315 $dir = $conf['basedir']; 316 }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){ 317 $dir = dirname($_SERVER['SCRIPT_NAME']); 318 }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){ 319 $dir = dirname($_SERVER['PHP_SELF']); 320 }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){ 321 $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','', 322 $_SERVER['SCRIPT_FILENAME']); 323 $dir = dirname('/'.$dir); 324 }else{ 325 $dir = '.'; //probably wrong 326 } 327 328 $dir = str_replace('\\','/',$dir); // bugfix for weird WIN behaviour 329 $dir = preg_replace('#//+#','/',"/$dir/"); // ensure leading and trailing slashes 330 331 //handle script in lib/exe dir 332 $dir = preg_replace('!lib/exe/$!','',$dir); 333 334 //handle script in lib/plugins dir 335 $dir = preg_replace('!lib/plugins/.*$!','',$dir); 336 337 //finish here for relative URLs 338 if(!$abs) return $dir; 339 340 //use config option if available, trim any slash from end of baseurl to avoid multiple consecutive slashes in the path 341 if($conf['baseurl']) return rtrim($conf['baseurl'],'/').$dir; 342 343 //split hostheader into host and port 344 list($host,$port) = explode(':',$_SERVER['HTTP_HOST']); 345 if(!$port) $port = $_SERVER['SERVER_PORT']; 346 if(!$port) $port = 80; 347 348 if(!is_ssl()){ 349 $proto = 'http://'; 350 if ($port == '80') { 351 $port=''; 352 } 353 }else{ 354 $proto = 'https://'; 355 if ($port == '443') { 356 $port=''; 357 } 358 } 359 360 if($port) $port = ':'.$port; 361 362 return $proto.$host.$port.$dir; 363} 364 365/** 366 * Check if accessed via HTTPS 367 * 368 * Apache leaves ,$_SERVER['HTTPS'] empty when not available, IIS sets it to 'off'. 369 * 'false' and 'disabled' are just guessing 370 * 371 * @returns bool true when SSL is active 372 */ 373function is_ssl(){ 374 if (preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){ 375 return false; 376 }else{ 377 return true; 378 } 379} 380 381/** 382 * Append a PHP extension to a given file and adds an exit call 383 * 384 * This is used to migrate some old configfiles. An added PHP extension 385 * ensures the contents are not shown to webusers even if .htaccess files 386 * do not work 387 * 388 * @author Jan Decaluwe <jan@jandecaluwe.com> 389 */ 390function scriptify($file) { 391 // checks 392 if (!is_readable($file)) { 393 return; 394 } 395 $fn = $file.'.php'; 396 if (@file_exists($fn)) { 397 return; 398 } 399 $fh = fopen($fn, 'w'); 400 if (!$fh) { 401 nice_die($fn.' is not writable. Check your permission settings!'); 402 } 403 // write php exit hack first 404 fwrite($fh, "# $fn\n"); 405 fwrite($fh, '# <?php exit()?>'."\n"); 406 fwrite($fh, "# Don't modify the lines above\n"); 407 fwrite($fh, "#\n"); 408 // copy existing lines 409 $lines = file($file); 410 foreach ($lines as $line){ 411 fwrite($fh, $line); 412 } 413 fclose($fh); 414 //try to rename the old file 415 io_rename($file,"$file.old"); 416} 417 418/** 419 * print a nice message even if no styles are loaded yet. 420 */ 421function nice_die($msg){ 422 echo<<<EOT 423 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 424 "http://www.w3.org/TR/html4/loose.dtd"> 425 <html> 426 <head><title>DokuWiki Setup Error</title></head> 427 <body style="font-family: Arial, sans-serif"> 428 <div style="width:60%; margin: auto; background-color: #fcc; 429 border: 1px solid #faa; padding: 0.5em 1em;"> 430 <h1 style="font-size: 120%">DokuWiki Setup Error</h1> 431 <p>$msg</p> 432 </div> 433 </body> 434 </html> 435EOT; 436 exit; 437} 438 439 440/** 441 * A realpath() replacement 442 * 443 * This function behaves similar to PHP's realpath() but does not resolve 444 * symlinks or accesses upper directories 445 * 446 * @author <richpageau at yahoo dot co dot uk> 447 * @link http://de3.php.net/manual/en/function.realpath.php#75992 448 */ 449function fullpath($path){ 450 $iswin = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'); 451 $isunc = 0===strpos($path,'\\\\'); 452 if($iswin) $path = str_replace('\\','/',$path); // windows compatibility 453 454 // check if path begins with "/" or "c:" ie. is absolute 455 // if it isnt concat with script path 456 if (!$isunc && 457 ((!$iswin && $path{0} !== '/') || 458 ($iswin && $path{1} !== ':'))) { 459 $base=dirname($_SERVER['SCRIPT_FILENAME']); 460 $path=$base."/".$path; 461 } 462 463 // canonicalize 464 $path=explode('/', $path); 465 $newpath=array(); 466 foreach($path as $p) { 467 if ($p === '' || $p === '.') continue; 468 if ($p==='..') { 469 array_pop($newpath); 470 continue; 471 } 472 array_push($newpath, $p); 473 } 474 $finalpath = implode('/', $newpath); 475 if($isunc) $finalpath = '//'.$finalpath; 476 if(!$iswin) $finalpath = '/'.$finalpath; 477 478 // check then return valid path or filename 479 if (@file_exists($finalpath)) { 480 return ($finalpath); 481 } 482 else return false; 483} 484 485 486 487//Setup VIM: ex: et ts=2 enc=utf-8 : 488