xref: /dokuwiki/inc/init.php (revision e6a6dbfe6cfcfaf3fb0992350ea7769faa762116)
1ed7b5f09Sandi<?php
2ed7b5f09Sandi/**
3ed7b5f09Sandi * Initialize some defaults needed for DokuWiki
4ed7b5f09Sandi */
5ed7b5f09Sandi
6a609a9ccSBen Coburn// start timing Dokuwiki execution
7a609a9ccSBen Coburnfunction delta_time($start=0) {
8a609a9ccSBen Coburn    list($usec, $sec) = explode(" ", microtime());
9a609a9ccSBen Coburn    return ((float)$usec+(float)$sec)-((float)$start);
10a609a9ccSBen Coburn}
11a609a9ccSBen Coburndefine('DOKU_START_TIME', delta_time());
12a609a9ccSBen Coburn
13ccaeaa85SAndreas Gohrglobal $config_cascade;
14ccaeaa85SAndreas Gohr$config_cascade = '';
15ccaeaa85SAndreas Gohr
1648beefecSAndreas Gohr// if available load a preload config file
17e6266454SChris Smith$preload = fullpath(dirname(__FILE__)).'/preload.php';
18e6266454SChris Smithif (@file_exists($preload)) include($preload);
1948beefecSAndreas Gohr
20ed7b5f09Sandi// define the include path
2100976812SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../').'/');
22ad15db82Sandi
23c2a6d816SAndreas Gohr// define Plugin dir
24c2a6d816SAndreas Gohrif(!defined('DOKU_PLUGIN'))  define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
25c2a6d816SAndreas Gohr
26e7cb32dcSAndreas Gohr// define config path (packagers may want to change this to /etc/dokuwiki/)
27b7551a6dSEsther Brunnerif(!defined('DOKU_CONF')) define('DOKU_CONF',DOKU_INC.'conf/');
28e7cb32dcSAndreas Gohr
29bad905f1SBen Coburn// check for error reporting override or set error reporting to sane values
30d8186216SBen Coburnif (!defined('DOKU_E_LEVEL') && @file_exists(DOKU_CONF.'report_e_all')) {
31bad905f1SBen Coburn    define('DOKU_E_LEVEL', E_ALL);
32bad905f1SBen Coburn}
33fc80ed59SAndreas Gohrif (!defined('DOKU_E_LEVEL')) {
34fc80ed59SAndreas Gohr    if(defined('E_DEPRECATED')){ // since php 5.3
35fc80ed59SAndreas Gohr        error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
36fc80ed59SAndreas Gohr    }else{
37fc80ed59SAndreas Gohr        error_reporting(E_ALL ^ E_NOTICE);
38fc80ed59SAndreas Gohr    }
39fc80ed59SAndreas Gohr} else {
40fc80ed59SAndreas Gohr    error_reporting(DOKU_E_LEVEL);
41fc80ed59SAndreas Gohr}
42c53ea5f2Sandi
4350602150SBen Coburn// init memory caches
44db959ae3SAndreas Gohrglobal $cache_revinfo;
45db959ae3SAndreas Gohr       $cache_revinfo = array();
46db959ae3SAndreas Gohrglobal $cache_wikifn;
47db959ae3SAndreas Gohr       $cache_wikifn = array();
48db959ae3SAndreas Gohrglobal $cache_cleanid;
49db959ae3SAndreas Gohr       $cache_cleanid = array();
50db959ae3SAndreas Gohrglobal $cache_authname;
51db959ae3SAndreas Gohr       $cache_authname = array();
52db959ae3SAndreas Gohrglobal $cache_metadata;
53db959ae3SAndreas Gohr       $cache_metadata = array();
5450602150SBen Coburn
55cb043f52SChris Smith//set the configuration cascade - but only if its not already been set in preload.php
56cb043f52SChris Smithif (empty($config_cascade)) {
57*e6a6dbfeSAndreas Gohr    include(DOKU_INC.'inc/config_cascade.php');
58cb043f52SChris Smith}
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
74ad15db82Sandi//prepare language array
75ee20e7d1Sandiglobal $lang;
76ad15db82Sandi$lang = array();
77ed7b5f09Sandi
7816521111Sandi//load the language files
79bc3b6aecSandirequire_once(DOKU_INC.'inc/lang/en/lang.php');
80f949a01cSAndreas Gohrif ( $conf['lang'] && $conf['lang'] != 'en' ) {
81bc3b6aecSandi    require_once(DOKU_INC.'inc/lang/'.$conf['lang'].'/lang.php');
82fc1c55b1Shfuecks}
8316521111Sandi
84066fee30SAndreas Gohr//prepare license array()
85066fee30SAndreas Gohrglobal $license;
86066fee30SAndreas Gohr$license = array();
87066fee30SAndreas Gohr
88066fee30SAndreas Gohr// load the license file(s)
89f8121585SChris Smithforeach (array('default','local') as $config_group) {
90f8121585SChris Smith    if (empty($config_cascade['license'][$config_group])) continue;
91f8121585SChris Smith    foreach ($config_cascade['license'][$config_group] as $config_file) {
92f8121585SChris Smith        if(@file_exists($config_file)){
93f8121585SChris Smith            include($config_file);
94f8121585SChris Smith        }
95f8121585SChris Smith    }
96066fee30SAndreas Gohr}
97066fee30SAndreas Gohr
981f8eb24fSAndreas Gohr// set timezone (as in pre 5.3.0 days)
991f8eb24fSAndreas Gohrdate_default_timezone_set(@date_default_timezone_get());
1001f8eb24fSAndreas Gohr
101ed7b5f09Sandi// define baseURL
1024b1a4e04SAndreas Gohrif(!defined('DOKU_REL')) define('DOKU_REL',getBaseURL(false));
103ed7b5f09Sandiif(!defined('DOKU_URL')) define('DOKU_URL',getBaseURL(true));
1044b1a4e04SAndreas Gohrif(!defined('DOKU_BASE')){
1054b1a4e04SAndreas Gohr    if($conf['canonical']){
1064b1a4e04SAndreas Gohr        define('DOKU_BASE',DOKU_URL);
1074b1a4e04SAndreas Gohr    }else{
1084b1a4e04SAndreas Gohr        define('DOKU_BASE',DOKU_REL);
1094b1a4e04SAndreas Gohr    }
1104b1a4e04SAndreas Gohr}
1114b1a4e04SAndreas Gohr
112b8595a66SAndreas Gohr// define whitespace
113b8595a66SAndreas Gohrif(!defined('DOKU_LF')) define ('DOKU_LF',"\n");
114b8595a66SAndreas Gohrif(!defined('DOKU_TAB')) define ('DOKU_TAB',"\t");
115ed7b5f09Sandi
116656c8fb3SAndreas Gohr// define cookie and session id, append server port when securecookie is configured FS#1664
117656c8fb3SAndreas Gohrif (!defined('DOKU_COOKIE')) define('DOKU_COOKIE', 'DW'.md5(DOKU_REL.(($conf['securecookie'])?$_SERVER['SERVER_PORT']:'')));
118e71ce681SAndreas Gohr
119ee20e7d1Sandi
120ed7b5f09Sandi// define main script
121ed7b5f09Sandiif(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php');
122ed7b5f09Sandi
1236b13307fSandi// define Template baseURL
1246b13307fSandiif(!defined('DOKU_TPL')) define('DOKU_TPL',
125f62ea8a1Sandi        DOKU_BASE.'lib/tpl/'.$conf['template'].'/');
1266b13307fSandi
12778a6aeb1SAndreas Gohr// define real Template directory
12878a6aeb1SAndreas Gohrif(!defined('DOKU_TPLINC')) define('DOKU_TPLINC',
12978a6aeb1SAndreas Gohr        DOKU_INC.'lib/tpl/'.$conf['template'].'/');
13078a6aeb1SAndreas Gohr
131ed7b5f09Sandi// make session rewrites XHTML compliant
1323fc74836Sandi@ini_set('arg_separator.output', '&amp;');
133ed7b5f09Sandi
134d7e6bba9SAndreas Gohr// make sure global zlib does not interfere FS#1132
135d7e6bba9SAndreas Gohr@ini_set('zlib.output_compression', 'off');
136d7e6bba9SAndreas Gohr
1376deb5405SAndreas Gohr// increase PCRE backtrack limit
1386deb5405SAndreas Gohr@ini_set('pcre.backtrack_limit', '20971520');
1396deb5405SAndreas Gohr
14098bda4fdSAndreas Gohr// enable gzip compression if supported
14198bda4fdSAndreas Gohr$conf['gzip_output'] &= (strpos($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip') !== false);
1423138b5c7SAndreas Gohrif ($conf['gzip_output'] &&
1433138b5c7SAndreas Gohr        !defined('DOKU_DISABLE_GZIP_OUTPUT') &&
14498bda4fdSAndreas Gohr        function_exists('ob_gzhandler')) {
1453138b5c7SAndreas Gohr    ob_start('ob_gzhandler');
1463138b5c7SAndreas Gohr}
1473138b5c7SAndreas Gohr
148ed7b5f09Sandi// init session
1496534245aSAndreas Gohrif (!headers_sent() && !defined('NOSESSION')){
150ed7b5f09Sandi    session_name("DokuWiki");
151f5c6743cSAndreas Gohr    if (version_compare(PHP_VERSION, '5.2.0', '>')) {
152f5c6743cSAndreas Gohr        session_set_cookie_params(0,DOKU_REL,'',($conf['securecookie'] && is_ssl()),true);
153f5c6743cSAndreas Gohr    }else{
154f5c6743cSAndreas Gohr        session_set_cookie_params(0,DOKU_REL,'',($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}
1743fc74836Sandi@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
191e656dcd4SAndreas Gohr// fix dateformat for upgraders
192e656dcd4SAndreas Gohrif(strpos($conf['dformat'],'%') === false){
193e656dcd4SAndreas Gohr    $conf['dformat'] = '%Y/%m/%d %H:%M';
194e656dcd4SAndreas Gohr}
195e656dcd4SAndreas Gohr
1961ca31cfeSAndreas Gohr// precalculate file creation modes
1971ca31cfeSAndreas Gohrinit_creationmodes();
198ed7b5f09Sandi
1993dc3a5f1Sandi// make real paths and check them
20098407a7aSandiinit_paths();
2017367b368SAndreas Gohrinit_files();
202ed7b5f09Sandi
2038c4f28e8Sjan// automatic upgrade to script versions of certain files
204e7cb32dcSAndreas Gohrscriptify(DOKU_CONF.'users.auth');
205e7cb32dcSAndreas Gohrscriptify(DOKU_CONF.'acl.auth');
206f62ea8a1Sandi
207f1986589SMichael Klier// setup plugin controller class (can be overwritten in preload.php)
208f1986589SMichael Klier$plugin_types = array('admin','syntax','action','renderer', 'helper');
209f1986589SMichael Klierglobal $plugin_controller_class, $plugin_controller;
210f1986589SMichael Klierif (empty($plugin_controller_class)) $plugin_controller_class = 'Doku_Plugin_Controller';
211f1986589SMichael Klier
212c7cb395cSAdrian Lang// load libraries
213c7cb395cSAdrian Langrequire_once(DOKU_INC.'inc/load.php');
214c7cb395cSAdrian Lang
215f1986589SMichael Klier// initialize plugin controller
216f1986589SMichael Klier$plugin_controller = new $plugin_controller_class();
217f1986589SMichael Klier
218f1986589SMichael Klier// initialize the event handler
219f1986589SMichael Klierglobal $EVENT_HANDLER;
220f1986589SMichael Klier$EVENT_HANDLER = new Doku_Event_Handler();
221f1986589SMichael Klier
22216905344SAndreas Gohr// setup authentication system
223c7cb395cSAdrian Langif (!defined('NOSESSION')) {
22416905344SAndreas Gohr    auth_setup();
225c7cb395cSAdrian Lang}
226f62ea8a1Sandi
227f62ea8a1Sandi/**
22898407a7aSandi * Checks paths from config file
22998407a7aSandi */
23098407a7aSandifunction init_paths(){
23198407a7aSandi    global $conf;
23298407a7aSandi
23398407a7aSandi    $paths = array('datadir'   => 'pages',
23498407a7aSandi            'olddir'    => 'attic',
23598407a7aSandi            'mediadir'  => 'media',
23698407a7aSandi            'metadir'   => 'meta',
23798407a7aSandi            'cachedir'  => 'cache',
238579b0f7eSTNHarris            'indexdir'  => 'index',
239de33a58fSMichael Klier            'lockdir'   => 'locks',
240de33a58fSMichael Klier            'tmpdir'    => 'tmp');
24198407a7aSandi
24298407a7aSandi    foreach($paths as $c => $p){
243bb4866bdSchris        if(empty($conf[$c]))  $conf[$c] = $conf['savedir'].'/'.$p;
24498407a7aSandi        $conf[$c]             = init_path($conf[$c]);
2451983acc2SGuy Brand        if(empty($conf[$c]))  nice_die("The $c ('$p') does not exist, isn't accessible or writable.
24669dc3177SAndreas Gohr                You should check your config and permission settings.
24769dc3177SAndreas Gohr                Or maybe you want to <a href=\"install.php\">run the
24869dc3177SAndreas Gohr                installer</a>?");
24998407a7aSandi    }
25071726d78SBen Coburn
25171726d78SBen Coburn    // path to old changelog only needed for upgrading
25271726d78SBen Coburn    $conf['changelog_old'] = init_path((isset($conf['changelog']))?($conf['changelog']):($conf['savedir'].'/changes.log'));
25371726d78SBen Coburn    if ($conf['changelog_old']=='') { unset($conf['changelog_old']); }
25471726d78SBen Coburn    // hardcoded changelog because it is now a cache that lives in meta
25571726d78SBen Coburn    $conf['changelog'] = $conf['metadir'].'/_dokuwiki.changes';
25699c8d7f2Smichael    $conf['media_changelog'] = $conf['metadir'].'/_media.changes';
25798407a7aSandi}
25898407a7aSandi
25998407a7aSandi/**
2600d8850c4SAndreas Gohr * Checks the existance of certain files and creates them if missing.
2617367b368SAndreas Gohr */
2627367b368SAndreas Gohrfunction init_files(){
2637367b368SAndreas Gohr    global $conf;
2640d8850c4SAndreas Gohr
265345b1674SAndreas Gohr    $files = array($conf['indexdir'].'/page.idx');
2667367b368SAndreas Gohr
2677367b368SAndreas Gohr    foreach($files as $file){
2687367b368SAndreas Gohr        if(!@file_exists($file)){
2690d8850c4SAndreas Gohr            $fh = @fopen($file,'a');
2700d8850c4SAndreas Gohr            if($fh){
2717367b368SAndreas Gohr                fclose($fh);
2721ca31cfeSAndreas Gohr                if($conf['fperm']) chmod($file, $conf['fperm']);
2730d8850c4SAndreas Gohr            }else{
2743816dcbcSAndreas Gohr                nice_die("$file is not writable. Check your permissions settings!");
2750d8850c4SAndreas Gohr            }
2767367b368SAndreas Gohr        }
2777367b368SAndreas Gohr    }
278345b1674SAndreas Gohr
279345b1674SAndreas Gohr    # create title index (needs to have same length as page.idx)
280345b1674SAndreas Gohr    $file = $conf['indexdir'].'/title.idx';
281345b1674SAndreas Gohr    if(!@file_exists($file)){
282345b1674SAndreas Gohr        $pages = file($conf['indexdir'].'/page.idx');
283345b1674SAndreas Gohr        $pages = count($pages);
284345b1674SAndreas Gohr        $fh = @fopen($file,'a');
285345b1674SAndreas Gohr        if($fh){
286345b1674SAndreas Gohr            for($i=0; $i<$pages; $i++){
287345b1674SAndreas Gohr                fwrite($fh,"\n");
288345b1674SAndreas Gohr            }
289345b1674SAndreas Gohr            fclose($fh);
290345b1674SAndreas Gohr        }else{
291345b1674SAndreas Gohr            nice_die("$file is not writable. Check your permissions settings!");
292345b1674SAndreas Gohr        }
293345b1674SAndreas Gohr    }
2947367b368SAndreas Gohr}
2957367b368SAndreas Gohr
2967367b368SAndreas Gohr/**
2970d8850c4SAndreas Gohr * Returns absolute path
298f62ea8a1Sandi *
2990d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC.
3000d8850c4SAndreas Gohr * Check for accessability on directories as well.
3010d8850c4SAndreas Gohr *
3020d8850c4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
303f62ea8a1Sandi */
304f62ea8a1Sandifunction init_path($path){
3050d8850c4SAndreas Gohr    // check existance
30600976812SAndreas Gohr    $p = fullpath($path);
3070d8850c4SAndreas Gohr    if(!@file_exists($p)){
30800976812SAndreas Gohr        $p = fullpath(DOKU_INC.$path);
3090d8850c4SAndreas Gohr        if(!@file_exists($p)){
3108fc4e739Sandi            return '';
311f62ea8a1Sandi        }
3120d8850c4SAndreas Gohr    }
3130d8850c4SAndreas Gohr
3140d8850c4SAndreas Gohr    // check writability
3150d8850c4SAndreas Gohr    if(!@is_writable($p)){
3160d8850c4SAndreas Gohr        return '';
3170d8850c4SAndreas Gohr    }
3180d8850c4SAndreas Gohr
3190d8850c4SAndreas Gohr    // check accessability (execute bit) for directories
3200d8850c4SAndreas Gohr    if(@is_dir($p) && !@file_exists("$p/.")){
3210d8850c4SAndreas Gohr        return '';
3220d8850c4SAndreas Gohr    }
3230d8850c4SAndreas Gohr
3240d8850c4SAndreas Gohr    return $p;
3250d8850c4SAndreas Gohr}
3268c4f28e8Sjan
327ed7b5f09Sandi/**
3281ca31cfeSAndreas Gohr * Sets the internal config values fperm and dperm which, when set,
3291ca31cfeSAndreas Gohr * will be used to change the permission of a newly created dir or
3301ca31cfeSAndreas Gohr * file with chmod. Considers the influence of the system's umask
3311ca31cfeSAndreas Gohr * setting the values only if needed.
3321ca31cfeSAndreas Gohr */
3331ca31cfeSAndreas Gohrfunction init_creationmodes(){
3341ca31cfeSAndreas Gohr    global $conf;
3351ca31cfeSAndreas Gohr
3361ca31cfeSAndreas Gohr    // Legacy support for old umask/dmask scheme
3371ca31cfeSAndreas Gohr    unset($conf['dmask']);
3381ca31cfeSAndreas Gohr    unset($conf['fmask']);
3391ca31cfeSAndreas Gohr    unset($conf['umask']);
3401ca31cfeSAndreas Gohr    unset($conf['fperm']);
3411ca31cfeSAndreas Gohr    unset($conf['dperm']);
3421ca31cfeSAndreas Gohr
3439f3cdec3SAndreas Gohr    // get system umask, fallback to 0 if none available
3449f3cdec3SAndreas Gohr    $umask = @umask();
3459f3cdec3SAndreas Gohr    if(!$umask) $umask = 0000;
3461ca31cfeSAndreas Gohr
3471ca31cfeSAndreas Gohr    // check what is set automatically by the system on file creation
3481ca31cfeSAndreas Gohr    // and set the fperm param if it's not what we want
3491ca31cfeSAndreas Gohr    $auto_fmode = 0666 & ~$umask;
3501ca31cfeSAndreas Gohr    if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode'];
3511ca31cfeSAndreas Gohr
3521ca31cfeSAndreas Gohr    // check what is set automatically by the system on file creation
3531ca31cfeSAndreas Gohr    // and set the dperm param if it's not what we want
3541ca31cfeSAndreas Gohr    $auto_dmode = $conf['dmode'] & ~$umask;
3551ca31cfeSAndreas Gohr    if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode'];
3561ca31cfeSAndreas Gohr}
3571ca31cfeSAndreas Gohr
3581ca31cfeSAndreas Gohr/**
359ed7b5f09Sandi * remove magic quotes recursivly
360ed7b5f09Sandi *
361ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
362ed7b5f09Sandi */
363ed7b5f09Sandifunction remove_magic_quotes(&$array) {
364ed7b5f09Sandi    foreach (array_keys($array) as $key) {
36581c54349SAndreas Gohr        // handle magic quotes in keynames (breaks order)
36681c54349SAndreas Gohr        $sk = stripslashes($key);
36781c54349SAndreas Gohr        if($sk != $key){
36881c54349SAndreas Gohr            $array[$sk] = $array[$key];
36981c54349SAndreas Gohr            unset($array[$key]);
37081c54349SAndreas Gohr            $key = $sk;
37181c54349SAndreas Gohr        }
37281c54349SAndreas Gohr
37381c54349SAndreas Gohr        // do recursion if needed
374ed7b5f09Sandi        if (is_array($array[$key])) {
375ed7b5f09Sandi            remove_magic_quotes($array[$key]);
376ed7b5f09Sandi        }else {
377ed7b5f09Sandi            $array[$key] = stripslashes($array[$key]);
378ed7b5f09Sandi        }
379ed7b5f09Sandi    }
380ed7b5f09Sandi}
381ed7b5f09Sandi
382ed7b5f09Sandi/**
383ed7b5f09Sandi * Returns the full absolute URL to the directory where
384ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash)
385ed7b5f09Sandi *
386ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
387ed7b5f09Sandi */
3884b1a4e04SAndreas Gohrfunction getBaseURL($abs=null){
389ed7b5f09Sandi    global $conf;
390ed7b5f09Sandi    //if canonical url enabled always return absolute
3914b1a4e04SAndreas Gohr    if(is_null($abs)) $abs = $conf['canonical'];
392ed7b5f09Sandi
39392b83b77Sandi    if($conf['basedir']){
39446c73e01SChris Smith        $dir = $conf['basedir'];
39589aa05dbSAndreas Gohr    }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){
39646c73e01SChris Smith        $dir = dirname($_SERVER['SCRIPT_NAME']);
39789aa05dbSAndreas Gohr    }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){
39846c73e01SChris Smith        $dir = dirname($_SERVER['PHP_SELF']);
399093ec9e4Sandi    }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
400093ec9e4Sandi        $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
401093ec9e4Sandi                $_SERVER['SCRIPT_FILENAME']);
40246c73e01SChris Smith        $dir = dirname('/'.$dir);
40392b83b77Sandi    }else{
40446c73e01SChris Smith        $dir = '.'; //probably wrong
40592b83b77Sandi    }
406ed7b5f09Sandi
40746c73e01SChris Smith    $dir = str_replace('\\','/',$dir);             // bugfix for weird WIN behaviour
40846c73e01SChris Smith    $dir = preg_replace('#//+#','/',"/$dir/");     // ensure leading and trailing slashes
409ed7b5f09Sandi
410f62ea8a1Sandi    //handle script in lib/exe dir
411f62ea8a1Sandi    $dir = preg_replace('!lib/exe/$!','',$dir);
412f62ea8a1Sandi
413488d5fa0SMichael Klier chi@chimeric.de    //handle script in lib/plugins dir
414488d5fa0SMichael Klier chi@chimeric.de    $dir = preg_replace('!lib/plugins/.*$!','',$dir);
415488d5fa0SMichael Klier chi@chimeric.de
416ed7b5f09Sandi    //finish here for relative URLs
417ed7b5f09Sandi    if(!$abs) return $dir;
418ed7b5f09Sandi
41946c73e01SChris Smith    //use config option if available, trim any slash from end of baseurl to avoid multiple consecutive slashes in the path
42046c73e01SChris Smith    if($conf['baseurl']) return rtrim($conf['baseurl'],'/').$dir;
421ef7b3ecdSAndreas Gohr
422e82e3526SAndreas Gohr    //split hostheader into host and port
423c66972f2SAdrian Lang    $addr = explode(':',$_SERVER['HTTP_HOST']);
424c66972f2SAdrian Lang    $host = $addr[0];
425c66972f2SAdrian Lang    $port = '';
426c66972f2SAdrian Lang    if (isset($addr[1])) {
427c66972f2SAdrian Lang        $port = $addr[1];
428c66972f2SAdrian Lang    } elseif (isset($_SERVER['SERVER_PORT'])) {
429c66972f2SAdrian Lang        $port = $_SERVER['SERVER_PORT'];
430c66972f2SAdrian Lang    }
431f5c6743cSAndreas Gohr    if(!is_ssl()){
432ed7b5f09Sandi        $proto = 'http://';
433e82e3526SAndreas Gohr        if ($port == '80') {
434ed7b5f09Sandi            $port = '';
435ed7b5f09Sandi        }
436ed7b5f09Sandi    }else{
437ed7b5f09Sandi        $proto = 'https://';
438e82e3526SAndreas Gohr        if ($port == '443') {
439ed7b5f09Sandi            $port = '';
440ed7b5f09Sandi        }
441ed7b5f09Sandi    }
442ed7b5f09Sandi
443c66972f2SAdrian Lang    if($port !== '') $port = ':'.$port;
444e82e3526SAndreas Gohr
445ed7b5f09Sandi    return $proto.$host.$port.$dir;
446ed7b5f09Sandi}
447ed7b5f09Sandi
448b000c6d4Sandi/**
449f5c6743cSAndreas Gohr * Check if accessed via HTTPS
450f5c6743cSAndreas Gohr *
451f5c6743cSAndreas Gohr * Apache leaves ,$_SERVER['HTTPS'] empty when not available, IIS sets it to 'off'.
452f5c6743cSAndreas Gohr * 'false' and 'disabled' are just guessing
453f5c6743cSAndreas Gohr *
454f5c6743cSAndreas Gohr * @returns bool true when SSL is active
455f5c6743cSAndreas Gohr */
456f5c6743cSAndreas Gohrfunction is_ssl(){
457c66972f2SAdrian Lang    if (!isset($_SERVER['HTTPS']) ||
458c66972f2SAdrian Lang        preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){
459f5c6743cSAndreas Gohr        return false;
460f5c6743cSAndreas Gohr    }else{
461f5c6743cSAndreas Gohr        return true;
462f5c6743cSAndreas Gohr    }
463f5c6743cSAndreas Gohr}
464f5c6743cSAndreas Gohr
465f5c6743cSAndreas Gohr/**
466b000c6d4Sandi * Append a PHP extension to a given file and adds an exit call
467b000c6d4Sandi *
468b000c6d4Sandi * This is used to migrate some old configfiles. An added PHP extension
469b000c6d4Sandi * ensures the contents are not shown to webusers even if .htaccess files
470b000c6d4Sandi * do not work
471b000c6d4Sandi *
472b000c6d4Sandi * @author Jan Decaluwe <jan@jandecaluwe.com>
473b000c6d4Sandi */
4748c4f28e8Sjanfunction scriptify($file) {
4758c4f28e8Sjan    // checks
4768c4f28e8Sjan    if (!is_readable($file)) {
4778c4f28e8Sjan        return;
4788c4f28e8Sjan    }
4798c4f28e8Sjan    $fn = $file.'.php';
4808c4f28e8Sjan    if (@file_exists($fn)) {
4818c4f28e8Sjan        return;
4828c4f28e8Sjan    }
4838c4f28e8Sjan    $fh = fopen($fn, 'w');
4848c4f28e8Sjan    if (!$fh) {
4853816dcbcSAndreas Gohr        nice_die($fn.' is not writable. Check your permission settings!');
4868c4f28e8Sjan    }
4878c4f28e8Sjan    // write php exit hack first
4888c4f28e8Sjan    fwrite($fh, "# $fn\n");
4898c4f28e8Sjan    fwrite($fh, '# <?php exit()?>'."\n");
4908c4f28e8Sjan    fwrite($fh, "# Don't modify the lines above\n");
4918c4f28e8Sjan    fwrite($fh, "#\n");
4928c4f28e8Sjan    // copy existing lines
4938c4f28e8Sjan    $lines = file($file);
4948c4f28e8Sjan    foreach ($lines as $line){
4958c4f28e8Sjan        fwrite($fh, $line);
4968c4f28e8Sjan    }
4973ba793e2Sandi    fclose($fh);
498b000c6d4Sandi    //try to rename the old file
4993aee4c27SAndreas Gohr    io_rename($file,"$file.old");
5008c4f28e8Sjan}
5018c4f28e8Sjan
5023816dcbcSAndreas Gohr/**
5033816dcbcSAndreas Gohr * print a nice message even if no styles are loaded yet.
5043816dcbcSAndreas Gohr */
5053816dcbcSAndreas Gohrfunction nice_die($msg){
5063816dcbcSAndreas Gohr    echo<<<EOT
5073816dcbcSAndreas Gohr<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
5083816dcbcSAndreas Gohr    "http://www.w3.org/TR/html4/loose.dtd">
5093816dcbcSAndreas Gohr<html>
5103816dcbcSAndreas Gohr<head><title>DokuWiki Setup Error</title></head>
5113816dcbcSAndreas Gohr<body style="font-family: Arial, sans-serif">
5123816dcbcSAndreas Gohr    <div style="width:60%; margin: auto; background-color: #fcc;
5133816dcbcSAndreas Gohr                border: 1px solid #faa; padding: 0.5em 1em;">
5143816dcbcSAndreas Gohr        <h1 style="font-size: 120%">DokuWiki Setup Error</h1>
5153816dcbcSAndreas Gohr        <p>$msg</p>
5163816dcbcSAndreas Gohr    </div>
5173816dcbcSAndreas Gohr</body>
5183816dcbcSAndreas Gohr</html>
5193816dcbcSAndreas GohrEOT;
5203816dcbcSAndreas Gohr    exit;
5213816dcbcSAndreas Gohr}
5223816dcbcSAndreas Gohr
52300976812SAndreas Gohr/**
52400976812SAndreas Gohr * A realpath() replacement
52500976812SAndreas Gohr *
52600976812SAndreas Gohr * This function behaves similar to PHP's realpath() but does not resolve
52700976812SAndreas Gohr * symlinks or accesses upper directories
52800976812SAndreas Gohr *
5294761d30cSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
53000976812SAndreas Gohr * @author <richpageau at yahoo dot co dot uk>
53100976812SAndreas Gohr * @link   http://de3.php.net/manual/en/function.realpath.php#75992
53200976812SAndreas Gohr */
533b328697dSAndreas Gohrfunction fullpath($path,$exists=false){
5344761d30cSAndreas Gohr    static $run = 0;
5354761d30cSAndreas Gohr    $root  = '';
536f0a201c5SChris Smith    $iswin = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' || @$GLOBALS['DOKU_UNITTEST_ASSUME_WINDOWS']);
53700976812SAndreas Gohr
5384761d30cSAndreas Gohr    // find the (indestructable) root of the path - keeps windows stuff intact
5394761d30cSAndreas Gohr    if($path{0} == '/'){
5404761d30cSAndreas Gohr        $root = '/';
5414761d30cSAndreas Gohr    }elseif($iswin){
5424761d30cSAndreas Gohr        // match drive letter and UNC paths
5434761d30cSAndreas Gohr        if(preg_match('!^([a-zA-z]:)(.*)!',$path,$match)){
544b9c4302bSAndreas Gohr            $root = $match[1].'/';
5454761d30cSAndreas Gohr            $path = $match[2];
5464761d30cSAndreas Gohr        }else if(preg_match('!^(\\\\\\\\[^\\\\/]+\\\\[^\\\\/]+[\\\\/])(.*)!',$path,$match)){
5474761d30cSAndreas Gohr            $root = $match[1];
5484761d30cSAndreas Gohr            $path = $match[2];
54900976812SAndreas Gohr        }
5504761d30cSAndreas Gohr    }
5514761d30cSAndreas Gohr    $path = str_replace('\\','/',$path);
5524761d30cSAndreas Gohr
5534761d30cSAndreas Gohr    // if the given path wasn't absolute already, prepend the script path and retry
5544761d30cSAndreas Gohr    if(!$root){
5554761d30cSAndreas Gohr        $base = dirname($_SERVER['SCRIPT_FILENAME']);
5564761d30cSAndreas Gohr        $path = $base.'/'.$path;
5574761d30cSAndreas Gohr        if($run == 0){ // avoid endless recursion when base isn't absolute for some reason
5584761d30cSAndreas Gohr            $run++;
559b328697dSAndreas Gohr            return fullpath($path,$exists);
5604761d30cSAndreas Gohr        }
5614761d30cSAndreas Gohr    }
5624761d30cSAndreas Gohr    $run = 0;
56300976812SAndreas Gohr
56400976812SAndreas Gohr    // canonicalize
56500976812SAndreas Gohr    $path=explode('/', $path);
56600976812SAndreas Gohr    $newpath=array();
567ef38bfe8SAndreas Gohr    foreach($path as $p) {
568ef38bfe8SAndreas Gohr        if ($p === '' || $p === '.') continue;
569ef38bfe8SAndreas Gohr        if ($p==='..') {
57000976812SAndreas Gohr            array_pop($newpath);
57100976812SAndreas Gohr            continue;
57200976812SAndreas Gohr        }
573ef38bfe8SAndreas Gohr        array_push($newpath, $p);
57400976812SAndreas Gohr    }
5754761d30cSAndreas Gohr    $finalpath = $root.implode('/', $newpath);
57600976812SAndreas Gohr
577b328697dSAndreas Gohr    // check for existance when needed (except when unit testing)
578b328697dSAndreas Gohr    if($exists && !defined('DOKU_UNITTEST') && !@file_exists($finalpath)) {
5794761d30cSAndreas Gohr        return false;
58000976812SAndreas Gohr    }
5814761d30cSAndreas Gohr    return $finalpath;
58200976812SAndreas Gohr}
58300976812SAndreas Gohr
584