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