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