1ed7b5f09Sandi<?php 2ed7b5f09Sandi/** 3ed7b5f09Sandi * Initialize some defaults needed for DokuWiki 4ed7b5f09Sandi */ 5ed7b5f09Sandi 6a609a9ccSBen Coburn// start timing Dokuwiki execution 7a609a9ccSBen Coburnfunction delta_time($start=0) { 8ac4be4d7SPiyush Mishra return microtime(true)-((float)$start); 9a609a9ccSBen Coburn} 10a609a9ccSBen Coburndefine('DOKU_START_TIME', delta_time()); 11a609a9ccSBen Coburn 12ccaeaa85SAndreas Gohrglobal $config_cascade; 13cca94fbcSRoland Hager$config_cascade = array(); 14ccaeaa85SAndreas Gohr 1548beefecSAndreas Gohr// if available load a preload config file 16e6266454SChris Smith$preload = fullpath(dirname(__FILE__)).'/preload.php'; 17e6266454SChris Smithif (@file_exists($preload)) include($preload); 1848beefecSAndreas Gohr 19ed7b5f09Sandi// define the include path 2000976812SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../').'/'); 21ad15db82Sandi 22c2a6d816SAndreas Gohr// define Plugin dir 23c2a6d816SAndreas Gohrif(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 24c2a6d816SAndreas Gohr 25e7cb32dcSAndreas Gohr// define config path (packagers may want to change this to /etc/dokuwiki/) 26b7551a6dSEsther Brunnerif(!defined('DOKU_CONF')) define('DOKU_CONF',DOKU_INC.'conf/'); 27e7cb32dcSAndreas Gohr 28bad905f1SBen Coburn// check for error reporting override or set error reporting to sane values 29d8186216SBen Coburnif (!defined('DOKU_E_LEVEL') && @file_exists(DOKU_CONF.'report_e_all')) { 30bad905f1SBen Coburn define('DOKU_E_LEVEL', E_ALL); 31bad905f1SBen Coburn} 32fc80ed59SAndreas Gohrif (!defined('DOKU_E_LEVEL')) { 334fcd684aSMichael Hamann if(defined('E_DEPRECATED')){ // since php 5.3, since php 5.4 E_STRICT is part of E_ALL 344fcd684aSMichael Hamann error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT); 35fc80ed59SAndreas Gohr }else{ 36fc80ed59SAndreas Gohr error_reporting(E_ALL ^ E_NOTICE); 37fc80ed59SAndreas Gohr } 38fc80ed59SAndreas Gohr} else { 39fc80ed59SAndreas Gohr error_reporting(DOKU_E_LEVEL); 40fc80ed59SAndreas Gohr} 41c53ea5f2Sandi 4250602150SBen Coburn// init memory caches 43db959ae3SAndreas Gohrglobal $cache_revinfo; 44db959ae3SAndreas Gohr $cache_revinfo = array(); 45db959ae3SAndreas Gohrglobal $cache_wikifn; 46db959ae3SAndreas Gohr $cache_wikifn = array(); 47db959ae3SAndreas Gohrglobal $cache_cleanid; 48db959ae3SAndreas Gohr $cache_cleanid = array(); 49db959ae3SAndreas Gohrglobal $cache_authname; 50db959ae3SAndreas Gohr $cache_authname = array(); 51db959ae3SAndreas Gohrglobal $cache_metadata; 52db959ae3SAndreas Gohr $cache_metadata = array(); 5350602150SBen Coburn 54cca94fbcSRoland Hager// always include 'inc/config_cascade.php' 55cca94fbcSRoland Hager// previously in preload.php set fields of $config_cascade will be merged with the defaults 56e6a6dbfeSAndreas Gohrinclude(DOKU_INC.'inc/config_cascade.php'); 57cb043f52SChris Smith 584724a577Sandi//prepare config array() 59ee20e7d1Sandiglobal $conf; 604724a577Sandi$conf = array(); 614724a577Sandi 62cb043f52SChris Smith// load the global config file(s) 63b303b92cSChris Smithforeach (array('default','local','protected') as $config_group) { 64f8121585SChris Smith if (empty($config_cascade['main'][$config_group])) continue; 65b303b92cSChris Smith foreach ($config_cascade['main'][$config_group] as $config_file) { 66f8121585SChris Smith if (@file_exists($config_file)) { 67f8121585SChris Smith include($config_file); 68f8121585SChris Smith } 69cb043f52SChris Smith } 700a6ead41SAndreas Gohr} 71ad15db82Sandi 72066fee30SAndreas Gohr//prepare license array() 73066fee30SAndreas Gohrglobal $license; 74066fee30SAndreas Gohr$license = array(); 75066fee30SAndreas Gohr 76066fee30SAndreas Gohr// load the license file(s) 77f8121585SChris Smithforeach (array('default','local') as $config_group) { 78f8121585SChris Smith if (empty($config_cascade['license'][$config_group])) continue; 79f8121585SChris Smith foreach ($config_cascade['license'][$config_group] as $config_file) { 80f8121585SChris Smith if(@file_exists($config_file)){ 81f8121585SChris Smith include($config_file); 82f8121585SChris Smith } 83f8121585SChris Smith } 84066fee30SAndreas Gohr} 85066fee30SAndreas Gohr 861f8eb24fSAndreas Gohr// set timezone (as in pre 5.3.0 days) 871f8eb24fSAndreas Gohrdate_default_timezone_set(@date_default_timezone_get()); 881f8eb24fSAndreas Gohr 89ed7b5f09Sandi// define baseURL 904b1a4e04SAndreas Gohrif(!defined('DOKU_REL')) define('DOKU_REL',getBaseURL(false)); 91ed7b5f09Sandiif(!defined('DOKU_URL')) define('DOKU_URL',getBaseURL(true)); 924b1a4e04SAndreas Gohrif(!defined('DOKU_BASE')){ 934b1a4e04SAndreas Gohr if($conf['canonical']){ 944b1a4e04SAndreas Gohr define('DOKU_BASE',DOKU_URL); 954b1a4e04SAndreas Gohr }else{ 964b1a4e04SAndreas Gohr define('DOKU_BASE',DOKU_REL); 974b1a4e04SAndreas Gohr } 984b1a4e04SAndreas Gohr} 994b1a4e04SAndreas Gohr 100b8595a66SAndreas Gohr// define whitespace 101b8595a66SAndreas Gohrif(!defined('DOKU_LF')) define ('DOKU_LF',"\n"); 102b8595a66SAndreas Gohrif(!defined('DOKU_TAB')) define ('DOKU_TAB',"\t"); 103ed7b5f09Sandi 104656c8fb3SAndreas Gohr// define cookie and session id, append server port when securecookie is configured FS#1664 105656c8fb3SAndreas Gohrif (!defined('DOKU_COOKIE')) define('DOKU_COOKIE', 'DW'.md5(DOKU_REL.(($conf['securecookie'])?$_SERVER['SERVER_PORT']:''))); 106e71ce681SAndreas Gohr 107ee20e7d1Sandi 108ed7b5f09Sandi// define main script 109ed7b5f09Sandiif(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php'); 110ed7b5f09Sandi 111c4766956SAndreas Gohr// DEPRECATED, use tpl_basedir() instead 1126b13307fSandiif(!defined('DOKU_TPL')) define('DOKU_TPL', 113f62ea8a1Sandi DOKU_BASE.'lib/tpl/'.$conf['template'].'/'); 1146b13307fSandi 115c4766956SAndreas Gohr// DEPRECATED, use tpl_incdir() instead 11678a6aeb1SAndreas Gohrif(!defined('DOKU_TPLINC')) define('DOKU_TPLINC', 11778a6aeb1SAndreas Gohr DOKU_INC.'lib/tpl/'.$conf['template'].'/'); 11878a6aeb1SAndreas Gohr 119ed7b5f09Sandi// make session rewrites XHTML compliant 1203fc74836Sandi@ini_set('arg_separator.output', '&'); 121ed7b5f09Sandi 122d7e6bba9SAndreas Gohr// make sure global zlib does not interfere FS#1132 123d7e6bba9SAndreas Gohr@ini_set('zlib.output_compression', 'off'); 124d7e6bba9SAndreas Gohr 1256deb5405SAndreas Gohr// increase PCRE backtrack limit 1266deb5405SAndreas Gohr@ini_set('pcre.backtrack_limit', '20971520'); 1276deb5405SAndreas Gohr 12898bda4fdSAndreas Gohr// enable gzip compression if supported 12998bda4fdSAndreas Gohr$conf['gzip_output'] &= (strpos($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip') !== false); 1303138b5c7SAndreas Gohrif ($conf['gzip_output'] && 1313138b5c7SAndreas Gohr !defined('DOKU_DISABLE_GZIP_OUTPUT') && 13298bda4fdSAndreas Gohr function_exists('ob_gzhandler')) { 1333138b5c7SAndreas Gohr ob_start('ob_gzhandler'); 1343138b5c7SAndreas Gohr} 1353138b5c7SAndreas Gohr 136ed7b5f09Sandi// init session 1376534245aSAndreas Gohrif (!headers_sent() && !defined('NOSESSION')){ 138ed7b5f09Sandi session_name("DokuWiki"); 13973ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 140f5c6743cSAndreas Gohr if (version_compare(PHP_VERSION, '5.2.0', '>')) { 14173ab87deSGabriel Birke session_set_cookie_params(0,$cookieDir,'',($conf['securecookie'] && is_ssl()),true); 142f5c6743cSAndreas Gohr }else{ 14373ab87deSGabriel Birke session_set_cookie_params(0,$cookieDir,'',($conf['securecookie'] && is_ssl())); 144f5c6743cSAndreas Gohr } 145bad31ae9SAndreas Gohr session_start(); 14614a122deSAndreas Gohr 14714a122deSAndreas Gohr // load left over messages 14814a122deSAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['msg'])){ 14914a122deSAndreas Gohr $MSG = $_SESSION[DOKU_COOKIE]['msg']; 15014a122deSAndreas Gohr unset($_SESSION[DOKU_COOKIE]['msg']); 15114a122deSAndreas Gohr } 152bad31ae9SAndreas Gohr} 153ed7b5f09Sandi 154ed7b5f09Sandi// kill magic quotes 155e55eb89cSAndreas Gohrif (get_magic_quotes_gpc() && !defined('MAGIC_QUOTES_STRIPPED')) { 156ed7b5f09Sandi if (!empty($_GET)) remove_magic_quotes($_GET); 157ed7b5f09Sandi if (!empty($_POST)) remove_magic_quotes($_POST); 158ed7b5f09Sandi if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE); 159ed7b5f09Sandi if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST); 1603fc74836Sandi @ini_set('magic_quotes_gpc', 0); 161e55eb89cSAndreas Gohr define('MAGIC_QUOTES_STRIPPED',1); 162ed7b5f09Sandi} 1633fc74836Sandi@set_magic_quotes_runtime(0); 1643fc74836Sandi@ini_set('magic_quotes_sybase',0); 165ed7b5f09Sandi 166a1637ffdSAndreas Gohr// don't let cookies ever interfere with request vars 167a1637ffdSAndreas Gohr$_REQUEST = array_merge($_GET,$_POST); 168a1637ffdSAndreas Gohr 1693dea4ebcSAndreas Gohr// we don't want a purge URL to be digged 170c66972f2SAdrian Langif(isset($_REQUEST['purge']) && $_SERVER['HTTP_REFERER']) unset($_REQUEST['purge']); 1713dea4ebcSAndreas Gohr 172ed7b5f09Sandi// disable gzip if not available 1738a447e5cSAndreas Gohrif($conf['compression'] == 'bz2' && !function_exists('bzopen')){ 174fe893490SAndreas Gohr $conf['compression'] = 'gz'; 175501252a5SAndreas Gohr} 176fe893490SAndreas Gohrif($conf['compression'] == 'gz' && !function_exists('gzopen')){ 177501252a5SAndreas Gohr $conf['compression'] = 0; 178ed7b5f09Sandi} 179ed7b5f09Sandi 180e656dcd4SAndreas Gohr// fix dateformat for upgraders 181e656dcd4SAndreas Gohrif(strpos($conf['dformat'],'%') === false){ 182e656dcd4SAndreas Gohr $conf['dformat'] = '%Y/%m/%d %H:%M'; 183e656dcd4SAndreas Gohr} 184e656dcd4SAndreas Gohr 1851ca31cfeSAndreas Gohr// precalculate file creation modes 1861ca31cfeSAndreas Gohrinit_creationmodes(); 187ed7b5f09Sandi 1883dc3a5f1Sandi// make real paths and check them 18998407a7aSandiinit_paths(); 1907367b368SAndreas Gohrinit_files(); 191ed7b5f09Sandi 192f1986589SMichael Klier// setup plugin controller class (can be overwritten in preload.php) 193a4e0e797SDominik Eckelmann$plugin_types = array('admin','syntax','action','renderer', 'helper','remote'); 194f1986589SMichael Klierglobal $plugin_controller_class, $plugin_controller; 195f1986589SMichael Klierif (empty($plugin_controller_class)) $plugin_controller_class = 'Doku_Plugin_Controller'; 196f1986589SMichael Klier 197c7cb395cSAdrian Lang// load libraries 198c7cb395cSAdrian Langrequire_once(DOKU_INC.'inc/load.php'); 199c7cb395cSAdrian Lang 200f1986589SMichael Klier// initialize plugin controller 201f1986589SMichael Klier$plugin_controller = new $plugin_controller_class(); 202f1986589SMichael Klier 203f1986589SMichael Klier// initialize the event handler 204f1986589SMichael Klierglobal $EVENT_HANDLER; 205f1986589SMichael Klier$EVENT_HANDLER = new Doku_Event_Handler(); 206f1986589SMichael Klier 2076d06b26aSDominik Eckelmann$local = $conf['lang']; 2086d06b26aSDominik Eckelmanntrigger_event('INIT_LANG_LOAD', $local, 'init_lang', true); 2096d06b26aSDominik Eckelmann 2106d06b26aSDominik Eckelmann 21116905344SAndreas Gohr// setup authentication system 212c7cb395cSAdrian Langif (!defined('NOSESSION')) { 21316905344SAndreas Gohr auth_setup(); 214c7cb395cSAdrian Lang} 215f62ea8a1Sandi 2165ec3fefcSAndreas Gohr// setup mail system 2175ec3fefcSAndreas Gohrmail_setup(); 2185ec3fefcSAndreas Gohr 219f62ea8a1Sandi/** 22098407a7aSandi * Checks paths from config file 22198407a7aSandi */ 22298407a7aSandifunction init_paths(){ 22398407a7aSandi global $conf; 22498407a7aSandi 22598407a7aSandi $paths = array('datadir' => 'pages', 22698407a7aSandi 'olddir' => 'attic', 22798407a7aSandi 'mediadir' => 'media', 228e4f389efSKate Arzamastseva 'mediaolddir' => 'media_attic', 22998407a7aSandi 'metadir' => 'meta', 230e4f389efSKate Arzamastseva 'mediametadir' => 'media_meta', 23198407a7aSandi 'cachedir' => 'cache', 232579b0f7eSTNHarris 'indexdir' => 'index', 233de33a58fSMichael Klier 'lockdir' => 'locks', 234de33a58fSMichael Klier 'tmpdir' => 'tmp'); 23598407a7aSandi 23698407a7aSandi foreach($paths as $c => $p){ 237*6b9c156cSAnika Henke $path = $conf[$c]; 238*6b9c156cSAnika Henke if(empty($path)) 239*6b9c156cSAnika Henke $path = $conf['savedir'].'/'.$p; 240*6b9c156cSAnika Henke $conf[$c] = init_path($path); 241*6b9c156cSAnika Henke if(empty($conf[$c])) 242*6b9c156cSAnika Henke nice_die("The $c ('$p') at $path is not found, isn't accessible or writable. 24369dc3177SAndreas Gohr You should check your config and permission settings. 24469dc3177SAndreas Gohr Or maybe you want to <a href=\"install.php\">run the 24569dc3177SAndreas Gohr installer</a>?"); 24698407a7aSandi } 24771726d78SBen Coburn 24871726d78SBen Coburn // path to old changelog only needed for upgrading 24971726d78SBen Coburn $conf['changelog_old'] = init_path((isset($conf['changelog']))?($conf['changelog']):($conf['savedir'].'/changes.log')); 25071726d78SBen Coburn if ($conf['changelog_old']=='') { unset($conf['changelog_old']); } 25171726d78SBen Coburn // hardcoded changelog because it is now a cache that lives in meta 25271726d78SBen Coburn $conf['changelog'] = $conf['metadir'].'/_dokuwiki.changes'; 25399c8d7f2Smichael $conf['media_changelog'] = $conf['metadir'].'/_media.changes'; 25498407a7aSandi} 25598407a7aSandi 2566d06b26aSDominik Eckelmannfunction init_lang($langCode) { 2576d06b26aSDominik Eckelmann //prepare language array 2586d06b26aSDominik Eckelmann global $lang; 2596d06b26aSDominik Eckelmann $lang = array(); 2606d06b26aSDominik Eckelmann 2616d06b26aSDominik Eckelmann //load the language files 2626d06b26aSDominik Eckelmann require_once(DOKU_INC.'inc/lang/en/lang.php'); 2636d06b26aSDominik Eckelmann if ($langCode && $langCode != 'en') { 2646d06b26aSDominik Eckelmann if (file_exists(DOKU_INC."inc/lang/$langCode/lang.php")) { 2656d06b26aSDominik Eckelmann require_once(DOKU_INC."inc/lang/$langCode/lang.php"); 2666d06b26aSDominik Eckelmann } 2676d06b26aSDominik Eckelmann } 2686d06b26aSDominik Eckelmann} 2696d06b26aSDominik Eckelmann 27098407a7aSandi/** 271*6b9c156cSAnika Henke * Checks the existence of certain files and creates them if missing. 2727367b368SAndreas Gohr */ 2737367b368SAndreas Gohrfunction init_files(){ 2747367b368SAndreas Gohr global $conf; 2750d8850c4SAndreas Gohr 276345b1674SAndreas Gohr $files = array($conf['indexdir'].'/page.idx'); 2777367b368SAndreas Gohr 2787367b368SAndreas Gohr foreach($files as $file){ 2797367b368SAndreas Gohr if(!@file_exists($file)){ 2800d8850c4SAndreas Gohr $fh = @fopen($file,'a'); 2810d8850c4SAndreas Gohr if($fh){ 2827367b368SAndreas Gohr fclose($fh); 2831ca31cfeSAndreas Gohr if($conf['fperm']) chmod($file, $conf['fperm']); 2840d8850c4SAndreas Gohr }else{ 2853816dcbcSAndreas Gohr nice_die("$file is not writable. Check your permissions settings!"); 2860d8850c4SAndreas Gohr } 2877367b368SAndreas Gohr } 2887367b368SAndreas Gohr } 289345b1674SAndreas Gohr 290345b1674SAndreas Gohr # create title index (needs to have same length as page.idx) 2919b41be24STom N Harris /* 292345b1674SAndreas Gohr $file = $conf['indexdir'].'/title.idx'; 293345b1674SAndreas Gohr if(!@file_exists($file)){ 294345b1674SAndreas Gohr $pages = file($conf['indexdir'].'/page.idx'); 295345b1674SAndreas Gohr $pages = count($pages); 296345b1674SAndreas Gohr $fh = @fopen($file,'a'); 297345b1674SAndreas Gohr if($fh){ 298345b1674SAndreas Gohr for($i=0; $i<$pages; $i++){ 299345b1674SAndreas Gohr fwrite($fh,"\n"); 300345b1674SAndreas Gohr } 301345b1674SAndreas Gohr fclose($fh); 302345b1674SAndreas Gohr }else{ 303345b1674SAndreas Gohr nice_die("$file is not writable. Check your permissions settings!"); 304345b1674SAndreas Gohr } 305345b1674SAndreas Gohr } 3069b41be24STom N Harris */ 3077367b368SAndreas Gohr} 3087367b368SAndreas Gohr 3097367b368SAndreas Gohr/** 3100d8850c4SAndreas Gohr * Returns absolute path 311f62ea8a1Sandi * 3120d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC. 3130d8850c4SAndreas Gohr * Check for accessability on directories as well. 3140d8850c4SAndreas Gohr * 3150d8850c4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 316f62ea8a1Sandi */ 317f62ea8a1Sandifunction init_path($path){ 318*6b9c156cSAnika Henke // check existence 31900976812SAndreas Gohr $p = fullpath($path); 3200d8850c4SAndreas Gohr if(!@file_exists($p)){ 32100976812SAndreas Gohr $p = fullpath(DOKU_INC.$path); 3220d8850c4SAndreas Gohr if(!@file_exists($p)){ 3238fc4e739Sandi return ''; 324f62ea8a1Sandi } 3250d8850c4SAndreas Gohr } 3260d8850c4SAndreas Gohr 3270d8850c4SAndreas Gohr // check writability 3280d8850c4SAndreas Gohr if(!@is_writable($p)){ 3290d8850c4SAndreas Gohr return ''; 3300d8850c4SAndreas Gohr } 3310d8850c4SAndreas Gohr 3320d8850c4SAndreas Gohr // check accessability (execute bit) for directories 3330d8850c4SAndreas Gohr if(@is_dir($p) && !@file_exists("$p/.")){ 3340d8850c4SAndreas Gohr return ''; 3350d8850c4SAndreas Gohr } 3360d8850c4SAndreas Gohr 3370d8850c4SAndreas Gohr return $p; 3380d8850c4SAndreas Gohr} 3398c4f28e8Sjan 340ed7b5f09Sandi/** 3411ca31cfeSAndreas Gohr * Sets the internal config values fperm and dperm which, when set, 3421ca31cfeSAndreas Gohr * will be used to change the permission of a newly created dir or 3431ca31cfeSAndreas Gohr * file with chmod. Considers the influence of the system's umask 3441ca31cfeSAndreas Gohr * setting the values only if needed. 3451ca31cfeSAndreas Gohr */ 3461ca31cfeSAndreas Gohrfunction init_creationmodes(){ 3471ca31cfeSAndreas Gohr global $conf; 3481ca31cfeSAndreas Gohr 3491ca31cfeSAndreas Gohr // Legacy support for old umask/dmask scheme 3501ca31cfeSAndreas Gohr unset($conf['dmask']); 3511ca31cfeSAndreas Gohr unset($conf['fmask']); 3521ca31cfeSAndreas Gohr unset($conf['umask']); 3531ca31cfeSAndreas Gohr unset($conf['fperm']); 3541ca31cfeSAndreas Gohr unset($conf['dperm']); 3551ca31cfeSAndreas Gohr 3569f3cdec3SAndreas Gohr // get system umask, fallback to 0 if none available 3579f3cdec3SAndreas Gohr $umask = @umask(); 3589f3cdec3SAndreas Gohr if(!$umask) $umask = 0000; 3591ca31cfeSAndreas Gohr 3601ca31cfeSAndreas Gohr // check what is set automatically by the system on file creation 3611ca31cfeSAndreas Gohr // and set the fperm param if it's not what we want 3621ca31cfeSAndreas Gohr $auto_fmode = 0666 & ~$umask; 3631ca31cfeSAndreas Gohr if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode']; 3641ca31cfeSAndreas Gohr 3651ca31cfeSAndreas Gohr // check what is set automatically by the system on file creation 3661ca31cfeSAndreas Gohr // and set the dperm param if it's not what we want 3671ca31cfeSAndreas Gohr $auto_dmode = $conf['dmode'] & ~$umask; 3681ca31cfeSAndreas Gohr if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode']; 3691ca31cfeSAndreas Gohr} 3701ca31cfeSAndreas Gohr 3711ca31cfeSAndreas Gohr/** 372ed7b5f09Sandi * remove magic quotes recursivly 373ed7b5f09Sandi * 374ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org> 375ed7b5f09Sandi */ 376ed7b5f09Sandifunction remove_magic_quotes(&$array) { 377ed7b5f09Sandi foreach (array_keys($array) as $key) { 37881c54349SAndreas Gohr // handle magic quotes in keynames (breaks order) 37981c54349SAndreas Gohr $sk = stripslashes($key); 38081c54349SAndreas Gohr if($sk != $key){ 38181c54349SAndreas Gohr $array[$sk] = $array[$key]; 38281c54349SAndreas Gohr unset($array[$key]); 38381c54349SAndreas Gohr $key = $sk; 38481c54349SAndreas Gohr } 38581c54349SAndreas Gohr 38681c54349SAndreas Gohr // do recursion if needed 387ed7b5f09Sandi if (is_array($array[$key])) { 388ed7b5f09Sandi remove_magic_quotes($array[$key]); 389ed7b5f09Sandi }else { 390ed7b5f09Sandi $array[$key] = stripslashes($array[$key]); 391ed7b5f09Sandi } 392ed7b5f09Sandi } 393ed7b5f09Sandi} 394ed7b5f09Sandi 395ed7b5f09Sandi/** 396ed7b5f09Sandi * Returns the full absolute URL to the directory where 397ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash) 398ed7b5f09Sandi * 399ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org> 400ed7b5f09Sandi */ 4014b1a4e04SAndreas Gohrfunction getBaseURL($abs=null){ 402ed7b5f09Sandi global $conf; 403ed7b5f09Sandi //if canonical url enabled always return absolute 4044b1a4e04SAndreas Gohr if(is_null($abs)) $abs = $conf['canonical']; 405ed7b5f09Sandi 40692b83b77Sandi if($conf['basedir']){ 40746c73e01SChris Smith $dir = $conf['basedir']; 40889aa05dbSAndreas Gohr }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){ 40946c73e01SChris Smith $dir = dirname($_SERVER['SCRIPT_NAME']); 41089aa05dbSAndreas Gohr }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){ 41146c73e01SChris Smith $dir = dirname($_SERVER['PHP_SELF']); 412093ec9e4Sandi }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){ 413093ec9e4Sandi $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','', 414093ec9e4Sandi $_SERVER['SCRIPT_FILENAME']); 41546c73e01SChris Smith $dir = dirname('/'.$dir); 41692b83b77Sandi }else{ 41746c73e01SChris Smith $dir = '.'; //probably wrong 41892b83b77Sandi } 419ed7b5f09Sandi 42046c73e01SChris Smith $dir = str_replace('\\','/',$dir); // bugfix for weird WIN behaviour 42146c73e01SChris Smith $dir = preg_replace('#//+#','/',"/$dir/"); // ensure leading and trailing slashes 422ed7b5f09Sandi 423f62ea8a1Sandi //handle script in lib/exe dir 424f62ea8a1Sandi $dir = preg_replace('!lib/exe/$!','',$dir); 425f62ea8a1Sandi 426488d5fa0SMichael Klier chi@chimeric.de //handle script in lib/plugins dir 427488d5fa0SMichael Klier chi@chimeric.de $dir = preg_replace('!lib/plugins/.*$!','',$dir); 428488d5fa0SMichael Klier chi@chimeric.de 429ed7b5f09Sandi //finish here for relative URLs 430ed7b5f09Sandi if(!$abs) return $dir; 431ed7b5f09Sandi 43246c73e01SChris Smith //use config option if available, trim any slash from end of baseurl to avoid multiple consecutive slashes in the path 43346c73e01SChris Smith if($conf['baseurl']) return rtrim($conf['baseurl'],'/').$dir; 434ef7b3ecdSAndreas Gohr 435e82e3526SAndreas Gohr //split hostheader into host and port 4365627186cSAndreas Gohr if(isset($_SERVER['HTTP_HOST'])){ 437204b27c8SMichael Hamann $parsed_host = parse_url('http://'.$_SERVER['HTTP_HOST']); 438204b27c8SMichael Hamann $host = $parsed_host['host']; 439204b27c8SMichael Hamann $port = $parsed_host['port']; 4405627186cSAndreas Gohr }elseif(isset($_SERVER['SERVER_NAME'])){ 441204b27c8SMichael Hamann $parsed_host = parse_url('http://'.$_SERVER['SERVER_NAME']); 442204b27c8SMichael Hamann $host = $parsed_host['host']; 443204b27c8SMichael Hamann $port = $parsed_host['port']; 4445627186cSAndreas Gohr }else{ 4455627186cSAndreas Gohr $host = php_uname('n'); 446c66972f2SAdrian Lang $port = ''; 4475627186cSAndreas Gohr } 4485627186cSAndreas Gohr 4495627186cSAndreas Gohr if(!$port && isset($_SERVER['SERVER_PORT'])) { 450c66972f2SAdrian Lang $port = $_SERVER['SERVER_PORT']; 451c66972f2SAdrian Lang } 452204b27c8SMichael Hamann 453204b27c8SMichael Hamann if(is_null($port)){ 454204b27c8SMichael Hamann $port = ''; 455204b27c8SMichael Hamann } 456204b27c8SMichael Hamann 457f5c6743cSAndreas Gohr if(!is_ssl()){ 458ed7b5f09Sandi $proto = 'http://'; 459e82e3526SAndreas Gohr if ($port == '80') { 460ed7b5f09Sandi $port = ''; 461ed7b5f09Sandi } 462ed7b5f09Sandi }else{ 463ed7b5f09Sandi $proto = 'https://'; 464e82e3526SAndreas Gohr if ($port == '443') { 465ed7b5f09Sandi $port = ''; 466ed7b5f09Sandi } 467ed7b5f09Sandi } 468ed7b5f09Sandi 469c66972f2SAdrian Lang if($port !== '') $port = ':'.$port; 470e82e3526SAndreas Gohr 471ed7b5f09Sandi return $proto.$host.$port.$dir; 472ed7b5f09Sandi} 473ed7b5f09Sandi 474b000c6d4Sandi/** 475f5c6743cSAndreas Gohr * Check if accessed via HTTPS 476f5c6743cSAndreas Gohr * 477f5c6743cSAndreas Gohr * Apache leaves ,$_SERVER['HTTPS'] empty when not available, IIS sets it to 'off'. 478f5c6743cSAndreas Gohr * 'false' and 'disabled' are just guessing 479f5c6743cSAndreas Gohr * 480f5c6743cSAndreas Gohr * @returns bool true when SSL is active 481f5c6743cSAndreas Gohr */ 482f5c6743cSAndreas Gohrfunction is_ssl(){ 483c66972f2SAdrian Lang if (!isset($_SERVER['HTTPS']) || 484c66972f2SAdrian Lang preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){ 485f5c6743cSAndreas Gohr return false; 486f5c6743cSAndreas Gohr }else{ 487f5c6743cSAndreas Gohr return true; 488f5c6743cSAndreas Gohr } 489f5c6743cSAndreas Gohr} 490f5c6743cSAndreas Gohr 491f5c6743cSAndreas Gohr/** 4923816dcbcSAndreas Gohr * print a nice message even if no styles are loaded yet. 4933816dcbcSAndreas Gohr */ 4943816dcbcSAndreas Gohrfunction nice_die($msg){ 4953816dcbcSAndreas Gohr echo<<<EOT 4963816dcbcSAndreas Gohr<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 4973816dcbcSAndreas Gohr "http://www.w3.org/TR/html4/loose.dtd"> 4983816dcbcSAndreas Gohr<html> 4993816dcbcSAndreas Gohr<head><title>DokuWiki Setup Error</title></head> 5003816dcbcSAndreas Gohr<body style="font-family: Arial, sans-serif"> 5013816dcbcSAndreas Gohr <div style="width:60%; margin: auto; background-color: #fcc; 5023816dcbcSAndreas Gohr border: 1px solid #faa; padding: 0.5em 1em;"> 5033816dcbcSAndreas Gohr <h1 style="font-size: 120%">DokuWiki Setup Error</h1> 5043816dcbcSAndreas Gohr <p>$msg</p> 5053816dcbcSAndreas Gohr </div> 5063816dcbcSAndreas Gohr</body> 5073816dcbcSAndreas Gohr</html> 5083816dcbcSAndreas GohrEOT; 5093816dcbcSAndreas Gohr exit; 5103816dcbcSAndreas Gohr} 5113816dcbcSAndreas Gohr 51200976812SAndreas Gohr/** 51300976812SAndreas Gohr * A realpath() replacement 51400976812SAndreas Gohr * 51500976812SAndreas Gohr * This function behaves similar to PHP's realpath() but does not resolve 51600976812SAndreas Gohr * symlinks or accesses upper directories 51700976812SAndreas Gohr * 5184761d30cSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 51900976812SAndreas Gohr * @author <richpageau at yahoo dot co dot uk> 52000976812SAndreas Gohr * @link http://de3.php.net/manual/en/function.realpath.php#75992 52100976812SAndreas Gohr */ 522b328697dSAndreas Gohrfunction fullpath($path,$exists=false){ 5234761d30cSAndreas Gohr static $run = 0; 5244761d30cSAndreas Gohr $root = ''; 525f0a201c5SChris Smith $iswin = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' || @$GLOBALS['DOKU_UNITTEST_ASSUME_WINDOWS']); 52600976812SAndreas Gohr 5274761d30cSAndreas Gohr // find the (indestructable) root of the path - keeps windows stuff intact 5284761d30cSAndreas Gohr if($path{0} == '/'){ 5294761d30cSAndreas Gohr $root = '/'; 5304761d30cSAndreas Gohr }elseif($iswin){ 5314761d30cSAndreas Gohr // match drive letter and UNC paths 5324761d30cSAndreas Gohr if(preg_match('!^([a-zA-z]:)(.*)!',$path,$match)){ 533b9c4302bSAndreas Gohr $root = $match[1].'/'; 5344761d30cSAndreas Gohr $path = $match[2]; 5354761d30cSAndreas Gohr }else if(preg_match('!^(\\\\\\\\[^\\\\/]+\\\\[^\\\\/]+[\\\\/])(.*)!',$path,$match)){ 5364761d30cSAndreas Gohr $root = $match[1]; 5374761d30cSAndreas Gohr $path = $match[2]; 53800976812SAndreas Gohr } 5394761d30cSAndreas Gohr } 5404761d30cSAndreas Gohr $path = str_replace('\\','/',$path); 5414761d30cSAndreas Gohr 5424761d30cSAndreas Gohr // if the given path wasn't absolute already, prepend the script path and retry 5434761d30cSAndreas Gohr if(!$root){ 5444761d30cSAndreas Gohr $base = dirname($_SERVER['SCRIPT_FILENAME']); 5454761d30cSAndreas Gohr $path = $base.'/'.$path; 5464761d30cSAndreas Gohr if($run == 0){ // avoid endless recursion when base isn't absolute for some reason 5474761d30cSAndreas Gohr $run++; 548b328697dSAndreas Gohr return fullpath($path,$exists); 5494761d30cSAndreas Gohr } 5504761d30cSAndreas Gohr } 5514761d30cSAndreas Gohr $run = 0; 55200976812SAndreas Gohr 55300976812SAndreas Gohr // canonicalize 55400976812SAndreas Gohr $path=explode('/', $path); 55500976812SAndreas Gohr $newpath=array(); 556ef38bfe8SAndreas Gohr foreach($path as $p) { 557ef38bfe8SAndreas Gohr if ($p === '' || $p === '.') continue; 558ef38bfe8SAndreas Gohr if ($p==='..') { 55900976812SAndreas Gohr array_pop($newpath); 56000976812SAndreas Gohr continue; 56100976812SAndreas Gohr } 562ef38bfe8SAndreas Gohr array_push($newpath, $p); 56300976812SAndreas Gohr } 5644761d30cSAndreas Gohr $finalpath = $root.implode('/', $newpath); 56500976812SAndreas Gohr 566*6b9c156cSAnika Henke // check for existence when needed (except when unit testing) 567b328697dSAndreas Gohr if($exists && !defined('DOKU_UNITTEST') && !@file_exists($finalpath)) { 5684761d30cSAndreas Gohr return false; 56900976812SAndreas Gohr } 5704761d30cSAndreas Gohr return $finalpath; 57100976812SAndreas Gohr} 57200976812SAndreas Gohr 573