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