1ed7b5f09Sandi<?php 2ed7b5f09Sandi/** 3ed7b5f09Sandi * Initialize some defaults needed for DokuWiki 4ed7b5f09Sandi */ 5ed7b5f09Sandi 630085ef3SYurii K 73272d797SAndreas Gohr/** 83272d797SAndreas Gohr * timing Dokuwiki execution 9f50a239bSTakamura * 10f50a239bSTakamura * @param integer $start 11f50a239bSTakamura * 12f50a239bSTakamura * @return mixed 133272d797SAndreas Gohr */ 14a609a9ccSBen Coburnfunction delta_time($start=0) { 15ac4be4d7SPiyush Mishra return microtime(true)-((float)$start); 16a609a9ccSBen Coburn} 17a609a9ccSBen Coburndefine('DOKU_START_TIME', delta_time()); 18a609a9ccSBen Coburn 19ccaeaa85SAndreas Gohrglobal $config_cascade; 20cca94fbcSRoland Hager$config_cascade = array(); 21ccaeaa85SAndreas Gohr 2248beefecSAndreas Gohr// if available load a preload config file 23e6266454SChris Smith$preload = fullpath(dirname(__FILE__)).'/preload.php'; 2479e79377SAndreas Gohrif (file_exists($preload)) include($preload); 2548beefecSAndreas Gohr 26ed7b5f09Sandi// define the include path 2700976812SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../').'/'); 28ad15db82Sandi 29c2a6d816SAndreas Gohr// define Plugin dir 30c2a6d816SAndreas Gohrif(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 31c2a6d816SAndreas Gohr 32e7cb32dcSAndreas Gohr// define config path (packagers may want to change this to /etc/dokuwiki/) 33b7551a6dSEsther Brunnerif(!defined('DOKU_CONF')) define('DOKU_CONF',DOKU_INC.'conf/'); 34e7cb32dcSAndreas Gohr 35bad905f1SBen Coburn// check for error reporting override or set error reporting to sane values 3679e79377SAndreas Gohrif (!defined('DOKU_E_LEVEL') && file_exists(DOKU_CONF.'report_e_all')) { 37bad905f1SBen Coburn define('DOKU_E_LEVEL', E_ALL); 38bad905f1SBen Coburn} 39fc80ed59SAndreas Gohrif (!defined('DOKU_E_LEVEL')) { 404fcd684aSMichael Hamann error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT); 41fc80ed59SAndreas Gohr} else { 42fc80ed59SAndreas Gohr error_reporting(DOKU_E_LEVEL); 43fc80ed59SAndreas Gohr} 44c53ea5f2Sandi 45a69722b3SAndreas Gohr// avoid caching issues #1594 46a69722b3SAndreas Gohrheader('Vary: Cookie'); 47a69722b3SAndreas Gohr 4850602150SBen Coburn// init memory caches 49db959ae3SAndreas Gohrglobal $cache_revinfo; 50db959ae3SAndreas Gohr $cache_revinfo = array(); 51db959ae3SAndreas Gohrglobal $cache_wikifn; 52db959ae3SAndreas Gohr $cache_wikifn = array(); 53db959ae3SAndreas Gohrglobal $cache_cleanid; 54db959ae3SAndreas Gohr $cache_cleanid = array(); 55db959ae3SAndreas Gohrglobal $cache_authname; 56db959ae3SAndreas Gohr $cache_authname = array(); 57db959ae3SAndreas Gohrglobal $cache_metadata; 58db959ae3SAndreas Gohr $cache_metadata = array(); 5950602150SBen Coburn 60cca94fbcSRoland Hager// always include 'inc/config_cascade.php' 61cca94fbcSRoland Hager// previously in preload.php set fields of $config_cascade will be merged with the defaults 62e6a6dbfeSAndreas Gohrinclude(DOKU_INC.'inc/config_cascade.php'); 63cb043f52SChris Smith 644724a577Sandi//prepare config array() 65ee20e7d1Sandiglobal $conf; 664724a577Sandi$conf = array(); 674724a577Sandi 68cb043f52SChris Smith// load the global config file(s) 69b303b92cSChris Smithforeach (array('default','local','protected') as $config_group) { 70f8121585SChris Smith if (empty($config_cascade['main'][$config_group])) continue; 71b303b92cSChris Smith foreach ($config_cascade['main'][$config_group] as $config_file) { 7279e79377SAndreas Gohr if (file_exists($config_file)) { 73f8121585SChris Smith include($config_file); 74f8121585SChris Smith } 75cb043f52SChris Smith } 760a6ead41SAndreas Gohr} 77ad15db82Sandi 78066fee30SAndreas Gohr//prepare license array() 79066fee30SAndreas Gohrglobal $license; 80066fee30SAndreas Gohr$license = array(); 81066fee30SAndreas Gohr 82066fee30SAndreas Gohr// load the license file(s) 83f8121585SChris Smithforeach (array('default','local') as $config_group) { 84f8121585SChris Smith if (empty($config_cascade['license'][$config_group])) continue; 85f8121585SChris Smith foreach ($config_cascade['license'][$config_group] as $config_file) { 8679e79377SAndreas Gohr if(file_exists($config_file)){ 87f8121585SChris Smith include($config_file); 88f8121585SChris Smith } 89f8121585SChris Smith } 90066fee30SAndreas Gohr} 91066fee30SAndreas Gohr 921f8eb24fSAndreas Gohr// set timezone (as in pre 5.3.0 days) 931f8eb24fSAndreas Gohrdate_default_timezone_set(@date_default_timezone_get()); 941f8eb24fSAndreas Gohr 95ed7b5f09Sandi// define baseURL 964b1a4e04SAndreas Gohrif(!defined('DOKU_REL')) define('DOKU_REL',getBaseURL(false)); 97ed7b5f09Sandiif(!defined('DOKU_URL')) define('DOKU_URL',getBaseURL(true)); 984b1a4e04SAndreas Gohrif(!defined('DOKU_BASE')){ 994b1a4e04SAndreas Gohr if($conf['canonical']){ 1004b1a4e04SAndreas Gohr define('DOKU_BASE',DOKU_URL); 1014b1a4e04SAndreas Gohr }else{ 1024b1a4e04SAndreas Gohr define('DOKU_BASE',DOKU_REL); 1034b1a4e04SAndreas Gohr } 1044b1a4e04SAndreas Gohr} 1054b1a4e04SAndreas Gohr 106b8595a66SAndreas Gohr// define whitespace 107b4f2363aSAndreas Gohrif(!defined('NL')) define ('NL',"\n"); 108b8595a66SAndreas Gohrif(!defined('DOKU_LF')) define ('DOKU_LF',"\n"); 109b8595a66SAndreas Gohrif(!defined('DOKU_TAB')) define ('DOKU_TAB',"\t"); 110ed7b5f09Sandi 111656c8fb3SAndreas Gohr// define cookie and session id, append server port when securecookie is configured FS#1664 112*64159a61SAndreas Gohrif(!defined('DOKU_COOKIE')) define( 113*64159a61SAndreas Gohr 'DOKU_COOKIE', 114*64159a61SAndreas Gohr 'DW' . md5(DOKU_REL . (($conf['securecookie']) ? $_SERVER['SERVER_PORT'] : '')) 115*64159a61SAndreas Gohr); 116e71ce681SAndreas Gohr 117ee20e7d1Sandi 118ed7b5f09Sandi// define main script 119ed7b5f09Sandiif(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php'); 120ed7b5f09Sandi 121c4766956SAndreas Gohr// DEPRECATED, use tpl_basedir() instead 1226b13307fSandiif(!defined('DOKU_TPL')) define('DOKU_TPL', 123f62ea8a1Sandi DOKU_BASE.'lib/tpl/'.$conf['template'].'/'); 1246b13307fSandi 125c4766956SAndreas Gohr// DEPRECATED, use tpl_incdir() instead 12678a6aeb1SAndreas Gohrif(!defined('DOKU_TPLINC')) define('DOKU_TPLINC', 12778a6aeb1SAndreas Gohr DOKU_INC.'lib/tpl/'.$conf['template'].'/'); 12878a6aeb1SAndreas Gohr 129ed7b5f09Sandi// make session rewrites XHTML compliant 1303fc74836Sandi@ini_set('arg_separator.output', '&'); 131ed7b5f09Sandi 132d7e6bba9SAndreas Gohr// make sure global zlib does not interfere FS#1132 133d7e6bba9SAndreas Gohr@ini_set('zlib.output_compression', 'off'); 134d7e6bba9SAndreas Gohr 1356deb5405SAndreas Gohr// increase PCRE backtrack limit 1366deb5405SAndreas Gohr@ini_set('pcre.backtrack_limit', '20971520'); 1376deb5405SAndreas Gohr 13898bda4fdSAndreas Gohr// enable gzip compression if supported 13998bda4fdSAndreas Gohr$conf['gzip_output'] &= (strpos($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip') !== false); 14065f6e7d6SMichael Hamannglobal $ACT; 1413138b5c7SAndreas Gohrif ($conf['gzip_output'] && 1423138b5c7SAndreas Gohr !defined('DOKU_DISABLE_GZIP_OUTPUT') && 14365f6e7d6SMichael Hamann function_exists('ob_gzhandler') && 14499e10b7fSMichael Hamann // Disable compression when a (compressed) sitemap might be delivered 14565f6e7d6SMichael Hamann // See https://bugs.dokuwiki.org/index.php?do=details&task_id=2576 14699e10b7fSMichael Hamann $ACT != 'sitemap') { 1473138b5c7SAndreas Gohr ob_start('ob_gzhandler'); 1483138b5c7SAndreas Gohr} 1493138b5c7SAndreas Gohr 150ed7b5f09Sandi// init session 1516534245aSAndreas Gohrif(!headers_sent() && !defined('NOSESSION')) { 152c09f0eb1SGerrit Uitslag if(!defined('DOKU_SESSION_NAME')) define ('DOKU_SESSION_NAME', "DokuWiki"); 153c09f0eb1SGerrit Uitslag if(!defined('DOKU_SESSION_LIFETIME')) define ('DOKU_SESSION_LIFETIME', 0); 15455a71a16SGerrit Uitslag if(!defined('DOKU_SESSION_PATH')) { 15573ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 15655a71a16SGerrit Uitslag define ('DOKU_SESSION_PATH', $cookieDir); 157f5c6743cSAndreas Gohr } 158c09f0eb1SGerrit Uitslag if(!defined('DOKU_SESSION_DOMAIN')) define ('DOKU_SESSION_DOMAIN', ''); 159c09f0eb1SGerrit Uitslag 1606eb3cdf6SAndreas Gohr // start the session 1616eb3cdf6SAndreas Gohr init_session(); 16214a122deSAndreas Gohr 16314a122deSAndreas Gohr // load left over messages 16414a122deSAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['msg'])) { 16514a122deSAndreas Gohr $MSG = $_SESSION[DOKU_COOKIE]['msg']; 16614a122deSAndreas Gohr unset($_SESSION[DOKU_COOKIE]['msg']); 16714a122deSAndreas Gohr } 168bad31ae9SAndreas Gohr} 169ed7b5f09Sandi 170a1637ffdSAndreas Gohr// don't let cookies ever interfere with request vars 171a1637ffdSAndreas Gohr$_REQUEST = array_merge($_GET,$_POST); 172a1637ffdSAndreas Gohr 1733dea4ebcSAndreas Gohr// we don't want a purge URL to be digged 1740e80bb5eSChristopher Smithif(isset($_REQUEST['purge']) && !empty($_SERVER['HTTP_REFERER'])) unset($_REQUEST['purge']); 1753dea4ebcSAndreas Gohr 1761ca31cfeSAndreas Gohr// precalculate file creation modes 1771ca31cfeSAndreas Gohrinit_creationmodes(); 178ed7b5f09Sandi 1793dc3a5f1Sandi// make real paths and check them 18098407a7aSandiinit_paths(); 1817367b368SAndreas Gohrinit_files(); 182ed7b5f09Sandi 183f1986589SMichael Klier// setup plugin controller class (can be overwritten in preload.php) 18493a7873eSAndreas Gohr$plugin_types = array('auth', 'admin','syntax','action','renderer', 'helper','remote'); 185f1986589SMichael Klierglobal $plugin_controller_class, $plugin_controller; 186f1986589SMichael Klierif (empty($plugin_controller_class)) $plugin_controller_class = 'Doku_Plugin_Controller'; 187f1986589SMichael Klier 188c7cb395cSAdrian Lang// load libraries 189605f8e8dSAndreas Gohrrequire_once(DOKU_INC.'vendor/autoload.php'); 190c7cb395cSAdrian Langrequire_once(DOKU_INC.'inc/load.php'); 191c7cb395cSAdrian Lang 1920f8f7aaaSDanny Lin// disable gzip if not available 19313c37900SAndreas Gohrdefine('DOKU_HAS_BZIP', function_exists('bzopen')); 19413c37900SAndreas Gohrdefine('DOKU_HAS_GZIP', function_exists('gzopen')); 19513c37900SAndreas Gohrif($conf['compression'] == 'bz2' && !DOKU_HAS_BZIP) { 1960f8f7aaaSDanny Lin $conf['compression'] = 'gz'; 1970f8f7aaaSDanny Lin} 19813c37900SAndreas Gohrif($conf['compression'] == 'gz' && !DOKU_HAS_GZIP) { 1990f8f7aaaSDanny Lin $conf['compression'] = 0; 2000f8f7aaaSDanny Lin} 2010f8f7aaaSDanny Lin 20289177306SAndreas Gohr// input handle class 20389177306SAndreas Gohrglobal $INPUT; 20489177306SAndreas Gohr$INPUT = new Input(); 20589177306SAndreas Gohr 206f1986589SMichael Klier// initialize plugin controller 207f1986589SMichael Klier$plugin_controller = new $plugin_controller_class(); 208f1986589SMichael Klier 209f1986589SMichael Klier// initialize the event handler 210f1986589SMichael Klierglobal $EVENT_HANDLER; 211f1986589SMichael Klier$EVENT_HANDLER = new Doku_Event_Handler(); 212f1986589SMichael Klier 2136d06b26aSDominik Eckelmann$local = $conf['lang']; 2146d06b26aSDominik Eckelmanntrigger_event('INIT_LANG_LOAD', $local, 'init_lang', true); 2156d06b26aSDominik Eckelmann 2166d06b26aSDominik Eckelmann 21716905344SAndreas Gohr// setup authentication system 218c7cb395cSAdrian Langif (!defined('NOSESSION')) { 21916905344SAndreas Gohr auth_setup(); 220c7cb395cSAdrian Lang} 221f62ea8a1Sandi 2225ec3fefcSAndreas Gohr// setup mail system 2235ec3fefcSAndreas Gohrmail_setup(); 2245ec3fefcSAndreas Gohr 225f62ea8a1Sandi/** 2266eb3cdf6SAndreas Gohr * Initializes the session 2276eb3cdf6SAndreas Gohr * 2286eb3cdf6SAndreas Gohr * Makes sure the passed session cookie is valid, invalid ones are ignored an a new session ID is issued 2296eb3cdf6SAndreas Gohr * 2306eb3cdf6SAndreas Gohr * @link http://stackoverflow.com/a/33024310/172068 231924e477eSAndreas Gohr * @link http://php.net/manual/en/session.configuration.php#ini.session.sid-length 2326eb3cdf6SAndreas Gohr */ 2336eb3cdf6SAndreas Gohrfunction init_session() { 2346eb3cdf6SAndreas Gohr global $conf; 2356eb3cdf6SAndreas Gohr session_name(DOKU_SESSION_NAME); 236*64159a61SAndreas Gohr session_set_cookie_params( 237*64159a61SAndreas Gohr DOKU_SESSION_LIFETIME, 238*64159a61SAndreas Gohr DOKU_SESSION_PATH, 239*64159a61SAndreas Gohr DOKU_SESSION_DOMAIN, 240*64159a61SAndreas Gohr ($conf['securecookie'] && is_ssl()), 241*64159a61SAndreas Gohr true 242*64159a61SAndreas Gohr ); 2436eb3cdf6SAndreas Gohr 2446eb3cdf6SAndreas Gohr // make sure the session cookie contains a valid session ID 245924e477eSAndreas Gohr if(isset($_COOKIE[DOKU_SESSION_NAME]) && !preg_match('/^[-,a-zA-Z0-9]{22,256}$/', $_COOKIE[DOKU_SESSION_NAME])) { 2466eb3cdf6SAndreas Gohr unset($_COOKIE[DOKU_SESSION_NAME]); 2476eb3cdf6SAndreas Gohr } 2486eb3cdf6SAndreas Gohr 2496eb3cdf6SAndreas Gohr session_start(); 2506eb3cdf6SAndreas Gohr} 2516eb3cdf6SAndreas Gohr 2526eb3cdf6SAndreas Gohr 2536eb3cdf6SAndreas Gohr/** 25498407a7aSandi * Checks paths from config file 25598407a7aSandi */ 25698407a7aSandifunction init_paths(){ 25798407a7aSandi global $conf; 25898407a7aSandi 25998407a7aSandi $paths = array('datadir' => 'pages', 26098407a7aSandi 'olddir' => 'attic', 26198407a7aSandi 'mediadir' => 'media', 262e4f389efSKate Arzamastseva 'mediaolddir' => 'media_attic', 26398407a7aSandi 'metadir' => 'meta', 264e4f389efSKate Arzamastseva 'mediametadir' => 'media_meta', 26598407a7aSandi 'cachedir' => 'cache', 266579b0f7eSTNHarris 'indexdir' => 'index', 267de33a58fSMichael Klier 'lockdir' => 'locks', 268de33a58fSMichael Klier 'tmpdir' => 'tmp'); 26998407a7aSandi 27098407a7aSandi foreach($paths as $c => $p) { 2717f086b67SAnika Henke $path = empty($conf[$c]) ? $conf['savedir'].'/'.$p : $conf[$c]; 2726b9c156cSAnika Henke $conf[$c] = init_path($path); 2736b9c156cSAnika Henke if(empty($conf[$c])) 2746b9c156cSAnika Henke nice_die("The $c ('$p') at $path is not found, isn't accessible or writable. 27569dc3177SAndreas Gohr You should check your config and permission settings. 27669dc3177SAndreas Gohr Or maybe you want to <a href=\"install.php\">run the 27769dc3177SAndreas Gohr installer</a>?"); 27898407a7aSandi } 27971726d78SBen Coburn 28071726d78SBen Coburn // path to old changelog only needed for upgrading 281*64159a61SAndreas Gohr $conf['changelog_old'] = init_path( 282*64159a61SAndreas Gohr (isset($conf['changelog'])) ? ($conf['changelog']) : ($conf['savedir'] . '/changes.log') 283*64159a61SAndreas Gohr ); 28471726d78SBen Coburn if ($conf['changelog_old']=='') { unset($conf['changelog_old']); } 28571726d78SBen Coburn // hardcoded changelog because it is now a cache that lives in meta 28671726d78SBen Coburn $conf['changelog'] = $conf['metadir'].'/_dokuwiki.changes'; 28799c8d7f2Smichael $conf['media_changelog'] = $conf['metadir'].'/_media.changes'; 28898407a7aSandi} 28998407a7aSandi 29038fb1fc7SGerrit Uitslag/** 29138fb1fc7SGerrit Uitslag * Load the language strings 29238fb1fc7SGerrit Uitslag * 29338fb1fc7SGerrit Uitslag * @param string $langCode language code, as passed by event handler 29438fb1fc7SGerrit Uitslag */ 2956d06b26aSDominik Eckelmannfunction init_lang($langCode) { 2966d06b26aSDominik Eckelmann //prepare language array 297dd7a6159SGerrit Uitslag global $lang, $config_cascade; 2986d06b26aSDominik Eckelmann $lang = array(); 2996d06b26aSDominik Eckelmann 3006d06b26aSDominik Eckelmann //load the language files 3011d82c8d3SChristopher Smith require(DOKU_INC.'inc/lang/en/lang.php'); 302dd7a6159SGerrit Uitslag foreach ($config_cascade['lang']['core'] as $config_file) { 30379e79377SAndreas Gohr if (file_exists($config_file . 'en/lang.php')) { 304dd7a6159SGerrit Uitslag include($config_file . 'en/lang.php'); 305dd7a6159SGerrit Uitslag } 306dd7a6159SGerrit Uitslag } 307dd7a6159SGerrit Uitslag 3086d06b26aSDominik Eckelmann if ($langCode && $langCode != 'en') { 3096d06b26aSDominik Eckelmann if (file_exists(DOKU_INC."inc/lang/$langCode/lang.php")) { 3101d82c8d3SChristopher Smith require(DOKU_INC."inc/lang/$langCode/lang.php"); 3116d06b26aSDominik Eckelmann } 312dd7a6159SGerrit Uitslag foreach ($config_cascade['lang']['core'] as $config_file) { 31379e79377SAndreas Gohr if (file_exists($config_file . "$langCode/lang.php")) { 314dd7a6159SGerrit Uitslag include($config_file . "$langCode/lang.php"); 3156d06b26aSDominik Eckelmann } 316dd7a6159SGerrit Uitslag } 3176d06b26aSDominik Eckelmann } 3186d06b26aSDominik Eckelmann} 3196d06b26aSDominik Eckelmann 32098407a7aSandi/** 3216b9c156cSAnika Henke * Checks the existence of certain files and creates them if missing. 3227367b368SAndreas Gohr */ 3237367b368SAndreas Gohrfunction init_files(){ 3247367b368SAndreas Gohr global $conf; 3250d8850c4SAndreas Gohr 326345b1674SAndreas Gohr $files = array($conf['indexdir'].'/page.idx'); 3277367b368SAndreas Gohr 3287367b368SAndreas Gohr foreach($files as $file){ 32979e79377SAndreas Gohr if(!file_exists($file)){ 3300d8850c4SAndreas Gohr $fh = @fopen($file,'a'); 3310d8850c4SAndreas Gohr if($fh){ 3327367b368SAndreas Gohr fclose($fh); 333443e135dSChristopher Smith if(!empty($conf['fperm'])) chmod($file, $conf['fperm']); 3340d8850c4SAndreas Gohr }else{ 3353816dcbcSAndreas Gohr nice_die("$file is not writable. Check your permissions settings!"); 3360d8850c4SAndreas Gohr } 3377367b368SAndreas Gohr } 3387367b368SAndreas Gohr } 3397367b368SAndreas Gohr} 3407367b368SAndreas Gohr 3417367b368SAndreas Gohr/** 3420d8850c4SAndreas Gohr * Returns absolute path 343f62ea8a1Sandi * 3440d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC. 3457f086b67SAnika Henke * Check for accessibility on directories as well. 3460d8850c4SAndreas Gohr * 3470d8850c4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 348f50a239bSTakamura * 349f50a239bSTakamura * @param string $path 350f50a239bSTakamura * 351f50a239bSTakamura * @return bool|string 352f62ea8a1Sandi */ 353f62ea8a1Sandifunction init_path($path){ 3546b9c156cSAnika Henke // check existence 35500976812SAndreas Gohr $p = fullpath($path); 35679e79377SAndreas Gohr if(!file_exists($p)){ 35700976812SAndreas Gohr $p = fullpath(DOKU_INC.$path); 35879e79377SAndreas Gohr if(!file_exists($p)){ 3598fc4e739Sandi return ''; 360f62ea8a1Sandi } 3610d8850c4SAndreas Gohr } 3620d8850c4SAndreas Gohr 3630d8850c4SAndreas Gohr // check writability 3640d8850c4SAndreas Gohr if(!@is_writable($p)){ 3650d8850c4SAndreas Gohr return ''; 3660d8850c4SAndreas Gohr } 3670d8850c4SAndreas Gohr 3680d8850c4SAndreas Gohr // check accessability (execute bit) for directories 36979e79377SAndreas Gohr if(@is_dir($p) && !file_exists("$p/.")){ 3700d8850c4SAndreas Gohr return ''; 3710d8850c4SAndreas Gohr } 3720d8850c4SAndreas Gohr 3730d8850c4SAndreas Gohr return $p; 3740d8850c4SAndreas Gohr} 3758c4f28e8Sjan 376ed7b5f09Sandi/** 3771ca31cfeSAndreas Gohr * Sets the internal config values fperm and dperm which, when set, 3781ca31cfeSAndreas Gohr * will be used to change the permission of a newly created dir or 3791ca31cfeSAndreas Gohr * file with chmod. Considers the influence of the system's umask 3801ca31cfeSAndreas Gohr * setting the values only if needed. 3811ca31cfeSAndreas Gohr */ 3821ca31cfeSAndreas Gohrfunction init_creationmodes(){ 3831ca31cfeSAndreas Gohr global $conf; 3841ca31cfeSAndreas Gohr 3851ca31cfeSAndreas Gohr // Legacy support for old umask/dmask scheme 3861ca31cfeSAndreas Gohr unset($conf['dmask']); 3871ca31cfeSAndreas Gohr unset($conf['fmask']); 3881ca31cfeSAndreas Gohr unset($conf['umask']); 3891ca31cfeSAndreas Gohr unset($conf['fperm']); 3901ca31cfeSAndreas Gohr unset($conf['dperm']); 3911ca31cfeSAndreas Gohr 3929f3cdec3SAndreas Gohr // get system umask, fallback to 0 if none available 3939f3cdec3SAndreas Gohr $umask = @umask(); 3949f3cdec3SAndreas Gohr if(!$umask) $umask = 0000; 3951ca31cfeSAndreas Gohr 3961ca31cfeSAndreas Gohr // check what is set automatically by the system on file creation 3971ca31cfeSAndreas Gohr // and set the fperm param if it's not what we want 3981ca31cfeSAndreas Gohr $auto_fmode = 0666 & ~$umask; 3991ca31cfeSAndreas Gohr if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode']; 4001ca31cfeSAndreas Gohr 4011ca31cfeSAndreas Gohr // check what is set automatically by the system on file creation 4021ca31cfeSAndreas Gohr // and set the dperm param if it's not what we want 4031ca31cfeSAndreas Gohr $auto_dmode = $conf['dmode'] & ~$umask; 4041ca31cfeSAndreas Gohr if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode']; 4051ca31cfeSAndreas Gohr} 4061ca31cfeSAndreas Gohr 4071ca31cfeSAndreas Gohr/** 408ed7b5f09Sandi * Returns the full absolute URL to the directory where 409ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash) 410ed7b5f09Sandi * 411585bf44eSChristopher Smith * !! Can not access $_SERVER values through $INPUT 412585bf44eSChristopher Smith * !! here as this function is called before $INPUT is 413585bf44eSChristopher Smith * !! initialized. 414585bf44eSChristopher Smith * 415ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org> 416f50a239bSTakamura * 417f50a239bSTakamura * @param null|string $abs 418f50a239bSTakamura * 419f50a239bSTakamura * @return string 420ed7b5f09Sandi */ 4214b1a4e04SAndreas Gohrfunction getBaseURL($abs=null){ 422ed7b5f09Sandi global $conf; 423ed7b5f09Sandi //if canonical url enabled always return absolute 4244b1a4e04SAndreas Gohr if(is_null($abs)) $abs = $conf['canonical']; 425ed7b5f09Sandi 4261858e4d7SGerry Weißbach if(!empty($conf['basedir'])){ 42746c73e01SChris Smith $dir = $conf['basedir']; 42889aa05dbSAndreas Gohr }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){ 42946c73e01SChris Smith $dir = dirname($_SERVER['SCRIPT_NAME']); 43089aa05dbSAndreas Gohr }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){ 43146c73e01SChris Smith $dir = dirname($_SERVER['PHP_SELF']); 432093ec9e4Sandi }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){ 433093ec9e4Sandi $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','', 434093ec9e4Sandi $_SERVER['SCRIPT_FILENAME']); 43546c73e01SChris Smith $dir = dirname('/'.$dir); 43692b83b77Sandi }else{ 43746c73e01SChris Smith $dir = '.'; //probably wrong 43892b83b77Sandi } 439ed7b5f09Sandi 44046c73e01SChris Smith $dir = str_replace('\\','/',$dir); // bugfix for weird WIN behaviour 44146c73e01SChris Smith $dir = preg_replace('#//+#','/',"/$dir/"); // ensure leading and trailing slashes 442ed7b5f09Sandi 443f62ea8a1Sandi //handle script in lib/exe dir 444f62ea8a1Sandi $dir = preg_replace('!lib/exe/$!','',$dir); 445f62ea8a1Sandi 446488d5fa0SMichael Klier chi@chimeric.de //handle script in lib/plugins dir 447488d5fa0SMichael Klier chi@chimeric.de $dir = preg_replace('!lib/plugins/.*$!','',$dir); 448488d5fa0SMichael Klier chi@chimeric.de 449ed7b5f09Sandi //finish here for relative URLs 450ed7b5f09Sandi if(!$abs) return $dir; 451ed7b5f09Sandi 452*64159a61SAndreas Gohr //use config if available, trim any slash from end of baseurl to avoid multiple consecutive slashes in the path 4531858e4d7SGerry Weißbach if(!empty($conf['baseurl'])) return rtrim($conf['baseurl'],'/').$dir; 454ef7b3ecdSAndreas Gohr 455e82e3526SAndreas Gohr //split hostheader into host and port 4565627186cSAndreas Gohr if(isset($_SERVER['HTTP_HOST'])){ 457204b27c8SMichael Hamann $parsed_host = parse_url('http://'.$_SERVER['HTTP_HOST']); 458f87b5dbbSChristopher Smith $host = isset($parsed_host['host']) ? $parsed_host['host'] : null; 459f87b5dbbSChristopher Smith $port = isset($parsed_host['port']) ? $parsed_host['port'] : null; 4605627186cSAndreas Gohr }elseif(isset($_SERVER['SERVER_NAME'])){ 461204b27c8SMichael Hamann $parsed_host = parse_url('http://'.$_SERVER['SERVER_NAME']); 462f87b5dbbSChristopher Smith $host = isset($parsed_host['host']) ? $parsed_host['host'] : null; 463f87b5dbbSChristopher Smith $port = isset($parsed_host['port']) ? $parsed_host['port'] : null; 4645627186cSAndreas Gohr }else{ 4655627186cSAndreas Gohr $host = php_uname('n'); 466c66972f2SAdrian Lang $port = ''; 4675627186cSAndreas Gohr } 4685627186cSAndreas Gohr 469204b27c8SMichael Hamann if(is_null($port)){ 470204b27c8SMichael Hamann $port = ''; 471204b27c8SMichael Hamann } 472204b27c8SMichael Hamann 473f5c6743cSAndreas Gohr if(!is_ssl()){ 474ed7b5f09Sandi $proto = 'http://'; 475e82e3526SAndreas Gohr if ($port == '80') { 476ed7b5f09Sandi $port = ''; 477ed7b5f09Sandi } 478ed7b5f09Sandi }else{ 479ed7b5f09Sandi $proto = 'https://'; 480e82e3526SAndreas Gohr if ($port == '443') { 481ed7b5f09Sandi $port = ''; 482ed7b5f09Sandi } 483ed7b5f09Sandi } 484ed7b5f09Sandi 485c66972f2SAdrian Lang if($port !== '') $port = ':'.$port; 486e82e3526SAndreas Gohr 487ed7b5f09Sandi return $proto.$host.$port.$dir; 488ed7b5f09Sandi} 489ed7b5f09Sandi 490b000c6d4Sandi/** 491f5c6743cSAndreas Gohr * Check if accessed via HTTPS 492f5c6743cSAndreas Gohr * 493f5c6743cSAndreas Gohr * Apache leaves ,$_SERVER['HTTPS'] empty when not available, IIS sets it to 'off'. 494f5c6743cSAndreas Gohr * 'false' and 'disabled' are just guessing 495f5c6743cSAndreas Gohr * 496f5c6743cSAndreas Gohr * @returns bool true when SSL is active 497f5c6743cSAndreas Gohr */ 498f5c6743cSAndreas Gohrfunction is_ssl() { 49919738e65SEnrico Tagliavini // check if we are behind a reverse proxy 50019738e65SEnrico Tagliavini if(isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) { 50119738e65SEnrico Tagliavini if($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') { 50219738e65SEnrico Tagliavini return true; 50319738e65SEnrico Tagliavini } else { 50419738e65SEnrico Tagliavini return false; 50519738e65SEnrico Tagliavini } 50619738e65SEnrico Tagliavini } 507c66972f2SAdrian Lang if(!isset($_SERVER['HTTPS']) || 508c66972f2SAdrian Lang preg_match('/^(|off|false|disabled)$/i', $_SERVER['HTTPS'])) { 509f5c6743cSAndreas Gohr return false; 510f5c6743cSAndreas Gohr } else { 511f5c6743cSAndreas Gohr return true; 512f5c6743cSAndreas Gohr } 513f5c6743cSAndreas Gohr} 514f5c6743cSAndreas Gohr 515f5c6743cSAndreas Gohr/** 51626714386SAndreas Gohr * checks it is windows OS 51726714386SAndreas Gohr * @return bool 51826714386SAndreas Gohr */ 51926714386SAndreas Gohrfunction isWindows() { 52026714386SAndreas Gohr return (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? true : false; 52126714386SAndreas Gohr} 52226714386SAndreas Gohr 52326714386SAndreas Gohr/** 5243816dcbcSAndreas Gohr * print a nice message even if no styles are loaded yet. 525f50a239bSTakamura * 526f50a239bSTakamura * @param integer|string $msg 5273816dcbcSAndreas Gohr */ 5283816dcbcSAndreas Gohrfunction nice_die($msg){ 5293816dcbcSAndreas Gohr echo<<<EOT 530c8839c22SAnika Henke<!DOCTYPE html> 5313816dcbcSAndreas Gohr<html> 5323816dcbcSAndreas Gohr<head><title>DokuWiki Setup Error</title></head> 5333816dcbcSAndreas Gohr<body style="font-family: Arial, sans-serif"> 5343816dcbcSAndreas Gohr <div style="width:60%; margin: auto; background-color: #fcc; 5353816dcbcSAndreas Gohr border: 1px solid #faa; padding: 0.5em 1em;"> 5363816dcbcSAndreas Gohr <h1 style="font-size: 120%">DokuWiki Setup Error</h1> 5373816dcbcSAndreas Gohr <p>$msg</p> 5383816dcbcSAndreas Gohr </div> 5393816dcbcSAndreas Gohr</body> 5403816dcbcSAndreas Gohr</html> 5413816dcbcSAndreas GohrEOT; 5423862da0eSAndreas Gohr if(defined('DOKU_UNITTEST')) { 5433862da0eSAndreas Gohr throw new RuntimeException('nice_die: '.$msg); 5443862da0eSAndreas Gohr } 5450a4266d4SElan Ruusamäe exit(1); 5463816dcbcSAndreas Gohr} 5473816dcbcSAndreas Gohr 54800976812SAndreas Gohr/** 54900976812SAndreas Gohr * A realpath() replacement 55000976812SAndreas Gohr * 55100976812SAndreas Gohr * This function behaves similar to PHP's realpath() but does not resolve 55200976812SAndreas Gohr * symlinks or accesses upper directories 55300976812SAndreas Gohr * 5544761d30cSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 55500976812SAndreas Gohr * @author <richpageau at yahoo dot co dot uk> 55659752844SAnders Sandblad * @link http://php.net/manual/en/function.realpath.php#75992 557f50a239bSTakamura * 558f50a239bSTakamura * @param string $path 559f50a239bSTakamura * @param bool $exists 560f50a239bSTakamura * 561f50a239bSTakamura * @return bool|string 56200976812SAndreas Gohr */ 563b328697dSAndreas Gohrfunction fullpath($path,$exists=false){ 5644761d30cSAndreas Gohr static $run = 0; 5654761d30cSAndreas Gohr $root = ''; 566f0a201c5SChris Smith $iswin = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' || @$GLOBALS['DOKU_UNITTEST_ASSUME_WINDOWS']); 56700976812SAndreas Gohr 5684761d30cSAndreas Gohr // find the (indestructable) root of the path - keeps windows stuff intact 5694761d30cSAndreas Gohr if($path{0} == '/'){ 5704761d30cSAndreas Gohr $root = '/'; 5714761d30cSAndreas Gohr }elseif($iswin){ 5724761d30cSAndreas Gohr // match drive letter and UNC paths 5734761d30cSAndreas Gohr if(preg_match('!^([a-zA-z]:)(.*)!',$path,$match)){ 574b9c4302bSAndreas Gohr $root = $match[1].'/'; 5754761d30cSAndreas Gohr $path = $match[2]; 5764761d30cSAndreas Gohr }else if(preg_match('!^(\\\\\\\\[^\\\\/]+\\\\[^\\\\/]+[\\\\/])(.*)!',$path,$match)){ 5774761d30cSAndreas Gohr $root = $match[1]; 5784761d30cSAndreas Gohr $path = $match[2]; 57900976812SAndreas Gohr } 5804761d30cSAndreas Gohr } 5814761d30cSAndreas Gohr $path = str_replace('\\','/',$path); 5824761d30cSAndreas Gohr 5834761d30cSAndreas Gohr // if the given path wasn't absolute already, prepend the script path and retry 5844761d30cSAndreas Gohr if(!$root){ 5854761d30cSAndreas Gohr $base = dirname($_SERVER['SCRIPT_FILENAME']); 5864761d30cSAndreas Gohr $path = $base.'/'.$path; 5874761d30cSAndreas Gohr if($run == 0){ // avoid endless recursion when base isn't absolute for some reason 5884761d30cSAndreas Gohr $run++; 589b328697dSAndreas Gohr return fullpath($path,$exists); 5904761d30cSAndreas Gohr } 5914761d30cSAndreas Gohr } 5924761d30cSAndreas Gohr $run = 0; 59300976812SAndreas Gohr 59400976812SAndreas Gohr // canonicalize 59500976812SAndreas Gohr $path=explode('/', $path); 59600976812SAndreas Gohr $newpath=array(); 597ef38bfe8SAndreas Gohr foreach($path as $p) { 598ef38bfe8SAndreas Gohr if ($p === '' || $p === '.') continue; 599ef38bfe8SAndreas Gohr if ($p==='..') { 60000976812SAndreas Gohr array_pop($newpath); 60100976812SAndreas Gohr continue; 60200976812SAndreas Gohr } 603ef38bfe8SAndreas Gohr array_push($newpath, $p); 60400976812SAndreas Gohr } 6054761d30cSAndreas Gohr $finalpath = $root.implode('/', $newpath); 60600976812SAndreas Gohr 6076b9c156cSAnika Henke // check for existence when needed (except when unit testing) 60879e79377SAndreas Gohr if($exists && !defined('DOKU_UNITTEST') && !file_exists($finalpath)) { 6094761d30cSAndreas Gohr return false; 61000976812SAndreas Gohr } 6114761d30cSAndreas Gohr return $finalpath; 61200976812SAndreas Gohr} 61300976812SAndreas Gohr 614