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