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 1448beefecSAndreas Gohr @include(fullpath(dirname(__FILE__)).'/preload.php'); 1548beefecSAndreas Gohr 16ed7b5f09Sandi // define the include path 1700976812SAndreas Gohr if(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../').'/'); 18ad15db82Sandi 19e7cb32dcSAndreas Gohr // define config path (packagers may want to change this to /etc/dokuwiki/) 20b7551a6dSEsther Brunner if(!defined('DOKU_CONF')) define('DOKU_CONF',DOKU_INC.'conf/'); 21e7cb32dcSAndreas Gohr 22bad905f1SBen Coburn // check for error reporting override or set error reporting to sane values 23d8186216SBen Coburn if (!defined('DOKU_E_LEVEL') && @file_exists(DOKU_CONF.'report_e_all')) { 24bad905f1SBen Coburn define('DOKU_E_LEVEL', E_ALL); 25bad905f1SBen Coburn } 26fc80ed59SAndreas Gohr if (!defined('DOKU_E_LEVEL')) { 27fc80ed59SAndreas Gohr if(defined('E_DEPRECATED')){ // since php 5.3 28fc80ed59SAndreas Gohr error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED); 29fc80ed59SAndreas Gohr }else{ 30fc80ed59SAndreas Gohr error_reporting(E_ALL ^ E_NOTICE); 31fc80ed59SAndreas Gohr } 32fc80ed59SAndreas Gohr } else { 33fc80ed59SAndreas Gohr error_reporting(DOKU_E_LEVEL); 34fc80ed59SAndreas Gohr } 35c53ea5f2Sandi 3650602150SBen Coburn // init memory caches 37bc3e97beSAndreas Gohr global $cache_revinfo; $cache_revinfo = array(); 38bc3e97beSAndreas Gohr global $cache_wikifn; $cache_wikifn = array(); 39a424cd8eSchris global $cache_cleanid; $cache_cleanid = array(); 40a424cd8eSchris global $cache_authname; $cache_authname = array(); 410a7e3bceSchris global $cache_metadata; $cache_metadata = array(); 4250602150SBen Coburn 43*cb043f52SChris Smith //set the configuration cascade - but only if its not already been set in preload.php 44*cb043f52SChris Smith global $config_cascade; 45*cb043f52SChris Smith if (empty($config_cascade)) { 46*cb043f52SChris Smith $config_cascade = array( 47*cb043f52SChris Smith 'main' => array( 48*cb043f52SChris Smith 'default' => array(DOKU_CONF.'dokuwiki.php'), 49*cb043f52SChris Smith 'local' => array(DOKU_CONF.'local.php'), 50*cb043f52SChris Smith 'protected' => array(DOKU_CONF.'local.protected.php'), 51*cb043f52SChris Smith ), 52*cb043f52SChris Smith 'acronyms' => array( 53*cb043f52SChris Smith 'default' => array(DOKU_CONF.'acronyms.php'), 54*cb043f52SChris Smith 'local' => array(DOKU_CONF.'acronyms.local.php'), 55*cb043f52SChris Smith ), 56*cb043f52SChris Smith 'entities' => array( 57*cb043f52SChris Smith 'default' => array(DOKU_CONF.'entities.php'), 58*cb043f52SChris Smith 'local' => array(DOKU_CONF.'entities.local.php'), 59*cb043f52SChris Smith ), 60*cb043f52SChris Smith 'interwiki' => array( 61*cb043f52SChris Smith 'default' => array(DOKU_CONF.'interwiki.php'), 62*cb043f52SChris Smith 'local' => array(DOKU_CONF.'interwiki.local.php'), 63*cb043f52SChris Smith ), 64*cb043f52SChris Smith 'mime' => array( 65*cb043f52SChris Smith 'default' => array(DOKU_CONF.'mime.php'), 66*cb043f52SChris Smith 'local' => array(DOKU_CONF.'mime.local.php'), 67*cb043f52SChris Smith ), 68*cb043f52SChris Smith 'scheme' => array( 69*cb043f52SChris Smith 'default' => array(DOKU_CONF.'scheme.php'), 70*cb043f52SChris Smith 'local' => array(DOKU_CONF.'scheme.local.php'), 71*cb043f52SChris Smith ), 72*cb043f52SChris Smith 'smileys' => array( 73*cb043f52SChris Smith 'default' => array(DOKU_CONF.'smileys.php'), 74*cb043f52SChris Smith 'local' => array(DOKU_CONF.'smileys.local.php'), 75*cb043f52SChris Smith ), 76*cb043f52SChris Smith 'wordblock' => array( 77*cb043f52SChris Smith 'default' => array(DOKU_CONF.'wordblock.php'), 78*cb043f52SChris Smith 'local' => array(DOKU_CONF.'wordblock.local.php'), 79*cb043f52SChris Smith ), 80*cb043f52SChris Smith ); 81*cb043f52SChris Smith } 82*cb043f52SChris Smith 834724a577Sandi //prepare config array() 84ee20e7d1Sandi global $conf; 854724a577Sandi $conf = array(); 864724a577Sandi 87*cb043f52SChris Smith // load the global config file(s) 88*cb043f52SChris Smith foreach ($config_cascade['main'] as $config_group) { 89*cb043f52SChris Smith foreach ($config_group as $config_file) { 90*cb043f52SChris Smith @include($config_file); 91*cb043f52SChris Smith } 920a6ead41SAndreas Gohr } 93ad15db82Sandi 94ad15db82Sandi //prepare language array 95ee20e7d1Sandi global $lang; 96ad15db82Sandi $lang = array(); 97ed7b5f09Sandi 9816521111Sandi //load the language files 99bc3b6aecSandi require_once(DOKU_INC.'inc/lang/en/lang.php'); 100f949a01cSAndreas Gohr if ( $conf['lang'] && $conf['lang'] != 'en' ) { 101bc3b6aecSandi require_once(DOKU_INC.'inc/lang/'.$conf['lang'].'/lang.php'); 102fc1c55b1Shfuecks } 10316521111Sandi 104066fee30SAndreas Gohr //prepare license array() 105066fee30SAndreas Gohr global $license; 106066fee30SAndreas Gohr $license = array(); 107066fee30SAndreas Gohr 108066fee30SAndreas Gohr // load the license file(s) 109066fee30SAndreas Gohr require_once(DOKU_CONF.'license.php'); 110066fee30SAndreas Gohr if(@file_exists(DOKU_CONF.'license.php')){ 111066fee30SAndreas Gohr require_once(DOKU_CONF.'license.php'); 112066fee30SAndreas Gohr } 113066fee30SAndreas Gohr 114ed7b5f09Sandi // define baseURL 1154b1a4e04SAndreas Gohr if(!defined('DOKU_REL')) define('DOKU_REL',getBaseURL(false)); 116ed7b5f09Sandi if(!defined('DOKU_URL')) define('DOKU_URL',getBaseURL(true)); 1174b1a4e04SAndreas Gohr if(!defined('DOKU_BASE')){ 1184b1a4e04SAndreas Gohr if($conf['canonical']){ 1194b1a4e04SAndreas Gohr define('DOKU_BASE',DOKU_URL); 1204b1a4e04SAndreas Gohr }else{ 1214b1a4e04SAndreas Gohr define('DOKU_BASE',DOKU_REL); 1224b1a4e04SAndreas Gohr } 1234b1a4e04SAndreas Gohr } 1244b1a4e04SAndreas Gohr 125b8595a66SAndreas Gohr // define whitespace 126b8595a66SAndreas Gohr if(!defined('DOKU_LF')) define ('DOKU_LF',"\n"); 127b8595a66SAndreas Gohr if(!defined('DOKU_TAB')) define ('DOKU_TAB',"\t"); 128ed7b5f09Sandi 129e71ce681SAndreas Gohr // define cookie and session id 1308b11f0a5SAndreas Gohr if (!defined('DOKU_COOKIE')) define('DOKU_COOKIE', 'DW'.md5(DOKU_REL)); 131e71ce681SAndreas Gohr 132ee20e7d1Sandi // define Plugin dir 133f62ea8a1Sandi if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 134ee20e7d1Sandi 135ed7b5f09Sandi // define main script 136ed7b5f09Sandi if(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php'); 137ed7b5f09Sandi 1386b13307fSandi // define Template baseURL 1396b13307fSandi if(!defined('DOKU_TPL')) define('DOKU_TPL', 140f62ea8a1Sandi DOKU_BASE.'lib/tpl/'.$conf['template'].'/'); 1416b13307fSandi 14278a6aeb1SAndreas Gohr // define real Template directory 14378a6aeb1SAndreas Gohr if(!defined('DOKU_TPLINC')) define('DOKU_TPLINC', 14478a6aeb1SAndreas Gohr DOKU_INC.'lib/tpl/'.$conf['template'].'/'); 14578a6aeb1SAndreas Gohr 146ed7b5f09Sandi // make session rewrites XHTML compliant 1473fc74836Sandi @ini_set('arg_separator.output', '&'); 148ed7b5f09Sandi 149d7e6bba9SAndreas Gohr // make sure global zlib does not interfere FS#1132 150d7e6bba9SAndreas Gohr @ini_set('zlib.output_compression', 'off'); 151d7e6bba9SAndreas Gohr 1526deb5405SAndreas Gohr // increase PCRE backtrack limit 1536deb5405SAndreas Gohr @ini_set('pcre.backtrack_limit', '20971520'); 1546deb5405SAndreas Gohr 1553138b5c7SAndreas Gohr // enable gzip compression 1563138b5c7SAndreas Gohr if ($conf['gzip_output'] && 1573138b5c7SAndreas Gohr !defined('DOKU_DISABLE_GZIP_OUTPUT') && 1583138b5c7SAndreas Gohr function_exists('ob_gzhandler') && 1593138b5c7SAndreas Gohr preg_match('/gzip|deflate/', $_SERVER['HTTP_ACCEPT_ENCODING'])) { 1603138b5c7SAndreas Gohr ob_start('ob_gzhandler'); 1613138b5c7SAndreas Gohr } 1623138b5c7SAndreas Gohr 163ed7b5f09Sandi // init session 1646534245aSAndreas Gohr if (!headers_sent() && !defined('NOSESSION')){ 165ed7b5f09Sandi session_name("DokuWiki"); 166f5c6743cSAndreas Gohr if (version_compare(PHP_VERSION, '5.2.0', '>')) { 167f5c6743cSAndreas Gohr session_set_cookie_params(0,DOKU_REL,'',($conf['securecookie'] && is_ssl()),true); 168f5c6743cSAndreas Gohr }else{ 169f5c6743cSAndreas Gohr session_set_cookie_params(0,DOKU_REL,'',($conf['securecookie'] && is_ssl())); 170f5c6743cSAndreas Gohr } 171bad31ae9SAndreas Gohr session_start(); 17214a122deSAndreas Gohr 17314a122deSAndreas Gohr // load left over messages 17414a122deSAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['msg'])){ 17514a122deSAndreas Gohr $MSG = $_SESSION[DOKU_COOKIE]['msg']; 17614a122deSAndreas Gohr unset($_SESSION[DOKU_COOKIE]['msg']); 17714a122deSAndreas Gohr } 178bad31ae9SAndreas Gohr } 179ed7b5f09Sandi 180ed7b5f09Sandi // kill magic quotes 181e55eb89cSAndreas Gohr if (get_magic_quotes_gpc() && !defined('MAGIC_QUOTES_STRIPPED')) { 182ed7b5f09Sandi if (!empty($_GET)) remove_magic_quotes($_GET); 183ed7b5f09Sandi if (!empty($_POST)) remove_magic_quotes($_POST); 184ed7b5f09Sandi if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE); 185ed7b5f09Sandi if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST); 1863fc74836Sandi @ini_set('magic_quotes_gpc', 0); 187e55eb89cSAndreas Gohr define('MAGIC_QUOTES_STRIPPED',1); 188ed7b5f09Sandi } 1893fc74836Sandi @set_magic_quotes_runtime(0); 1903fc74836Sandi @ini_set('magic_quotes_sybase',0); 191ed7b5f09Sandi 192a1637ffdSAndreas Gohr // don't let cookies ever interfere with request vars 193a1637ffdSAndreas Gohr $_REQUEST = array_merge($_GET,$_POST); 194a1637ffdSAndreas Gohr 1953dea4ebcSAndreas Gohr // we don't want a purge URL to be digged 1963dea4ebcSAndreas Gohr if($_REQUEST['purge'] && $_SERVER['HTTP_REFERER']) unset($_REQUEST['purge']); 1973dea4ebcSAndreas Gohr 198ed7b5f09Sandi // disable gzip if not available 1998a447e5cSAndreas Gohr if($conf['compression'] == 'bz2' && !function_exists('bzopen')){ 200fe893490SAndreas Gohr $conf['compression'] = 'gz'; 201501252a5SAndreas Gohr } 202fe893490SAndreas Gohr if($conf['compression'] == 'gz' && !function_exists('gzopen')){ 203501252a5SAndreas Gohr $conf['compression'] = 0; 204ed7b5f09Sandi } 205ed7b5f09Sandi 206e656dcd4SAndreas Gohr // fix dateformat for upgraders 207e656dcd4SAndreas Gohr if(strpos($conf['dformat'],'%') === false){ 208e656dcd4SAndreas Gohr $conf['dformat'] = '%Y/%m/%d %H:%M'; 209e656dcd4SAndreas Gohr } 210e656dcd4SAndreas Gohr 2111ca31cfeSAndreas Gohr // precalculate file creation modes 2121ca31cfeSAndreas Gohr init_creationmodes(); 213ed7b5f09Sandi 2143dc3a5f1Sandi // make real paths and check them 21598407a7aSandi init_paths(); 2167367b368SAndreas Gohr init_files(); 217ed7b5f09Sandi 2188c4f28e8Sjan // automatic upgrade to script versions of certain files 219e7cb32dcSAndreas Gohr scriptify(DOKU_CONF.'users.auth'); 220e7cb32dcSAndreas Gohr scriptify(DOKU_CONF.'acl.auth'); 221f62ea8a1Sandi 222f62ea8a1Sandi 223f62ea8a1Sandi/** 22498407a7aSandi * Checks paths from config file 22598407a7aSandi */ 22698407a7aSandifunction init_paths(){ 22798407a7aSandi global $conf; 22898407a7aSandi 22998407a7aSandi $paths = array('datadir' => 'pages', 23098407a7aSandi 'olddir' => 'attic', 23198407a7aSandi 'mediadir' => 'media', 23298407a7aSandi 'metadir' => 'meta', 23398407a7aSandi 'cachedir' => 'cache', 234579b0f7eSTNHarris 'indexdir' => 'index', 235de33a58fSMichael Klier 'lockdir' => 'locks', 236de33a58fSMichael Klier 'tmpdir' => 'tmp'); 23798407a7aSandi 23898407a7aSandi foreach($paths as $c => $p){ 239bb4866bdSchris if(empty($conf[$c])) $conf[$c] = $conf['savedir'].'/'.$p; 24098407a7aSandi $conf[$c] = init_path($conf[$c]); 2411983acc2SGuy Brand if(empty($conf[$c])) nice_die("The $c ('$p') does not exist, isn't accessible or writable. 24269dc3177SAndreas Gohr You should check your config and permission settings. 24369dc3177SAndreas Gohr Or maybe you want to <a href=\"install.php\">run the 24469dc3177SAndreas Gohr installer</a>?"); 24598407a7aSandi } 24671726d78SBen Coburn 24771726d78SBen Coburn // path to old changelog only needed for upgrading 24871726d78SBen Coburn $conf['changelog_old'] = init_path((isset($conf['changelog']))?($conf['changelog']):($conf['savedir'].'/changes.log')); 24971726d78SBen Coburn if ($conf['changelog_old']=='') { unset($conf['changelog_old']); } 25071726d78SBen Coburn // hardcoded changelog because it is now a cache that lives in meta 25171726d78SBen Coburn $conf['changelog'] = $conf['metadir'].'/_dokuwiki.changes'; 25299c8d7f2Smichael $conf['media_changelog'] = $conf['metadir'].'/_media.changes'; 25398407a7aSandi} 25498407a7aSandi 25598407a7aSandi/** 2560d8850c4SAndreas Gohr * Checks the existance of certain files and creates them if missing. 2577367b368SAndreas Gohr */ 2587367b368SAndreas Gohrfunction init_files(){ 2597367b368SAndreas Gohr global $conf; 2600d8850c4SAndreas Gohr 261579b0f7eSTNHarris $files = array( $conf['indexdir'].'/page.idx'); 2627367b368SAndreas Gohr 2637367b368SAndreas Gohr foreach($files as $file){ 2647367b368SAndreas Gohr if(!@file_exists($file)){ 2650d8850c4SAndreas Gohr $fh = @fopen($file,'a'); 2660d8850c4SAndreas Gohr if($fh){ 2677367b368SAndreas Gohr fclose($fh); 2681ca31cfeSAndreas Gohr if($conf['fperm']) chmod($file, $conf['fperm']); 2690d8850c4SAndreas Gohr }else{ 2703816dcbcSAndreas Gohr nice_die("$file is not writable. Check your permissions settings!"); 2710d8850c4SAndreas Gohr } 2727367b368SAndreas Gohr } 2737367b368SAndreas Gohr } 2747367b368SAndreas Gohr} 2757367b368SAndreas Gohr 2767367b368SAndreas Gohr/** 2770d8850c4SAndreas Gohr * Returns absolute path 278f62ea8a1Sandi * 2790d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC. 2800d8850c4SAndreas Gohr * Check for accessability on directories as well. 2810d8850c4SAndreas Gohr * 2820d8850c4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 283f62ea8a1Sandi */ 284f62ea8a1Sandifunction init_path($path){ 2850d8850c4SAndreas Gohr // check existance 28600976812SAndreas Gohr $p = fullpath($path); 2870d8850c4SAndreas Gohr if(!@file_exists($p)){ 28800976812SAndreas Gohr $p = fullpath(DOKU_INC.$path); 2890d8850c4SAndreas Gohr if(!@file_exists($p)){ 2908fc4e739Sandi return ''; 291f62ea8a1Sandi } 2920d8850c4SAndreas Gohr } 2930d8850c4SAndreas Gohr 2940d8850c4SAndreas Gohr // check writability 2950d8850c4SAndreas Gohr if(!@is_writable($p)){ 2960d8850c4SAndreas Gohr return ''; 2970d8850c4SAndreas Gohr } 2980d8850c4SAndreas Gohr 2990d8850c4SAndreas Gohr // check accessability (execute bit) for directories 3000d8850c4SAndreas Gohr if(@is_dir($p) && !@file_exists("$p/.")){ 3010d8850c4SAndreas Gohr return ''; 3020d8850c4SAndreas Gohr } 3030d8850c4SAndreas Gohr 3040d8850c4SAndreas Gohr return $p; 3050d8850c4SAndreas Gohr} 3068c4f28e8Sjan 307ed7b5f09Sandi/** 3081ca31cfeSAndreas Gohr * Sets the internal config values fperm and dperm which, when set, 3091ca31cfeSAndreas Gohr * will be used to change the permission of a newly created dir or 3101ca31cfeSAndreas Gohr * file with chmod. Considers the influence of the system's umask 3111ca31cfeSAndreas Gohr * setting the values only if needed. 3121ca31cfeSAndreas Gohr */ 3131ca31cfeSAndreas Gohrfunction init_creationmodes(){ 3141ca31cfeSAndreas Gohr global $conf; 3151ca31cfeSAndreas Gohr 3161ca31cfeSAndreas Gohr // Legacy support for old umask/dmask scheme 3171ca31cfeSAndreas Gohr unset($conf['dmask']); 3181ca31cfeSAndreas Gohr unset($conf['fmask']); 3191ca31cfeSAndreas Gohr unset($conf['umask']); 3201ca31cfeSAndreas Gohr unset($conf['fperm']); 3211ca31cfeSAndreas Gohr unset($conf['dperm']); 3221ca31cfeSAndreas Gohr 3239f3cdec3SAndreas Gohr // get system umask, fallback to 0 if none available 3249f3cdec3SAndreas Gohr $umask = @umask(); 3259f3cdec3SAndreas Gohr if(!$umask) $umask = 0000; 3261ca31cfeSAndreas Gohr 3271ca31cfeSAndreas Gohr // check what is set automatically by the system on file creation 3281ca31cfeSAndreas Gohr // and set the fperm param if it's not what we want 3291ca31cfeSAndreas Gohr $auto_fmode = 0666 & ~$umask; 3301ca31cfeSAndreas Gohr if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode']; 3311ca31cfeSAndreas Gohr 3321ca31cfeSAndreas Gohr // check what is set automatically by the system on file creation 3331ca31cfeSAndreas Gohr // and set the dperm param if it's not what we want 3341ca31cfeSAndreas Gohr $auto_dmode = $conf['dmode'] & ~$umask; 3351ca31cfeSAndreas Gohr if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode']; 3361ca31cfeSAndreas Gohr} 3371ca31cfeSAndreas Gohr 3381ca31cfeSAndreas Gohr/** 339ed7b5f09Sandi * remove magic quotes recursivly 340ed7b5f09Sandi * 341ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org> 342ed7b5f09Sandi */ 343ed7b5f09Sandifunction remove_magic_quotes(&$array) { 344ed7b5f09Sandi foreach (array_keys($array) as $key) { 34581c54349SAndreas Gohr // handle magic quotes in keynames (breaks order) 34681c54349SAndreas Gohr $sk = stripslashes($key); 34781c54349SAndreas Gohr if($sk != $key){ 34881c54349SAndreas Gohr $array[$sk] = $array[$key]; 34981c54349SAndreas Gohr unset($array[$key]); 35081c54349SAndreas Gohr $key = $sk; 35181c54349SAndreas Gohr } 35281c54349SAndreas Gohr 35381c54349SAndreas Gohr // do recursion if needed 354ed7b5f09Sandi if (is_array($array[$key])) { 355ed7b5f09Sandi remove_magic_quotes($array[$key]); 356ed7b5f09Sandi }else { 357ed7b5f09Sandi $array[$key] = stripslashes($array[$key]); 358ed7b5f09Sandi } 359ed7b5f09Sandi } 360ed7b5f09Sandi} 361ed7b5f09Sandi 362ed7b5f09Sandi/** 363ed7b5f09Sandi * Returns the full absolute URL to the directory where 364ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash) 365ed7b5f09Sandi * 366ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org> 367ed7b5f09Sandi */ 3684b1a4e04SAndreas Gohrfunction getBaseURL($abs=null){ 369ed7b5f09Sandi global $conf; 370ed7b5f09Sandi //if canonical url enabled always return absolute 3714b1a4e04SAndreas Gohr if(is_null($abs)) $abs = $conf['canonical']; 372ed7b5f09Sandi 37392b83b77Sandi if($conf['basedir']){ 37446c73e01SChris Smith $dir = $conf['basedir']; 37589aa05dbSAndreas Gohr }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){ 37646c73e01SChris Smith $dir = dirname($_SERVER['SCRIPT_NAME']); 37789aa05dbSAndreas Gohr }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){ 37846c73e01SChris Smith $dir = dirname($_SERVER['PHP_SELF']); 379093ec9e4Sandi }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){ 380093ec9e4Sandi $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','', 381093ec9e4Sandi $_SERVER['SCRIPT_FILENAME']); 38246c73e01SChris Smith $dir = dirname('/'.$dir); 38392b83b77Sandi }else{ 38446c73e01SChris Smith $dir = '.'; //probably wrong 38592b83b77Sandi } 386ed7b5f09Sandi 38746c73e01SChris Smith $dir = str_replace('\\','/',$dir); // bugfix for weird WIN behaviour 38846c73e01SChris Smith $dir = preg_replace('#//+#','/',"/$dir/"); // ensure leading and trailing slashes 389ed7b5f09Sandi 390f62ea8a1Sandi //handle script in lib/exe dir 391f62ea8a1Sandi $dir = preg_replace('!lib/exe/$!','',$dir); 392f62ea8a1Sandi 393488d5fa0SMichael Klier chi@chimeric.de //handle script in lib/plugins dir 394488d5fa0SMichael Klier chi@chimeric.de $dir = preg_replace('!lib/plugins/.*$!','',$dir); 395488d5fa0SMichael Klier chi@chimeric.de 396ed7b5f09Sandi //finish here for relative URLs 397ed7b5f09Sandi if(!$abs) return $dir; 398ed7b5f09Sandi 39946c73e01SChris Smith //use config option if available, trim any slash from end of baseurl to avoid multiple consecutive slashes in the path 40046c73e01SChris Smith if($conf['baseurl']) return rtrim($conf['baseurl'],'/').$dir; 401ef7b3ecdSAndreas Gohr 402e82e3526SAndreas Gohr //split hostheader into host and port 403e82e3526SAndreas Gohr list($host,$port) = explode(':',$_SERVER['HTTP_HOST']); 404e82e3526SAndreas Gohr if(!$port) $port = $_SERVER['SERVER_PORT']; 405e82e3526SAndreas Gohr if(!$port) $port = 80; 406ed7b5f09Sandi 407f5c6743cSAndreas Gohr if(!is_ssl()){ 408ed7b5f09Sandi $proto = 'http://'; 409e82e3526SAndreas Gohr if ($port == '80') { 410ed7b5f09Sandi $port=''; 411ed7b5f09Sandi } 412ed7b5f09Sandi }else{ 413ed7b5f09Sandi $proto = 'https://'; 414e82e3526SAndreas Gohr if ($port == '443') { 415ed7b5f09Sandi $port=''; 416ed7b5f09Sandi } 417ed7b5f09Sandi } 418ed7b5f09Sandi 419e82e3526SAndreas Gohr if($port) $port = ':'.$port; 420e82e3526SAndreas Gohr 421ed7b5f09Sandi return $proto.$host.$port.$dir; 422ed7b5f09Sandi} 423ed7b5f09Sandi 424b000c6d4Sandi/** 425f5c6743cSAndreas Gohr * Check if accessed via HTTPS 426f5c6743cSAndreas Gohr * 427f5c6743cSAndreas Gohr * Apache leaves ,$_SERVER['HTTPS'] empty when not available, IIS sets it to 'off'. 428f5c6743cSAndreas Gohr * 'false' and 'disabled' are just guessing 429f5c6743cSAndreas Gohr * 430f5c6743cSAndreas Gohr * @returns bool true when SSL is active 431f5c6743cSAndreas Gohr */ 432f5c6743cSAndreas Gohrfunction is_ssl(){ 433f5c6743cSAndreas Gohr if (preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){ 434f5c6743cSAndreas Gohr return false; 435f5c6743cSAndreas Gohr }else{ 436f5c6743cSAndreas Gohr return true; 437f5c6743cSAndreas Gohr } 438f5c6743cSAndreas Gohr} 439f5c6743cSAndreas Gohr 440f5c6743cSAndreas Gohr/** 441b000c6d4Sandi * Append a PHP extension to a given file and adds an exit call 442b000c6d4Sandi * 443b000c6d4Sandi * This is used to migrate some old configfiles. An added PHP extension 444b000c6d4Sandi * ensures the contents are not shown to webusers even if .htaccess files 445b000c6d4Sandi * do not work 446b000c6d4Sandi * 447b000c6d4Sandi * @author Jan Decaluwe <jan@jandecaluwe.com> 448b000c6d4Sandi */ 4498c4f28e8Sjanfunction scriptify($file) { 4508c4f28e8Sjan // checks 4518c4f28e8Sjan if (!is_readable($file)) { 4528c4f28e8Sjan return; 4538c4f28e8Sjan } 4548c4f28e8Sjan $fn = $file.'.php'; 4558c4f28e8Sjan if (@file_exists($fn)) { 4568c4f28e8Sjan return; 4578c4f28e8Sjan } 4588c4f28e8Sjan $fh = fopen($fn, 'w'); 4598c4f28e8Sjan if (!$fh) { 4603816dcbcSAndreas Gohr nice_die($fn.' is not writable. Check your permission settings!'); 4618c4f28e8Sjan } 4628c4f28e8Sjan // write php exit hack first 4638c4f28e8Sjan fwrite($fh, "# $fn\n"); 4648c4f28e8Sjan fwrite($fh, '# <?php exit()?>'."\n"); 4658c4f28e8Sjan fwrite($fh, "# Don't modify the lines above\n"); 4668c4f28e8Sjan fwrite($fh, "#\n"); 4678c4f28e8Sjan // copy existing lines 4688c4f28e8Sjan $lines = file($file); 4698c4f28e8Sjan foreach ($lines as $line){ 4708c4f28e8Sjan fwrite($fh, $line); 4718c4f28e8Sjan } 4723ba793e2Sandi fclose($fh); 473b000c6d4Sandi //try to rename the old file 4743aee4c27SAndreas Gohr io_rename($file,"$file.old"); 4758c4f28e8Sjan} 4768c4f28e8Sjan 4773816dcbcSAndreas Gohr/** 4783816dcbcSAndreas Gohr * print a nice message even if no styles are loaded yet. 4793816dcbcSAndreas Gohr */ 4803816dcbcSAndreas Gohrfunction nice_die($msg){ 4813816dcbcSAndreas Gohr echo<<<EOT 4823816dcbcSAndreas Gohr <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 4833816dcbcSAndreas Gohr "http://www.w3.org/TR/html4/loose.dtd"> 4843816dcbcSAndreas Gohr <html> 4853816dcbcSAndreas Gohr <head><title>DokuWiki Setup Error</title></head> 4863816dcbcSAndreas Gohr <body style="font-family: Arial, sans-serif"> 4873816dcbcSAndreas Gohr <div style="width:60%; margin: auto; background-color: #fcc; 4883816dcbcSAndreas Gohr border: 1px solid #faa; padding: 0.5em 1em;"> 4893816dcbcSAndreas Gohr <h1 style="font-size: 120%">DokuWiki Setup Error</h1> 4903816dcbcSAndreas Gohr <p>$msg</p> 4913816dcbcSAndreas Gohr </div> 4923816dcbcSAndreas Gohr </body> 4933816dcbcSAndreas Gohr </html> 4943816dcbcSAndreas GohrEOT; 4953816dcbcSAndreas Gohr exit; 4963816dcbcSAndreas Gohr} 4973816dcbcSAndreas Gohr 498ed7b5f09Sandi 49900976812SAndreas Gohr/** 50000976812SAndreas Gohr * A realpath() replacement 50100976812SAndreas Gohr * 50200976812SAndreas Gohr * This function behaves similar to PHP's realpath() but does not resolve 50300976812SAndreas Gohr * symlinks or accesses upper directories 50400976812SAndreas Gohr * 5054761d30cSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 50600976812SAndreas Gohr * @author <richpageau at yahoo dot co dot uk> 50700976812SAndreas Gohr * @link http://de3.php.net/manual/en/function.realpath.php#75992 50800976812SAndreas Gohr */ 509b328697dSAndreas Gohrfunction fullpath($path,$exists=false){ 5104761d30cSAndreas Gohr static $run = 0; 5114761d30cSAndreas Gohr $root = ''; 5124761d30cSAndreas Gohr $iswin = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' || $GLOBALS['DOKU_UNITTEST_ASSUME_WINDOWS']); 51300976812SAndreas Gohr 5144761d30cSAndreas Gohr // find the (indestructable) root of the path - keeps windows stuff intact 5154761d30cSAndreas Gohr if($path{0} == '/'){ 5164761d30cSAndreas Gohr $root = '/'; 5174761d30cSAndreas Gohr }elseif($iswin){ 5184761d30cSAndreas Gohr // match drive letter and UNC paths 5194761d30cSAndreas Gohr if(preg_match('!^([a-zA-z]:)(.*)!',$path,$match)){ 5204761d30cSAndreas Gohr $root = $match[1]; 5214761d30cSAndreas Gohr $path = $match[2]; 5224761d30cSAndreas Gohr }else if(preg_match('!^(\\\\\\\\[^\\\\/]+\\\\[^\\\\/]+[\\\\/])(.*)!',$path,$match)){ 5234761d30cSAndreas Gohr $root = $match[1]; 5244761d30cSAndreas Gohr $path = $match[2]; 52500976812SAndreas Gohr } 5264761d30cSAndreas Gohr } 5274761d30cSAndreas Gohr $path = str_replace('\\','/',$path); 5284761d30cSAndreas Gohr 5294761d30cSAndreas Gohr // if the given path wasn't absolute already, prepend the script path and retry 5304761d30cSAndreas Gohr if(!$root){ 5314761d30cSAndreas Gohr $base = dirname($_SERVER['SCRIPT_FILENAME']); 5324761d30cSAndreas Gohr $path = $base.'/'.$path; 5334761d30cSAndreas Gohr if($run == 0){ // avoid endless recursion when base isn't absolute for some reason 5344761d30cSAndreas Gohr $run++; 535b328697dSAndreas Gohr return fullpath($path,$exists); 5364761d30cSAndreas Gohr } 5374761d30cSAndreas Gohr } 5384761d30cSAndreas Gohr $run = 0; 53900976812SAndreas Gohr 54000976812SAndreas Gohr // canonicalize 54100976812SAndreas Gohr $path=explode('/', $path); 54200976812SAndreas Gohr $newpath=array(); 543ef38bfe8SAndreas Gohr foreach($path as $p) { 544ef38bfe8SAndreas Gohr if ($p === '' || $p === '.') continue; 545ef38bfe8SAndreas Gohr if ($p==='..') { 54600976812SAndreas Gohr array_pop($newpath); 54700976812SAndreas Gohr continue; 54800976812SAndreas Gohr } 549ef38bfe8SAndreas Gohr array_push($newpath, $p); 55000976812SAndreas Gohr } 5514761d30cSAndreas Gohr $finalpath = $root.implode('/', $newpath); 55200976812SAndreas Gohr 553b328697dSAndreas Gohr // check for existance when needed (except when unit testing) 554b328697dSAndreas Gohr if($exists && !defined('DOKU_UNITTEST') && !@file_exists($finalpath)) { 5554761d30cSAndreas Gohr return false; 55600976812SAndreas Gohr } 5574761d30cSAndreas Gohr return $finalpath; 55800976812SAndreas Gohr} 55900976812SAndreas Gohr 56000976812SAndreas Gohr 56100976812SAndreas Gohr 562340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 : 563