1ed7b5f09Sandi<?php 2d4f83172SAndreas Gohr 3ed7b5f09Sandi/** 4ed7b5f09Sandi * Initialize some defaults needed for DokuWiki 5ed7b5f09Sandi */ 6d4f83172SAndreas Gohr 7*9399c87eSsplitbrainuse dokuwiki\Ip; 824870174SAndreas Gohruse dokuwiki\Extension\PluginController; 924870174SAndreas Gohruse dokuwiki\ErrorHandler; 1024870174SAndreas Gohruse dokuwiki\Input\Input; 11cbb44eabSAndreas Gohruse dokuwiki\Extension\Event; 12e1d9dcc8SAndreas Gohruse dokuwiki\Extension\EventHandler; 13e1d9dcc8SAndreas Gohr 143272d797SAndreas Gohr/** 153272d797SAndreas Gohr * timing Dokuwiki execution 16f50a239bSTakamura * 17f50a239bSTakamura * @param integer $start 18f50a239bSTakamura * 19f50a239bSTakamura * @return mixed 203272d797SAndreas Gohr */ 21d868eb89SAndreas Gohrfunction delta_time($start = 0) 22d868eb89SAndreas Gohr{ 23ac4be4d7SPiyush Mishra return microtime(true) - ((float)$start); 24a609a9ccSBen Coburn} 25a609a9ccSBen Coburndefine('DOKU_START_TIME', delta_time()); 26a609a9ccSBen Coburn 27ccaeaa85SAndreas Gohrglobal $config_cascade; 2824870174SAndreas Gohr$config_cascade = []; 29ccaeaa85SAndreas Gohr 3048beefecSAndreas Gohr// if available load a preload config file 3124870174SAndreas Gohr$preload = fullpath(__DIR__) . '/preload.php'; 3279e79377SAndreas Gohrif (file_exists($preload)) include($preload); 3348beefecSAndreas Gohr 34ed7b5f09Sandi// define the include path 3524870174SAndreas Gohrif (!defined('DOKU_INC')) define('DOKU_INC', fullpath(__DIR__ . '/../') . '/'); 36ad15db82Sandi 37c2a6d816SAndreas Gohr// define Plugin dir 38c2a6d816SAndreas Gohrif (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/'); 39c2a6d816SAndreas Gohr 40e7cb32dcSAndreas Gohr// define config path (packagers may want to change this to /etc/dokuwiki/) 41b7551a6dSEsther Brunnerif (!defined('DOKU_CONF')) define('DOKU_CONF', DOKU_INC . 'conf/'); 42e7cb32dcSAndreas Gohr 43bad905f1SBen Coburn// check for error reporting override or set error reporting to sane values 4479e79377SAndreas Gohrif (!defined('DOKU_E_LEVEL') && file_exists(DOKU_CONF . 'report_e_all')) { 45bad905f1SBen Coburn define('DOKU_E_LEVEL', E_ALL); 46bad905f1SBen Coburn} 47fc80ed59SAndreas Gohrif (!defined('DOKU_E_LEVEL')) { 48e086ef6cSlvl1ch43l error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED); 49fc80ed59SAndreas Gohr} else { 50fc80ed59SAndreas Gohr error_reporting(DOKU_E_LEVEL); 51fc80ed59SAndreas Gohr} 52c53ea5f2Sandi 536a9b3303SAndreas Gohr// autoloader 546a9b3303SAndreas Gohrrequire_once(DOKU_INC . 'inc/load.php'); 556a9b3303SAndreas Gohr 56a69722b3SAndreas Gohr// avoid caching issues #1594 57a69722b3SAndreas Gohrheader('Vary: Cookie'); 58a69722b3SAndreas Gohr 5950602150SBen Coburn// init memory caches 60db959ae3SAndreas Gohrglobal $cache_revinfo; 6124870174SAndreas Gohr $cache_revinfo = []; 62db959ae3SAndreas Gohrglobal $cache_wikifn; 6324870174SAndreas Gohr $cache_wikifn = []; 64db959ae3SAndreas Gohrglobal $cache_cleanid; 6524870174SAndreas Gohr $cache_cleanid = []; 66db959ae3SAndreas Gohrglobal $cache_authname; 6724870174SAndreas Gohr $cache_authname = []; 68db959ae3SAndreas Gohrglobal $cache_metadata; 6924870174SAndreas Gohr $cache_metadata = []; 7050602150SBen Coburn 71cca94fbcSRoland Hager// always include 'inc/config_cascade.php' 72cca94fbcSRoland Hager// previously in preload.php set fields of $config_cascade will be merged with the defaults 73e6a6dbfeSAndreas Gohrinclude(DOKU_INC . 'inc/config_cascade.php'); 74cb043f52SChris Smith 754724a577Sandi//prepare config array() 76ee20e7d1Sandiglobal $conf; 7724870174SAndreas Gohr$conf = []; 784724a577Sandi 79cb043f52SChris Smith// load the global config file(s) 8024870174SAndreas Gohrforeach (['default', 'local', 'protected'] as $config_group) { 81f8121585SChris Smith if (empty($config_cascade['main'][$config_group])) continue; 82b303b92cSChris Smith foreach ($config_cascade['main'][$config_group] as $config_file) { 8379e79377SAndreas Gohr if (file_exists($config_file)) { 84f8121585SChris Smith include($config_file); 85f8121585SChris Smith } 86cb043f52SChris Smith } 870a6ead41SAndreas Gohr} 88ad15db82Sandi 896a9b3303SAndreas Gohr// precalculate file creation modes 906a9b3303SAndreas Gohrinit_creationmodes(); 916a9b3303SAndreas Gohr 926a9b3303SAndreas Gohr// make real paths and check them 936a9b3303SAndreas Gohrinit_paths(); 946a9b3303SAndreas Gohrinit_files(); 956a9b3303SAndreas Gohr 96066fee30SAndreas Gohr//prepare license array() 97066fee30SAndreas Gohrglobal $license; 9824870174SAndreas Gohr$license = []; 99066fee30SAndreas Gohr 100066fee30SAndreas Gohr// load the license file(s) 10124870174SAndreas Gohrforeach (['default', 'local'] as $config_group) { 102f8121585SChris Smith if (empty($config_cascade['license'][$config_group])) continue; 103f8121585SChris Smith foreach ($config_cascade['license'][$config_group] as $config_file) { 10479e79377SAndreas Gohr if (file_exists($config_file)) { 105f8121585SChris Smith include($config_file); 106f8121585SChris Smith } 107f8121585SChris Smith } 108066fee30SAndreas Gohr} 109066fee30SAndreas Gohr 110*9399c87eSsplitbrainif ($conf === []) { 111e0c5fc94Sfjf2002 nice_die("No configuration found in " . DOKU_CONF . "."); 112e0c5fc94Sfjf2002} 113e0c5fc94Sfjf2002 1141f8eb24fSAndreas Gohr// set timezone (as in pre 5.3.0 days) 1151f8eb24fSAndreas Gohrdate_default_timezone_set(@date_default_timezone_get()); 1161f8eb24fSAndreas Gohr 1176a9b3303SAndreas Gohr 1186a9b3303SAndreas Gohr// don't let cookies ever interfere with request vars 1196a9b3303SAndreas Gohr$_REQUEST = array_merge($_GET, $_POST); 1206a9b3303SAndreas Gohr// input handle class 1216a9b3303SAndreas Gohrglobal $INPUT; 1226a9b3303SAndreas Gohr$INPUT = new Input(); 1236a9b3303SAndreas Gohr 1246a9b3303SAndreas Gohr 125ed7b5f09Sandi// define baseURL 1264b1a4e04SAndreas Gohrif (!defined('DOKU_REL')) define('DOKU_REL', getBaseURL(false)); 127ed7b5f09Sandiif (!defined('DOKU_URL')) define('DOKU_URL', getBaseURL(true)); 1284b1a4e04SAndreas Gohrif (!defined('DOKU_BASE')) { 1294b1a4e04SAndreas Gohr if ($conf['canonical']) { 1304b1a4e04SAndreas Gohr define('DOKU_BASE', DOKU_URL); 1314b1a4e04SAndreas Gohr } else { 1324b1a4e04SAndreas Gohr define('DOKU_BASE', DOKU_REL); 1334b1a4e04SAndreas Gohr } 1344b1a4e04SAndreas Gohr} 1354b1a4e04SAndreas Gohr 136b8595a66SAndreas Gohr// define whitespace 137b4f2363aSAndreas Gohrif (!defined('NL')) define('NL', "\n"); 138b8595a66SAndreas Gohrif (!defined('DOKU_LF')) define('DOKU_LF', "\n"); 139b8595a66SAndreas Gohrif (!defined('DOKU_TAB')) define('DOKU_TAB', "\t"); 140ed7b5f09Sandi 141656c8fb3SAndreas Gohr// define cookie and session id, append server port when securecookie is configured FS#1664 142fb97a12aSMichael Großeif (!defined('DOKU_COOKIE')) { 14324870174SAndreas Gohr $serverPort = $_SERVER['SERVER_PORT'] ?? ''; 144fb97a12aSMichael Große define('DOKU_COOKIE', 'DW' . md5(DOKU_REL . (($conf['securecookie']) ? $serverPort : ''))); 145abc9c0d2SAndreas Gohr unset($serverPort); 146fb97a12aSMichael Große} 147ee20e7d1Sandi 148ed7b5f09Sandi// define main script 149ed7b5f09Sandiif (!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT', 'doku.php'); 150ed7b5f09Sandi 151c163dbefSMichael Großeif (!defined('DOKU_TPL')) { 152c163dbefSMichael Große /** 153c163dbefSMichael Große * @deprecated 2012-10-13 replaced by more dynamic method 154c163dbefSMichael Große * @see tpl_basedir() 155c163dbefSMichael Große */ 156c163dbefSMichael Große define('DOKU_TPL', DOKU_BASE . 'lib/tpl/' . $conf['template'] . '/'); 157c163dbefSMichael Große} 1586b13307fSandi 159c163dbefSMichael Großeif (!defined('DOKU_TPLINC')) { 160c163dbefSMichael Große /** 161c163dbefSMichael Große * @deprecated 2012-10-13 replaced by more dynamic method 162c163dbefSMichael Große * @see tpl_incdir() 163c163dbefSMichael Große */ 164c163dbefSMichael Große define('DOKU_TPLINC', DOKU_INC . 'lib/tpl/' . $conf['template'] . '/'); 165c163dbefSMichael Große} 16678a6aeb1SAndreas Gohr 167ed7b5f09Sandi// make session rewrites XHTML compliant 1683fc74836Sandi@ini_set('arg_separator.output', '&'); 169ed7b5f09Sandi 170d7e6bba9SAndreas Gohr// make sure global zlib does not interfere FS#1132 171d7e6bba9SAndreas Gohr@ini_set('zlib.output_compression', 'off'); 172d7e6bba9SAndreas Gohr 1736deb5405SAndreas Gohr// increase PCRE backtrack limit 1746deb5405SAndreas Gohr@ini_set('pcre.backtrack_limit', '20971520'); 1756deb5405SAndreas Gohr 17698bda4fdSAndreas Gohr// enable gzip compression if supported 17724870174SAndreas Gohr$httpAcceptEncoding = $_SERVER['HTTP_ACCEPT_ENCODING'] ?? ''; 178fb97a12aSMichael Große$conf['gzip_output'] &= (strpos($httpAcceptEncoding, 'gzip') !== false); 17965f6e7d6SMichael Hamannglobal $ACT; 1807d34963bSAndreas Gohrif ( 1817d34963bSAndreas Gohr $conf['gzip_output'] && 1823138b5c7SAndreas Gohr !defined('DOKU_DISABLE_GZIP_OUTPUT') && 18365f6e7d6SMichael Hamann function_exists('ob_gzhandler') && 18499e10b7fSMichael Hamann // Disable compression when a (compressed) sitemap might be delivered 18565f6e7d6SMichael Hamann // See https://bugs.dokuwiki.org/index.php?do=details&task_id=2576 1867d34963bSAndreas Gohr $ACT != 'sitemap' 1877d34963bSAndreas Gohr) { 1883138b5c7SAndreas Gohr ob_start('ob_gzhandler'); 1893138b5c7SAndreas Gohr} 1903138b5c7SAndreas Gohr 191ed7b5f09Sandi// init session 1926534245aSAndreas Gohrif (!headers_sent() && !defined('NOSESSION')) { 193c09f0eb1SGerrit Uitslag if (!defined('DOKU_SESSION_NAME')) define('DOKU_SESSION_NAME', "DokuWiki"); 194c09f0eb1SGerrit Uitslag if (!defined('DOKU_SESSION_LIFETIME')) define('DOKU_SESSION_LIFETIME', 0); 19555a71a16SGerrit Uitslag if (!defined('DOKU_SESSION_PATH')) { 19673ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 19755a71a16SGerrit Uitslag define('DOKU_SESSION_PATH', $cookieDir); 198f5c6743cSAndreas Gohr } 199c09f0eb1SGerrit Uitslag if (!defined('DOKU_SESSION_DOMAIN')) define('DOKU_SESSION_DOMAIN', ''); 200c09f0eb1SGerrit Uitslag 2016eb3cdf6SAndreas Gohr // start the session 2026eb3cdf6SAndreas Gohr init_session(); 20314a122deSAndreas Gohr 20414a122deSAndreas Gohr // load left over messages 20514a122deSAndreas Gohr if (isset($_SESSION[DOKU_COOKIE]['msg'])) { 20614a122deSAndreas Gohr $MSG = $_SESSION[DOKU_COOKIE]['msg']; 20714a122deSAndreas Gohr unset($_SESSION[DOKU_COOKIE]['msg']); 20814a122deSAndreas Gohr } 209bad31ae9SAndreas Gohr} 210ed7b5f09Sandi 211a1637ffdSAndreas Gohr 2123dea4ebcSAndreas Gohr// we don't want a purge URL to be digged 2130e80bb5eSChristopher Smithif (isset($_REQUEST['purge']) && !empty($_SERVER['HTTP_REFERER'])) unset($_REQUEST['purge']); 2143dea4ebcSAndreas Gohr 215ed7b5f09Sandi 216f1986589SMichael Klier// setup plugin controller class (can be overwritten in preload.php) 217f1986589SMichael Klierglobal $plugin_controller_class, $plugin_controller; 21824870174SAndreas Gohrif (empty($plugin_controller_class)) $plugin_controller_class = PluginController::class; 219f1986589SMichael Klier 220642e976cSAndreas Gohr// from now on everything is an exception 22124870174SAndreas GohrErrorHandler::register(); 222642e976cSAndreas Gohr 2230f8f7aaaSDanny Lin// disable gzip if not available 22413c37900SAndreas Gohrdefine('DOKU_HAS_BZIP', function_exists('bzopen')); 22513c37900SAndreas Gohrdefine('DOKU_HAS_GZIP', function_exists('gzopen')); 22613c37900SAndreas Gohrif ($conf['compression'] == 'bz2' && !DOKU_HAS_BZIP) { 2270f8f7aaaSDanny Lin $conf['compression'] = 'gz'; 2280f8f7aaaSDanny Lin} 22913c37900SAndreas Gohrif ($conf['compression'] == 'gz' && !DOKU_HAS_GZIP) { 2300f8f7aaaSDanny Lin $conf['compression'] = 0; 2310f8f7aaaSDanny Lin} 2320f8f7aaaSDanny Lin 233f1986589SMichael Klier// initialize plugin controller 234f1986589SMichael Klier$plugin_controller = new $plugin_controller_class(); 235f1986589SMichael Klier 236f1986589SMichael Klier// initialize the event handler 237f1986589SMichael Klierglobal $EVENT_HANDLER; 238e1d9dcc8SAndreas Gohr$EVENT_HANDLER = new EventHandler(); 239f1986589SMichael Klier 2406d06b26aSDominik Eckelmann$local = $conf['lang']; 241cbb44eabSAndreas GohrEvent::createAndTrigger('INIT_LANG_LOAD', $local, 'init_lang', true); 2426d06b26aSDominik Eckelmann 2436d06b26aSDominik Eckelmann 24416905344SAndreas Gohr// setup authentication system 245c7cb395cSAdrian Langif (!defined('NOSESSION')) { 24616905344SAndreas Gohr auth_setup(); 247c7cb395cSAdrian Lang} 248f62ea8a1Sandi 2495ec3fefcSAndreas Gohr// setup mail system 2505ec3fefcSAndreas Gohrmail_setup(); 2515ec3fefcSAndreas Gohr 252042b9fecSAndreas Gohr$nil = null; 253042b9fecSAndreas GohrEvent::createAndTrigger('DOKUWIKI_INIT_DONE', $nil, null, false); 254042b9fecSAndreas Gohr 255f62ea8a1Sandi/** 2566eb3cdf6SAndreas Gohr * Initializes the session 2576eb3cdf6SAndreas Gohr * 2586eb3cdf6SAndreas Gohr * Makes sure the passed session cookie is valid, invalid ones are ignored an a new session ID is issued 2596eb3cdf6SAndreas Gohr * 2606eb3cdf6SAndreas Gohr * @link http://stackoverflow.com/a/33024310/172068 261924e477eSAndreas Gohr * @link http://php.net/manual/en/session.configuration.php#ini.session.sid-length 2626eb3cdf6SAndreas Gohr */ 263d868eb89SAndreas Gohrfunction init_session() 264d868eb89SAndreas Gohr{ 2656eb3cdf6SAndreas Gohr global $conf; 2666eb3cdf6SAndreas Gohr session_name(DOKU_SESSION_NAME); 267bf8392ebSAndreas Gohr session_set_cookie_params([ 268bf8392ebSAndreas Gohr 'lifetime' => DOKU_SESSION_LIFETIME, 269bf8392ebSAndreas Gohr 'path' => DOKU_SESSION_PATH, 270bf8392ebSAndreas Gohr 'domain' => DOKU_SESSION_DOMAIN, 271*9399c87eSsplitbrain 'secure' => ($conf['securecookie'] && Ip::isSsl()), 272bf8392ebSAndreas Gohr 'httponly' => true, 273bf8392ebSAndreas Gohr 'samesite' => 'Lax', 274bf8392ebSAndreas Gohr ]); 2756eb3cdf6SAndreas Gohr 2766eb3cdf6SAndreas Gohr // make sure the session cookie contains a valid session ID 277924e477eSAndreas Gohr if (isset($_COOKIE[DOKU_SESSION_NAME]) && !preg_match('/^[-,a-zA-Z0-9]{22,256}$/', $_COOKIE[DOKU_SESSION_NAME])) { 2786eb3cdf6SAndreas Gohr unset($_COOKIE[DOKU_SESSION_NAME]); 2796eb3cdf6SAndreas Gohr } 2806eb3cdf6SAndreas Gohr 2816eb3cdf6SAndreas Gohr session_start(); 2826eb3cdf6SAndreas Gohr} 2836eb3cdf6SAndreas Gohr 2846eb3cdf6SAndreas Gohr 2856eb3cdf6SAndreas Gohr/** 28698407a7aSandi * Checks paths from config file 28798407a7aSandi */ 288d868eb89SAndreas Gohrfunction init_paths() 289d868eb89SAndreas Gohr{ 29098407a7aSandi global $conf; 29198407a7aSandi 2920ecde6ceSAndreas Gohr $paths = [ 2930ecde6ceSAndreas Gohr 'datadir' => 'pages', 29498407a7aSandi 'olddir' => 'attic', 29598407a7aSandi 'mediadir' => 'media', 296e4f389efSKate Arzamastseva 'mediaolddir' => 'media_attic', 29798407a7aSandi 'metadir' => 'meta', 298e4f389efSKate Arzamastseva 'mediametadir' => 'media_meta', 29998407a7aSandi 'cachedir' => 'cache', 300579b0f7eSTNHarris 'indexdir' => 'index', 301de33a58fSMichael Klier 'lockdir' => 'locks', 3020ecde6ceSAndreas Gohr 'tmpdir' => 'tmp', 3030ecde6ceSAndreas Gohr 'logdir' => 'log', 3040ecde6ceSAndreas Gohr ]; 30598407a7aSandi 30698407a7aSandi foreach ($paths as $c => $p) { 3077f086b67SAnika Henke $path = empty($conf[$c]) ? $conf['savedir'] . '/' . $p : $conf[$c]; 3086b9c156cSAnika Henke $conf[$c] = init_path($path); 309697a39aeSAndreas Gohr if (empty($conf[$c])) { 310697a39aeSAndreas Gohr $path = fullpath($path); 3116b9c156cSAnika Henke nice_die("The $c ('$p') at $path is not found, isn't accessible or writable. 31269dc3177SAndreas Gohr You should check your config and permission settings. 31369dc3177SAndreas Gohr Or maybe you want to <a href=\"install.php\">run the 31469dc3177SAndreas Gohr installer</a>?"); 31598407a7aSandi } 316697a39aeSAndreas Gohr } 31771726d78SBen Coburn 31871726d78SBen Coburn // path to old changelog only needed for upgrading 31964159a61SAndreas Gohr $conf['changelog_old'] = init_path( 32024870174SAndreas Gohr $conf['changelog'] ?? $conf['savedir'] . '/changes.log' 32164159a61SAndreas Gohr ); 322177d6836SAndreas Gohr if ($conf['changelog_old'] == '') { 323d4f83172SAndreas Gohr unset($conf['changelog_old']); 324d4f83172SAndreas Gohr } 32571726d78SBen Coburn // hardcoded changelog because it is now a cache that lives in meta 32671726d78SBen Coburn $conf['changelog'] = $conf['metadir'] . '/_dokuwiki.changes'; 32799c8d7f2Smichael $conf['media_changelog'] = $conf['metadir'] . '/_media.changes'; 32898407a7aSandi} 32998407a7aSandi 33038fb1fc7SGerrit Uitslag/** 33138fb1fc7SGerrit Uitslag * Load the language strings 33238fb1fc7SGerrit Uitslag * 33338fb1fc7SGerrit Uitslag * @param string $langCode language code, as passed by event handler 33438fb1fc7SGerrit Uitslag */ 335d868eb89SAndreas Gohrfunction init_lang($langCode) 336d868eb89SAndreas Gohr{ 3376d06b26aSDominik Eckelmann //prepare language array 338dd7a6159SGerrit Uitslag global $lang, $config_cascade; 33924870174SAndreas Gohr $lang = []; 3406d06b26aSDominik Eckelmann 3416d06b26aSDominik Eckelmann //load the language files 3421d82c8d3SChristopher Smith require(DOKU_INC . 'inc/lang/en/lang.php'); 343dd7a6159SGerrit Uitslag foreach ($config_cascade['lang']['core'] as $config_file) { 34479e79377SAndreas Gohr if (file_exists($config_file . 'en/lang.php')) { 345dd7a6159SGerrit Uitslag include($config_file . 'en/lang.php'); 346dd7a6159SGerrit Uitslag } 347dd7a6159SGerrit Uitslag } 348dd7a6159SGerrit Uitslag 3496d06b26aSDominik Eckelmann if ($langCode && $langCode != 'en') { 3506d06b26aSDominik Eckelmann if (file_exists(DOKU_INC . "inc/lang/$langCode/lang.php")) { 3511d82c8d3SChristopher Smith require(DOKU_INC . "inc/lang/$langCode/lang.php"); 3526d06b26aSDominik Eckelmann } 353dd7a6159SGerrit Uitslag foreach ($config_cascade['lang']['core'] as $config_file) { 35479e79377SAndreas Gohr if (file_exists($config_file . "$langCode/lang.php")) { 355dd7a6159SGerrit Uitslag include($config_file . "$langCode/lang.php"); 3566d06b26aSDominik Eckelmann } 357dd7a6159SGerrit Uitslag } 3586d06b26aSDominik Eckelmann } 3596d06b26aSDominik Eckelmann} 3606d06b26aSDominik Eckelmann 36198407a7aSandi/** 3626b9c156cSAnika Henke * Checks the existence of certain files and creates them if missing. 3637367b368SAndreas Gohr */ 364d868eb89SAndreas Gohrfunction init_files() 365d868eb89SAndreas Gohr{ 3667367b368SAndreas Gohr global $conf; 3670d8850c4SAndreas Gohr 36824870174SAndreas Gohr $files = [$conf['indexdir'] . '/page.idx']; 3697367b368SAndreas Gohr 3707367b368SAndreas Gohr foreach ($files as $file) { 37179e79377SAndreas Gohr if (!file_exists($file)) { 3720d8850c4SAndreas Gohr $fh = @fopen($file, 'a'); 3730d8850c4SAndreas Gohr if ($fh) { 3747367b368SAndreas Gohr fclose($fh); 3753aa75874Smovatica if ($conf['fperm']) chmod($file, $conf['fperm']); 3760d8850c4SAndreas Gohr } else { 3773816dcbcSAndreas Gohr nice_die("$file is not writable. Check your permissions settings!"); 3780d8850c4SAndreas Gohr } 3797367b368SAndreas Gohr } 3807367b368SAndreas Gohr } 3817367b368SAndreas Gohr} 3827367b368SAndreas Gohr 3837367b368SAndreas Gohr/** 3840d8850c4SAndreas Gohr * Returns absolute path 385f62ea8a1Sandi * 3860d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC. 3877f086b67SAnika Henke * Check for accessibility on directories as well. 3880d8850c4SAndreas Gohr * 3890d8850c4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 390f50a239bSTakamura * 391f50a239bSTakamura * @param string $path 392f50a239bSTakamura * 393f50a239bSTakamura * @return bool|string 394f62ea8a1Sandi */ 395d868eb89SAndreas Gohrfunction init_path($path) 396d868eb89SAndreas Gohr{ 3976b9c156cSAnika Henke // check existence 39800976812SAndreas Gohr $p = fullpath($path); 39979e79377SAndreas Gohr if (!file_exists($p)) { 40000976812SAndreas Gohr $p = fullpath(DOKU_INC . $path); 40179e79377SAndreas Gohr if (!file_exists($p)) { 4028fc4e739Sandi return ''; 403f62ea8a1Sandi } 4040d8850c4SAndreas Gohr } 4050d8850c4SAndreas Gohr 4060d8850c4SAndreas Gohr // check writability 4070d8850c4SAndreas Gohr if (!@is_writable($p)) { 4080d8850c4SAndreas Gohr return ''; 4090d8850c4SAndreas Gohr } 4100d8850c4SAndreas Gohr 4110d8850c4SAndreas Gohr // check accessability (execute bit) for directories 41279e79377SAndreas Gohr if (@is_dir($p) && !file_exists("$p/.")) { 4130d8850c4SAndreas Gohr return ''; 4140d8850c4SAndreas Gohr } 4150d8850c4SAndreas Gohr 4160d8850c4SAndreas Gohr return $p; 4170d8850c4SAndreas Gohr} 4188c4f28e8Sjan 419ed7b5f09Sandi/** 4201ca31cfeSAndreas Gohr * Sets the internal config values fperm and dperm which, when set, 4211ca31cfeSAndreas Gohr * will be used to change the permission of a newly created dir or 4221ca31cfeSAndreas Gohr * file with chmod. Considers the influence of the system's umask 4231ca31cfeSAndreas Gohr * setting the values only if needed. 4241ca31cfeSAndreas Gohr */ 425d868eb89SAndreas Gohrfunction init_creationmodes() 426d868eb89SAndreas Gohr{ 4271ca31cfeSAndreas Gohr global $conf; 4281ca31cfeSAndreas Gohr 4291ca31cfeSAndreas Gohr // Legacy support for old umask/dmask scheme 4301ca31cfeSAndreas Gohr unset($conf['dmask']); 4311ca31cfeSAndreas Gohr unset($conf['fmask']); 4321ca31cfeSAndreas Gohr unset($conf['umask']); 43323420346SDamien Regad 43423420346SDamien Regad $conf['fperm'] = false; 43523420346SDamien Regad $conf['dperm'] = false; 4361ca31cfeSAndreas Gohr 4379f3cdec3SAndreas Gohr // get system umask, fallback to 0 if none available 4389f3cdec3SAndreas Gohr $umask = @umask(); 4399f3cdec3SAndreas Gohr if (!$umask) $umask = 0000; 4401ca31cfeSAndreas Gohr 4411ca31cfeSAndreas Gohr // check what is set automatically by the system on file creation 4421ca31cfeSAndreas Gohr // and set the fperm param if it's not what we want 443bd539124SAndreas Gohr $auto_fmode = 0666 & ~$umask; 4441ca31cfeSAndreas Gohr if ($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode']; 4451ca31cfeSAndreas Gohr 446bd539124SAndreas Gohr // check what is set automatically by the system on directory creation 447bd539124SAndreas Gohr // and set the dperm param if it's not what we want. 448bd539124SAndreas Gohr $auto_dmode = 0777 & ~$umask; 4491ca31cfeSAndreas Gohr if ($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode']; 4501ca31cfeSAndreas Gohr} 4511ca31cfeSAndreas Gohr 4521ca31cfeSAndreas Gohr/** 453ed7b5f09Sandi * Returns the full absolute URL to the directory where 454ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash) 455ed7b5f09Sandi * 456585bf44eSChristopher Smith * !! Can not access $_SERVER values through $INPUT 457585bf44eSChristopher Smith * !! here as this function is called before $INPUT is 458585bf44eSChristopher Smith * !! initialized. 459585bf44eSChristopher Smith * 460ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org> 461f50a239bSTakamura * 4623bfb10aeSAndreas Gohr * @param null|bool $abs Return an absolute URL? (null defaults to $conf['canonical']) 463f50a239bSTakamura * 464f50a239bSTakamura * @return string 465ed7b5f09Sandi */ 466d868eb89SAndreas Gohrfunction getBaseURL($abs = null) 467d868eb89SAndreas Gohr{ 468ed7b5f09Sandi global $conf; 4693bfb10aeSAndreas Gohr 4703bfb10aeSAndreas Gohr $abs ??= $conf['canonical']; 471ed7b5f09Sandi 4721858e4d7SGerry Weißbach if (!empty($conf['basedir'])) { 47346c73e01SChris Smith $dir = $conf['basedir']; 47489aa05dbSAndreas Gohr } elseif (substr($_SERVER['SCRIPT_NAME'], -4) == '.php') { 47546c73e01SChris Smith $dir = dirname($_SERVER['SCRIPT_NAME']); 47689aa05dbSAndreas Gohr } elseif (substr($_SERVER['PHP_SELF'], -4) == '.php') { 47746c73e01SChris Smith $dir = dirname($_SERVER['PHP_SELF']); 478093ec9e4Sandi } elseif ($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']) { 479dccd6b2bSAndreas Gohr $dir = preg_replace( 480dccd6b2bSAndreas Gohr '/^' . preg_quote($_SERVER['DOCUMENT_ROOT'], '/') . '/', 481dccd6b2bSAndreas Gohr '', 482dccd6b2bSAndreas Gohr $_SERVER['SCRIPT_FILENAME'] 483dccd6b2bSAndreas Gohr ); 48446c73e01SChris Smith $dir = dirname('/' . $dir); 48592b83b77Sandi } else { 486ac56bec8SAndreas Gohr $dir = ''; //probably wrong, but we assume it's in the root 48792b83b77Sandi } 488ed7b5f09Sandi 48946c73e01SChris Smith $dir = str_replace('\\', '/', $dir); // bugfix for weird WIN behaviour 49046c73e01SChris Smith $dir = preg_replace('#//+#', '/', "/$dir/"); // ensure leading and trailing slashes 491ed7b5f09Sandi 492f62ea8a1Sandi //handle script in lib/exe dir 493f62ea8a1Sandi $dir = preg_replace('!lib/exe/$!', '', $dir); 494f62ea8a1Sandi 495488d5fa0SMichael Klier chi@chimeric.de //handle script in lib/plugins dir 496488d5fa0SMichael Klier chi@chimeric.de $dir = preg_replace('!lib/plugins/.*$!', '', $dir); 497488d5fa0SMichael Klier chi@chimeric.de 498ed7b5f09Sandi //finish here for relative URLs 499ed7b5f09Sandi if (!$abs) return $dir; 500ed7b5f09Sandi 50164159a61SAndreas Gohr //use config if available, trim any slash from end of baseurl to avoid multiple consecutive slashes in the path 5021858e4d7SGerry Weißbach if (!empty($conf['baseurl'])) return rtrim($conf['baseurl'], '/') . $dir; 503ef7b3ecdSAndreas Gohr 504e82e3526SAndreas Gohr //split hostheader into host and port 505*9399c87eSsplitbrain $hostname = Ip::hostName(); 50633cb4e01SAndreas Gohr $parsed_host = parse_url('http://' . $hostname); 5073bfb10aeSAndreas Gohr $host = $parsed_host['host'] ?? ''; 5083bfb10aeSAndreas Gohr $port = $parsed_host['port'] ?? ''; 5095627186cSAndreas Gohr 510*9399c87eSsplitbrain if (!Ip::isSsl()) { 511ed7b5f09Sandi $proto = 'http://'; 512e82e3526SAndreas Gohr if ($port == '80') { 513ed7b5f09Sandi $port = ''; 514ed7b5f09Sandi } 515ed7b5f09Sandi } else { 516ed7b5f09Sandi $proto = 'https://'; 517e82e3526SAndreas Gohr if ($port == '443') { 518ed7b5f09Sandi $port = ''; 519ed7b5f09Sandi } 520ed7b5f09Sandi } 521ed7b5f09Sandi 522c66972f2SAdrian Lang if ($port !== '') $port = ':' . $port; 523e82e3526SAndreas Gohr 524ed7b5f09Sandi return $proto . $host . $port . $dir; 525ed7b5f09Sandi} 526ed7b5f09Sandi 527b000c6d4Sandi/** 52833cb4e01SAndreas Gohr * @deprecated 2025-06-03 529f5c6743cSAndreas Gohr */ 530d868eb89SAndreas Gohrfunction is_ssl() 531d868eb89SAndreas Gohr{ 53233cb4e01SAndreas Gohr dbg_deprecated('Ip::isSsl()'); 533*9399c87eSsplitbrain return Ip::isSsl(); 534f5c6743cSAndreas Gohr} 535f5c6743cSAndreas Gohr 536f5c6743cSAndreas Gohr/** 53726714386SAndreas Gohr * checks it is windows OS 53826714386SAndreas Gohr * @return bool 53926714386SAndreas Gohr */ 540d868eb89SAndreas Gohrfunction isWindows() 541d868eb89SAndreas Gohr{ 54294c7e51fSfiwswe return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'; 54326714386SAndreas Gohr} 54426714386SAndreas Gohr 54526714386SAndreas Gohr/** 5463816dcbcSAndreas Gohr * print a nice message even if no styles are loaded yet. 547f50a239bSTakamura * 548f50a239bSTakamura * @param integer|string $msg 5493816dcbcSAndreas Gohr */ 550d868eb89SAndreas Gohrfunction nice_die($msg) 551d868eb89SAndreas Gohr{ 5523816dcbcSAndreas Gohr echo<<<EOT 553c8839c22SAnika Henke<!DOCTYPE html> 5543816dcbcSAndreas Gohr<html> 5553816dcbcSAndreas Gohr<head><title>DokuWiki Setup Error</title></head> 5563816dcbcSAndreas Gohr<body style="font-family: Arial, sans-serif"> 5573816dcbcSAndreas Gohr <div style="width:60%; margin: auto; background-color: #fcc; 5583816dcbcSAndreas Gohr border: 1px solid #faa; padding: 0.5em 1em;"> 5593816dcbcSAndreas Gohr <h1 style="font-size: 120%">DokuWiki Setup Error</h1> 5603816dcbcSAndreas Gohr <p>$msg</p> 5613816dcbcSAndreas Gohr </div> 5623816dcbcSAndreas Gohr</body> 5633816dcbcSAndreas Gohr</html> 5643816dcbcSAndreas GohrEOT; 5653862da0eSAndreas Gohr if (defined('DOKU_UNITTEST')) { 5663862da0eSAndreas Gohr throw new RuntimeException('nice_die: ' . $msg); 5673862da0eSAndreas Gohr } 5680a4266d4SElan Ruusamäe exit(1); 5693816dcbcSAndreas Gohr} 5703816dcbcSAndreas Gohr 57100976812SAndreas Gohr/** 57200976812SAndreas Gohr * A realpath() replacement 57300976812SAndreas Gohr * 57400976812SAndreas Gohr * This function behaves similar to PHP's realpath() but does not resolve 57500976812SAndreas Gohr * symlinks or accesses upper directories 57600976812SAndreas Gohr * 5774761d30cSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 57800976812SAndreas Gohr * @author <richpageau at yahoo dot co dot uk> 57959752844SAnders Sandblad * @link http://php.net/manual/en/function.realpath.php#75992 580f50a239bSTakamura * 581f50a239bSTakamura * @param string $path 582f50a239bSTakamura * @param bool $exists 583f50a239bSTakamura * 584f50a239bSTakamura * @return bool|string 58500976812SAndreas Gohr */ 586d868eb89SAndreas Gohrfunction fullpath($path, $exists = false) 587d868eb89SAndreas Gohr{ 5884761d30cSAndreas Gohr static $run = 0; 5894761d30cSAndreas Gohr $root = ''; 5906c16a3a9Sfiwswe $iswin = (isWindows() || !empty($GLOBALS['DOKU_UNITTEST_ASSUME_WINDOWS'])); 59100976812SAndreas Gohr 5924761d30cSAndreas Gohr // find the (indestructable) root of the path - keeps windows stuff intact 5932401f18dSSyntaxseed if ($path[0] == '/') { 5944761d30cSAndreas Gohr $root = '/'; 5954761d30cSAndreas Gohr } elseif ($iswin) { 5964761d30cSAndreas Gohr // match drive letter and UNC paths 5974761d30cSAndreas Gohr if (preg_match('!^([a-zA-z]:)(.*)!', $path, $match)) { 598b9c4302bSAndreas Gohr $root = $match[1] . '/'; 5994761d30cSAndreas Gohr $path = $match[2]; 6004761d30cSAndreas Gohr } elseif (preg_match('!^(\\\\\\\\[^\\\\/]+\\\\[^\\\\/]+[\\\\/])(.*)!', $path, $match)) { 6014761d30cSAndreas Gohr $root = $match[1]; 6024761d30cSAndreas Gohr $path = $match[2]; 60300976812SAndreas Gohr } 6044761d30cSAndreas Gohr } 6054761d30cSAndreas Gohr $path = str_replace('\\', '/', $path); 6064761d30cSAndreas Gohr 6074761d30cSAndreas Gohr // if the given path wasn't absolute already, prepend the script path and retry 6084761d30cSAndreas Gohr if (!$root) { 6094761d30cSAndreas Gohr $base = dirname($_SERVER['SCRIPT_FILENAME']); 6104761d30cSAndreas Gohr $path = $base . '/' . $path; 6114761d30cSAndreas Gohr if ($run == 0) { // avoid endless recursion when base isn't absolute for some reason 6124761d30cSAndreas Gohr $run++; 613b328697dSAndreas Gohr return fullpath($path, $exists); 6144761d30cSAndreas Gohr } 6154761d30cSAndreas Gohr } 6164761d30cSAndreas Gohr $run = 0; 61700976812SAndreas Gohr 61800976812SAndreas Gohr // canonicalize 61900976812SAndreas Gohr $path = explode('/', $path); 62024870174SAndreas Gohr $newpath = []; 621ef38bfe8SAndreas Gohr foreach ($path as $p) { 622ef38bfe8SAndreas Gohr if ($p === '' || $p === '.') continue; 623ef38bfe8SAndreas Gohr if ($p === '..') { 62400976812SAndreas Gohr array_pop($newpath); 62500976812SAndreas Gohr continue; 62600976812SAndreas Gohr } 62724870174SAndreas Gohr $newpath[] = $p; 62800976812SAndreas Gohr } 6294761d30cSAndreas Gohr $finalpath = $root . implode('/', $newpath); 63000976812SAndreas Gohr 6316b9c156cSAnika Henke // check for existence when needed (except when unit testing) 63279e79377SAndreas Gohr if ($exists && !defined('DOKU_UNITTEST') && !file_exists($finalpath)) { 6334761d30cSAndreas Gohr return false; 63400976812SAndreas Gohr } 6354761d30cSAndreas Gohr return $finalpath; 63600976812SAndreas Gohr} 637