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