xref: /dokuwiki/inc/init.php (revision c09f0eb1d9009ce0a7d2a12c41b125957604eff5)
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';
19e6266454SChris Smithif (@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
31d8186216SBen Coburnif (!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) {
68f8121585SChris Smith        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) {
82f8121585SChris Smith        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')) {
144*c09f0eb1SGerrit Uitslag    if(!defined('DOKU_SESSION_NAME'))     define ('DOKU_SESSION_NAME', "DokuWiki");
145*c09f0eb1SGerrit Uitslag    if(!defined('DOKU_SESSION_LIFETIME')) define ('DOKU_SESSION_LIFETIME', 0);
14673ab87deSGabriel Birke    $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir'];
147*c09f0eb1SGerrit Uitslag    if(!defined('DOKU_SESSION_PATH'))     define ('DOKU_SESSION_PATH', $cookieDir);
148*c09f0eb1SGerrit Uitslag    if(!defined('DOKU_SESSION_DOMAIN'))   define ('DOKU_SESSION_DOMAIN', '');
149*c09f0eb1SGerrit Uitslag
150*c09f0eb1SGerrit Uitslag    session_name(DOKU_SESSION_NAME);
151f5c6743cSAndreas Gohr    if(version_compare(PHP_VERSION, '5.2.0', '>')) {
152*c09f0eb1SGerrit Uitslag        session_set_cookie_params(DOKU_SESSION_LIFETIME, DOKU_SESSION_PATH, DOKU_SESSION_DOMAIN, ($conf['securecookie'] && is_ssl()), true);
153f5c6743cSAndreas Gohr    } else {
154*c09f0eb1SGerrit Uitslag        session_set_cookie_params(DOKU_SESSION_LIFETIME, DOKU_SESSION_PATH, DOKU_SESSION_DOMAIN, ($conf['securecookie'] && is_ssl()));
155f5c6743cSAndreas Gohr    }
156bad31ae9SAndreas Gohr    session_start();
15714a122deSAndreas Gohr
15814a122deSAndreas Gohr    // load left over messages
15914a122deSAndreas Gohr    if(isset($_SESSION[DOKU_COOKIE]['msg'])) {
16014a122deSAndreas Gohr        $MSG = $_SESSION[DOKU_COOKIE]['msg'];
16114a122deSAndreas Gohr        unset($_SESSION[DOKU_COOKIE]['msg']);
16214a122deSAndreas Gohr    }
163bad31ae9SAndreas Gohr}
164ed7b5f09Sandi
165ed7b5f09Sandi// kill magic quotes
166e55eb89cSAndreas Gohrif (get_magic_quotes_gpc() && !defined('MAGIC_QUOTES_STRIPPED')) {
167ed7b5f09Sandi    if (!empty($_GET))    remove_magic_quotes($_GET);
168ed7b5f09Sandi    if (!empty($_POST))   remove_magic_quotes($_POST);
169ed7b5f09Sandi    if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE);
170ed7b5f09Sandi    if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST);
1713fc74836Sandi    @ini_set('magic_quotes_gpc', 0);
172e55eb89cSAndreas Gohr    define('MAGIC_QUOTES_STRIPPED',1);
173ed7b5f09Sandi}
174c114d4c4SAndreas Gohrif(function_exists('set_magic_quotes_runtime')) @set_magic_quotes_runtime(0);
1753fc74836Sandi@ini_set('magic_quotes_sybase',0);
176ed7b5f09Sandi
177a1637ffdSAndreas Gohr// don't let cookies ever interfere with request vars
178a1637ffdSAndreas Gohr$_REQUEST = array_merge($_GET,$_POST);
179a1637ffdSAndreas Gohr
1803dea4ebcSAndreas Gohr// we don't want a purge URL to be digged
181c66972f2SAdrian Langif(isset($_REQUEST['purge']) && $_SERVER['HTTP_REFERER']) unset($_REQUEST['purge']);
1823dea4ebcSAndreas Gohr
183ed7b5f09Sandi// disable gzip if not available
1848a447e5cSAndreas Gohrif($conf['compression'] == 'bz2' && !function_exists('bzopen')){
185fe893490SAndreas Gohr    $conf['compression'] = 'gz';
186501252a5SAndreas Gohr}
187fe893490SAndreas Gohrif($conf['compression'] == 'gz' && !function_exists('gzopen')){
188501252a5SAndreas Gohr    $conf['compression'] = 0;
189ed7b5f09Sandi}
190ed7b5f09Sandi
1911ca31cfeSAndreas Gohr// precalculate file creation modes
1921ca31cfeSAndreas Gohrinit_creationmodes();
193ed7b5f09Sandi
1943dc3a5f1Sandi// make real paths and check them
19598407a7aSandiinit_paths();
1967367b368SAndreas Gohrinit_files();
197ed7b5f09Sandi
198f1986589SMichael Klier// setup plugin controller class (can be overwritten in preload.php)
19993a7873eSAndreas Gohr$plugin_types = array('auth', 'admin','syntax','action','renderer', 'helper','remote');
200f1986589SMichael Klierglobal $plugin_controller_class, $plugin_controller;
201f1986589SMichael Klierif (empty($plugin_controller_class)) $plugin_controller_class = 'Doku_Plugin_Controller';
202f1986589SMichael Klier
203c7cb395cSAdrian Lang// load libraries
204c7cb395cSAdrian Langrequire_once(DOKU_INC.'inc/load.php');
205c7cb395cSAdrian Lang
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/**
23098407a7aSandi * Checks paths from config file
23198407a7aSandi */
23298407a7aSandifunction init_paths(){
23398407a7aSandi    global $conf;
23498407a7aSandi
23598407a7aSandi    $paths = array('datadir'   => 'pages',
23698407a7aSandi            'olddir'    => 'attic',
23798407a7aSandi            'mediadir'  => 'media',
238e4f389efSKate Arzamastseva            'mediaolddir' => 'media_attic',
23998407a7aSandi            'metadir'   => 'meta',
240e4f389efSKate Arzamastseva            'mediametadir' => 'media_meta',
24198407a7aSandi            'cachedir'  => 'cache',
242579b0f7eSTNHarris            'indexdir'  => 'index',
243de33a58fSMichael Klier            'lockdir'   => 'locks',
244de33a58fSMichael Klier            'tmpdir'    => 'tmp');
24598407a7aSandi
24698407a7aSandi    foreach($paths as $c => $p) {
2477f086b67SAnika Henke        $path = empty($conf[$c]) ? $conf['savedir'].'/'.$p : $conf[$c];
2486b9c156cSAnika Henke        $conf[$c] = init_path($path);
2496b9c156cSAnika Henke        if(empty($conf[$c]))
2506b9c156cSAnika Henke            nice_die("The $c ('$p') at $path is not found, isn't accessible or writable.
25169dc3177SAndreas Gohr                You should check your config and permission settings.
25269dc3177SAndreas Gohr                Or maybe you want to <a href=\"install.php\">run the
25369dc3177SAndreas Gohr                installer</a>?");
25498407a7aSandi    }
25571726d78SBen Coburn
25671726d78SBen Coburn    // path to old changelog only needed for upgrading
25771726d78SBen Coburn    $conf['changelog_old'] = init_path((isset($conf['changelog']))?($conf['changelog']):($conf['savedir'].'/changes.log'));
25871726d78SBen Coburn    if ($conf['changelog_old']=='') { unset($conf['changelog_old']); }
25971726d78SBen Coburn    // hardcoded changelog because it is now a cache that lives in meta
26071726d78SBen Coburn    $conf['changelog'] = $conf['metadir'].'/_dokuwiki.changes';
26199c8d7f2Smichael    $conf['media_changelog'] = $conf['metadir'].'/_media.changes';
26298407a7aSandi}
26398407a7aSandi
2646d06b26aSDominik Eckelmannfunction init_lang($langCode) {
2656d06b26aSDominik Eckelmann    //prepare language array
2666d06b26aSDominik Eckelmann    global $lang;
2676d06b26aSDominik Eckelmann    $lang = array();
2686d06b26aSDominik Eckelmann
2696d06b26aSDominik Eckelmann    //load the language files
2701d82c8d3SChristopher Smith    require(DOKU_INC.'inc/lang/en/lang.php');
2716d06b26aSDominik Eckelmann    if ($langCode && $langCode != 'en') {
2726d06b26aSDominik Eckelmann        if (file_exists(DOKU_INC."inc/lang/$langCode/lang.php")) {
2731d82c8d3SChristopher Smith            require(DOKU_INC."inc/lang/$langCode/lang.php");
2746d06b26aSDominik Eckelmann        }
2756d06b26aSDominik Eckelmann    }
2766d06b26aSDominik Eckelmann}
2776d06b26aSDominik Eckelmann
27898407a7aSandi/**
2796b9c156cSAnika Henke * Checks the existence of certain files and creates them if missing.
2807367b368SAndreas Gohr */
2817367b368SAndreas Gohrfunction init_files(){
2827367b368SAndreas Gohr    global $conf;
2830d8850c4SAndreas Gohr
284345b1674SAndreas Gohr    $files = array($conf['indexdir'].'/page.idx');
2857367b368SAndreas Gohr
2867367b368SAndreas Gohr    foreach($files as $file){
2877367b368SAndreas Gohr        if(!@file_exists($file)){
2880d8850c4SAndreas Gohr            $fh = @fopen($file,'a');
2890d8850c4SAndreas Gohr            if($fh){
2907367b368SAndreas Gohr                fclose($fh);
291443e135dSChristopher Smith                if(!empty($conf['fperm'])) chmod($file, $conf['fperm']);
2920d8850c4SAndreas Gohr            }else{
2933816dcbcSAndreas Gohr                nice_die("$file is not writable. Check your permissions settings!");
2940d8850c4SAndreas Gohr            }
2957367b368SAndreas Gohr        }
2967367b368SAndreas Gohr    }
297345b1674SAndreas Gohr
298345b1674SAndreas Gohr    # create title index (needs to have same length as page.idx)
2999b41be24STom N Harris    /*
300345b1674SAndreas Gohr    $file = $conf['indexdir'].'/title.idx';
301345b1674SAndreas Gohr    if(!@file_exists($file)){
302345b1674SAndreas Gohr        $pages = file($conf['indexdir'].'/page.idx');
303345b1674SAndreas Gohr        $pages = count($pages);
304345b1674SAndreas Gohr        $fh = @fopen($file,'a');
305345b1674SAndreas Gohr        if($fh){
306345b1674SAndreas Gohr            for($i=0; $i<$pages; $i++){
307345b1674SAndreas Gohr                fwrite($fh,"\n");
308345b1674SAndreas Gohr            }
309345b1674SAndreas Gohr            fclose($fh);
310345b1674SAndreas Gohr        }else{
311345b1674SAndreas Gohr            nice_die("$file is not writable. Check your permissions settings!");
312345b1674SAndreas Gohr        }
313345b1674SAndreas Gohr    }
3149b41be24STom N Harris    */
3157367b368SAndreas Gohr}
3167367b368SAndreas Gohr
3177367b368SAndreas Gohr/**
3180d8850c4SAndreas Gohr * Returns absolute path
319f62ea8a1Sandi *
3200d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC.
3217f086b67SAnika Henke * Check for accessibility on directories as well.
3220d8850c4SAndreas Gohr *
3230d8850c4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
324f62ea8a1Sandi */
325f62ea8a1Sandifunction init_path($path){
3266b9c156cSAnika Henke    // check existence
32700976812SAndreas Gohr    $p = fullpath($path);
3280d8850c4SAndreas Gohr    if(!@file_exists($p)){
32900976812SAndreas Gohr        $p = fullpath(DOKU_INC.$path);
3300d8850c4SAndreas Gohr        if(!@file_exists($p)){
3318fc4e739Sandi            return '';
332f62ea8a1Sandi        }
3330d8850c4SAndreas Gohr    }
3340d8850c4SAndreas Gohr
3350d8850c4SAndreas Gohr    // check writability
3360d8850c4SAndreas Gohr    if(!@is_writable($p)){
3370d8850c4SAndreas Gohr        return '';
3380d8850c4SAndreas Gohr    }
3390d8850c4SAndreas Gohr
3400d8850c4SAndreas Gohr    // check accessability (execute bit) for directories
3410d8850c4SAndreas Gohr    if(@is_dir($p) && !@file_exists("$p/.")){
3420d8850c4SAndreas Gohr        return '';
3430d8850c4SAndreas Gohr    }
3440d8850c4SAndreas Gohr
3450d8850c4SAndreas Gohr    return $p;
3460d8850c4SAndreas Gohr}
3478c4f28e8Sjan
348ed7b5f09Sandi/**
3491ca31cfeSAndreas Gohr * Sets the internal config values fperm and dperm which, when set,
3501ca31cfeSAndreas Gohr * will be used to change the permission of a newly created dir or
3511ca31cfeSAndreas Gohr * file with chmod. Considers the influence of the system's umask
3521ca31cfeSAndreas Gohr * setting the values only if needed.
3531ca31cfeSAndreas Gohr */
3541ca31cfeSAndreas Gohrfunction init_creationmodes(){
3551ca31cfeSAndreas Gohr    global $conf;
3561ca31cfeSAndreas Gohr
3571ca31cfeSAndreas Gohr    // Legacy support for old umask/dmask scheme
3581ca31cfeSAndreas Gohr    unset($conf['dmask']);
3591ca31cfeSAndreas Gohr    unset($conf['fmask']);
3601ca31cfeSAndreas Gohr    unset($conf['umask']);
3611ca31cfeSAndreas Gohr    unset($conf['fperm']);
3621ca31cfeSAndreas Gohr    unset($conf['dperm']);
3631ca31cfeSAndreas Gohr
3649f3cdec3SAndreas Gohr    // get system umask, fallback to 0 if none available
3659f3cdec3SAndreas Gohr    $umask = @umask();
3669f3cdec3SAndreas Gohr    if(!$umask) $umask = 0000;
3671ca31cfeSAndreas Gohr
3681ca31cfeSAndreas Gohr    // check what is set automatically by the system on file creation
3691ca31cfeSAndreas Gohr    // and set the fperm param if it's not what we want
3701ca31cfeSAndreas Gohr    $auto_fmode = 0666 & ~$umask;
3711ca31cfeSAndreas Gohr    if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode'];
3721ca31cfeSAndreas Gohr
3731ca31cfeSAndreas Gohr    // check what is set automatically by the system on file creation
3741ca31cfeSAndreas Gohr    // and set the dperm param if it's not what we want
3751ca31cfeSAndreas Gohr    $auto_dmode = $conf['dmode'] & ~$umask;
3761ca31cfeSAndreas Gohr    if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode'];
3771ca31cfeSAndreas Gohr}
3781ca31cfeSAndreas Gohr
3791ca31cfeSAndreas Gohr/**
380ed7b5f09Sandi * remove magic quotes recursivly
381ed7b5f09Sandi *
382ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
383ed7b5f09Sandi */
384ed7b5f09Sandifunction remove_magic_quotes(&$array) {
385ed7b5f09Sandi    foreach (array_keys($array) as $key) {
38681c54349SAndreas Gohr        // handle magic quotes in keynames (breaks order)
38781c54349SAndreas Gohr        $sk = stripslashes($key);
38881c54349SAndreas Gohr        if($sk != $key){
38981c54349SAndreas Gohr            $array[$sk] = $array[$key];
39081c54349SAndreas Gohr            unset($array[$key]);
39181c54349SAndreas Gohr            $key = $sk;
39281c54349SAndreas Gohr        }
39381c54349SAndreas Gohr
39481c54349SAndreas Gohr        // do recursion if needed
395ed7b5f09Sandi        if (is_array($array[$key])) {
396ed7b5f09Sandi            remove_magic_quotes($array[$key]);
397ed7b5f09Sandi        }else {
398ed7b5f09Sandi            $array[$key] = stripslashes($array[$key]);
399ed7b5f09Sandi        }
400ed7b5f09Sandi    }
401ed7b5f09Sandi}
402ed7b5f09Sandi
403ed7b5f09Sandi/**
404ed7b5f09Sandi * Returns the full absolute URL to the directory where
405ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash)
406ed7b5f09Sandi *
407ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
408ed7b5f09Sandi */
4094b1a4e04SAndreas Gohrfunction getBaseURL($abs=null){
410ed7b5f09Sandi    global $conf;
411ed7b5f09Sandi    //if canonical url enabled always return absolute
4124b1a4e04SAndreas Gohr    if(is_null($abs)) $abs = $conf['canonical'];
413ed7b5f09Sandi
41492b83b77Sandi    if($conf['basedir']){
41546c73e01SChris Smith        $dir = $conf['basedir'];
41689aa05dbSAndreas Gohr    }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){
41746c73e01SChris Smith        $dir = dirname($_SERVER['SCRIPT_NAME']);
41889aa05dbSAndreas Gohr    }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){
41946c73e01SChris Smith        $dir = dirname($_SERVER['PHP_SELF']);
420093ec9e4Sandi    }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
421093ec9e4Sandi        $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
422093ec9e4Sandi                $_SERVER['SCRIPT_FILENAME']);
42346c73e01SChris Smith        $dir = dirname('/'.$dir);
42492b83b77Sandi    }else{
42546c73e01SChris Smith        $dir = '.'; //probably wrong
42692b83b77Sandi    }
427ed7b5f09Sandi
42846c73e01SChris Smith    $dir = str_replace('\\','/',$dir);             // bugfix for weird WIN behaviour
42946c73e01SChris Smith    $dir = preg_replace('#//+#','/',"/$dir/");     // ensure leading and trailing slashes
430ed7b5f09Sandi
431f62ea8a1Sandi    //handle script in lib/exe dir
432f62ea8a1Sandi    $dir = preg_replace('!lib/exe/$!','',$dir);
433f62ea8a1Sandi
434488d5fa0SMichael Klier chi@chimeric.de    //handle script in lib/plugins dir
435488d5fa0SMichael Klier chi@chimeric.de    $dir = preg_replace('!lib/plugins/.*$!','',$dir);
436488d5fa0SMichael Klier chi@chimeric.de
437ed7b5f09Sandi    //finish here for relative URLs
438ed7b5f09Sandi    if(!$abs) return $dir;
439ed7b5f09Sandi
44046c73e01SChris Smith    //use config option if available, trim any slash from end of baseurl to avoid multiple consecutive slashes in the path
44146c73e01SChris Smith    if($conf['baseurl']) return rtrim($conf['baseurl'],'/').$dir;
442ef7b3ecdSAndreas Gohr
443e82e3526SAndreas Gohr    //split hostheader into host and port
4445627186cSAndreas Gohr    if(isset($_SERVER['HTTP_HOST'])){
445204b27c8SMichael Hamann        $parsed_host = parse_url('http://'.$_SERVER['HTTP_HOST']);
446204b27c8SMichael Hamann        $host = $parsed_host['host'];
447204b27c8SMichael Hamann        $port = $parsed_host['port'];
4485627186cSAndreas Gohr    }elseif(isset($_SERVER['SERVER_NAME'])){
449204b27c8SMichael Hamann        $parsed_host = parse_url('http://'.$_SERVER['SERVER_NAME']);
450204b27c8SMichael Hamann        $host = $parsed_host['host'];
451204b27c8SMichael Hamann        $port = $parsed_host['port'];
4525627186cSAndreas Gohr    }else{
4535627186cSAndreas Gohr        $host = php_uname('n');
454c66972f2SAdrian Lang        $port = '';
4555627186cSAndreas Gohr    }
4565627186cSAndreas Gohr
4575627186cSAndreas Gohr    if(!$port && isset($_SERVER['SERVER_PORT'])) {
458c66972f2SAdrian Lang        $port = $_SERVER['SERVER_PORT'];
459c66972f2SAdrian Lang    }
460204b27c8SMichael Hamann
461204b27c8SMichael Hamann    if(is_null($port)){
462204b27c8SMichael Hamann        $port = '';
463204b27c8SMichael Hamann    }
464204b27c8SMichael Hamann
465f5c6743cSAndreas Gohr    if(!is_ssl()){
466ed7b5f09Sandi        $proto = 'http://';
467e82e3526SAndreas Gohr        if ($port == '80') {
468ed7b5f09Sandi            $port = '';
469ed7b5f09Sandi        }
470ed7b5f09Sandi    }else{
471ed7b5f09Sandi        $proto = 'https://';
472e82e3526SAndreas Gohr        if ($port == '443') {
473ed7b5f09Sandi            $port = '';
474ed7b5f09Sandi        }
475ed7b5f09Sandi    }
476ed7b5f09Sandi
477c66972f2SAdrian Lang    if($port !== '') $port = ':'.$port;
478e82e3526SAndreas Gohr
479ed7b5f09Sandi    return $proto.$host.$port.$dir;
480ed7b5f09Sandi}
481ed7b5f09Sandi
482b000c6d4Sandi/**
483f5c6743cSAndreas Gohr * Check if accessed via HTTPS
484f5c6743cSAndreas Gohr *
485f5c6743cSAndreas Gohr * Apache leaves ,$_SERVER['HTTPS'] empty when not available, IIS sets it to 'off'.
486f5c6743cSAndreas Gohr * 'false' and 'disabled' are just guessing
487f5c6743cSAndreas Gohr *
488f5c6743cSAndreas Gohr * @returns bool true when SSL is active
489f5c6743cSAndreas Gohr */
490f5c6743cSAndreas Gohrfunction is_ssl(){
491c66972f2SAdrian Lang    if (!isset($_SERVER['HTTPS']) ||
492c66972f2SAdrian Lang        preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){
493f5c6743cSAndreas Gohr        return false;
494f5c6743cSAndreas Gohr    }else{
495f5c6743cSAndreas Gohr        return true;
496f5c6743cSAndreas Gohr    }
497f5c6743cSAndreas Gohr}
498f5c6743cSAndreas Gohr
499f5c6743cSAndreas Gohr/**
5003816dcbcSAndreas Gohr * print a nice message even if no styles are loaded yet.
5013816dcbcSAndreas Gohr */
5023816dcbcSAndreas Gohrfunction nice_die($msg){
5033816dcbcSAndreas Gohr    echo<<<EOT
504c8839c22SAnika Henke<!DOCTYPE html>
5053816dcbcSAndreas Gohr<html>
5063816dcbcSAndreas Gohr<head><title>DokuWiki Setup Error</title></head>
5073816dcbcSAndreas Gohr<body style="font-family: Arial, sans-serif">
5083816dcbcSAndreas Gohr    <div style="width:60%; margin: auto; background-color: #fcc;
5093816dcbcSAndreas Gohr                border: 1px solid #faa; padding: 0.5em 1em;">
5103816dcbcSAndreas Gohr        <h1 style="font-size: 120%">DokuWiki Setup Error</h1>
5113816dcbcSAndreas Gohr        <p>$msg</p>
5123816dcbcSAndreas Gohr    </div>
5133816dcbcSAndreas Gohr</body>
5143816dcbcSAndreas Gohr</html>
5153816dcbcSAndreas GohrEOT;
5163816dcbcSAndreas Gohr    exit;
5173816dcbcSAndreas Gohr}
5183816dcbcSAndreas Gohr
51900976812SAndreas Gohr/**
52000976812SAndreas Gohr * A realpath() replacement
52100976812SAndreas Gohr *
52200976812SAndreas Gohr * This function behaves similar to PHP's realpath() but does not resolve
52300976812SAndreas Gohr * symlinks or accesses upper directories
52400976812SAndreas Gohr *
5254761d30cSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
52600976812SAndreas Gohr * @author <richpageau at yahoo dot co dot uk>
52700976812SAndreas Gohr * @link   http://de3.php.net/manual/en/function.realpath.php#75992
52800976812SAndreas Gohr */
529b328697dSAndreas Gohrfunction fullpath($path,$exists=false){
5304761d30cSAndreas Gohr    static $run = 0;
5314761d30cSAndreas Gohr    $root  = '';
532f0a201c5SChris Smith    $iswin = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' || @$GLOBALS['DOKU_UNITTEST_ASSUME_WINDOWS']);
53300976812SAndreas Gohr
5344761d30cSAndreas Gohr    // find the (indestructable) root of the path - keeps windows stuff intact
5354761d30cSAndreas Gohr    if($path{0} == '/'){
5364761d30cSAndreas Gohr        $root = '/';
5374761d30cSAndreas Gohr    }elseif($iswin){
5384761d30cSAndreas Gohr        // match drive letter and UNC paths
5394761d30cSAndreas Gohr        if(preg_match('!^([a-zA-z]:)(.*)!',$path,$match)){
540b9c4302bSAndreas Gohr            $root = $match[1].'/';
5414761d30cSAndreas Gohr            $path = $match[2];
5424761d30cSAndreas Gohr        }else if(preg_match('!^(\\\\\\\\[^\\\\/]+\\\\[^\\\\/]+[\\\\/])(.*)!',$path,$match)){
5434761d30cSAndreas Gohr            $root = $match[1];
5444761d30cSAndreas Gohr            $path = $match[2];
54500976812SAndreas Gohr        }
5464761d30cSAndreas Gohr    }
5474761d30cSAndreas Gohr    $path = str_replace('\\','/',$path);
5484761d30cSAndreas Gohr
5494761d30cSAndreas Gohr    // if the given path wasn't absolute already, prepend the script path and retry
5504761d30cSAndreas Gohr    if(!$root){
5514761d30cSAndreas Gohr        $base = dirname($_SERVER['SCRIPT_FILENAME']);
5524761d30cSAndreas Gohr        $path = $base.'/'.$path;
5534761d30cSAndreas Gohr        if($run == 0){ // avoid endless recursion when base isn't absolute for some reason
5544761d30cSAndreas Gohr            $run++;
555b328697dSAndreas Gohr            return fullpath($path,$exists);
5564761d30cSAndreas Gohr        }
5574761d30cSAndreas Gohr    }
5584761d30cSAndreas Gohr    $run = 0;
55900976812SAndreas Gohr
56000976812SAndreas Gohr    // canonicalize
56100976812SAndreas Gohr    $path=explode('/', $path);
56200976812SAndreas Gohr    $newpath=array();
563ef38bfe8SAndreas Gohr    foreach($path as $p) {
564ef38bfe8SAndreas Gohr        if ($p === '' || $p === '.') continue;
565ef38bfe8SAndreas Gohr        if ($p==='..') {
56600976812SAndreas Gohr            array_pop($newpath);
56700976812SAndreas Gohr            continue;
56800976812SAndreas Gohr        }
569ef38bfe8SAndreas Gohr        array_push($newpath, $p);
57000976812SAndreas Gohr    }
5714761d30cSAndreas Gohr    $finalpath = $root.implode('/', $newpath);
57200976812SAndreas Gohr
5736b9c156cSAnika Henke    // check for existence when needed (except when unit testing)
574b328697dSAndreas Gohr    if($exists && !defined('DOKU_UNITTEST') && !@file_exists($finalpath)) {
5754761d30cSAndreas Gohr        return false;
57600976812SAndreas Gohr    }
5774761d30cSAndreas Gohr    return $finalpath;
57800976812SAndreas Gohr}
57900976812SAndreas Gohr
580