xref: /dokuwiki/inc/init.php (revision b4f2363aa1360136c8a826f09aaebc6505211c73)
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
107*b4f2363aSAndreas Gohrif(!defined('NL')) define ('NL',"\n");
108b8595a66SAndreas Gohrif(!defined('DOKU_LF')) define ('DOKU_LF',"\n");
109b8595a66SAndreas Gohrif(!defined('DOKU_TAB')) define ('DOKU_TAB',"\t");
110ed7b5f09Sandi
111656c8fb3SAndreas Gohr// define cookie and session id, append server port when securecookie is configured FS#1664
112656c8fb3SAndreas Gohrif (!defined('DOKU_COOKIE')) define('DOKU_COOKIE', 'DW'.md5(DOKU_REL.(($conf['securecookie'])?$_SERVER['SERVER_PORT']:'')));
113e71ce681SAndreas Gohr
114ee20e7d1Sandi
115ed7b5f09Sandi// define main script
116ed7b5f09Sandiif(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php');
117ed7b5f09Sandi
118c4766956SAndreas Gohr// DEPRECATED, use tpl_basedir() instead
1196b13307fSandiif(!defined('DOKU_TPL')) define('DOKU_TPL',
120f62ea8a1Sandi        DOKU_BASE.'lib/tpl/'.$conf['template'].'/');
1216b13307fSandi
122c4766956SAndreas Gohr// DEPRECATED, use tpl_incdir() instead
12378a6aeb1SAndreas Gohrif(!defined('DOKU_TPLINC')) define('DOKU_TPLINC',
12478a6aeb1SAndreas Gohr        DOKU_INC.'lib/tpl/'.$conf['template'].'/');
12578a6aeb1SAndreas Gohr
126ed7b5f09Sandi// make session rewrites XHTML compliant
1273fc74836Sandi@ini_set('arg_separator.output', '&amp;');
128ed7b5f09Sandi
129d7e6bba9SAndreas Gohr// make sure global zlib does not interfere FS#1132
130d7e6bba9SAndreas Gohr@ini_set('zlib.output_compression', 'off');
131d7e6bba9SAndreas Gohr
1326deb5405SAndreas Gohr// increase PCRE backtrack limit
1336deb5405SAndreas Gohr@ini_set('pcre.backtrack_limit', '20971520');
1346deb5405SAndreas Gohr
13598bda4fdSAndreas Gohr// enable gzip compression if supported
13698bda4fdSAndreas Gohr$conf['gzip_output'] &= (strpos($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip') !== false);
13765f6e7d6SMichael Hamannglobal $ACT;
1383138b5c7SAndreas Gohrif ($conf['gzip_output'] &&
1393138b5c7SAndreas Gohr        !defined('DOKU_DISABLE_GZIP_OUTPUT') &&
14065f6e7d6SMichael Hamann        function_exists('ob_gzhandler') &&
14199e10b7fSMichael Hamann        // Disable compression when a (compressed) sitemap might be delivered
14265f6e7d6SMichael Hamann        // See https://bugs.dokuwiki.org/index.php?do=details&task_id=2576
14399e10b7fSMichael Hamann        $ACT != 'sitemap') {
1443138b5c7SAndreas Gohr    ob_start('ob_gzhandler');
1453138b5c7SAndreas Gohr}
1463138b5c7SAndreas Gohr
147ed7b5f09Sandi// init session
1486534245aSAndreas Gohrif(!headers_sent() && !defined('NOSESSION')) {
149c09f0eb1SGerrit Uitslag    if(!defined('DOKU_SESSION_NAME'))     define ('DOKU_SESSION_NAME', "DokuWiki");
150c09f0eb1SGerrit Uitslag    if(!defined('DOKU_SESSION_LIFETIME')) define ('DOKU_SESSION_LIFETIME', 0);
15155a71a16SGerrit Uitslag    if(!defined('DOKU_SESSION_PATH')) {
15273ab87deSGabriel Birke        $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir'];
15355a71a16SGerrit Uitslag        define ('DOKU_SESSION_PATH', $cookieDir);
154f5c6743cSAndreas Gohr    }
155c09f0eb1SGerrit Uitslag    if(!defined('DOKU_SESSION_DOMAIN'))   define ('DOKU_SESSION_DOMAIN', '');
156c09f0eb1SGerrit Uitslag
1576eb3cdf6SAndreas Gohr    // start the session
1586eb3cdf6SAndreas Gohr    init_session();
15914a122deSAndreas Gohr
16014a122deSAndreas Gohr    // load left over messages
16114a122deSAndreas Gohr    if(isset($_SESSION[DOKU_COOKIE]['msg'])) {
16214a122deSAndreas Gohr        $MSG = $_SESSION[DOKU_COOKIE]['msg'];
16314a122deSAndreas Gohr        unset($_SESSION[DOKU_COOKIE]['msg']);
16414a122deSAndreas Gohr    }
165bad31ae9SAndreas Gohr}
166ed7b5f09Sandi
167a1637ffdSAndreas Gohr// don't let cookies ever interfere with request vars
168a1637ffdSAndreas Gohr$_REQUEST = array_merge($_GET,$_POST);
169a1637ffdSAndreas Gohr
1703dea4ebcSAndreas Gohr// we don't want a purge URL to be digged
1710e80bb5eSChristopher Smithif(isset($_REQUEST['purge']) && !empty($_SERVER['HTTP_REFERER'])) unset($_REQUEST['purge']);
1723dea4ebcSAndreas Gohr
1731ca31cfeSAndreas Gohr// precalculate file creation modes
1741ca31cfeSAndreas Gohrinit_creationmodes();
175ed7b5f09Sandi
1763dc3a5f1Sandi// make real paths and check them
17798407a7aSandiinit_paths();
1787367b368SAndreas Gohrinit_files();
179ed7b5f09Sandi
180f1986589SMichael Klier// setup plugin controller class (can be overwritten in preload.php)
18193a7873eSAndreas Gohr$plugin_types = array('auth', 'admin','syntax','action','renderer', 'helper','remote');
182f1986589SMichael Klierglobal $plugin_controller_class, $plugin_controller;
183f1986589SMichael Klierif (empty($plugin_controller_class)) $plugin_controller_class = 'Doku_Plugin_Controller';
184f1986589SMichael Klier
185c7cb395cSAdrian Lang// load libraries
186605f8e8dSAndreas Gohrrequire_once(DOKU_INC.'vendor/autoload.php');
187c7cb395cSAdrian Langrequire_once(DOKU_INC.'inc/load.php');
188c7cb395cSAdrian Lang
1890f8f7aaaSDanny Lin// disable gzip if not available
19013c37900SAndreas Gohrdefine('DOKU_HAS_BZIP', function_exists('bzopen'));
19113c37900SAndreas Gohrdefine('DOKU_HAS_GZIP', function_exists('gzopen'));
19213c37900SAndreas Gohrif($conf['compression'] == 'bz2' && !DOKU_HAS_BZIP) {
1930f8f7aaaSDanny Lin    $conf['compression'] = 'gz';
1940f8f7aaaSDanny Lin}
19513c37900SAndreas Gohrif($conf['compression'] == 'gz' && !DOKU_HAS_GZIP) {
1960f8f7aaaSDanny Lin    $conf['compression'] = 0;
1970f8f7aaaSDanny Lin}
1980f8f7aaaSDanny Lin
19989177306SAndreas Gohr// input handle class
20089177306SAndreas Gohrglobal $INPUT;
20189177306SAndreas Gohr$INPUT = new Input();
20289177306SAndreas Gohr
203f1986589SMichael Klier// initialize plugin controller
204f1986589SMichael Klier$plugin_controller = new $plugin_controller_class();
205f1986589SMichael Klier
206f1986589SMichael Klier// initialize the event handler
207f1986589SMichael Klierglobal $EVENT_HANDLER;
208f1986589SMichael Klier$EVENT_HANDLER = new Doku_Event_Handler();
209f1986589SMichael Klier
2106d06b26aSDominik Eckelmann$local = $conf['lang'];
2116d06b26aSDominik Eckelmanntrigger_event('INIT_LANG_LOAD', $local, 'init_lang', true);
2126d06b26aSDominik Eckelmann
2136d06b26aSDominik Eckelmann
21416905344SAndreas Gohr// setup authentication system
215c7cb395cSAdrian Langif (!defined('NOSESSION')) {
21616905344SAndreas Gohr    auth_setup();
217c7cb395cSAdrian Lang}
218f62ea8a1Sandi
2195ec3fefcSAndreas Gohr// setup mail system
2205ec3fefcSAndreas Gohrmail_setup();
2215ec3fefcSAndreas Gohr
222f62ea8a1Sandi/**
2236eb3cdf6SAndreas Gohr * Initializes the session
2246eb3cdf6SAndreas Gohr *
2256eb3cdf6SAndreas Gohr * Makes sure the passed session cookie is valid, invalid ones are ignored an a new session ID is issued
2266eb3cdf6SAndreas Gohr *
2276eb3cdf6SAndreas Gohr * @link http://stackoverflow.com/a/33024310/172068
228924e477eSAndreas Gohr * @link http://php.net/manual/en/session.configuration.php#ini.session.sid-length
2296eb3cdf6SAndreas Gohr */
2306eb3cdf6SAndreas Gohrfunction init_session() {
2316eb3cdf6SAndreas Gohr    global $conf;
2326eb3cdf6SAndreas Gohr    session_name(DOKU_SESSION_NAME);
2336eb3cdf6SAndreas Gohr    session_set_cookie_params(DOKU_SESSION_LIFETIME, DOKU_SESSION_PATH, DOKU_SESSION_DOMAIN, ($conf['securecookie'] && is_ssl()), true);
2346eb3cdf6SAndreas Gohr
2356eb3cdf6SAndreas Gohr    // make sure the session cookie contains a valid session ID
236924e477eSAndreas Gohr    if(isset($_COOKIE[DOKU_SESSION_NAME]) && !preg_match('/^[-,a-zA-Z0-9]{22,256}$/', $_COOKIE[DOKU_SESSION_NAME])) {
2376eb3cdf6SAndreas Gohr        unset($_COOKIE[DOKU_SESSION_NAME]);
2386eb3cdf6SAndreas Gohr    }
2396eb3cdf6SAndreas Gohr
2406eb3cdf6SAndreas Gohr    session_start();
2416eb3cdf6SAndreas Gohr}
2426eb3cdf6SAndreas Gohr
2436eb3cdf6SAndreas Gohr
2446eb3cdf6SAndreas Gohr/**
24598407a7aSandi * Checks paths from config file
24698407a7aSandi */
24798407a7aSandifunction init_paths(){
24898407a7aSandi    global $conf;
24998407a7aSandi
25098407a7aSandi    $paths = array('datadir'   => 'pages',
25198407a7aSandi            'olddir'    => 'attic',
25298407a7aSandi            'mediadir'  => 'media',
253e4f389efSKate Arzamastseva            'mediaolddir' => 'media_attic',
25498407a7aSandi            'metadir'   => 'meta',
255e4f389efSKate Arzamastseva            'mediametadir' => 'media_meta',
25698407a7aSandi            'cachedir'  => 'cache',
257579b0f7eSTNHarris            'indexdir'  => 'index',
258de33a58fSMichael Klier            'lockdir'   => 'locks',
259de33a58fSMichael Klier            'tmpdir'    => 'tmp');
26098407a7aSandi
26198407a7aSandi    foreach($paths as $c => $p) {
2627f086b67SAnika Henke        $path = empty($conf[$c]) ? $conf['savedir'].'/'.$p : $conf[$c];
2636b9c156cSAnika Henke        $conf[$c] = init_path($path);
2646b9c156cSAnika Henke        if(empty($conf[$c]))
2656b9c156cSAnika Henke            nice_die("The $c ('$p') at $path is not found, isn't accessible or writable.
26669dc3177SAndreas Gohr                You should check your config and permission settings.
26769dc3177SAndreas Gohr                Or maybe you want to <a href=\"install.php\">run the
26869dc3177SAndreas Gohr                installer</a>?");
26998407a7aSandi    }
27071726d78SBen Coburn
27171726d78SBen Coburn    // path to old changelog only needed for upgrading
27271726d78SBen Coburn    $conf['changelog_old'] = init_path((isset($conf['changelog']))?($conf['changelog']):($conf['savedir'].'/changes.log'));
27371726d78SBen Coburn    if ($conf['changelog_old']=='') { unset($conf['changelog_old']); }
27471726d78SBen Coburn    // hardcoded changelog because it is now a cache that lives in meta
27571726d78SBen Coburn    $conf['changelog'] = $conf['metadir'].'/_dokuwiki.changes';
27699c8d7f2Smichael    $conf['media_changelog'] = $conf['metadir'].'/_media.changes';
27798407a7aSandi}
27898407a7aSandi
27938fb1fc7SGerrit Uitslag/**
28038fb1fc7SGerrit Uitslag * Load the language strings
28138fb1fc7SGerrit Uitslag *
28238fb1fc7SGerrit Uitslag * @param string $langCode language code, as passed by event handler
28338fb1fc7SGerrit Uitslag */
2846d06b26aSDominik Eckelmannfunction init_lang($langCode) {
2856d06b26aSDominik Eckelmann    //prepare language array
286dd7a6159SGerrit Uitslag    global $lang, $config_cascade;
2876d06b26aSDominik Eckelmann    $lang = array();
2886d06b26aSDominik Eckelmann
2896d06b26aSDominik Eckelmann    //load the language files
2901d82c8d3SChristopher Smith    require(DOKU_INC.'inc/lang/en/lang.php');
291dd7a6159SGerrit Uitslag    foreach ($config_cascade['lang']['core'] as $config_file) {
29279e79377SAndreas Gohr        if (file_exists($config_file . 'en/lang.php')) {
293dd7a6159SGerrit Uitslag            include($config_file . 'en/lang.php');
294dd7a6159SGerrit Uitslag        }
295dd7a6159SGerrit Uitslag    }
296dd7a6159SGerrit Uitslag
2976d06b26aSDominik Eckelmann    if ($langCode && $langCode != 'en') {
2986d06b26aSDominik Eckelmann        if (file_exists(DOKU_INC."inc/lang/$langCode/lang.php")) {
2991d82c8d3SChristopher Smith            require(DOKU_INC."inc/lang/$langCode/lang.php");
3006d06b26aSDominik Eckelmann        }
301dd7a6159SGerrit Uitslag        foreach ($config_cascade['lang']['core'] as $config_file) {
30279e79377SAndreas Gohr            if (file_exists($config_file . "$langCode/lang.php")) {
303dd7a6159SGerrit Uitslag                include($config_file . "$langCode/lang.php");
3046d06b26aSDominik Eckelmann            }
305dd7a6159SGerrit Uitslag        }
3066d06b26aSDominik Eckelmann    }
3076d06b26aSDominik Eckelmann}
3086d06b26aSDominik Eckelmann
30998407a7aSandi/**
3106b9c156cSAnika Henke * Checks the existence of certain files and creates them if missing.
3117367b368SAndreas Gohr */
3127367b368SAndreas Gohrfunction init_files(){
3137367b368SAndreas Gohr    global $conf;
3140d8850c4SAndreas Gohr
315345b1674SAndreas Gohr    $files = array($conf['indexdir'].'/page.idx');
3167367b368SAndreas Gohr
3177367b368SAndreas Gohr    foreach($files as $file){
31879e79377SAndreas Gohr        if(!file_exists($file)){
3190d8850c4SAndreas Gohr            $fh = @fopen($file,'a');
3200d8850c4SAndreas Gohr            if($fh){
3217367b368SAndreas Gohr                fclose($fh);
322443e135dSChristopher Smith                if(!empty($conf['fperm'])) chmod($file, $conf['fperm']);
3230d8850c4SAndreas Gohr            }else{
3243816dcbcSAndreas Gohr                nice_die("$file is not writable. Check your permissions settings!");
3250d8850c4SAndreas Gohr            }
3267367b368SAndreas Gohr        }
3277367b368SAndreas Gohr    }
3287367b368SAndreas Gohr}
3297367b368SAndreas Gohr
3307367b368SAndreas Gohr/**
3310d8850c4SAndreas Gohr * Returns absolute path
332f62ea8a1Sandi *
3330d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC.
3347f086b67SAnika Henke * Check for accessibility on directories as well.
3350d8850c4SAndreas Gohr *
3360d8850c4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
337f50a239bSTakamura *
338f50a239bSTakamura * @param string $path
339f50a239bSTakamura *
340f50a239bSTakamura * @return bool|string
341f62ea8a1Sandi */
342f62ea8a1Sandifunction init_path($path){
3436b9c156cSAnika Henke    // check existence
34400976812SAndreas Gohr    $p = fullpath($path);
34579e79377SAndreas Gohr    if(!file_exists($p)){
34600976812SAndreas Gohr        $p = fullpath(DOKU_INC.$path);
34779e79377SAndreas Gohr        if(!file_exists($p)){
3488fc4e739Sandi            return '';
349f62ea8a1Sandi        }
3500d8850c4SAndreas Gohr    }
3510d8850c4SAndreas Gohr
3520d8850c4SAndreas Gohr    // check writability
3530d8850c4SAndreas Gohr    if(!@is_writable($p)){
3540d8850c4SAndreas Gohr        return '';
3550d8850c4SAndreas Gohr    }
3560d8850c4SAndreas Gohr
3570d8850c4SAndreas Gohr    // check accessability (execute bit) for directories
35879e79377SAndreas Gohr    if(@is_dir($p) && !file_exists("$p/.")){
3590d8850c4SAndreas Gohr        return '';
3600d8850c4SAndreas Gohr    }
3610d8850c4SAndreas Gohr
3620d8850c4SAndreas Gohr    return $p;
3630d8850c4SAndreas Gohr}
3648c4f28e8Sjan
365ed7b5f09Sandi/**
3661ca31cfeSAndreas Gohr * Sets the internal config values fperm and dperm which, when set,
3671ca31cfeSAndreas Gohr * will be used to change the permission of a newly created dir or
3681ca31cfeSAndreas Gohr * file with chmod. Considers the influence of the system's umask
3691ca31cfeSAndreas Gohr * setting the values only if needed.
3701ca31cfeSAndreas Gohr */
3711ca31cfeSAndreas Gohrfunction init_creationmodes(){
3721ca31cfeSAndreas Gohr    global $conf;
3731ca31cfeSAndreas Gohr
3741ca31cfeSAndreas Gohr    // Legacy support for old umask/dmask scheme
3751ca31cfeSAndreas Gohr    unset($conf['dmask']);
3761ca31cfeSAndreas Gohr    unset($conf['fmask']);
3771ca31cfeSAndreas Gohr    unset($conf['umask']);
3781ca31cfeSAndreas Gohr    unset($conf['fperm']);
3791ca31cfeSAndreas Gohr    unset($conf['dperm']);
3801ca31cfeSAndreas Gohr
3819f3cdec3SAndreas Gohr    // get system umask, fallback to 0 if none available
3829f3cdec3SAndreas Gohr    $umask = @umask();
3839f3cdec3SAndreas Gohr    if(!$umask) $umask = 0000;
3841ca31cfeSAndreas Gohr
3851ca31cfeSAndreas Gohr    // check what is set automatically by the system on file creation
3861ca31cfeSAndreas Gohr    // and set the fperm param if it's not what we want
3871ca31cfeSAndreas Gohr    $auto_fmode = 0666 & ~$umask;
3881ca31cfeSAndreas Gohr    if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode'];
3891ca31cfeSAndreas Gohr
3901ca31cfeSAndreas Gohr    // check what is set automatically by the system on file creation
3911ca31cfeSAndreas Gohr    // and set the dperm param if it's not what we want
3921ca31cfeSAndreas Gohr    $auto_dmode = $conf['dmode'] & ~$umask;
3931ca31cfeSAndreas Gohr    if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode'];
3941ca31cfeSAndreas Gohr}
3951ca31cfeSAndreas Gohr
3961ca31cfeSAndreas Gohr/**
397ed7b5f09Sandi * Returns the full absolute URL to the directory where
398ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash)
399ed7b5f09Sandi *
400585bf44eSChristopher Smith * !! Can not access $_SERVER values through $INPUT
401585bf44eSChristopher Smith * !! here as this function is called before $INPUT is
402585bf44eSChristopher Smith * !! initialized.
403585bf44eSChristopher Smith *
404ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
405f50a239bSTakamura *
406f50a239bSTakamura * @param null|string $abs
407f50a239bSTakamura *
408f50a239bSTakamura * @return string
409ed7b5f09Sandi */
4104b1a4e04SAndreas Gohrfunction getBaseURL($abs=null){
411ed7b5f09Sandi    global $conf;
412ed7b5f09Sandi    //if canonical url enabled always return absolute
4134b1a4e04SAndreas Gohr    if(is_null($abs)) $abs = $conf['canonical'];
414ed7b5f09Sandi
4151858e4d7SGerry Weißbach    if(!empty($conf['basedir'])){
41646c73e01SChris Smith        $dir = $conf['basedir'];
41789aa05dbSAndreas Gohr    }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){
41846c73e01SChris Smith        $dir = dirname($_SERVER['SCRIPT_NAME']);
41989aa05dbSAndreas Gohr    }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){
42046c73e01SChris Smith        $dir = dirname($_SERVER['PHP_SELF']);
421093ec9e4Sandi    }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
422093ec9e4Sandi        $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
423093ec9e4Sandi                $_SERVER['SCRIPT_FILENAME']);
42446c73e01SChris Smith        $dir = dirname('/'.$dir);
42592b83b77Sandi    }else{
42646c73e01SChris Smith        $dir = '.'; //probably wrong
42792b83b77Sandi    }
428ed7b5f09Sandi
42946c73e01SChris Smith    $dir = str_replace('\\','/',$dir);             // bugfix for weird WIN behaviour
43046c73e01SChris Smith    $dir = preg_replace('#//+#','/',"/$dir/");     // ensure leading and trailing slashes
431ed7b5f09Sandi
432f62ea8a1Sandi    //handle script in lib/exe dir
433f62ea8a1Sandi    $dir = preg_replace('!lib/exe/$!','',$dir);
434f62ea8a1Sandi
435488d5fa0SMichael Klier chi@chimeric.de    //handle script in lib/plugins dir
436488d5fa0SMichael Klier chi@chimeric.de    $dir = preg_replace('!lib/plugins/.*$!','',$dir);
437488d5fa0SMichael Klier chi@chimeric.de
438ed7b5f09Sandi    //finish here for relative URLs
439ed7b5f09Sandi    if(!$abs) return $dir;
440ed7b5f09Sandi
44146c73e01SChris Smith    //use config option if available, trim any slash from end of baseurl to avoid multiple consecutive slashes in the path
4421858e4d7SGerry Weißbach    if(!empty($conf['baseurl'])) return rtrim($conf['baseurl'],'/').$dir;
443ef7b3ecdSAndreas Gohr
444e82e3526SAndreas Gohr    //split hostheader into host and port
4455627186cSAndreas Gohr    if(isset($_SERVER['HTTP_HOST'])){
446204b27c8SMichael Hamann        $parsed_host = parse_url('http://'.$_SERVER['HTTP_HOST']);
447f87b5dbbSChristopher Smith        $host = isset($parsed_host['host']) ? $parsed_host['host'] : null;
448f87b5dbbSChristopher Smith        $port = isset($parsed_host['port']) ? $parsed_host['port'] : null;
4495627186cSAndreas Gohr    }elseif(isset($_SERVER['SERVER_NAME'])){
450204b27c8SMichael Hamann        $parsed_host = parse_url('http://'.$_SERVER['SERVER_NAME']);
451f87b5dbbSChristopher Smith        $host = isset($parsed_host['host']) ? $parsed_host['host'] : null;
452f87b5dbbSChristopher Smith        $port = isset($parsed_host['port']) ? $parsed_host['port'] : null;
4535627186cSAndreas Gohr    }else{
4545627186cSAndreas Gohr        $host = php_uname('n');
455c66972f2SAdrian Lang        $port = '';
4565627186cSAndreas Gohr    }
4575627186cSAndreas Gohr
458204b27c8SMichael Hamann    if(is_null($port)){
459204b27c8SMichael Hamann        $port = '';
460204b27c8SMichael Hamann    }
461204b27c8SMichael Hamann
462f5c6743cSAndreas Gohr    if(!is_ssl()){
463ed7b5f09Sandi        $proto = 'http://';
464e82e3526SAndreas Gohr        if ($port == '80') {
465ed7b5f09Sandi            $port = '';
466ed7b5f09Sandi        }
467ed7b5f09Sandi    }else{
468ed7b5f09Sandi        $proto = 'https://';
469e82e3526SAndreas Gohr        if ($port == '443') {
470ed7b5f09Sandi            $port = '';
471ed7b5f09Sandi        }
472ed7b5f09Sandi    }
473ed7b5f09Sandi
474c66972f2SAdrian Lang    if($port !== '') $port = ':'.$port;
475e82e3526SAndreas Gohr
476ed7b5f09Sandi    return $proto.$host.$port.$dir;
477ed7b5f09Sandi}
478ed7b5f09Sandi
479b000c6d4Sandi/**
480f5c6743cSAndreas Gohr * Check if accessed via HTTPS
481f5c6743cSAndreas Gohr *
482f5c6743cSAndreas Gohr * Apache leaves ,$_SERVER['HTTPS'] empty when not available, IIS sets it to 'off'.
483f5c6743cSAndreas Gohr * 'false' and 'disabled' are just guessing
484f5c6743cSAndreas Gohr *
485f5c6743cSAndreas Gohr * @returns bool true when SSL is active
486f5c6743cSAndreas Gohr */
487f5c6743cSAndreas Gohrfunction is_ssl() {
48819738e65SEnrico Tagliavini    // check if we are behind a reverse proxy
48919738e65SEnrico Tagliavini    if(isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
49019738e65SEnrico Tagliavini        if($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
49119738e65SEnrico Tagliavini            return true;
49219738e65SEnrico Tagliavini        } else {
49319738e65SEnrico Tagliavini            return false;
49419738e65SEnrico Tagliavini        }
49519738e65SEnrico Tagliavini    }
496c66972f2SAdrian Lang    if(!isset($_SERVER['HTTPS']) ||
497c66972f2SAdrian Lang        preg_match('/^(|off|false|disabled)$/i', $_SERVER['HTTPS'])) {
498f5c6743cSAndreas Gohr        return false;
499f5c6743cSAndreas Gohr    } else {
500f5c6743cSAndreas Gohr        return true;
501f5c6743cSAndreas Gohr    }
502f5c6743cSAndreas Gohr}
503f5c6743cSAndreas Gohr
504f5c6743cSAndreas Gohr/**
50526714386SAndreas Gohr * checks it is windows OS
50626714386SAndreas Gohr * @return bool
50726714386SAndreas Gohr */
50826714386SAndreas Gohrfunction isWindows() {
50926714386SAndreas Gohr    return (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? true : false;
51026714386SAndreas Gohr}
51126714386SAndreas Gohr
51226714386SAndreas Gohr/**
5133816dcbcSAndreas Gohr * print a nice message even if no styles are loaded yet.
514f50a239bSTakamura *
515f50a239bSTakamura * @param integer|string $msg
5163816dcbcSAndreas Gohr */
5173816dcbcSAndreas Gohrfunction nice_die($msg){
5183816dcbcSAndreas Gohr    echo<<<EOT
519c8839c22SAnika Henke<!DOCTYPE html>
5203816dcbcSAndreas Gohr<html>
5213816dcbcSAndreas Gohr<head><title>DokuWiki Setup Error</title></head>
5223816dcbcSAndreas Gohr<body style="font-family: Arial, sans-serif">
5233816dcbcSAndreas Gohr    <div style="width:60%; margin: auto; background-color: #fcc;
5243816dcbcSAndreas Gohr                border: 1px solid #faa; padding: 0.5em 1em;">
5253816dcbcSAndreas Gohr        <h1 style="font-size: 120%">DokuWiki Setup Error</h1>
5263816dcbcSAndreas Gohr        <p>$msg</p>
5273816dcbcSAndreas Gohr    </div>
5283816dcbcSAndreas Gohr</body>
5293816dcbcSAndreas Gohr</html>
5303816dcbcSAndreas GohrEOT;
5313862da0eSAndreas Gohr    if(defined('DOKU_UNITTEST')) {
5323862da0eSAndreas Gohr        throw new RuntimeException('nice_die: '.$msg);
5333862da0eSAndreas Gohr    }
5340a4266d4SElan Ruusamäe    exit(1);
5353816dcbcSAndreas Gohr}
5363816dcbcSAndreas Gohr
53700976812SAndreas Gohr/**
53800976812SAndreas Gohr * A realpath() replacement
53900976812SAndreas Gohr *
54000976812SAndreas Gohr * This function behaves similar to PHP's realpath() but does not resolve
54100976812SAndreas Gohr * symlinks or accesses upper directories
54200976812SAndreas Gohr *
5434761d30cSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
54400976812SAndreas Gohr * @author <richpageau at yahoo dot co dot uk>
54559752844SAnders Sandblad * @link   http://php.net/manual/en/function.realpath.php#75992
546f50a239bSTakamura *
547f50a239bSTakamura * @param string $path
548f50a239bSTakamura * @param bool $exists
549f50a239bSTakamura *
550f50a239bSTakamura * @return bool|string
55100976812SAndreas Gohr */
552b328697dSAndreas Gohrfunction fullpath($path,$exists=false){
5534761d30cSAndreas Gohr    static $run = 0;
5544761d30cSAndreas Gohr    $root  = '';
555f0a201c5SChris Smith    $iswin = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' || @$GLOBALS['DOKU_UNITTEST_ASSUME_WINDOWS']);
55600976812SAndreas Gohr
5574761d30cSAndreas Gohr    // find the (indestructable) root of the path - keeps windows stuff intact
5584761d30cSAndreas Gohr    if($path{0} == '/'){
5594761d30cSAndreas Gohr        $root = '/';
5604761d30cSAndreas Gohr    }elseif($iswin){
5614761d30cSAndreas Gohr        // match drive letter and UNC paths
5624761d30cSAndreas Gohr        if(preg_match('!^([a-zA-z]:)(.*)!',$path,$match)){
563b9c4302bSAndreas Gohr            $root = $match[1].'/';
5644761d30cSAndreas Gohr            $path = $match[2];
5654761d30cSAndreas Gohr        }else if(preg_match('!^(\\\\\\\\[^\\\\/]+\\\\[^\\\\/]+[\\\\/])(.*)!',$path,$match)){
5664761d30cSAndreas Gohr            $root = $match[1];
5674761d30cSAndreas Gohr            $path = $match[2];
56800976812SAndreas Gohr        }
5694761d30cSAndreas Gohr    }
5704761d30cSAndreas Gohr    $path = str_replace('\\','/',$path);
5714761d30cSAndreas Gohr
5724761d30cSAndreas Gohr    // if the given path wasn't absolute already, prepend the script path and retry
5734761d30cSAndreas Gohr    if(!$root){
5744761d30cSAndreas Gohr        $base = dirname($_SERVER['SCRIPT_FILENAME']);
5754761d30cSAndreas Gohr        $path = $base.'/'.$path;
5764761d30cSAndreas Gohr        if($run == 0){ // avoid endless recursion when base isn't absolute for some reason
5774761d30cSAndreas Gohr            $run++;
578b328697dSAndreas Gohr            return fullpath($path,$exists);
5794761d30cSAndreas Gohr        }
5804761d30cSAndreas Gohr    }
5814761d30cSAndreas Gohr    $run = 0;
58200976812SAndreas Gohr
58300976812SAndreas Gohr    // canonicalize
58400976812SAndreas Gohr    $path=explode('/', $path);
58500976812SAndreas Gohr    $newpath=array();
586ef38bfe8SAndreas Gohr    foreach($path as $p) {
587ef38bfe8SAndreas Gohr        if ($p === '' || $p === '.') continue;
588ef38bfe8SAndreas Gohr        if ($p==='..') {
58900976812SAndreas Gohr            array_pop($newpath);
59000976812SAndreas Gohr            continue;
59100976812SAndreas Gohr        }
592ef38bfe8SAndreas Gohr        array_push($newpath, $p);
59300976812SAndreas Gohr    }
5944761d30cSAndreas Gohr    $finalpath = $root.implode('/', $newpath);
59500976812SAndreas Gohr
5966b9c156cSAnika Henke    // check for existence when needed (except when unit testing)
59779e79377SAndreas Gohr    if($exists && !defined('DOKU_UNITTEST') && !file_exists($finalpath)) {
5984761d30cSAndreas Gohr        return false;
59900976812SAndreas Gohr    }
6004761d30cSAndreas Gohr    return $finalpath;
60100976812SAndreas Gohr}
60200976812SAndreas Gohr
603