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