xref: /dokuwiki/inc/init.php (revision 3862da0e1ff79062b5fe615568f47957b40d0e99)
1ed7b5f09Sandi<?php
2ed7b5f09Sandi/**
3ed7b5f09Sandi * Initialize some defaults needed for DokuWiki
4ed7b5f09Sandi */
5ed7b5f09Sandi
630085ef3SYurii K
73272d797SAndreas Gohr/**
83272d797SAndreas Gohr * timing Dokuwiki execution
9f50a239bSTakamura *
10f50a239bSTakamura * @param integer $start
11f50a239bSTakamura *
12f50a239bSTakamura * @return mixed
133272d797SAndreas Gohr */
14a609a9ccSBen Coburnfunction delta_time($start=0) {
15ac4be4d7SPiyush Mishra    return microtime(true)-((float)$start);
16a609a9ccSBen Coburn}
17a609a9ccSBen Coburndefine('DOKU_START_TIME', delta_time());
18a609a9ccSBen Coburn
19ccaeaa85SAndreas Gohrglobal $config_cascade;
20cca94fbcSRoland Hager$config_cascade = array();
21ccaeaa85SAndreas Gohr
2248beefecSAndreas Gohr// if available load a preload config file
23e6266454SChris Smith$preload = fullpath(dirname(__FILE__)).'/preload.php';
2479e79377SAndreas Gohrif (file_exists($preload)) include($preload);
2548beefecSAndreas Gohr
26ed7b5f09Sandi// define the include path
2700976812SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../').'/');
28ad15db82Sandi
29c2a6d816SAndreas Gohr// define Plugin dir
30c2a6d816SAndreas Gohrif(!defined('DOKU_PLUGIN'))  define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
31c2a6d816SAndreas Gohr
32e7cb32dcSAndreas Gohr// define config path (packagers may want to change this to /etc/dokuwiki/)
33b7551a6dSEsther Brunnerif(!defined('DOKU_CONF')) define('DOKU_CONF',DOKU_INC.'conf/');
34e7cb32dcSAndreas Gohr
35bad905f1SBen Coburn// check for error reporting override or set error reporting to sane values
3679e79377SAndreas Gohrif (!defined('DOKU_E_LEVEL') && file_exists(DOKU_CONF.'report_e_all')) {
37bad905f1SBen Coburn    define('DOKU_E_LEVEL', E_ALL);
38bad905f1SBen Coburn}
39fc80ed59SAndreas Gohrif (!defined('DOKU_E_LEVEL')) {
404fcd684aSMichael Hamann    error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT);
41fc80ed59SAndreas Gohr} else {
42fc80ed59SAndreas Gohr    error_reporting(DOKU_E_LEVEL);
43fc80ed59SAndreas Gohr}
44c53ea5f2Sandi
45a69722b3SAndreas Gohr// avoid caching issues #1594
46a69722b3SAndreas Gohrheader('Vary: Cookie');
47a69722b3SAndreas Gohr
4850602150SBen Coburn// init memory caches
49db959ae3SAndreas Gohrglobal $cache_revinfo;
50db959ae3SAndreas Gohr       $cache_revinfo = array();
51db959ae3SAndreas Gohrglobal $cache_wikifn;
52db959ae3SAndreas Gohr       $cache_wikifn = array();
53db959ae3SAndreas Gohrglobal $cache_cleanid;
54db959ae3SAndreas Gohr       $cache_cleanid = array();
55db959ae3SAndreas Gohrglobal $cache_authname;
56db959ae3SAndreas Gohr       $cache_authname = array();
57db959ae3SAndreas Gohrglobal $cache_metadata;
58db959ae3SAndreas Gohr       $cache_metadata = array();
5950602150SBen Coburn
60cca94fbcSRoland Hager// always include 'inc/config_cascade.php'
61cca94fbcSRoland Hager// previously in preload.php set fields of $config_cascade will be merged with the defaults
62e6a6dbfeSAndreas Gohrinclude(DOKU_INC.'inc/config_cascade.php');
63cb043f52SChris Smith
644724a577Sandi//prepare config array()
65ee20e7d1Sandiglobal $conf;
664724a577Sandi$conf = array();
674724a577Sandi
68cb043f52SChris Smith// load the global config file(s)
69b303b92cSChris Smithforeach (array('default','local','protected') as $config_group) {
70f8121585SChris Smith    if (empty($config_cascade['main'][$config_group])) continue;
71b303b92cSChris Smith    foreach ($config_cascade['main'][$config_group] as $config_file) {
7279e79377SAndreas Gohr        if (file_exists($config_file)) {
73f8121585SChris Smith            include($config_file);
74f8121585SChris Smith        }
75cb043f52SChris Smith    }
760a6ead41SAndreas Gohr}
77ad15db82Sandi
78066fee30SAndreas Gohr//prepare license array()
79066fee30SAndreas Gohrglobal $license;
80066fee30SAndreas Gohr$license = array();
81066fee30SAndreas Gohr
82066fee30SAndreas Gohr// load the license file(s)
83f8121585SChris Smithforeach (array('default','local') as $config_group) {
84f8121585SChris Smith    if (empty($config_cascade['license'][$config_group])) continue;
85f8121585SChris Smith    foreach ($config_cascade['license'][$config_group] as $config_file) {
8679e79377SAndreas Gohr        if(file_exists($config_file)){
87f8121585SChris Smith            include($config_file);
88f8121585SChris Smith        }
89f8121585SChris Smith    }
90066fee30SAndreas Gohr}
91066fee30SAndreas Gohr
921f8eb24fSAndreas Gohr// set timezone (as in pre 5.3.0 days)
931f8eb24fSAndreas Gohrdate_default_timezone_set(@date_default_timezone_get());
941f8eb24fSAndreas Gohr
95ed7b5f09Sandi// define baseURL
964b1a4e04SAndreas Gohrif(!defined('DOKU_REL')) define('DOKU_REL',getBaseURL(false));
97ed7b5f09Sandiif(!defined('DOKU_URL')) define('DOKU_URL',getBaseURL(true));
984b1a4e04SAndreas Gohrif(!defined('DOKU_BASE')){
994b1a4e04SAndreas Gohr    if($conf['canonical']){
1004b1a4e04SAndreas Gohr        define('DOKU_BASE',DOKU_URL);
1014b1a4e04SAndreas Gohr    }else{
1024b1a4e04SAndreas Gohr        define('DOKU_BASE',DOKU_REL);
1034b1a4e04SAndreas Gohr    }
1044b1a4e04SAndreas Gohr}
1054b1a4e04SAndreas Gohr
106b8595a66SAndreas Gohr// define whitespace
107b8595a66SAndreas Gohrif(!defined('DOKU_LF')) define ('DOKU_LF',"\n");
108b8595a66SAndreas Gohrif(!defined('DOKU_TAB')) define ('DOKU_TAB',"\t");
109ed7b5f09Sandi
110656c8fb3SAndreas Gohr// define cookie and session id, append server port when securecookie is configured FS#1664
111656c8fb3SAndreas Gohrif (!defined('DOKU_COOKIE')) define('DOKU_COOKIE', 'DW'.md5(DOKU_REL.(($conf['securecookie'])?$_SERVER['SERVER_PORT']:'')));
112e71ce681SAndreas Gohr
113ee20e7d1Sandi
114ed7b5f09Sandi// define main script
115ed7b5f09Sandiif(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php');
116ed7b5f09Sandi
117c4766956SAndreas Gohr// DEPRECATED, use tpl_basedir() instead
1186b13307fSandiif(!defined('DOKU_TPL')) define('DOKU_TPL',
119f62ea8a1Sandi        DOKU_BASE.'lib/tpl/'.$conf['template'].'/');
1206b13307fSandi
121c4766956SAndreas Gohr// DEPRECATED, use tpl_incdir() instead
12278a6aeb1SAndreas Gohrif(!defined('DOKU_TPLINC')) define('DOKU_TPLINC',
12378a6aeb1SAndreas Gohr        DOKU_INC.'lib/tpl/'.$conf['template'].'/');
12478a6aeb1SAndreas Gohr
125ed7b5f09Sandi// make session rewrites XHTML compliant
1263fc74836Sandi@ini_set('arg_separator.output', '&amp;');
127ed7b5f09Sandi
128d7e6bba9SAndreas Gohr// make sure global zlib does not interfere FS#1132
129d7e6bba9SAndreas Gohr@ini_set('zlib.output_compression', 'off');
130d7e6bba9SAndreas Gohr
1316deb5405SAndreas Gohr// increase PCRE backtrack limit
1326deb5405SAndreas Gohr@ini_set('pcre.backtrack_limit', '20971520');
1336deb5405SAndreas Gohr
13498bda4fdSAndreas Gohr// enable gzip compression if supported
13598bda4fdSAndreas Gohr$conf['gzip_output'] &= (strpos($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip') !== false);
13665f6e7d6SMichael Hamannglobal $ACT;
1373138b5c7SAndreas Gohrif ($conf['gzip_output'] &&
1383138b5c7SAndreas Gohr        !defined('DOKU_DISABLE_GZIP_OUTPUT') &&
13965f6e7d6SMichael Hamann        function_exists('ob_gzhandler') &&
14099e10b7fSMichael Hamann        // Disable compression when a (compressed) sitemap might be delivered
14165f6e7d6SMichael Hamann        // See https://bugs.dokuwiki.org/index.php?do=details&task_id=2576
14299e10b7fSMichael Hamann        $ACT != 'sitemap') {
1433138b5c7SAndreas Gohr    ob_start('ob_gzhandler');
1443138b5c7SAndreas Gohr}
1453138b5c7SAndreas Gohr
146ed7b5f09Sandi// init session
1476534245aSAndreas Gohrif(!headers_sent() && !defined('NOSESSION')) {
148c09f0eb1SGerrit Uitslag    if(!defined('DOKU_SESSION_NAME'))     define ('DOKU_SESSION_NAME', "DokuWiki");
149c09f0eb1SGerrit Uitslag    if(!defined('DOKU_SESSION_LIFETIME')) define ('DOKU_SESSION_LIFETIME', 0);
15055a71a16SGerrit Uitslag    if(!defined('DOKU_SESSION_PATH')) {
15173ab87deSGabriel Birke        $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir'];
15255a71a16SGerrit Uitslag        define ('DOKU_SESSION_PATH', $cookieDir);
153f5c6743cSAndreas Gohr    }
154c09f0eb1SGerrit Uitslag    if(!defined('DOKU_SESSION_DOMAIN'))   define ('DOKU_SESSION_DOMAIN', '');
155c09f0eb1SGerrit Uitslag
1566eb3cdf6SAndreas Gohr    // start the session
1576eb3cdf6SAndreas Gohr    init_session();
15814a122deSAndreas Gohr
15914a122deSAndreas Gohr    // load left over messages
16014a122deSAndreas Gohr    if(isset($_SESSION[DOKU_COOKIE]['msg'])) {
16114a122deSAndreas Gohr        $MSG = $_SESSION[DOKU_COOKIE]['msg'];
16214a122deSAndreas Gohr        unset($_SESSION[DOKU_COOKIE]['msg']);
16314a122deSAndreas Gohr    }
164bad31ae9SAndreas Gohr}
165ed7b5f09Sandi
166a1637ffdSAndreas Gohr// don't let cookies ever interfere with request vars
167a1637ffdSAndreas Gohr$_REQUEST = array_merge($_GET,$_POST);
168a1637ffdSAndreas Gohr
1693dea4ebcSAndreas Gohr// we don't want a purge URL to be digged
1700e80bb5eSChristopher Smithif(isset($_REQUEST['purge']) && !empty($_SERVER['HTTP_REFERER'])) unset($_REQUEST['purge']);
1713dea4ebcSAndreas Gohr
1721ca31cfeSAndreas Gohr// precalculate file creation modes
1731ca31cfeSAndreas Gohrinit_creationmodes();
174ed7b5f09Sandi
1753dc3a5f1Sandi// make real paths and check them
17698407a7aSandiinit_paths();
1777367b368SAndreas Gohrinit_files();
178ed7b5f09Sandi
179f1986589SMichael Klier// setup plugin controller class (can be overwritten in preload.php)
18093a7873eSAndreas Gohr$plugin_types = array('auth', 'admin','syntax','action','renderer', 'helper','remote');
181f1986589SMichael Klierglobal $plugin_controller_class, $plugin_controller;
182f1986589SMichael Klierif (empty($plugin_controller_class)) $plugin_controller_class = 'Doku_Plugin_Controller';
183f1986589SMichael Klier
184c7cb395cSAdrian Lang// load libraries
185605f8e8dSAndreas Gohrrequire_once(DOKU_INC.'vendor/autoload.php');
186c7cb395cSAdrian Langrequire_once(DOKU_INC.'inc/load.php');
187c7cb395cSAdrian Lang
1880f8f7aaaSDanny Lin// disable gzip if not available
18913c37900SAndreas Gohrdefine('DOKU_HAS_BZIP', function_exists('bzopen'));
19013c37900SAndreas Gohrdefine('DOKU_HAS_GZIP', function_exists('gzopen'));
19113c37900SAndreas Gohrif($conf['compression'] == 'bz2' && !DOKU_HAS_BZIP) {
1920f8f7aaaSDanny Lin    $conf['compression'] = 'gz';
1930f8f7aaaSDanny Lin}
19413c37900SAndreas Gohrif($conf['compression'] == 'gz' && !DOKU_HAS_GZIP) {
1950f8f7aaaSDanny Lin    $conf['compression'] = 0;
1960f8f7aaaSDanny Lin}
1970f8f7aaaSDanny Lin
19889177306SAndreas Gohr// input handle class
19989177306SAndreas Gohrglobal $INPUT;
20089177306SAndreas Gohr$INPUT = new Input();
20189177306SAndreas Gohr
202f1986589SMichael Klier// initialize plugin controller
203f1986589SMichael Klier$plugin_controller = new $plugin_controller_class();
204f1986589SMichael Klier
205f1986589SMichael Klier// initialize the event handler
206f1986589SMichael Klierglobal $EVENT_HANDLER;
207f1986589SMichael Klier$EVENT_HANDLER = new Doku_Event_Handler();
208f1986589SMichael Klier
2096d06b26aSDominik Eckelmann$local = $conf['lang'];
2106d06b26aSDominik Eckelmanntrigger_event('INIT_LANG_LOAD', $local, 'init_lang', true);
2116d06b26aSDominik Eckelmann
2126d06b26aSDominik Eckelmann
21316905344SAndreas Gohr// setup authentication system
214c7cb395cSAdrian Langif (!defined('NOSESSION')) {
21516905344SAndreas Gohr    auth_setup();
216c7cb395cSAdrian Lang}
217f62ea8a1Sandi
2185ec3fefcSAndreas Gohr// setup mail system
2195ec3fefcSAndreas Gohrmail_setup();
2205ec3fefcSAndreas Gohr
221f62ea8a1Sandi/**
2226eb3cdf6SAndreas Gohr * Initializes the session
2236eb3cdf6SAndreas Gohr *
2246eb3cdf6SAndreas Gohr * Makes sure the passed session cookie is valid, invalid ones are ignored an a new session ID is issued
2256eb3cdf6SAndreas Gohr *
2266eb3cdf6SAndreas Gohr * @link http://stackoverflow.com/a/33024310/172068
227924e477eSAndreas Gohr * @link http://php.net/manual/en/session.configuration.php#ini.session.sid-length
2286eb3cdf6SAndreas Gohr */
2296eb3cdf6SAndreas Gohrfunction init_session() {
2306eb3cdf6SAndreas Gohr    global $conf;
2316eb3cdf6SAndreas Gohr    session_name(DOKU_SESSION_NAME);
2326eb3cdf6SAndreas Gohr    session_set_cookie_params(DOKU_SESSION_LIFETIME, DOKU_SESSION_PATH, DOKU_SESSION_DOMAIN, ($conf['securecookie'] && is_ssl()), true);
2336eb3cdf6SAndreas Gohr
2346eb3cdf6SAndreas Gohr    // make sure the session cookie contains a valid session ID
235924e477eSAndreas Gohr    if(isset($_COOKIE[DOKU_SESSION_NAME]) && !preg_match('/^[-,a-zA-Z0-9]{22,256}$/', $_COOKIE[DOKU_SESSION_NAME])) {
2366eb3cdf6SAndreas Gohr        unset($_COOKIE[DOKU_SESSION_NAME]);
2376eb3cdf6SAndreas Gohr    }
2386eb3cdf6SAndreas Gohr
2396eb3cdf6SAndreas Gohr    session_start();
2406eb3cdf6SAndreas Gohr}
2416eb3cdf6SAndreas Gohr
2426eb3cdf6SAndreas Gohr
2436eb3cdf6SAndreas Gohr/**
24498407a7aSandi * Checks paths from config file
24598407a7aSandi */
24698407a7aSandifunction init_paths(){
24798407a7aSandi    global $conf;
24898407a7aSandi
24998407a7aSandi    $paths = array('datadir'   => 'pages',
25098407a7aSandi            'olddir'    => 'attic',
25198407a7aSandi            'mediadir'  => 'media',
252e4f389efSKate Arzamastseva            'mediaolddir' => 'media_attic',
25398407a7aSandi            'metadir'   => 'meta',
254e4f389efSKate Arzamastseva            'mediametadir' => 'media_meta',
25598407a7aSandi            'cachedir'  => 'cache',
256579b0f7eSTNHarris            'indexdir'  => 'index',
257de33a58fSMichael Klier            'lockdir'   => 'locks',
258de33a58fSMichael Klier            'tmpdir'    => 'tmp');
25998407a7aSandi
26098407a7aSandi    foreach($paths as $c => $p) {
2617f086b67SAnika Henke        $path = empty($conf[$c]) ? $conf['savedir'].'/'.$p : $conf[$c];
2626b9c156cSAnika Henke        $conf[$c] = init_path($path);
2636b9c156cSAnika Henke        if(empty($conf[$c]))
2646b9c156cSAnika Henke            nice_die("The $c ('$p') at $path is not found, isn't accessible or writable.
26569dc3177SAndreas Gohr                You should check your config and permission settings.
26669dc3177SAndreas Gohr                Or maybe you want to <a href=\"install.php\">run the
26769dc3177SAndreas Gohr                installer</a>?");
26898407a7aSandi    }
26971726d78SBen Coburn
27071726d78SBen Coburn    // path to old changelog only needed for upgrading
27171726d78SBen Coburn    $conf['changelog_old'] = init_path((isset($conf['changelog']))?($conf['changelog']):($conf['savedir'].'/changes.log'));
27271726d78SBen Coburn    if ($conf['changelog_old']=='') { unset($conf['changelog_old']); }
27371726d78SBen Coburn    // hardcoded changelog because it is now a cache that lives in meta
27471726d78SBen Coburn    $conf['changelog'] = $conf['metadir'].'/_dokuwiki.changes';
27599c8d7f2Smichael    $conf['media_changelog'] = $conf['metadir'].'/_media.changes';
27698407a7aSandi}
27798407a7aSandi
27838fb1fc7SGerrit Uitslag/**
27938fb1fc7SGerrit Uitslag * Load the language strings
28038fb1fc7SGerrit Uitslag *
28138fb1fc7SGerrit Uitslag * @param string $langCode language code, as passed by event handler
28238fb1fc7SGerrit Uitslag */
2836d06b26aSDominik Eckelmannfunction init_lang($langCode) {
2846d06b26aSDominik Eckelmann    //prepare language array
285dd7a6159SGerrit Uitslag    global $lang, $config_cascade;
2866d06b26aSDominik Eckelmann    $lang = array();
2876d06b26aSDominik Eckelmann
2886d06b26aSDominik Eckelmann    //load the language files
2891d82c8d3SChristopher Smith    require(DOKU_INC.'inc/lang/en/lang.php');
290dd7a6159SGerrit Uitslag    foreach ($config_cascade['lang']['core'] as $config_file) {
29179e79377SAndreas Gohr        if (file_exists($config_file . 'en/lang.php')) {
292dd7a6159SGerrit Uitslag            include($config_file . 'en/lang.php');
293dd7a6159SGerrit Uitslag        }
294dd7a6159SGerrit Uitslag    }
295dd7a6159SGerrit Uitslag
2966d06b26aSDominik Eckelmann    if ($langCode && $langCode != 'en') {
2976d06b26aSDominik Eckelmann        if (file_exists(DOKU_INC."inc/lang/$langCode/lang.php")) {
2981d82c8d3SChristopher Smith            require(DOKU_INC."inc/lang/$langCode/lang.php");
2996d06b26aSDominik Eckelmann        }
300dd7a6159SGerrit Uitslag        foreach ($config_cascade['lang']['core'] as $config_file) {
30179e79377SAndreas Gohr            if (file_exists($config_file . "$langCode/lang.php")) {
302dd7a6159SGerrit Uitslag                include($config_file . "$langCode/lang.php");
3036d06b26aSDominik Eckelmann            }
304dd7a6159SGerrit Uitslag        }
3056d06b26aSDominik Eckelmann    }
3066d06b26aSDominik Eckelmann}
3076d06b26aSDominik Eckelmann
30898407a7aSandi/**
3096b9c156cSAnika Henke * Checks the existence of certain files and creates them if missing.
3107367b368SAndreas Gohr */
3117367b368SAndreas Gohrfunction init_files(){
3127367b368SAndreas Gohr    global $conf;
3130d8850c4SAndreas Gohr
314345b1674SAndreas Gohr    $files = array($conf['indexdir'].'/page.idx');
3157367b368SAndreas Gohr
3167367b368SAndreas Gohr    foreach($files as $file){
31779e79377SAndreas Gohr        if(!file_exists($file)){
3180d8850c4SAndreas Gohr            $fh = @fopen($file,'a');
3190d8850c4SAndreas Gohr            if($fh){
3207367b368SAndreas Gohr                fclose($fh);
321443e135dSChristopher Smith                if(!empty($conf['fperm'])) chmod($file, $conf['fperm']);
3220d8850c4SAndreas Gohr            }else{
3233816dcbcSAndreas Gohr                nice_die("$file is not writable. Check your permissions settings!");
3240d8850c4SAndreas Gohr            }
3257367b368SAndreas Gohr        }
3267367b368SAndreas Gohr    }
3277367b368SAndreas Gohr}
3287367b368SAndreas Gohr
3297367b368SAndreas Gohr/**
3300d8850c4SAndreas Gohr * Returns absolute path
331f62ea8a1Sandi *
3320d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC.
3337f086b67SAnika Henke * Check for accessibility on directories as well.
3340d8850c4SAndreas Gohr *
3350d8850c4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
336f50a239bSTakamura *
337f50a239bSTakamura * @param string $path
338f50a239bSTakamura *
339f50a239bSTakamura * @return bool|string
340f62ea8a1Sandi */
341f62ea8a1Sandifunction init_path($path){
3426b9c156cSAnika Henke    // check existence
34300976812SAndreas Gohr    $p = fullpath($path);
34479e79377SAndreas Gohr    if(!file_exists($p)){
34500976812SAndreas Gohr        $p = fullpath(DOKU_INC.$path);
34679e79377SAndreas Gohr        if(!file_exists($p)){
3478fc4e739Sandi            return '';
348f62ea8a1Sandi        }
3490d8850c4SAndreas Gohr    }
3500d8850c4SAndreas Gohr
3510d8850c4SAndreas Gohr    // check writability
3520d8850c4SAndreas Gohr    if(!@is_writable($p)){
3530d8850c4SAndreas Gohr        return '';
3540d8850c4SAndreas Gohr    }
3550d8850c4SAndreas Gohr
3560d8850c4SAndreas Gohr    // check accessability (execute bit) for directories
35779e79377SAndreas Gohr    if(@is_dir($p) && !file_exists("$p/.")){
3580d8850c4SAndreas Gohr        return '';
3590d8850c4SAndreas Gohr    }
3600d8850c4SAndreas Gohr
3610d8850c4SAndreas Gohr    return $p;
3620d8850c4SAndreas Gohr}
3638c4f28e8Sjan
364ed7b5f09Sandi/**
3651ca31cfeSAndreas Gohr * Sets the internal config values fperm and dperm which, when set,
3661ca31cfeSAndreas Gohr * will be used to change the permission of a newly created dir or
3671ca31cfeSAndreas Gohr * file with chmod. Considers the influence of the system's umask
3681ca31cfeSAndreas Gohr * setting the values only if needed.
3691ca31cfeSAndreas Gohr */
3701ca31cfeSAndreas Gohrfunction init_creationmodes(){
3711ca31cfeSAndreas Gohr    global $conf;
3721ca31cfeSAndreas Gohr
3731ca31cfeSAndreas Gohr    // Legacy support for old umask/dmask scheme
3741ca31cfeSAndreas Gohr    unset($conf['dmask']);
3751ca31cfeSAndreas Gohr    unset($conf['fmask']);
3761ca31cfeSAndreas Gohr    unset($conf['umask']);
3771ca31cfeSAndreas Gohr    unset($conf['fperm']);
3781ca31cfeSAndreas Gohr    unset($conf['dperm']);
3791ca31cfeSAndreas Gohr
3809f3cdec3SAndreas Gohr    // get system umask, fallback to 0 if none available
3819f3cdec3SAndreas Gohr    $umask = @umask();
3829f3cdec3SAndreas Gohr    if(!$umask) $umask = 0000;
3831ca31cfeSAndreas Gohr
3841ca31cfeSAndreas Gohr    // check what is set automatically by the system on file creation
3851ca31cfeSAndreas Gohr    // and set the fperm param if it's not what we want
3861ca31cfeSAndreas Gohr    $auto_fmode = 0666 & ~$umask;
3871ca31cfeSAndreas Gohr    if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode'];
3881ca31cfeSAndreas Gohr
3891ca31cfeSAndreas Gohr    // check what is set automatically by the system on file creation
3901ca31cfeSAndreas Gohr    // and set the dperm param if it's not what we want
3911ca31cfeSAndreas Gohr    $auto_dmode = $conf['dmode'] & ~$umask;
3921ca31cfeSAndreas Gohr    if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode'];
3931ca31cfeSAndreas Gohr}
3941ca31cfeSAndreas Gohr
3951ca31cfeSAndreas Gohr/**
396ed7b5f09Sandi * Returns the full absolute URL to the directory where
397ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash)
398ed7b5f09Sandi *
399585bf44eSChristopher Smith * !! Can not access $_SERVER values through $INPUT
400585bf44eSChristopher Smith * !! here as this function is called before $INPUT is
401585bf44eSChristopher Smith * !! initialized.
402585bf44eSChristopher Smith *
403ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
404f50a239bSTakamura *
405f50a239bSTakamura * @param null|string $abs
406f50a239bSTakamura *
407f50a239bSTakamura * @return string
408ed7b5f09Sandi */
4094b1a4e04SAndreas Gohrfunction getBaseURL($abs=null){
410ed7b5f09Sandi    global $conf;
411ed7b5f09Sandi    //if canonical url enabled always return absolute
4124b1a4e04SAndreas Gohr    if(is_null($abs)) $abs = $conf['canonical'];
413ed7b5f09Sandi
4141858e4d7SGerry Weißbach    if(!empty($conf['basedir'])){
41546c73e01SChris Smith        $dir = $conf['basedir'];
41689aa05dbSAndreas Gohr    }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){
41746c73e01SChris Smith        $dir = dirname($_SERVER['SCRIPT_NAME']);
41889aa05dbSAndreas Gohr    }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){
41946c73e01SChris Smith        $dir = dirname($_SERVER['PHP_SELF']);
420093ec9e4Sandi    }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
421093ec9e4Sandi        $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
422093ec9e4Sandi                $_SERVER['SCRIPT_FILENAME']);
42346c73e01SChris Smith        $dir = dirname('/'.$dir);
42492b83b77Sandi    }else{
42546c73e01SChris Smith        $dir = '.'; //probably wrong
42692b83b77Sandi    }
427ed7b5f09Sandi
42846c73e01SChris Smith    $dir = str_replace('\\','/',$dir);             // bugfix for weird WIN behaviour
42946c73e01SChris Smith    $dir = preg_replace('#//+#','/',"/$dir/");     // ensure leading and trailing slashes
430ed7b5f09Sandi
431f62ea8a1Sandi    //handle script in lib/exe dir
432f62ea8a1Sandi    $dir = preg_replace('!lib/exe/$!','',$dir);
433f62ea8a1Sandi
434488d5fa0SMichael Klier chi@chimeric.de    //handle script in lib/plugins dir
435488d5fa0SMichael Klier chi@chimeric.de    $dir = preg_replace('!lib/plugins/.*$!','',$dir);
436488d5fa0SMichael Klier chi@chimeric.de
437ed7b5f09Sandi    //finish here for relative URLs
438ed7b5f09Sandi    if(!$abs) return $dir;
439ed7b5f09Sandi
44046c73e01SChris Smith    //use config option if available, trim any slash from end of baseurl to avoid multiple consecutive slashes in the path
4411858e4d7SGerry Weißbach    if(!empty($conf['baseurl'])) return rtrim($conf['baseurl'],'/').$dir;
442ef7b3ecdSAndreas Gohr
443e82e3526SAndreas Gohr    //split hostheader into host and port
4445627186cSAndreas Gohr    if(isset($_SERVER['HTTP_HOST'])){
445204b27c8SMichael Hamann        $parsed_host = parse_url('http://'.$_SERVER['HTTP_HOST']);
446f87b5dbbSChristopher Smith        $host = isset($parsed_host['host']) ? $parsed_host['host'] : null;
447f87b5dbbSChristopher Smith        $port = isset($parsed_host['port']) ? $parsed_host['port'] : null;
4485627186cSAndreas Gohr    }elseif(isset($_SERVER['SERVER_NAME'])){
449204b27c8SMichael Hamann        $parsed_host = parse_url('http://'.$_SERVER['SERVER_NAME']);
450f87b5dbbSChristopher Smith        $host = isset($parsed_host['host']) ? $parsed_host['host'] : null;
451f87b5dbbSChristopher Smith        $port = isset($parsed_host['port']) ? $parsed_host['port'] : null;
4525627186cSAndreas Gohr    }else{
4535627186cSAndreas Gohr        $host = php_uname('n');
454c66972f2SAdrian Lang        $port = '';
4555627186cSAndreas Gohr    }
4565627186cSAndreas Gohr
457204b27c8SMichael Hamann    if(is_null($port)){
458204b27c8SMichael Hamann        $port = '';
459204b27c8SMichael Hamann    }
460204b27c8SMichael Hamann
461f5c6743cSAndreas Gohr    if(!is_ssl()){
462ed7b5f09Sandi        $proto = 'http://';
463e82e3526SAndreas Gohr        if ($port == '80') {
464ed7b5f09Sandi            $port = '';
465ed7b5f09Sandi        }
466ed7b5f09Sandi    }else{
467ed7b5f09Sandi        $proto = 'https://';
468e82e3526SAndreas Gohr        if ($port == '443') {
469ed7b5f09Sandi            $port = '';
470ed7b5f09Sandi        }
471ed7b5f09Sandi    }
472ed7b5f09Sandi
473c66972f2SAdrian Lang    if($port !== '') $port = ':'.$port;
474e82e3526SAndreas Gohr
475ed7b5f09Sandi    return $proto.$host.$port.$dir;
476ed7b5f09Sandi}
477ed7b5f09Sandi
478b000c6d4Sandi/**
479f5c6743cSAndreas Gohr * Check if accessed via HTTPS
480f5c6743cSAndreas Gohr *
481f5c6743cSAndreas Gohr * Apache leaves ,$_SERVER['HTTPS'] empty when not available, IIS sets it to 'off'.
482f5c6743cSAndreas Gohr * 'false' and 'disabled' are just guessing
483f5c6743cSAndreas Gohr *
484f5c6743cSAndreas Gohr * @returns bool true when SSL is active
485f5c6743cSAndreas Gohr */
486f5c6743cSAndreas Gohrfunction is_ssl() {
48719738e65SEnrico Tagliavini    // check if we are behind a reverse proxy
48819738e65SEnrico Tagliavini    if(isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
48919738e65SEnrico Tagliavini        if($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
49019738e65SEnrico Tagliavini            return true;
49119738e65SEnrico Tagliavini        } else {
49219738e65SEnrico Tagliavini            return false;
49319738e65SEnrico Tagliavini        }
49419738e65SEnrico Tagliavini    }
495c66972f2SAdrian Lang    if(!isset($_SERVER['HTTPS']) ||
496c66972f2SAdrian Lang        preg_match('/^(|off|false|disabled)$/i', $_SERVER['HTTPS'])) {
497f5c6743cSAndreas Gohr        return false;
498f5c6743cSAndreas Gohr    } else {
499f5c6743cSAndreas Gohr        return true;
500f5c6743cSAndreas Gohr    }
501f5c6743cSAndreas Gohr}
502f5c6743cSAndreas Gohr
503f5c6743cSAndreas Gohr/**
50426714386SAndreas Gohr * checks it is windows OS
50526714386SAndreas Gohr * @return bool
50626714386SAndreas Gohr */
50726714386SAndreas Gohrfunction isWindows() {
50826714386SAndreas Gohr    return (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? true : false;
50926714386SAndreas Gohr}
51026714386SAndreas Gohr
51126714386SAndreas Gohr/**
5123816dcbcSAndreas Gohr * print a nice message even if no styles are loaded yet.
513f50a239bSTakamura *
514f50a239bSTakamura * @param integer|string $msg
5153816dcbcSAndreas Gohr */
5163816dcbcSAndreas Gohrfunction nice_die($msg){
5173816dcbcSAndreas Gohr    echo<<<EOT
518c8839c22SAnika Henke<!DOCTYPE html>
5193816dcbcSAndreas Gohr<html>
5203816dcbcSAndreas Gohr<head><title>DokuWiki Setup Error</title></head>
5213816dcbcSAndreas Gohr<body style="font-family: Arial, sans-serif">
5223816dcbcSAndreas Gohr    <div style="width:60%; margin: auto; background-color: #fcc;
5233816dcbcSAndreas Gohr                border: 1px solid #faa; padding: 0.5em 1em;">
5243816dcbcSAndreas Gohr        <h1 style="font-size: 120%">DokuWiki Setup Error</h1>
5253816dcbcSAndreas Gohr        <p>$msg</p>
5263816dcbcSAndreas Gohr    </div>
5273816dcbcSAndreas Gohr</body>
5283816dcbcSAndreas Gohr</html>
5293816dcbcSAndreas GohrEOT;
530*3862da0eSAndreas Gohr    if(defined('DOKU_UNITTEST')) {
531*3862da0eSAndreas Gohr        throw new RuntimeException('nice_die: '.$msg);
532*3862da0eSAndreas Gohr    }
5330a4266d4SElan Ruusamäe    exit(1);
5343816dcbcSAndreas Gohr}
5353816dcbcSAndreas Gohr
53600976812SAndreas Gohr/**
53700976812SAndreas Gohr * A realpath() replacement
53800976812SAndreas Gohr *
53900976812SAndreas Gohr * This function behaves similar to PHP's realpath() but does not resolve
54000976812SAndreas Gohr * symlinks or accesses upper directories
54100976812SAndreas Gohr *
5424761d30cSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
54300976812SAndreas Gohr * @author <richpageau at yahoo dot co dot uk>
54459752844SAnders Sandblad * @link   http://php.net/manual/en/function.realpath.php#75992
545f50a239bSTakamura *
546f50a239bSTakamura * @param string $path
547f50a239bSTakamura * @param bool $exists
548f50a239bSTakamura *
549f50a239bSTakamura * @return bool|string
55000976812SAndreas Gohr */
551b328697dSAndreas Gohrfunction fullpath($path,$exists=false){
5524761d30cSAndreas Gohr    static $run = 0;
5534761d30cSAndreas Gohr    $root  = '';
554f0a201c5SChris Smith    $iswin = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' || @$GLOBALS['DOKU_UNITTEST_ASSUME_WINDOWS']);
55500976812SAndreas Gohr
5564761d30cSAndreas Gohr    // find the (indestructable) root of the path - keeps windows stuff intact
5574761d30cSAndreas Gohr    if($path{0} == '/'){
5584761d30cSAndreas Gohr        $root = '/';
5594761d30cSAndreas Gohr    }elseif($iswin){
5604761d30cSAndreas Gohr        // match drive letter and UNC paths
5614761d30cSAndreas Gohr        if(preg_match('!^([a-zA-z]:)(.*)!',$path,$match)){
562b9c4302bSAndreas Gohr            $root = $match[1].'/';
5634761d30cSAndreas Gohr            $path = $match[2];
5644761d30cSAndreas Gohr        }else if(preg_match('!^(\\\\\\\\[^\\\\/]+\\\\[^\\\\/]+[\\\\/])(.*)!',$path,$match)){
5654761d30cSAndreas Gohr            $root = $match[1];
5664761d30cSAndreas Gohr            $path = $match[2];
56700976812SAndreas Gohr        }
5684761d30cSAndreas Gohr    }
5694761d30cSAndreas Gohr    $path = str_replace('\\','/',$path);
5704761d30cSAndreas Gohr
5714761d30cSAndreas Gohr    // if the given path wasn't absolute already, prepend the script path and retry
5724761d30cSAndreas Gohr    if(!$root){
5734761d30cSAndreas Gohr        $base = dirname($_SERVER['SCRIPT_FILENAME']);
5744761d30cSAndreas Gohr        $path = $base.'/'.$path;
5754761d30cSAndreas Gohr        if($run == 0){ // avoid endless recursion when base isn't absolute for some reason
5764761d30cSAndreas Gohr            $run++;
577b328697dSAndreas Gohr            return fullpath($path,$exists);
5784761d30cSAndreas Gohr        }
5794761d30cSAndreas Gohr    }
5804761d30cSAndreas Gohr    $run = 0;
58100976812SAndreas Gohr
58200976812SAndreas Gohr    // canonicalize
58300976812SAndreas Gohr    $path=explode('/', $path);
58400976812SAndreas Gohr    $newpath=array();
585ef38bfe8SAndreas Gohr    foreach($path as $p) {
586ef38bfe8SAndreas Gohr        if ($p === '' || $p === '.') continue;
587ef38bfe8SAndreas Gohr        if ($p==='..') {
58800976812SAndreas Gohr            array_pop($newpath);
58900976812SAndreas Gohr            continue;
59000976812SAndreas Gohr        }
591ef38bfe8SAndreas Gohr        array_push($newpath, $p);
59200976812SAndreas Gohr    }
5934761d30cSAndreas Gohr    $finalpath = $root.implode('/', $newpath);
59400976812SAndreas Gohr
5956b9c156cSAnika Henke    // check for existence when needed (except when unit testing)
59679e79377SAndreas Gohr    if($exists && !defined('DOKU_UNITTEST') && !file_exists($finalpath)) {
5974761d30cSAndreas Gohr        return false;
59800976812SAndreas Gohr    }
5994761d30cSAndreas Gohr    return $finalpath;
60000976812SAndreas Gohr}
60100976812SAndreas Gohr
602