1ed7b5f09Sandi<?php 2ed7b5f09Sandi/** 3ed7b5f09Sandi * Initialize some defaults needed for DokuWiki 4ed7b5f09Sandi */ 5ed7b5f09Sandi 6a609a9ccSBen Coburn // start timing Dokuwiki execution 7a609a9ccSBen Coburn function delta_time($start=0) { 8a609a9ccSBen Coburn list($usec, $sec) = explode(" ", microtime()); 9a609a9ccSBen Coburn return ((float)$usec+(float)$sec)-((float)$start); 10a609a9ccSBen Coburn } 11a609a9ccSBen Coburn define('DOKU_START_TIME', delta_time()); 12a609a9ccSBen Coburn 13ed7b5f09Sandi // define the include path 14ed7b5f09Sandi if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/'); 15ad15db82Sandi 16e7cb32dcSAndreas Gohr // define config path (packagers may want to change this to /etc/dokuwiki/) 17b7551a6dSEsther Brunner if(!defined('DOKU_CONF')) define('DOKU_CONF',DOKU_INC.'conf/'); 18e7cb32dcSAndreas Gohr 19bad905f1SBen Coburn // check for error reporting override or set error reporting to sane values 20bad905f1SBen Coburn if (!defined('DOKU_E_LEVEL') && file_exists(DOKU_CONF.'report_e_all')) { 21bad905f1SBen Coburn define('DOKU_E_LEVEL', E_ALL); 22bad905f1SBen Coburn } 23bad905f1SBen Coburn if (!defined('DOKU_E_LEVEL')) { error_reporting(E_ALL ^ E_NOTICE); } 24bad905f1SBen Coburn else { error_reporting(DOKU_E_LEVEL); } 25c53ea5f2Sandi 264724a577Sandi //prepare config array() 27ee20e7d1Sandi global $conf; 28*03c4aec3Schris if (!defined('DOKU_UNITTEST')) { 294724a577Sandi $conf = array(); 304724a577Sandi 31ad15db82Sandi // load the config file(s) 32e7cb32dcSAndreas Gohr require_once(DOKU_CONF.'dokuwiki.php'); 330a6ead41SAndreas Gohr if(@file_exists(DOKU_CONF.'local.php')){ 340a6ead41SAndreas Gohr require_once(DOKU_CONF.'local.php'); 350a6ead41SAndreas Gohr } 36*03c4aec3Schris } 37ad15db82Sandi 38ad15db82Sandi //prepare language array 39ee20e7d1Sandi global $lang; 40ad15db82Sandi $lang = array(); 41ed7b5f09Sandi 4216521111Sandi //load the language files 43bc3b6aecSandi require_once(DOKU_INC.'inc/lang/en/lang.php'); 44f949a01cSAndreas Gohr if ( $conf['lang'] && $conf['lang'] != 'en' ) { 45bc3b6aecSandi require_once(DOKU_INC.'inc/lang/'.$conf['lang'].'/lang.php'); 46fc1c55b1Shfuecks } 4716521111Sandi 48ed7b5f09Sandi // define baseURL 49ed7b5f09Sandi if(!defined('DOKU_BASE')) define('DOKU_BASE',getBaseURL()); 50ed7b5f09Sandi if(!defined('DOKU_URL')) define('DOKU_URL',getBaseURL(true)); 51ed7b5f09Sandi 52ee20e7d1Sandi // define Plugin dir 53f62ea8a1Sandi if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 54ee20e7d1Sandi 55ed7b5f09Sandi // define main script 56ed7b5f09Sandi if(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php'); 57ed7b5f09Sandi 586b13307fSandi // define Template baseURL 596b13307fSandi if(!defined('DOKU_TPL')) define('DOKU_TPL', 60f62ea8a1Sandi DOKU_BASE.'lib/tpl/'.$conf['template'].'/'); 616b13307fSandi 6278a6aeb1SAndreas Gohr // define real Template directory 6378a6aeb1SAndreas Gohr if(!defined('DOKU_TPLINC')) define('DOKU_TPLINC', 6478a6aeb1SAndreas Gohr DOKU_INC.'lib/tpl/'.$conf['template'].'/'); 6578a6aeb1SAndreas Gohr 66ed7b5f09Sandi // make session rewrites XHTML compliant 673fc74836Sandi @ini_set('arg_separator.output', '&'); 68ed7b5f09Sandi 693138b5c7SAndreas Gohr // enable gzip compression 703138b5c7SAndreas Gohr if ($conf['gzip_output'] && 713138b5c7SAndreas Gohr !defined('DOKU_DISABLE_GZIP_OUTPUT') && 723138b5c7SAndreas Gohr function_exists('ob_gzhandler') && 733138b5c7SAndreas Gohr preg_match('/gzip|deflate/', $_SERVER['HTTP_ACCEPT_ENCODING'])) { 743138b5c7SAndreas Gohr ob_start('ob_gzhandler'); 753138b5c7SAndreas Gohr } 763138b5c7SAndreas Gohr 77ed7b5f09Sandi // init session 786534245aSAndreas Gohr if (!headers_sent() && !defined('NOSESSION')){ 79ed7b5f09Sandi session_name("DokuWiki"); 80bad31ae9SAndreas Gohr session_start(); 81bad31ae9SAndreas Gohr } 82ed7b5f09Sandi 83ed7b5f09Sandi // kill magic quotes 84e55eb89cSAndreas Gohr if (get_magic_quotes_gpc() && !defined('MAGIC_QUOTES_STRIPPED')) { 85ed7b5f09Sandi if (!empty($_GET)) remove_magic_quotes($_GET); 86ed7b5f09Sandi if (!empty($_POST)) remove_magic_quotes($_POST); 87ed7b5f09Sandi if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE); 88ed7b5f09Sandi if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST); 8956146e0dSAndreas Gohr# if (!empty($_SESSION)) remove_magic_quotes($_SESSION); #FIXME needed ? 903fc74836Sandi @ini_set('magic_quotes_gpc', 0); 91e55eb89cSAndreas Gohr define('MAGIC_QUOTES_STRIPPED',1); 92ed7b5f09Sandi } 933fc74836Sandi @set_magic_quotes_runtime(0); 943fc74836Sandi @ini_set('magic_quotes_sybase',0); 95ed7b5f09Sandi 96ed7b5f09Sandi // disable gzip if not available 97ed7b5f09Sandi if($conf['usegzip'] && !function_exists('gzopen')){ 98ed7b5f09Sandi $conf['usegzip'] = 0; 99ed7b5f09Sandi } 100ed7b5f09Sandi 1011ca31cfeSAndreas Gohr // precalculate file creation modes 1021ca31cfeSAndreas Gohr init_creationmodes(); 103ed7b5f09Sandi 1043dc3a5f1Sandi // make real paths and check them 10598407a7aSandi init_paths(); 1067367b368SAndreas Gohr init_files(); 107ed7b5f09Sandi 1088c4f28e8Sjan // automatic upgrade to script versions of certain files 109e7cb32dcSAndreas Gohr scriptify(DOKU_CONF.'users.auth'); 110e7cb32dcSAndreas Gohr scriptify(DOKU_CONF.'acl.auth'); 111f62ea8a1Sandi 112f62ea8a1Sandi 113f62ea8a1Sandi/** 11498407a7aSandi * Checks paths from config file 11598407a7aSandi */ 11698407a7aSandifunction init_paths(){ 11798407a7aSandi global $conf; 11898407a7aSandi 11998407a7aSandi $paths = array('datadir' => 'pages', 12098407a7aSandi 'olddir' => 'attic', 12198407a7aSandi 'mediadir' => 'media', 12298407a7aSandi 'metadir' => 'meta', 12398407a7aSandi 'cachedir' => 'cache', 12498407a7aSandi 'lockdir' => 'locks', 12598407a7aSandi 'changelog' => 'changes.log'); 12698407a7aSandi 12798407a7aSandi foreach($paths as $c => $p){ 12898407a7aSandi if(!$conf[$c]) $conf[$c] = $conf['savedir'].'/'.$p; 12998407a7aSandi $conf[$c] = init_path($conf[$c]); 1303816dcbcSAndreas Gohr if(!$conf[$c]) nice_die("The $c does not exist, isn't accessable or writable. 13169dc3177SAndreas Gohr You should check your config and permission settings. 13269dc3177SAndreas Gohr Or maybe you want to <a href=\"install.php\">run the 13369dc3177SAndreas Gohr installer</a>?"); 13498407a7aSandi } 13598407a7aSandi} 13698407a7aSandi 13798407a7aSandi/** 1380d8850c4SAndreas Gohr * Checks the existance of certain files and creates them if missing. 1397367b368SAndreas Gohr */ 1407367b368SAndreas Gohrfunction init_files(){ 1417367b368SAndreas Gohr global $conf; 1420d8850c4SAndreas Gohr 1437367b368SAndreas Gohr $files = array( $conf['cachedir'].'/word.idx', 1447367b368SAndreas Gohr $conf['cachedir'].'/page.idx', 1450d8850c4SAndreas Gohr $conf['cachedir'].'/index.idx'); 1467367b368SAndreas Gohr 1477367b368SAndreas Gohr foreach($files as $file){ 1487367b368SAndreas Gohr if(!@file_exists($file)){ 1490d8850c4SAndreas Gohr $fh = @fopen($file,'a'); 1500d8850c4SAndreas Gohr if($fh){ 1517367b368SAndreas Gohr fclose($fh); 1521ca31cfeSAndreas Gohr if($conf['fperm']) chmod($file, $conf['fperm']); 1530d8850c4SAndreas Gohr }else{ 1543816dcbcSAndreas Gohr nice_die("$file is not writable. Check your permissions settings!"); 1550d8850c4SAndreas Gohr } 1567367b368SAndreas Gohr } 1577367b368SAndreas Gohr } 1587367b368SAndreas Gohr} 1597367b368SAndreas Gohr 1607367b368SAndreas Gohr/** 1610d8850c4SAndreas Gohr * Returns absolute path 162f62ea8a1Sandi * 1630d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC. 1640d8850c4SAndreas Gohr * Check for accessability on directories as well. 1650d8850c4SAndreas Gohr * 1660d8850c4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 167f62ea8a1Sandi */ 168f62ea8a1Sandifunction init_path($path){ 1690d8850c4SAndreas Gohr // check existance 170f62ea8a1Sandi $p = realpath($path); 1710d8850c4SAndreas Gohr if(!@file_exists($p)){ 172f62ea8a1Sandi $p = realpath(DOKU_INC.$path); 1730d8850c4SAndreas Gohr if(!@file_exists($p)){ 1748fc4e739Sandi return ''; 175f62ea8a1Sandi } 1760d8850c4SAndreas Gohr } 1770d8850c4SAndreas Gohr 1780d8850c4SAndreas Gohr // check writability 1790d8850c4SAndreas Gohr if(!@is_writable($p)){ 1800d8850c4SAndreas Gohr return ''; 1810d8850c4SAndreas Gohr } 1820d8850c4SAndreas Gohr 1830d8850c4SAndreas Gohr // check accessability (execute bit) for directories 1840d8850c4SAndreas Gohr if(@is_dir($p) && !@file_exists("$p/.")){ 1850d8850c4SAndreas Gohr return ''; 1860d8850c4SAndreas Gohr } 1870d8850c4SAndreas Gohr 1880d8850c4SAndreas Gohr return $p; 1890d8850c4SAndreas Gohr} 1908c4f28e8Sjan 191ed7b5f09Sandi/** 1921ca31cfeSAndreas Gohr * Sets the internal config values fperm and dperm which, when set, 1931ca31cfeSAndreas Gohr * will be used to change the permission of a newly created dir or 1941ca31cfeSAndreas Gohr * file with chmod. Considers the influence of the system's umask 1951ca31cfeSAndreas Gohr * setting the values only if needed. 1961ca31cfeSAndreas Gohr */ 1971ca31cfeSAndreas Gohrfunction init_creationmodes(){ 1981ca31cfeSAndreas Gohr global $conf; 1991ca31cfeSAndreas Gohr 2001ca31cfeSAndreas Gohr // Legacy support for old umask/dmask scheme 2011ca31cfeSAndreas Gohr unset($conf['dmask']); 2021ca31cfeSAndreas Gohr unset($conf['fmask']); 2031ca31cfeSAndreas Gohr unset($conf['umask']); 2041ca31cfeSAndreas Gohr unset($conf['fperm']); 2051ca31cfeSAndreas Gohr unset($conf['dperm']); 2061ca31cfeSAndreas Gohr 2079f3cdec3SAndreas Gohr // get system umask, fallback to 0 if none available 2089f3cdec3SAndreas Gohr $umask = @umask(); 2099f3cdec3SAndreas Gohr if(!$umask) $umask = 0000; 2101ca31cfeSAndreas Gohr 2111ca31cfeSAndreas Gohr // check what is set automatically by the system on file creation 2121ca31cfeSAndreas Gohr // and set the fperm param if it's not what we want 2131ca31cfeSAndreas Gohr $auto_fmode = 0666 & ~$umask; 2141ca31cfeSAndreas Gohr if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode']; 2151ca31cfeSAndreas Gohr 2161ca31cfeSAndreas Gohr // check what is set automatically by the system on file creation 2171ca31cfeSAndreas Gohr // and set the dperm param if it's not what we want 2181ca31cfeSAndreas Gohr $auto_dmode = $conf['dmode'] & ~$umask; 2191ca31cfeSAndreas Gohr if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode']; 2201ca31cfeSAndreas Gohr} 2211ca31cfeSAndreas Gohr 2221ca31cfeSAndreas Gohr/** 223ed7b5f09Sandi * remove magic quotes recursivly 224ed7b5f09Sandi * 225ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org> 226ed7b5f09Sandi */ 227ed7b5f09Sandifunction remove_magic_quotes(&$array) { 228ed7b5f09Sandi foreach (array_keys($array) as $key) { 229ed7b5f09Sandi if (is_array($array[$key])) { 230ed7b5f09Sandi remove_magic_quotes($array[$key]); 231ed7b5f09Sandi }else { 232ed7b5f09Sandi $array[$key] = stripslashes($array[$key]); 233ed7b5f09Sandi } 234ed7b5f09Sandi } 235ed7b5f09Sandi} 236ed7b5f09Sandi 237ed7b5f09Sandi/** 238ed7b5f09Sandi * Returns the full absolute URL to the directory where 239ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash) 240ed7b5f09Sandi * 241ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org> 242ed7b5f09Sandi */ 243ed7b5f09Sandifunction getBaseURL($abs=false){ 244ed7b5f09Sandi global $conf; 245ed7b5f09Sandi //if canonical url enabled always return absolute 246ed7b5f09Sandi if($conf['canonical']) $abs = true; 247ed7b5f09Sandi 24892b83b77Sandi if($conf['basedir']){ 249919eeb46Sandi $dir = $conf['basedir'].'/'; 25089aa05dbSAndreas Gohr }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){ 251bdc127a4Sandi $dir = dirname($_SERVER['SCRIPT_NAME']).'/'; 25289aa05dbSAndreas Gohr }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){ 25389aa05dbSAndreas Gohr $dir = dirname($_SERVER['PHP_SELF']).'/'; 254093ec9e4Sandi }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){ 255093ec9e4Sandi $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','', 256093ec9e4Sandi $_SERVER['SCRIPT_FILENAME']); 257093ec9e4Sandi $dir = dirname('/'.$dir).'/'; 25892b83b77Sandi }else{ 25989aa05dbSAndreas Gohr $dir = './'; //probably wrong 26092b83b77Sandi } 261ed7b5f09Sandi 262ed7b5f09Sandi $dir = str_replace('\\','/',$dir); #bugfix for weird WIN behaviour 263ed7b5f09Sandi $dir = preg_replace('#//+#','/',$dir); 264ed7b5f09Sandi 265f62ea8a1Sandi //handle script in lib/exe dir 266f62ea8a1Sandi $dir = preg_replace('!lib/exe/$!','',$dir); 267f62ea8a1Sandi 268488d5fa0SMichael Klier chi@chimeric.de //handle script in lib/plugins dir 269488d5fa0SMichael Klier chi@chimeric.de $dir = preg_replace('!lib/plugins/.*$!','',$dir); 270488d5fa0SMichael Klier chi@chimeric.de 271ed7b5f09Sandi //finish here for relative URLs 272ed7b5f09Sandi if(!$abs) return $dir; 273ed7b5f09Sandi 274ef7b3ecdSAndreas Gohr //use config option if available 275ef7b3ecdSAndreas Gohr if($conf['baseurl']) return $conf['baseurl'].$dir; 276ef7b3ecdSAndreas Gohr 277e82e3526SAndreas Gohr //split hostheader into host and port 278e82e3526SAndreas Gohr list($host,$port) = explode(':',$_SERVER['HTTP_HOST']); 279e82e3526SAndreas Gohr if(!$port) $port = $_SERVER['SERVER_PORT']; 280e82e3526SAndreas Gohr if(!$port) $port = 80; 281ed7b5f09Sandi 282ed7b5f09Sandi // see if HTTPS is enabled - apache leaves this empty when not available, 283ed7b5f09Sandi // IIS sets it to 'off', 'false' and 'disabled' are just guessing 284ed7b5f09Sandi if (preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){ 285ed7b5f09Sandi $proto = 'http://'; 286e82e3526SAndreas Gohr if ($port == '80') { 287ed7b5f09Sandi $port=''; 288ed7b5f09Sandi } 289ed7b5f09Sandi }else{ 290ed7b5f09Sandi $proto = 'https://'; 291e82e3526SAndreas Gohr if ($port == '443') { 292ed7b5f09Sandi $port=''; 293ed7b5f09Sandi } 294ed7b5f09Sandi } 295ed7b5f09Sandi 296e82e3526SAndreas Gohr if($port) $port = ':'.$port; 297e82e3526SAndreas Gohr 298ed7b5f09Sandi return $proto.$host.$port.$dir; 299ed7b5f09Sandi} 300ed7b5f09Sandi 301b000c6d4Sandi/** 302b000c6d4Sandi * Append a PHP extension to a given file and adds an exit call 303b000c6d4Sandi * 304b000c6d4Sandi * This is used to migrate some old configfiles. An added PHP extension 305b000c6d4Sandi * ensures the contents are not shown to webusers even if .htaccess files 306b000c6d4Sandi * do not work 307b000c6d4Sandi * 308b000c6d4Sandi * @author Jan Decaluwe <jan@jandecaluwe.com> 309b000c6d4Sandi */ 3108c4f28e8Sjanfunction scriptify($file) { 3118c4f28e8Sjan // checks 3128c4f28e8Sjan if (!is_readable($file)) { 3138c4f28e8Sjan return; 3148c4f28e8Sjan } 3158c4f28e8Sjan $fn = $file.'.php'; 3168c4f28e8Sjan if (@file_exists($fn)) { 3178c4f28e8Sjan return; 3188c4f28e8Sjan } 3198c4f28e8Sjan $fh = fopen($fn, 'w'); 3208c4f28e8Sjan if (!$fh) { 3213816dcbcSAndreas Gohr nice_die($fn.' is not writable. Check your permission settings!'); 3228c4f28e8Sjan } 3238c4f28e8Sjan // write php exit hack first 3248c4f28e8Sjan fwrite($fh, "# $fn\n"); 3258c4f28e8Sjan fwrite($fh, '# <?php exit()?>'."\n"); 3268c4f28e8Sjan fwrite($fh, "# Don't modify the lines above\n"); 3278c4f28e8Sjan fwrite($fh, "#\n"); 3288c4f28e8Sjan // copy existing lines 3298c4f28e8Sjan $lines = file($file); 3308c4f28e8Sjan foreach ($lines as $line){ 3318c4f28e8Sjan fwrite($fh, $line); 3328c4f28e8Sjan } 3333ba793e2Sandi fclose($fh); 334b000c6d4Sandi //try to rename the old file 3353aee4c27SAndreas Gohr io_rename($file,"$file.old"); 3368c4f28e8Sjan} 3378c4f28e8Sjan 3383816dcbcSAndreas Gohr/** 3393816dcbcSAndreas Gohr * print a nice message even if no styles are loaded yet. 3403816dcbcSAndreas Gohr */ 3413816dcbcSAndreas Gohrfunction nice_die($msg){ 3423816dcbcSAndreas Gohr echo<<<EOT 3433816dcbcSAndreas Gohr <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 3443816dcbcSAndreas Gohr "http://www.w3.org/TR/html4/loose.dtd"> 3453816dcbcSAndreas Gohr <html> 3463816dcbcSAndreas Gohr <head><title>DokuWiki Setup Error</title></head> 3473816dcbcSAndreas Gohr <body style="font-family: Arial, sans-serif"> 3483816dcbcSAndreas Gohr <div style="width:60%; margin: auto; background-color: #fcc; 3493816dcbcSAndreas Gohr border: 1px solid #faa; padding: 0.5em 1em;"> 3503816dcbcSAndreas Gohr <h1 style="font-size: 120%">DokuWiki Setup Error</h1> 3513816dcbcSAndreas Gohr <p>$msg</p> 3523816dcbcSAndreas Gohr </div> 3533816dcbcSAndreas Gohr </body> 3543816dcbcSAndreas Gohr </html> 3553816dcbcSAndreas GohrEOT; 3563816dcbcSAndreas Gohr exit; 3573816dcbcSAndreas Gohr} 3583816dcbcSAndreas Gohr 359ed7b5f09Sandi 360340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 : 361