xref: /dokuwiki/inc/init.php (revision d868eb89f182718a31113373a6272670bd7f8012)
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 */
18*d868eb89SAndreas Gohrfunction delta_time($start = 0)
19*d868eb89SAndreas 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 */
252*d868eb89SAndreas Gohrfunction init_session()
253*d868eb89SAndreas 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 */
277*d868eb89SAndreas Gohrfunction init_paths()
278*d868eb89SAndreas 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    );
31171726d78SBen Coburn    if ($conf['changelog_old']=='') { unset($conf['changelog_old']); }
31271726d78SBen Coburn    // hardcoded changelog because it is now a cache that lives in meta
31371726d78SBen Coburn    $conf['changelog'] = $conf['metadir'].'/_dokuwiki.changes';
31499c8d7f2Smichael    $conf['media_changelog'] = $conf['metadir'].'/_media.changes';
31598407a7aSandi}
31698407a7aSandi
31738fb1fc7SGerrit Uitslag/**
31838fb1fc7SGerrit Uitslag * Load the language strings
31938fb1fc7SGerrit Uitslag *
32038fb1fc7SGerrit Uitslag * @param string $langCode language code, as passed by event handler
32138fb1fc7SGerrit Uitslag */
322*d868eb89SAndreas Gohrfunction init_lang($langCode)
323*d868eb89SAndreas Gohr{
3246d06b26aSDominik Eckelmann    //prepare language array
325dd7a6159SGerrit Uitslag    global $lang, $config_cascade;
32624870174SAndreas Gohr    $lang = [];
3276d06b26aSDominik Eckelmann
3286d06b26aSDominik Eckelmann    //load the language files
3291d82c8d3SChristopher Smith    require(DOKU_INC.'inc/lang/en/lang.php');
330dd7a6159SGerrit Uitslag    foreach ($config_cascade['lang']['core'] as $config_file) {
33179e79377SAndreas Gohr        if (file_exists($config_file . 'en/lang.php')) {
332dd7a6159SGerrit Uitslag            include($config_file . 'en/lang.php');
333dd7a6159SGerrit Uitslag        }
334dd7a6159SGerrit Uitslag    }
335dd7a6159SGerrit Uitslag
3366d06b26aSDominik Eckelmann    if ($langCode && $langCode != 'en') {
3376d06b26aSDominik Eckelmann        if (file_exists(DOKU_INC."inc/lang/$langCode/lang.php")) {
3381d82c8d3SChristopher Smith            require(DOKU_INC."inc/lang/$langCode/lang.php");
3396d06b26aSDominik Eckelmann        }
340dd7a6159SGerrit Uitslag        foreach ($config_cascade['lang']['core'] as $config_file) {
34179e79377SAndreas Gohr            if (file_exists($config_file . "$langCode/lang.php")) {
342dd7a6159SGerrit Uitslag                include($config_file . "$langCode/lang.php");
3436d06b26aSDominik Eckelmann            }
344dd7a6159SGerrit Uitslag        }
3456d06b26aSDominik Eckelmann    }
3466d06b26aSDominik Eckelmann}
3476d06b26aSDominik Eckelmann
34898407a7aSandi/**
3496b9c156cSAnika Henke * Checks the existence of certain files and creates them if missing.
3507367b368SAndreas Gohr */
351*d868eb89SAndreas Gohrfunction init_files()
352*d868eb89SAndreas Gohr{
3537367b368SAndreas Gohr    global $conf;
3540d8850c4SAndreas Gohr
35524870174SAndreas Gohr    $files = [$conf['indexdir'].'/page.idx'];
3567367b368SAndreas Gohr
3577367b368SAndreas Gohr    foreach($files as $file){
35879e79377SAndreas Gohr        if(!file_exists($file)){
3590d8850c4SAndreas Gohr            $fh = @fopen($file, 'a');
3600d8850c4SAndreas Gohr            if($fh){
3617367b368SAndreas Gohr                fclose($fh);
3623aa75874Smovatica                if($conf['fperm']) chmod($file, $conf['fperm']);
3630d8850c4SAndreas Gohr            }else{
3643816dcbcSAndreas Gohr                nice_die("$file is not writable. Check your permissions settings!");
3650d8850c4SAndreas Gohr            }
3667367b368SAndreas Gohr        }
3677367b368SAndreas Gohr    }
3687367b368SAndreas Gohr}
3697367b368SAndreas Gohr
3707367b368SAndreas Gohr/**
3710d8850c4SAndreas Gohr * Returns absolute path
372f62ea8a1Sandi *
3730d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC.
3747f086b67SAnika Henke * Check for accessibility on directories as well.
3750d8850c4SAndreas Gohr *
3760d8850c4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
377f50a239bSTakamura *
378f50a239bSTakamura * @param string $path
379f50a239bSTakamura *
380f50a239bSTakamura * @return bool|string
381f62ea8a1Sandi */
382*d868eb89SAndreas Gohrfunction init_path($path)
383*d868eb89SAndreas Gohr{
3846b9c156cSAnika Henke    // check existence
38500976812SAndreas Gohr    $p = fullpath($path);
38679e79377SAndreas Gohr    if(!file_exists($p)){
38700976812SAndreas Gohr        $p = fullpath(DOKU_INC.$path);
38879e79377SAndreas Gohr        if(!file_exists($p)){
3898fc4e739Sandi            return '';
390f62ea8a1Sandi        }
3910d8850c4SAndreas Gohr    }
3920d8850c4SAndreas Gohr
3930d8850c4SAndreas Gohr    // check writability
3940d8850c4SAndreas Gohr    if(!@is_writable($p)){
3950d8850c4SAndreas Gohr        return '';
3960d8850c4SAndreas Gohr    }
3970d8850c4SAndreas Gohr
3980d8850c4SAndreas Gohr    // check accessability (execute bit) for directories
39979e79377SAndreas Gohr    if(@is_dir($p) && !file_exists("$p/.")){
4000d8850c4SAndreas Gohr        return '';
4010d8850c4SAndreas Gohr    }
4020d8850c4SAndreas Gohr
4030d8850c4SAndreas Gohr    return $p;
4040d8850c4SAndreas Gohr}
4058c4f28e8Sjan
406ed7b5f09Sandi/**
4071ca31cfeSAndreas Gohr * Sets the internal config values fperm and dperm which, when set,
4081ca31cfeSAndreas Gohr * will be used to change the permission of a newly created dir or
4091ca31cfeSAndreas Gohr * file with chmod. Considers the influence of the system's umask
4101ca31cfeSAndreas Gohr * setting the values only if needed.
4111ca31cfeSAndreas Gohr */
412*d868eb89SAndreas Gohrfunction init_creationmodes()
413*d868eb89SAndreas Gohr{
4141ca31cfeSAndreas Gohr    global $conf;
4151ca31cfeSAndreas Gohr
4161ca31cfeSAndreas Gohr    // Legacy support for old umask/dmask scheme
4171ca31cfeSAndreas Gohr    unset($conf['dmask']);
4181ca31cfeSAndreas Gohr    unset($conf['fmask']);
4191ca31cfeSAndreas Gohr    unset($conf['umask']);
42023420346SDamien Regad
42123420346SDamien Regad    $conf['fperm'] = false;
42223420346SDamien Regad    $conf['dperm'] = false;
4231ca31cfeSAndreas Gohr
4249f3cdec3SAndreas Gohr    // get system umask, fallback to 0 if none available
4259f3cdec3SAndreas Gohr    $umask = @umask();
4269f3cdec3SAndreas Gohr    if(!$umask) $umask = 0000;
4271ca31cfeSAndreas Gohr
4281ca31cfeSAndreas Gohr    // check what is set automatically by the system on file creation
4291ca31cfeSAndreas Gohr    // and set the fperm param if it's not what we want
430bd539124SAndreas Gohr    $auto_fmode = 0666 & ~$umask;
4311ca31cfeSAndreas Gohr    if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode'];
4321ca31cfeSAndreas Gohr
433bd539124SAndreas Gohr    // check what is set automatically by the system on directory creation
434bd539124SAndreas Gohr    // and set the dperm param if it's not what we want.
435bd539124SAndreas Gohr    $auto_dmode = 0777 & ~$umask;
4361ca31cfeSAndreas Gohr    if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode'];
4371ca31cfeSAndreas Gohr}
4381ca31cfeSAndreas Gohr
4391ca31cfeSAndreas Gohr/**
440ed7b5f09Sandi * Returns the full absolute URL to the directory where
441ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash)
442ed7b5f09Sandi *
443585bf44eSChristopher Smith * !! Can not access $_SERVER values through $INPUT
444585bf44eSChristopher Smith * !! here as this function is called before $INPUT is
445585bf44eSChristopher Smith * !! initialized.
446585bf44eSChristopher Smith *
447ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
448f50a239bSTakamura *
449f50a239bSTakamura * @param null|string $abs
450f50a239bSTakamura *
451f50a239bSTakamura * @return string
452ed7b5f09Sandi */
453*d868eb89SAndreas Gohrfunction getBaseURL($abs = null)
454*d868eb89SAndreas Gohr{
455ed7b5f09Sandi    global $conf;
456ed7b5f09Sandi    //if canonical url enabled always return absolute
4574b1a4e04SAndreas Gohr    if(is_null($abs)) $abs = $conf['canonical'];
458ed7b5f09Sandi
4591858e4d7SGerry Weißbach    if(!empty($conf['basedir'])){
46046c73e01SChris Smith        $dir = $conf['basedir'];
46189aa05dbSAndreas Gohr    }elseif(substr($_SERVER['SCRIPT_NAME'], -4) == '.php'){
46246c73e01SChris Smith        $dir = dirname($_SERVER['SCRIPT_NAME']);
46389aa05dbSAndreas Gohr    }elseif(substr($_SERVER['PHP_SELF'], -4) == '.php'){
46446c73e01SChris Smith        $dir = dirname($_SERVER['PHP_SELF']);
465093ec9e4Sandi    }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
466093ec9e4Sandi        $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'], '/').'/', '',
467093ec9e4Sandi                $_SERVER['SCRIPT_FILENAME']);
46846c73e01SChris Smith        $dir = dirname('/'.$dir);
46992b83b77Sandi    }else{
47046c73e01SChris Smith        $dir = '.'; //probably wrong
47192b83b77Sandi    }
472ed7b5f09Sandi
47346c73e01SChris Smith    $dir = str_replace('\\', '/', $dir);             // bugfix for weird WIN behaviour
47446c73e01SChris Smith    $dir = preg_replace('#//+#', '/', "/$dir/");     // ensure leading and trailing slashes
475ed7b5f09Sandi
476f62ea8a1Sandi    //handle script in lib/exe dir
477f62ea8a1Sandi    $dir = preg_replace('!lib/exe/$!', '', $dir);
478f62ea8a1Sandi
479488d5fa0SMichael Klier chi@chimeric.de    //handle script in lib/plugins dir
480488d5fa0SMichael Klier chi@chimeric.de    $dir = preg_replace('!lib/plugins/.*$!', '', $dir);
481488d5fa0SMichael Klier chi@chimeric.de
482ed7b5f09Sandi    //finish here for relative URLs
483ed7b5f09Sandi    if(!$abs) return $dir;
484ed7b5f09Sandi
48564159a61SAndreas Gohr    //use config if available, trim any slash from end of baseurl to avoid multiple consecutive slashes in the path
4861858e4d7SGerry Weißbach    if(!empty($conf['baseurl'])) return rtrim($conf['baseurl'], '/').$dir;
487ef7b3ecdSAndreas Gohr
488e82e3526SAndreas Gohr    //split hostheader into host and port
4895627186cSAndreas Gohr    if(isset($_SERVER['HTTP_HOST'])){
490204b27c8SMichael Hamann        $parsed_host = parse_url('http://'.$_SERVER['HTTP_HOST']);
49124870174SAndreas Gohr        $host = $parsed_host['host'] ?? null;
49224870174SAndreas Gohr        $port = $parsed_host['port'] ?? null;
4935627186cSAndreas Gohr    }elseif(isset($_SERVER['SERVER_NAME'])){
494204b27c8SMichael Hamann        $parsed_host = parse_url('http://'.$_SERVER['SERVER_NAME']);
49524870174SAndreas Gohr        $host = $parsed_host['host'] ?? null;
49624870174SAndreas Gohr        $port = $parsed_host['port'] ?? null;
4975627186cSAndreas Gohr    }else{
4985627186cSAndreas Gohr        $host = php_uname('n');
499c66972f2SAdrian Lang        $port = '';
5005627186cSAndreas Gohr    }
5015627186cSAndreas Gohr
502204b27c8SMichael Hamann    if(is_null($port)){
503204b27c8SMichael Hamann        $port = '';
504204b27c8SMichael Hamann    }
505204b27c8SMichael Hamann
506f5c6743cSAndreas Gohr    if(!is_ssl()){
507ed7b5f09Sandi        $proto = 'http://';
508e82e3526SAndreas Gohr        if ($port == '80') {
509ed7b5f09Sandi            $port = '';
510ed7b5f09Sandi        }
511ed7b5f09Sandi    }else{
512ed7b5f09Sandi        $proto = 'https://';
513e82e3526SAndreas Gohr        if ($port == '443') {
514ed7b5f09Sandi            $port = '';
515ed7b5f09Sandi        }
516ed7b5f09Sandi    }
517ed7b5f09Sandi
518c66972f2SAdrian Lang    if($port !== '') $port = ':'.$port;
519e82e3526SAndreas Gohr
520ed7b5f09Sandi    return $proto.$host.$port.$dir;
521ed7b5f09Sandi}
522ed7b5f09Sandi
523b000c6d4Sandi/**
524f5c6743cSAndreas Gohr * Check if accessed via HTTPS
525f5c6743cSAndreas Gohr *
526f5c6743cSAndreas Gohr * Apache leaves ,$_SERVER['HTTPS'] empty when not available, IIS sets it to 'off'.
527f5c6743cSAndreas Gohr * 'false' and 'disabled' are just guessing
528f5c6743cSAndreas Gohr *
529f5c6743cSAndreas Gohr * @returns bool true when SSL is active
530f5c6743cSAndreas Gohr */
531*d868eb89SAndreas Gohrfunction is_ssl()
532*d868eb89SAndreas Gohr{
53319738e65SEnrico Tagliavini    // check if we are behind a reverse proxy
53419738e65SEnrico Tagliavini    if(isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
53519738e65SEnrico Tagliavini        if($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
53619738e65SEnrico Tagliavini            return true;
53719738e65SEnrico Tagliavini        } else {
53819738e65SEnrico Tagliavini            return false;
53919738e65SEnrico Tagliavini        }
54019738e65SEnrico Tagliavini    }
541c66972f2SAdrian Lang    if(!isset($_SERVER['HTTPS']) ||
542c66972f2SAdrian Lang        preg_match('/^(|off|false|disabled)$/i', $_SERVER['HTTPS'])) {
543f5c6743cSAndreas Gohr        return false;
544f5c6743cSAndreas Gohr    } else {
545f5c6743cSAndreas Gohr        return true;
546f5c6743cSAndreas Gohr    }
547f5c6743cSAndreas Gohr}
548f5c6743cSAndreas Gohr
549f5c6743cSAndreas Gohr/**
55026714386SAndreas Gohr * checks it is windows OS
55126714386SAndreas Gohr * @return bool
55226714386SAndreas Gohr */
553*d868eb89SAndreas Gohrfunction isWindows()
554*d868eb89SAndreas Gohr{
55524870174SAndreas Gohr    return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
55626714386SAndreas Gohr}
55726714386SAndreas Gohr
55826714386SAndreas Gohr/**
5593816dcbcSAndreas Gohr * print a nice message even if no styles are loaded yet.
560f50a239bSTakamura *
561f50a239bSTakamura * @param integer|string $msg
5623816dcbcSAndreas Gohr */
563*d868eb89SAndreas Gohrfunction nice_die($msg)
564*d868eb89SAndreas Gohr{
5653816dcbcSAndreas Gohr    echo<<<EOT
566c8839c22SAnika Henke<!DOCTYPE html>
5673816dcbcSAndreas Gohr<html>
5683816dcbcSAndreas Gohr<head><title>DokuWiki Setup Error</title></head>
5693816dcbcSAndreas Gohr<body style="font-family: Arial, sans-serif">
5703816dcbcSAndreas Gohr    <div style="width:60%; margin: auto; background-color: #fcc;
5713816dcbcSAndreas Gohr                border: 1px solid #faa; padding: 0.5em 1em;">
5723816dcbcSAndreas Gohr        <h1 style="font-size: 120%">DokuWiki Setup Error</h1>
5733816dcbcSAndreas Gohr        <p>$msg</p>
5743816dcbcSAndreas Gohr    </div>
5753816dcbcSAndreas Gohr</body>
5763816dcbcSAndreas Gohr</html>
5773816dcbcSAndreas GohrEOT;
5783862da0eSAndreas Gohr    if(defined('DOKU_UNITTEST')) {
5793862da0eSAndreas Gohr        throw new RuntimeException('nice_die: '.$msg);
5803862da0eSAndreas Gohr    }
5810a4266d4SElan Ruusamäe    exit(1);
5823816dcbcSAndreas Gohr}
5833816dcbcSAndreas Gohr
58400976812SAndreas Gohr/**
58500976812SAndreas Gohr * A realpath() replacement
58600976812SAndreas Gohr *
58700976812SAndreas Gohr * This function behaves similar to PHP's realpath() but does not resolve
58800976812SAndreas Gohr * symlinks or accesses upper directories
58900976812SAndreas Gohr *
5904761d30cSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
59100976812SAndreas Gohr * @author <richpageau at yahoo dot co dot uk>
59259752844SAnders Sandblad * @link   http://php.net/manual/en/function.realpath.php#75992
593f50a239bSTakamura *
594f50a239bSTakamura * @param string $path
595f50a239bSTakamura * @param bool $exists
596f50a239bSTakamura *
597f50a239bSTakamura * @return bool|string
59800976812SAndreas Gohr */
599*d868eb89SAndreas Gohrfunction fullpath($path, $exists = false)
600*d868eb89SAndreas Gohr{
6014761d30cSAndreas Gohr    static $run = 0;
6024761d30cSAndreas Gohr    $root  = '';
603724970e6SAndreas Gohr    $iswin = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' || !empty($GLOBALS['DOKU_UNITTEST_ASSUME_WINDOWS']));
60400976812SAndreas Gohr
6054761d30cSAndreas Gohr    // find the (indestructable) root of the path - keeps windows stuff intact
6062401f18dSSyntaxseed    if($path[0] == '/'){
6074761d30cSAndreas Gohr        $root = '/';
6084761d30cSAndreas Gohr    }elseif($iswin){
6094761d30cSAndreas Gohr        // match drive letter and UNC paths
6104761d30cSAndreas Gohr        if (preg_match('!^([a-zA-z]:)(.*)!', $path, $match)) {
611b9c4302bSAndreas Gohr            $root = $match[1].'/';
6124761d30cSAndreas Gohr            $path = $match[2];
6134761d30cSAndreas Gohr        } elseif (preg_match('!^(\\\\\\\\[^\\\\/]+\\\\[^\\\\/]+[\\\\/])(.*)!', $path, $match)) {
6144761d30cSAndreas Gohr            $root = $match[1];
6154761d30cSAndreas Gohr            $path = $match[2];
61600976812SAndreas Gohr        }
6174761d30cSAndreas Gohr    }
6184761d30cSAndreas Gohr    $path = str_replace('\\', '/', $path);
6194761d30cSAndreas Gohr
6204761d30cSAndreas Gohr    // if the given path wasn't absolute already, prepend the script path and retry
6214761d30cSAndreas Gohr    if(!$root){
6224761d30cSAndreas Gohr        $base = dirname($_SERVER['SCRIPT_FILENAME']);
6234761d30cSAndreas Gohr        $path = $base.'/'.$path;
6244761d30cSAndreas Gohr        if($run == 0){ // avoid endless recursion when base isn't absolute for some reason
6254761d30cSAndreas Gohr            $run++;
626b328697dSAndreas Gohr            return fullpath($path, $exists);
6274761d30cSAndreas Gohr        }
6284761d30cSAndreas Gohr    }
6294761d30cSAndreas Gohr    $run = 0;
63000976812SAndreas Gohr
63100976812SAndreas Gohr    // canonicalize
63200976812SAndreas Gohr    $path=explode('/', $path);
63324870174SAndreas Gohr    $newpath=[];
634ef38bfe8SAndreas Gohr    foreach($path as $p) {
635ef38bfe8SAndreas Gohr        if ($p === '' || $p === '.') continue;
636ef38bfe8SAndreas Gohr        if ($p==='..') {
63700976812SAndreas Gohr            array_pop($newpath);
63800976812SAndreas Gohr            continue;
63900976812SAndreas Gohr        }
64024870174SAndreas Gohr        $newpath[] = $p;
64100976812SAndreas Gohr    }
6424761d30cSAndreas Gohr    $finalpath = $root.implode('/', $newpath);
64300976812SAndreas Gohr
6446b9c156cSAnika Henke    // check for existence when needed (except when unit testing)
64579e79377SAndreas Gohr    if($exists && !defined('DOKU_UNITTEST') && !file_exists($finalpath)) {
6464761d30cSAndreas Gohr        return false;
64700976812SAndreas Gohr    }
6484761d30cSAndreas Gohr    return $finalpath;
64900976812SAndreas Gohr}
65000976812SAndreas Gohr
651