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 20d8186216SBen 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 2650602150SBen Coburn // init memory caches 27bc3e97beSAndreas Gohr global $cache_revinfo; $cache_revinfo = array(); 28bc3e97beSAndreas Gohr global $cache_wikifn; $cache_wikifn = array(); 29a424cd8eSchris global $cache_cleanid; $cache_cleanid = array(); 30a424cd8eSchris global $cache_authname; $cache_authname = array(); 310a7e3bceSchris global $cache_metadata; $cache_metadata = array(); 3250602150SBen Coburn 334724a577Sandi //prepare config array() 34ee20e7d1Sandi global $conf; 3503c4aec3Schris if (!defined('DOKU_UNITTEST')) { 364724a577Sandi $conf = array(); 374724a577Sandi 38ad15db82Sandi // load the config file(s) 39e7cb32dcSAndreas Gohr require_once(DOKU_CONF.'dokuwiki.php'); 400a6ead41SAndreas Gohr if(@file_exists(DOKU_CONF.'local.php')){ 410a6ead41SAndreas Gohr require_once(DOKU_CONF.'local.php'); 420a6ead41SAndreas Gohr } 4303c4aec3Schris } 44ad15db82Sandi 45ad15db82Sandi //prepare language array 46ee20e7d1Sandi global $lang; 47ad15db82Sandi $lang = array(); 48ed7b5f09Sandi 4916521111Sandi //load the language files 50bc3b6aecSandi require_once(DOKU_INC.'inc/lang/en/lang.php'); 51f949a01cSAndreas Gohr if ( $conf['lang'] && $conf['lang'] != 'en' ) { 52bc3b6aecSandi require_once(DOKU_INC.'inc/lang/'.$conf['lang'].'/lang.php'); 53fc1c55b1Shfuecks } 5416521111Sandi 55ed7b5f09Sandi // define baseURL 56ed7b5f09Sandi if(!defined('DOKU_BASE')) define('DOKU_BASE',getBaseURL()); 57ed7b5f09Sandi if(!defined('DOKU_URL')) define('DOKU_URL',getBaseURL(true)); 58ed7b5f09Sandi 59e71ce681SAndreas Gohr // define cookie and session id 60e71ce681SAndreas Gohr if (!defined('DOKU_COOKIE')) define('DOKU_COOKIE', 'DW'.md5(DOKU_URL)); 61e71ce681SAndreas Gohr 62ee20e7d1Sandi // define Plugin dir 63f62ea8a1Sandi if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 64ee20e7d1Sandi 65ed7b5f09Sandi // define main script 66ed7b5f09Sandi if(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php'); 67ed7b5f09Sandi 686b13307fSandi // define Template baseURL 696b13307fSandi if(!defined('DOKU_TPL')) define('DOKU_TPL', 70f62ea8a1Sandi DOKU_BASE.'lib/tpl/'.$conf['template'].'/'); 716b13307fSandi 7278a6aeb1SAndreas Gohr // define real Template directory 7378a6aeb1SAndreas Gohr if(!defined('DOKU_TPLINC')) define('DOKU_TPLINC', 7478a6aeb1SAndreas Gohr DOKU_INC.'lib/tpl/'.$conf['template'].'/'); 7578a6aeb1SAndreas Gohr 76ed7b5f09Sandi // make session rewrites XHTML compliant 773fc74836Sandi @ini_set('arg_separator.output', '&'); 78ed7b5f09Sandi 793138b5c7SAndreas Gohr // enable gzip compression 803138b5c7SAndreas Gohr if ($conf['gzip_output'] && 813138b5c7SAndreas Gohr !defined('DOKU_DISABLE_GZIP_OUTPUT') && 823138b5c7SAndreas Gohr function_exists('ob_gzhandler') && 833138b5c7SAndreas Gohr preg_match('/gzip|deflate/', $_SERVER['HTTP_ACCEPT_ENCODING'])) { 843138b5c7SAndreas Gohr ob_start('ob_gzhandler'); 853138b5c7SAndreas Gohr } 863138b5c7SAndreas Gohr 87ed7b5f09Sandi // init session 886534245aSAndreas Gohr if (!headers_sent() && !defined('NOSESSION')){ 89ed7b5f09Sandi session_name("DokuWiki"); 90f77429b8SAndreas Gohr session_set_cookie_params(0, DOKU_BASE); 91bad31ae9SAndreas Gohr session_start(); 92bad31ae9SAndreas Gohr } 93ed7b5f09Sandi 94ed7b5f09Sandi // kill magic quotes 95e55eb89cSAndreas Gohr if (get_magic_quotes_gpc() && !defined('MAGIC_QUOTES_STRIPPED')) { 96ed7b5f09Sandi if (!empty($_GET)) remove_magic_quotes($_GET); 97ed7b5f09Sandi if (!empty($_POST)) remove_magic_quotes($_POST); 98ed7b5f09Sandi if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE); 99ed7b5f09Sandi if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST); 1003fc74836Sandi @ini_set('magic_quotes_gpc', 0); 101e55eb89cSAndreas Gohr define('MAGIC_QUOTES_STRIPPED',1); 102ed7b5f09Sandi } 1033fc74836Sandi @set_magic_quotes_runtime(0); 1043fc74836Sandi @ini_set('magic_quotes_sybase',0); 105ed7b5f09Sandi 106*a1637ffdSAndreas Gohr // don't let cookies ever interfere with request vars 107*a1637ffdSAndreas Gohr $_REQUEST = array_merge($_GET,$_POST); 108*a1637ffdSAndreas Gohr 109ed7b5f09Sandi // disable gzip if not available 110fe893490SAndreas Gohr if($conf['compression'] == 'bz' && !function_exists('bzopen')){ 111fe893490SAndreas Gohr $conf['compression'] = 'gz'; 112501252a5SAndreas Gohr } 113fe893490SAndreas Gohr if($conf['compression'] == 'gz' && !function_exists('gzopen')){ 114501252a5SAndreas Gohr $conf['compression'] = 0; 115ed7b5f09Sandi } 116ed7b5f09Sandi 1171ca31cfeSAndreas Gohr // precalculate file creation modes 1181ca31cfeSAndreas Gohr init_creationmodes(); 119ed7b5f09Sandi 1203dc3a5f1Sandi // make real paths and check them 12198407a7aSandi init_paths(); 1227367b368SAndreas Gohr init_files(); 123ed7b5f09Sandi 1248c4f28e8Sjan // automatic upgrade to script versions of certain files 125e7cb32dcSAndreas Gohr scriptify(DOKU_CONF.'users.auth'); 126e7cb32dcSAndreas Gohr scriptify(DOKU_CONF.'acl.auth'); 127f62ea8a1Sandi 128f62ea8a1Sandi 129f62ea8a1Sandi/** 13098407a7aSandi * Checks paths from config file 13198407a7aSandi */ 13298407a7aSandifunction init_paths(){ 13398407a7aSandi global $conf; 13498407a7aSandi 13598407a7aSandi $paths = array('datadir' => 'pages', 13698407a7aSandi 'olddir' => 'attic', 13798407a7aSandi 'mediadir' => 'media', 13898407a7aSandi 'metadir' => 'meta', 13998407a7aSandi 'cachedir' => 'cache', 140579b0f7eSTNHarris 'indexdir' => 'index', 14171726d78SBen Coburn 'lockdir' => 'locks'); 14298407a7aSandi 14398407a7aSandi foreach($paths as $c => $p){ 144bb4866bdSchris if(empty($conf[$c])) $conf[$c] = $conf['savedir'].'/'.$p; 14598407a7aSandi $conf[$c] = init_path($conf[$c]); 1461983acc2SGuy Brand if(empty($conf[$c])) nice_die("The $c ('$p') does not exist, isn't accessible or writable. 14769dc3177SAndreas Gohr You should check your config and permission settings. 14869dc3177SAndreas Gohr Or maybe you want to <a href=\"install.php\">run the 14969dc3177SAndreas Gohr installer</a>?"); 15098407a7aSandi } 15171726d78SBen Coburn 15271726d78SBen Coburn // path to old changelog only needed for upgrading 15371726d78SBen Coburn $conf['changelog_old'] = init_path((isset($conf['changelog']))?($conf['changelog']):($conf['savedir'].'/changes.log')); 15471726d78SBen Coburn if ($conf['changelog_old']=='') { unset($conf['changelog_old']); } 15571726d78SBen Coburn // hardcoded changelog because it is now a cache that lives in meta 15671726d78SBen Coburn $conf['changelog'] = $conf['metadir'].'/_dokuwiki.changes'; 15798407a7aSandi} 15898407a7aSandi 15998407a7aSandi/** 1600d8850c4SAndreas Gohr * Checks the existance of certain files and creates them if missing. 1617367b368SAndreas Gohr */ 1627367b368SAndreas Gohrfunction init_files(){ 1637367b368SAndreas Gohr global $conf; 1640d8850c4SAndreas Gohr 165579b0f7eSTNHarris $files = array( $conf['indexdir'].'/page.idx'); 1667367b368SAndreas Gohr 1677367b368SAndreas Gohr foreach($files as $file){ 1687367b368SAndreas Gohr if(!@file_exists($file)){ 1690d8850c4SAndreas Gohr $fh = @fopen($file,'a'); 1700d8850c4SAndreas Gohr if($fh){ 1717367b368SAndreas Gohr fclose($fh); 1721ca31cfeSAndreas Gohr if($conf['fperm']) chmod($file, $conf['fperm']); 1730d8850c4SAndreas Gohr }else{ 1743816dcbcSAndreas Gohr nice_die("$file is not writable. Check your permissions settings!"); 1750d8850c4SAndreas Gohr } 1767367b368SAndreas Gohr } 1777367b368SAndreas Gohr } 1787367b368SAndreas Gohr} 1797367b368SAndreas Gohr 1807367b368SAndreas Gohr/** 1810d8850c4SAndreas Gohr * Returns absolute path 182f62ea8a1Sandi * 1830d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC. 1840d8850c4SAndreas Gohr * Check for accessability on directories as well. 1850d8850c4SAndreas Gohr * 1860d8850c4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 187f62ea8a1Sandi */ 188f62ea8a1Sandifunction init_path($path){ 1890d8850c4SAndreas Gohr // check existance 190f62ea8a1Sandi $p = realpath($path); 1910d8850c4SAndreas Gohr if(!@file_exists($p)){ 192f62ea8a1Sandi $p = realpath(DOKU_INC.$path); 1930d8850c4SAndreas Gohr if(!@file_exists($p)){ 1948fc4e739Sandi return ''; 195f62ea8a1Sandi } 1960d8850c4SAndreas Gohr } 1970d8850c4SAndreas Gohr 1980d8850c4SAndreas Gohr // check writability 1990d8850c4SAndreas Gohr if(!@is_writable($p)){ 2000d8850c4SAndreas Gohr return ''; 2010d8850c4SAndreas Gohr } 2020d8850c4SAndreas Gohr 2030d8850c4SAndreas Gohr // check accessability (execute bit) for directories 2040d8850c4SAndreas Gohr if(@is_dir($p) && !@file_exists("$p/.")){ 2050d8850c4SAndreas Gohr return ''; 2060d8850c4SAndreas Gohr } 2070d8850c4SAndreas Gohr 2080d8850c4SAndreas Gohr return $p; 2090d8850c4SAndreas Gohr} 2108c4f28e8Sjan 211ed7b5f09Sandi/** 2121ca31cfeSAndreas Gohr * Sets the internal config values fperm and dperm which, when set, 2131ca31cfeSAndreas Gohr * will be used to change the permission of a newly created dir or 2141ca31cfeSAndreas Gohr * file with chmod. Considers the influence of the system's umask 2151ca31cfeSAndreas Gohr * setting the values only if needed. 2161ca31cfeSAndreas Gohr */ 2171ca31cfeSAndreas Gohrfunction init_creationmodes(){ 2181ca31cfeSAndreas Gohr global $conf; 2191ca31cfeSAndreas Gohr 2201ca31cfeSAndreas Gohr // Legacy support for old umask/dmask scheme 2211ca31cfeSAndreas Gohr unset($conf['dmask']); 2221ca31cfeSAndreas Gohr unset($conf['fmask']); 2231ca31cfeSAndreas Gohr unset($conf['umask']); 2241ca31cfeSAndreas Gohr unset($conf['fperm']); 2251ca31cfeSAndreas Gohr unset($conf['dperm']); 2261ca31cfeSAndreas Gohr 2279f3cdec3SAndreas Gohr // get system umask, fallback to 0 if none available 2289f3cdec3SAndreas Gohr $umask = @umask(); 2299f3cdec3SAndreas Gohr if(!$umask) $umask = 0000; 2301ca31cfeSAndreas Gohr 2311ca31cfeSAndreas Gohr // check what is set automatically by the system on file creation 2321ca31cfeSAndreas Gohr // and set the fperm param if it's not what we want 2331ca31cfeSAndreas Gohr $auto_fmode = 0666 & ~$umask; 2341ca31cfeSAndreas Gohr if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode']; 2351ca31cfeSAndreas Gohr 2361ca31cfeSAndreas Gohr // check what is set automatically by the system on file creation 2371ca31cfeSAndreas Gohr // and set the dperm param if it's not what we want 2381ca31cfeSAndreas Gohr $auto_dmode = $conf['dmode'] & ~$umask; 2391ca31cfeSAndreas Gohr if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode']; 2401ca31cfeSAndreas Gohr} 2411ca31cfeSAndreas Gohr 2421ca31cfeSAndreas Gohr/** 243ed7b5f09Sandi * remove magic quotes recursivly 244ed7b5f09Sandi * 245ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org> 246ed7b5f09Sandi */ 247ed7b5f09Sandifunction remove_magic_quotes(&$array) { 248ed7b5f09Sandi foreach (array_keys($array) as $key) { 249ed7b5f09Sandi if (is_array($array[$key])) { 250ed7b5f09Sandi remove_magic_quotes($array[$key]); 251ed7b5f09Sandi }else { 252ed7b5f09Sandi $array[$key] = stripslashes($array[$key]); 253ed7b5f09Sandi } 254ed7b5f09Sandi } 255ed7b5f09Sandi} 256ed7b5f09Sandi 257ed7b5f09Sandi/** 258ed7b5f09Sandi * Returns the full absolute URL to the directory where 259ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash) 260ed7b5f09Sandi * 261ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org> 262ed7b5f09Sandi */ 263ed7b5f09Sandifunction getBaseURL($abs=false){ 264ed7b5f09Sandi global $conf; 265ed7b5f09Sandi //if canonical url enabled always return absolute 266ed7b5f09Sandi if($conf['canonical']) $abs = true; 267ed7b5f09Sandi 26892b83b77Sandi if($conf['basedir']){ 269919eeb46Sandi $dir = $conf['basedir'].'/'; 27089aa05dbSAndreas Gohr }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){ 271bdc127a4Sandi $dir = dirname($_SERVER['SCRIPT_NAME']).'/'; 27289aa05dbSAndreas Gohr }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){ 27389aa05dbSAndreas Gohr $dir = dirname($_SERVER['PHP_SELF']).'/'; 274093ec9e4Sandi }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){ 275093ec9e4Sandi $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','', 276093ec9e4Sandi $_SERVER['SCRIPT_FILENAME']); 277093ec9e4Sandi $dir = dirname('/'.$dir).'/'; 27892b83b77Sandi }else{ 27989aa05dbSAndreas Gohr $dir = './'; //probably wrong 28092b83b77Sandi } 281ed7b5f09Sandi 282ed7b5f09Sandi $dir = str_replace('\\','/',$dir); #bugfix for weird WIN behaviour 283ed7b5f09Sandi $dir = preg_replace('#//+#','/',$dir); 284ed7b5f09Sandi 285f62ea8a1Sandi //handle script in lib/exe dir 286f62ea8a1Sandi $dir = preg_replace('!lib/exe/$!','',$dir); 287f62ea8a1Sandi 288488d5fa0SMichael Klier chi@chimeric.de //handle script in lib/plugins dir 289488d5fa0SMichael Klier chi@chimeric.de $dir = preg_replace('!lib/plugins/.*$!','',$dir); 290488d5fa0SMichael Klier chi@chimeric.de 291ed7b5f09Sandi //finish here for relative URLs 292ed7b5f09Sandi if(!$abs) return $dir; 293ed7b5f09Sandi 294ef7b3ecdSAndreas Gohr //use config option if available 295ef7b3ecdSAndreas Gohr if($conf['baseurl']) return $conf['baseurl'].$dir; 296ef7b3ecdSAndreas Gohr 297e82e3526SAndreas Gohr //split hostheader into host and port 298e82e3526SAndreas Gohr list($host,$port) = explode(':',$_SERVER['HTTP_HOST']); 299e82e3526SAndreas Gohr if(!$port) $port = $_SERVER['SERVER_PORT']; 300e82e3526SAndreas Gohr if(!$port) $port = 80; 301ed7b5f09Sandi 302ed7b5f09Sandi // see if HTTPS is enabled - apache leaves this empty when not available, 303ed7b5f09Sandi // IIS sets it to 'off', 'false' and 'disabled' are just guessing 304ed7b5f09Sandi if (preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){ 305ed7b5f09Sandi $proto = 'http://'; 306e82e3526SAndreas Gohr if ($port == '80') { 307ed7b5f09Sandi $port=''; 308ed7b5f09Sandi } 309ed7b5f09Sandi }else{ 310ed7b5f09Sandi $proto = 'https://'; 311e82e3526SAndreas Gohr if ($port == '443') { 312ed7b5f09Sandi $port=''; 313ed7b5f09Sandi } 314ed7b5f09Sandi } 315ed7b5f09Sandi 316e82e3526SAndreas Gohr if($port) $port = ':'.$port; 317e82e3526SAndreas Gohr 318ed7b5f09Sandi return $proto.$host.$port.$dir; 319ed7b5f09Sandi} 320ed7b5f09Sandi 321b000c6d4Sandi/** 322b000c6d4Sandi * Append a PHP extension to a given file and adds an exit call 323b000c6d4Sandi * 324b000c6d4Sandi * This is used to migrate some old configfiles. An added PHP extension 325b000c6d4Sandi * ensures the contents are not shown to webusers even if .htaccess files 326b000c6d4Sandi * do not work 327b000c6d4Sandi * 328b000c6d4Sandi * @author Jan Decaluwe <jan@jandecaluwe.com> 329b000c6d4Sandi */ 3308c4f28e8Sjanfunction scriptify($file) { 3318c4f28e8Sjan // checks 3328c4f28e8Sjan if (!is_readable($file)) { 3338c4f28e8Sjan return; 3348c4f28e8Sjan } 3358c4f28e8Sjan $fn = $file.'.php'; 3368c4f28e8Sjan if (@file_exists($fn)) { 3378c4f28e8Sjan return; 3388c4f28e8Sjan } 3398c4f28e8Sjan $fh = fopen($fn, 'w'); 3408c4f28e8Sjan if (!$fh) { 3413816dcbcSAndreas Gohr nice_die($fn.' is not writable. Check your permission settings!'); 3428c4f28e8Sjan } 3438c4f28e8Sjan // write php exit hack first 3448c4f28e8Sjan fwrite($fh, "# $fn\n"); 3458c4f28e8Sjan fwrite($fh, '# <?php exit()?>'."\n"); 3468c4f28e8Sjan fwrite($fh, "# Don't modify the lines above\n"); 3478c4f28e8Sjan fwrite($fh, "#\n"); 3488c4f28e8Sjan // copy existing lines 3498c4f28e8Sjan $lines = file($file); 3508c4f28e8Sjan foreach ($lines as $line){ 3518c4f28e8Sjan fwrite($fh, $line); 3528c4f28e8Sjan } 3533ba793e2Sandi fclose($fh); 354b000c6d4Sandi //try to rename the old file 3553aee4c27SAndreas Gohr io_rename($file,"$file.old"); 3568c4f28e8Sjan} 3578c4f28e8Sjan 3583816dcbcSAndreas Gohr/** 3593816dcbcSAndreas Gohr * print a nice message even if no styles are loaded yet. 3603816dcbcSAndreas Gohr */ 3613816dcbcSAndreas Gohrfunction nice_die($msg){ 3623816dcbcSAndreas Gohr echo<<<EOT 3633816dcbcSAndreas Gohr <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 3643816dcbcSAndreas Gohr "http://www.w3.org/TR/html4/loose.dtd"> 3653816dcbcSAndreas Gohr <html> 3663816dcbcSAndreas Gohr <head><title>DokuWiki Setup Error</title></head> 3673816dcbcSAndreas Gohr <body style="font-family: Arial, sans-serif"> 3683816dcbcSAndreas Gohr <div style="width:60%; margin: auto; background-color: #fcc; 3693816dcbcSAndreas Gohr border: 1px solid #faa; padding: 0.5em 1em;"> 3703816dcbcSAndreas Gohr <h1 style="font-size: 120%">DokuWiki Setup Error</h1> 3713816dcbcSAndreas Gohr <p>$msg</p> 3723816dcbcSAndreas Gohr </div> 3733816dcbcSAndreas Gohr </body> 3743816dcbcSAndreas Gohr </html> 3753816dcbcSAndreas GohrEOT; 3763816dcbcSAndreas Gohr exit; 3773816dcbcSAndreas Gohr} 3783816dcbcSAndreas Gohr 379ed7b5f09Sandi 380340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 : 381