xref: /dokuwiki/inc/init.php (revision 924e477e18a8bef615c98bea5344d1b1b8054df7)
1ed7b5f09Sandi<?php
2ed7b5f09Sandi/**
3ed7b5f09Sandi * Initialize some defaults needed for DokuWiki
4ed7b5f09Sandi */
5ed7b5f09Sandi
63272d797SAndreas Gohr/**
73272d797SAndreas Gohr * timing Dokuwiki execution
83272d797SAndreas Gohr */
9a609a9ccSBen Coburnfunction delta_time($start=0) {
10ac4be4d7SPiyush Mishra    return microtime(true)-((float)$start);
11a609a9ccSBen Coburn}
12a609a9ccSBen Coburndefine('DOKU_START_TIME', delta_time());
13a609a9ccSBen Coburn
14ccaeaa85SAndreas Gohrglobal $config_cascade;
15cca94fbcSRoland Hager$config_cascade = array();
16ccaeaa85SAndreas Gohr
1748beefecSAndreas Gohr// if available load a preload config file
18e6266454SChris Smith$preload = fullpath(dirname(__FILE__)).'/preload.php';
1979e79377SAndreas Gohrif (file_exists($preload)) include($preload);
2048beefecSAndreas Gohr
21ed7b5f09Sandi// define the include path
2200976812SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../').'/');
23ad15db82Sandi
24c2a6d816SAndreas Gohr// define Plugin dir
25c2a6d816SAndreas Gohrif(!defined('DOKU_PLUGIN'))  define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
26c2a6d816SAndreas Gohr
27e7cb32dcSAndreas Gohr// define config path (packagers may want to change this to /etc/dokuwiki/)
28b7551a6dSEsther Brunnerif(!defined('DOKU_CONF')) define('DOKU_CONF',DOKU_INC.'conf/');
29e7cb32dcSAndreas Gohr
30bad905f1SBen Coburn// check for error reporting override or set error reporting to sane values
3179e79377SAndreas Gohrif (!defined('DOKU_E_LEVEL') && file_exists(DOKU_CONF.'report_e_all')) {
32bad905f1SBen Coburn    define('DOKU_E_LEVEL', E_ALL);
33bad905f1SBen Coburn}
34fc80ed59SAndreas Gohrif (!defined('DOKU_E_LEVEL')) {
354fcd684aSMichael Hamann    if(defined('E_DEPRECATED')){ // since php 5.3, since php 5.4 E_STRICT is part of E_ALL
364fcd684aSMichael Hamann        error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT);
37fc80ed59SAndreas Gohr    }else{
38fc80ed59SAndreas Gohr        error_reporting(E_ALL ^ E_NOTICE);
39fc80ed59SAndreas Gohr    }
40fc80ed59SAndreas Gohr} else {
41fc80ed59SAndreas Gohr    error_reporting(DOKU_E_LEVEL);
42fc80ed59SAndreas Gohr}
43c53ea5f2Sandi
4450602150SBen Coburn// init memory caches
45db959ae3SAndreas Gohrglobal $cache_revinfo;
46db959ae3SAndreas Gohr       $cache_revinfo = array();
47db959ae3SAndreas Gohrglobal $cache_wikifn;
48db959ae3SAndreas Gohr       $cache_wikifn = array();
49db959ae3SAndreas Gohrglobal $cache_cleanid;
50db959ae3SAndreas Gohr       $cache_cleanid = array();
51db959ae3SAndreas Gohrglobal $cache_authname;
52db959ae3SAndreas Gohr       $cache_authname = array();
53db959ae3SAndreas Gohrglobal $cache_metadata;
54db959ae3SAndreas Gohr       $cache_metadata = array();
5550602150SBen Coburn
56cca94fbcSRoland Hager// always include 'inc/config_cascade.php'
57cca94fbcSRoland Hager// previously in preload.php set fields of $config_cascade will be merged with the defaults
58e6a6dbfeSAndreas Gohrinclude(DOKU_INC.'inc/config_cascade.php');
59cb043f52SChris Smith
604724a577Sandi//prepare config array()
61ee20e7d1Sandiglobal $conf;
624724a577Sandi$conf = array();
634724a577Sandi
64cb043f52SChris Smith// load the global config file(s)
65b303b92cSChris Smithforeach (array('default','local','protected') as $config_group) {
66f8121585SChris Smith    if (empty($config_cascade['main'][$config_group])) continue;
67b303b92cSChris Smith    foreach ($config_cascade['main'][$config_group] as $config_file) {
6879e79377SAndreas Gohr        if (file_exists($config_file)) {
69f8121585SChris Smith            include($config_file);
70f8121585SChris Smith        }
71cb043f52SChris Smith    }
720a6ead41SAndreas Gohr}
73ad15db82Sandi
74066fee30SAndreas Gohr//prepare license array()
75066fee30SAndreas Gohrglobal $license;
76066fee30SAndreas Gohr$license = array();
77066fee30SAndreas Gohr
78066fee30SAndreas Gohr// load the license file(s)
79f8121585SChris Smithforeach (array('default','local') as $config_group) {
80f8121585SChris Smith    if (empty($config_cascade['license'][$config_group])) continue;
81f8121585SChris Smith    foreach ($config_cascade['license'][$config_group] as $config_file) {
8279e79377SAndreas Gohr        if(file_exists($config_file)){
83f8121585SChris Smith            include($config_file);
84f8121585SChris Smith        }
85f8121585SChris Smith    }
86066fee30SAndreas Gohr}
87066fee30SAndreas Gohr
881f8eb24fSAndreas Gohr// set timezone (as in pre 5.3.0 days)
891f8eb24fSAndreas Gohrdate_default_timezone_set(@date_default_timezone_get());
901f8eb24fSAndreas Gohr
91ed7b5f09Sandi// define baseURL
924b1a4e04SAndreas Gohrif(!defined('DOKU_REL')) define('DOKU_REL',getBaseURL(false));
93ed7b5f09Sandiif(!defined('DOKU_URL')) define('DOKU_URL',getBaseURL(true));
944b1a4e04SAndreas Gohrif(!defined('DOKU_BASE')){
954b1a4e04SAndreas Gohr    if($conf['canonical']){
964b1a4e04SAndreas Gohr        define('DOKU_BASE',DOKU_URL);
974b1a4e04SAndreas Gohr    }else{
984b1a4e04SAndreas Gohr        define('DOKU_BASE',DOKU_REL);
994b1a4e04SAndreas Gohr    }
1004b1a4e04SAndreas Gohr}
1014b1a4e04SAndreas Gohr
102b8595a66SAndreas Gohr// define whitespace
103b8595a66SAndreas Gohrif(!defined('DOKU_LF')) define ('DOKU_LF',"\n");
104b8595a66SAndreas Gohrif(!defined('DOKU_TAB')) define ('DOKU_TAB',"\t");
105ed7b5f09Sandi
106656c8fb3SAndreas Gohr// define cookie and session id, append server port when securecookie is configured FS#1664
107656c8fb3SAndreas Gohrif (!defined('DOKU_COOKIE')) define('DOKU_COOKIE', 'DW'.md5(DOKU_REL.(($conf['securecookie'])?$_SERVER['SERVER_PORT']:'')));
108e71ce681SAndreas Gohr
109ee20e7d1Sandi
110ed7b5f09Sandi// define main script
111ed7b5f09Sandiif(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php');
112ed7b5f09Sandi
113c4766956SAndreas Gohr// DEPRECATED, use tpl_basedir() instead
1146b13307fSandiif(!defined('DOKU_TPL')) define('DOKU_TPL',
115f62ea8a1Sandi        DOKU_BASE.'lib/tpl/'.$conf['template'].'/');
1166b13307fSandi
117c4766956SAndreas Gohr// DEPRECATED, use tpl_incdir() instead
11878a6aeb1SAndreas Gohrif(!defined('DOKU_TPLINC')) define('DOKU_TPLINC',
11978a6aeb1SAndreas Gohr        DOKU_INC.'lib/tpl/'.$conf['template'].'/');
12078a6aeb1SAndreas Gohr
121ed7b5f09Sandi// make session rewrites XHTML compliant
1223fc74836Sandi@ini_set('arg_separator.output', '&amp;');
123ed7b5f09Sandi
124d7e6bba9SAndreas Gohr// make sure global zlib does not interfere FS#1132
125d7e6bba9SAndreas Gohr@ini_set('zlib.output_compression', 'off');
126d7e6bba9SAndreas Gohr
1276deb5405SAndreas Gohr// increase PCRE backtrack limit
1286deb5405SAndreas Gohr@ini_set('pcre.backtrack_limit', '20971520');
1296deb5405SAndreas Gohr
13098bda4fdSAndreas Gohr// enable gzip compression if supported
13198bda4fdSAndreas Gohr$conf['gzip_output'] &= (strpos($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip') !== false);
13265f6e7d6SMichael Hamannglobal $ACT;
1333138b5c7SAndreas Gohrif ($conf['gzip_output'] &&
1343138b5c7SAndreas Gohr        !defined('DOKU_DISABLE_GZIP_OUTPUT') &&
13565f6e7d6SMichael Hamann        function_exists('ob_gzhandler') &&
13699e10b7fSMichael Hamann        // Disable compression when a (compressed) sitemap might be delivered
13765f6e7d6SMichael Hamann        // See https://bugs.dokuwiki.org/index.php?do=details&task_id=2576
13899e10b7fSMichael Hamann        $ACT != 'sitemap') {
1393138b5c7SAndreas Gohr    ob_start('ob_gzhandler');
1403138b5c7SAndreas Gohr}
1413138b5c7SAndreas Gohr
142ed7b5f09Sandi// init session
1436534245aSAndreas Gohrif(!headers_sent() && !defined('NOSESSION')) {
144c09f0eb1SGerrit Uitslag    if(!defined('DOKU_SESSION_NAME'))     define ('DOKU_SESSION_NAME', "DokuWiki");
145c09f0eb1SGerrit Uitslag    if(!defined('DOKU_SESSION_LIFETIME')) define ('DOKU_SESSION_LIFETIME', 0);
14655a71a16SGerrit Uitslag    if(!defined('DOKU_SESSION_PATH')) {
14773ab87deSGabriel Birke        $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir'];
14855a71a16SGerrit Uitslag        define ('DOKU_SESSION_PATH', $cookieDir);
149f5c6743cSAndreas Gohr    }
150c09f0eb1SGerrit Uitslag    if(!defined('DOKU_SESSION_DOMAIN'))   define ('DOKU_SESSION_DOMAIN', '');
151c09f0eb1SGerrit Uitslag
1526eb3cdf6SAndreas Gohr    // start the session
1536eb3cdf6SAndreas Gohr    init_session();
15414a122deSAndreas Gohr
15514a122deSAndreas Gohr    // load left over messages
15614a122deSAndreas Gohr    if(isset($_SESSION[DOKU_COOKIE]['msg'])) {
15714a122deSAndreas Gohr        $MSG = $_SESSION[DOKU_COOKIE]['msg'];
15814a122deSAndreas Gohr        unset($_SESSION[DOKU_COOKIE]['msg']);
15914a122deSAndreas Gohr    }
160bad31ae9SAndreas Gohr}
161ed7b5f09Sandi
162ed7b5f09Sandi// kill magic quotes
163e55eb89cSAndreas Gohrif (get_magic_quotes_gpc() && !defined('MAGIC_QUOTES_STRIPPED')) {
164ed7b5f09Sandi    if (!empty($_GET))    remove_magic_quotes($_GET);
165ed7b5f09Sandi    if (!empty($_POST))   remove_magic_quotes($_POST);
166ed7b5f09Sandi    if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE);
167ed7b5f09Sandi    if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST);
1683fc74836Sandi    @ini_set('magic_quotes_gpc', 0);
169e55eb89cSAndreas Gohr    define('MAGIC_QUOTES_STRIPPED',1);
170ed7b5f09Sandi}
171c114d4c4SAndreas Gohrif(function_exists('set_magic_quotes_runtime')) @set_magic_quotes_runtime(0);
1723fc74836Sandi@ini_set('magic_quotes_sybase',0);
173ed7b5f09Sandi
174a1637ffdSAndreas Gohr// don't let cookies ever interfere with request vars
175a1637ffdSAndreas Gohr$_REQUEST = array_merge($_GET,$_POST);
176a1637ffdSAndreas Gohr
1773dea4ebcSAndreas Gohr// we don't want a purge URL to be digged
1780e80bb5eSChristopher Smithif(isset($_REQUEST['purge']) && !empty($_SERVER['HTTP_REFERER'])) unset($_REQUEST['purge']);
1793dea4ebcSAndreas Gohr
1801ca31cfeSAndreas Gohr// precalculate file creation modes
1811ca31cfeSAndreas Gohrinit_creationmodes();
182ed7b5f09Sandi
1833dc3a5f1Sandi// make real paths and check them
18498407a7aSandiinit_paths();
1857367b368SAndreas Gohrinit_files();
186ed7b5f09Sandi
187f1986589SMichael Klier// setup plugin controller class (can be overwritten in preload.php)
18893a7873eSAndreas Gohr$plugin_types = array('auth', 'admin','syntax','action','renderer', 'helper','remote');
189f1986589SMichael Klierglobal $plugin_controller_class, $plugin_controller;
190f1986589SMichael Klierif (empty($plugin_controller_class)) $plugin_controller_class = 'Doku_Plugin_Controller';
191f1986589SMichael Klier
192c7cb395cSAdrian Lang// load libraries
193605f8e8dSAndreas Gohrrequire_once(DOKU_INC.'vendor/autoload.php');
194c7cb395cSAdrian Langrequire_once(DOKU_INC.'inc/load.php');
195c7cb395cSAdrian Lang
1960f8f7aaaSDanny Lin// disable gzip if not available
19713c37900SAndreas Gohrdefine('DOKU_HAS_BZIP', function_exists('bzopen'));
19813c37900SAndreas Gohrdefine('DOKU_HAS_GZIP', function_exists('gzopen'));
19913c37900SAndreas Gohrif($conf['compression'] == 'bz2' && !DOKU_HAS_BZIP) {
2000f8f7aaaSDanny Lin    $conf['compression'] = 'gz';
2010f8f7aaaSDanny Lin}
20213c37900SAndreas Gohrif($conf['compression'] == 'gz' && !DOKU_HAS_GZIP) {
2030f8f7aaaSDanny Lin    $conf['compression'] = 0;
2040f8f7aaaSDanny Lin}
2050f8f7aaaSDanny Lin
20689177306SAndreas Gohr// input handle class
20789177306SAndreas Gohrglobal $INPUT;
20889177306SAndreas Gohr$INPUT = new Input();
20989177306SAndreas Gohr
210f1986589SMichael Klier// initialize plugin controller
211f1986589SMichael Klier$plugin_controller = new $plugin_controller_class();
212f1986589SMichael Klier
213f1986589SMichael Klier// initialize the event handler
214f1986589SMichael Klierglobal $EVENT_HANDLER;
215f1986589SMichael Klier$EVENT_HANDLER = new Doku_Event_Handler();
216f1986589SMichael Klier
2176d06b26aSDominik Eckelmann$local = $conf['lang'];
2186d06b26aSDominik Eckelmanntrigger_event('INIT_LANG_LOAD', $local, 'init_lang', true);
2196d06b26aSDominik Eckelmann
2206d06b26aSDominik Eckelmann
22116905344SAndreas Gohr// setup authentication system
222c7cb395cSAdrian Langif (!defined('NOSESSION')) {
22316905344SAndreas Gohr    auth_setup();
224c7cb395cSAdrian Lang}
225f62ea8a1Sandi
2265ec3fefcSAndreas Gohr// setup mail system
2275ec3fefcSAndreas Gohrmail_setup();
2285ec3fefcSAndreas Gohr
229f62ea8a1Sandi/**
2306eb3cdf6SAndreas Gohr * Initializes the session
2316eb3cdf6SAndreas Gohr *
2326eb3cdf6SAndreas Gohr * Makes sure the passed session cookie is valid, invalid ones are ignored an a new session ID is issued
2336eb3cdf6SAndreas Gohr *
2346eb3cdf6SAndreas Gohr * @link http://stackoverflow.com/a/33024310/172068
235*924e477eSAndreas Gohr * @link http://php.net/manual/en/session.configuration.php#ini.session.sid-length
2366eb3cdf6SAndreas Gohr */
2376eb3cdf6SAndreas Gohrfunction init_session() {
2386eb3cdf6SAndreas Gohr    global $conf;
2396eb3cdf6SAndreas Gohr    session_name(DOKU_SESSION_NAME);
2406eb3cdf6SAndreas Gohr    session_set_cookie_params(DOKU_SESSION_LIFETIME, DOKU_SESSION_PATH, DOKU_SESSION_DOMAIN, ($conf['securecookie'] && is_ssl()), true);
2416eb3cdf6SAndreas Gohr
2426eb3cdf6SAndreas Gohr    // make sure the session cookie contains a valid session ID
243*924e477eSAndreas Gohr    if(isset($_COOKIE[DOKU_SESSION_NAME]) && !preg_match('/^[-,a-zA-Z0-9]{22,256}$/', $_COOKIE[DOKU_SESSION_NAME])) {
2446eb3cdf6SAndreas Gohr        unset($_COOKIE[DOKU_SESSION_NAME]);
2456eb3cdf6SAndreas Gohr    }
2466eb3cdf6SAndreas Gohr
2476eb3cdf6SAndreas Gohr    session_start();
2486eb3cdf6SAndreas Gohr}
2496eb3cdf6SAndreas Gohr
2506eb3cdf6SAndreas Gohr
2516eb3cdf6SAndreas Gohr/**
25298407a7aSandi * Checks paths from config file
25398407a7aSandi */
25498407a7aSandifunction init_paths(){
25598407a7aSandi    global $conf;
25698407a7aSandi
25798407a7aSandi    $paths = array('datadir'   => 'pages',
25898407a7aSandi            'olddir'    => 'attic',
25998407a7aSandi            'mediadir'  => 'media',
260e4f389efSKate Arzamastseva            'mediaolddir' => 'media_attic',
26198407a7aSandi            'metadir'   => 'meta',
262e4f389efSKate Arzamastseva            'mediametadir' => 'media_meta',
26398407a7aSandi            'cachedir'  => 'cache',
264579b0f7eSTNHarris            'indexdir'  => 'index',
265de33a58fSMichael Klier            'lockdir'   => 'locks',
266de33a58fSMichael Klier            'tmpdir'    => 'tmp');
26798407a7aSandi
26898407a7aSandi    foreach($paths as $c => $p) {
2697f086b67SAnika Henke        $path = empty($conf[$c]) ? $conf['savedir'].'/'.$p : $conf[$c];
2706b9c156cSAnika Henke        $conf[$c] = init_path($path);
2716b9c156cSAnika Henke        if(empty($conf[$c]))
2726b9c156cSAnika Henke            nice_die("The $c ('$p') at $path is not found, isn't accessible or writable.
27369dc3177SAndreas Gohr                You should check your config and permission settings.
27469dc3177SAndreas Gohr                Or maybe you want to <a href=\"install.php\">run the
27569dc3177SAndreas Gohr                installer</a>?");
27698407a7aSandi    }
27771726d78SBen Coburn
27871726d78SBen Coburn    // path to old changelog only needed for upgrading
27971726d78SBen Coburn    $conf['changelog_old'] = init_path((isset($conf['changelog']))?($conf['changelog']):($conf['savedir'].'/changes.log'));
28071726d78SBen Coburn    if ($conf['changelog_old']=='') { unset($conf['changelog_old']); }
28171726d78SBen Coburn    // hardcoded changelog because it is now a cache that lives in meta
28271726d78SBen Coburn    $conf['changelog'] = $conf['metadir'].'/_dokuwiki.changes';
28399c8d7f2Smichael    $conf['media_changelog'] = $conf['metadir'].'/_media.changes';
28498407a7aSandi}
28598407a7aSandi
28638fb1fc7SGerrit Uitslag/**
28738fb1fc7SGerrit Uitslag * Load the language strings
28838fb1fc7SGerrit Uitslag *
28938fb1fc7SGerrit Uitslag * @param string $langCode language code, as passed by event handler
29038fb1fc7SGerrit Uitslag */
2916d06b26aSDominik Eckelmannfunction init_lang($langCode) {
2926d06b26aSDominik Eckelmann    //prepare language array
293dd7a6159SGerrit Uitslag    global $lang, $config_cascade;
2946d06b26aSDominik Eckelmann    $lang = array();
2956d06b26aSDominik Eckelmann
2966d06b26aSDominik Eckelmann    //load the language files
2971d82c8d3SChristopher Smith    require(DOKU_INC.'inc/lang/en/lang.php');
298dd7a6159SGerrit Uitslag    foreach ($config_cascade['lang']['core'] as $config_file) {
29979e79377SAndreas Gohr        if (file_exists($config_file . 'en/lang.php')) {
300dd7a6159SGerrit Uitslag            include($config_file . 'en/lang.php');
301dd7a6159SGerrit Uitslag        }
302dd7a6159SGerrit Uitslag    }
303dd7a6159SGerrit Uitslag
3046d06b26aSDominik Eckelmann    if ($langCode && $langCode != 'en') {
3056d06b26aSDominik Eckelmann        if (file_exists(DOKU_INC."inc/lang/$langCode/lang.php")) {
3061d82c8d3SChristopher Smith            require(DOKU_INC."inc/lang/$langCode/lang.php");
3076d06b26aSDominik Eckelmann        }
308dd7a6159SGerrit Uitslag        foreach ($config_cascade['lang']['core'] as $config_file) {
30979e79377SAndreas Gohr            if (file_exists($config_file . "$langCode/lang.php")) {
310dd7a6159SGerrit Uitslag                include($config_file . "$langCode/lang.php");
3116d06b26aSDominik Eckelmann            }
312dd7a6159SGerrit Uitslag        }
3136d06b26aSDominik Eckelmann    }
3146d06b26aSDominik Eckelmann}
3156d06b26aSDominik Eckelmann
31698407a7aSandi/**
3176b9c156cSAnika Henke * Checks the existence of certain files and creates them if missing.
3187367b368SAndreas Gohr */
3197367b368SAndreas Gohrfunction init_files(){
3207367b368SAndreas Gohr    global $conf;
3210d8850c4SAndreas Gohr
322345b1674SAndreas Gohr    $files = array($conf['indexdir'].'/page.idx');
3237367b368SAndreas Gohr
3247367b368SAndreas Gohr    foreach($files as $file){
32579e79377SAndreas Gohr        if(!file_exists($file)){
3260d8850c4SAndreas Gohr            $fh = @fopen($file,'a');
3270d8850c4SAndreas Gohr            if($fh){
3287367b368SAndreas Gohr                fclose($fh);
329443e135dSChristopher Smith                if(!empty($conf['fperm'])) chmod($file, $conf['fperm']);
3300d8850c4SAndreas Gohr            }else{
3313816dcbcSAndreas Gohr                nice_die("$file is not writable. Check your permissions settings!");
3320d8850c4SAndreas Gohr            }
3337367b368SAndreas Gohr        }
3347367b368SAndreas Gohr    }
3357367b368SAndreas Gohr}
3367367b368SAndreas Gohr
3377367b368SAndreas Gohr/**
3380d8850c4SAndreas Gohr * Returns absolute path
339f62ea8a1Sandi *
3400d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC.
3417f086b67SAnika Henke * Check for accessibility on directories as well.
3420d8850c4SAndreas Gohr *
3430d8850c4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
344f62ea8a1Sandi */
345f62ea8a1Sandifunction init_path($path){
3466b9c156cSAnika Henke    // check existence
34700976812SAndreas Gohr    $p = fullpath($path);
34879e79377SAndreas Gohr    if(!file_exists($p)){
34900976812SAndreas Gohr        $p = fullpath(DOKU_INC.$path);
35079e79377SAndreas Gohr        if(!file_exists($p)){
3518fc4e739Sandi            return '';
352f62ea8a1Sandi        }
3530d8850c4SAndreas Gohr    }
3540d8850c4SAndreas Gohr
3550d8850c4SAndreas Gohr    // check writability
3560d8850c4SAndreas Gohr    if(!@is_writable($p)){
3570d8850c4SAndreas Gohr        return '';
3580d8850c4SAndreas Gohr    }
3590d8850c4SAndreas Gohr
3600d8850c4SAndreas Gohr    // check accessability (execute bit) for directories
36179e79377SAndreas Gohr    if(@is_dir($p) && !file_exists("$p/.")){
3620d8850c4SAndreas Gohr        return '';
3630d8850c4SAndreas Gohr    }
3640d8850c4SAndreas Gohr
3650d8850c4SAndreas Gohr    return $p;
3660d8850c4SAndreas Gohr}
3678c4f28e8Sjan
368ed7b5f09Sandi/**
3691ca31cfeSAndreas Gohr * Sets the internal config values fperm and dperm which, when set,
3701ca31cfeSAndreas Gohr * will be used to change the permission of a newly created dir or
3711ca31cfeSAndreas Gohr * file with chmod. Considers the influence of the system's umask
3721ca31cfeSAndreas Gohr * setting the values only if needed.
3731ca31cfeSAndreas Gohr */
3741ca31cfeSAndreas Gohrfunction init_creationmodes(){
3751ca31cfeSAndreas Gohr    global $conf;
3761ca31cfeSAndreas Gohr
3771ca31cfeSAndreas Gohr    // Legacy support for old umask/dmask scheme
3781ca31cfeSAndreas Gohr    unset($conf['dmask']);
3791ca31cfeSAndreas Gohr    unset($conf['fmask']);
3801ca31cfeSAndreas Gohr    unset($conf['umask']);
3811ca31cfeSAndreas Gohr    unset($conf['fperm']);
3821ca31cfeSAndreas Gohr    unset($conf['dperm']);
3831ca31cfeSAndreas Gohr
3849f3cdec3SAndreas Gohr    // get system umask, fallback to 0 if none available
3859f3cdec3SAndreas Gohr    $umask = @umask();
3869f3cdec3SAndreas Gohr    if(!$umask) $umask = 0000;
3871ca31cfeSAndreas Gohr
3881ca31cfeSAndreas Gohr    // check what is set automatically by the system on file creation
3891ca31cfeSAndreas Gohr    // and set the fperm param if it's not what we want
3901ca31cfeSAndreas Gohr    $auto_fmode = 0666 & ~$umask;
3911ca31cfeSAndreas Gohr    if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode'];
3921ca31cfeSAndreas Gohr
3931ca31cfeSAndreas Gohr    // check what is set automatically by the system on file creation
3941ca31cfeSAndreas Gohr    // and set the dperm param if it's not what we want
3951ca31cfeSAndreas Gohr    $auto_dmode = $conf['dmode'] & ~$umask;
3961ca31cfeSAndreas Gohr    if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode'];
3971ca31cfeSAndreas Gohr}
3981ca31cfeSAndreas Gohr
3991ca31cfeSAndreas Gohr/**
400ed7b5f09Sandi * remove magic quotes recursivly
401ed7b5f09Sandi *
402ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
403ed7b5f09Sandi */
404ed7b5f09Sandifunction remove_magic_quotes(&$array) {
405ed7b5f09Sandi    foreach (array_keys($array) as $key) {
40681c54349SAndreas Gohr        // handle magic quotes in keynames (breaks order)
40781c54349SAndreas Gohr        $sk = stripslashes($key);
40881c54349SAndreas Gohr        if($sk != $key){
40981c54349SAndreas Gohr            $array[$sk] = $array[$key];
41081c54349SAndreas Gohr            unset($array[$key]);
41181c54349SAndreas Gohr            $key = $sk;
41281c54349SAndreas Gohr        }
41381c54349SAndreas Gohr
41481c54349SAndreas Gohr        // do recursion if needed
415ed7b5f09Sandi        if (is_array($array[$key])) {
416ed7b5f09Sandi            remove_magic_quotes($array[$key]);
417ed7b5f09Sandi        }else {
418ed7b5f09Sandi            $array[$key] = stripslashes($array[$key]);
419ed7b5f09Sandi        }
420ed7b5f09Sandi    }
421ed7b5f09Sandi}
422ed7b5f09Sandi
423ed7b5f09Sandi/**
424ed7b5f09Sandi * Returns the full absolute URL to the directory where
425ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash)
426ed7b5f09Sandi *
427585bf44eSChristopher Smith * !! Can not access $_SERVER values through $INPUT
428585bf44eSChristopher Smith * !! here as this function is called before $INPUT is
429585bf44eSChristopher Smith * !! initialized.
430585bf44eSChristopher Smith *
431ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
432ed7b5f09Sandi */
4334b1a4e04SAndreas Gohrfunction getBaseURL($abs=null){
434ed7b5f09Sandi    global $conf;
435ed7b5f09Sandi    //if canonical url enabled always return absolute
4364b1a4e04SAndreas Gohr    if(is_null($abs)) $abs = $conf['canonical'];
437ed7b5f09Sandi
4381858e4d7SGerry Weißbach    if(!empty($conf['basedir'])){
43946c73e01SChris Smith        $dir = $conf['basedir'];
44089aa05dbSAndreas Gohr    }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){
44146c73e01SChris Smith        $dir = dirname($_SERVER['SCRIPT_NAME']);
44289aa05dbSAndreas Gohr    }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){
44346c73e01SChris Smith        $dir = dirname($_SERVER['PHP_SELF']);
444093ec9e4Sandi    }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
445093ec9e4Sandi        $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
446093ec9e4Sandi                $_SERVER['SCRIPT_FILENAME']);
44746c73e01SChris Smith        $dir = dirname('/'.$dir);
44892b83b77Sandi    }else{
44946c73e01SChris Smith        $dir = '.'; //probably wrong
45092b83b77Sandi    }
451ed7b5f09Sandi
45246c73e01SChris Smith    $dir = str_replace('\\','/',$dir);             // bugfix for weird WIN behaviour
45346c73e01SChris Smith    $dir = preg_replace('#//+#','/',"/$dir/");     // ensure leading and trailing slashes
454ed7b5f09Sandi
455f62ea8a1Sandi    //handle script in lib/exe dir
456f62ea8a1Sandi    $dir = preg_replace('!lib/exe/$!','',$dir);
457f62ea8a1Sandi
458488d5fa0SMichael Klier chi@chimeric.de    //handle script in lib/plugins dir
459488d5fa0SMichael Klier chi@chimeric.de    $dir = preg_replace('!lib/plugins/.*$!','',$dir);
460488d5fa0SMichael Klier chi@chimeric.de
461ed7b5f09Sandi    //finish here for relative URLs
462ed7b5f09Sandi    if(!$abs) return $dir;
463ed7b5f09Sandi
46446c73e01SChris Smith    //use config option if available, trim any slash from end of baseurl to avoid multiple consecutive slashes in the path
4651858e4d7SGerry Weißbach    if(!empty($conf['baseurl'])) return rtrim($conf['baseurl'],'/').$dir;
466ef7b3ecdSAndreas Gohr
467e82e3526SAndreas Gohr    //split hostheader into host and port
4685627186cSAndreas Gohr    if(isset($_SERVER['HTTP_HOST'])){
469204b27c8SMichael Hamann        $parsed_host = parse_url('http://'.$_SERVER['HTTP_HOST']);
470f87b5dbbSChristopher Smith        $host = isset($parsed_host['host']) ? $parsed_host['host'] : null;
471f87b5dbbSChristopher Smith        $port = isset($parsed_host['port']) ? $parsed_host['port'] : null;
4725627186cSAndreas Gohr    }elseif(isset($_SERVER['SERVER_NAME'])){
473204b27c8SMichael Hamann        $parsed_host = parse_url('http://'.$_SERVER['SERVER_NAME']);
474f87b5dbbSChristopher Smith        $host = isset($parsed_host['host']) ? $parsed_host['host'] : null;
475f87b5dbbSChristopher Smith        $port = isset($parsed_host['port']) ? $parsed_host['port'] : null;
4765627186cSAndreas Gohr    }else{
4775627186cSAndreas Gohr        $host = php_uname('n');
478c66972f2SAdrian Lang        $port = '';
4795627186cSAndreas Gohr    }
4805627186cSAndreas Gohr
481204b27c8SMichael Hamann    if(is_null($port)){
482204b27c8SMichael Hamann        $port = '';
483204b27c8SMichael Hamann    }
484204b27c8SMichael Hamann
485f5c6743cSAndreas Gohr    if(!is_ssl()){
486ed7b5f09Sandi        $proto = 'http://';
487e82e3526SAndreas Gohr        if ($port == '80') {
488ed7b5f09Sandi            $port = '';
489ed7b5f09Sandi        }
490ed7b5f09Sandi    }else{
491ed7b5f09Sandi        $proto = 'https://';
492e82e3526SAndreas Gohr        if ($port == '443') {
493ed7b5f09Sandi            $port = '';
494ed7b5f09Sandi        }
495ed7b5f09Sandi    }
496ed7b5f09Sandi
497c66972f2SAdrian Lang    if($port !== '') $port = ':'.$port;
498e82e3526SAndreas Gohr
499ed7b5f09Sandi    return $proto.$host.$port.$dir;
500ed7b5f09Sandi}
501ed7b5f09Sandi
502b000c6d4Sandi/**
503f5c6743cSAndreas Gohr * Check if accessed via HTTPS
504f5c6743cSAndreas Gohr *
505f5c6743cSAndreas Gohr * Apache leaves ,$_SERVER['HTTPS'] empty when not available, IIS sets it to 'off'.
506f5c6743cSAndreas Gohr * 'false' and 'disabled' are just guessing
507f5c6743cSAndreas Gohr *
508f5c6743cSAndreas Gohr * @returns bool true when SSL is active
509f5c6743cSAndreas Gohr */
510f5c6743cSAndreas Gohrfunction is_ssl(){
51119738e65SEnrico Tagliavini    // check if we are behind a reverse proxy
51219738e65SEnrico Tagliavini    if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
51319738e65SEnrico Tagliavini        if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
51419738e65SEnrico Tagliavini	    return true;
51519738e65SEnrico Tagliavini	} else {
51619738e65SEnrico Tagliavini	    return false;
51719738e65SEnrico Tagliavini	}
51819738e65SEnrico Tagliavini    }
519c66972f2SAdrian Lang    if (!isset($_SERVER['HTTPS']) ||
520c66972f2SAdrian Lang        preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){
521f5c6743cSAndreas Gohr        return false;
522f5c6743cSAndreas Gohr    }else{
523f5c6743cSAndreas Gohr        return true;
524f5c6743cSAndreas Gohr    }
525f5c6743cSAndreas Gohr}
526f5c6743cSAndreas Gohr
527f5c6743cSAndreas Gohr/**
5283816dcbcSAndreas Gohr * print a nice message even if no styles are loaded yet.
5293816dcbcSAndreas Gohr */
5303816dcbcSAndreas Gohrfunction nice_die($msg){
5313816dcbcSAndreas Gohr    echo<<<EOT
532c8839c22SAnika Henke<!DOCTYPE html>
5333816dcbcSAndreas Gohr<html>
5343816dcbcSAndreas Gohr<head><title>DokuWiki Setup Error</title></head>
5353816dcbcSAndreas Gohr<body style="font-family: Arial, sans-serif">
5363816dcbcSAndreas Gohr    <div style="width:60%; margin: auto; background-color: #fcc;
5373816dcbcSAndreas Gohr                border: 1px solid #faa; padding: 0.5em 1em;">
5383816dcbcSAndreas Gohr        <h1 style="font-size: 120%">DokuWiki Setup Error</h1>
5393816dcbcSAndreas Gohr        <p>$msg</p>
5403816dcbcSAndreas Gohr    </div>
5413816dcbcSAndreas Gohr</body>
5423816dcbcSAndreas Gohr</html>
5433816dcbcSAndreas GohrEOT;
5440a4266d4SElan Ruusamäe    exit(1);
5453816dcbcSAndreas Gohr}
5463816dcbcSAndreas Gohr
54700976812SAndreas Gohr/**
54800976812SAndreas Gohr * A realpath() replacement
54900976812SAndreas Gohr *
55000976812SAndreas Gohr * This function behaves similar to PHP's realpath() but does not resolve
55100976812SAndreas Gohr * symlinks or accesses upper directories
55200976812SAndreas Gohr *
5534761d30cSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
55400976812SAndreas Gohr * @author <richpageau at yahoo dot co dot uk>
55559752844SAnders Sandblad * @link   http://php.net/manual/en/function.realpath.php#75992
55600976812SAndreas Gohr */
557b328697dSAndreas Gohrfunction fullpath($path,$exists=false){
5584761d30cSAndreas Gohr    static $run = 0;
5594761d30cSAndreas Gohr    $root  = '';
560f0a201c5SChris Smith    $iswin = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' || @$GLOBALS['DOKU_UNITTEST_ASSUME_WINDOWS']);
56100976812SAndreas Gohr
5624761d30cSAndreas Gohr    // find the (indestructable) root of the path - keeps windows stuff intact
5634761d30cSAndreas Gohr    if($path{0} == '/'){
5644761d30cSAndreas Gohr        $root = '/';
5654761d30cSAndreas Gohr    }elseif($iswin){
5664761d30cSAndreas Gohr        // match drive letter and UNC paths
5674761d30cSAndreas Gohr        if(preg_match('!^([a-zA-z]:)(.*)!',$path,$match)){
568b9c4302bSAndreas Gohr            $root = $match[1].'/';
5694761d30cSAndreas Gohr            $path = $match[2];
5704761d30cSAndreas Gohr        }else if(preg_match('!^(\\\\\\\\[^\\\\/]+\\\\[^\\\\/]+[\\\\/])(.*)!',$path,$match)){
5714761d30cSAndreas Gohr            $root = $match[1];
5724761d30cSAndreas Gohr            $path = $match[2];
57300976812SAndreas Gohr        }
5744761d30cSAndreas Gohr    }
5754761d30cSAndreas Gohr    $path = str_replace('\\','/',$path);
5764761d30cSAndreas Gohr
5774761d30cSAndreas Gohr    // if the given path wasn't absolute already, prepend the script path and retry
5784761d30cSAndreas Gohr    if(!$root){
5794761d30cSAndreas Gohr        $base = dirname($_SERVER['SCRIPT_FILENAME']);
5804761d30cSAndreas Gohr        $path = $base.'/'.$path;
5814761d30cSAndreas Gohr        if($run == 0){ // avoid endless recursion when base isn't absolute for some reason
5824761d30cSAndreas Gohr            $run++;
583b328697dSAndreas Gohr            return fullpath($path,$exists);
5844761d30cSAndreas Gohr        }
5854761d30cSAndreas Gohr    }
5864761d30cSAndreas Gohr    $run = 0;
58700976812SAndreas Gohr
58800976812SAndreas Gohr    // canonicalize
58900976812SAndreas Gohr    $path=explode('/', $path);
59000976812SAndreas Gohr    $newpath=array();
591ef38bfe8SAndreas Gohr    foreach($path as $p) {
592ef38bfe8SAndreas Gohr        if ($p === '' || $p === '.') continue;
593ef38bfe8SAndreas Gohr        if ($p==='..') {
59400976812SAndreas Gohr            array_pop($newpath);
59500976812SAndreas Gohr            continue;
59600976812SAndreas Gohr        }
597ef38bfe8SAndreas Gohr        array_push($newpath, $p);
59800976812SAndreas Gohr    }
5994761d30cSAndreas Gohr    $finalpath = $root.implode('/', $newpath);
60000976812SAndreas Gohr
6016b9c156cSAnika Henke    // check for existence when needed (except when unit testing)
60279e79377SAndreas Gohr    if($exists && !defined('DOKU_UNITTEST') && !file_exists($finalpath)) {
6034761d30cSAndreas Gohr        return false;
60400976812SAndreas Gohr    }
6054761d30cSAndreas Gohr    return $finalpath;
60600976812SAndreas Gohr}
60700976812SAndreas Gohr
608