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 Check your config and permission settings!"); 130 } 131} 132 133/** 134 * Checks the existance of certain files and creates them if missing. 135 */ 136function init_files(){ 137 global $conf; 138 139 $files = array( $conf['cachedir'].'/word.idx', 140 $conf['cachedir'].'/page.idx', 141 $conf['cachedir'].'/index.idx'); 142 143 foreach($files as $file){ 144 if(!@file_exists($file)){ 145 $fh = @fopen($file,'a'); 146 if($fh){ 147 fclose($fh); 148 if($conf['fperm']) chmod($file, $conf['fperm']); 149 }else{ 150 nice_die("$file is not writable. Check your permissions settings!"); 151 } 152 } 153 } 154} 155 156/** 157 * Returns absolute path 158 * 159 * This tries the given path first, then checks in DOKU_INC. 160 * Check for accessability on directories as well. 161 * 162 * @author Andreas Gohr <andi@splitbrain.org> 163 */ 164function init_path($path){ 165 // check existance 166 $p = realpath($path); 167 if(!@file_exists($p)){ 168 $p = realpath(DOKU_INC.$path); 169 if(!@file_exists($p)){ 170 return ''; 171 } 172 } 173 174 // check writability 175 if(!@is_writable($p)){ 176 return ''; 177 } 178 179 // check accessability (execute bit) for directories 180 if(@is_dir($p) && !@file_exists("$p/.")){ 181 return ''; 182 } 183 184 return $p; 185} 186 187/** 188 * Sets the internal config values fperm and dperm which, when set, 189 * will be used to change the permission of a newly created dir or 190 * file with chmod. Considers the influence of the system's umask 191 * setting the values only if needed. 192 */ 193function init_creationmodes(){ 194 global $conf; 195 196 // Legacy support for old umask/dmask scheme 197 unset($conf['dmask']); 198 unset($conf['fmask']); 199 unset($conf['umask']); 200 unset($conf['fperm']); 201 unset($conf['dperm']); 202 203 // get system umask, fallback to 0 if none available 204 $umask = @umask(); 205 if(!$umask) $umask = 0000; 206 207 // check what is set automatically by the system on file creation 208 // and set the fperm param if it's not what we want 209 $auto_fmode = 0666 & ~$umask; 210 if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode']; 211 212 // check what is set automatically by the system on file creation 213 // and set the dperm param if it's not what we want 214 $auto_dmode = $conf['dmode'] & ~$umask; 215 if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode']; 216} 217 218/** 219 * remove magic quotes recursivly 220 * 221 * @author Andreas Gohr <andi@splitbrain.org> 222 */ 223function remove_magic_quotes(&$array) { 224 foreach (array_keys($array) as $key) { 225 if (is_array($array[$key])) { 226 remove_magic_quotes($array[$key]); 227 }else { 228 $array[$key] = stripslashes($array[$key]); 229 } 230 } 231} 232 233/** 234 * Returns the full absolute URL to the directory where 235 * DokuWiki is installed in (includes a trailing slash) 236 * 237 * @author Andreas Gohr <andi@splitbrain.org> 238 */ 239function getBaseURL($abs=false){ 240 global $conf; 241 //if canonical url enabled always return absolute 242 if($conf['canonical']) $abs = true; 243 244 if($conf['basedir']){ 245 $dir = $conf['basedir'].'/'; 246 }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){ 247 $dir = dirname($_SERVER['SCRIPT_NAME']).'/'; 248 }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){ 249 $dir = dirname($_SERVER['PHP_SELF']).'/'; 250 }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){ 251 $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','', 252 $_SERVER['SCRIPT_FILENAME']); 253 $dir = dirname('/'.$dir).'/'; 254 }else{ 255 $dir = './'; //probably wrong 256 } 257 258 $dir = str_replace('\\','/',$dir); #bugfix for weird WIN behaviour 259 $dir = preg_replace('#//+#','/',$dir); 260 261 //handle script in lib/exe dir 262 $dir = preg_replace('!lib/exe/$!','',$dir); 263 264 //handle script in lib/plugins dir 265 $dir = preg_replace('!lib/plugins/.*$!','',$dir); 266 267 //finish here for relative URLs 268 if(!$abs) return $dir; 269 270 //use config option if available 271 if($conf['baseurl']) return $conf['baseurl'].$dir; 272 273 //split hostheader into host and port 274 list($host,$port) = explode(':',$_SERVER['HTTP_HOST']); 275 if(!$port) $port = $_SERVER['SERVER_PORT']; 276 if(!$port) $port = 80; 277 278 // see if HTTPS is enabled - apache leaves this empty when not available, 279 // IIS sets it to 'off', 'false' and 'disabled' are just guessing 280 if (preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){ 281 $proto = 'http://'; 282 if ($port == '80') { 283 $port=''; 284 } 285 }else{ 286 $proto = 'https://'; 287 if ($port == '443') { 288 $port=''; 289 } 290 } 291 292 if($port) $port = ':'.$port; 293 294 return $proto.$host.$port.$dir; 295} 296 297/** 298 * Append a PHP extension to a given file and adds an exit call 299 * 300 * This is used to migrate some old configfiles. An added PHP extension 301 * ensures the contents are not shown to webusers even if .htaccess files 302 * do not work 303 * 304 * @author Jan Decaluwe <jan@jandecaluwe.com> 305 */ 306function scriptify($file) { 307 // checks 308 if (!is_readable($file)) { 309 return; 310 } 311 $fn = $file.'.php'; 312 if (@file_exists($fn)) { 313 return; 314 } 315 $fh = fopen($fn, 'w'); 316 if (!$fh) { 317 nice_die($fn.' is not writable. Check your permission settings!'); 318 } 319 // write php exit hack first 320 fwrite($fh, "# $fn\n"); 321 fwrite($fh, '# <?php exit()?>'."\n"); 322 fwrite($fh, "# Don't modify the lines above\n"); 323 fwrite($fh, "#\n"); 324 // copy existing lines 325 $lines = file($file); 326 foreach ($lines as $line){ 327 fwrite($fh, $line); 328 } 329 fclose($fh); 330 //try to rename the old file 331 io_rename($file,"$file.old"); 332} 333 334/** 335 * print a nice message even if no styles are loaded yet. 336 */ 337function nice_die($msg){ 338 echo<<<EOT 339 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 340 "http://www.w3.org/TR/html4/loose.dtd"> 341 <html> 342 <head><title>DokuWiki Setup Error</title></head> 343 <body style="font-family: Arial, sans-serif"> 344 <div style="width:60%; margin: auto; background-color: #fcc; 345 border: 1px solid #faa; padding: 0.5em 1em;"> 346 <h1 style="font-size: 120%">DokuWiki Setup Error</h1> 347 <p>$msg</p> 348 </div> 349 </body> 350 </html> 351EOT; 352 exit; 353} 354 355 356//Setup VIM: ex: et ts=2 enc=utf-8 : 357