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 1348beefecSAndreas Gohr // if available load a preload config file 14e6266454SChris Smith $preload = fullpath(dirname(__FILE__)).'/preload.php'; 15e6266454SChris Smith if (@file_exists($preload)) include($preload); 1648beefecSAndreas Gohr 17ed7b5f09Sandi // define the include path 1800976812SAndreas Gohr if(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../').'/'); 19ad15db82Sandi 20e7cb32dcSAndreas Gohr // define config path (packagers may want to change this to /etc/dokuwiki/) 21b7551a6dSEsther Brunner if(!defined('DOKU_CONF')) define('DOKU_CONF',DOKU_INC.'conf/'); 22e7cb32dcSAndreas Gohr 23bad905f1SBen Coburn // check for error reporting override or set error reporting to sane values 24d8186216SBen Coburn if (!defined('DOKU_E_LEVEL') && @file_exists(DOKU_CONF.'report_e_all')) { 25bad905f1SBen Coburn define('DOKU_E_LEVEL', E_ALL); 26bad905f1SBen Coburn } 27fc80ed59SAndreas Gohr if (!defined('DOKU_E_LEVEL')) { 28fc80ed59SAndreas Gohr if(defined('E_DEPRECATED')){ // since php 5.3 29fc80ed59SAndreas Gohr error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED); 30fc80ed59SAndreas Gohr }else{ 31fc80ed59SAndreas Gohr error_reporting(E_ALL ^ E_NOTICE); 32fc80ed59SAndreas Gohr } 33fc80ed59SAndreas Gohr } else { 34fc80ed59SAndreas Gohr error_reporting(DOKU_E_LEVEL); 35fc80ed59SAndreas Gohr } 36c53ea5f2Sandi 3750602150SBen Coburn // init memory caches 38bc3e97beSAndreas Gohr global $cache_revinfo; $cache_revinfo = array(); 39bc3e97beSAndreas Gohr global $cache_wikifn; $cache_wikifn = array(); 40a424cd8eSchris global $cache_cleanid; $cache_cleanid = array(); 41a424cd8eSchris global $cache_authname; $cache_authname = array(); 420a7e3bceSchris global $cache_metadata; $cache_metadata = array(); 4350602150SBen Coburn 44cb043f52SChris Smith //set the configuration cascade - but only if its not already been set in preload.php 45cb043f52SChris Smith global $config_cascade; 46cb043f52SChris Smith if (empty($config_cascade)) { 47cb043f52SChris Smith $config_cascade = array( 48cb043f52SChris Smith 'main' => array( 49cb043f52SChris Smith 'default' => array(DOKU_CONF.'dokuwiki.php'), 50cb043f52SChris Smith 'local' => array(DOKU_CONF.'local.php'), 51cb043f52SChris Smith 'protected' => array(DOKU_CONF.'local.protected.php'), 52cb043f52SChris Smith ), 53cb043f52SChris Smith 'acronyms' => array( 54b303b92cSChris Smith 'default' => array(DOKU_CONF.'acronyms.conf'), 55b303b92cSChris Smith 'local' => array(DOKU_CONF.'acronyms.local.conf'), 56cb043f52SChris Smith ), 57cb043f52SChris Smith 'entities' => array( 58b303b92cSChris Smith 'default' => array(DOKU_CONF.'entities.conf'), 59b303b92cSChris Smith 'local' => array(DOKU_CONF.'entities.local.conf'), 60cb043f52SChris Smith ), 61cb043f52SChris Smith 'interwiki' => array( 62b303b92cSChris Smith 'default' => array(DOKU_CONF.'interwiki.conf'), 63b303b92cSChris Smith 'local' => array(DOKU_CONF.'interwiki.local.conf'), 64cb043f52SChris Smith ), 65f8121585SChris Smith 'license' => array( 66f8121585SChris Smith 'default' => array(DOKU_CONF.'license.php'), 67f8121585SChris Smith 'local' => array(DOKU_CONF.'license.local.php'), 68f8121585SChris Smith ), 69f8121585SChris Smith 'mediameta' => array( 70f8121585SChris Smith 'default' => array(DOKU_CONF.'mediameta.php'), 71f8121585SChris Smith 'local' => array(DOKU_CONF.'mediameta.local.php'), 72f8121585SChris Smith ), 73cb043f52SChris Smith 'mime' => array( 74b303b92cSChris Smith 'default' => array(DOKU_CONF.'mime.conf'), 75b303b92cSChris Smith 'local' => array(DOKU_CONF.'mime.local.conf'), 76cb043f52SChris Smith ), 77cb043f52SChris Smith 'scheme' => array( 78b303b92cSChris Smith 'default' => array(DOKU_CONF.'scheme.conf'), 79b303b92cSChris Smith 'local' => array(DOKU_CONF.'scheme.local.conf'), 80cb043f52SChris Smith ), 81cb043f52SChris Smith 'smileys' => array( 82b303b92cSChris Smith 'default' => array(DOKU_CONF.'smileys.conf'), 83b303b92cSChris Smith 'local' => array(DOKU_CONF.'smileys.local.conf'), 84cb043f52SChris Smith ), 85cb043f52SChris Smith 'wordblock' => array( 86b303b92cSChris Smith 'default' => array(DOKU_CONF.'wordblock.conf'), 87b303b92cSChris Smith 'local' => array(DOKU_CONF.'wordblock.local.conf'), 88cb043f52SChris Smith ), 89cb043f52SChris Smith ); 90cb043f52SChris Smith } 91cb043f52SChris Smith 924724a577Sandi //prepare config array() 93ee20e7d1Sandi global $conf; 944724a577Sandi $conf = array(); 954724a577Sandi 96cb043f52SChris Smith // load the global config file(s) 97b303b92cSChris Smith foreach (array('default','local','protected') as $config_group) { 98f8121585SChris Smith if (empty($config_cascade['main'][$config_group])) continue; 99b303b92cSChris Smith foreach ($config_cascade['main'][$config_group] as $config_file) { 100f8121585SChris Smith if (@file_exists($config_file)) { 101f8121585SChris Smith include($config_file); 102f8121585SChris Smith } 103cb043f52SChris Smith } 1040a6ead41SAndreas Gohr } 105ad15db82Sandi 106ad15db82Sandi //prepare language array 107ee20e7d1Sandi global $lang; 108ad15db82Sandi $lang = array(); 109ed7b5f09Sandi 11016521111Sandi //load the language files 111bc3b6aecSandi require_once(DOKU_INC.'inc/lang/en/lang.php'); 112f949a01cSAndreas Gohr if ( $conf['lang'] && $conf['lang'] != 'en' ) { 113bc3b6aecSandi require_once(DOKU_INC.'inc/lang/'.$conf['lang'].'/lang.php'); 114fc1c55b1Shfuecks } 11516521111Sandi 116066fee30SAndreas Gohr //prepare license array() 117066fee30SAndreas Gohr global $license; 118066fee30SAndreas Gohr $license = array(); 119066fee30SAndreas Gohr 120066fee30SAndreas Gohr // load the license file(s) 121f8121585SChris Smith foreach (array('default','local') as $config_group) { 122f8121585SChris Smith if (empty($config_cascade['license'][$config_group])) continue; 123f8121585SChris Smith foreach ($config_cascade['license'][$config_group] as $config_file) { 124f8121585SChris Smith if(@file_exists($config_file)){ 125f8121585SChris Smith include($config_file); 126f8121585SChris Smith } 127f8121585SChris Smith } 128066fee30SAndreas Gohr } 129066fee30SAndreas Gohr 130ed7b5f09Sandi // define baseURL 1314b1a4e04SAndreas Gohr if(!defined('DOKU_REL')) define('DOKU_REL',getBaseURL(false)); 132ed7b5f09Sandi if(!defined('DOKU_URL')) define('DOKU_URL',getBaseURL(true)); 1334b1a4e04SAndreas Gohr if(!defined('DOKU_BASE')){ 1344b1a4e04SAndreas Gohr if($conf['canonical']){ 1354b1a4e04SAndreas Gohr define('DOKU_BASE',DOKU_URL); 1364b1a4e04SAndreas Gohr }else{ 1374b1a4e04SAndreas Gohr define('DOKU_BASE',DOKU_REL); 1384b1a4e04SAndreas Gohr } 1394b1a4e04SAndreas Gohr } 1404b1a4e04SAndreas Gohr 141b8595a66SAndreas Gohr // define whitespace 142b8595a66SAndreas Gohr if(!defined('DOKU_LF')) define ('DOKU_LF',"\n"); 143b8595a66SAndreas Gohr if(!defined('DOKU_TAB')) define ('DOKU_TAB',"\t"); 144ed7b5f09Sandi 145e71ce681SAndreas Gohr // define cookie and session id 1468b11f0a5SAndreas Gohr if (!defined('DOKU_COOKIE')) define('DOKU_COOKIE', 'DW'.md5(DOKU_REL)); 147e71ce681SAndreas Gohr 148ee20e7d1Sandi // define Plugin dir 149f62ea8a1Sandi if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 150ee20e7d1Sandi 151ed7b5f09Sandi // define main script 152ed7b5f09Sandi if(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php'); 153ed7b5f09Sandi 1546b13307fSandi // define Template baseURL 1556b13307fSandi if(!defined('DOKU_TPL')) define('DOKU_TPL', 156f62ea8a1Sandi DOKU_BASE.'lib/tpl/'.$conf['template'].'/'); 1576b13307fSandi 15878a6aeb1SAndreas Gohr // define real Template directory 15978a6aeb1SAndreas Gohr if(!defined('DOKU_TPLINC')) define('DOKU_TPLINC', 16078a6aeb1SAndreas Gohr DOKU_INC.'lib/tpl/'.$conf['template'].'/'); 16178a6aeb1SAndreas Gohr 162ed7b5f09Sandi // make session rewrites XHTML compliant 1633fc74836Sandi @ini_set('arg_separator.output', '&'); 164ed7b5f09Sandi 165d7e6bba9SAndreas Gohr // make sure global zlib does not interfere FS#1132 166d7e6bba9SAndreas Gohr @ini_set('zlib.output_compression', 'off'); 167d7e6bba9SAndreas Gohr 1686deb5405SAndreas Gohr // increase PCRE backtrack limit 1696deb5405SAndreas Gohr @ini_set('pcre.backtrack_limit', '20971520'); 1706deb5405SAndreas Gohr 17198bda4fdSAndreas Gohr // enable gzip compression if supported 17298bda4fdSAndreas Gohr $conf['gzip_output'] &= (strpos($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip') !== false); 1733138b5c7SAndreas Gohr if ($conf['gzip_output'] && 1743138b5c7SAndreas Gohr !defined('DOKU_DISABLE_GZIP_OUTPUT') && 17598bda4fdSAndreas Gohr function_exists('ob_gzhandler')) { 1763138b5c7SAndreas Gohr ob_start('ob_gzhandler'); 1773138b5c7SAndreas Gohr } 1783138b5c7SAndreas Gohr 179ed7b5f09Sandi // init session 1806534245aSAndreas Gohr if (!headers_sent() && !defined('NOSESSION')){ 181ed7b5f09Sandi session_name("DokuWiki"); 182f5c6743cSAndreas Gohr if (version_compare(PHP_VERSION, '5.2.0', '>')) { 183f5c6743cSAndreas Gohr session_set_cookie_params(0,DOKU_REL,'',($conf['securecookie'] && is_ssl()),true); 184f5c6743cSAndreas Gohr }else{ 185f5c6743cSAndreas Gohr session_set_cookie_params(0,DOKU_REL,'',($conf['securecookie'] && is_ssl())); 186f5c6743cSAndreas Gohr } 187bad31ae9SAndreas Gohr session_start(); 18814a122deSAndreas Gohr 18914a122deSAndreas Gohr // load left over messages 19014a122deSAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['msg'])){ 19114a122deSAndreas Gohr $MSG = $_SESSION[DOKU_COOKIE]['msg']; 19214a122deSAndreas Gohr unset($_SESSION[DOKU_COOKIE]['msg']); 19314a122deSAndreas Gohr } 194bad31ae9SAndreas Gohr } 195ed7b5f09Sandi 196ed7b5f09Sandi // kill magic quotes 197e55eb89cSAndreas Gohr if (get_magic_quotes_gpc() && !defined('MAGIC_QUOTES_STRIPPED')) { 198ed7b5f09Sandi if (!empty($_GET)) remove_magic_quotes($_GET); 199ed7b5f09Sandi if (!empty($_POST)) remove_magic_quotes($_POST); 200ed7b5f09Sandi if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE); 201ed7b5f09Sandi if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST); 2023fc74836Sandi @ini_set('magic_quotes_gpc', 0); 203e55eb89cSAndreas Gohr define('MAGIC_QUOTES_STRIPPED',1); 204ed7b5f09Sandi } 2053fc74836Sandi @set_magic_quotes_runtime(0); 2063fc74836Sandi @ini_set('magic_quotes_sybase',0); 207ed7b5f09Sandi 208a1637ffdSAndreas Gohr // don't let cookies ever interfere with request vars 209a1637ffdSAndreas Gohr $_REQUEST = array_merge($_GET,$_POST); 210a1637ffdSAndreas Gohr 2113dea4ebcSAndreas Gohr // we don't want a purge URL to be digged 2123dea4ebcSAndreas Gohr if($_REQUEST['purge'] && $_SERVER['HTTP_REFERER']) unset($_REQUEST['purge']); 2133dea4ebcSAndreas Gohr 214ed7b5f09Sandi // disable gzip if not available 2158a447e5cSAndreas Gohr if($conf['compression'] == 'bz2' && !function_exists('bzopen')){ 216fe893490SAndreas Gohr $conf['compression'] = 'gz'; 217501252a5SAndreas Gohr } 218fe893490SAndreas Gohr if($conf['compression'] == 'gz' && !function_exists('gzopen')){ 219501252a5SAndreas Gohr $conf['compression'] = 0; 220ed7b5f09Sandi } 221ed7b5f09Sandi 222e656dcd4SAndreas Gohr // fix dateformat for upgraders 223e656dcd4SAndreas Gohr if(strpos($conf['dformat'],'%') === false){ 224e656dcd4SAndreas Gohr $conf['dformat'] = '%Y/%m/%d %H:%M'; 225e656dcd4SAndreas Gohr } 226e656dcd4SAndreas Gohr 2271ca31cfeSAndreas Gohr // precalculate file creation modes 2281ca31cfeSAndreas Gohr init_creationmodes(); 229ed7b5f09Sandi 2303dc3a5f1Sandi // make real paths and check them 23198407a7aSandi init_paths(); 2327367b368SAndreas Gohr init_files(); 233ed7b5f09Sandi 2348c4f28e8Sjan // automatic upgrade to script versions of certain files 235e7cb32dcSAndreas Gohr scriptify(DOKU_CONF.'users.auth'); 236e7cb32dcSAndreas Gohr scriptify(DOKU_CONF.'acl.auth'); 237f62ea8a1Sandi 238f62ea8a1Sandi 239f62ea8a1Sandi/** 24098407a7aSandi * Checks paths from config file 24198407a7aSandi */ 24298407a7aSandifunction init_paths(){ 24398407a7aSandi global $conf; 24498407a7aSandi 24598407a7aSandi $paths = array('datadir' => 'pages', 24698407a7aSandi 'olddir' => 'attic', 24798407a7aSandi 'mediadir' => 'media', 24898407a7aSandi 'metadir' => 'meta', 24998407a7aSandi 'cachedir' => 'cache', 250579b0f7eSTNHarris 'indexdir' => 'index', 251de33a58fSMichael Klier 'lockdir' => 'locks', 252de33a58fSMichael Klier 'tmpdir' => 'tmp'); 25398407a7aSandi 25498407a7aSandi foreach($paths as $c => $p){ 255bb4866bdSchris if(empty($conf[$c])) $conf[$c] = $conf['savedir'].'/'.$p; 25698407a7aSandi $conf[$c] = init_path($conf[$c]); 2571983acc2SGuy Brand if(empty($conf[$c])) nice_die("The $c ('$p') does not exist, isn't accessible or writable. 25869dc3177SAndreas Gohr You should check your config and permission settings. 25969dc3177SAndreas Gohr Or maybe you want to <a href=\"install.php\">run the 26069dc3177SAndreas Gohr installer</a>?"); 26198407a7aSandi } 26271726d78SBen Coburn 26371726d78SBen Coburn // path to old changelog only needed for upgrading 26471726d78SBen Coburn $conf['changelog_old'] = init_path((isset($conf['changelog']))?($conf['changelog']):($conf['savedir'].'/changes.log')); 26571726d78SBen Coburn if ($conf['changelog_old']=='') { unset($conf['changelog_old']); } 26671726d78SBen Coburn // hardcoded changelog because it is now a cache that lives in meta 26771726d78SBen Coburn $conf['changelog'] = $conf['metadir'].'/_dokuwiki.changes'; 26899c8d7f2Smichael $conf['media_changelog'] = $conf['metadir'].'/_media.changes'; 26998407a7aSandi} 27098407a7aSandi 27198407a7aSandi/** 2720d8850c4SAndreas Gohr * Checks the existance of certain files and creates them if missing. 2737367b368SAndreas Gohr */ 2747367b368SAndreas Gohrfunction init_files(){ 2757367b368SAndreas Gohr global $conf; 2760d8850c4SAndreas Gohr 277579b0f7eSTNHarris $files = array( $conf['indexdir'].'/page.idx'); 2787367b368SAndreas Gohr 2797367b368SAndreas Gohr foreach($files as $file){ 2807367b368SAndreas Gohr if(!@file_exists($file)){ 2810d8850c4SAndreas Gohr $fh = @fopen($file,'a'); 2820d8850c4SAndreas Gohr if($fh){ 2837367b368SAndreas Gohr fclose($fh); 2841ca31cfeSAndreas Gohr if($conf['fperm']) chmod($file, $conf['fperm']); 2850d8850c4SAndreas Gohr }else{ 2863816dcbcSAndreas Gohr nice_die("$file is not writable. Check your permissions settings!"); 2870d8850c4SAndreas Gohr } 2887367b368SAndreas Gohr } 2897367b368SAndreas Gohr } 2907367b368SAndreas Gohr} 2917367b368SAndreas Gohr 2927367b368SAndreas Gohr/** 2930d8850c4SAndreas Gohr * Returns absolute path 294f62ea8a1Sandi * 2950d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC. 2960d8850c4SAndreas Gohr * Check for accessability on directories as well. 2970d8850c4SAndreas Gohr * 2980d8850c4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 299f62ea8a1Sandi */ 300f62ea8a1Sandifunction init_path($path){ 3010d8850c4SAndreas Gohr // check existance 30200976812SAndreas Gohr $p = fullpath($path); 3030d8850c4SAndreas Gohr if(!@file_exists($p)){ 30400976812SAndreas Gohr $p = fullpath(DOKU_INC.$path); 3050d8850c4SAndreas Gohr if(!@file_exists($p)){ 3068fc4e739Sandi return ''; 307f62ea8a1Sandi } 3080d8850c4SAndreas Gohr } 3090d8850c4SAndreas Gohr 3100d8850c4SAndreas Gohr // check writability 3110d8850c4SAndreas Gohr if(!@is_writable($p)){ 3120d8850c4SAndreas Gohr return ''; 3130d8850c4SAndreas Gohr } 3140d8850c4SAndreas Gohr 3150d8850c4SAndreas Gohr // check accessability (execute bit) for directories 3160d8850c4SAndreas Gohr if(@is_dir($p) && !@file_exists("$p/.")){ 3170d8850c4SAndreas Gohr return ''; 3180d8850c4SAndreas Gohr } 3190d8850c4SAndreas Gohr 3200d8850c4SAndreas Gohr return $p; 3210d8850c4SAndreas Gohr} 3228c4f28e8Sjan 323ed7b5f09Sandi/** 3241ca31cfeSAndreas Gohr * Sets the internal config values fperm and dperm which, when set, 3251ca31cfeSAndreas Gohr * will be used to change the permission of a newly created dir or 3261ca31cfeSAndreas Gohr * file with chmod. Considers the influence of the system's umask 3271ca31cfeSAndreas Gohr * setting the values only if needed. 3281ca31cfeSAndreas Gohr */ 3291ca31cfeSAndreas Gohrfunction init_creationmodes(){ 3301ca31cfeSAndreas Gohr global $conf; 3311ca31cfeSAndreas Gohr 3321ca31cfeSAndreas Gohr // Legacy support for old umask/dmask scheme 3331ca31cfeSAndreas Gohr unset($conf['dmask']); 3341ca31cfeSAndreas Gohr unset($conf['fmask']); 3351ca31cfeSAndreas Gohr unset($conf['umask']); 3361ca31cfeSAndreas Gohr unset($conf['fperm']); 3371ca31cfeSAndreas Gohr unset($conf['dperm']); 3381ca31cfeSAndreas Gohr 3399f3cdec3SAndreas Gohr // get system umask, fallback to 0 if none available 3409f3cdec3SAndreas Gohr $umask = @umask(); 3419f3cdec3SAndreas Gohr if(!$umask) $umask = 0000; 3421ca31cfeSAndreas Gohr 3431ca31cfeSAndreas Gohr // check what is set automatically by the system on file creation 3441ca31cfeSAndreas Gohr // and set the fperm param if it's not what we want 3451ca31cfeSAndreas Gohr $auto_fmode = 0666 & ~$umask; 3461ca31cfeSAndreas Gohr if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode']; 3471ca31cfeSAndreas Gohr 3481ca31cfeSAndreas Gohr // check what is set automatically by the system on file creation 3491ca31cfeSAndreas Gohr // and set the dperm param if it's not what we want 3501ca31cfeSAndreas Gohr $auto_dmode = $conf['dmode'] & ~$umask; 3511ca31cfeSAndreas Gohr if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode']; 3521ca31cfeSAndreas Gohr} 3531ca31cfeSAndreas Gohr 3541ca31cfeSAndreas Gohr/** 355ed7b5f09Sandi * remove magic quotes recursivly 356ed7b5f09Sandi * 357ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org> 358ed7b5f09Sandi */ 359ed7b5f09Sandifunction remove_magic_quotes(&$array) { 360ed7b5f09Sandi foreach (array_keys($array) as $key) { 36181c54349SAndreas Gohr // handle magic quotes in keynames (breaks order) 36281c54349SAndreas Gohr $sk = stripslashes($key); 36381c54349SAndreas Gohr if($sk != $key){ 36481c54349SAndreas Gohr $array[$sk] = $array[$key]; 36581c54349SAndreas Gohr unset($array[$key]); 36681c54349SAndreas Gohr $key = $sk; 36781c54349SAndreas Gohr } 36881c54349SAndreas Gohr 36981c54349SAndreas Gohr // do recursion if needed 370ed7b5f09Sandi if (is_array($array[$key])) { 371ed7b5f09Sandi remove_magic_quotes($array[$key]); 372ed7b5f09Sandi }else { 373ed7b5f09Sandi $array[$key] = stripslashes($array[$key]); 374ed7b5f09Sandi } 375ed7b5f09Sandi } 376ed7b5f09Sandi} 377ed7b5f09Sandi 378ed7b5f09Sandi/** 379ed7b5f09Sandi * Returns the full absolute URL to the directory where 380ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash) 381ed7b5f09Sandi * 382ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org> 383ed7b5f09Sandi */ 3844b1a4e04SAndreas Gohrfunction getBaseURL($abs=null){ 385ed7b5f09Sandi global $conf; 386ed7b5f09Sandi //if canonical url enabled always return absolute 3874b1a4e04SAndreas Gohr if(is_null($abs)) $abs = $conf['canonical']; 388ed7b5f09Sandi 38992b83b77Sandi if($conf['basedir']){ 39046c73e01SChris Smith $dir = $conf['basedir']; 39189aa05dbSAndreas Gohr }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){ 39246c73e01SChris Smith $dir = dirname($_SERVER['SCRIPT_NAME']); 39389aa05dbSAndreas Gohr }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){ 39446c73e01SChris Smith $dir = dirname($_SERVER['PHP_SELF']); 395093ec9e4Sandi }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){ 396093ec9e4Sandi $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','', 397093ec9e4Sandi $_SERVER['SCRIPT_FILENAME']); 39846c73e01SChris Smith $dir = dirname('/'.$dir); 39992b83b77Sandi }else{ 40046c73e01SChris Smith $dir = '.'; //probably wrong 40192b83b77Sandi } 402ed7b5f09Sandi 40346c73e01SChris Smith $dir = str_replace('\\','/',$dir); // bugfix for weird WIN behaviour 40446c73e01SChris Smith $dir = preg_replace('#//+#','/',"/$dir/"); // ensure leading and trailing slashes 405ed7b5f09Sandi 406f62ea8a1Sandi //handle script in lib/exe dir 407f62ea8a1Sandi $dir = preg_replace('!lib/exe/$!','',$dir); 408f62ea8a1Sandi 409488d5fa0SMichael Klier chi@chimeric.de //handle script in lib/plugins dir 410488d5fa0SMichael Klier chi@chimeric.de $dir = preg_replace('!lib/plugins/.*$!','',$dir); 411488d5fa0SMichael Klier chi@chimeric.de 412ed7b5f09Sandi //finish here for relative URLs 413ed7b5f09Sandi if(!$abs) return $dir; 414ed7b5f09Sandi 41546c73e01SChris Smith //use config option if available, trim any slash from end of baseurl to avoid multiple consecutive slashes in the path 41646c73e01SChris Smith if($conf['baseurl']) return rtrim($conf['baseurl'],'/').$dir; 417ef7b3ecdSAndreas Gohr 418e82e3526SAndreas Gohr //split hostheader into host and port 419e82e3526SAndreas Gohr list($host,$port) = explode(':',$_SERVER['HTTP_HOST']); 420e82e3526SAndreas Gohr if(!$port) $port = $_SERVER['SERVER_PORT']; 421e82e3526SAndreas Gohr if(!$port) $port = 80; 422ed7b5f09Sandi 423f5c6743cSAndreas Gohr if(!is_ssl()){ 424ed7b5f09Sandi $proto = 'http://'; 425e82e3526SAndreas Gohr if ($port == '80') { 426ed7b5f09Sandi $port=''; 427ed7b5f09Sandi } 428ed7b5f09Sandi }else{ 429ed7b5f09Sandi $proto = 'https://'; 430e82e3526SAndreas Gohr if ($port == '443') { 431ed7b5f09Sandi $port=''; 432ed7b5f09Sandi } 433ed7b5f09Sandi } 434ed7b5f09Sandi 435e82e3526SAndreas Gohr if($port) $port = ':'.$port; 436e82e3526SAndreas Gohr 437ed7b5f09Sandi return $proto.$host.$port.$dir; 438ed7b5f09Sandi} 439ed7b5f09Sandi 440b000c6d4Sandi/** 441f5c6743cSAndreas Gohr * Check if accessed via HTTPS 442f5c6743cSAndreas Gohr * 443f5c6743cSAndreas Gohr * Apache leaves ,$_SERVER['HTTPS'] empty when not available, IIS sets it to 'off'. 444f5c6743cSAndreas Gohr * 'false' and 'disabled' are just guessing 445f5c6743cSAndreas Gohr * 446f5c6743cSAndreas Gohr * @returns bool true when SSL is active 447f5c6743cSAndreas Gohr */ 448f5c6743cSAndreas Gohrfunction is_ssl(){ 449f5c6743cSAndreas Gohr if (preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){ 450f5c6743cSAndreas Gohr return false; 451f5c6743cSAndreas Gohr }else{ 452f5c6743cSAndreas Gohr return true; 453f5c6743cSAndreas Gohr } 454f5c6743cSAndreas Gohr} 455f5c6743cSAndreas Gohr 456f5c6743cSAndreas Gohr/** 457b000c6d4Sandi * Append a PHP extension to a given file and adds an exit call 458b000c6d4Sandi * 459b000c6d4Sandi * This is used to migrate some old configfiles. An added PHP extension 460b000c6d4Sandi * ensures the contents are not shown to webusers even if .htaccess files 461b000c6d4Sandi * do not work 462b000c6d4Sandi * 463b000c6d4Sandi * @author Jan Decaluwe <jan@jandecaluwe.com> 464b000c6d4Sandi */ 4658c4f28e8Sjanfunction scriptify($file) { 4668c4f28e8Sjan // checks 4678c4f28e8Sjan if (!is_readable($file)) { 4688c4f28e8Sjan return; 4698c4f28e8Sjan } 4708c4f28e8Sjan $fn = $file.'.php'; 4718c4f28e8Sjan if (@file_exists($fn)) { 4728c4f28e8Sjan return; 4738c4f28e8Sjan } 4748c4f28e8Sjan $fh = fopen($fn, 'w'); 4758c4f28e8Sjan if (!$fh) { 4763816dcbcSAndreas Gohr nice_die($fn.' is not writable. Check your permission settings!'); 4778c4f28e8Sjan } 4788c4f28e8Sjan // write php exit hack first 4798c4f28e8Sjan fwrite($fh, "# $fn\n"); 4808c4f28e8Sjan fwrite($fh, '# <?php exit()?>'."\n"); 4818c4f28e8Sjan fwrite($fh, "# Don't modify the lines above\n"); 4828c4f28e8Sjan fwrite($fh, "#\n"); 4838c4f28e8Sjan // copy existing lines 4848c4f28e8Sjan $lines = file($file); 4858c4f28e8Sjan foreach ($lines as $line){ 4868c4f28e8Sjan fwrite($fh, $line); 4878c4f28e8Sjan } 4883ba793e2Sandi fclose($fh); 489b000c6d4Sandi //try to rename the old file 4903aee4c27SAndreas Gohr io_rename($file,"$file.old"); 4918c4f28e8Sjan} 4928c4f28e8Sjan 4933816dcbcSAndreas Gohr/** 4943816dcbcSAndreas Gohr * print a nice message even if no styles are loaded yet. 4953816dcbcSAndreas Gohr */ 4963816dcbcSAndreas Gohrfunction nice_die($msg){ 4973816dcbcSAndreas Gohr echo<<<EOT 4983816dcbcSAndreas Gohr <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 4993816dcbcSAndreas Gohr "http://www.w3.org/TR/html4/loose.dtd"> 5003816dcbcSAndreas Gohr <html> 5013816dcbcSAndreas Gohr <head><title>DokuWiki Setup Error</title></head> 5023816dcbcSAndreas Gohr <body style="font-family: Arial, sans-serif"> 5033816dcbcSAndreas Gohr <div style="width:60%; margin: auto; background-color: #fcc; 5043816dcbcSAndreas Gohr border: 1px solid #faa; padding: 0.5em 1em;"> 5053816dcbcSAndreas Gohr <h1 style="font-size: 120%">DokuWiki Setup Error</h1> 5063816dcbcSAndreas Gohr <p>$msg</p> 5073816dcbcSAndreas Gohr </div> 5083816dcbcSAndreas Gohr </body> 5093816dcbcSAndreas Gohr </html> 5103816dcbcSAndreas GohrEOT; 5113816dcbcSAndreas Gohr exit; 5123816dcbcSAndreas Gohr} 5133816dcbcSAndreas Gohr 514ed7b5f09Sandi 51500976812SAndreas Gohr/** 51600976812SAndreas Gohr * A realpath() replacement 51700976812SAndreas Gohr * 51800976812SAndreas Gohr * This function behaves similar to PHP's realpath() but does not resolve 51900976812SAndreas Gohr * symlinks or accesses upper directories 52000976812SAndreas Gohr * 5214761d30cSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 52200976812SAndreas Gohr * @author <richpageau at yahoo dot co dot uk> 52300976812SAndreas Gohr * @link http://de3.php.net/manual/en/function.realpath.php#75992 52400976812SAndreas Gohr */ 525b328697dSAndreas Gohrfunction fullpath($path,$exists=false){ 5264761d30cSAndreas Gohr static $run = 0; 5274761d30cSAndreas Gohr $root = ''; 528f0a201c5SChris Smith $iswin = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' || @$GLOBALS['DOKU_UNITTEST_ASSUME_WINDOWS']); 52900976812SAndreas Gohr 5304761d30cSAndreas Gohr // find the (indestructable) root of the path - keeps windows stuff intact 5314761d30cSAndreas Gohr if($path{0} == '/'){ 5324761d30cSAndreas Gohr $root = '/'; 5334761d30cSAndreas Gohr }elseif($iswin){ 5344761d30cSAndreas Gohr // match drive letter and UNC paths 5354761d30cSAndreas Gohr if(preg_match('!^([a-zA-z]:)(.*)!',$path,$match)){ 536*b9c4302bSAndreas Gohr $root = $match[1].'/'; 5374761d30cSAndreas Gohr $path = $match[2]; 5384761d30cSAndreas Gohr }else if(preg_match('!^(\\\\\\\\[^\\\\/]+\\\\[^\\\\/]+[\\\\/])(.*)!',$path,$match)){ 5394761d30cSAndreas Gohr $root = $match[1]; 5404761d30cSAndreas Gohr $path = $match[2]; 54100976812SAndreas Gohr } 5424761d30cSAndreas Gohr } 5434761d30cSAndreas Gohr $path = str_replace('\\','/',$path); 5444761d30cSAndreas Gohr 5454761d30cSAndreas Gohr // if the given path wasn't absolute already, prepend the script path and retry 5464761d30cSAndreas Gohr if(!$root){ 5474761d30cSAndreas Gohr $base = dirname($_SERVER['SCRIPT_FILENAME']); 5484761d30cSAndreas Gohr $path = $base.'/'.$path; 5494761d30cSAndreas Gohr if($run == 0){ // avoid endless recursion when base isn't absolute for some reason 5504761d30cSAndreas Gohr $run++; 551b328697dSAndreas Gohr return fullpath($path,$exists); 5524761d30cSAndreas Gohr } 5534761d30cSAndreas Gohr } 5544761d30cSAndreas Gohr $run = 0; 55500976812SAndreas Gohr 55600976812SAndreas Gohr // canonicalize 55700976812SAndreas Gohr $path=explode('/', $path); 55800976812SAndreas Gohr $newpath=array(); 559ef38bfe8SAndreas Gohr foreach($path as $p) { 560ef38bfe8SAndreas Gohr if ($p === '' || $p === '.') continue; 561ef38bfe8SAndreas Gohr if ($p==='..') { 56200976812SAndreas Gohr array_pop($newpath); 56300976812SAndreas Gohr continue; 56400976812SAndreas Gohr } 565ef38bfe8SAndreas Gohr array_push($newpath, $p); 56600976812SAndreas Gohr } 5674761d30cSAndreas Gohr $finalpath = $root.implode('/', $newpath); 56800976812SAndreas Gohr 569b328697dSAndreas Gohr // check for existance when needed (except when unit testing) 570b328697dSAndreas Gohr if($exists && !defined('DOKU_UNITTEST') && !@file_exists($finalpath)) { 5714761d30cSAndreas Gohr return false; 57200976812SAndreas Gohr } 5734761d30cSAndreas Gohr return $finalpath; 57400976812SAndreas Gohr} 57500976812SAndreas Gohr 57600976812SAndreas Gohr 57700976812SAndreas Gohr 578340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 : 579