xref: /dokuwiki/inc/init.php (revision db959ae30700e3924a73350a3a43a2c36b431de7)
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
23e7cb32dcSAndreas Gohr// define config path (packagers may want to change this to /etc/dokuwiki/)
24b7551a6dSEsther Brunnerif(!defined('DOKU_CONF')) define('DOKU_CONF',DOKU_INC.'conf/');
25e7cb32dcSAndreas Gohr
26bad905f1SBen Coburn// check for error reporting override or set error reporting to sane values
27d8186216SBen Coburnif (!defined('DOKU_E_LEVEL') && @file_exists(DOKU_CONF.'report_e_all')) {
28bad905f1SBen Coburn    define('DOKU_E_LEVEL', E_ALL);
29bad905f1SBen Coburn}
30fc80ed59SAndreas Gohrif (!defined('DOKU_E_LEVEL')) {
31fc80ed59SAndreas Gohr    if(defined('E_DEPRECATED')){ // since php 5.3
32fc80ed59SAndreas Gohr        error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
33fc80ed59SAndreas Gohr    }else{
34fc80ed59SAndreas Gohr        error_reporting(E_ALL ^ E_NOTICE);
35fc80ed59SAndreas Gohr    }
36fc80ed59SAndreas Gohr} else {
37fc80ed59SAndreas Gohr    error_reporting(DOKU_E_LEVEL);
38fc80ed59SAndreas Gohr}
39c53ea5f2Sandi
4050602150SBen Coburn// init memory caches
41*db959ae3SAndreas Gohrglobal $cache_revinfo;
42*db959ae3SAndreas Gohr       $cache_revinfo = array();
43*db959ae3SAndreas Gohrglobal $cache_wikifn;
44*db959ae3SAndreas Gohr       $cache_wikifn = array();
45*db959ae3SAndreas Gohrglobal $cache_cleanid;
46*db959ae3SAndreas Gohr       $cache_cleanid = array();
47*db959ae3SAndreas Gohrglobal $cache_authname;
48*db959ae3SAndreas Gohr       $cache_authname = array();
49*db959ae3SAndreas Gohrglobal $cache_metadata;
50*db959ae3SAndreas Gohr       $cache_metadata = array();
5150602150SBen Coburn
52cb043f52SChris Smith//set the configuration cascade - but only if its not already been set in preload.php
53cb043f52SChris Smithif (empty($config_cascade)) {
54cb043f52SChris Smith    $config_cascade = array(
55cb043f52SChris Smith            'main' => array(
56cb043f52SChris Smith                'default'   => array(DOKU_CONF.'dokuwiki.php'),
57cb043f52SChris Smith                'local'     => array(DOKU_CONF.'local.php'),
58cb043f52SChris Smith                'protected' => array(DOKU_CONF.'local.protected.php'),
59cb043f52SChris Smith                ),
60cb043f52SChris Smith            'acronyms'  => array(
61b303b92cSChris Smith                'default'   => array(DOKU_CONF.'acronyms.conf'),
62b303b92cSChris Smith                'local'     => array(DOKU_CONF.'acronyms.local.conf'),
63cb043f52SChris Smith                ),
64cb043f52SChris Smith            'entities'  => array(
65b303b92cSChris Smith                'default'   => array(DOKU_CONF.'entities.conf'),
66b303b92cSChris Smith                'local'     => array(DOKU_CONF.'entities.local.conf'),
67cb043f52SChris Smith                ),
68cb043f52SChris Smith            'interwiki' => array(
69b303b92cSChris Smith                'default'   => array(DOKU_CONF.'interwiki.conf'),
70b303b92cSChris Smith                'local'     => array(DOKU_CONF.'interwiki.local.conf'),
71cb043f52SChris Smith                ),
72f8121585SChris Smith            'license' => array(
73f8121585SChris Smith                'default'   => array(DOKU_CONF.'license.php'),
74f8121585SChris Smith                'local'     => array(DOKU_CONF.'license.local.php'),
75f8121585SChris Smith                ),
76f8121585SChris Smith            'mediameta' => array(
77f8121585SChris Smith                    'default'   => array(DOKU_CONF.'mediameta.php'),
78f8121585SChris Smith                    'local'     => array(DOKU_CONF.'mediameta.local.php'),
79f8121585SChris Smith                    ),
80cb043f52SChris Smith            'mime'      => array(
81b303b92cSChris Smith                    'default'   => array(DOKU_CONF.'mime.conf'),
82b303b92cSChris Smith                    'local'     => array(DOKU_CONF.'mime.local.conf'),
83cb043f52SChris Smith                    ),
84cb043f52SChris Smith            'scheme'    => array(
85b303b92cSChris Smith                    'default'   => array(DOKU_CONF.'scheme.conf'),
86b303b92cSChris Smith                    'local'     => array(DOKU_CONF.'scheme.local.conf'),
87cb043f52SChris Smith                    ),
88cb043f52SChris Smith            'smileys'   => array(
89b303b92cSChris Smith                    'default'   => array(DOKU_CONF.'smileys.conf'),
90b303b92cSChris Smith                    'local'     => array(DOKU_CONF.'smileys.local.conf'),
91cb043f52SChris Smith                    ),
92cb043f52SChris Smith            'wordblock' => array(
93b303b92cSChris Smith                    'default'   => array(DOKU_CONF.'wordblock.conf'),
94b303b92cSChris Smith                    'local'     => array(DOKU_CONF.'wordblock.local.conf'),
95cb043f52SChris Smith                    ),
96cb043f52SChris Smith            );
97cb043f52SChris Smith}
98cb043f52SChris Smith
994724a577Sandi//prepare config array()
100ee20e7d1Sandiglobal $conf;
1014724a577Sandi$conf = array();
1024724a577Sandi
103cb043f52SChris Smith// load the global config file(s)
104b303b92cSChris Smithforeach (array('default','local','protected') as $config_group) {
105f8121585SChris Smith    if (empty($config_cascade['main'][$config_group])) continue;
106b303b92cSChris Smith    foreach ($config_cascade['main'][$config_group] as $config_file) {
107f8121585SChris Smith        if (@file_exists($config_file)) {
108f8121585SChris Smith            include($config_file);
109f8121585SChris Smith        }
110cb043f52SChris Smith    }
1110a6ead41SAndreas Gohr}
112ad15db82Sandi
113ad15db82Sandi//prepare language array
114ee20e7d1Sandiglobal $lang;
115ad15db82Sandi$lang = array();
116ed7b5f09Sandi
11716521111Sandi//load the language files
118bc3b6aecSandirequire_once(DOKU_INC.'inc/lang/en/lang.php');
119f949a01cSAndreas Gohrif ( $conf['lang'] && $conf['lang'] != 'en' ) {
120bc3b6aecSandi    require_once(DOKU_INC.'inc/lang/'.$conf['lang'].'/lang.php');
121fc1c55b1Shfuecks}
12216521111Sandi
123066fee30SAndreas Gohr//prepare license array()
124066fee30SAndreas Gohrglobal $license;
125066fee30SAndreas Gohr$license = array();
126066fee30SAndreas Gohr
127066fee30SAndreas Gohr// load the license file(s)
128f8121585SChris Smithforeach (array('default','local') as $config_group) {
129f8121585SChris Smith    if (empty($config_cascade['license'][$config_group])) continue;
130f8121585SChris Smith    foreach ($config_cascade['license'][$config_group] as $config_file) {
131f8121585SChris Smith        if(@file_exists($config_file)){
132f8121585SChris Smith            include($config_file);
133f8121585SChris Smith        }
134f8121585SChris Smith    }
135066fee30SAndreas Gohr}
136066fee30SAndreas Gohr
137ed7b5f09Sandi// define baseURL
1384b1a4e04SAndreas Gohrif(!defined('DOKU_REL')) define('DOKU_REL',getBaseURL(false));
139ed7b5f09Sandiif(!defined('DOKU_URL')) define('DOKU_URL',getBaseURL(true));
1404b1a4e04SAndreas Gohrif(!defined('DOKU_BASE')){
1414b1a4e04SAndreas Gohr    if($conf['canonical']){
1424b1a4e04SAndreas Gohr        define('DOKU_BASE',DOKU_URL);
1434b1a4e04SAndreas Gohr    }else{
1444b1a4e04SAndreas Gohr        define('DOKU_BASE',DOKU_REL);
1454b1a4e04SAndreas Gohr    }
1464b1a4e04SAndreas Gohr}
1474b1a4e04SAndreas Gohr
148b8595a66SAndreas Gohr// define whitespace
149b8595a66SAndreas Gohrif(!defined('DOKU_LF')) define ('DOKU_LF',"\n");
150b8595a66SAndreas Gohrif(!defined('DOKU_TAB')) define ('DOKU_TAB',"\t");
151ed7b5f09Sandi
152656c8fb3SAndreas Gohr// define cookie and session id, append server port when securecookie is configured FS#1664
153656c8fb3SAndreas Gohrif (!defined('DOKU_COOKIE')) define('DOKU_COOKIE', 'DW'.md5(DOKU_REL.(($conf['securecookie'])?$_SERVER['SERVER_PORT']:'')));
154e71ce681SAndreas Gohr
155ee20e7d1Sandi// define Plugin dir
156f62ea8a1Sandiif(!defined('DOKU_PLUGIN'))  define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
157ee20e7d1Sandi
158ed7b5f09Sandi// define main script
159ed7b5f09Sandiif(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php');
160ed7b5f09Sandi
1616b13307fSandi// define Template baseURL
1626b13307fSandiif(!defined('DOKU_TPL')) define('DOKU_TPL',
163f62ea8a1Sandi        DOKU_BASE.'lib/tpl/'.$conf['template'].'/');
1646b13307fSandi
16578a6aeb1SAndreas Gohr// define real Template directory
16678a6aeb1SAndreas Gohrif(!defined('DOKU_TPLINC')) define('DOKU_TPLINC',
16778a6aeb1SAndreas Gohr        DOKU_INC.'lib/tpl/'.$conf['template'].'/');
16878a6aeb1SAndreas Gohr
169ed7b5f09Sandi// make session rewrites XHTML compliant
1703fc74836Sandi@ini_set('arg_separator.output', '&amp;');
171ed7b5f09Sandi
172d7e6bba9SAndreas Gohr// make sure global zlib does not interfere FS#1132
173d7e6bba9SAndreas Gohr@ini_set('zlib.output_compression', 'off');
174d7e6bba9SAndreas Gohr
1756deb5405SAndreas Gohr// increase PCRE backtrack limit
1766deb5405SAndreas Gohr@ini_set('pcre.backtrack_limit', '20971520');
1776deb5405SAndreas Gohr
17898bda4fdSAndreas Gohr// enable gzip compression if supported
17998bda4fdSAndreas Gohr$conf['gzip_output'] &= (strpos($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip') !== false);
1803138b5c7SAndreas Gohrif ($conf['gzip_output'] &&
1813138b5c7SAndreas Gohr        !defined('DOKU_DISABLE_GZIP_OUTPUT') &&
18298bda4fdSAndreas Gohr        function_exists('ob_gzhandler')) {
1833138b5c7SAndreas Gohr    ob_start('ob_gzhandler');
1843138b5c7SAndreas Gohr}
1853138b5c7SAndreas Gohr
186ed7b5f09Sandi// init session
1876534245aSAndreas Gohrif (!headers_sent() && !defined('NOSESSION')){
188ed7b5f09Sandi    session_name("DokuWiki");
189f5c6743cSAndreas Gohr    if (version_compare(PHP_VERSION, '5.2.0', '>')) {
190f5c6743cSAndreas Gohr        session_set_cookie_params(0,DOKU_REL,'',($conf['securecookie'] && is_ssl()),true);
191f5c6743cSAndreas Gohr    }else{
192f5c6743cSAndreas Gohr        session_set_cookie_params(0,DOKU_REL,'',($conf['securecookie'] && is_ssl()));
193f5c6743cSAndreas Gohr    }
194bad31ae9SAndreas Gohr    session_start();
19514a122deSAndreas Gohr
19614a122deSAndreas Gohr    // load left over messages
19714a122deSAndreas Gohr    if(isset($_SESSION[DOKU_COOKIE]['msg'])){
19814a122deSAndreas Gohr        $MSG = $_SESSION[DOKU_COOKIE]['msg'];
19914a122deSAndreas Gohr        unset($_SESSION[DOKU_COOKIE]['msg']);
20014a122deSAndreas Gohr    }
201bad31ae9SAndreas Gohr}
202ed7b5f09Sandi
203ed7b5f09Sandi// kill magic quotes
204e55eb89cSAndreas Gohrif (get_magic_quotes_gpc() && !defined('MAGIC_QUOTES_STRIPPED')) {
205ed7b5f09Sandi    if (!empty($_GET))    remove_magic_quotes($_GET);
206ed7b5f09Sandi    if (!empty($_POST))   remove_magic_quotes($_POST);
207ed7b5f09Sandi    if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE);
208ed7b5f09Sandi    if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST);
2093fc74836Sandi    @ini_set('magic_quotes_gpc', 0);
210e55eb89cSAndreas Gohr    define('MAGIC_QUOTES_STRIPPED',1);
211ed7b5f09Sandi}
2123fc74836Sandi@set_magic_quotes_runtime(0);
2133fc74836Sandi@ini_set('magic_quotes_sybase',0);
214ed7b5f09Sandi
215a1637ffdSAndreas Gohr// don't let cookies ever interfere with request vars
216a1637ffdSAndreas Gohr$_REQUEST = array_merge($_GET,$_POST);
217a1637ffdSAndreas Gohr
2183dea4ebcSAndreas Gohr// we don't want a purge URL to be digged
2193dea4ebcSAndreas Gohrif($_REQUEST['purge'] && $_SERVER['HTTP_REFERER']) unset($_REQUEST['purge']);
2203dea4ebcSAndreas Gohr
221ed7b5f09Sandi// disable gzip if not available
2228a447e5cSAndreas Gohrif($conf['compression'] == 'bz2' && !function_exists('bzopen')){
223fe893490SAndreas Gohr    $conf['compression'] = 'gz';
224501252a5SAndreas Gohr}
225fe893490SAndreas Gohrif($conf['compression'] == 'gz' && !function_exists('gzopen')){
226501252a5SAndreas Gohr    $conf['compression'] = 0;
227ed7b5f09Sandi}
228ed7b5f09Sandi
229e656dcd4SAndreas Gohr// fix dateformat for upgraders
230e656dcd4SAndreas Gohrif(strpos($conf['dformat'],'%') === false){
231e656dcd4SAndreas Gohr    $conf['dformat'] = '%Y/%m/%d %H:%M';
232e656dcd4SAndreas Gohr}
233e656dcd4SAndreas Gohr
2341ca31cfeSAndreas Gohr// precalculate file creation modes
2351ca31cfeSAndreas Gohrinit_creationmodes();
236ed7b5f09Sandi
2373dc3a5f1Sandi// make real paths and check them
23898407a7aSandiinit_paths();
2397367b368SAndreas Gohrinit_files();
240ed7b5f09Sandi
2418c4f28e8Sjan// automatic upgrade to script versions of certain files
242e7cb32dcSAndreas Gohrscriptify(DOKU_CONF.'users.auth');
243e7cb32dcSAndreas Gohrscriptify(DOKU_CONF.'acl.auth');
244f62ea8a1Sandi
245f62ea8a1Sandi
246f62ea8a1Sandi/**
24798407a7aSandi * Checks paths from config file
24898407a7aSandi */
24998407a7aSandifunction init_paths(){
25098407a7aSandi    global $conf;
25198407a7aSandi
25298407a7aSandi    $paths = array('datadir'   => 'pages',
25398407a7aSandi            'olddir'    => 'attic',
25498407a7aSandi            'mediadir'  => 'media',
25598407a7aSandi            'metadir'   => 'meta',
25698407a7aSandi            'cachedir'  => 'cache',
257579b0f7eSTNHarris            'indexdir'  => 'index',
258de33a58fSMichael Klier            'lockdir'   => 'locks',
259de33a58fSMichael Klier            'tmpdir'    => 'tmp');
26098407a7aSandi
26198407a7aSandi    foreach($paths as $c => $p){
262bb4866bdSchris        if(empty($conf[$c]))  $conf[$c] = $conf['savedir'].'/'.$p;
26398407a7aSandi        $conf[$c]             = init_path($conf[$c]);
2641983acc2SGuy Brand        if(empty($conf[$c]))  nice_die("The $c ('$p') does not exist, isn't accessible or writable.
26569dc3177SAndreas Gohr                You should check your config and permission settings.
26669dc3177SAndreas Gohr                Or maybe you want to <a href=\"install.php\">run the
26769dc3177SAndreas Gohr                installer</a>?");
26898407a7aSandi    }
26971726d78SBen Coburn
27071726d78SBen Coburn    // path to old changelog only needed for upgrading
27171726d78SBen Coburn    $conf['changelog_old'] = init_path((isset($conf['changelog']))?($conf['changelog']):($conf['savedir'].'/changes.log'));
27271726d78SBen Coburn    if ($conf['changelog_old']=='') { unset($conf['changelog_old']); }
27371726d78SBen Coburn    // hardcoded changelog because it is now a cache that lives in meta
27471726d78SBen Coburn    $conf['changelog'] = $conf['metadir'].'/_dokuwiki.changes';
27599c8d7f2Smichael    $conf['media_changelog'] = $conf['metadir'].'/_media.changes';
27698407a7aSandi}
27798407a7aSandi
27898407a7aSandi/**
2790d8850c4SAndreas Gohr * Checks the existance of certain files and creates them if missing.
2807367b368SAndreas Gohr */
2817367b368SAndreas Gohrfunction init_files(){
2827367b368SAndreas Gohr    global $conf;
2830d8850c4SAndreas Gohr
284579b0f7eSTNHarris    $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);
2911ca31cfeSAndreas Gohr                if($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    }
2977367b368SAndreas Gohr}
2987367b368SAndreas Gohr
2997367b368SAndreas Gohr/**
3000d8850c4SAndreas Gohr * Returns absolute path
301f62ea8a1Sandi *
3020d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC.
3030d8850c4SAndreas Gohr * Check for accessability on directories as well.
3040d8850c4SAndreas Gohr *
3050d8850c4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
306f62ea8a1Sandi */
307f62ea8a1Sandifunction init_path($path){
3080d8850c4SAndreas Gohr    // check existance
30900976812SAndreas Gohr    $p = fullpath($path);
3100d8850c4SAndreas Gohr    if(!@file_exists($p)){
31100976812SAndreas Gohr        $p = fullpath(DOKU_INC.$path);
3120d8850c4SAndreas Gohr        if(!@file_exists($p)){
3138fc4e739Sandi            return '';
314f62ea8a1Sandi        }
3150d8850c4SAndreas Gohr    }
3160d8850c4SAndreas Gohr
3170d8850c4SAndreas Gohr    // check writability
3180d8850c4SAndreas Gohr    if(!@is_writable($p)){
3190d8850c4SAndreas Gohr        return '';
3200d8850c4SAndreas Gohr    }
3210d8850c4SAndreas Gohr
3220d8850c4SAndreas Gohr    // check accessability (execute bit) for directories
3230d8850c4SAndreas Gohr    if(@is_dir($p) && !@file_exists("$p/.")){
3240d8850c4SAndreas Gohr        return '';
3250d8850c4SAndreas Gohr    }
3260d8850c4SAndreas Gohr
3270d8850c4SAndreas Gohr    return $p;
3280d8850c4SAndreas Gohr}
3298c4f28e8Sjan
330ed7b5f09Sandi/**
3311ca31cfeSAndreas Gohr * Sets the internal config values fperm and dperm which, when set,
3321ca31cfeSAndreas Gohr * will be used to change the permission of a newly created dir or
3331ca31cfeSAndreas Gohr * file with chmod. Considers the influence of the system's umask
3341ca31cfeSAndreas Gohr * setting the values only if needed.
3351ca31cfeSAndreas Gohr */
3361ca31cfeSAndreas Gohrfunction init_creationmodes(){
3371ca31cfeSAndreas Gohr    global $conf;
3381ca31cfeSAndreas Gohr
3391ca31cfeSAndreas Gohr    // Legacy support for old umask/dmask scheme
3401ca31cfeSAndreas Gohr    unset($conf['dmask']);
3411ca31cfeSAndreas Gohr    unset($conf['fmask']);
3421ca31cfeSAndreas Gohr    unset($conf['umask']);
3431ca31cfeSAndreas Gohr    unset($conf['fperm']);
3441ca31cfeSAndreas Gohr    unset($conf['dperm']);
3451ca31cfeSAndreas Gohr
3469f3cdec3SAndreas Gohr    // get system umask, fallback to 0 if none available
3479f3cdec3SAndreas Gohr    $umask = @umask();
3489f3cdec3SAndreas Gohr    if(!$umask) $umask = 0000;
3491ca31cfeSAndreas Gohr
3501ca31cfeSAndreas Gohr    // check what is set automatically by the system on file creation
3511ca31cfeSAndreas Gohr    // and set the fperm param if it's not what we want
3521ca31cfeSAndreas Gohr    $auto_fmode = 0666 & ~$umask;
3531ca31cfeSAndreas Gohr    if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode'];
3541ca31cfeSAndreas Gohr
3551ca31cfeSAndreas Gohr    // check what is set automatically by the system on file creation
3561ca31cfeSAndreas Gohr    // and set the dperm param if it's not what we want
3571ca31cfeSAndreas Gohr    $auto_dmode = $conf['dmode'] & ~$umask;
3581ca31cfeSAndreas Gohr    if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode'];
3591ca31cfeSAndreas Gohr}
3601ca31cfeSAndreas Gohr
3611ca31cfeSAndreas Gohr/**
362ed7b5f09Sandi * remove magic quotes recursivly
363ed7b5f09Sandi *
364ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
365ed7b5f09Sandi */
366ed7b5f09Sandifunction remove_magic_quotes(&$array) {
367ed7b5f09Sandi    foreach (array_keys($array) as $key) {
36881c54349SAndreas Gohr        // handle magic quotes in keynames (breaks order)
36981c54349SAndreas Gohr        $sk = stripslashes($key);
37081c54349SAndreas Gohr        if($sk != $key){
37181c54349SAndreas Gohr            $array[$sk] = $array[$key];
37281c54349SAndreas Gohr            unset($array[$key]);
37381c54349SAndreas Gohr            $key = $sk;
37481c54349SAndreas Gohr        }
37581c54349SAndreas Gohr
37681c54349SAndreas Gohr        // do recursion if needed
377ed7b5f09Sandi        if (is_array($array[$key])) {
378ed7b5f09Sandi            remove_magic_quotes($array[$key]);
379ed7b5f09Sandi        }else {
380ed7b5f09Sandi            $array[$key] = stripslashes($array[$key]);
381ed7b5f09Sandi        }
382ed7b5f09Sandi    }
383ed7b5f09Sandi}
384ed7b5f09Sandi
385ed7b5f09Sandi/**
386ed7b5f09Sandi * Returns the full absolute URL to the directory where
387ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash)
388ed7b5f09Sandi *
389ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
390ed7b5f09Sandi */
3914b1a4e04SAndreas Gohrfunction getBaseURL($abs=null){
392ed7b5f09Sandi    global $conf;
393ed7b5f09Sandi    //if canonical url enabled always return absolute
3944b1a4e04SAndreas Gohr    if(is_null($abs)) $abs = $conf['canonical'];
395ed7b5f09Sandi
39692b83b77Sandi    if($conf['basedir']){
39746c73e01SChris Smith        $dir = $conf['basedir'];
39889aa05dbSAndreas Gohr    }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){
39946c73e01SChris Smith        $dir = dirname($_SERVER['SCRIPT_NAME']);
40089aa05dbSAndreas Gohr    }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){
40146c73e01SChris Smith        $dir = dirname($_SERVER['PHP_SELF']);
402093ec9e4Sandi    }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
403093ec9e4Sandi        $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
404093ec9e4Sandi                $_SERVER['SCRIPT_FILENAME']);
40546c73e01SChris Smith        $dir = dirname('/'.$dir);
40692b83b77Sandi    }else{
40746c73e01SChris Smith        $dir = '.'; //probably wrong
40892b83b77Sandi    }
409ed7b5f09Sandi
41046c73e01SChris Smith    $dir = str_replace('\\','/',$dir);             // bugfix for weird WIN behaviour
41146c73e01SChris Smith    $dir = preg_replace('#//+#','/',"/$dir/");     // ensure leading and trailing slashes
412ed7b5f09Sandi
413f62ea8a1Sandi        //handle script in lib/exe dir
414f62ea8a1Sandi        $dir = preg_replace('!lib/exe/$!','',$dir);
415f62ea8a1Sandi
416488d5fa0SMichael Klier chi@chimeric.de    //handle script in lib/plugins dir
417488d5fa0SMichael Klier chi@chimeric.de    $dir = preg_replace('!lib/plugins/.*$!','',$dir);
418488d5fa0SMichael Klier chi@chimeric.de
419ed7b5f09Sandi    //finish here for relative URLs
420ed7b5f09Sandi    if(!$abs) return $dir;
421ed7b5f09Sandi
42246c73e01SChris Smith    //use config option if available, trim any slash from end of baseurl to avoid multiple consecutive slashes in the path
42346c73e01SChris Smith    if($conf['baseurl']) return rtrim($conf['baseurl'],'/').$dir;
424ef7b3ecdSAndreas Gohr
425e82e3526SAndreas Gohr    //split hostheader into host and port
426e82e3526SAndreas Gohr    list($host,$port) = explode(':',$_SERVER['HTTP_HOST']);
427e82e3526SAndreas Gohr    if(!$port)  $port = $_SERVER['SERVER_PORT'];
428e82e3526SAndreas Gohr    if(!$port)  $port = 80;
429ed7b5f09Sandi
430f5c6743cSAndreas Gohr    if(!is_ssl()){
431ed7b5f09Sandi        $proto = 'http://';
432e82e3526SAndreas Gohr        if ($port == '80') {
433ed7b5f09Sandi            $port='';
434ed7b5f09Sandi        }
435ed7b5f09Sandi    }else{
436ed7b5f09Sandi        $proto = 'https://';
437e82e3526SAndreas Gohr        if ($port == '443') {
438ed7b5f09Sandi            $port='';
439ed7b5f09Sandi        }
440ed7b5f09Sandi    }
441ed7b5f09Sandi
442e82e3526SAndreas Gohr    if($port) $port = ':'.$port;
443e82e3526SAndreas Gohr
444ed7b5f09Sandi    return $proto.$host.$port.$dir;
445ed7b5f09Sandi}
446ed7b5f09Sandi
447b000c6d4Sandi/**
448f5c6743cSAndreas Gohr * Check if accessed via HTTPS
449f5c6743cSAndreas Gohr *
450f5c6743cSAndreas Gohr * Apache leaves ,$_SERVER['HTTPS'] empty when not available, IIS sets it to 'off'.
451f5c6743cSAndreas Gohr * 'false' and 'disabled' are just guessing
452f5c6743cSAndreas Gohr *
453f5c6743cSAndreas Gohr * @returns bool true when SSL is active
454f5c6743cSAndreas Gohr */
455f5c6743cSAndreas Gohrfunction is_ssl(){
456f5c6743cSAndreas Gohr    if (preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){
457f5c6743cSAndreas Gohr        return false;
458f5c6743cSAndreas Gohr    }else{
459f5c6743cSAndreas Gohr        return true;
460f5c6743cSAndreas Gohr    }
461f5c6743cSAndreas Gohr}
462f5c6743cSAndreas Gohr
463f5c6743cSAndreas Gohr/**
464b000c6d4Sandi * Append a PHP extension to a given file and adds an exit call
465b000c6d4Sandi *
466b000c6d4Sandi * This is used to migrate some old configfiles. An added PHP extension
467b000c6d4Sandi * ensures the contents are not shown to webusers even if .htaccess files
468b000c6d4Sandi * do not work
469b000c6d4Sandi *
470b000c6d4Sandi * @author Jan Decaluwe <jan@jandecaluwe.com>
471b000c6d4Sandi */
4728c4f28e8Sjanfunction scriptify($file) {
4738c4f28e8Sjan    // checks
4748c4f28e8Sjan    if (!is_readable($file)) {
4758c4f28e8Sjan        return;
4768c4f28e8Sjan    }
4778c4f28e8Sjan    $fn = $file.'.php';
4788c4f28e8Sjan    if (@file_exists($fn)) {
4798c4f28e8Sjan        return;
4808c4f28e8Sjan    }
4818c4f28e8Sjan    $fh = fopen($fn, 'w');
4828c4f28e8Sjan    if (!$fh) {
4833816dcbcSAndreas Gohr        nice_die($fn.' is not writable. Check your permission settings!');
4848c4f28e8Sjan    }
4858c4f28e8Sjan    // write php exit hack first
4868c4f28e8Sjan    fwrite($fh, "# $fn\n");
4878c4f28e8Sjan    fwrite($fh, '# <?php exit()?>'."\n");
4888c4f28e8Sjan    fwrite($fh, "# Don't modify the lines above\n");
4898c4f28e8Sjan    fwrite($fh, "#\n");
4908c4f28e8Sjan    // copy existing lines
4918c4f28e8Sjan    $lines = file($file);
4928c4f28e8Sjan    foreach ($lines as $line){
4938c4f28e8Sjan        fwrite($fh, $line);
4948c4f28e8Sjan    }
4953ba793e2Sandi    fclose($fh);
496b000c6d4Sandi    //try to rename the old file
4973aee4c27SAndreas Gohr    io_rename($file,"$file.old");
4988c4f28e8Sjan}
4998c4f28e8Sjan
5003816dcbcSAndreas Gohr/**
5013816dcbcSAndreas Gohr * print a nice message even if no styles are loaded yet.
5023816dcbcSAndreas Gohr */
5033816dcbcSAndreas Gohrfunction nice_die($msg){
5043816dcbcSAndreas Gohr    echo<<<EOT
5053816dcbcSAndreas Gohr<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
5063816dcbcSAndreas Gohr    "http://www.w3.org/TR/html4/loose.dtd">
5073816dcbcSAndreas Gohr<html>
5083816dcbcSAndreas Gohr<head><title>DokuWiki Setup Error</title></head>
5093816dcbcSAndreas Gohr<body style="font-family: Arial, sans-serif">
5103816dcbcSAndreas Gohr    <div style="width:60%; margin: auto; background-color: #fcc;
5113816dcbcSAndreas Gohr                border: 1px solid #faa; padding: 0.5em 1em;">
5123816dcbcSAndreas Gohr        <h1 style="font-size: 120%">DokuWiki Setup Error</h1>
5133816dcbcSAndreas Gohr        <p>$msg</p>
5143816dcbcSAndreas Gohr    </div>
5153816dcbcSAndreas Gohr</body>
5163816dcbcSAndreas Gohr</html>
5173816dcbcSAndreas GohrEOT;
5183816dcbcSAndreas Gohr    exit;
5193816dcbcSAndreas Gohr}
5203816dcbcSAndreas Gohr
521ed7b5f09Sandi
52200976812SAndreas Gohr/**
52300976812SAndreas Gohr * A realpath() replacement
52400976812SAndreas Gohr *
52500976812SAndreas Gohr * This function behaves similar to PHP's realpath() but does not resolve
52600976812SAndreas Gohr * symlinks or accesses upper directories
52700976812SAndreas Gohr *
5284761d30cSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
52900976812SAndreas Gohr * @author <richpageau at yahoo dot co dot uk>
53000976812SAndreas Gohr * @link   http://de3.php.net/manual/en/function.realpath.php#75992
53100976812SAndreas Gohr */
532b328697dSAndreas Gohrfunction fullpath($path,$exists=false){
5334761d30cSAndreas Gohr    static $run = 0;
5344761d30cSAndreas Gohr    $root  = '';
535f0a201c5SChris Smith    $iswin = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' || @$GLOBALS['DOKU_UNITTEST_ASSUME_WINDOWS']);
53600976812SAndreas Gohr
5374761d30cSAndreas Gohr    // find the (indestructable) root of the path - keeps windows stuff intact
5384761d30cSAndreas Gohr    if($path{0} == '/'){
5394761d30cSAndreas Gohr        $root = '/';
5404761d30cSAndreas Gohr    }elseif($iswin){
5414761d30cSAndreas Gohr        // match drive letter and UNC paths
5424761d30cSAndreas Gohr        if(preg_match('!^([a-zA-z]:)(.*)!',$path,$match)){
543b9c4302bSAndreas Gohr            $root = $match[1].'/';
5444761d30cSAndreas Gohr            $path = $match[2];
5454761d30cSAndreas Gohr        }else if(preg_match('!^(\\\\\\\\[^\\\\/]+\\\\[^\\\\/]+[\\\\/])(.*)!',$path,$match)){
5464761d30cSAndreas Gohr            $root = $match[1];
5474761d30cSAndreas Gohr            $path = $match[2];
54800976812SAndreas Gohr        }
5494761d30cSAndreas Gohr    }
5504761d30cSAndreas Gohr    $path = str_replace('\\','/',$path);
5514761d30cSAndreas Gohr
5524761d30cSAndreas Gohr    // if the given path wasn't absolute already, prepend the script path and retry
5534761d30cSAndreas Gohr    if(!$root){
5544761d30cSAndreas Gohr        $base = dirname($_SERVER['SCRIPT_FILENAME']);
5554761d30cSAndreas Gohr        $path = $base.'/'.$path;
5564761d30cSAndreas Gohr        if($run == 0){ // avoid endless recursion when base isn't absolute for some reason
5574761d30cSAndreas Gohr            $run++;
558b328697dSAndreas Gohr            return fullpath($path,$exists);
5594761d30cSAndreas Gohr        }
5604761d30cSAndreas Gohr    }
5614761d30cSAndreas Gohr    $run = 0;
56200976812SAndreas Gohr
56300976812SAndreas Gohr    // canonicalize
56400976812SAndreas Gohr    $path=explode('/', $path);
56500976812SAndreas Gohr    $newpath=array();
566ef38bfe8SAndreas Gohr    foreach($path as $p) {
567ef38bfe8SAndreas Gohr        if ($p === '' || $p === '.') continue;
568ef38bfe8SAndreas Gohr        if ($p==='..') {
56900976812SAndreas Gohr            array_pop($newpath);
57000976812SAndreas Gohr            continue;
57100976812SAndreas Gohr        }
572ef38bfe8SAndreas Gohr        array_push($newpath, $p);
57300976812SAndreas Gohr    }
5744761d30cSAndreas Gohr    $finalpath = $root.implode('/', $newpath);
57500976812SAndreas Gohr
576b328697dSAndreas Gohr    // check for existance when needed (except when unit testing)
577b328697dSAndreas Gohr    if($exists && !defined('DOKU_UNITTEST') && !@file_exists($finalpath)) {
5784761d30cSAndreas Gohr        return false;
57900976812SAndreas Gohr    }
5804761d30cSAndreas Gohr    return $finalpath;
58100976812SAndreas Gohr}
58200976812SAndreas Gohr
58300976812SAndreas Gohr
58400976812SAndreas Gohr
585