xref: /dokuwiki/inc/init.php (revision 177d6836e2f75d0e404be9c566e61725852a1e07)
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', '&amp;');
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;
1553138b5c7SAndreas Gohrif ($conf['gzip_output'] &&
1563138b5c7SAndreas Gohr        !defined('DOKU_DISABLE_GZIP_OUTPUT') &&
15765f6e7d6SMichael Hamann        function_exists('ob_gzhandler') &&
15899e10b7fSMichael Hamann        // Disable compression when a (compressed) sitemap might be delivered
15965f6e7d6SMichael Hamann        // See https://bugs.dokuwiki.org/index.php?do=details&task_id=2576
16099e10b7fSMichael Hamann        $ACT != 'sitemap') {
1613138b5c7SAndreas Gohr    ob_start('ob_gzhandler');
1623138b5c7SAndreas Gohr}
1633138b5c7SAndreas Gohr
164ed7b5f09Sandi// init session
1656534245aSAndreas Gohrif (!headers_sent() && !defined('NOSESSION')) {
166c09f0eb1SGerrit Uitslag    if (!defined('DOKU_SESSION_NAME'))     define('DOKU_SESSION_NAME', "DokuWiki");
167c09f0eb1SGerrit Uitslag    if (!defined('DOKU_SESSION_LIFETIME')) define('DOKU_SESSION_LIFETIME', 0);
16855a71a16SGerrit Uitslag    if (!defined('DOKU_SESSION_PATH')) {
16973ab87deSGabriel Birke        $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir'];
17055a71a16SGerrit Uitslag        define('DOKU_SESSION_PATH', $cookieDir);
171f5c6743cSAndreas Gohr    }
172c09f0eb1SGerrit Uitslag    if (!defined('DOKU_SESSION_DOMAIN'))   define('DOKU_SESSION_DOMAIN', '');
173c09f0eb1SGerrit Uitslag
1746eb3cdf6SAndreas Gohr    // start the session
1756eb3cdf6SAndreas Gohr    init_session();
17614a122deSAndreas Gohr
17714a122deSAndreas Gohr    // load left over messages
17814a122deSAndreas Gohr    if (isset($_SESSION[DOKU_COOKIE]['msg'])) {
17914a122deSAndreas Gohr        $MSG = $_SESSION[DOKU_COOKIE]['msg'];
18014a122deSAndreas Gohr        unset($_SESSION[DOKU_COOKIE]['msg']);
18114a122deSAndreas Gohr    }
182bad31ae9SAndreas Gohr}
183ed7b5f09Sandi
184a1637ffdSAndreas Gohr// don't let cookies ever interfere with request vars
185a1637ffdSAndreas Gohr$_REQUEST = array_merge($_GET, $_POST);
186a1637ffdSAndreas Gohr
1873dea4ebcSAndreas Gohr// we don't want a purge URL to be digged
1880e80bb5eSChristopher Smithif (isset($_REQUEST['purge']) && !empty($_SERVER['HTTP_REFERER'])) unset($_REQUEST['purge']);
1893dea4ebcSAndreas Gohr
1901ca31cfeSAndreas Gohr// precalculate file creation modes
1911ca31cfeSAndreas Gohrinit_creationmodes();
192ed7b5f09Sandi
1933dc3a5f1Sandi// make real paths and check them
19498407a7aSandiinit_paths();
1957367b368SAndreas Gohrinit_files();
196ed7b5f09Sandi
197f1986589SMichael Klier// setup plugin controller class (can be overwritten in preload.php)
198f1986589SMichael Klierglobal $plugin_controller_class, $plugin_controller;
19924870174SAndreas Gohrif (empty($plugin_controller_class)) $plugin_controller_class = PluginController::class;
200f1986589SMichael Klier
201c7cb395cSAdrian Lang// load libraries
202605f8e8dSAndreas Gohrrequire_once(DOKU_INC.'vendor/autoload.php');
203c7cb395cSAdrian Langrequire_once(DOKU_INC.'inc/load.php');
204c7cb395cSAdrian Lang
205642e976cSAndreas Gohr// from now on everything is an exception
20624870174SAndreas GohrErrorHandler::register();
207642e976cSAndreas Gohr
2080f8f7aaaSDanny Lin// disable gzip if not available
20913c37900SAndreas Gohrdefine('DOKU_HAS_BZIP', function_exists('bzopen'));
21013c37900SAndreas Gohrdefine('DOKU_HAS_GZIP', function_exists('gzopen'));
21113c37900SAndreas Gohrif ($conf['compression'] == 'bz2' && !DOKU_HAS_BZIP) {
2120f8f7aaaSDanny Lin    $conf['compression'] = 'gz';
2130f8f7aaaSDanny Lin}
21413c37900SAndreas Gohrif ($conf['compression'] == 'gz' && !DOKU_HAS_GZIP) {
2150f8f7aaaSDanny Lin    $conf['compression'] = 0;
2160f8f7aaaSDanny Lin}
2170f8f7aaaSDanny Lin
21889177306SAndreas Gohr// input handle class
21989177306SAndreas Gohrglobal $INPUT;
22024870174SAndreas Gohr$INPUT = new Input();
22189177306SAndreas Gohr
222f1986589SMichael Klier// initialize plugin controller
223f1986589SMichael Klier$plugin_controller = new $plugin_controller_class();
224f1986589SMichael Klier
225f1986589SMichael Klier// initialize the event handler
226f1986589SMichael Klierglobal $EVENT_HANDLER;
227e1d9dcc8SAndreas Gohr$EVENT_HANDLER = new EventHandler();
228f1986589SMichael Klier
2296d06b26aSDominik Eckelmann$local = $conf['lang'];
230cbb44eabSAndreas GohrEvent::createAndTrigger('INIT_LANG_LOAD', $local, 'init_lang', true);
2316d06b26aSDominik Eckelmann
2326d06b26aSDominik Eckelmann
23316905344SAndreas Gohr// setup authentication system
234c7cb395cSAdrian Langif (!defined('NOSESSION')) {
23516905344SAndreas Gohr    auth_setup();
236c7cb395cSAdrian Lang}
237f62ea8a1Sandi
2385ec3fefcSAndreas Gohr// setup mail system
2395ec3fefcSAndreas Gohrmail_setup();
2405ec3fefcSAndreas Gohr
241042b9fecSAndreas Gohr$nil = null;
242042b9fecSAndreas GohrEvent::createAndTrigger('DOKUWIKI_INIT_DONE', $nil, null, false);
243042b9fecSAndreas Gohr
244f62ea8a1Sandi/**
2456eb3cdf6SAndreas Gohr * Initializes the session
2466eb3cdf6SAndreas Gohr *
2476eb3cdf6SAndreas Gohr * Makes sure the passed session cookie is valid, invalid ones are ignored an a new session ID is issued
2486eb3cdf6SAndreas Gohr *
2496eb3cdf6SAndreas Gohr * @link http://stackoverflow.com/a/33024310/172068
250924e477eSAndreas Gohr * @link http://php.net/manual/en/session.configuration.php#ini.session.sid-length
2516eb3cdf6SAndreas Gohr */
252d868eb89SAndreas Gohrfunction init_session()
253d868eb89SAndreas Gohr{
2546eb3cdf6SAndreas Gohr    global $conf;
2556eb3cdf6SAndreas Gohr    session_name(DOKU_SESSION_NAME);
256bf8392ebSAndreas Gohr    session_set_cookie_params([
257bf8392ebSAndreas Gohr        'lifetime' => DOKU_SESSION_LIFETIME,
258bf8392ebSAndreas Gohr        'path' => DOKU_SESSION_PATH,
259bf8392ebSAndreas Gohr        'domain' => DOKU_SESSION_DOMAIN,
260bf8392ebSAndreas Gohr        'secure' => ($conf['securecookie'] && is_ssl()),
261bf8392ebSAndreas Gohr        'httponly' => true,
262bf8392ebSAndreas Gohr        'samesite' => 'Lax',
263bf8392ebSAndreas Gohr    ]);
2646eb3cdf6SAndreas Gohr
2656eb3cdf6SAndreas Gohr    // make sure the session cookie contains a valid session ID
266924e477eSAndreas Gohr    if (isset($_COOKIE[DOKU_SESSION_NAME]) && !preg_match('/^[-,a-zA-Z0-9]{22,256}$/', $_COOKIE[DOKU_SESSION_NAME])) {
2676eb3cdf6SAndreas Gohr        unset($_COOKIE[DOKU_SESSION_NAME]);
2686eb3cdf6SAndreas Gohr    }
2696eb3cdf6SAndreas Gohr
2706eb3cdf6SAndreas Gohr    session_start();
2716eb3cdf6SAndreas Gohr}
2726eb3cdf6SAndreas Gohr
2736eb3cdf6SAndreas Gohr
2746eb3cdf6SAndreas Gohr/**
27598407a7aSandi * Checks paths from config file
27698407a7aSandi */
277d868eb89SAndreas Gohrfunction init_paths()
278d868eb89SAndreas Gohr{
27998407a7aSandi    global $conf;
28098407a7aSandi
2810ecde6ceSAndreas Gohr    $paths = [
2820ecde6ceSAndreas Gohr        'datadir'   => 'pages',
28398407a7aSandi        'olddir'    => 'attic',
28498407a7aSandi        'mediadir'  => 'media',
285e4f389efSKate Arzamastseva        'mediaolddir' => 'media_attic',
28698407a7aSandi        'metadir'   => 'meta',
287e4f389efSKate Arzamastseva        'mediametadir' => 'media_meta',
28898407a7aSandi        'cachedir'  => 'cache',
289579b0f7eSTNHarris        'indexdir'  => 'index',
290de33a58fSMichael Klier        'lockdir'   => 'locks',
2910ecde6ceSAndreas Gohr        'tmpdir'    => 'tmp',
2920ecde6ceSAndreas Gohr        'logdir'    => 'log',
2930ecde6ceSAndreas Gohr    ];
29498407a7aSandi
29598407a7aSandi    foreach ($paths as $c => $p) {
2967f086b67SAnika Henke        $path = empty($conf[$c]) ? $conf['savedir'].'/'.$p : $conf[$c];
2976b9c156cSAnika Henke        $conf[$c] = init_path($path);
298697a39aeSAndreas Gohr        if (empty($conf[$c])) {
299697a39aeSAndreas Gohr            $path = fullpath($path);
3006b9c156cSAnika Henke            nice_die("The $c ('$p') at $path is not found, isn't accessible or writable.
30169dc3177SAndreas Gohr                You should check your config and permission settings.
30269dc3177SAndreas Gohr                Or maybe you want to <a href=\"install.php\">run the
30369dc3177SAndreas Gohr                installer</a>?");
30498407a7aSandi        }
305697a39aeSAndreas Gohr    }
30671726d78SBen Coburn
30771726d78SBen Coburn    // path to old changelog only needed for upgrading
30864159a61SAndreas Gohr    $conf['changelog_old'] = init_path(
30924870174SAndreas Gohr        $conf['changelog'] ?? $conf['savedir'] . '/changes.log'
31064159a61SAndreas Gohr    );
311*177d6836SAndreas Gohr    if ($conf['changelog_old']=='') {
312*177d6836SAndreas Gohrunset($conf['changelog_old']); }
31371726d78SBen Coburn    // hardcoded changelog because it is now a cache that lives in meta
31471726d78SBen Coburn    $conf['changelog'] = $conf['metadir'].'/_dokuwiki.changes';
31599c8d7f2Smichael    $conf['media_changelog'] = $conf['metadir'].'/_media.changes';
31698407a7aSandi}
31798407a7aSandi
31838fb1fc7SGerrit Uitslag/**
31938fb1fc7SGerrit Uitslag * Load the language strings
32038fb1fc7SGerrit Uitslag *
32138fb1fc7SGerrit Uitslag * @param string $langCode language code, as passed by event handler
32238fb1fc7SGerrit Uitslag */
323d868eb89SAndreas Gohrfunction init_lang($langCode)
324d868eb89SAndreas Gohr{
3256d06b26aSDominik Eckelmann    //prepare language array
326dd7a6159SGerrit Uitslag    global $lang, $config_cascade;
32724870174SAndreas Gohr    $lang = [];
3286d06b26aSDominik Eckelmann
3296d06b26aSDominik Eckelmann    //load the language files
3301d82c8d3SChristopher Smith    require(DOKU_INC.'inc/lang/en/lang.php');
331dd7a6159SGerrit Uitslag    foreach ($config_cascade['lang']['core'] as $config_file) {
33279e79377SAndreas Gohr        if (file_exists($config_file . 'en/lang.php')) {
333dd7a6159SGerrit Uitslag            include($config_file . 'en/lang.php');
334dd7a6159SGerrit Uitslag        }
335dd7a6159SGerrit Uitslag    }
336dd7a6159SGerrit Uitslag
3376d06b26aSDominik Eckelmann    if ($langCode && $langCode != 'en') {
3386d06b26aSDominik Eckelmann        if (file_exists(DOKU_INC."inc/lang/$langCode/lang.php")) {
3391d82c8d3SChristopher Smith            require(DOKU_INC."inc/lang/$langCode/lang.php");
3406d06b26aSDominik Eckelmann        }
341dd7a6159SGerrit Uitslag        foreach ($config_cascade['lang']['core'] as $config_file) {
34279e79377SAndreas Gohr            if (file_exists($config_file . "$langCode/lang.php")) {
343dd7a6159SGerrit Uitslag                include($config_file . "$langCode/lang.php");
3446d06b26aSDominik Eckelmann            }
345dd7a6159SGerrit Uitslag        }
3466d06b26aSDominik Eckelmann    }
3476d06b26aSDominik Eckelmann}
3486d06b26aSDominik Eckelmann
34998407a7aSandi/**
3506b9c156cSAnika Henke * Checks the existence of certain files and creates them if missing.
3517367b368SAndreas Gohr */
352d868eb89SAndreas Gohrfunction init_files()
353d868eb89SAndreas Gohr{
3547367b368SAndreas Gohr    global $conf;
3550d8850c4SAndreas Gohr
35624870174SAndreas Gohr    $files = [$conf['indexdir'].'/page.idx'];
3577367b368SAndreas Gohr
3587367b368SAndreas Gohr    foreach ($files as $file) {
35979e79377SAndreas Gohr        if (!file_exists($file)) {
3600d8850c4SAndreas Gohr            $fh = @fopen($file, 'a');
3610d8850c4SAndreas Gohr            if ($fh) {
3627367b368SAndreas Gohr                fclose($fh);
3633aa75874Smovatica                if ($conf['fperm']) chmod($file, $conf['fperm']);
3640d8850c4SAndreas Gohr            } else {
3653816dcbcSAndreas Gohr                nice_die("$file is not writable. Check your permissions settings!");
3660d8850c4SAndreas Gohr            }
3677367b368SAndreas Gohr        }
3687367b368SAndreas Gohr    }
3697367b368SAndreas Gohr}
3707367b368SAndreas Gohr
3717367b368SAndreas Gohr/**
3720d8850c4SAndreas Gohr * Returns absolute path
373f62ea8a1Sandi *
3740d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC.
3757f086b67SAnika Henke * Check for accessibility on directories as well.
3760d8850c4SAndreas Gohr *
3770d8850c4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
378f50a239bSTakamura *
379f50a239bSTakamura * @param string $path
380f50a239bSTakamura *
381f50a239bSTakamura * @return bool|string
382f62ea8a1Sandi */
383d868eb89SAndreas Gohrfunction init_path($path)
384d868eb89SAndreas Gohr{
3856b9c156cSAnika Henke    // check existence
38600976812SAndreas Gohr    $p = fullpath($path);
38779e79377SAndreas Gohr    if (!file_exists($p)) {
38800976812SAndreas Gohr        $p = fullpath(DOKU_INC.$path);
38979e79377SAndreas Gohr        if (!file_exists($p)) {
3908fc4e739Sandi            return '';
391f62ea8a1Sandi        }
3920d8850c4SAndreas Gohr    }
3930d8850c4SAndreas Gohr
3940d8850c4SAndreas Gohr    // check writability
3950d8850c4SAndreas Gohr    if (!@is_writable($p)) {
3960d8850c4SAndreas Gohr        return '';
3970d8850c4SAndreas Gohr    }
3980d8850c4SAndreas Gohr
3990d8850c4SAndreas Gohr    // check accessability (execute bit) for directories
40079e79377SAndreas Gohr    if (@is_dir($p) && !file_exists("$p/.")) {
4010d8850c4SAndreas Gohr        return '';
4020d8850c4SAndreas Gohr    }
4030d8850c4SAndreas Gohr
4040d8850c4SAndreas Gohr    return $p;
4050d8850c4SAndreas Gohr}
4068c4f28e8Sjan
407ed7b5f09Sandi/**
4081ca31cfeSAndreas Gohr * Sets the internal config values fperm and dperm which, when set,
4091ca31cfeSAndreas Gohr * will be used to change the permission of a newly created dir or
4101ca31cfeSAndreas Gohr * file with chmod. Considers the influence of the system's umask
4111ca31cfeSAndreas Gohr * setting the values only if needed.
4121ca31cfeSAndreas Gohr */
413d868eb89SAndreas Gohrfunction init_creationmodes()
414d868eb89SAndreas Gohr{
4151ca31cfeSAndreas Gohr    global $conf;
4161ca31cfeSAndreas Gohr
4171ca31cfeSAndreas Gohr    // Legacy support for old umask/dmask scheme
4181ca31cfeSAndreas Gohr    unset($conf['dmask']);
4191ca31cfeSAndreas Gohr    unset($conf['fmask']);
4201ca31cfeSAndreas Gohr    unset($conf['umask']);
42123420346SDamien Regad
42223420346SDamien Regad    $conf['fperm'] = false;
42323420346SDamien Regad    $conf['dperm'] = false;
4241ca31cfeSAndreas Gohr
4259f3cdec3SAndreas Gohr    // get system umask, fallback to 0 if none available
4269f3cdec3SAndreas Gohr    $umask = @umask();
4279f3cdec3SAndreas Gohr    if (!$umask) $umask = 0000;
4281ca31cfeSAndreas Gohr
4291ca31cfeSAndreas Gohr    // check what is set automatically by the system on file creation
4301ca31cfeSAndreas Gohr    // and set the fperm param if it's not what we want
431bd539124SAndreas Gohr    $auto_fmode = 0666 & ~$umask;
4321ca31cfeSAndreas Gohr    if ($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode'];
4331ca31cfeSAndreas Gohr
434bd539124SAndreas Gohr    // check what is set automatically by the system on directory creation
435bd539124SAndreas Gohr    // and set the dperm param if it's not what we want.
436bd539124SAndreas Gohr    $auto_dmode = 0777 & ~$umask;
4371ca31cfeSAndreas Gohr    if ($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode'];
4381ca31cfeSAndreas Gohr}
4391ca31cfeSAndreas Gohr
4401ca31cfeSAndreas Gohr/**
441ed7b5f09Sandi * Returns the full absolute URL to the directory where
442ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash)
443ed7b5f09Sandi *
444585bf44eSChristopher Smith * !! Can not access $_SERVER values through $INPUT
445585bf44eSChristopher Smith * !! here as this function is called before $INPUT is
446585bf44eSChristopher Smith * !! initialized.
447585bf44eSChristopher Smith *
448ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
449f50a239bSTakamura *
450f50a239bSTakamura * @param null|string $abs
451f50a239bSTakamura *
452f50a239bSTakamura * @return string
453ed7b5f09Sandi */
454d868eb89SAndreas Gohrfunction getBaseURL($abs = null)
455d868eb89SAndreas Gohr{
456ed7b5f09Sandi    global $conf;
457ed7b5f09Sandi    //if canonical url enabled always return absolute
4584b1a4e04SAndreas Gohr    if (is_null($abs)) $abs = $conf['canonical'];
459ed7b5f09Sandi
4601858e4d7SGerry Weißbach    if (!empty($conf['basedir'])) {
46146c73e01SChris Smith        $dir = $conf['basedir'];
46289aa05dbSAndreas Gohr    } elseif (substr($_SERVER['SCRIPT_NAME'], -4) == '.php') {
46346c73e01SChris Smith        $dir = dirname($_SERVER['SCRIPT_NAME']);
46489aa05dbSAndreas Gohr    } elseif (substr($_SERVER['PHP_SELF'], -4) == '.php') {
46546c73e01SChris Smith        $dir = dirname($_SERVER['PHP_SELF']);
466093ec9e4Sandi    } elseif ($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']) {
467dccd6b2bSAndreas Gohr        $dir = preg_replace(
468dccd6b2bSAndreas Gohr            '/^'.preg_quote($_SERVER['DOCUMENT_ROOT'], '/').'/',
469dccd6b2bSAndreas Gohr            '',
470dccd6b2bSAndreas Gohr            $_SERVER['SCRIPT_FILENAME']
471dccd6b2bSAndreas Gohr        );
47246c73e01SChris Smith        $dir = dirname('/'.$dir);
47392b83b77Sandi    } else {
47446c73e01SChris Smith        $dir = '.'; //probably wrong
47592b83b77Sandi    }
476ed7b5f09Sandi
47746c73e01SChris Smith    $dir = str_replace('\\', '/', $dir);             // bugfix for weird WIN behaviour
47846c73e01SChris Smith    $dir = preg_replace('#//+#', '/', "/$dir/");     // ensure leading and trailing slashes
479ed7b5f09Sandi
480f62ea8a1Sandi    //handle script in lib/exe dir
481f62ea8a1Sandi    $dir = preg_replace('!lib/exe/$!', '', $dir);
482f62ea8a1Sandi
483488d5fa0SMichael Klier chi@chimeric.de    //handle script in lib/plugins dir
484488d5fa0SMichael Klier chi@chimeric.de    $dir = preg_replace('!lib/plugins/.*$!', '', $dir);
485488d5fa0SMichael Klier chi@chimeric.de
486ed7b5f09Sandi    //finish here for relative URLs
487ed7b5f09Sandi    if (!$abs) return $dir;
488ed7b5f09Sandi
48964159a61SAndreas Gohr    //use config if available, trim any slash from end of baseurl to avoid multiple consecutive slashes in the path
4901858e4d7SGerry Weißbach    if (!empty($conf['baseurl'])) return rtrim($conf['baseurl'], '/').$dir;
491ef7b3ecdSAndreas Gohr
492e82e3526SAndreas Gohr    //split hostheader into host and port
4935627186cSAndreas Gohr    if (isset($_SERVER['HTTP_HOST'])) {
494204b27c8SMichael Hamann        $parsed_host = parse_url('http://'.$_SERVER['HTTP_HOST']);
49524870174SAndreas Gohr        $host = $parsed_host['host'] ?? null;
49624870174SAndreas Gohr        $port = $parsed_host['port'] ?? null;
4975627186cSAndreas Gohr    } elseif (isset($_SERVER['SERVER_NAME'])) {
498204b27c8SMichael Hamann        $parsed_host = parse_url('http://'.$_SERVER['SERVER_NAME']);
49924870174SAndreas Gohr        $host = $parsed_host['host'] ?? null;
50024870174SAndreas Gohr        $port = $parsed_host['port'] ?? null;
5015627186cSAndreas Gohr    } else {
5025627186cSAndreas Gohr        $host = php_uname('n');
503c66972f2SAdrian Lang        $port = '';
5045627186cSAndreas Gohr    }
5055627186cSAndreas Gohr
506204b27c8SMichael Hamann    if (is_null($port)) {
507204b27c8SMichael Hamann        $port = '';
508204b27c8SMichael Hamann    }
509204b27c8SMichael Hamann
510f5c6743cSAndreas Gohr    if (!is_ssl()) {
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/**
528f5c6743cSAndreas Gohr * Check if accessed via HTTPS
529f5c6743cSAndreas Gohr *
530f5c6743cSAndreas Gohr * Apache leaves ,$_SERVER['HTTPS'] empty when not available, IIS sets it to 'off'.
531f5c6743cSAndreas Gohr * 'false' and 'disabled' are just guessing
532f5c6743cSAndreas Gohr *
533f5c6743cSAndreas Gohr * @returns bool true when SSL is active
534f5c6743cSAndreas Gohr */
535d868eb89SAndreas Gohrfunction is_ssl()
536d868eb89SAndreas Gohr{
53719738e65SEnrico Tagliavini    // check if we are behind a reverse proxy
53819738e65SEnrico Tagliavini    if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
53919738e65SEnrico Tagliavini        if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
54019738e65SEnrico Tagliavini            return true;
54119738e65SEnrico Tagliavini        } else {
54219738e65SEnrico Tagliavini            return false;
54319738e65SEnrico Tagliavini        }
54419738e65SEnrico Tagliavini    }
545c66972f2SAdrian Lang    if (!isset($_SERVER['HTTPS']) ||
546c66972f2SAdrian Lang        preg_match('/^(|off|false|disabled)$/i', $_SERVER['HTTPS'])) {
547f5c6743cSAndreas Gohr        return false;
548f5c6743cSAndreas Gohr    } else {
549f5c6743cSAndreas Gohr        return true;
550f5c6743cSAndreas Gohr    }
551f5c6743cSAndreas Gohr}
552f5c6743cSAndreas Gohr
553f5c6743cSAndreas Gohr/**
55426714386SAndreas Gohr * checks it is windows OS
55526714386SAndreas Gohr * @return bool
55626714386SAndreas Gohr */
557d868eb89SAndreas Gohrfunction isWindows()
558d868eb89SAndreas Gohr{
55924870174SAndreas Gohr    return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
56026714386SAndreas Gohr}
56126714386SAndreas Gohr
56226714386SAndreas Gohr/**
5633816dcbcSAndreas Gohr * print a nice message even if no styles are loaded yet.
564f50a239bSTakamura *
565f50a239bSTakamura * @param integer|string $msg
5663816dcbcSAndreas Gohr */
567d868eb89SAndreas Gohrfunction nice_die($msg)
568d868eb89SAndreas Gohr{
5693816dcbcSAndreas Gohr    echo<<<EOT
570c8839c22SAnika Henke<!DOCTYPE html>
5713816dcbcSAndreas Gohr<html>
5723816dcbcSAndreas Gohr<head><title>DokuWiki Setup Error</title></head>
5733816dcbcSAndreas Gohr<body style="font-family: Arial, sans-serif">
5743816dcbcSAndreas Gohr    <div style="width:60%; margin: auto; background-color: #fcc;
5753816dcbcSAndreas Gohr                border: 1px solid #faa; padding: 0.5em 1em;">
5763816dcbcSAndreas Gohr        <h1 style="font-size: 120%">DokuWiki Setup Error</h1>
5773816dcbcSAndreas Gohr        <p>$msg</p>
5783816dcbcSAndreas Gohr    </div>
5793816dcbcSAndreas Gohr</body>
5803816dcbcSAndreas Gohr</html>
5813816dcbcSAndreas GohrEOT;
5823862da0eSAndreas Gohr    if (defined('DOKU_UNITTEST')) {
5833862da0eSAndreas Gohr        throw new RuntimeException('nice_die: '.$msg);
5843862da0eSAndreas Gohr    }
5850a4266d4SElan Ruusamäe    exit(1);
5863816dcbcSAndreas Gohr}
5873816dcbcSAndreas Gohr
58800976812SAndreas Gohr/**
58900976812SAndreas Gohr * A realpath() replacement
59000976812SAndreas Gohr *
59100976812SAndreas Gohr * This function behaves similar to PHP's realpath() but does not resolve
59200976812SAndreas Gohr * symlinks or accesses upper directories
59300976812SAndreas Gohr *
5944761d30cSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
59500976812SAndreas Gohr * @author <richpageau at yahoo dot co dot uk>
59659752844SAnders Sandblad * @link   http://php.net/manual/en/function.realpath.php#75992
597f50a239bSTakamura *
598f50a239bSTakamura * @param string $path
599f50a239bSTakamura * @param bool $exists
600f50a239bSTakamura *
601f50a239bSTakamura * @return bool|string
60200976812SAndreas Gohr */
603d868eb89SAndreas Gohrfunction fullpath($path, $exists = false)
604d868eb89SAndreas Gohr{
6054761d30cSAndreas Gohr    static $run = 0;
6064761d30cSAndreas Gohr    $root  = '';
607724970e6SAndreas Gohr    $iswin = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' || !empty($GLOBALS['DOKU_UNITTEST_ASSUME_WINDOWS']));
60800976812SAndreas Gohr
6094761d30cSAndreas Gohr    // find the (indestructable) root of the path - keeps windows stuff intact
6102401f18dSSyntaxseed    if ($path[0] == '/') {
6114761d30cSAndreas Gohr        $root = '/';
6124761d30cSAndreas Gohr    } elseif ($iswin) {
6134761d30cSAndreas Gohr        // match drive letter and UNC paths
6144761d30cSAndreas Gohr        if (preg_match('!^([a-zA-z]:)(.*)!', $path, $match)) {
615b9c4302bSAndreas Gohr            $root = $match[1].'/';
6164761d30cSAndreas Gohr            $path = $match[2];
6174761d30cSAndreas Gohr        } elseif (preg_match('!^(\\\\\\\\[^\\\\/]+\\\\[^\\\\/]+[\\\\/])(.*)!', $path, $match)) {
6184761d30cSAndreas Gohr            $root = $match[1];
6194761d30cSAndreas Gohr            $path = $match[2];
62000976812SAndreas Gohr        }
6214761d30cSAndreas Gohr    }
6224761d30cSAndreas Gohr    $path = str_replace('\\', '/', $path);
6234761d30cSAndreas Gohr
6244761d30cSAndreas Gohr    // if the given path wasn't absolute already, prepend the script path and retry
6254761d30cSAndreas Gohr    if (!$root) {
6264761d30cSAndreas Gohr        $base = dirname($_SERVER['SCRIPT_FILENAME']);
6274761d30cSAndreas Gohr        $path = $base.'/'.$path;
6284761d30cSAndreas Gohr        if ($run == 0) { // avoid endless recursion when base isn't absolute for some reason
6294761d30cSAndreas Gohr            $run++;
630b328697dSAndreas Gohr            return fullpath($path, $exists);
6314761d30cSAndreas Gohr        }
6324761d30cSAndreas Gohr    }
6334761d30cSAndreas Gohr    $run = 0;
63400976812SAndreas Gohr
63500976812SAndreas Gohr    // canonicalize
63600976812SAndreas Gohr    $path=explode('/', $path);
63724870174SAndreas Gohr    $newpath=[];
638ef38bfe8SAndreas Gohr    foreach ($path as $p) {
639ef38bfe8SAndreas Gohr        if ($p === '' || $p === '.') continue;
640ef38bfe8SAndreas Gohr        if ($p==='..') {
64100976812SAndreas Gohr            array_pop($newpath);
64200976812SAndreas Gohr            continue;
64300976812SAndreas Gohr        }
64424870174SAndreas Gohr        $newpath[] = $p;
64500976812SAndreas Gohr    }
6464761d30cSAndreas Gohr    $finalpath = $root.implode('/', $newpath);
64700976812SAndreas Gohr
6486b9c156cSAnika Henke    // check for existence when needed (except when unit testing)
64979e79377SAndreas Gohr    if ($exists && !defined('DOKU_UNITTEST') && !file_exists($finalpath)) {
6504761d30cSAndreas Gohr        return false;
65100976812SAndreas Gohr    }
6524761d30cSAndreas Gohr    return $finalpath;
65300976812SAndreas Gohr}
654