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