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