xref: /dokuwiki/inc/init.php (revision f50a239b3b819527445d240746b09a05fb76d103)
1ed7b5f09Sandi<?php
2ed7b5f09Sandi/**
3ed7b5f09Sandi * Initialize some defaults needed for DokuWiki
4ed7b5f09Sandi */
5ed7b5f09Sandi
63272d797SAndreas Gohr/**
73272d797SAndreas Gohr * timing Dokuwiki execution
8*f50a239bSTakamura *
9*f50a239bSTakamura * @param integer $start
10*f50a239bSTakamura *
11*f50a239bSTakamura * @return mixed
123272d797SAndreas Gohr */
13a609a9ccSBen Coburnfunction delta_time($start=0) {
14ac4be4d7SPiyush Mishra    return microtime(true)-((float)$start);
15a609a9ccSBen Coburn}
16a609a9ccSBen Coburndefine('DOKU_START_TIME', delta_time());
17a609a9ccSBen Coburn
18ccaeaa85SAndreas Gohrglobal $config_cascade;
19cca94fbcSRoland Hager$config_cascade = array();
20ccaeaa85SAndreas Gohr
2148beefecSAndreas Gohr// if available load a preload config file
22e6266454SChris Smith$preload = fullpath(dirname(__FILE__)).'/preload.php';
2379e79377SAndreas Gohrif (file_exists($preload)) include($preload);
2448beefecSAndreas Gohr
25ed7b5f09Sandi// define the include path
2600976812SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../').'/');
27ad15db82Sandi
28c2a6d816SAndreas Gohr// define Plugin dir
29c2a6d816SAndreas Gohrif(!defined('DOKU_PLUGIN'))  define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
30c2a6d816SAndreas Gohr
31e7cb32dcSAndreas Gohr// define config path (packagers may want to change this to /etc/dokuwiki/)
32b7551a6dSEsther Brunnerif(!defined('DOKU_CONF')) define('DOKU_CONF',DOKU_INC.'conf/');
33e7cb32dcSAndreas Gohr
34bad905f1SBen Coburn// check for error reporting override or set error reporting to sane values
3579e79377SAndreas Gohrif (!defined('DOKU_E_LEVEL') && file_exists(DOKU_CONF.'report_e_all')) {
36bad905f1SBen Coburn    define('DOKU_E_LEVEL', E_ALL);
37bad905f1SBen Coburn}
38fc80ed59SAndreas Gohrif (!defined('DOKU_E_LEVEL')) {
394fcd684aSMichael Hamann    if(defined('E_DEPRECATED')){ // since php 5.3, since php 5.4 E_STRICT is part of E_ALL
404fcd684aSMichael Hamann        error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT);
41fc80ed59SAndreas Gohr    }else{
42fc80ed59SAndreas Gohr        error_reporting(E_ALL ^ E_NOTICE);
43fc80ed59SAndreas Gohr    }
44fc80ed59SAndreas Gohr} else {
45fc80ed59SAndreas Gohr    error_reporting(DOKU_E_LEVEL);
46fc80ed59SAndreas Gohr}
47c53ea5f2Sandi
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
166ed7b5f09Sandi// kill magic quotes
167e55eb89cSAndreas Gohrif (get_magic_quotes_gpc() && !defined('MAGIC_QUOTES_STRIPPED')) {
168ed7b5f09Sandi    if (!empty($_GET))    remove_magic_quotes($_GET);
169ed7b5f09Sandi    if (!empty($_POST))   remove_magic_quotes($_POST);
170ed7b5f09Sandi    if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE);
171ed7b5f09Sandi    if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST);
1723fc74836Sandi    @ini_set('magic_quotes_gpc', 0);
173e55eb89cSAndreas Gohr    define('MAGIC_QUOTES_STRIPPED',1);
174ed7b5f09Sandi}
175c114d4c4SAndreas Gohrif(function_exists('set_magic_quotes_runtime')) @set_magic_quotes_runtime(0);
1763fc74836Sandi@ini_set('magic_quotes_sybase',0);
177ed7b5f09Sandi
178a1637ffdSAndreas Gohr// don't let cookies ever interfere with request vars
179a1637ffdSAndreas Gohr$_REQUEST = array_merge($_GET,$_POST);
180a1637ffdSAndreas Gohr
1813dea4ebcSAndreas Gohr// we don't want a purge URL to be digged
1820e80bb5eSChristopher Smithif(isset($_REQUEST['purge']) && !empty($_SERVER['HTTP_REFERER'])) unset($_REQUEST['purge']);
1833dea4ebcSAndreas Gohr
1841ca31cfeSAndreas Gohr// precalculate file creation modes
1851ca31cfeSAndreas Gohrinit_creationmodes();
186ed7b5f09Sandi
1873dc3a5f1Sandi// make real paths and check them
18898407a7aSandiinit_paths();
1897367b368SAndreas Gohrinit_files();
190ed7b5f09Sandi
191f1986589SMichael Klier// setup plugin controller class (can be overwritten in preload.php)
19293a7873eSAndreas Gohr$plugin_types = array('auth', 'admin','syntax','action','renderer', 'helper','remote');
193f1986589SMichael Klierglobal $plugin_controller_class, $plugin_controller;
194f1986589SMichael Klierif (empty($plugin_controller_class)) $plugin_controller_class = 'Doku_Plugin_Controller';
195f1986589SMichael Klier
196c7cb395cSAdrian Lang// load libraries
197605f8e8dSAndreas Gohrrequire_once(DOKU_INC.'vendor/autoload.php');
198c7cb395cSAdrian Langrequire_once(DOKU_INC.'inc/load.php');
199c7cb395cSAdrian Lang
2000f8f7aaaSDanny Lin// disable gzip if not available
20113c37900SAndreas Gohrdefine('DOKU_HAS_BZIP', function_exists('bzopen'));
20213c37900SAndreas Gohrdefine('DOKU_HAS_GZIP', function_exists('gzopen'));
20313c37900SAndreas Gohrif($conf['compression'] == 'bz2' && !DOKU_HAS_BZIP) {
2040f8f7aaaSDanny Lin    $conf['compression'] = 'gz';
2050f8f7aaaSDanny Lin}
20613c37900SAndreas Gohrif($conf['compression'] == 'gz' && !DOKU_HAS_GZIP) {
2070f8f7aaaSDanny Lin    $conf['compression'] = 0;
2080f8f7aaaSDanny Lin}
2090f8f7aaaSDanny Lin
21089177306SAndreas Gohr// input handle class
21189177306SAndreas Gohrglobal $INPUT;
21289177306SAndreas Gohr$INPUT = new Input();
21389177306SAndreas Gohr
214f1986589SMichael Klier// initialize plugin controller
215f1986589SMichael Klier$plugin_controller = new $plugin_controller_class();
216f1986589SMichael Klier
217f1986589SMichael Klier// initialize the event handler
218f1986589SMichael Klierglobal $EVENT_HANDLER;
219f1986589SMichael Klier$EVENT_HANDLER = new Doku_Event_Handler();
220f1986589SMichael Klier
2216d06b26aSDominik Eckelmann$local = $conf['lang'];
2226d06b26aSDominik Eckelmanntrigger_event('INIT_LANG_LOAD', $local, 'init_lang', true);
2236d06b26aSDominik Eckelmann
2246d06b26aSDominik Eckelmann
22516905344SAndreas Gohr// setup authentication system
226c7cb395cSAdrian Langif (!defined('NOSESSION')) {
22716905344SAndreas Gohr    auth_setup();
228c7cb395cSAdrian Lang}
229f62ea8a1Sandi
2305ec3fefcSAndreas Gohr// setup mail system
2315ec3fefcSAndreas Gohrmail_setup();
2325ec3fefcSAndreas Gohr
233f62ea8a1Sandi/**
2346eb3cdf6SAndreas Gohr * Initializes the session
2356eb3cdf6SAndreas Gohr *
2366eb3cdf6SAndreas Gohr * Makes sure the passed session cookie is valid, invalid ones are ignored an a new session ID is issued
2376eb3cdf6SAndreas Gohr *
2386eb3cdf6SAndreas Gohr * @link http://stackoverflow.com/a/33024310/172068
239924e477eSAndreas Gohr * @link http://php.net/manual/en/session.configuration.php#ini.session.sid-length
2406eb3cdf6SAndreas Gohr */
2416eb3cdf6SAndreas Gohrfunction init_session() {
2426eb3cdf6SAndreas Gohr    global $conf;
2436eb3cdf6SAndreas Gohr    session_name(DOKU_SESSION_NAME);
2446eb3cdf6SAndreas Gohr    session_set_cookie_params(DOKU_SESSION_LIFETIME, DOKU_SESSION_PATH, DOKU_SESSION_DOMAIN, ($conf['securecookie'] && is_ssl()), true);
2456eb3cdf6SAndreas Gohr
2466eb3cdf6SAndreas Gohr    // make sure the session cookie contains a valid session ID
247924e477eSAndreas Gohr    if(isset($_COOKIE[DOKU_SESSION_NAME]) && !preg_match('/^[-,a-zA-Z0-9]{22,256}$/', $_COOKIE[DOKU_SESSION_NAME])) {
2486eb3cdf6SAndreas Gohr        unset($_COOKIE[DOKU_SESSION_NAME]);
2496eb3cdf6SAndreas Gohr    }
2506eb3cdf6SAndreas Gohr
2516eb3cdf6SAndreas Gohr    session_start();
2526eb3cdf6SAndreas Gohr}
2536eb3cdf6SAndreas Gohr
2546eb3cdf6SAndreas Gohr
2556eb3cdf6SAndreas Gohr/**
25698407a7aSandi * Checks paths from config file
25798407a7aSandi */
25898407a7aSandifunction init_paths(){
25998407a7aSandi    global $conf;
26098407a7aSandi
26198407a7aSandi    $paths = array('datadir'   => 'pages',
26298407a7aSandi            'olddir'    => 'attic',
26398407a7aSandi            'mediadir'  => 'media',
264e4f389efSKate Arzamastseva            'mediaolddir' => 'media_attic',
26598407a7aSandi            'metadir'   => 'meta',
266e4f389efSKate Arzamastseva            'mediametadir' => 'media_meta',
26798407a7aSandi            'cachedir'  => 'cache',
268579b0f7eSTNHarris            'indexdir'  => 'index',
269de33a58fSMichael Klier            'lockdir'   => 'locks',
270de33a58fSMichael Klier            'tmpdir'    => 'tmp');
27198407a7aSandi
27298407a7aSandi    foreach($paths as $c => $p) {
2737f086b67SAnika Henke        $path = empty($conf[$c]) ? $conf['savedir'].'/'.$p : $conf[$c];
2746b9c156cSAnika Henke        $conf[$c] = init_path($path);
2756b9c156cSAnika Henke        if(empty($conf[$c]))
2766b9c156cSAnika Henke            nice_die("The $c ('$p') at $path is not found, isn't accessible or writable.
27769dc3177SAndreas Gohr                You should check your config and permission settings.
27869dc3177SAndreas Gohr                Or maybe you want to <a href=\"install.php\">run the
27969dc3177SAndreas Gohr                installer</a>?");
28098407a7aSandi    }
28171726d78SBen Coburn
28271726d78SBen Coburn    // path to old changelog only needed for upgrading
28371726d78SBen Coburn    $conf['changelog_old'] = init_path((isset($conf['changelog']))?($conf['changelog']):($conf['savedir'].'/changes.log'));
28471726d78SBen Coburn    if ($conf['changelog_old']=='') { unset($conf['changelog_old']); }
28571726d78SBen Coburn    // hardcoded changelog because it is now a cache that lives in meta
28671726d78SBen Coburn    $conf['changelog'] = $conf['metadir'].'/_dokuwiki.changes';
28799c8d7f2Smichael    $conf['media_changelog'] = $conf['metadir'].'/_media.changes';
28898407a7aSandi}
28998407a7aSandi
29038fb1fc7SGerrit Uitslag/**
29138fb1fc7SGerrit Uitslag * Load the language strings
29238fb1fc7SGerrit Uitslag *
29338fb1fc7SGerrit Uitslag * @param string $langCode language code, as passed by event handler
29438fb1fc7SGerrit Uitslag */
2956d06b26aSDominik Eckelmannfunction init_lang($langCode) {
2966d06b26aSDominik Eckelmann    //prepare language array
297dd7a6159SGerrit Uitslag    global $lang, $config_cascade;
2986d06b26aSDominik Eckelmann    $lang = array();
2996d06b26aSDominik Eckelmann
3006d06b26aSDominik Eckelmann    //load the language files
3011d82c8d3SChristopher Smith    require(DOKU_INC.'inc/lang/en/lang.php');
302dd7a6159SGerrit Uitslag    foreach ($config_cascade['lang']['core'] as $config_file) {
30379e79377SAndreas Gohr        if (file_exists($config_file . 'en/lang.php')) {
304dd7a6159SGerrit Uitslag            include($config_file . 'en/lang.php');
305dd7a6159SGerrit Uitslag        }
306dd7a6159SGerrit Uitslag    }
307dd7a6159SGerrit Uitslag
3086d06b26aSDominik Eckelmann    if ($langCode && $langCode != 'en') {
3096d06b26aSDominik Eckelmann        if (file_exists(DOKU_INC."inc/lang/$langCode/lang.php")) {
3101d82c8d3SChristopher Smith            require(DOKU_INC."inc/lang/$langCode/lang.php");
3116d06b26aSDominik Eckelmann        }
312dd7a6159SGerrit Uitslag        foreach ($config_cascade['lang']['core'] as $config_file) {
31379e79377SAndreas Gohr            if (file_exists($config_file . "$langCode/lang.php")) {
314dd7a6159SGerrit Uitslag                include($config_file . "$langCode/lang.php");
3156d06b26aSDominik Eckelmann            }
316dd7a6159SGerrit Uitslag        }
3176d06b26aSDominik Eckelmann    }
3186d06b26aSDominik Eckelmann}
3196d06b26aSDominik Eckelmann
32098407a7aSandi/**
3216b9c156cSAnika Henke * Checks the existence of certain files and creates them if missing.
3227367b368SAndreas Gohr */
3237367b368SAndreas Gohrfunction init_files(){
3247367b368SAndreas Gohr    global $conf;
3250d8850c4SAndreas Gohr
326345b1674SAndreas Gohr    $files = array($conf['indexdir'].'/page.idx');
3277367b368SAndreas Gohr
3287367b368SAndreas Gohr    foreach($files as $file){
32979e79377SAndreas Gohr        if(!file_exists($file)){
3300d8850c4SAndreas Gohr            $fh = @fopen($file,'a');
3310d8850c4SAndreas Gohr            if($fh){
3327367b368SAndreas Gohr                fclose($fh);
333443e135dSChristopher Smith                if(!empty($conf['fperm'])) chmod($file, $conf['fperm']);
3340d8850c4SAndreas Gohr            }else{
3353816dcbcSAndreas Gohr                nice_die("$file is not writable. Check your permissions settings!");
3360d8850c4SAndreas Gohr            }
3377367b368SAndreas Gohr        }
3387367b368SAndreas Gohr    }
3397367b368SAndreas Gohr}
3407367b368SAndreas Gohr
3417367b368SAndreas Gohr/**
3420d8850c4SAndreas Gohr * Returns absolute path
343f62ea8a1Sandi *
3440d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC.
3457f086b67SAnika Henke * Check for accessibility on directories as well.
3460d8850c4SAndreas Gohr *
3470d8850c4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
348*f50a239bSTakamura *
349*f50a239bSTakamura * @param string $path
350*f50a239bSTakamura *
351*f50a239bSTakamura * @return bool|string
352f62ea8a1Sandi */
353f62ea8a1Sandifunction init_path($path){
3546b9c156cSAnika Henke    // check existence
35500976812SAndreas Gohr    $p = fullpath($path);
35679e79377SAndreas Gohr    if(!file_exists($p)){
35700976812SAndreas Gohr        $p = fullpath(DOKU_INC.$path);
35879e79377SAndreas Gohr        if(!file_exists($p)){
3598fc4e739Sandi            return '';
360f62ea8a1Sandi        }
3610d8850c4SAndreas Gohr    }
3620d8850c4SAndreas Gohr
3630d8850c4SAndreas Gohr    // check writability
3640d8850c4SAndreas Gohr    if(!@is_writable($p)){
3650d8850c4SAndreas Gohr        return '';
3660d8850c4SAndreas Gohr    }
3670d8850c4SAndreas Gohr
3680d8850c4SAndreas Gohr    // check accessability (execute bit) for directories
36979e79377SAndreas Gohr    if(@is_dir($p) && !file_exists("$p/.")){
3700d8850c4SAndreas Gohr        return '';
3710d8850c4SAndreas Gohr    }
3720d8850c4SAndreas Gohr
3730d8850c4SAndreas Gohr    return $p;
3740d8850c4SAndreas Gohr}
3758c4f28e8Sjan
376ed7b5f09Sandi/**
3771ca31cfeSAndreas Gohr * Sets the internal config values fperm and dperm which, when set,
3781ca31cfeSAndreas Gohr * will be used to change the permission of a newly created dir or
3791ca31cfeSAndreas Gohr * file with chmod. Considers the influence of the system's umask
3801ca31cfeSAndreas Gohr * setting the values only if needed.
3811ca31cfeSAndreas Gohr */
3821ca31cfeSAndreas Gohrfunction init_creationmodes(){
3831ca31cfeSAndreas Gohr    global $conf;
3841ca31cfeSAndreas Gohr
3851ca31cfeSAndreas Gohr    // Legacy support for old umask/dmask scheme
3861ca31cfeSAndreas Gohr    unset($conf['dmask']);
3871ca31cfeSAndreas Gohr    unset($conf['fmask']);
3881ca31cfeSAndreas Gohr    unset($conf['umask']);
3891ca31cfeSAndreas Gohr    unset($conf['fperm']);
3901ca31cfeSAndreas Gohr    unset($conf['dperm']);
3911ca31cfeSAndreas Gohr
3929f3cdec3SAndreas Gohr    // get system umask, fallback to 0 if none available
3939f3cdec3SAndreas Gohr    $umask = @umask();
3949f3cdec3SAndreas Gohr    if(!$umask) $umask = 0000;
3951ca31cfeSAndreas Gohr
3961ca31cfeSAndreas Gohr    // check what is set automatically by the system on file creation
3971ca31cfeSAndreas Gohr    // and set the fperm param if it's not what we want
3981ca31cfeSAndreas Gohr    $auto_fmode = 0666 & ~$umask;
3991ca31cfeSAndreas Gohr    if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode'];
4001ca31cfeSAndreas Gohr
4011ca31cfeSAndreas Gohr    // check what is set automatically by the system on file creation
4021ca31cfeSAndreas Gohr    // and set the dperm param if it's not what we want
4031ca31cfeSAndreas Gohr    $auto_dmode = $conf['dmode'] & ~$umask;
4041ca31cfeSAndreas Gohr    if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode'];
4051ca31cfeSAndreas Gohr}
4061ca31cfeSAndreas Gohr
4071ca31cfeSAndreas Gohr/**
408ed7b5f09Sandi * remove magic quotes recursivly
409ed7b5f09Sandi *
410ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
411*f50a239bSTakamura *
412*f50a239bSTakamura * @param $array
413ed7b5f09Sandi */
414ed7b5f09Sandifunction remove_magic_quotes(&$array) {
415ed7b5f09Sandi    foreach (array_keys($array) as $key) {
41681c54349SAndreas Gohr        // handle magic quotes in keynames (breaks order)
41781c54349SAndreas Gohr        $sk = stripslashes($key);
41881c54349SAndreas Gohr        if($sk != $key){
41981c54349SAndreas Gohr            $array[$sk] = $array[$key];
42081c54349SAndreas Gohr            unset($array[$key]);
42181c54349SAndreas Gohr            $key = $sk;
42281c54349SAndreas Gohr        }
42381c54349SAndreas Gohr
42481c54349SAndreas Gohr        // do recursion if needed
425ed7b5f09Sandi        if (is_array($array[$key])) {
426ed7b5f09Sandi            remove_magic_quotes($array[$key]);
427ed7b5f09Sandi        }else {
428ed7b5f09Sandi            $array[$key] = stripslashes($array[$key]);
429ed7b5f09Sandi        }
430ed7b5f09Sandi    }
431ed7b5f09Sandi}
432ed7b5f09Sandi
433ed7b5f09Sandi/**
434ed7b5f09Sandi * Returns the full absolute URL to the directory where
435ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash)
436ed7b5f09Sandi *
437585bf44eSChristopher Smith * !! Can not access $_SERVER values through $INPUT
438585bf44eSChristopher Smith * !! here as this function is called before $INPUT is
439585bf44eSChristopher Smith * !! initialized.
440585bf44eSChristopher Smith *
441ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
442*f50a239bSTakamura *
443*f50a239bSTakamura * @param null|string $abs
444*f50a239bSTakamura *
445*f50a239bSTakamura * @return string
446ed7b5f09Sandi */
4474b1a4e04SAndreas Gohrfunction getBaseURL($abs=null){
448ed7b5f09Sandi    global $conf;
449ed7b5f09Sandi    //if canonical url enabled always return absolute
4504b1a4e04SAndreas Gohr    if(is_null($abs)) $abs = $conf['canonical'];
451ed7b5f09Sandi
4521858e4d7SGerry Weißbach    if(!empty($conf['basedir'])){
45346c73e01SChris Smith        $dir = $conf['basedir'];
45489aa05dbSAndreas Gohr    }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){
45546c73e01SChris Smith        $dir = dirname($_SERVER['SCRIPT_NAME']);
45689aa05dbSAndreas Gohr    }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){
45746c73e01SChris Smith        $dir = dirname($_SERVER['PHP_SELF']);
458093ec9e4Sandi    }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
459093ec9e4Sandi        $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
460093ec9e4Sandi                $_SERVER['SCRIPT_FILENAME']);
46146c73e01SChris Smith        $dir = dirname('/'.$dir);
46292b83b77Sandi    }else{
46346c73e01SChris Smith        $dir = '.'; //probably wrong
46492b83b77Sandi    }
465ed7b5f09Sandi
46646c73e01SChris Smith    $dir = str_replace('\\','/',$dir);             // bugfix for weird WIN behaviour
46746c73e01SChris Smith    $dir = preg_replace('#//+#','/',"/$dir/");     // ensure leading and trailing slashes
468ed7b5f09Sandi
469f62ea8a1Sandi    //handle script in lib/exe dir
470f62ea8a1Sandi    $dir = preg_replace('!lib/exe/$!','',$dir);
471f62ea8a1Sandi
472488d5fa0SMichael Klier chi@chimeric.de    //handle script in lib/plugins dir
473488d5fa0SMichael Klier chi@chimeric.de    $dir = preg_replace('!lib/plugins/.*$!','',$dir);
474488d5fa0SMichael Klier chi@chimeric.de
475ed7b5f09Sandi    //finish here for relative URLs
476ed7b5f09Sandi    if(!$abs) return $dir;
477ed7b5f09Sandi
47846c73e01SChris Smith    //use config option if available, trim any slash from end of baseurl to avoid multiple consecutive slashes in the path
4791858e4d7SGerry Weißbach    if(!empty($conf['baseurl'])) return rtrim($conf['baseurl'],'/').$dir;
480ef7b3ecdSAndreas Gohr
481e82e3526SAndreas Gohr    //split hostheader into host and port
4825627186cSAndreas Gohr    if(isset($_SERVER['HTTP_HOST'])){
483204b27c8SMichael Hamann        $parsed_host = parse_url('http://'.$_SERVER['HTTP_HOST']);
484f87b5dbbSChristopher Smith        $host = isset($parsed_host['host']) ? $parsed_host['host'] : null;
485f87b5dbbSChristopher Smith        $port = isset($parsed_host['port']) ? $parsed_host['port'] : null;
4865627186cSAndreas Gohr    }elseif(isset($_SERVER['SERVER_NAME'])){
487204b27c8SMichael Hamann        $parsed_host = parse_url('http://'.$_SERVER['SERVER_NAME']);
488f87b5dbbSChristopher Smith        $host = isset($parsed_host['host']) ? $parsed_host['host'] : null;
489f87b5dbbSChristopher Smith        $port = isset($parsed_host['port']) ? $parsed_host['port'] : null;
4905627186cSAndreas Gohr    }else{
4915627186cSAndreas Gohr        $host = php_uname('n');
492c66972f2SAdrian Lang        $port = '';
4935627186cSAndreas Gohr    }
4945627186cSAndreas Gohr
495204b27c8SMichael Hamann    if(is_null($port)){
496204b27c8SMichael Hamann        $port = '';
497204b27c8SMichael Hamann    }
498204b27c8SMichael Hamann
499f5c6743cSAndreas Gohr    if(!is_ssl()){
500ed7b5f09Sandi        $proto = 'http://';
501e82e3526SAndreas Gohr        if ($port == '80') {
502ed7b5f09Sandi            $port = '';
503ed7b5f09Sandi        }
504ed7b5f09Sandi    }else{
505ed7b5f09Sandi        $proto = 'https://';
506e82e3526SAndreas Gohr        if ($port == '443') {
507ed7b5f09Sandi            $port = '';
508ed7b5f09Sandi        }
509ed7b5f09Sandi    }
510ed7b5f09Sandi
511c66972f2SAdrian Lang    if($port !== '') $port = ':'.$port;
512e82e3526SAndreas Gohr
513ed7b5f09Sandi    return $proto.$host.$port.$dir;
514ed7b5f09Sandi}
515ed7b5f09Sandi
516b000c6d4Sandi/**
517f5c6743cSAndreas Gohr * Check if accessed via HTTPS
518f5c6743cSAndreas Gohr *
519f5c6743cSAndreas Gohr * Apache leaves ,$_SERVER['HTTPS'] empty when not available, IIS sets it to 'off'.
520f5c6743cSAndreas Gohr * 'false' and 'disabled' are just guessing
521f5c6743cSAndreas Gohr *
522f5c6743cSAndreas Gohr * @returns bool true when SSL is active
523f5c6743cSAndreas Gohr */
524f5c6743cSAndreas Gohrfunction is_ssl(){
52519738e65SEnrico Tagliavini    // check if we are behind a reverse proxy
52619738e65SEnrico Tagliavini    if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
52719738e65SEnrico Tagliavini        if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
52819738e65SEnrico Tagliavini	    return true;
52919738e65SEnrico Tagliavini	} else {
53019738e65SEnrico Tagliavini	    return false;
53119738e65SEnrico Tagliavini	}
53219738e65SEnrico Tagliavini    }
533c66972f2SAdrian Lang    if (!isset($_SERVER['HTTPS']) ||
534c66972f2SAdrian Lang        preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){
535f5c6743cSAndreas Gohr        return false;
536f5c6743cSAndreas Gohr    }else{
537f5c6743cSAndreas Gohr        return true;
538f5c6743cSAndreas Gohr    }
539f5c6743cSAndreas Gohr}
540f5c6743cSAndreas Gohr
541f5c6743cSAndreas Gohr/**
5423816dcbcSAndreas Gohr * print a nice message even if no styles are loaded yet.
543*f50a239bSTakamura *
544*f50a239bSTakamura * @param integer|string $msg
5453816dcbcSAndreas Gohr */
5463816dcbcSAndreas Gohrfunction nice_die($msg){
5473816dcbcSAndreas Gohr    echo<<<EOT
548c8839c22SAnika Henke<!DOCTYPE html>
5493816dcbcSAndreas Gohr<html>
5503816dcbcSAndreas Gohr<head><title>DokuWiki Setup Error</title></head>
5513816dcbcSAndreas Gohr<body style="font-family: Arial, sans-serif">
5523816dcbcSAndreas Gohr    <div style="width:60%; margin: auto; background-color: #fcc;
5533816dcbcSAndreas Gohr                border: 1px solid #faa; padding: 0.5em 1em;">
5543816dcbcSAndreas Gohr        <h1 style="font-size: 120%">DokuWiki Setup Error</h1>
5553816dcbcSAndreas Gohr        <p>$msg</p>
5563816dcbcSAndreas Gohr    </div>
5573816dcbcSAndreas Gohr</body>
5583816dcbcSAndreas Gohr</html>
5593816dcbcSAndreas GohrEOT;
5600a4266d4SElan Ruusamäe    exit(1);
5613816dcbcSAndreas Gohr}
5623816dcbcSAndreas Gohr
56300976812SAndreas Gohr/**
56400976812SAndreas Gohr * A realpath() replacement
56500976812SAndreas Gohr *
56600976812SAndreas Gohr * This function behaves similar to PHP's realpath() but does not resolve
56700976812SAndreas Gohr * symlinks or accesses upper directories
56800976812SAndreas Gohr *
5694761d30cSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
57000976812SAndreas Gohr * @author <richpageau at yahoo dot co dot uk>
57159752844SAnders Sandblad * @link   http://php.net/manual/en/function.realpath.php#75992
572*f50a239bSTakamura *
573*f50a239bSTakamura * @param string $path
574*f50a239bSTakamura * @param bool $exists
575*f50a239bSTakamura *
576*f50a239bSTakamura * @return bool|string
57700976812SAndreas Gohr */
578b328697dSAndreas Gohrfunction fullpath($path,$exists=false){
5794761d30cSAndreas Gohr    static $run = 0;
5804761d30cSAndreas Gohr    $root  = '';
581f0a201c5SChris Smith    $iswin = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' || @$GLOBALS['DOKU_UNITTEST_ASSUME_WINDOWS']);
58200976812SAndreas Gohr
5834761d30cSAndreas Gohr    // find the (indestructable) root of the path - keeps windows stuff intact
5844761d30cSAndreas Gohr    if($path{0} == '/'){
5854761d30cSAndreas Gohr        $root = '/';
5864761d30cSAndreas Gohr    }elseif($iswin){
5874761d30cSAndreas Gohr        // match drive letter and UNC paths
5884761d30cSAndreas Gohr        if(preg_match('!^([a-zA-z]:)(.*)!',$path,$match)){
589b9c4302bSAndreas Gohr            $root = $match[1].'/';
5904761d30cSAndreas Gohr            $path = $match[2];
5914761d30cSAndreas Gohr        }else if(preg_match('!^(\\\\\\\\[^\\\\/]+\\\\[^\\\\/]+[\\\\/])(.*)!',$path,$match)){
5924761d30cSAndreas Gohr            $root = $match[1];
5934761d30cSAndreas Gohr            $path = $match[2];
59400976812SAndreas Gohr        }
5954761d30cSAndreas Gohr    }
5964761d30cSAndreas Gohr    $path = str_replace('\\','/',$path);
5974761d30cSAndreas Gohr
5984761d30cSAndreas Gohr    // if the given path wasn't absolute already, prepend the script path and retry
5994761d30cSAndreas Gohr    if(!$root){
6004761d30cSAndreas Gohr        $base = dirname($_SERVER['SCRIPT_FILENAME']);
6014761d30cSAndreas Gohr        $path = $base.'/'.$path;
6024761d30cSAndreas Gohr        if($run == 0){ // avoid endless recursion when base isn't absolute for some reason
6034761d30cSAndreas Gohr            $run++;
604b328697dSAndreas Gohr            return fullpath($path,$exists);
6054761d30cSAndreas Gohr        }
6064761d30cSAndreas Gohr    }
6074761d30cSAndreas Gohr    $run = 0;
60800976812SAndreas Gohr
60900976812SAndreas Gohr    // canonicalize
61000976812SAndreas Gohr    $path=explode('/', $path);
61100976812SAndreas Gohr    $newpath=array();
612ef38bfe8SAndreas Gohr    foreach($path as $p) {
613ef38bfe8SAndreas Gohr        if ($p === '' || $p === '.') continue;
614ef38bfe8SAndreas Gohr        if ($p==='..') {
61500976812SAndreas Gohr            array_pop($newpath);
61600976812SAndreas Gohr            continue;
61700976812SAndreas Gohr        }
618ef38bfe8SAndreas Gohr        array_push($newpath, $p);
61900976812SAndreas Gohr    }
6204761d30cSAndreas Gohr    $finalpath = $root.implode('/', $newpath);
62100976812SAndreas Gohr
6226b9c156cSAnika Henke    // check for existence when needed (except when unit testing)
62379e79377SAndreas Gohr    if($exists && !defined('DOKU_UNITTEST') && !file_exists($finalpath)) {
6244761d30cSAndreas Gohr        return false;
62500976812SAndreas Gohr    }
6264761d30cSAndreas Gohr    return $finalpath;
62700976812SAndreas Gohr}
62800976812SAndreas Gohr
629