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 13ccaeaa85SAndreas Gohr global $config_cascade; 14ccaeaa85SAndreas Gohr $config_cascade = ''; 15ccaeaa85SAndreas Gohr 1648beefecSAndreas Gohr // if available load a preload config file 17e6266454SChris Smith $preload = fullpath(dirname(__FILE__)).'/preload.php'; 18e6266454SChris Smith if (@file_exists($preload)) include($preload); 1948beefecSAndreas Gohr 20ed7b5f09Sandi // define the include path 2100976812SAndreas Gohr if(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../').'/'); 22ad15db82Sandi 23e7cb32dcSAndreas Gohr // define config path (packagers may want to change this to /etc/dokuwiki/) 24b7551a6dSEsther Brunner if(!defined('DOKU_CONF')) define('DOKU_CONF',DOKU_INC.'conf/'); 25e7cb32dcSAndreas Gohr 26bad905f1SBen Coburn // check for error reporting override or set error reporting to sane values 27d8186216SBen Coburn if (!defined('DOKU_E_LEVEL') && @file_exists(DOKU_CONF.'report_e_all')) { 28bad905f1SBen Coburn define('DOKU_E_LEVEL', E_ALL); 29bad905f1SBen Coburn } 30fc80ed59SAndreas Gohr if (!defined('DOKU_E_LEVEL')) { 31fc80ed59SAndreas Gohr if(defined('E_DEPRECATED')){ // since php 5.3 32fc80ed59SAndreas Gohr error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED); 33fc80ed59SAndreas Gohr }else{ 34fc80ed59SAndreas Gohr error_reporting(E_ALL ^ E_NOTICE); 35fc80ed59SAndreas Gohr } 36fc80ed59SAndreas Gohr } else { 37fc80ed59SAndreas Gohr error_reporting(DOKU_E_LEVEL); 38fc80ed59SAndreas Gohr } 39c53ea5f2Sandi 4050602150SBen Coburn // init memory caches 41bc3e97beSAndreas Gohr global $cache_revinfo; $cache_revinfo = array(); 42bc3e97beSAndreas Gohr global $cache_wikifn; $cache_wikifn = array(); 43a424cd8eSchris global $cache_cleanid; $cache_cleanid = array(); 44a424cd8eSchris global $cache_authname; $cache_authname = array(); 450a7e3bceSchris global $cache_metadata; $cache_metadata = array(); 4650602150SBen Coburn 47cb043f52SChris Smith //set the configuration cascade - but only if its not already been set in preload.php 48cb043f52SChris Smith if (empty($config_cascade)) { 49cb043f52SChris Smith $config_cascade = array( 50cb043f52SChris Smith 'main' => array( 51cb043f52SChris Smith 'default' => array(DOKU_CONF.'dokuwiki.php'), 52cb043f52SChris Smith 'local' => array(DOKU_CONF.'local.php'), 53cb043f52SChris Smith 'protected' => array(DOKU_CONF.'local.protected.php'), 54cb043f52SChris Smith ), 55cb043f52SChris Smith 'acronyms' => array( 56b303b92cSChris Smith 'default' => array(DOKU_CONF.'acronyms.conf'), 57b303b92cSChris Smith 'local' => array(DOKU_CONF.'acronyms.local.conf'), 58cb043f52SChris Smith ), 59cb043f52SChris Smith 'entities' => array( 60b303b92cSChris Smith 'default' => array(DOKU_CONF.'entities.conf'), 61b303b92cSChris Smith 'local' => array(DOKU_CONF.'entities.local.conf'), 62cb043f52SChris Smith ), 63cb043f52SChris Smith 'interwiki' => array( 64b303b92cSChris Smith 'default' => array(DOKU_CONF.'interwiki.conf'), 65b303b92cSChris Smith 'local' => array(DOKU_CONF.'interwiki.local.conf'), 66cb043f52SChris Smith ), 67f8121585SChris Smith 'license' => array( 68f8121585SChris Smith 'default' => array(DOKU_CONF.'license.php'), 69f8121585SChris Smith 'local' => array(DOKU_CONF.'license.local.php'), 70f8121585SChris Smith ), 71f8121585SChris Smith 'mediameta' => array( 72f8121585SChris Smith 'default' => array(DOKU_CONF.'mediameta.php'), 73f8121585SChris Smith 'local' => array(DOKU_CONF.'mediameta.local.php'), 74f8121585SChris Smith ), 75cb043f52SChris Smith 'mime' => array( 76b303b92cSChris Smith 'default' => array(DOKU_CONF.'mime.conf'), 77b303b92cSChris Smith 'local' => array(DOKU_CONF.'mime.local.conf'), 78cb043f52SChris Smith ), 79cb043f52SChris Smith 'scheme' => array( 80b303b92cSChris Smith 'default' => array(DOKU_CONF.'scheme.conf'), 81b303b92cSChris Smith 'local' => array(DOKU_CONF.'scheme.local.conf'), 82cb043f52SChris Smith ), 83cb043f52SChris Smith 'smileys' => array( 84b303b92cSChris Smith 'default' => array(DOKU_CONF.'smileys.conf'), 85b303b92cSChris Smith 'local' => array(DOKU_CONF.'smileys.local.conf'), 86cb043f52SChris Smith ), 87cb043f52SChris Smith 'wordblock' => array( 88b303b92cSChris Smith 'default' => array(DOKU_CONF.'wordblock.conf'), 89b303b92cSChris Smith 'local' => array(DOKU_CONF.'wordblock.local.conf'), 90cb043f52SChris Smith ), 91cb043f52SChris Smith ); 92cb043f52SChris Smith } 93cb043f52SChris Smith 944724a577Sandi //prepare config array() 95ee20e7d1Sandi global $conf; 964724a577Sandi $conf = array(); 974724a577Sandi 98cb043f52SChris Smith // load the global config file(s) 99b303b92cSChris Smith foreach (array('default','local','protected') as $config_group) { 100f8121585SChris Smith if (empty($config_cascade['main'][$config_group])) continue; 101b303b92cSChris Smith foreach ($config_cascade['main'][$config_group] as $config_file) { 102f8121585SChris Smith if (@file_exists($config_file)) { 103f8121585SChris Smith include($config_file); 104f8121585SChris Smith } 105cb043f52SChris Smith } 1060a6ead41SAndreas Gohr } 107ad15db82Sandi 108ad15db82Sandi //prepare language array 109ee20e7d1Sandi global $lang; 110ad15db82Sandi $lang = array(); 111ed7b5f09Sandi 11216521111Sandi //load the language files 113bc3b6aecSandi require_once(DOKU_INC.'inc/lang/en/lang.php'); 114f949a01cSAndreas Gohr if ( $conf['lang'] && $conf['lang'] != 'en' ) { 115bc3b6aecSandi require_once(DOKU_INC.'inc/lang/'.$conf['lang'].'/lang.php'); 116fc1c55b1Shfuecks } 11716521111Sandi 118066fee30SAndreas Gohr //prepare license array() 119066fee30SAndreas Gohr global $license; 120066fee30SAndreas Gohr $license = array(); 121066fee30SAndreas Gohr 122066fee30SAndreas Gohr // load the license file(s) 123f8121585SChris Smith foreach (array('default','local') as $config_group) { 124f8121585SChris Smith if (empty($config_cascade['license'][$config_group])) continue; 125f8121585SChris Smith foreach ($config_cascade['license'][$config_group] as $config_file) { 126f8121585SChris Smith if(@file_exists($config_file)){ 127f8121585SChris Smith include($config_file); 128f8121585SChris Smith } 129f8121585SChris Smith } 130066fee30SAndreas Gohr } 131066fee30SAndreas Gohr 132ed7b5f09Sandi // define baseURL 1334b1a4e04SAndreas Gohr if(!defined('DOKU_REL')) define('DOKU_REL',getBaseURL(false)); 134ed7b5f09Sandi if(!defined('DOKU_URL')) define('DOKU_URL',getBaseURL(true)); 1354b1a4e04SAndreas Gohr if(!defined('DOKU_BASE')){ 1364b1a4e04SAndreas Gohr if($conf['canonical']){ 1374b1a4e04SAndreas Gohr define('DOKU_BASE',DOKU_URL); 1384b1a4e04SAndreas Gohr }else{ 1394b1a4e04SAndreas Gohr define('DOKU_BASE',DOKU_REL); 1404b1a4e04SAndreas Gohr } 1414b1a4e04SAndreas Gohr } 1424b1a4e04SAndreas Gohr 143b8595a66SAndreas Gohr // define whitespace 144b8595a66SAndreas Gohr if(!defined('DOKU_LF')) define ('DOKU_LF',"\n"); 145b8595a66SAndreas Gohr if(!defined('DOKU_TAB')) define ('DOKU_TAB',"\t"); 146ed7b5f09Sandi 147*656c8fb3SAndreas Gohr // define cookie and session id, append server port when securecookie is configured FS#1664 148*656c8fb3SAndreas Gohr if (!defined('DOKU_COOKIE')) define('DOKU_COOKIE', 'DW'.md5(DOKU_REL.(($conf['securecookie'])?$_SERVER['SERVER_PORT']:''))); 149e71ce681SAndreas Gohr 150ee20e7d1Sandi // define Plugin dir 151f62ea8a1Sandi if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 152ee20e7d1Sandi 153ed7b5f09Sandi // define main script 154ed7b5f09Sandi if(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php'); 155ed7b5f09Sandi 1566b13307fSandi // define Template baseURL 1576b13307fSandi if(!defined('DOKU_TPL')) define('DOKU_TPL', 158f62ea8a1Sandi DOKU_BASE.'lib/tpl/'.$conf['template'].'/'); 1596b13307fSandi 16078a6aeb1SAndreas Gohr // define real Template directory 16178a6aeb1SAndreas Gohr if(!defined('DOKU_TPLINC')) define('DOKU_TPLINC', 16278a6aeb1SAndreas Gohr DOKU_INC.'lib/tpl/'.$conf['template'].'/'); 16378a6aeb1SAndreas Gohr 164ed7b5f09Sandi // make session rewrites XHTML compliant 1653fc74836Sandi @ini_set('arg_separator.output', '&'); 166ed7b5f09Sandi 167d7e6bba9SAndreas Gohr // make sure global zlib does not interfere FS#1132 168d7e6bba9SAndreas Gohr @ini_set('zlib.output_compression', 'off'); 169d7e6bba9SAndreas Gohr 1706deb5405SAndreas Gohr // increase PCRE backtrack limit 1716deb5405SAndreas Gohr @ini_set('pcre.backtrack_limit', '20971520'); 1726deb5405SAndreas Gohr 17398bda4fdSAndreas Gohr // enable gzip compression if supported 17498bda4fdSAndreas Gohr $conf['gzip_output'] &= (strpos($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip') !== false); 1753138b5c7SAndreas Gohr if ($conf['gzip_output'] && 1763138b5c7SAndreas Gohr !defined('DOKU_DISABLE_GZIP_OUTPUT') && 17798bda4fdSAndreas Gohr function_exists('ob_gzhandler')) { 1783138b5c7SAndreas Gohr ob_start('ob_gzhandler'); 1793138b5c7SAndreas Gohr } 1803138b5c7SAndreas Gohr 181ed7b5f09Sandi // init session 1826534245aSAndreas Gohr if (!headers_sent() && !defined('NOSESSION')){ 183ed7b5f09Sandi session_name("DokuWiki"); 184f5c6743cSAndreas Gohr if (version_compare(PHP_VERSION, '5.2.0', '>')) { 185f5c6743cSAndreas Gohr session_set_cookie_params(0,DOKU_REL,'',($conf['securecookie'] && is_ssl()),true); 186f5c6743cSAndreas Gohr }else{ 187f5c6743cSAndreas Gohr session_set_cookie_params(0,DOKU_REL,'',($conf['securecookie'] && is_ssl())); 188f5c6743cSAndreas Gohr } 189bad31ae9SAndreas Gohr session_start(); 19014a122deSAndreas Gohr 19114a122deSAndreas Gohr // load left over messages 19214a122deSAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['msg'])){ 19314a122deSAndreas Gohr $MSG = $_SESSION[DOKU_COOKIE]['msg']; 19414a122deSAndreas Gohr unset($_SESSION[DOKU_COOKIE]['msg']); 19514a122deSAndreas Gohr } 196bad31ae9SAndreas Gohr } 197ed7b5f09Sandi 198ed7b5f09Sandi // kill magic quotes 199e55eb89cSAndreas Gohr if (get_magic_quotes_gpc() && !defined('MAGIC_QUOTES_STRIPPED')) { 200ed7b5f09Sandi if (!empty($_GET)) remove_magic_quotes($_GET); 201ed7b5f09Sandi if (!empty($_POST)) remove_magic_quotes($_POST); 202ed7b5f09Sandi if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE); 203ed7b5f09Sandi if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST); 2043fc74836Sandi @ini_set('magic_quotes_gpc', 0); 205e55eb89cSAndreas Gohr define('MAGIC_QUOTES_STRIPPED',1); 206ed7b5f09Sandi } 2073fc74836Sandi @set_magic_quotes_runtime(0); 2083fc74836Sandi @ini_set('magic_quotes_sybase',0); 209ed7b5f09Sandi 210a1637ffdSAndreas Gohr // don't let cookies ever interfere with request vars 211a1637ffdSAndreas Gohr $_REQUEST = array_merge($_GET,$_POST); 212a1637ffdSAndreas Gohr 2133dea4ebcSAndreas Gohr // we don't want a purge URL to be digged 2143dea4ebcSAndreas Gohr if($_REQUEST['purge'] && $_SERVER['HTTP_REFERER']) unset($_REQUEST['purge']); 2153dea4ebcSAndreas Gohr 216ed7b5f09Sandi // disable gzip if not available 2178a447e5cSAndreas Gohr if($conf['compression'] == 'bz2' && !function_exists('bzopen')){ 218fe893490SAndreas Gohr $conf['compression'] = 'gz'; 219501252a5SAndreas Gohr } 220fe893490SAndreas Gohr if($conf['compression'] == 'gz' && !function_exists('gzopen')){ 221501252a5SAndreas Gohr $conf['compression'] = 0; 222ed7b5f09Sandi } 223ed7b5f09Sandi 224e656dcd4SAndreas Gohr // fix dateformat for upgraders 225e656dcd4SAndreas Gohr if(strpos($conf['dformat'],'%') === false){ 226e656dcd4SAndreas Gohr $conf['dformat'] = '%Y/%m/%d %H:%M'; 227e656dcd4SAndreas Gohr } 228e656dcd4SAndreas Gohr 2291ca31cfeSAndreas Gohr // precalculate file creation modes 2301ca31cfeSAndreas Gohr init_creationmodes(); 231ed7b5f09Sandi 2323dc3a5f1Sandi // make real paths and check them 23398407a7aSandi init_paths(); 2347367b368SAndreas Gohr init_files(); 235ed7b5f09Sandi 2368c4f28e8Sjan // automatic upgrade to script versions of certain files 237e7cb32dcSAndreas Gohr scriptify(DOKU_CONF.'users.auth'); 238e7cb32dcSAndreas Gohr scriptify(DOKU_CONF.'acl.auth'); 239f62ea8a1Sandi 240f62ea8a1Sandi 241f62ea8a1Sandi/** 24298407a7aSandi * Checks paths from config file 24398407a7aSandi */ 24498407a7aSandifunction init_paths(){ 24598407a7aSandi global $conf; 24698407a7aSandi 24798407a7aSandi $paths = array('datadir' => 'pages', 24898407a7aSandi 'olddir' => 'attic', 24998407a7aSandi 'mediadir' => 'media', 25098407a7aSandi 'metadir' => 'meta', 25198407a7aSandi 'cachedir' => 'cache', 252579b0f7eSTNHarris 'indexdir' => 'index', 253de33a58fSMichael Klier 'lockdir' => 'locks', 254de33a58fSMichael Klier 'tmpdir' => 'tmp'); 25598407a7aSandi 25698407a7aSandi foreach($paths as $c => $p){ 257bb4866bdSchris if(empty($conf[$c])) $conf[$c] = $conf['savedir'].'/'.$p; 25898407a7aSandi $conf[$c] = init_path($conf[$c]); 2591983acc2SGuy Brand if(empty($conf[$c])) nice_die("The $c ('$p') does not exist, isn't accessible or writable. 26069dc3177SAndreas Gohr You should check your config and permission settings. 26169dc3177SAndreas Gohr Or maybe you want to <a href=\"install.php\">run the 26269dc3177SAndreas Gohr installer</a>?"); 26398407a7aSandi } 26471726d78SBen Coburn 26571726d78SBen Coburn // path to old changelog only needed for upgrading 26671726d78SBen Coburn $conf['changelog_old'] = init_path((isset($conf['changelog']))?($conf['changelog']):($conf['savedir'].'/changes.log')); 26771726d78SBen Coburn if ($conf['changelog_old']=='') { unset($conf['changelog_old']); } 26871726d78SBen Coburn // hardcoded changelog because it is now a cache that lives in meta 26971726d78SBen Coburn $conf['changelog'] = $conf['metadir'].'/_dokuwiki.changes'; 27099c8d7f2Smichael $conf['media_changelog'] = $conf['metadir'].'/_media.changes'; 27198407a7aSandi} 27298407a7aSandi 27398407a7aSandi/** 2740d8850c4SAndreas Gohr * Checks the existance of certain files and creates them if missing. 2757367b368SAndreas Gohr */ 2767367b368SAndreas Gohrfunction init_files(){ 2777367b368SAndreas Gohr global $conf; 2780d8850c4SAndreas Gohr 279579b0f7eSTNHarris $files = array( $conf['indexdir'].'/page.idx'); 2807367b368SAndreas Gohr 2817367b368SAndreas Gohr foreach($files as $file){ 2827367b368SAndreas Gohr if(!@file_exists($file)){ 2830d8850c4SAndreas Gohr $fh = @fopen($file,'a'); 2840d8850c4SAndreas Gohr if($fh){ 2857367b368SAndreas Gohr fclose($fh); 2861ca31cfeSAndreas Gohr if($conf['fperm']) chmod($file, $conf['fperm']); 2870d8850c4SAndreas Gohr }else{ 2883816dcbcSAndreas Gohr nice_die("$file is not writable. Check your permissions settings!"); 2890d8850c4SAndreas Gohr } 2907367b368SAndreas Gohr } 2917367b368SAndreas Gohr } 2927367b368SAndreas Gohr} 2937367b368SAndreas Gohr 2947367b368SAndreas Gohr/** 2950d8850c4SAndreas Gohr * Returns absolute path 296f62ea8a1Sandi * 2970d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC. 2980d8850c4SAndreas Gohr * Check for accessability on directories as well. 2990d8850c4SAndreas Gohr * 3000d8850c4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 301f62ea8a1Sandi */ 302f62ea8a1Sandifunction init_path($path){ 3030d8850c4SAndreas Gohr // check existance 30400976812SAndreas Gohr $p = fullpath($path); 3050d8850c4SAndreas Gohr if(!@file_exists($p)){ 30600976812SAndreas Gohr $p = fullpath(DOKU_INC.$path); 3070d8850c4SAndreas Gohr if(!@file_exists($p)){ 3088fc4e739Sandi return ''; 309f62ea8a1Sandi } 3100d8850c4SAndreas Gohr } 3110d8850c4SAndreas Gohr 3120d8850c4SAndreas Gohr // check writability 3130d8850c4SAndreas Gohr if(!@is_writable($p)){ 3140d8850c4SAndreas Gohr return ''; 3150d8850c4SAndreas Gohr } 3160d8850c4SAndreas Gohr 3170d8850c4SAndreas Gohr // check accessability (execute bit) for directories 3180d8850c4SAndreas Gohr if(@is_dir($p) && !@file_exists("$p/.")){ 3190d8850c4SAndreas Gohr return ''; 3200d8850c4SAndreas Gohr } 3210d8850c4SAndreas Gohr 3220d8850c4SAndreas Gohr return $p; 3230d8850c4SAndreas Gohr} 3248c4f28e8Sjan 325ed7b5f09Sandi/** 3261ca31cfeSAndreas Gohr * Sets the internal config values fperm and dperm which, when set, 3271ca31cfeSAndreas Gohr * will be used to change the permission of a newly created dir or 3281ca31cfeSAndreas Gohr * file with chmod. Considers the influence of the system's umask 3291ca31cfeSAndreas Gohr * setting the values only if needed. 3301ca31cfeSAndreas Gohr */ 3311ca31cfeSAndreas Gohrfunction init_creationmodes(){ 3321ca31cfeSAndreas Gohr global $conf; 3331ca31cfeSAndreas Gohr 3341ca31cfeSAndreas Gohr // Legacy support for old umask/dmask scheme 3351ca31cfeSAndreas Gohr unset($conf['dmask']); 3361ca31cfeSAndreas Gohr unset($conf['fmask']); 3371ca31cfeSAndreas Gohr unset($conf['umask']); 3381ca31cfeSAndreas Gohr unset($conf['fperm']); 3391ca31cfeSAndreas Gohr unset($conf['dperm']); 3401ca31cfeSAndreas Gohr 3419f3cdec3SAndreas Gohr // get system umask, fallback to 0 if none available 3429f3cdec3SAndreas Gohr $umask = @umask(); 3439f3cdec3SAndreas Gohr if(!$umask) $umask = 0000; 3441ca31cfeSAndreas Gohr 3451ca31cfeSAndreas Gohr // check what is set automatically by the system on file creation 3461ca31cfeSAndreas Gohr // and set the fperm param if it's not what we want 3471ca31cfeSAndreas Gohr $auto_fmode = 0666 & ~$umask; 3481ca31cfeSAndreas Gohr if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode']; 3491ca31cfeSAndreas Gohr 3501ca31cfeSAndreas Gohr // check what is set automatically by the system on file creation 3511ca31cfeSAndreas Gohr // and set the dperm param if it's not what we want 3521ca31cfeSAndreas Gohr $auto_dmode = $conf['dmode'] & ~$umask; 3531ca31cfeSAndreas Gohr if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode']; 3541ca31cfeSAndreas Gohr} 3551ca31cfeSAndreas Gohr 3561ca31cfeSAndreas Gohr/** 357ed7b5f09Sandi * remove magic quotes recursivly 358ed7b5f09Sandi * 359ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org> 360ed7b5f09Sandi */ 361ed7b5f09Sandifunction remove_magic_quotes(&$array) { 362ed7b5f09Sandi foreach (array_keys($array) as $key) { 36381c54349SAndreas Gohr // handle magic quotes in keynames (breaks order) 36481c54349SAndreas Gohr $sk = stripslashes($key); 36581c54349SAndreas Gohr if($sk != $key){ 36681c54349SAndreas Gohr $array[$sk] = $array[$key]; 36781c54349SAndreas Gohr unset($array[$key]); 36881c54349SAndreas Gohr $key = $sk; 36981c54349SAndreas Gohr } 37081c54349SAndreas Gohr 37181c54349SAndreas Gohr // do recursion if needed 372ed7b5f09Sandi if (is_array($array[$key])) { 373ed7b5f09Sandi remove_magic_quotes($array[$key]); 374ed7b5f09Sandi }else { 375ed7b5f09Sandi $array[$key] = stripslashes($array[$key]); 376ed7b5f09Sandi } 377ed7b5f09Sandi } 378ed7b5f09Sandi} 379ed7b5f09Sandi 380ed7b5f09Sandi/** 381ed7b5f09Sandi * Returns the full absolute URL to the directory where 382ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash) 383ed7b5f09Sandi * 384ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org> 385ed7b5f09Sandi */ 3864b1a4e04SAndreas Gohrfunction getBaseURL($abs=null){ 387ed7b5f09Sandi global $conf; 388ed7b5f09Sandi //if canonical url enabled always return absolute 3894b1a4e04SAndreas Gohr if(is_null($abs)) $abs = $conf['canonical']; 390ed7b5f09Sandi 39192b83b77Sandi if($conf['basedir']){ 39246c73e01SChris Smith $dir = $conf['basedir']; 39389aa05dbSAndreas Gohr }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){ 39446c73e01SChris Smith $dir = dirname($_SERVER['SCRIPT_NAME']); 39589aa05dbSAndreas Gohr }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){ 39646c73e01SChris Smith $dir = dirname($_SERVER['PHP_SELF']); 397093ec9e4Sandi }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){ 398093ec9e4Sandi $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','', 399093ec9e4Sandi $_SERVER['SCRIPT_FILENAME']); 40046c73e01SChris Smith $dir = dirname('/'.$dir); 40192b83b77Sandi }else{ 40246c73e01SChris Smith $dir = '.'; //probably wrong 40392b83b77Sandi } 404ed7b5f09Sandi 40546c73e01SChris Smith $dir = str_replace('\\','/',$dir); // bugfix for weird WIN behaviour 40646c73e01SChris Smith $dir = preg_replace('#//+#','/',"/$dir/"); // ensure leading and trailing slashes 407ed7b5f09Sandi 408f62ea8a1Sandi //handle script in lib/exe dir 409f62ea8a1Sandi $dir = preg_replace('!lib/exe/$!','',$dir); 410f62ea8a1Sandi 411488d5fa0SMichael Klier chi@chimeric.de //handle script in lib/plugins dir 412488d5fa0SMichael Klier chi@chimeric.de $dir = preg_replace('!lib/plugins/.*$!','',$dir); 413488d5fa0SMichael Klier chi@chimeric.de 414ed7b5f09Sandi //finish here for relative URLs 415ed7b5f09Sandi if(!$abs) return $dir; 416ed7b5f09Sandi 41746c73e01SChris Smith //use config option if available, trim any slash from end of baseurl to avoid multiple consecutive slashes in the path 41846c73e01SChris Smith if($conf['baseurl']) return rtrim($conf['baseurl'],'/').$dir; 419ef7b3ecdSAndreas Gohr 420e82e3526SAndreas Gohr //split hostheader into host and port 421e82e3526SAndreas Gohr list($host,$port) = explode(':',$_SERVER['HTTP_HOST']); 422e82e3526SAndreas Gohr if(!$port) $port = $_SERVER['SERVER_PORT']; 423e82e3526SAndreas Gohr if(!$port) $port = 80; 424ed7b5f09Sandi 425f5c6743cSAndreas Gohr if(!is_ssl()){ 426ed7b5f09Sandi $proto = 'http://'; 427e82e3526SAndreas Gohr if ($port == '80') { 428ed7b5f09Sandi $port=''; 429ed7b5f09Sandi } 430ed7b5f09Sandi }else{ 431ed7b5f09Sandi $proto = 'https://'; 432e82e3526SAndreas Gohr if ($port == '443') { 433ed7b5f09Sandi $port=''; 434ed7b5f09Sandi } 435ed7b5f09Sandi } 436ed7b5f09Sandi 437e82e3526SAndreas Gohr if($port) $port = ':'.$port; 438e82e3526SAndreas Gohr 439ed7b5f09Sandi return $proto.$host.$port.$dir; 440ed7b5f09Sandi} 441ed7b5f09Sandi 442b000c6d4Sandi/** 443f5c6743cSAndreas Gohr * Check if accessed via HTTPS 444f5c6743cSAndreas Gohr * 445f5c6743cSAndreas Gohr * Apache leaves ,$_SERVER['HTTPS'] empty when not available, IIS sets it to 'off'. 446f5c6743cSAndreas Gohr * 'false' and 'disabled' are just guessing 447f5c6743cSAndreas Gohr * 448f5c6743cSAndreas Gohr * @returns bool true when SSL is active 449f5c6743cSAndreas Gohr */ 450f5c6743cSAndreas Gohrfunction is_ssl(){ 451f5c6743cSAndreas Gohr if (preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){ 452f5c6743cSAndreas Gohr return false; 453f5c6743cSAndreas Gohr }else{ 454f5c6743cSAndreas Gohr return true; 455f5c6743cSAndreas Gohr } 456f5c6743cSAndreas Gohr} 457f5c6743cSAndreas Gohr 458f5c6743cSAndreas Gohr/** 459b000c6d4Sandi * Append a PHP extension to a given file and adds an exit call 460b000c6d4Sandi * 461b000c6d4Sandi * This is used to migrate some old configfiles. An added PHP extension 462b000c6d4Sandi * ensures the contents are not shown to webusers even if .htaccess files 463b000c6d4Sandi * do not work 464b000c6d4Sandi * 465b000c6d4Sandi * @author Jan Decaluwe <jan@jandecaluwe.com> 466b000c6d4Sandi */ 4678c4f28e8Sjanfunction scriptify($file) { 4688c4f28e8Sjan // checks 4698c4f28e8Sjan if (!is_readable($file)) { 4708c4f28e8Sjan return; 4718c4f28e8Sjan } 4728c4f28e8Sjan $fn = $file.'.php'; 4738c4f28e8Sjan if (@file_exists($fn)) { 4748c4f28e8Sjan return; 4758c4f28e8Sjan } 4768c4f28e8Sjan $fh = fopen($fn, 'w'); 4778c4f28e8Sjan if (!$fh) { 4783816dcbcSAndreas Gohr nice_die($fn.' is not writable. Check your permission settings!'); 4798c4f28e8Sjan } 4808c4f28e8Sjan // write php exit hack first 4818c4f28e8Sjan fwrite($fh, "# $fn\n"); 4828c4f28e8Sjan fwrite($fh, '# <?php exit()?>'."\n"); 4838c4f28e8Sjan fwrite($fh, "# Don't modify the lines above\n"); 4848c4f28e8Sjan fwrite($fh, "#\n"); 4858c4f28e8Sjan // copy existing lines 4868c4f28e8Sjan $lines = file($file); 4878c4f28e8Sjan foreach ($lines as $line){ 4888c4f28e8Sjan fwrite($fh, $line); 4898c4f28e8Sjan } 4903ba793e2Sandi fclose($fh); 491b000c6d4Sandi //try to rename the old file 4923aee4c27SAndreas Gohr io_rename($file,"$file.old"); 4938c4f28e8Sjan} 4948c4f28e8Sjan 4953816dcbcSAndreas Gohr/** 4963816dcbcSAndreas Gohr * print a nice message even if no styles are loaded yet. 4973816dcbcSAndreas Gohr */ 4983816dcbcSAndreas Gohrfunction nice_die($msg){ 4993816dcbcSAndreas Gohr echo<<<EOT 5003816dcbcSAndreas Gohr <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 5013816dcbcSAndreas Gohr "http://www.w3.org/TR/html4/loose.dtd"> 5023816dcbcSAndreas Gohr <html> 5033816dcbcSAndreas Gohr <head><title>DokuWiki Setup Error</title></head> 5043816dcbcSAndreas Gohr <body style="font-family: Arial, sans-serif"> 5053816dcbcSAndreas Gohr <div style="width:60%; margin: auto; background-color: #fcc; 5063816dcbcSAndreas Gohr border: 1px solid #faa; padding: 0.5em 1em;"> 5073816dcbcSAndreas Gohr <h1 style="font-size: 120%">DokuWiki Setup Error</h1> 5083816dcbcSAndreas Gohr <p>$msg</p> 5093816dcbcSAndreas Gohr </div> 5103816dcbcSAndreas Gohr </body> 5113816dcbcSAndreas Gohr </html> 5123816dcbcSAndreas GohrEOT; 5133816dcbcSAndreas Gohr exit; 5143816dcbcSAndreas Gohr} 5153816dcbcSAndreas Gohr 516ed7b5f09Sandi 51700976812SAndreas Gohr/** 51800976812SAndreas Gohr * A realpath() replacement 51900976812SAndreas Gohr * 52000976812SAndreas Gohr * This function behaves similar to PHP's realpath() but does not resolve 52100976812SAndreas Gohr * symlinks or accesses upper directories 52200976812SAndreas Gohr * 5234761d30cSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 52400976812SAndreas Gohr * @author <richpageau at yahoo dot co dot uk> 52500976812SAndreas Gohr * @link http://de3.php.net/manual/en/function.realpath.php#75992 52600976812SAndreas Gohr */ 527b328697dSAndreas Gohrfunction fullpath($path,$exists=false){ 5284761d30cSAndreas Gohr static $run = 0; 5294761d30cSAndreas Gohr $root = ''; 530f0a201c5SChris Smith $iswin = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' || @$GLOBALS['DOKU_UNITTEST_ASSUME_WINDOWS']); 53100976812SAndreas Gohr 5324761d30cSAndreas Gohr // find the (indestructable) root of the path - keeps windows stuff intact 5334761d30cSAndreas Gohr if($path{0} == '/'){ 5344761d30cSAndreas Gohr $root = '/'; 5354761d30cSAndreas Gohr }elseif($iswin){ 5364761d30cSAndreas Gohr // match drive letter and UNC paths 5374761d30cSAndreas Gohr if(preg_match('!^([a-zA-z]:)(.*)!',$path,$match)){ 538b9c4302bSAndreas Gohr $root = $match[1].'/'; 5394761d30cSAndreas Gohr $path = $match[2]; 5404761d30cSAndreas Gohr }else if(preg_match('!^(\\\\\\\\[^\\\\/]+\\\\[^\\\\/]+[\\\\/])(.*)!',$path,$match)){ 5414761d30cSAndreas Gohr $root = $match[1]; 5424761d30cSAndreas Gohr $path = $match[2]; 54300976812SAndreas Gohr } 5444761d30cSAndreas Gohr } 5454761d30cSAndreas Gohr $path = str_replace('\\','/',$path); 5464761d30cSAndreas Gohr 5474761d30cSAndreas Gohr // if the given path wasn't absolute already, prepend the script path and retry 5484761d30cSAndreas Gohr if(!$root){ 5494761d30cSAndreas Gohr $base = dirname($_SERVER['SCRIPT_FILENAME']); 5504761d30cSAndreas Gohr $path = $base.'/'.$path; 5514761d30cSAndreas Gohr if($run == 0){ // avoid endless recursion when base isn't absolute for some reason 5524761d30cSAndreas Gohr $run++; 553b328697dSAndreas Gohr return fullpath($path,$exists); 5544761d30cSAndreas Gohr } 5554761d30cSAndreas Gohr } 5564761d30cSAndreas Gohr $run = 0; 55700976812SAndreas Gohr 55800976812SAndreas Gohr // canonicalize 55900976812SAndreas Gohr $path=explode('/', $path); 56000976812SAndreas Gohr $newpath=array(); 561ef38bfe8SAndreas Gohr foreach($path as $p) { 562ef38bfe8SAndreas Gohr if ($p === '' || $p === '.') continue; 563ef38bfe8SAndreas Gohr if ($p==='..') { 56400976812SAndreas Gohr array_pop($newpath); 56500976812SAndreas Gohr continue; 56600976812SAndreas Gohr } 567ef38bfe8SAndreas Gohr array_push($newpath, $p); 56800976812SAndreas Gohr } 5694761d30cSAndreas Gohr $finalpath = $root.implode('/', $newpath); 57000976812SAndreas Gohr 571b328697dSAndreas Gohr // check for existance when needed (except when unit testing) 572b328697dSAndreas Gohr if($exists && !defined('DOKU_UNITTEST') && !@file_exists($finalpath)) { 5734761d30cSAndreas Gohr return false; 57400976812SAndreas Gohr } 5754761d30cSAndreas Gohr return $finalpath; 57600976812SAndreas Gohr} 57700976812SAndreas Gohr 57800976812SAndreas Gohr 57900976812SAndreas Gohr 580340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 : 581