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