1ed7b5f09Sandi<?php 2ed7b5f09Sandi/** 3ed7b5f09Sandi * Initialize some defaults needed for DokuWiki 4ed7b5f09Sandi */ 524870174SAndreas Gohruse dokuwiki\Extension\PluginController; 624870174SAndreas Gohruse dokuwiki\ErrorHandler; 724870174SAndreas Gohruse dokuwiki\Input\Input; 8cbb44eabSAndreas Gohruse dokuwiki\Extension\Event; 9e1d9dcc8SAndreas Gohruse dokuwiki\Extension\EventHandler; 10e1d9dcc8SAndreas Gohr 113272d797SAndreas Gohr/** 123272d797SAndreas Gohr * timing Dokuwiki execution 13f50a239bSTakamura * 14f50a239bSTakamura * @param integer $start 15f50a239bSTakamura * 16f50a239bSTakamura * @return mixed 173272d797SAndreas Gohr */ 18d868eb89SAndreas Gohrfunction delta_time($start = 0) 19d868eb89SAndreas Gohr{ 20ac4be4d7SPiyush Mishra return microtime(true)-((float)$start); 21a609a9ccSBen Coburn} 22a609a9ccSBen Coburndefine('DOKU_START_TIME', delta_time()); 23a609a9ccSBen Coburn 24ccaeaa85SAndreas Gohrglobal $config_cascade; 2524870174SAndreas Gohr$config_cascade = []; 26ccaeaa85SAndreas Gohr 2748beefecSAndreas Gohr// if available load a preload config file 2824870174SAndreas Gohr$preload = fullpath(__DIR__).'/preload.php'; 2979e79377SAndreas Gohrif (file_exists($preload)) include($preload); 3048beefecSAndreas Gohr 31ed7b5f09Sandi// define the include path 3224870174SAndreas Gohrif (!defined('DOKU_INC')) define('DOKU_INC', fullpath(__DIR__.'/../').'/'); 33ad15db82Sandi 34c2a6d816SAndreas Gohr// define Plugin dir 35c2a6d816SAndreas Gohrif (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/'); 36c2a6d816SAndreas Gohr 37e7cb32dcSAndreas Gohr// define config path (packagers may want to change this to /etc/dokuwiki/) 38b7551a6dSEsther Brunnerif (!defined('DOKU_CONF')) define('DOKU_CONF', DOKU_INC.'conf/'); 39e7cb32dcSAndreas Gohr 40bad905f1SBen Coburn// check for error reporting override or set error reporting to sane values 4179e79377SAndreas Gohrif (!defined('DOKU_E_LEVEL') && file_exists(DOKU_CONF.'report_e_all')) { 42bad905f1SBen Coburn define('DOKU_E_LEVEL', E_ALL); 43bad905f1SBen Coburn} 44fc80ed59SAndreas Gohrif (!defined('DOKU_E_LEVEL')) { 454fcd684aSMichael Hamann error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT); 46fc80ed59SAndreas Gohr} else { 47fc80ed59SAndreas Gohr error_reporting(DOKU_E_LEVEL); 48fc80ed59SAndreas Gohr} 49c53ea5f2Sandi 50a69722b3SAndreas Gohr// avoid caching issues #1594 51a69722b3SAndreas Gohrheader('Vary: Cookie'); 52a69722b3SAndreas Gohr 5350602150SBen Coburn// init memory caches 54db959ae3SAndreas Gohrglobal $cache_revinfo; 5524870174SAndreas Gohr $cache_revinfo = []; 56db959ae3SAndreas Gohrglobal $cache_wikifn; 5724870174SAndreas Gohr $cache_wikifn = []; 58db959ae3SAndreas Gohrglobal $cache_cleanid; 5924870174SAndreas Gohr $cache_cleanid = []; 60db959ae3SAndreas Gohrglobal $cache_authname; 6124870174SAndreas Gohr $cache_authname = []; 62db959ae3SAndreas Gohrglobal $cache_metadata; 6324870174SAndreas Gohr $cache_metadata = []; 6450602150SBen Coburn 65cca94fbcSRoland Hager// always include 'inc/config_cascade.php' 66cca94fbcSRoland Hager// previously in preload.php set fields of $config_cascade will be merged with the defaults 67e6a6dbfeSAndreas Gohrinclude(DOKU_INC.'inc/config_cascade.php'); 68cb043f52SChris Smith 694724a577Sandi//prepare config array() 70ee20e7d1Sandiglobal $conf; 7124870174SAndreas Gohr$conf = []; 724724a577Sandi 73cb043f52SChris Smith// load the global config file(s) 7424870174SAndreas Gohrforeach (['default', 'local', 'protected'] as $config_group) { 75f8121585SChris Smith if (empty($config_cascade['main'][$config_group])) continue; 76b303b92cSChris Smith foreach ($config_cascade['main'][$config_group] as $config_file) { 7779e79377SAndreas Gohr if (file_exists($config_file)) { 78f8121585SChris Smith include($config_file); 79f8121585SChris Smith } 80cb043f52SChris Smith } 810a6ead41SAndreas Gohr} 82ad15db82Sandi 83066fee30SAndreas Gohr//prepare license array() 84066fee30SAndreas Gohrglobal $license; 8524870174SAndreas Gohr$license = []; 86066fee30SAndreas Gohr 87066fee30SAndreas Gohr// load the license file(s) 8824870174SAndreas Gohrforeach (['default', 'local'] as $config_group) { 89f8121585SChris Smith if (empty($config_cascade['license'][$config_group])) continue; 90f8121585SChris Smith foreach ($config_cascade['license'][$config_group] as $config_file) { 9179e79377SAndreas Gohr if (file_exists($config_file)) { 92f8121585SChris Smith include($config_file); 93f8121585SChris Smith } 94f8121585SChris Smith } 95066fee30SAndreas Gohr} 96066fee30SAndreas Gohr 971f8eb24fSAndreas Gohr// set timezone (as in pre 5.3.0 days) 981f8eb24fSAndreas Gohrdate_default_timezone_set(@date_default_timezone_get()); 991f8eb24fSAndreas Gohr 100ed7b5f09Sandi// define baseURL 1014b1a4e04SAndreas Gohrif (!defined('DOKU_REL')) define('DOKU_REL', getBaseURL(false)); 102ed7b5f09Sandiif (!defined('DOKU_URL')) define('DOKU_URL', getBaseURL(true)); 1034b1a4e04SAndreas Gohrif (!defined('DOKU_BASE')) { 1044b1a4e04SAndreas Gohr if ($conf['canonical']) { 1054b1a4e04SAndreas Gohr define('DOKU_BASE', DOKU_URL); 1064b1a4e04SAndreas Gohr } else { 1074b1a4e04SAndreas Gohr define('DOKU_BASE', DOKU_REL); 1084b1a4e04SAndreas Gohr } 1094b1a4e04SAndreas Gohr} 1104b1a4e04SAndreas Gohr 111b8595a66SAndreas Gohr// define whitespace 112b4f2363aSAndreas Gohrif (!defined('NL')) define('NL', "\n"); 113b8595a66SAndreas Gohrif (!defined('DOKU_LF')) define('DOKU_LF', "\n"); 114b8595a66SAndreas Gohrif (!defined('DOKU_TAB')) define('DOKU_TAB', "\t"); 115ed7b5f09Sandi 116656c8fb3SAndreas Gohr// define cookie and session id, append server port when securecookie is configured FS#1664 117fb97a12aSMichael Großeif (!defined('DOKU_COOKIE')) { 11824870174SAndreas Gohr $serverPort = $_SERVER['SERVER_PORT'] ?? ''; 119fb97a12aSMichael Große define('DOKU_COOKIE', 'DW' . md5(DOKU_REL . (($conf['securecookie']) ? $serverPort : ''))); 120abc9c0d2SAndreas Gohr unset($serverPort); 121fb97a12aSMichael Große} 122ee20e7d1Sandi 123ed7b5f09Sandi// define main script 124ed7b5f09Sandiif (!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT', 'doku.php'); 125ed7b5f09Sandi 126c163dbefSMichael Großeif (!defined('DOKU_TPL')) { 127c163dbefSMichael Große /** 128c163dbefSMichael Große * @deprecated 2012-10-13 replaced by more dynamic method 129c163dbefSMichael Große * @see tpl_basedir() 130c163dbefSMichael Große */ 131c163dbefSMichael Große define('DOKU_TPL', DOKU_BASE.'lib/tpl/'.$conf['template'].'/'); 132c163dbefSMichael Große} 1336b13307fSandi 134c163dbefSMichael Großeif (!defined('DOKU_TPLINC')) { 135c163dbefSMichael Große /** 136c163dbefSMichael Große * @deprecated 2012-10-13 replaced by more dynamic method 137c163dbefSMichael Große * @see tpl_incdir() 138c163dbefSMichael Große */ 139c163dbefSMichael Große define('DOKU_TPLINC', DOKU_INC.'lib/tpl/'.$conf['template'].'/'); 140c163dbefSMichael Große} 14178a6aeb1SAndreas Gohr 142ed7b5f09Sandi// make session rewrites XHTML compliant 1433fc74836Sandi@ini_set('arg_separator.output', '&'); 144ed7b5f09Sandi 145d7e6bba9SAndreas Gohr// make sure global zlib does not interfere FS#1132 146d7e6bba9SAndreas Gohr@ini_set('zlib.output_compression', 'off'); 147d7e6bba9SAndreas Gohr 1486deb5405SAndreas Gohr// increase PCRE backtrack limit 1496deb5405SAndreas Gohr@ini_set('pcre.backtrack_limit', '20971520'); 1506deb5405SAndreas Gohr 15198bda4fdSAndreas Gohr// enable gzip compression if supported 15224870174SAndreas Gohr$httpAcceptEncoding = $_SERVER['HTTP_ACCEPT_ENCODING'] ?? ''; 153fb97a12aSMichael Große$conf['gzip_output'] &= (strpos($httpAcceptEncoding, 'gzip') !== false); 15465f6e7d6SMichael Hamannglobal $ACT; 155*7d34963bSAndreas Gohrif ( 156*7d34963bSAndreas Gohr $conf['gzip_output'] && 1573138b5c7SAndreas Gohr !defined('DOKU_DISABLE_GZIP_OUTPUT') && 15865f6e7d6SMichael Hamann function_exists('ob_gzhandler') && 15999e10b7fSMichael Hamann // Disable compression when a (compressed) sitemap might be delivered 16065f6e7d6SMichael Hamann // See https://bugs.dokuwiki.org/index.php?do=details&task_id=2576 161*7d34963bSAndreas Gohr $ACT != 'sitemap' 162*7d34963bSAndreas Gohr) { 1633138b5c7SAndreas Gohr ob_start('ob_gzhandler'); 1643138b5c7SAndreas Gohr} 1653138b5c7SAndreas Gohr 166ed7b5f09Sandi// init session 1676534245aSAndreas Gohrif (!headers_sent() && !defined('NOSESSION')) { 168c09f0eb1SGerrit Uitslag if (!defined('DOKU_SESSION_NAME')) define('DOKU_SESSION_NAME', "DokuWiki"); 169c09f0eb1SGerrit Uitslag if (!defined('DOKU_SESSION_LIFETIME')) define('DOKU_SESSION_LIFETIME', 0); 17055a71a16SGerrit Uitslag if (!defined('DOKU_SESSION_PATH')) { 17173ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 17255a71a16SGerrit Uitslag define('DOKU_SESSION_PATH', $cookieDir); 173f5c6743cSAndreas Gohr } 174c09f0eb1SGerrit Uitslag if (!defined('DOKU_SESSION_DOMAIN')) define('DOKU_SESSION_DOMAIN', ''); 175c09f0eb1SGerrit Uitslag 1766eb3cdf6SAndreas Gohr // start the session 1776eb3cdf6SAndreas Gohr init_session(); 17814a122deSAndreas Gohr 17914a122deSAndreas Gohr // load left over messages 18014a122deSAndreas Gohr if (isset($_SESSION[DOKU_COOKIE]['msg'])) { 18114a122deSAndreas Gohr $MSG = $_SESSION[DOKU_COOKIE]['msg']; 18214a122deSAndreas Gohr unset($_SESSION[DOKU_COOKIE]['msg']); 18314a122deSAndreas Gohr } 184bad31ae9SAndreas Gohr} 185ed7b5f09Sandi 186a1637ffdSAndreas Gohr// don't let cookies ever interfere with request vars 187a1637ffdSAndreas Gohr$_REQUEST = array_merge($_GET, $_POST); 188a1637ffdSAndreas Gohr 1893dea4ebcSAndreas Gohr// we don't want a purge URL to be digged 1900e80bb5eSChristopher Smithif (isset($_REQUEST['purge']) && !empty($_SERVER['HTTP_REFERER'])) unset($_REQUEST['purge']); 1913dea4ebcSAndreas Gohr 1921ca31cfeSAndreas Gohr// precalculate file creation modes 1931ca31cfeSAndreas Gohrinit_creationmodes(); 194ed7b5f09Sandi 1953dc3a5f1Sandi// make real paths and check them 19698407a7aSandiinit_paths(); 1977367b368SAndreas Gohrinit_files(); 198ed7b5f09Sandi 199f1986589SMichael Klier// setup plugin controller class (can be overwritten in preload.php) 200f1986589SMichael Klierglobal $plugin_controller_class, $plugin_controller; 20124870174SAndreas Gohrif (empty($plugin_controller_class)) $plugin_controller_class = PluginController::class; 202f1986589SMichael Klier 203c7cb395cSAdrian Lang// load libraries 204605f8e8dSAndreas Gohrrequire_once(DOKU_INC.'vendor/autoload.php'); 205c7cb395cSAdrian Langrequire_once(DOKU_INC.'inc/load.php'); 206c7cb395cSAdrian Lang 207642e976cSAndreas Gohr// from now on everything is an exception 20824870174SAndreas GohrErrorHandler::register(); 209642e976cSAndreas Gohr 2100f8f7aaaSDanny Lin// disable gzip if not available 21113c37900SAndreas Gohrdefine('DOKU_HAS_BZIP', function_exists('bzopen')); 21213c37900SAndreas Gohrdefine('DOKU_HAS_GZIP', function_exists('gzopen')); 21313c37900SAndreas Gohrif ($conf['compression'] == 'bz2' && !DOKU_HAS_BZIP) { 2140f8f7aaaSDanny Lin $conf['compression'] = 'gz'; 2150f8f7aaaSDanny Lin} 21613c37900SAndreas Gohrif ($conf['compression'] == 'gz' && !DOKU_HAS_GZIP) { 2170f8f7aaaSDanny Lin $conf['compression'] = 0; 2180f8f7aaaSDanny Lin} 2190f8f7aaaSDanny Lin 22089177306SAndreas Gohr// input handle class 22189177306SAndreas Gohrglobal $INPUT; 22224870174SAndreas Gohr$INPUT = new Input(); 22389177306SAndreas Gohr 224f1986589SMichael Klier// initialize plugin controller 225f1986589SMichael Klier$plugin_controller = new $plugin_controller_class(); 226f1986589SMichael Klier 227f1986589SMichael Klier// initialize the event handler 228f1986589SMichael Klierglobal $EVENT_HANDLER; 229e1d9dcc8SAndreas Gohr$EVENT_HANDLER = new EventHandler(); 230f1986589SMichael Klier 2316d06b26aSDominik Eckelmann$local = $conf['lang']; 232cbb44eabSAndreas GohrEvent::createAndTrigger('INIT_LANG_LOAD', $local, 'init_lang', true); 2336d06b26aSDominik Eckelmann 2346d06b26aSDominik Eckelmann 23516905344SAndreas Gohr// setup authentication system 236c7cb395cSAdrian Langif (!defined('NOSESSION')) { 23716905344SAndreas Gohr auth_setup(); 238c7cb395cSAdrian Lang} 239f62ea8a1Sandi 2405ec3fefcSAndreas Gohr// setup mail system 2415ec3fefcSAndreas Gohrmail_setup(); 2425ec3fefcSAndreas Gohr 243042b9fecSAndreas Gohr$nil = null; 244042b9fecSAndreas GohrEvent::createAndTrigger('DOKUWIKI_INIT_DONE', $nil, null, false); 245042b9fecSAndreas Gohr 246f62ea8a1Sandi/** 2476eb3cdf6SAndreas Gohr * Initializes the session 2486eb3cdf6SAndreas Gohr * 2496eb3cdf6SAndreas Gohr * Makes sure the passed session cookie is valid, invalid ones are ignored an a new session ID is issued 2506eb3cdf6SAndreas Gohr * 2516eb3cdf6SAndreas Gohr * @link http://stackoverflow.com/a/33024310/172068 252924e477eSAndreas Gohr * @link http://php.net/manual/en/session.configuration.php#ini.session.sid-length 2536eb3cdf6SAndreas Gohr */ 254d868eb89SAndreas Gohrfunction init_session() 255d868eb89SAndreas Gohr{ 2566eb3cdf6SAndreas Gohr global $conf; 2576eb3cdf6SAndreas Gohr session_name(DOKU_SESSION_NAME); 258bf8392ebSAndreas Gohr session_set_cookie_params([ 259bf8392ebSAndreas Gohr 'lifetime' => DOKU_SESSION_LIFETIME, 260bf8392ebSAndreas Gohr 'path' => DOKU_SESSION_PATH, 261bf8392ebSAndreas Gohr 'domain' => DOKU_SESSION_DOMAIN, 262bf8392ebSAndreas Gohr 'secure' => ($conf['securecookie'] && is_ssl()), 263bf8392ebSAndreas Gohr 'httponly' => true, 264bf8392ebSAndreas Gohr 'samesite' => 'Lax', 265bf8392ebSAndreas Gohr ]); 2666eb3cdf6SAndreas Gohr 2676eb3cdf6SAndreas Gohr // make sure the session cookie contains a valid session ID 268924e477eSAndreas Gohr if (isset($_COOKIE[DOKU_SESSION_NAME]) && !preg_match('/^[-,a-zA-Z0-9]{22,256}$/', $_COOKIE[DOKU_SESSION_NAME])) { 2696eb3cdf6SAndreas Gohr unset($_COOKIE[DOKU_SESSION_NAME]); 2706eb3cdf6SAndreas Gohr } 2716eb3cdf6SAndreas Gohr 2726eb3cdf6SAndreas Gohr session_start(); 2736eb3cdf6SAndreas Gohr} 2746eb3cdf6SAndreas Gohr 2756eb3cdf6SAndreas Gohr 2766eb3cdf6SAndreas Gohr/** 27798407a7aSandi * Checks paths from config file 27898407a7aSandi */ 279d868eb89SAndreas Gohrfunction init_paths() 280d868eb89SAndreas Gohr{ 28198407a7aSandi global $conf; 28298407a7aSandi 2830ecde6ceSAndreas Gohr $paths = [ 2840ecde6ceSAndreas Gohr 'datadir' => 'pages', 28598407a7aSandi 'olddir' => 'attic', 28698407a7aSandi 'mediadir' => 'media', 287e4f389efSKate Arzamastseva 'mediaolddir' => 'media_attic', 28898407a7aSandi 'metadir' => 'meta', 289e4f389efSKate Arzamastseva 'mediametadir' => 'media_meta', 29098407a7aSandi 'cachedir' => 'cache', 291579b0f7eSTNHarris 'indexdir' => 'index', 292de33a58fSMichael Klier 'lockdir' => 'locks', 2930ecde6ceSAndreas Gohr 'tmpdir' => 'tmp', 2940ecde6ceSAndreas Gohr 'logdir' => 'log', 2950ecde6ceSAndreas Gohr ]; 29698407a7aSandi 29798407a7aSandi foreach ($paths as $c => $p) { 2987f086b67SAnika Henke $path = empty($conf[$c]) ? $conf['savedir'].'/'.$p : $conf[$c]; 2996b9c156cSAnika Henke $conf[$c] = init_path($path); 300697a39aeSAndreas Gohr if (empty($conf[$c])) { 301697a39aeSAndreas Gohr $path = fullpath($path); 3026b9c156cSAnika Henke nice_die("The $c ('$p') at $path is not found, isn't accessible or writable. 30369dc3177SAndreas Gohr You should check your config and permission settings. 30469dc3177SAndreas Gohr Or maybe you want to <a href=\"install.php\">run the 30569dc3177SAndreas Gohr installer</a>?"); 30698407a7aSandi } 307697a39aeSAndreas Gohr } 30871726d78SBen Coburn 30971726d78SBen Coburn // path to old changelog only needed for upgrading 31064159a61SAndreas Gohr $conf['changelog_old'] = init_path( 31124870174SAndreas Gohr $conf['changelog'] ?? $conf['savedir'] . '/changes.log' 31264159a61SAndreas Gohr ); 313177d6836SAndreas Gohr if ($conf['changelog_old']=='') { 314177d6836SAndreas Gohrunset($conf['changelog_old']); } 31571726d78SBen Coburn // hardcoded changelog because it is now a cache that lives in meta 31671726d78SBen Coburn $conf['changelog'] = $conf['metadir'].'/_dokuwiki.changes'; 31799c8d7f2Smichael $conf['media_changelog'] = $conf['metadir'].'/_media.changes'; 31898407a7aSandi} 31998407a7aSandi 32038fb1fc7SGerrit Uitslag/** 32138fb1fc7SGerrit Uitslag * Load the language strings 32238fb1fc7SGerrit Uitslag * 32338fb1fc7SGerrit Uitslag * @param string $langCode language code, as passed by event handler 32438fb1fc7SGerrit Uitslag */ 325d868eb89SAndreas Gohrfunction init_lang($langCode) 326d868eb89SAndreas Gohr{ 3276d06b26aSDominik Eckelmann //prepare language array 328dd7a6159SGerrit Uitslag global $lang, $config_cascade; 32924870174SAndreas Gohr $lang = []; 3306d06b26aSDominik Eckelmann 3316d06b26aSDominik Eckelmann //load the language files 3321d82c8d3SChristopher Smith require(DOKU_INC.'inc/lang/en/lang.php'); 333dd7a6159SGerrit Uitslag foreach ($config_cascade['lang']['core'] as $config_file) { 33479e79377SAndreas Gohr if (file_exists($config_file . 'en/lang.php')) { 335dd7a6159SGerrit Uitslag include($config_file . 'en/lang.php'); 336dd7a6159SGerrit Uitslag } 337dd7a6159SGerrit Uitslag } 338dd7a6159SGerrit Uitslag 3396d06b26aSDominik Eckelmann if ($langCode && $langCode != 'en') { 3406d06b26aSDominik Eckelmann if (file_exists(DOKU_INC."inc/lang/$langCode/lang.php")) { 3411d82c8d3SChristopher Smith require(DOKU_INC."inc/lang/$langCode/lang.php"); 3426d06b26aSDominik Eckelmann } 343dd7a6159SGerrit Uitslag foreach ($config_cascade['lang']['core'] as $config_file) { 34479e79377SAndreas Gohr if (file_exists($config_file . "$langCode/lang.php")) { 345dd7a6159SGerrit Uitslag include($config_file . "$langCode/lang.php"); 3466d06b26aSDominik Eckelmann } 347dd7a6159SGerrit Uitslag } 3486d06b26aSDominik Eckelmann } 3496d06b26aSDominik Eckelmann} 3506d06b26aSDominik Eckelmann 35198407a7aSandi/** 3526b9c156cSAnika Henke * Checks the existence of certain files and creates them if missing. 3537367b368SAndreas Gohr */ 354d868eb89SAndreas Gohrfunction init_files() 355d868eb89SAndreas Gohr{ 3567367b368SAndreas Gohr global $conf; 3570d8850c4SAndreas Gohr 35824870174SAndreas Gohr $files = [$conf['indexdir'].'/page.idx']; 3597367b368SAndreas Gohr 3607367b368SAndreas Gohr foreach ($files as $file) { 36179e79377SAndreas Gohr if (!file_exists($file)) { 3620d8850c4SAndreas Gohr $fh = @fopen($file, 'a'); 3630d8850c4SAndreas Gohr if ($fh) { 3647367b368SAndreas Gohr fclose($fh); 3653aa75874Smovatica if ($conf['fperm']) chmod($file, $conf['fperm']); 3660d8850c4SAndreas Gohr } else { 3673816dcbcSAndreas Gohr nice_die("$file is not writable. Check your permissions settings!"); 3680d8850c4SAndreas Gohr } 3697367b368SAndreas Gohr } 3707367b368SAndreas Gohr } 3717367b368SAndreas Gohr} 3727367b368SAndreas Gohr 3737367b368SAndreas Gohr/** 3740d8850c4SAndreas Gohr * Returns absolute path 375f62ea8a1Sandi * 3760d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC. 3777f086b67SAnika Henke * Check for accessibility on directories as well. 3780d8850c4SAndreas Gohr * 3790d8850c4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 380f50a239bSTakamura * 381f50a239bSTakamura * @param string $path 382f50a239bSTakamura * 383f50a239bSTakamura * @return bool|string 384f62ea8a1Sandi */ 385d868eb89SAndreas Gohrfunction init_path($path) 386d868eb89SAndreas Gohr{ 3876b9c156cSAnika Henke // check existence 38800976812SAndreas Gohr $p = fullpath($path); 38979e79377SAndreas Gohr if (!file_exists($p)) { 39000976812SAndreas Gohr $p = fullpath(DOKU_INC.$path); 39179e79377SAndreas Gohr if (!file_exists($p)) { 3928fc4e739Sandi return ''; 393f62ea8a1Sandi } 3940d8850c4SAndreas Gohr } 3950d8850c4SAndreas Gohr 3960d8850c4SAndreas Gohr // check writability 3970d8850c4SAndreas Gohr if (!@is_writable($p)) { 3980d8850c4SAndreas Gohr return ''; 3990d8850c4SAndreas Gohr } 4000d8850c4SAndreas Gohr 4010d8850c4SAndreas Gohr // check accessability (execute bit) for directories 40279e79377SAndreas Gohr if (@is_dir($p) && !file_exists("$p/.")) { 4030d8850c4SAndreas Gohr return ''; 4040d8850c4SAndreas Gohr } 4050d8850c4SAndreas Gohr 4060d8850c4SAndreas Gohr return $p; 4070d8850c4SAndreas Gohr} 4088c4f28e8Sjan 409ed7b5f09Sandi/** 4101ca31cfeSAndreas Gohr * Sets the internal config values fperm and dperm which, when set, 4111ca31cfeSAndreas Gohr * will be used to change the permission of a newly created dir or 4121ca31cfeSAndreas Gohr * file with chmod. Considers the influence of the system's umask 4131ca31cfeSAndreas Gohr * setting the values only if needed. 4141ca31cfeSAndreas Gohr */ 415d868eb89SAndreas Gohrfunction init_creationmodes() 416d868eb89SAndreas Gohr{ 4171ca31cfeSAndreas Gohr global $conf; 4181ca31cfeSAndreas Gohr 4191ca31cfeSAndreas Gohr // Legacy support for old umask/dmask scheme 4201ca31cfeSAndreas Gohr unset($conf['dmask']); 4211ca31cfeSAndreas Gohr unset($conf['fmask']); 4221ca31cfeSAndreas Gohr unset($conf['umask']); 42323420346SDamien Regad 42423420346SDamien Regad $conf['fperm'] = false; 42523420346SDamien Regad $conf['dperm'] = false; 4261ca31cfeSAndreas Gohr 4279f3cdec3SAndreas Gohr // get system umask, fallback to 0 if none available 4289f3cdec3SAndreas Gohr $umask = @umask(); 4299f3cdec3SAndreas Gohr if (!$umask) $umask = 0000; 4301ca31cfeSAndreas Gohr 4311ca31cfeSAndreas Gohr // check what is set automatically by the system on file creation 4321ca31cfeSAndreas Gohr // and set the fperm param if it's not what we want 433bd539124SAndreas Gohr $auto_fmode = 0666 & ~$umask; 4341ca31cfeSAndreas Gohr if ($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode']; 4351ca31cfeSAndreas Gohr 436bd539124SAndreas Gohr // check what is set automatically by the system on directory creation 437bd539124SAndreas Gohr // and set the dperm param if it's not what we want. 438bd539124SAndreas Gohr $auto_dmode = 0777 & ~$umask; 4391ca31cfeSAndreas Gohr if ($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode']; 4401ca31cfeSAndreas Gohr} 4411ca31cfeSAndreas Gohr 4421ca31cfeSAndreas Gohr/** 443ed7b5f09Sandi * Returns the full absolute URL to the directory where 444ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash) 445ed7b5f09Sandi * 446585bf44eSChristopher Smith * !! Can not access $_SERVER values through $INPUT 447585bf44eSChristopher Smith * !! here as this function is called before $INPUT is 448585bf44eSChristopher Smith * !! initialized. 449585bf44eSChristopher Smith * 450ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org> 451f50a239bSTakamura * 452f50a239bSTakamura * @param null|string $abs 453f50a239bSTakamura * 454f50a239bSTakamura * @return string 455ed7b5f09Sandi */ 456d868eb89SAndreas Gohrfunction getBaseURL($abs = null) 457d868eb89SAndreas Gohr{ 458ed7b5f09Sandi global $conf; 459ed7b5f09Sandi //if canonical url enabled always return absolute 4604b1a4e04SAndreas Gohr if (is_null($abs)) $abs = $conf['canonical']; 461ed7b5f09Sandi 4621858e4d7SGerry Weißbach if (!empty($conf['basedir'])) { 46346c73e01SChris Smith $dir = $conf['basedir']; 46489aa05dbSAndreas Gohr } elseif (substr($_SERVER['SCRIPT_NAME'], -4) == '.php') { 46546c73e01SChris Smith $dir = dirname($_SERVER['SCRIPT_NAME']); 46689aa05dbSAndreas Gohr } elseif (substr($_SERVER['PHP_SELF'], -4) == '.php') { 46746c73e01SChris Smith $dir = dirname($_SERVER['PHP_SELF']); 468093ec9e4Sandi } elseif ($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']) { 469dccd6b2bSAndreas Gohr $dir = preg_replace( 470dccd6b2bSAndreas Gohr '/^'.preg_quote($_SERVER['DOCUMENT_ROOT'], '/').'/', 471dccd6b2bSAndreas Gohr '', 472dccd6b2bSAndreas Gohr $_SERVER['SCRIPT_FILENAME'] 473dccd6b2bSAndreas Gohr ); 47446c73e01SChris Smith $dir = dirname('/'.$dir); 47592b83b77Sandi } else { 47646c73e01SChris Smith $dir = '.'; //probably wrong 47792b83b77Sandi } 478ed7b5f09Sandi 47946c73e01SChris Smith $dir = str_replace('\\', '/', $dir); // bugfix for weird WIN behaviour 48046c73e01SChris Smith $dir = preg_replace('#//+#', '/', "/$dir/"); // ensure leading and trailing slashes 481ed7b5f09Sandi 482f62ea8a1Sandi //handle script in lib/exe dir 483f62ea8a1Sandi $dir = preg_replace('!lib/exe/$!', '', $dir); 484f62ea8a1Sandi 485488d5fa0SMichael Klier chi@chimeric.de //handle script in lib/plugins dir 486488d5fa0SMichael Klier chi@chimeric.de $dir = preg_replace('!lib/plugins/.*$!', '', $dir); 487488d5fa0SMichael Klier chi@chimeric.de 488ed7b5f09Sandi //finish here for relative URLs 489ed7b5f09Sandi if (!$abs) return $dir; 490ed7b5f09Sandi 49164159a61SAndreas Gohr //use config if available, trim any slash from end of baseurl to avoid multiple consecutive slashes in the path 4921858e4d7SGerry Weißbach if (!empty($conf['baseurl'])) return rtrim($conf['baseurl'], '/').$dir; 493ef7b3ecdSAndreas Gohr 494e82e3526SAndreas Gohr //split hostheader into host and port 4955627186cSAndreas Gohr if (isset($_SERVER['HTTP_HOST'])) { 496204b27c8SMichael Hamann $parsed_host = parse_url('http://'.$_SERVER['HTTP_HOST']); 49724870174SAndreas Gohr $host = $parsed_host['host'] ?? null; 49824870174SAndreas Gohr $port = $parsed_host['port'] ?? null; 4995627186cSAndreas Gohr } elseif (isset($_SERVER['SERVER_NAME'])) { 500204b27c8SMichael Hamann $parsed_host = parse_url('http://'.$_SERVER['SERVER_NAME']); 50124870174SAndreas Gohr $host = $parsed_host['host'] ?? null; 50224870174SAndreas Gohr $port = $parsed_host['port'] ?? null; 5035627186cSAndreas Gohr } else { 5045627186cSAndreas Gohr $host = php_uname('n'); 505c66972f2SAdrian Lang $port = ''; 5065627186cSAndreas Gohr } 5075627186cSAndreas Gohr 508204b27c8SMichael Hamann if (is_null($port)) { 509204b27c8SMichael Hamann $port = ''; 510204b27c8SMichael Hamann } 511204b27c8SMichael Hamann 512f5c6743cSAndreas Gohr if (!is_ssl()) { 513ed7b5f09Sandi $proto = 'http://'; 514e82e3526SAndreas Gohr if ($port == '80') { 515ed7b5f09Sandi $port = ''; 516ed7b5f09Sandi } 517ed7b5f09Sandi } else { 518ed7b5f09Sandi $proto = 'https://'; 519e82e3526SAndreas Gohr if ($port == '443') { 520ed7b5f09Sandi $port = ''; 521ed7b5f09Sandi } 522ed7b5f09Sandi } 523ed7b5f09Sandi 524c66972f2SAdrian Lang if ($port !== '') $port = ':'.$port; 525e82e3526SAndreas Gohr 526ed7b5f09Sandi return $proto.$host.$port.$dir; 527ed7b5f09Sandi} 528ed7b5f09Sandi 529b000c6d4Sandi/** 530f5c6743cSAndreas Gohr * Check if accessed via HTTPS 531f5c6743cSAndreas Gohr * 532f5c6743cSAndreas Gohr * Apache leaves ,$_SERVER['HTTPS'] empty when not available, IIS sets it to 'off'. 533f5c6743cSAndreas Gohr * 'false' and 'disabled' are just guessing 534f5c6743cSAndreas Gohr * 535f5c6743cSAndreas Gohr * @returns bool true when SSL is active 536f5c6743cSAndreas Gohr */ 537d868eb89SAndreas Gohrfunction is_ssl() 538d868eb89SAndreas Gohr{ 53919738e65SEnrico Tagliavini // check if we are behind a reverse proxy 54019738e65SEnrico Tagliavini if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) { 54119738e65SEnrico Tagliavini if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') { 54219738e65SEnrico Tagliavini return true; 54319738e65SEnrico Tagliavini } else { 54419738e65SEnrico Tagliavini return false; 54519738e65SEnrico Tagliavini } 54619738e65SEnrico Tagliavini } 547*7d34963bSAndreas Gohr if ( 548*7d34963bSAndreas Gohr !isset($_SERVER['HTTPS']) || 549*7d34963bSAndreas Gohr preg_match('/^(|off|false|disabled)$/i', $_SERVER['HTTPS']) 550*7d34963bSAndreas Gohr ) { 551f5c6743cSAndreas Gohr return false; 552f5c6743cSAndreas Gohr } else { 553f5c6743cSAndreas Gohr return true; 554f5c6743cSAndreas Gohr } 555f5c6743cSAndreas Gohr} 556f5c6743cSAndreas Gohr 557f5c6743cSAndreas Gohr/** 55826714386SAndreas Gohr * checks it is windows OS 55926714386SAndreas Gohr * @return bool 56026714386SAndreas Gohr */ 561d868eb89SAndreas Gohrfunction isWindows() 562d868eb89SAndreas Gohr{ 56324870174SAndreas Gohr return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'; 56426714386SAndreas Gohr} 56526714386SAndreas Gohr 56626714386SAndreas Gohr/** 5673816dcbcSAndreas Gohr * print a nice message even if no styles are loaded yet. 568f50a239bSTakamura * 569f50a239bSTakamura * @param integer|string $msg 5703816dcbcSAndreas Gohr */ 571d868eb89SAndreas Gohrfunction nice_die($msg) 572d868eb89SAndreas Gohr{ 5733816dcbcSAndreas Gohr echo<<<EOT 574c8839c22SAnika Henke<!DOCTYPE html> 5753816dcbcSAndreas Gohr<html> 5763816dcbcSAndreas Gohr<head><title>DokuWiki Setup Error</title></head> 5773816dcbcSAndreas Gohr<body style="font-family: Arial, sans-serif"> 5783816dcbcSAndreas Gohr <div style="width:60%; margin: auto; background-color: #fcc; 5793816dcbcSAndreas Gohr border: 1px solid #faa; padding: 0.5em 1em;"> 5803816dcbcSAndreas Gohr <h1 style="font-size: 120%">DokuWiki Setup Error</h1> 5813816dcbcSAndreas Gohr <p>$msg</p> 5823816dcbcSAndreas Gohr </div> 5833816dcbcSAndreas Gohr</body> 5843816dcbcSAndreas Gohr</html> 5853816dcbcSAndreas GohrEOT; 5863862da0eSAndreas Gohr if (defined('DOKU_UNITTEST')) { 5873862da0eSAndreas Gohr throw new RuntimeException('nice_die: '.$msg); 5883862da0eSAndreas Gohr } 5890a4266d4SElan Ruusamäe exit(1); 5903816dcbcSAndreas Gohr} 5913816dcbcSAndreas Gohr 59200976812SAndreas Gohr/** 59300976812SAndreas Gohr * A realpath() replacement 59400976812SAndreas Gohr * 59500976812SAndreas Gohr * This function behaves similar to PHP's realpath() but does not resolve 59600976812SAndreas Gohr * symlinks or accesses upper directories 59700976812SAndreas Gohr * 5984761d30cSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 59900976812SAndreas Gohr * @author <richpageau at yahoo dot co dot uk> 60059752844SAnders Sandblad * @link http://php.net/manual/en/function.realpath.php#75992 601f50a239bSTakamura * 602f50a239bSTakamura * @param string $path 603f50a239bSTakamura * @param bool $exists 604f50a239bSTakamura * 605f50a239bSTakamura * @return bool|string 60600976812SAndreas Gohr */ 607d868eb89SAndreas Gohrfunction fullpath($path, $exists = false) 608d868eb89SAndreas Gohr{ 6094761d30cSAndreas Gohr static $run = 0; 6104761d30cSAndreas Gohr $root = ''; 611724970e6SAndreas Gohr $iswin = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' || !empty($GLOBALS['DOKU_UNITTEST_ASSUME_WINDOWS'])); 61200976812SAndreas Gohr 6134761d30cSAndreas Gohr // find the (indestructable) root of the path - keeps windows stuff intact 6142401f18dSSyntaxseed if ($path[0] == '/') { 6154761d30cSAndreas Gohr $root = '/'; 6164761d30cSAndreas Gohr } elseif ($iswin) { 6174761d30cSAndreas Gohr // match drive letter and UNC paths 6184761d30cSAndreas Gohr if (preg_match('!^([a-zA-z]:)(.*)!', $path, $match)) { 619b9c4302bSAndreas Gohr $root = $match[1].'/'; 6204761d30cSAndreas Gohr $path = $match[2]; 6214761d30cSAndreas Gohr } elseif (preg_match('!^(\\\\\\\\[^\\\\/]+\\\\[^\\\\/]+[\\\\/])(.*)!', $path, $match)) { 6224761d30cSAndreas Gohr $root = $match[1]; 6234761d30cSAndreas Gohr $path = $match[2]; 62400976812SAndreas Gohr } 6254761d30cSAndreas Gohr } 6264761d30cSAndreas Gohr $path = str_replace('\\', '/', $path); 6274761d30cSAndreas Gohr 6284761d30cSAndreas Gohr // if the given path wasn't absolute already, prepend the script path and retry 6294761d30cSAndreas Gohr if (!$root) { 6304761d30cSAndreas Gohr $base = dirname($_SERVER['SCRIPT_FILENAME']); 6314761d30cSAndreas Gohr $path = $base.'/'.$path; 6324761d30cSAndreas Gohr if ($run == 0) { // avoid endless recursion when base isn't absolute for some reason 6334761d30cSAndreas Gohr $run++; 634b328697dSAndreas Gohr return fullpath($path, $exists); 6354761d30cSAndreas Gohr } 6364761d30cSAndreas Gohr } 6374761d30cSAndreas Gohr $run = 0; 63800976812SAndreas Gohr 63900976812SAndreas Gohr // canonicalize 64000976812SAndreas Gohr $path=explode('/', $path); 64124870174SAndreas Gohr $newpath=[]; 642ef38bfe8SAndreas Gohr foreach ($path as $p) { 643ef38bfe8SAndreas Gohr if ($p === '' || $p === '.') continue; 644ef38bfe8SAndreas Gohr if ($p==='..') { 64500976812SAndreas Gohr array_pop($newpath); 64600976812SAndreas Gohr continue; 64700976812SAndreas Gohr } 64824870174SAndreas Gohr $newpath[] = $p; 64900976812SAndreas Gohr } 6504761d30cSAndreas Gohr $finalpath = $root.implode('/', $newpath); 65100976812SAndreas Gohr 6526b9c156cSAnika Henke // check for existence when needed (except when unit testing) 65379e79377SAndreas Gohr if ($exists && !defined('DOKU_UNITTEST') && !file_exists($finalpath)) { 6544761d30cSAndreas Gohr return false; 65500976812SAndreas Gohr } 6564761d30cSAndreas Gohr return $finalpath; 65700976812SAndreas Gohr} 658