1ed7b5f09Sandi<?php 2d4f83172SAndreas Gohr 3ed7b5f09Sandi/** 4ed7b5f09Sandi * Initialize some defaults needed for DokuWiki 5ed7b5f09Sandi */ 6d4f83172SAndreas Gohr 724870174SAndreas Gohruse dokuwiki\Extension\PluginController; 824870174SAndreas Gohruse dokuwiki\ErrorHandler; 924870174SAndreas Gohruse dokuwiki\Input\Input; 10cbb44eabSAndreas Gohruse dokuwiki\Extension\Event; 11e1d9dcc8SAndreas Gohruse dokuwiki\Extension\EventHandler; 12e1d9dcc8SAndreas Gohr 133272d797SAndreas Gohr/** 143272d797SAndreas Gohr * timing Dokuwiki execution 15f50a239bSTakamura * 16f50a239bSTakamura * @param integer $start 17f50a239bSTakamura * 18f50a239bSTakamura * @return mixed 193272d797SAndreas Gohr */ 20d868eb89SAndreas Gohrfunction delta_time($start = 0) 21d868eb89SAndreas Gohr{ 22ac4be4d7SPiyush Mishra return microtime(true) - ((float)$start); 23a609a9ccSBen Coburn} 24a609a9ccSBen Coburndefine('DOKU_START_TIME', delta_time()); 25a609a9ccSBen Coburn 26ccaeaa85SAndreas Gohrglobal $config_cascade; 2724870174SAndreas Gohr$config_cascade = []; 28ccaeaa85SAndreas Gohr 2948beefecSAndreas Gohr// if available load a preload config file 3024870174SAndreas Gohr$preload = fullpath(__DIR__) . '/preload.php'; 3179e79377SAndreas Gohrif (file_exists($preload)) include($preload); 3248beefecSAndreas Gohr 33ed7b5f09Sandi// define the include path 3424870174SAndreas Gohrif (!defined('DOKU_INC')) define('DOKU_INC', fullpath(__DIR__ . '/../') . '/'); 35ad15db82Sandi 36c2a6d816SAndreas Gohr// define Plugin dir 37c2a6d816SAndreas Gohrif (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/'); 38c2a6d816SAndreas Gohr 39e7cb32dcSAndreas Gohr// define config path (packagers may want to change this to /etc/dokuwiki/) 40b7551a6dSEsther Brunnerif (!defined('DOKU_CONF')) define('DOKU_CONF', DOKU_INC . 'conf/'); 41e7cb32dcSAndreas Gohr 42bad905f1SBen Coburn// check for error reporting override or set error reporting to sane values 4379e79377SAndreas Gohrif (!defined('DOKU_E_LEVEL') && file_exists(DOKU_CONF . 'report_e_all')) { 44bad905f1SBen Coburn define('DOKU_E_LEVEL', E_ALL); 45bad905f1SBen Coburn} 46fc80ed59SAndreas Gohrif (!defined('DOKU_E_LEVEL')) { 47e086ef6cSlvl1ch43l error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED); 48fc80ed59SAndreas Gohr} else { 49fc80ed59SAndreas Gohr error_reporting(DOKU_E_LEVEL); 50fc80ed59SAndreas Gohr} 51c53ea5f2Sandi 526a9b3303SAndreas Gohr// autoloader 536a9b3303SAndreas Gohrrequire_once(DOKU_INC . 'inc/load.php'); 546a9b3303SAndreas Gohr 55a69722b3SAndreas Gohr// avoid caching issues #1594 56a69722b3SAndreas Gohrheader('Vary: Cookie'); 57a69722b3SAndreas Gohr 5850602150SBen Coburn// init memory caches 59db959ae3SAndreas Gohrglobal $cache_revinfo; 6024870174SAndreas Gohr $cache_revinfo = []; 61db959ae3SAndreas Gohrglobal $cache_wikifn; 6224870174SAndreas Gohr $cache_wikifn = []; 63db959ae3SAndreas Gohrglobal $cache_cleanid; 6424870174SAndreas Gohr $cache_cleanid = []; 65db959ae3SAndreas Gohrglobal $cache_authname; 6624870174SAndreas Gohr $cache_authname = []; 67db959ae3SAndreas Gohrglobal $cache_metadata; 6824870174SAndreas Gohr $cache_metadata = []; 6950602150SBen Coburn 70cca94fbcSRoland Hager// always include 'inc/config_cascade.php' 71cca94fbcSRoland Hager// previously in preload.php set fields of $config_cascade will be merged with the defaults 72e6a6dbfeSAndreas Gohrinclude(DOKU_INC . 'inc/config_cascade.php'); 73cb043f52SChris Smith 744724a577Sandi//prepare config array() 75ee20e7d1Sandiglobal $conf; 7624870174SAndreas Gohr$conf = []; 774724a577Sandi 78cb043f52SChris Smith// load the global config file(s) 7924870174SAndreas Gohrforeach (['default', 'local', 'protected'] as $config_group) { 80f8121585SChris Smith if (empty($config_cascade['main'][$config_group])) continue; 81b303b92cSChris Smith foreach ($config_cascade['main'][$config_group] as $config_file) { 8279e79377SAndreas Gohr if (file_exists($config_file)) { 83f8121585SChris Smith include($config_file); 84f8121585SChris Smith } 85cb043f52SChris Smith } 860a6ead41SAndreas Gohr} 87ad15db82Sandi 886a9b3303SAndreas Gohr// precalculate file creation modes 896a9b3303SAndreas Gohrinit_creationmodes(); 906a9b3303SAndreas Gohr 916a9b3303SAndreas Gohr// make real paths and check them 926a9b3303SAndreas Gohrinit_paths(); 936a9b3303SAndreas Gohrinit_files(); 946a9b3303SAndreas Gohr 95066fee30SAndreas Gohr//prepare license array() 96066fee30SAndreas Gohrglobal $license; 9724870174SAndreas Gohr$license = []; 98066fee30SAndreas Gohr 99066fee30SAndreas Gohr// load the license file(s) 10024870174SAndreas Gohrforeach (['default', 'local'] as $config_group) { 101f8121585SChris Smith if (empty($config_cascade['license'][$config_group])) continue; 102f8121585SChris Smith foreach ($config_cascade['license'][$config_group] as $config_file) { 10379e79377SAndreas Gohr if (file_exists($config_file)) { 104f8121585SChris Smith include($config_file); 105f8121585SChris Smith } 106f8121585SChris Smith } 107066fee30SAndreas Gohr} 108066fee30SAndreas Gohr 1091f8eb24fSAndreas Gohr// set timezone (as in pre 5.3.0 days) 1101f8eb24fSAndreas Gohrdate_default_timezone_set(@date_default_timezone_get()); 1111f8eb24fSAndreas Gohr 1126a9b3303SAndreas Gohr 1136a9b3303SAndreas Gohr// don't let cookies ever interfere with request vars 1146a9b3303SAndreas Gohr$_REQUEST = array_merge($_GET, $_POST); 1156a9b3303SAndreas Gohr// input handle class 1166a9b3303SAndreas Gohrglobal $INPUT; 1176a9b3303SAndreas Gohr$INPUT = new Input(); 1186a9b3303SAndreas Gohr 1196a9b3303SAndreas Gohr 120ed7b5f09Sandi// define baseURL 1214b1a4e04SAndreas Gohrif (!defined('DOKU_REL')) define('DOKU_REL', getBaseURL(false)); 122ed7b5f09Sandiif (!defined('DOKU_URL')) define('DOKU_URL', getBaseURL(true)); 1234b1a4e04SAndreas Gohrif (!defined('DOKU_BASE')) { 1244b1a4e04SAndreas Gohr if ($conf['canonical']) { 1254b1a4e04SAndreas Gohr define('DOKU_BASE', DOKU_URL); 1264b1a4e04SAndreas Gohr } else { 1274b1a4e04SAndreas Gohr define('DOKU_BASE', DOKU_REL); 1284b1a4e04SAndreas Gohr } 1294b1a4e04SAndreas Gohr} 1304b1a4e04SAndreas Gohr 131b8595a66SAndreas Gohr// define whitespace 132b4f2363aSAndreas Gohrif (!defined('NL')) define('NL', "\n"); 133b8595a66SAndreas Gohrif (!defined('DOKU_LF')) define('DOKU_LF', "\n"); 134b8595a66SAndreas Gohrif (!defined('DOKU_TAB')) define('DOKU_TAB', "\t"); 135ed7b5f09Sandi 136656c8fb3SAndreas Gohr// define cookie and session id, append server port when securecookie is configured FS#1664 137fb97a12aSMichael Großeif (!defined('DOKU_COOKIE')) { 13824870174SAndreas Gohr $serverPort = $_SERVER['SERVER_PORT'] ?? ''; 139fb97a12aSMichael Große define('DOKU_COOKIE', 'DW' . md5(DOKU_REL . (($conf['securecookie']) ? $serverPort : ''))); 140abc9c0d2SAndreas Gohr unset($serverPort); 141fb97a12aSMichael Große} 142ee20e7d1Sandi 143ed7b5f09Sandi// define main script 144ed7b5f09Sandiif (!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT', 'doku.php'); 145ed7b5f09Sandi 146c163dbefSMichael Großeif (!defined('DOKU_TPL')) { 147c163dbefSMichael Große /** 148c163dbefSMichael Große * @deprecated 2012-10-13 replaced by more dynamic method 149c163dbefSMichael Große * @see tpl_basedir() 150c163dbefSMichael Große */ 151c163dbefSMichael Große define('DOKU_TPL', DOKU_BASE . 'lib/tpl/' . $conf['template'] . '/'); 152c163dbefSMichael Große} 1536b13307fSandi 154c163dbefSMichael Großeif (!defined('DOKU_TPLINC')) { 155c163dbefSMichael Große /** 156c163dbefSMichael Große * @deprecated 2012-10-13 replaced by more dynamic method 157c163dbefSMichael Große * @see tpl_incdir() 158c163dbefSMichael Große */ 159c163dbefSMichael Große define('DOKU_TPLINC', DOKU_INC . 'lib/tpl/' . $conf['template'] . '/'); 160c163dbefSMichael Große} 16178a6aeb1SAndreas Gohr 162ed7b5f09Sandi// make session rewrites XHTML compliant 1633fc74836Sandi@ini_set('arg_separator.output', '&'); 164ed7b5f09Sandi 165d7e6bba9SAndreas Gohr// make sure global zlib does not interfere FS#1132 166d7e6bba9SAndreas Gohr@ini_set('zlib.output_compression', 'off'); 167d7e6bba9SAndreas Gohr 1686deb5405SAndreas Gohr// increase PCRE backtrack limit 1696deb5405SAndreas Gohr@ini_set('pcre.backtrack_limit', '20971520'); 1706deb5405SAndreas Gohr 17198bda4fdSAndreas Gohr// enable gzip compression if supported 17224870174SAndreas Gohr$httpAcceptEncoding = $_SERVER['HTTP_ACCEPT_ENCODING'] ?? ''; 173fb97a12aSMichael Große$conf['gzip_output'] &= (strpos($httpAcceptEncoding, 'gzip') !== false); 17465f6e7d6SMichael Hamannglobal $ACT; 1757d34963bSAndreas Gohrif ( 1767d34963bSAndreas Gohr $conf['gzip_output'] && 1773138b5c7SAndreas Gohr !defined('DOKU_DISABLE_GZIP_OUTPUT') && 17865f6e7d6SMichael Hamann function_exists('ob_gzhandler') && 17999e10b7fSMichael Hamann // Disable compression when a (compressed) sitemap might be delivered 18065f6e7d6SMichael Hamann // See https://bugs.dokuwiki.org/index.php?do=details&task_id=2576 1817d34963bSAndreas Gohr $ACT != 'sitemap' 1827d34963bSAndreas Gohr) { 1833138b5c7SAndreas Gohr ob_start('ob_gzhandler'); 1843138b5c7SAndreas Gohr} 1853138b5c7SAndreas Gohr 186ed7b5f09Sandi// init session 1876534245aSAndreas Gohrif (!headers_sent() && !defined('NOSESSION')) { 188c09f0eb1SGerrit Uitslag if (!defined('DOKU_SESSION_NAME')) define('DOKU_SESSION_NAME', "DokuWiki"); 189c09f0eb1SGerrit Uitslag if (!defined('DOKU_SESSION_LIFETIME')) define('DOKU_SESSION_LIFETIME', 0); 19055a71a16SGerrit Uitslag if (!defined('DOKU_SESSION_PATH')) { 19173ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 19255a71a16SGerrit Uitslag define('DOKU_SESSION_PATH', $cookieDir); 193f5c6743cSAndreas Gohr } 194c09f0eb1SGerrit Uitslag if (!defined('DOKU_SESSION_DOMAIN')) define('DOKU_SESSION_DOMAIN', ''); 195c09f0eb1SGerrit Uitslag 1966eb3cdf6SAndreas Gohr // start the session 1976eb3cdf6SAndreas Gohr init_session(); 19814a122deSAndreas Gohr 19914a122deSAndreas Gohr // load left over messages 20014a122deSAndreas Gohr if (isset($_SESSION[DOKU_COOKIE]['msg'])) { 20114a122deSAndreas Gohr $MSG = $_SESSION[DOKU_COOKIE]['msg']; 20214a122deSAndreas Gohr unset($_SESSION[DOKU_COOKIE]['msg']); 20314a122deSAndreas Gohr } 204bad31ae9SAndreas Gohr} 205ed7b5f09Sandi 206a1637ffdSAndreas Gohr 2073dea4ebcSAndreas Gohr// we don't want a purge URL to be digged 2080e80bb5eSChristopher Smithif (isset($_REQUEST['purge']) && !empty($_SERVER['HTTP_REFERER'])) unset($_REQUEST['purge']); 2093dea4ebcSAndreas Gohr 210ed7b5f09Sandi 211f1986589SMichael Klier// setup plugin controller class (can be overwritten in preload.php) 212f1986589SMichael Klierglobal $plugin_controller_class, $plugin_controller; 21324870174SAndreas Gohrif (empty($plugin_controller_class)) $plugin_controller_class = PluginController::class; 214f1986589SMichael Klier 215642e976cSAndreas Gohr// from now on everything is an exception 21624870174SAndreas GohrErrorHandler::register(); 217642e976cSAndreas Gohr 2180f8f7aaaSDanny Lin// disable gzip if not available 21913c37900SAndreas Gohrdefine('DOKU_HAS_BZIP', function_exists('bzopen')); 22013c37900SAndreas Gohrdefine('DOKU_HAS_GZIP', function_exists('gzopen')); 22113c37900SAndreas Gohrif ($conf['compression'] == 'bz2' && !DOKU_HAS_BZIP) { 2220f8f7aaaSDanny Lin $conf['compression'] = 'gz'; 2230f8f7aaaSDanny Lin} 22413c37900SAndreas Gohrif ($conf['compression'] == 'gz' && !DOKU_HAS_GZIP) { 2250f8f7aaaSDanny Lin $conf['compression'] = 0; 2260f8f7aaaSDanny Lin} 2270f8f7aaaSDanny Lin 228f1986589SMichael Klier// initialize plugin controller 229f1986589SMichael Klier$plugin_controller = new $plugin_controller_class(); 230f1986589SMichael Klier 231f1986589SMichael Klier// initialize the event handler 232f1986589SMichael Klierglobal $EVENT_HANDLER; 233e1d9dcc8SAndreas Gohr$EVENT_HANDLER = new EventHandler(); 234f1986589SMichael Klier 2356d06b26aSDominik Eckelmann$local = $conf['lang']; 236cbb44eabSAndreas GohrEvent::createAndTrigger('INIT_LANG_LOAD', $local, 'init_lang', true); 2376d06b26aSDominik Eckelmann 2386d06b26aSDominik Eckelmann 23916905344SAndreas Gohr// setup authentication system 240c7cb395cSAdrian Langif (!defined('NOSESSION')) { 24116905344SAndreas Gohr auth_setup(); 242c7cb395cSAdrian Lang} 243f62ea8a1Sandi 2445ec3fefcSAndreas Gohr// setup mail system 2455ec3fefcSAndreas Gohrmail_setup(); 2465ec3fefcSAndreas Gohr 247042b9fecSAndreas Gohr$nil = null; 248042b9fecSAndreas GohrEvent::createAndTrigger('DOKUWIKI_INIT_DONE', $nil, null, false); 249042b9fecSAndreas Gohr 250f62ea8a1Sandi/** 2516eb3cdf6SAndreas Gohr * Initializes the session 2526eb3cdf6SAndreas Gohr * 2536eb3cdf6SAndreas Gohr * Makes sure the passed session cookie is valid, invalid ones are ignored an a new session ID is issued 2546eb3cdf6SAndreas Gohr * 2556eb3cdf6SAndreas Gohr * @link http://stackoverflow.com/a/33024310/172068 256924e477eSAndreas Gohr * @link http://php.net/manual/en/session.configuration.php#ini.session.sid-length 2576eb3cdf6SAndreas Gohr */ 258d868eb89SAndreas Gohrfunction init_session() 259d868eb89SAndreas Gohr{ 2606eb3cdf6SAndreas Gohr global $conf; 2616eb3cdf6SAndreas Gohr session_name(DOKU_SESSION_NAME); 262bf8392ebSAndreas Gohr session_set_cookie_params([ 263bf8392ebSAndreas Gohr 'lifetime' => DOKU_SESSION_LIFETIME, 264bf8392ebSAndreas Gohr 'path' => DOKU_SESSION_PATH, 265bf8392ebSAndreas Gohr 'domain' => DOKU_SESSION_DOMAIN, 266*33cb4e01SAndreas Gohr 'secure' => ($conf['securecookie'] && \dokuwiki\Ip::isSsl()), 267bf8392ebSAndreas Gohr 'httponly' => true, 268bf8392ebSAndreas Gohr 'samesite' => 'Lax', 269bf8392ebSAndreas Gohr ]); 2706eb3cdf6SAndreas Gohr 2716eb3cdf6SAndreas Gohr // make sure the session cookie contains a valid session ID 272924e477eSAndreas Gohr if (isset($_COOKIE[DOKU_SESSION_NAME]) && !preg_match('/^[-,a-zA-Z0-9]{22,256}$/', $_COOKIE[DOKU_SESSION_NAME])) { 2736eb3cdf6SAndreas Gohr unset($_COOKIE[DOKU_SESSION_NAME]); 2746eb3cdf6SAndreas Gohr } 2756eb3cdf6SAndreas Gohr 2766eb3cdf6SAndreas Gohr session_start(); 2776eb3cdf6SAndreas Gohr} 2786eb3cdf6SAndreas Gohr 2796eb3cdf6SAndreas Gohr 2806eb3cdf6SAndreas Gohr/** 28198407a7aSandi * Checks paths from config file 28298407a7aSandi */ 283d868eb89SAndreas Gohrfunction init_paths() 284d868eb89SAndreas Gohr{ 28598407a7aSandi global $conf; 28698407a7aSandi 2870ecde6ceSAndreas Gohr $paths = [ 2880ecde6ceSAndreas Gohr 'datadir' => 'pages', 28998407a7aSandi 'olddir' => 'attic', 29098407a7aSandi 'mediadir' => 'media', 291e4f389efSKate Arzamastseva 'mediaolddir' => 'media_attic', 29298407a7aSandi 'metadir' => 'meta', 293e4f389efSKate Arzamastseva 'mediametadir' => 'media_meta', 29498407a7aSandi 'cachedir' => 'cache', 295579b0f7eSTNHarris 'indexdir' => 'index', 296de33a58fSMichael Klier 'lockdir' => 'locks', 2970ecde6ceSAndreas Gohr 'tmpdir' => 'tmp', 2980ecde6ceSAndreas Gohr 'logdir' => 'log', 2990ecde6ceSAndreas Gohr ]; 30098407a7aSandi 30198407a7aSandi foreach ($paths as $c => $p) { 3027f086b67SAnika Henke $path = empty($conf[$c]) ? $conf['savedir'] . '/' . $p : $conf[$c]; 3036b9c156cSAnika Henke $conf[$c] = init_path($path); 304697a39aeSAndreas Gohr if (empty($conf[$c])) { 305697a39aeSAndreas Gohr $path = fullpath($path); 3066b9c156cSAnika Henke nice_die("The $c ('$p') at $path is not found, isn't accessible or writable. 30769dc3177SAndreas Gohr You should check your config and permission settings. 30869dc3177SAndreas Gohr Or maybe you want to <a href=\"install.php\">run the 30969dc3177SAndreas Gohr installer</a>?"); 31098407a7aSandi } 311697a39aeSAndreas Gohr } 31271726d78SBen Coburn 31371726d78SBen Coburn // path to old changelog only needed for upgrading 31464159a61SAndreas Gohr $conf['changelog_old'] = init_path( 31524870174SAndreas Gohr $conf['changelog'] ?? $conf['savedir'] . '/changes.log' 31664159a61SAndreas Gohr ); 317177d6836SAndreas Gohr if ($conf['changelog_old'] == '') { 318d4f83172SAndreas Gohr unset($conf['changelog_old']); 319d4f83172SAndreas Gohr } 32071726d78SBen Coburn // hardcoded changelog because it is now a cache that lives in meta 32171726d78SBen Coburn $conf['changelog'] = $conf['metadir'] . '/_dokuwiki.changes'; 32299c8d7f2Smichael $conf['media_changelog'] = $conf['metadir'] . '/_media.changes'; 32398407a7aSandi} 32498407a7aSandi 32538fb1fc7SGerrit Uitslag/** 32638fb1fc7SGerrit Uitslag * Load the language strings 32738fb1fc7SGerrit Uitslag * 32838fb1fc7SGerrit Uitslag * @param string $langCode language code, as passed by event handler 32938fb1fc7SGerrit Uitslag */ 330d868eb89SAndreas Gohrfunction init_lang($langCode) 331d868eb89SAndreas Gohr{ 3326d06b26aSDominik Eckelmann //prepare language array 333dd7a6159SGerrit Uitslag global $lang, $config_cascade; 33424870174SAndreas Gohr $lang = []; 3356d06b26aSDominik Eckelmann 3366d06b26aSDominik Eckelmann //load the language files 3371d82c8d3SChristopher Smith require(DOKU_INC . 'inc/lang/en/lang.php'); 338dd7a6159SGerrit Uitslag foreach ($config_cascade['lang']['core'] as $config_file) { 33979e79377SAndreas Gohr if (file_exists($config_file . 'en/lang.php')) { 340dd7a6159SGerrit Uitslag include($config_file . 'en/lang.php'); 341dd7a6159SGerrit Uitslag } 342dd7a6159SGerrit Uitslag } 343dd7a6159SGerrit Uitslag 3446d06b26aSDominik Eckelmann if ($langCode && $langCode != 'en') { 3456d06b26aSDominik Eckelmann if (file_exists(DOKU_INC . "inc/lang/$langCode/lang.php")) { 3461d82c8d3SChristopher Smith require(DOKU_INC . "inc/lang/$langCode/lang.php"); 3476d06b26aSDominik Eckelmann } 348dd7a6159SGerrit Uitslag foreach ($config_cascade['lang']['core'] as $config_file) { 34979e79377SAndreas Gohr if (file_exists($config_file . "$langCode/lang.php")) { 350dd7a6159SGerrit Uitslag include($config_file . "$langCode/lang.php"); 3516d06b26aSDominik Eckelmann } 352dd7a6159SGerrit Uitslag } 3536d06b26aSDominik Eckelmann } 3546d06b26aSDominik Eckelmann} 3556d06b26aSDominik Eckelmann 35698407a7aSandi/** 3576b9c156cSAnika Henke * Checks the existence of certain files and creates them if missing. 3587367b368SAndreas Gohr */ 359d868eb89SAndreas Gohrfunction init_files() 360d868eb89SAndreas Gohr{ 3617367b368SAndreas Gohr global $conf; 3620d8850c4SAndreas Gohr 36324870174SAndreas Gohr $files = [$conf['indexdir'] . '/page.idx']; 3647367b368SAndreas Gohr 3657367b368SAndreas Gohr foreach ($files as $file) { 36679e79377SAndreas Gohr if (!file_exists($file)) { 3670d8850c4SAndreas Gohr $fh = @fopen($file, 'a'); 3680d8850c4SAndreas Gohr if ($fh) { 3697367b368SAndreas Gohr fclose($fh); 3703aa75874Smovatica if ($conf['fperm']) chmod($file, $conf['fperm']); 3710d8850c4SAndreas Gohr } else { 3723816dcbcSAndreas Gohr nice_die("$file is not writable. Check your permissions settings!"); 3730d8850c4SAndreas Gohr } 3747367b368SAndreas Gohr } 3757367b368SAndreas Gohr } 3767367b368SAndreas Gohr} 3777367b368SAndreas Gohr 3787367b368SAndreas Gohr/** 3790d8850c4SAndreas Gohr * Returns absolute path 380f62ea8a1Sandi * 3810d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC. 3827f086b67SAnika Henke * Check for accessibility on directories as well. 3830d8850c4SAndreas Gohr * 3840d8850c4SAndreas Gohr * @author Andreas Gohr <[email protected]> 385f50a239bSTakamura * 386f50a239bSTakamura * @param string $path 387f50a239bSTakamura * 388f50a239bSTakamura * @return bool|string 389f62ea8a1Sandi */ 390d868eb89SAndreas Gohrfunction init_path($path) 391d868eb89SAndreas Gohr{ 3926b9c156cSAnika Henke // check existence 39300976812SAndreas Gohr $p = fullpath($path); 39479e79377SAndreas Gohr if (!file_exists($p)) { 39500976812SAndreas Gohr $p = fullpath(DOKU_INC . $path); 39679e79377SAndreas Gohr if (!file_exists($p)) { 3978fc4e739Sandi return ''; 398f62ea8a1Sandi } 3990d8850c4SAndreas Gohr } 4000d8850c4SAndreas Gohr 4010d8850c4SAndreas Gohr // check writability 4020d8850c4SAndreas Gohr if (!@is_writable($p)) { 4030d8850c4SAndreas Gohr return ''; 4040d8850c4SAndreas Gohr } 4050d8850c4SAndreas Gohr 4060d8850c4SAndreas Gohr // check accessability (execute bit) for directories 40779e79377SAndreas Gohr if (@is_dir($p) && !file_exists("$p/.")) { 4080d8850c4SAndreas Gohr return ''; 4090d8850c4SAndreas Gohr } 4100d8850c4SAndreas Gohr 4110d8850c4SAndreas Gohr return $p; 4120d8850c4SAndreas Gohr} 4138c4f28e8Sjan 414ed7b5f09Sandi/** 4151ca31cfeSAndreas Gohr * Sets the internal config values fperm and dperm which, when set, 4161ca31cfeSAndreas Gohr * will be used to change the permission of a newly created dir or 4171ca31cfeSAndreas Gohr * file with chmod. Considers the influence of the system's umask 4181ca31cfeSAndreas Gohr * setting the values only if needed. 4191ca31cfeSAndreas Gohr */ 420d868eb89SAndreas Gohrfunction init_creationmodes() 421d868eb89SAndreas Gohr{ 4221ca31cfeSAndreas Gohr global $conf; 4231ca31cfeSAndreas Gohr 4241ca31cfeSAndreas Gohr // Legacy support for old umask/dmask scheme 4251ca31cfeSAndreas Gohr unset($conf['dmask']); 4261ca31cfeSAndreas Gohr unset($conf['fmask']); 4271ca31cfeSAndreas Gohr unset($conf['umask']); 42823420346SDamien Regad 42923420346SDamien Regad $conf['fperm'] = false; 43023420346SDamien Regad $conf['dperm'] = false; 4311ca31cfeSAndreas Gohr 4329f3cdec3SAndreas Gohr // get system umask, fallback to 0 if none available 4339f3cdec3SAndreas Gohr $umask = @umask(); 4349f3cdec3SAndreas Gohr if (!$umask) $umask = 0000; 4351ca31cfeSAndreas Gohr 4361ca31cfeSAndreas Gohr // check what is set automatically by the system on file creation 4371ca31cfeSAndreas Gohr // and set the fperm param if it's not what we want 438bd539124SAndreas Gohr $auto_fmode = 0666 & ~$umask; 4391ca31cfeSAndreas Gohr if ($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode']; 4401ca31cfeSAndreas Gohr 441bd539124SAndreas Gohr // check what is set automatically by the system on directory creation 442bd539124SAndreas Gohr // and set the dperm param if it's not what we want. 443bd539124SAndreas Gohr $auto_dmode = 0777 & ~$umask; 4441ca31cfeSAndreas Gohr if ($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode']; 4451ca31cfeSAndreas Gohr} 4461ca31cfeSAndreas Gohr 4471ca31cfeSAndreas Gohr/** 448ed7b5f09Sandi * Returns the full absolute URL to the directory where 449ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash) 450ed7b5f09Sandi * 451585bf44eSChristopher Smith * !! Can not access $_SERVER values through $INPUT 452585bf44eSChristopher Smith * !! here as this function is called before $INPUT is 453585bf44eSChristopher Smith * !! initialized. 454585bf44eSChristopher Smith * 455ed7b5f09Sandi * @author Andreas Gohr <[email protected]> 456f50a239bSTakamura * 4573bfb10aeSAndreas Gohr * @param null|bool $abs Return an absolute URL? (null defaults to $conf['canonical']) 458f50a239bSTakamura * 459f50a239bSTakamura * @return string 460ed7b5f09Sandi */ 461d868eb89SAndreas Gohrfunction getBaseURL($abs = null) 462d868eb89SAndreas Gohr{ 463ed7b5f09Sandi global $conf; 4643bfb10aeSAndreas Gohr 4653bfb10aeSAndreas Gohr $abs ??= $conf['canonical']; 466ed7b5f09Sandi 4671858e4d7SGerry Weißbach if (!empty($conf['basedir'])) { 46846c73e01SChris Smith $dir = $conf['basedir']; 46989aa05dbSAndreas Gohr } elseif (substr($_SERVER['SCRIPT_NAME'], -4) == '.php') { 47046c73e01SChris Smith $dir = dirname($_SERVER['SCRIPT_NAME']); 47189aa05dbSAndreas Gohr } elseif (substr($_SERVER['PHP_SELF'], -4) == '.php') { 47246c73e01SChris Smith $dir = dirname($_SERVER['PHP_SELF']); 473093ec9e4Sandi } elseif ($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']) { 474dccd6b2bSAndreas Gohr $dir = preg_replace( 475dccd6b2bSAndreas Gohr '/^' . preg_quote($_SERVER['DOCUMENT_ROOT'], '/') . '/', 476dccd6b2bSAndreas Gohr '', 477dccd6b2bSAndreas Gohr $_SERVER['SCRIPT_FILENAME'] 478dccd6b2bSAndreas Gohr ); 47946c73e01SChris Smith $dir = dirname('/' . $dir); 48092b83b77Sandi } else { 481ac56bec8SAndreas Gohr $dir = ''; //probably wrong, but we assume it's in the root 48292b83b77Sandi } 483ed7b5f09Sandi 48446c73e01SChris Smith $dir = str_replace('\\', '/', $dir); // bugfix for weird WIN behaviour 48546c73e01SChris Smith $dir = preg_replace('#//+#', '/', "/$dir/"); // ensure leading and trailing slashes 486ed7b5f09Sandi 487f62ea8a1Sandi //handle script in lib/exe dir 488f62ea8a1Sandi $dir = preg_replace('!lib/exe/$!', '', $dir); 489f62ea8a1Sandi 490488d5fa0SMichael Klier [email protected] //handle script in lib/plugins dir 491488d5fa0SMichael Klier [email protected] $dir = preg_replace('!lib/plugins/.*$!', '', $dir); 492488d5fa0SMichael Klier [email protected] 493ed7b5f09Sandi //finish here for relative URLs 494ed7b5f09Sandi if (!$abs) return $dir; 495ed7b5f09Sandi 49664159a61SAndreas Gohr //use config if available, trim any slash from end of baseurl to avoid multiple consecutive slashes in the path 4971858e4d7SGerry Weißbach if (!empty($conf['baseurl'])) return rtrim($conf['baseurl'], '/') . $dir; 498ef7b3ecdSAndreas Gohr 499e82e3526SAndreas Gohr //split hostheader into host and port 500*33cb4e01SAndreas Gohr $hostname = \dokuwiki\Ip::hostName(); 501*33cb4e01SAndreas Gohr $parsed_host = parse_url('http://' . $hostname); 5023bfb10aeSAndreas Gohr $host = $parsed_host['host'] ?? ''; 5033bfb10aeSAndreas Gohr $port = $parsed_host['port'] ?? ''; 5045627186cSAndreas Gohr 505*33cb4e01SAndreas Gohr if (!\dokuwiki\Ip::isSsl()) { 506ed7b5f09Sandi $proto = 'http://'; 507e82e3526SAndreas Gohr if ($port == '80') { 508ed7b5f09Sandi $port = ''; 509ed7b5f09Sandi } 510ed7b5f09Sandi } else { 511ed7b5f09Sandi $proto = 'https://'; 512e82e3526SAndreas Gohr if ($port == '443') { 513ed7b5f09Sandi $port = ''; 514ed7b5f09Sandi } 515ed7b5f09Sandi } 516ed7b5f09Sandi 517c66972f2SAdrian Lang if ($port !== '') $port = ':' . $port; 518e82e3526SAndreas Gohr 519ed7b5f09Sandi return $proto . $host . $port . $dir; 520ed7b5f09Sandi} 521ed7b5f09Sandi 522b000c6d4Sandi/** 523*33cb4e01SAndreas Gohr * @deprecated 2025-06-03 524f5c6743cSAndreas Gohr */ 525d868eb89SAndreas Gohrfunction is_ssl() 526d868eb89SAndreas Gohr{ 527*33cb4e01SAndreas Gohr dbg_deprecated('Ip::isSsl()'); 528*33cb4e01SAndreas Gohr return \dokuwiki\Ip::isSsl(); 529f5c6743cSAndreas Gohr} 530f5c6743cSAndreas Gohr 531f5c6743cSAndreas Gohr/** 53226714386SAndreas Gohr * checks it is windows OS 53326714386SAndreas Gohr * @return bool 53426714386SAndreas Gohr */ 535d868eb89SAndreas Gohrfunction isWindows() 536d868eb89SAndreas Gohr{ 53794c7e51fSfiwswe return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'; 53826714386SAndreas Gohr} 53926714386SAndreas Gohr 54026714386SAndreas Gohr/** 5413816dcbcSAndreas Gohr * print a nice message even if no styles are loaded yet. 542f50a239bSTakamura * 543f50a239bSTakamura * @param integer|string $msg 5443816dcbcSAndreas Gohr */ 545d868eb89SAndreas Gohrfunction nice_die($msg) 546d868eb89SAndreas Gohr{ 5473816dcbcSAndreas Gohr echo<<<EOT 548c8839c22SAnika Henke<!DOCTYPE html> 5493816dcbcSAndreas Gohr<html> 5503816dcbcSAndreas Gohr<head><title>DokuWiki Setup Error</title></head> 5513816dcbcSAndreas Gohr<body style="font-family: Arial, sans-serif"> 5523816dcbcSAndreas Gohr <div style="width:60%; margin: auto; background-color: #fcc; 5533816dcbcSAndreas Gohr border: 1px solid #faa; padding: 0.5em 1em;"> 5543816dcbcSAndreas Gohr <h1 style="font-size: 120%">DokuWiki Setup Error</h1> 5553816dcbcSAndreas Gohr <p>$msg</p> 5563816dcbcSAndreas Gohr </div> 5573816dcbcSAndreas Gohr</body> 5583816dcbcSAndreas Gohr</html> 5593816dcbcSAndreas GohrEOT; 5603862da0eSAndreas Gohr if (defined('DOKU_UNITTEST')) { 5613862da0eSAndreas Gohr throw new RuntimeException('nice_die: ' . $msg); 5623862da0eSAndreas Gohr } 5630a4266d4SElan Ruusamäe exit(1); 5643816dcbcSAndreas Gohr} 5653816dcbcSAndreas Gohr 56600976812SAndreas Gohr/** 56700976812SAndreas Gohr * A realpath() replacement 56800976812SAndreas Gohr * 56900976812SAndreas Gohr * This function behaves similar to PHP's realpath() but does not resolve 57000976812SAndreas Gohr * symlinks or accesses upper directories 57100976812SAndreas Gohr * 5724761d30cSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 57300976812SAndreas Gohr * @author <richpageau at yahoo dot co dot uk> 57459752844SAnders Sandblad * @link http://php.net/manual/en/function.realpath.php#75992 575f50a239bSTakamura * 576f50a239bSTakamura * @param string $path 577f50a239bSTakamura * @param bool $exists 578f50a239bSTakamura * 579f50a239bSTakamura * @return bool|string 58000976812SAndreas Gohr */ 581d868eb89SAndreas Gohrfunction fullpath($path, $exists = false) 582d868eb89SAndreas Gohr{ 5834761d30cSAndreas Gohr static $run = 0; 5844761d30cSAndreas Gohr $root = ''; 5856c16a3a9Sfiwswe $iswin = (isWindows() || !empty($GLOBALS['DOKU_UNITTEST_ASSUME_WINDOWS'])); 58600976812SAndreas Gohr 5874761d30cSAndreas Gohr // find the (indestructable) root of the path - keeps windows stuff intact 5882401f18dSSyntaxseed if ($path[0] == '/') { 5894761d30cSAndreas Gohr $root = '/'; 5904761d30cSAndreas Gohr } elseif ($iswin) { 5914761d30cSAndreas Gohr // match drive letter and UNC paths 5924761d30cSAndreas Gohr if (preg_match('!^([a-zA-z]:)(.*)!', $path, $match)) { 593b9c4302bSAndreas Gohr $root = $match[1] . '/'; 5944761d30cSAndreas Gohr $path = $match[2]; 5954761d30cSAndreas Gohr } elseif (preg_match('!^(\\\\\\\\[^\\\\/]+\\\\[^\\\\/]+[\\\\/])(.*)!', $path, $match)) { 5964761d30cSAndreas Gohr $root = $match[1]; 5974761d30cSAndreas Gohr $path = $match[2]; 59800976812SAndreas Gohr } 5994761d30cSAndreas Gohr } 6004761d30cSAndreas Gohr $path = str_replace('\\', '/', $path); 6014761d30cSAndreas Gohr 6024761d30cSAndreas Gohr // if the given path wasn't absolute already, prepend the script path and retry 6034761d30cSAndreas Gohr if (!$root) { 6044761d30cSAndreas Gohr $base = dirname($_SERVER['SCRIPT_FILENAME']); 6054761d30cSAndreas Gohr $path = $base . '/' . $path; 6064761d30cSAndreas Gohr if ($run == 0) { // avoid endless recursion when base isn't absolute for some reason 6074761d30cSAndreas Gohr $run++; 608b328697dSAndreas Gohr return fullpath($path, $exists); 6094761d30cSAndreas Gohr } 6104761d30cSAndreas Gohr } 6114761d30cSAndreas Gohr $run = 0; 61200976812SAndreas Gohr 61300976812SAndreas Gohr // canonicalize 61400976812SAndreas Gohr $path = explode('/', $path); 61524870174SAndreas Gohr $newpath = []; 616ef38bfe8SAndreas Gohr foreach ($path as $p) { 617ef38bfe8SAndreas Gohr if ($p === '' || $p === '.') continue; 618ef38bfe8SAndreas Gohr if ($p === '..') { 61900976812SAndreas Gohr array_pop($newpath); 62000976812SAndreas Gohr continue; 62100976812SAndreas Gohr } 62224870174SAndreas Gohr $newpath[] = $p; 62300976812SAndreas Gohr } 6244761d30cSAndreas Gohr $finalpath = $root . implode('/', $newpath); 62500976812SAndreas Gohr 6266b9c156cSAnika Henke // check for existence when needed (except when unit testing) 62779e79377SAndreas Gohr if ($exists && !defined('DOKU_UNITTEST') && !file_exists($finalpath)) { 6284761d30cSAndreas Gohr return false; 62900976812SAndreas Gohr } 6304761d30cSAndreas Gohr return $finalpath; 63100976812SAndreas Gohr} 632