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 4550602150SBen Coburn// init memory caches 46db959ae3SAndreas Gohrglobal $cache_revinfo; 47db959ae3SAndreas Gohr $cache_revinfo = array(); 48db959ae3SAndreas Gohrglobal $cache_wikifn; 49db959ae3SAndreas Gohr $cache_wikifn = array(); 50db959ae3SAndreas Gohrglobal $cache_cleanid; 51db959ae3SAndreas Gohr $cache_cleanid = array(); 52db959ae3SAndreas Gohrglobal $cache_authname; 53db959ae3SAndreas Gohr $cache_authname = array(); 54db959ae3SAndreas Gohrglobal $cache_metadata; 55db959ae3SAndreas Gohr $cache_metadata = array(); 5650602150SBen Coburn 57cca94fbcSRoland Hager// always include 'inc/config_cascade.php' 58cca94fbcSRoland Hager// previously in preload.php set fields of $config_cascade will be merged with the defaults 59e6a6dbfeSAndreas Gohrinclude(DOKU_INC.'inc/config_cascade.php'); 60cb043f52SChris Smith 614724a577Sandi//prepare config array() 62ee20e7d1Sandiglobal $conf; 634724a577Sandi$conf = array(); 644724a577Sandi 65cb043f52SChris Smith// load the global config file(s) 66b303b92cSChris Smithforeach (array('default','local','protected') as $config_group) { 67f8121585SChris Smith if (empty($config_cascade['main'][$config_group])) continue; 68b303b92cSChris Smith foreach ($config_cascade['main'][$config_group] as $config_file) { 6979e79377SAndreas Gohr if (file_exists($config_file)) { 70f8121585SChris Smith include($config_file); 71f8121585SChris Smith } 72cb043f52SChris Smith } 730a6ead41SAndreas Gohr} 74ad15db82Sandi 75066fee30SAndreas Gohr//prepare license array() 76066fee30SAndreas Gohrglobal $license; 77066fee30SAndreas Gohr$license = array(); 78066fee30SAndreas Gohr 79066fee30SAndreas Gohr// load the license file(s) 80f8121585SChris Smithforeach (array('default','local') as $config_group) { 81f8121585SChris Smith if (empty($config_cascade['license'][$config_group])) continue; 82f8121585SChris Smith foreach ($config_cascade['license'][$config_group] as $config_file) { 8379e79377SAndreas Gohr if(file_exists($config_file)){ 84f8121585SChris Smith include($config_file); 85f8121585SChris Smith } 86f8121585SChris Smith } 87066fee30SAndreas Gohr} 88066fee30SAndreas Gohr 891f8eb24fSAndreas Gohr// set timezone (as in pre 5.3.0 days) 901f8eb24fSAndreas Gohrdate_default_timezone_set(@date_default_timezone_get()); 911f8eb24fSAndreas Gohr 92ed7b5f09Sandi// define baseURL 934b1a4e04SAndreas Gohrif(!defined('DOKU_REL')) define('DOKU_REL',getBaseURL(false)); 94ed7b5f09Sandiif(!defined('DOKU_URL')) define('DOKU_URL',getBaseURL(true)); 954b1a4e04SAndreas Gohrif(!defined('DOKU_BASE')){ 964b1a4e04SAndreas Gohr if($conf['canonical']){ 974b1a4e04SAndreas Gohr define('DOKU_BASE',DOKU_URL); 984b1a4e04SAndreas Gohr }else{ 994b1a4e04SAndreas Gohr define('DOKU_BASE',DOKU_REL); 1004b1a4e04SAndreas Gohr } 1014b1a4e04SAndreas Gohr} 1024b1a4e04SAndreas Gohr 103b8595a66SAndreas Gohr// define whitespace 104b8595a66SAndreas Gohrif(!defined('DOKU_LF')) define ('DOKU_LF',"\n"); 105b8595a66SAndreas Gohrif(!defined('DOKU_TAB')) define ('DOKU_TAB',"\t"); 106ed7b5f09Sandi 107656c8fb3SAndreas Gohr// define cookie and session id, append server port when securecookie is configured FS#1664 108656c8fb3SAndreas Gohrif (!defined('DOKU_COOKIE')) define('DOKU_COOKIE', 'DW'.md5(DOKU_REL.(($conf['securecookie'])?$_SERVER['SERVER_PORT']:''))); 109e71ce681SAndreas Gohr 110ee20e7d1Sandi 111ed7b5f09Sandi// define main script 112ed7b5f09Sandiif(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php'); 113ed7b5f09Sandi 114c4766956SAndreas Gohr// DEPRECATED, use tpl_basedir() instead 1156b13307fSandiif(!defined('DOKU_TPL')) define('DOKU_TPL', 116f62ea8a1Sandi DOKU_BASE.'lib/tpl/'.$conf['template'].'/'); 1176b13307fSandi 118c4766956SAndreas Gohr// DEPRECATED, use tpl_incdir() instead 11978a6aeb1SAndreas Gohrif(!defined('DOKU_TPLINC')) define('DOKU_TPLINC', 12078a6aeb1SAndreas Gohr DOKU_INC.'lib/tpl/'.$conf['template'].'/'); 12178a6aeb1SAndreas Gohr 122ed7b5f09Sandi// make session rewrites XHTML compliant 1233fc74836Sandi@ini_set('arg_separator.output', '&'); 124ed7b5f09Sandi 125d7e6bba9SAndreas Gohr// make sure global zlib does not interfere FS#1132 126d7e6bba9SAndreas Gohr@ini_set('zlib.output_compression', 'off'); 127d7e6bba9SAndreas Gohr 1286deb5405SAndreas Gohr// increase PCRE backtrack limit 1296deb5405SAndreas Gohr@ini_set('pcre.backtrack_limit', '20971520'); 1306deb5405SAndreas Gohr 13198bda4fdSAndreas Gohr// enable gzip compression if supported 13298bda4fdSAndreas Gohr$conf['gzip_output'] &= (strpos($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip') !== false); 13365f6e7d6SMichael Hamannglobal $ACT; 1343138b5c7SAndreas Gohrif ($conf['gzip_output'] && 1353138b5c7SAndreas Gohr !defined('DOKU_DISABLE_GZIP_OUTPUT') && 13665f6e7d6SMichael Hamann function_exists('ob_gzhandler') && 13799e10b7fSMichael Hamann // Disable compression when a (compressed) sitemap might be delivered 13865f6e7d6SMichael Hamann // See https://bugs.dokuwiki.org/index.php?do=details&task_id=2576 13999e10b7fSMichael Hamann $ACT != 'sitemap') { 1403138b5c7SAndreas Gohr ob_start('ob_gzhandler'); 1413138b5c7SAndreas Gohr} 1423138b5c7SAndreas Gohr 143ed7b5f09Sandi// init session 1446534245aSAndreas Gohrif(!headers_sent() && !defined('NOSESSION')) { 145c09f0eb1SGerrit Uitslag if(!defined('DOKU_SESSION_NAME')) define ('DOKU_SESSION_NAME', "DokuWiki"); 146c09f0eb1SGerrit Uitslag if(!defined('DOKU_SESSION_LIFETIME')) define ('DOKU_SESSION_LIFETIME', 0); 14755a71a16SGerrit Uitslag if(!defined('DOKU_SESSION_PATH')) { 14873ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 14955a71a16SGerrit Uitslag define ('DOKU_SESSION_PATH', $cookieDir); 150f5c6743cSAndreas Gohr } 151c09f0eb1SGerrit Uitslag if(!defined('DOKU_SESSION_DOMAIN')) define ('DOKU_SESSION_DOMAIN', ''); 152c09f0eb1SGerrit Uitslag 1536eb3cdf6SAndreas Gohr // start the session 1546eb3cdf6SAndreas Gohr init_session(); 15514a122deSAndreas Gohr 15614a122deSAndreas Gohr // load left over messages 15714a122deSAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['msg'])) { 15814a122deSAndreas Gohr $MSG = $_SESSION[DOKU_COOKIE]['msg']; 15914a122deSAndreas Gohr unset($_SESSION[DOKU_COOKIE]['msg']); 16014a122deSAndreas Gohr } 161bad31ae9SAndreas Gohr} 162ed7b5f09Sandi 163a1637ffdSAndreas Gohr// don't let cookies ever interfere with request vars 164a1637ffdSAndreas Gohr$_REQUEST = array_merge($_GET,$_POST); 165a1637ffdSAndreas Gohr 1663dea4ebcSAndreas Gohr// we don't want a purge URL to be digged 1670e80bb5eSChristopher Smithif(isset($_REQUEST['purge']) && !empty($_SERVER['HTTP_REFERER'])) unset($_REQUEST['purge']); 1683dea4ebcSAndreas Gohr 1691ca31cfeSAndreas Gohr// precalculate file creation modes 1701ca31cfeSAndreas Gohrinit_creationmodes(); 171ed7b5f09Sandi 1723dc3a5f1Sandi// make real paths and check them 17398407a7aSandiinit_paths(); 1747367b368SAndreas Gohrinit_files(); 175ed7b5f09Sandi 176f1986589SMichael Klier// setup plugin controller class (can be overwritten in preload.php) 17793a7873eSAndreas Gohr$plugin_types = array('auth', 'admin','syntax','action','renderer', 'helper','remote'); 178f1986589SMichael Klierglobal $plugin_controller_class, $plugin_controller; 179f1986589SMichael Klierif (empty($plugin_controller_class)) $plugin_controller_class = 'Doku_Plugin_Controller'; 180f1986589SMichael Klier 181c7cb395cSAdrian Lang// load libraries 182605f8e8dSAndreas Gohrrequire_once(DOKU_INC.'vendor/autoload.php'); 183c7cb395cSAdrian Langrequire_once(DOKU_INC.'inc/load.php'); 184c7cb395cSAdrian Lang 1850f8f7aaaSDanny Lin// disable gzip if not available 18613c37900SAndreas Gohrdefine('DOKU_HAS_BZIP', function_exists('bzopen')); 18713c37900SAndreas Gohrdefine('DOKU_HAS_GZIP', function_exists('gzopen')); 18813c37900SAndreas Gohrif($conf['compression'] == 'bz2' && !DOKU_HAS_BZIP) { 1890f8f7aaaSDanny Lin $conf['compression'] = 'gz'; 1900f8f7aaaSDanny Lin} 19113c37900SAndreas Gohrif($conf['compression'] == 'gz' && !DOKU_HAS_GZIP) { 1920f8f7aaaSDanny Lin $conf['compression'] = 0; 1930f8f7aaaSDanny Lin} 1940f8f7aaaSDanny Lin 19589177306SAndreas Gohr// input handle class 19689177306SAndreas Gohrglobal $INPUT; 19789177306SAndreas Gohr$INPUT = new Input(); 19889177306SAndreas Gohr 199f1986589SMichael Klier// initialize plugin controller 200f1986589SMichael Klier$plugin_controller = new $plugin_controller_class(); 201f1986589SMichael Klier 202f1986589SMichael Klier// initialize the event handler 203f1986589SMichael Klierglobal $EVENT_HANDLER; 204f1986589SMichael Klier$EVENT_HANDLER = new Doku_Event_Handler(); 205f1986589SMichael Klier 2066d06b26aSDominik Eckelmann$local = $conf['lang']; 2076d06b26aSDominik Eckelmanntrigger_event('INIT_LANG_LOAD', $local, 'init_lang', true); 2086d06b26aSDominik Eckelmann 2096d06b26aSDominik Eckelmann 21016905344SAndreas Gohr// setup authentication system 211c7cb395cSAdrian Langif (!defined('NOSESSION')) { 21216905344SAndreas Gohr auth_setup(); 213c7cb395cSAdrian Lang} 214f62ea8a1Sandi 2155ec3fefcSAndreas Gohr// setup mail system 2165ec3fefcSAndreas Gohrmail_setup(); 2175ec3fefcSAndreas Gohr 218f62ea8a1Sandi/** 2196eb3cdf6SAndreas Gohr * Initializes the session 2206eb3cdf6SAndreas Gohr * 2216eb3cdf6SAndreas Gohr * Makes sure the passed session cookie is valid, invalid ones are ignored an a new session ID is issued 2226eb3cdf6SAndreas Gohr * 2236eb3cdf6SAndreas Gohr * @link http://stackoverflow.com/a/33024310/172068 224924e477eSAndreas Gohr * @link http://php.net/manual/en/session.configuration.php#ini.session.sid-length 2256eb3cdf6SAndreas Gohr */ 2266eb3cdf6SAndreas Gohrfunction init_session() { 2276eb3cdf6SAndreas Gohr global $conf; 2286eb3cdf6SAndreas Gohr session_name(DOKU_SESSION_NAME); 2296eb3cdf6SAndreas Gohr session_set_cookie_params(DOKU_SESSION_LIFETIME, DOKU_SESSION_PATH, DOKU_SESSION_DOMAIN, ($conf['securecookie'] && is_ssl()), true); 2306eb3cdf6SAndreas Gohr 2316eb3cdf6SAndreas Gohr // make sure the session cookie contains a valid session ID 232924e477eSAndreas Gohr if(isset($_COOKIE[DOKU_SESSION_NAME]) && !preg_match('/^[-,a-zA-Z0-9]{22,256}$/', $_COOKIE[DOKU_SESSION_NAME])) { 2336eb3cdf6SAndreas Gohr unset($_COOKIE[DOKU_SESSION_NAME]); 2346eb3cdf6SAndreas Gohr } 2356eb3cdf6SAndreas Gohr 2366eb3cdf6SAndreas Gohr session_start(); 2376eb3cdf6SAndreas Gohr} 2386eb3cdf6SAndreas Gohr 2396eb3cdf6SAndreas Gohr 2406eb3cdf6SAndreas Gohr/** 24198407a7aSandi * Checks paths from config file 24298407a7aSandi */ 24398407a7aSandifunction init_paths(){ 24498407a7aSandi global $conf; 24598407a7aSandi 24698407a7aSandi $paths = array('datadir' => 'pages', 24798407a7aSandi 'olddir' => 'attic', 24898407a7aSandi 'mediadir' => 'media', 249e4f389efSKate Arzamastseva 'mediaolddir' => 'media_attic', 25098407a7aSandi 'metadir' => 'meta', 251e4f389efSKate Arzamastseva 'mediametadir' => 'media_meta', 25298407a7aSandi 'cachedir' => 'cache', 253579b0f7eSTNHarris 'indexdir' => 'index', 254de33a58fSMichael Klier 'lockdir' => 'locks', 255de33a58fSMichael Klier 'tmpdir' => 'tmp'); 25698407a7aSandi 25798407a7aSandi foreach($paths as $c => $p) { 2587f086b67SAnika Henke $path = empty($conf[$c]) ? $conf['savedir'].'/'.$p : $conf[$c]; 2596b9c156cSAnika Henke $conf[$c] = init_path($path); 2606b9c156cSAnika Henke if(empty($conf[$c])) 2616b9c156cSAnika Henke nice_die("The $c ('$p') at $path is not found, isn't accessible or writable. 26269dc3177SAndreas Gohr You should check your config and permission settings. 26369dc3177SAndreas Gohr Or maybe you want to <a href=\"install.php\">run the 26469dc3177SAndreas Gohr installer</a>?"); 26598407a7aSandi } 26671726d78SBen Coburn 26771726d78SBen Coburn // path to old changelog only needed for upgrading 26871726d78SBen Coburn $conf['changelog_old'] = init_path((isset($conf['changelog']))?($conf['changelog']):($conf['savedir'].'/changes.log')); 26971726d78SBen Coburn if ($conf['changelog_old']=='') { unset($conf['changelog_old']); } 27071726d78SBen Coburn // hardcoded changelog because it is now a cache that lives in meta 27171726d78SBen Coburn $conf['changelog'] = $conf['metadir'].'/_dokuwiki.changes'; 27299c8d7f2Smichael $conf['media_changelog'] = $conf['metadir'].'/_media.changes'; 27398407a7aSandi} 27498407a7aSandi 27538fb1fc7SGerrit Uitslag/** 27638fb1fc7SGerrit Uitslag * Load the language strings 27738fb1fc7SGerrit Uitslag * 27838fb1fc7SGerrit Uitslag * @param string $langCode language code, as passed by event handler 27938fb1fc7SGerrit Uitslag */ 2806d06b26aSDominik Eckelmannfunction init_lang($langCode) { 2816d06b26aSDominik Eckelmann //prepare language array 282dd7a6159SGerrit Uitslag global $lang, $config_cascade; 2836d06b26aSDominik Eckelmann $lang = array(); 2846d06b26aSDominik Eckelmann 2856d06b26aSDominik Eckelmann //load the language files 2861d82c8d3SChristopher Smith require(DOKU_INC.'inc/lang/en/lang.php'); 287dd7a6159SGerrit Uitslag foreach ($config_cascade['lang']['core'] as $config_file) { 28879e79377SAndreas Gohr if (file_exists($config_file . 'en/lang.php')) { 289dd7a6159SGerrit Uitslag include($config_file . 'en/lang.php'); 290dd7a6159SGerrit Uitslag } 291dd7a6159SGerrit Uitslag } 292dd7a6159SGerrit Uitslag 2936d06b26aSDominik Eckelmann if ($langCode && $langCode != 'en') { 2946d06b26aSDominik Eckelmann if (file_exists(DOKU_INC."inc/lang/$langCode/lang.php")) { 2951d82c8d3SChristopher Smith require(DOKU_INC."inc/lang/$langCode/lang.php"); 2966d06b26aSDominik Eckelmann } 297dd7a6159SGerrit Uitslag foreach ($config_cascade['lang']['core'] as $config_file) { 29879e79377SAndreas Gohr if (file_exists($config_file . "$langCode/lang.php")) { 299dd7a6159SGerrit Uitslag include($config_file . "$langCode/lang.php"); 3006d06b26aSDominik Eckelmann } 301dd7a6159SGerrit Uitslag } 3026d06b26aSDominik Eckelmann } 3036d06b26aSDominik Eckelmann} 3046d06b26aSDominik Eckelmann 30598407a7aSandi/** 3066b9c156cSAnika Henke * Checks the existence of certain files and creates them if missing. 3077367b368SAndreas Gohr */ 3087367b368SAndreas Gohrfunction init_files(){ 3097367b368SAndreas Gohr global $conf; 3100d8850c4SAndreas Gohr 311345b1674SAndreas Gohr $files = array($conf['indexdir'].'/page.idx'); 3127367b368SAndreas Gohr 3137367b368SAndreas Gohr foreach($files as $file){ 31479e79377SAndreas Gohr if(!file_exists($file)){ 3150d8850c4SAndreas Gohr $fh = @fopen($file,'a'); 3160d8850c4SAndreas Gohr if($fh){ 3177367b368SAndreas Gohr fclose($fh); 318443e135dSChristopher Smith if(!empty($conf['fperm'])) chmod($file, $conf['fperm']); 3190d8850c4SAndreas Gohr }else{ 3203816dcbcSAndreas Gohr nice_die("$file is not writable. Check your permissions settings!"); 3210d8850c4SAndreas Gohr } 3227367b368SAndreas Gohr } 3237367b368SAndreas Gohr } 3247367b368SAndreas Gohr} 3257367b368SAndreas Gohr 3267367b368SAndreas Gohr/** 3270d8850c4SAndreas Gohr * Returns absolute path 328f62ea8a1Sandi * 3290d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC. 3307f086b67SAnika Henke * Check for accessibility on directories as well. 3310d8850c4SAndreas Gohr * 3320d8850c4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 333f50a239bSTakamura * 334f50a239bSTakamura * @param string $path 335f50a239bSTakamura * 336f50a239bSTakamura * @return bool|string 337f62ea8a1Sandi */ 338f62ea8a1Sandifunction init_path($path){ 3396b9c156cSAnika Henke // check existence 34000976812SAndreas Gohr $p = fullpath($path); 34179e79377SAndreas Gohr if(!file_exists($p)){ 34200976812SAndreas Gohr $p = fullpath(DOKU_INC.$path); 34379e79377SAndreas Gohr if(!file_exists($p)){ 3448fc4e739Sandi return ''; 345f62ea8a1Sandi } 3460d8850c4SAndreas Gohr } 3470d8850c4SAndreas Gohr 3480d8850c4SAndreas Gohr // check writability 3490d8850c4SAndreas Gohr if(!@is_writable($p)){ 3500d8850c4SAndreas Gohr return ''; 3510d8850c4SAndreas Gohr } 3520d8850c4SAndreas Gohr 3530d8850c4SAndreas Gohr // check accessability (execute bit) for directories 35479e79377SAndreas Gohr if(@is_dir($p) && !file_exists("$p/.")){ 3550d8850c4SAndreas Gohr return ''; 3560d8850c4SAndreas Gohr } 3570d8850c4SAndreas Gohr 3580d8850c4SAndreas Gohr return $p; 3590d8850c4SAndreas Gohr} 3608c4f28e8Sjan 361ed7b5f09Sandi/** 3621ca31cfeSAndreas Gohr * Sets the internal config values fperm and dperm which, when set, 3631ca31cfeSAndreas Gohr * will be used to change the permission of a newly created dir or 3641ca31cfeSAndreas Gohr * file with chmod. Considers the influence of the system's umask 3651ca31cfeSAndreas Gohr * setting the values only if needed. 3661ca31cfeSAndreas Gohr */ 3671ca31cfeSAndreas Gohrfunction init_creationmodes(){ 3681ca31cfeSAndreas Gohr global $conf; 3691ca31cfeSAndreas Gohr 3701ca31cfeSAndreas Gohr // Legacy support for old umask/dmask scheme 3711ca31cfeSAndreas Gohr unset($conf['dmask']); 3721ca31cfeSAndreas Gohr unset($conf['fmask']); 3731ca31cfeSAndreas Gohr unset($conf['umask']); 3741ca31cfeSAndreas Gohr unset($conf['fperm']); 3751ca31cfeSAndreas Gohr unset($conf['dperm']); 3761ca31cfeSAndreas Gohr 3779f3cdec3SAndreas Gohr // get system umask, fallback to 0 if none available 3789f3cdec3SAndreas Gohr $umask = @umask(); 3799f3cdec3SAndreas Gohr if(!$umask) $umask = 0000; 3801ca31cfeSAndreas Gohr 3811ca31cfeSAndreas Gohr // check what is set automatically by the system on file creation 3821ca31cfeSAndreas Gohr // and set the fperm param if it's not what we want 3831ca31cfeSAndreas Gohr $auto_fmode = 0666 & ~$umask; 3841ca31cfeSAndreas Gohr if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode']; 3851ca31cfeSAndreas Gohr 3861ca31cfeSAndreas Gohr // check what is set automatically by the system on file creation 3871ca31cfeSAndreas Gohr // and set the dperm param if it's not what we want 3881ca31cfeSAndreas Gohr $auto_dmode = $conf['dmode'] & ~$umask; 3891ca31cfeSAndreas Gohr if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode']; 3901ca31cfeSAndreas Gohr} 3911ca31cfeSAndreas Gohr 3921ca31cfeSAndreas Gohr/** 393ed7b5f09Sandi * Returns the full absolute URL to the directory where 394ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash) 395ed7b5f09Sandi * 396585bf44eSChristopher Smith * !! Can not access $_SERVER values through $INPUT 397585bf44eSChristopher Smith * !! here as this function is called before $INPUT is 398585bf44eSChristopher Smith * !! initialized. 399585bf44eSChristopher Smith * 400ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org> 401f50a239bSTakamura * 402f50a239bSTakamura * @param null|string $abs 403f50a239bSTakamura * 404f50a239bSTakamura * @return string 405ed7b5f09Sandi */ 4064b1a4e04SAndreas Gohrfunction getBaseURL($abs=null){ 407ed7b5f09Sandi global $conf; 408ed7b5f09Sandi //if canonical url enabled always return absolute 4094b1a4e04SAndreas Gohr if(is_null($abs)) $abs = $conf['canonical']; 410ed7b5f09Sandi 4111858e4d7SGerry Weißbach if(!empty($conf['basedir'])){ 41246c73e01SChris Smith $dir = $conf['basedir']; 41389aa05dbSAndreas Gohr }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){ 41446c73e01SChris Smith $dir = dirname($_SERVER['SCRIPT_NAME']); 41589aa05dbSAndreas Gohr }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){ 41646c73e01SChris Smith $dir = dirname($_SERVER['PHP_SELF']); 417093ec9e4Sandi }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){ 418093ec9e4Sandi $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','', 419093ec9e4Sandi $_SERVER['SCRIPT_FILENAME']); 42046c73e01SChris Smith $dir = dirname('/'.$dir); 42192b83b77Sandi }else{ 42246c73e01SChris Smith $dir = '.'; //probably wrong 42392b83b77Sandi } 424ed7b5f09Sandi 42546c73e01SChris Smith $dir = str_replace('\\','/',$dir); // bugfix for weird WIN behaviour 42646c73e01SChris Smith $dir = preg_replace('#//+#','/',"/$dir/"); // ensure leading and trailing slashes 427ed7b5f09Sandi 428f62ea8a1Sandi //handle script in lib/exe dir 429f62ea8a1Sandi $dir = preg_replace('!lib/exe/$!','',$dir); 430f62ea8a1Sandi 431488d5fa0SMichael Klier chi@chimeric.de //handle script in lib/plugins dir 432488d5fa0SMichael Klier chi@chimeric.de $dir = preg_replace('!lib/plugins/.*$!','',$dir); 433488d5fa0SMichael Klier chi@chimeric.de 434ed7b5f09Sandi //finish here for relative URLs 435ed7b5f09Sandi if(!$abs) return $dir; 436ed7b5f09Sandi 43746c73e01SChris Smith //use config option if available, trim any slash from end of baseurl to avoid multiple consecutive slashes in the path 4381858e4d7SGerry Weißbach if(!empty($conf['baseurl'])) return rtrim($conf['baseurl'],'/').$dir; 439ef7b3ecdSAndreas Gohr 440e82e3526SAndreas Gohr //split hostheader into host and port 4415627186cSAndreas Gohr if(isset($_SERVER['HTTP_HOST'])){ 442204b27c8SMichael Hamann $parsed_host = parse_url('http://'.$_SERVER['HTTP_HOST']); 443f87b5dbbSChristopher Smith $host = isset($parsed_host['host']) ? $parsed_host['host'] : null; 444f87b5dbbSChristopher Smith $port = isset($parsed_host['port']) ? $parsed_host['port'] : null; 4455627186cSAndreas Gohr }elseif(isset($_SERVER['SERVER_NAME'])){ 446204b27c8SMichael Hamann $parsed_host = parse_url('http://'.$_SERVER['SERVER_NAME']); 447f87b5dbbSChristopher Smith $host = isset($parsed_host['host']) ? $parsed_host['host'] : null; 448f87b5dbbSChristopher Smith $port = isset($parsed_host['port']) ? $parsed_host['port'] : null; 4495627186cSAndreas Gohr }else{ 4505627186cSAndreas Gohr $host = php_uname('n'); 451c66972f2SAdrian Lang $port = ''; 4525627186cSAndreas Gohr } 4535627186cSAndreas Gohr 454204b27c8SMichael Hamann if(is_null($port)){ 455204b27c8SMichael Hamann $port = ''; 456204b27c8SMichael Hamann } 457204b27c8SMichael Hamann 458f5c6743cSAndreas Gohr if(!is_ssl()){ 459ed7b5f09Sandi $proto = 'http://'; 460e82e3526SAndreas Gohr if ($port == '80') { 461ed7b5f09Sandi $port = ''; 462ed7b5f09Sandi } 463ed7b5f09Sandi }else{ 464ed7b5f09Sandi $proto = 'https://'; 465e82e3526SAndreas Gohr if ($port == '443') { 466ed7b5f09Sandi $port = ''; 467ed7b5f09Sandi } 468ed7b5f09Sandi } 469ed7b5f09Sandi 470c66972f2SAdrian Lang if($port !== '') $port = ':'.$port; 471e82e3526SAndreas Gohr 472ed7b5f09Sandi return $proto.$host.$port.$dir; 473ed7b5f09Sandi} 474ed7b5f09Sandi 475b000c6d4Sandi/** 476f5c6743cSAndreas Gohr * Check if accessed via HTTPS 477f5c6743cSAndreas Gohr * 478f5c6743cSAndreas Gohr * Apache leaves ,$_SERVER['HTTPS'] empty when not available, IIS sets it to 'off'. 479f5c6743cSAndreas Gohr * 'false' and 'disabled' are just guessing 480f5c6743cSAndreas Gohr * 481f5c6743cSAndreas Gohr * @returns bool true when SSL is active 482f5c6743cSAndreas Gohr */ 483f5c6743cSAndreas Gohrfunction is_ssl() { 48419738e65SEnrico Tagliavini // check if we are behind a reverse proxy 48519738e65SEnrico Tagliavini if(isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) { 48619738e65SEnrico Tagliavini if($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') { 48719738e65SEnrico Tagliavini return true; 48819738e65SEnrico Tagliavini } else { 48919738e65SEnrico Tagliavini return false; 49019738e65SEnrico Tagliavini } 49119738e65SEnrico Tagliavini } 492c66972f2SAdrian Lang if(!isset($_SERVER['HTTPS']) || 493c66972f2SAdrian Lang preg_match('/^(|off|false|disabled)$/i', $_SERVER['HTTPS'])) { 494f5c6743cSAndreas Gohr return false; 495f5c6743cSAndreas Gohr } else { 496f5c6743cSAndreas Gohr return true; 497f5c6743cSAndreas Gohr } 498f5c6743cSAndreas Gohr} 499f5c6743cSAndreas Gohr 500f5c6743cSAndreas Gohr/** 501*26714386SAndreas Gohr * checks it is windows OS 502*26714386SAndreas Gohr * @return bool 503*26714386SAndreas Gohr */ 504*26714386SAndreas Gohrfunction isWindows() { 505*26714386SAndreas Gohr return (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? true : false; 506*26714386SAndreas Gohr} 507*26714386SAndreas Gohr 508*26714386SAndreas Gohr/** 5093816dcbcSAndreas Gohr * print a nice message even if no styles are loaded yet. 510f50a239bSTakamura * 511f50a239bSTakamura * @param integer|string $msg 5123816dcbcSAndreas Gohr */ 5133816dcbcSAndreas Gohrfunction nice_die($msg){ 5143816dcbcSAndreas Gohr echo<<<EOT 515c8839c22SAnika Henke<!DOCTYPE html> 5163816dcbcSAndreas Gohr<html> 5173816dcbcSAndreas Gohr<head><title>DokuWiki Setup Error</title></head> 5183816dcbcSAndreas Gohr<body style="font-family: Arial, sans-serif"> 5193816dcbcSAndreas Gohr <div style="width:60%; margin: auto; background-color: #fcc; 5203816dcbcSAndreas Gohr border: 1px solid #faa; padding: 0.5em 1em;"> 5213816dcbcSAndreas Gohr <h1 style="font-size: 120%">DokuWiki Setup Error</h1> 5223816dcbcSAndreas Gohr <p>$msg</p> 5233816dcbcSAndreas Gohr </div> 5243816dcbcSAndreas Gohr</body> 5253816dcbcSAndreas Gohr</html> 5263816dcbcSAndreas GohrEOT; 5270a4266d4SElan Ruusamäe exit(1); 5283816dcbcSAndreas Gohr} 5293816dcbcSAndreas Gohr 53000976812SAndreas Gohr/** 53100976812SAndreas Gohr * A realpath() replacement 53200976812SAndreas Gohr * 53300976812SAndreas Gohr * This function behaves similar to PHP's realpath() but does not resolve 53400976812SAndreas Gohr * symlinks or accesses upper directories 53500976812SAndreas Gohr * 5364761d30cSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 53700976812SAndreas Gohr * @author <richpageau at yahoo dot co dot uk> 53859752844SAnders Sandblad * @link http://php.net/manual/en/function.realpath.php#75992 539f50a239bSTakamura * 540f50a239bSTakamura * @param string $path 541f50a239bSTakamura * @param bool $exists 542f50a239bSTakamura * 543f50a239bSTakamura * @return bool|string 54400976812SAndreas Gohr */ 545b328697dSAndreas Gohrfunction fullpath($path,$exists=false){ 5464761d30cSAndreas Gohr static $run = 0; 5474761d30cSAndreas Gohr $root = ''; 548f0a201c5SChris Smith $iswin = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' || @$GLOBALS['DOKU_UNITTEST_ASSUME_WINDOWS']); 54900976812SAndreas Gohr 5504761d30cSAndreas Gohr // find the (indestructable) root of the path - keeps windows stuff intact 5514761d30cSAndreas Gohr if($path{0} == '/'){ 5524761d30cSAndreas Gohr $root = '/'; 5534761d30cSAndreas Gohr }elseif($iswin){ 5544761d30cSAndreas Gohr // match drive letter and UNC paths 5554761d30cSAndreas Gohr if(preg_match('!^([a-zA-z]:)(.*)!',$path,$match)){ 556b9c4302bSAndreas Gohr $root = $match[1].'/'; 5574761d30cSAndreas Gohr $path = $match[2]; 5584761d30cSAndreas Gohr }else if(preg_match('!^(\\\\\\\\[^\\\\/]+\\\\[^\\\\/]+[\\\\/])(.*)!',$path,$match)){ 5594761d30cSAndreas Gohr $root = $match[1]; 5604761d30cSAndreas Gohr $path = $match[2]; 56100976812SAndreas Gohr } 5624761d30cSAndreas Gohr } 5634761d30cSAndreas Gohr $path = str_replace('\\','/',$path); 5644761d30cSAndreas Gohr 5654761d30cSAndreas Gohr // if the given path wasn't absolute already, prepend the script path and retry 5664761d30cSAndreas Gohr if(!$root){ 5674761d30cSAndreas Gohr $base = dirname($_SERVER['SCRIPT_FILENAME']); 5684761d30cSAndreas Gohr $path = $base.'/'.$path; 5694761d30cSAndreas Gohr if($run == 0){ // avoid endless recursion when base isn't absolute for some reason 5704761d30cSAndreas Gohr $run++; 571b328697dSAndreas Gohr return fullpath($path,$exists); 5724761d30cSAndreas Gohr } 5734761d30cSAndreas Gohr } 5744761d30cSAndreas Gohr $run = 0; 57500976812SAndreas Gohr 57600976812SAndreas Gohr // canonicalize 57700976812SAndreas Gohr $path=explode('/', $path); 57800976812SAndreas Gohr $newpath=array(); 579ef38bfe8SAndreas Gohr foreach($path as $p) { 580ef38bfe8SAndreas Gohr if ($p === '' || $p === '.') continue; 581ef38bfe8SAndreas Gohr if ($p==='..') { 58200976812SAndreas Gohr array_pop($newpath); 58300976812SAndreas Gohr continue; 58400976812SAndreas Gohr } 585ef38bfe8SAndreas Gohr array_push($newpath, $p); 58600976812SAndreas Gohr } 5874761d30cSAndreas Gohr $finalpath = $root.implode('/', $newpath); 58800976812SAndreas Gohr 5896b9c156cSAnika Henke // check for existence when needed (except when unit testing) 59079e79377SAndreas Gohr if($exists && !defined('DOKU_UNITTEST') && !file_exists($finalpath)) { 5914761d30cSAndreas Gohr return false; 59200976812SAndreas Gohr } 5934761d30cSAndreas Gohr return $finalpath; 59400976812SAndreas Gohr} 59500976812SAndreas Gohr 596