xref: /dokuwiki/inc/init.php (revision 16905344219a6293705b71cd526fad3ba07b04eb)
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
40*16905344SAndreas Gohr// load libraries
41*16905344SAndreas Gohrrequire_once(DOKU_INC.'inc/load.php');
42*16905344SAndreas Gohr
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)) {
57cb043f52SChris Smith    $config_cascade = array(
58cb043f52SChris Smith            'main' => array(
59cb043f52SChris Smith                'default'   => array(DOKU_CONF.'dokuwiki.php'),
60cb043f52SChris Smith                'local'     => array(DOKU_CONF.'local.php'),
61cb043f52SChris Smith                'protected' => array(DOKU_CONF.'local.protected.php'),
62cb043f52SChris Smith                ),
63cb043f52SChris Smith            'acronyms'  => array(
64b303b92cSChris Smith                'default'   => array(DOKU_CONF.'acronyms.conf'),
65b303b92cSChris Smith                'local'     => array(DOKU_CONF.'acronyms.local.conf'),
66cb043f52SChris Smith                ),
67cb043f52SChris Smith            'entities'  => array(
68b303b92cSChris Smith                'default'   => array(DOKU_CONF.'entities.conf'),
69b303b92cSChris Smith                'local'     => array(DOKU_CONF.'entities.local.conf'),
70cb043f52SChris Smith                ),
71cb043f52SChris Smith            'interwiki' => array(
72b303b92cSChris Smith                'default'   => array(DOKU_CONF.'interwiki.conf'),
73b303b92cSChris Smith                'local'     => array(DOKU_CONF.'interwiki.local.conf'),
74cb043f52SChris Smith                ),
75f8121585SChris Smith            'license' => array(
76f8121585SChris Smith                'default'   => array(DOKU_CONF.'license.php'),
77f8121585SChris Smith                'local'     => array(DOKU_CONF.'license.local.php'),
78f8121585SChris Smith                ),
79f8121585SChris Smith            'mediameta' => array(
80f8121585SChris Smith                    'default'   => array(DOKU_CONF.'mediameta.php'),
81f8121585SChris Smith                    'local'     => array(DOKU_CONF.'mediameta.local.php'),
82f8121585SChris Smith                    ),
83cb043f52SChris Smith            'mime'      => array(
84b303b92cSChris Smith                    'default'   => array(DOKU_CONF.'mime.conf'),
85b303b92cSChris Smith                    'local'     => array(DOKU_CONF.'mime.local.conf'),
86cb043f52SChris Smith                    ),
87cb043f52SChris Smith            'scheme'    => array(
88b303b92cSChris Smith                    'default'   => array(DOKU_CONF.'scheme.conf'),
89b303b92cSChris Smith                    'local'     => array(DOKU_CONF.'scheme.local.conf'),
90cb043f52SChris Smith                    ),
91cb043f52SChris Smith            'smileys'   => array(
92b303b92cSChris Smith                    'default'   => array(DOKU_CONF.'smileys.conf'),
93b303b92cSChris Smith                    'local'     => array(DOKU_CONF.'smileys.local.conf'),
94cb043f52SChris Smith                    ),
95cb043f52SChris Smith            'wordblock' => array(
96b303b92cSChris Smith                    'default'   => array(DOKU_CONF.'wordblock.conf'),
97b303b92cSChris Smith                    'local'     => array(DOKU_CONF.'wordblock.local.conf'),
98cb043f52SChris Smith                    ),
99cb043f52SChris Smith            );
100cb043f52SChris Smith}
101cb043f52SChris Smith
1024724a577Sandi//prepare config array()
103ee20e7d1Sandiglobal $conf;
1044724a577Sandi$conf = array();
1054724a577Sandi
106cb043f52SChris Smith// load the global config file(s)
107b303b92cSChris Smithforeach (array('default','local','protected') as $config_group) {
108f8121585SChris Smith    if (empty($config_cascade['main'][$config_group])) continue;
109b303b92cSChris Smith    foreach ($config_cascade['main'][$config_group] as $config_file) {
110f8121585SChris Smith        if (@file_exists($config_file)) {
111f8121585SChris Smith            include($config_file);
112f8121585SChris Smith        }
113cb043f52SChris Smith    }
1140a6ead41SAndreas Gohr}
115ad15db82Sandi
116ad15db82Sandi//prepare language array
117ee20e7d1Sandiglobal $lang;
118ad15db82Sandi$lang = array();
119ed7b5f09Sandi
12016521111Sandi//load the language files
121bc3b6aecSandirequire_once(DOKU_INC.'inc/lang/en/lang.php');
122f949a01cSAndreas Gohrif ( $conf['lang'] && $conf['lang'] != 'en' ) {
123bc3b6aecSandi    require_once(DOKU_INC.'inc/lang/'.$conf['lang'].'/lang.php');
124fc1c55b1Shfuecks}
12516521111Sandi
126066fee30SAndreas Gohr//prepare license array()
127066fee30SAndreas Gohrglobal $license;
128066fee30SAndreas Gohr$license = array();
129066fee30SAndreas Gohr
130066fee30SAndreas Gohr// load the license file(s)
131f8121585SChris Smithforeach (array('default','local') as $config_group) {
132f8121585SChris Smith    if (empty($config_cascade['license'][$config_group])) continue;
133f8121585SChris Smith    foreach ($config_cascade['license'][$config_group] as $config_file) {
134f8121585SChris Smith        if(@file_exists($config_file)){
135f8121585SChris Smith            include($config_file);
136f8121585SChris Smith        }
137f8121585SChris Smith    }
138066fee30SAndreas Gohr}
139066fee30SAndreas Gohr
1401f8eb24fSAndreas Gohr// set timezone (as in pre 5.3.0 days)
1411f8eb24fSAndreas Gohrdate_default_timezone_set(@date_default_timezone_get());
1421f8eb24fSAndreas Gohr
143ed7b5f09Sandi// define baseURL
1444b1a4e04SAndreas Gohrif(!defined('DOKU_REL')) define('DOKU_REL',getBaseURL(false));
145ed7b5f09Sandiif(!defined('DOKU_URL')) define('DOKU_URL',getBaseURL(true));
1464b1a4e04SAndreas Gohrif(!defined('DOKU_BASE')){
1474b1a4e04SAndreas Gohr    if($conf['canonical']){
1484b1a4e04SAndreas Gohr        define('DOKU_BASE',DOKU_URL);
1494b1a4e04SAndreas Gohr    }else{
1504b1a4e04SAndreas Gohr        define('DOKU_BASE',DOKU_REL);
1514b1a4e04SAndreas Gohr    }
1524b1a4e04SAndreas Gohr}
1534b1a4e04SAndreas Gohr
154b8595a66SAndreas Gohr// define whitespace
155b8595a66SAndreas Gohrif(!defined('DOKU_LF')) define ('DOKU_LF',"\n");
156b8595a66SAndreas Gohrif(!defined('DOKU_TAB')) define ('DOKU_TAB',"\t");
157ed7b5f09Sandi
158656c8fb3SAndreas Gohr// define cookie and session id, append server port when securecookie is configured FS#1664
159656c8fb3SAndreas Gohrif (!defined('DOKU_COOKIE')) define('DOKU_COOKIE', 'DW'.md5(DOKU_REL.(($conf['securecookie'])?$_SERVER['SERVER_PORT']:'')));
160e71ce681SAndreas Gohr
161ee20e7d1Sandi// define Plugin dir
162f62ea8a1Sandiif(!defined('DOKU_PLUGIN'))  define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
163ee20e7d1Sandi
164ed7b5f09Sandi// define main script
165ed7b5f09Sandiif(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php');
166ed7b5f09Sandi
1676b13307fSandi// define Template baseURL
1686b13307fSandiif(!defined('DOKU_TPL')) define('DOKU_TPL',
169f62ea8a1Sandi        DOKU_BASE.'lib/tpl/'.$conf['template'].'/');
1706b13307fSandi
17178a6aeb1SAndreas Gohr// define real Template directory
17278a6aeb1SAndreas Gohrif(!defined('DOKU_TPLINC')) define('DOKU_TPLINC',
17378a6aeb1SAndreas Gohr        DOKU_INC.'lib/tpl/'.$conf['template'].'/');
17478a6aeb1SAndreas Gohr
175ed7b5f09Sandi// make session rewrites XHTML compliant
1763fc74836Sandi@ini_set('arg_separator.output', '&amp;');
177ed7b5f09Sandi
178d7e6bba9SAndreas Gohr// make sure global zlib does not interfere FS#1132
179d7e6bba9SAndreas Gohr@ini_set('zlib.output_compression', 'off');
180d7e6bba9SAndreas Gohr
1816deb5405SAndreas Gohr// increase PCRE backtrack limit
1826deb5405SAndreas Gohr@ini_set('pcre.backtrack_limit', '20971520');
1836deb5405SAndreas Gohr
18498bda4fdSAndreas Gohr// enable gzip compression if supported
18598bda4fdSAndreas Gohr$conf['gzip_output'] &= (strpos($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip') !== false);
1863138b5c7SAndreas Gohrif ($conf['gzip_output'] &&
1873138b5c7SAndreas Gohr        !defined('DOKU_DISABLE_GZIP_OUTPUT') &&
18898bda4fdSAndreas Gohr        function_exists('ob_gzhandler')) {
1893138b5c7SAndreas Gohr    ob_start('ob_gzhandler');
1903138b5c7SAndreas Gohr}
1913138b5c7SAndreas Gohr
192ed7b5f09Sandi// init session
1936534245aSAndreas Gohrif (!headers_sent() && !defined('NOSESSION')){
194ed7b5f09Sandi    session_name("DokuWiki");
195f5c6743cSAndreas Gohr    if (version_compare(PHP_VERSION, '5.2.0', '>')) {
196f5c6743cSAndreas Gohr        session_set_cookie_params(0,DOKU_REL,'',($conf['securecookie'] && is_ssl()),true);
197f5c6743cSAndreas Gohr    }else{
198f5c6743cSAndreas Gohr        session_set_cookie_params(0,DOKU_REL,'',($conf['securecookie'] && is_ssl()));
199f5c6743cSAndreas Gohr    }
200bad31ae9SAndreas Gohr    session_start();
20114a122deSAndreas Gohr
20214a122deSAndreas Gohr    // load left over messages
20314a122deSAndreas Gohr    if(isset($_SESSION[DOKU_COOKIE]['msg'])){
20414a122deSAndreas Gohr        $MSG = $_SESSION[DOKU_COOKIE]['msg'];
20514a122deSAndreas Gohr        unset($_SESSION[DOKU_COOKIE]['msg']);
20614a122deSAndreas Gohr    }
207bad31ae9SAndreas Gohr}
208ed7b5f09Sandi
209ed7b5f09Sandi// kill magic quotes
210e55eb89cSAndreas Gohrif (get_magic_quotes_gpc() && !defined('MAGIC_QUOTES_STRIPPED')) {
211ed7b5f09Sandi    if (!empty($_GET))    remove_magic_quotes($_GET);
212ed7b5f09Sandi    if (!empty($_POST))   remove_magic_quotes($_POST);
213ed7b5f09Sandi    if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE);
214ed7b5f09Sandi    if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST);
2153fc74836Sandi    @ini_set('magic_quotes_gpc', 0);
216e55eb89cSAndreas Gohr    define('MAGIC_QUOTES_STRIPPED',1);
217ed7b5f09Sandi}
2183fc74836Sandi@set_magic_quotes_runtime(0);
2193fc74836Sandi@ini_set('magic_quotes_sybase',0);
220ed7b5f09Sandi
221a1637ffdSAndreas Gohr// don't let cookies ever interfere with request vars
222a1637ffdSAndreas Gohr$_REQUEST = array_merge($_GET,$_POST);
223a1637ffdSAndreas Gohr
2243dea4ebcSAndreas Gohr// we don't want a purge URL to be digged
225c66972f2SAdrian Langif(isset($_REQUEST['purge']) && $_SERVER['HTTP_REFERER']) unset($_REQUEST['purge']);
2263dea4ebcSAndreas Gohr
227ed7b5f09Sandi// disable gzip if not available
2288a447e5cSAndreas Gohrif($conf['compression'] == 'bz2' && !function_exists('bzopen')){
229fe893490SAndreas Gohr    $conf['compression'] = 'gz';
230501252a5SAndreas Gohr}
231fe893490SAndreas Gohrif($conf['compression'] == 'gz' && !function_exists('gzopen')){
232501252a5SAndreas Gohr    $conf['compression'] = 0;
233ed7b5f09Sandi}
234ed7b5f09Sandi
235e656dcd4SAndreas Gohr// fix dateformat for upgraders
236e656dcd4SAndreas Gohrif(strpos($conf['dformat'],'%') === false){
237e656dcd4SAndreas Gohr    $conf['dformat'] = '%Y/%m/%d %H:%M';
238e656dcd4SAndreas Gohr}
239e656dcd4SAndreas Gohr
2401ca31cfeSAndreas Gohr// precalculate file creation modes
2411ca31cfeSAndreas Gohrinit_creationmodes();
242ed7b5f09Sandi
2433dc3a5f1Sandi// make real paths and check them
24498407a7aSandiinit_paths();
2457367b368SAndreas Gohrinit_files();
246ed7b5f09Sandi
2478c4f28e8Sjan// automatic upgrade to script versions of certain files
248e7cb32dcSAndreas Gohrscriptify(DOKU_CONF.'users.auth');
249e7cb32dcSAndreas Gohrscriptify(DOKU_CONF.'acl.auth');
250f62ea8a1Sandi
251*16905344SAndreas Gohr// setup authentication system
252*16905344SAndreas Gohrauth_setup();
253f62ea8a1Sandi
254f62ea8a1Sandi/**
25598407a7aSandi * Checks paths from config file
25698407a7aSandi */
25798407a7aSandifunction init_paths(){
25898407a7aSandi    global $conf;
25998407a7aSandi
26098407a7aSandi    $paths = array('datadir'   => 'pages',
26198407a7aSandi            'olddir'    => 'attic',
26298407a7aSandi            'mediadir'  => 'media',
26398407a7aSandi            'metadir'   => 'meta',
26498407a7aSandi            'cachedir'  => 'cache',
265579b0f7eSTNHarris            'indexdir'  => 'index',
266de33a58fSMichael Klier            'lockdir'   => 'locks',
267de33a58fSMichael Klier            'tmpdir'    => 'tmp');
26898407a7aSandi
26998407a7aSandi    foreach($paths as $c => $p){
270bb4866bdSchris        if(empty($conf[$c]))  $conf[$c] = $conf['savedir'].'/'.$p;
27198407a7aSandi        $conf[$c]             = init_path($conf[$c]);
2721983acc2SGuy Brand        if(empty($conf[$c]))  nice_die("The $c ('$p') does not exist, isn't accessible or writable.
27369dc3177SAndreas Gohr                You should check your config and permission settings.
27469dc3177SAndreas Gohr                Or maybe you want to <a href=\"install.php\">run the
27569dc3177SAndreas Gohr                installer</a>?");
27698407a7aSandi    }
27771726d78SBen Coburn
27871726d78SBen Coburn    // path to old changelog only needed for upgrading
27971726d78SBen Coburn    $conf['changelog_old'] = init_path((isset($conf['changelog']))?($conf['changelog']):($conf['savedir'].'/changes.log'));
28071726d78SBen Coburn    if ($conf['changelog_old']=='') { unset($conf['changelog_old']); }
28171726d78SBen Coburn    // hardcoded changelog because it is now a cache that lives in meta
28271726d78SBen Coburn    $conf['changelog'] = $conf['metadir'].'/_dokuwiki.changes';
28399c8d7f2Smichael    $conf['media_changelog'] = $conf['metadir'].'/_media.changes';
28498407a7aSandi}
28598407a7aSandi
28698407a7aSandi/**
2870d8850c4SAndreas Gohr * Checks the existance of certain files and creates them if missing.
2887367b368SAndreas Gohr */
2897367b368SAndreas Gohrfunction init_files(){
2907367b368SAndreas Gohr    global $conf;
2910d8850c4SAndreas Gohr
292579b0f7eSTNHarris    $files = array( $conf['indexdir'].'/page.idx');
2937367b368SAndreas Gohr
2947367b368SAndreas Gohr    foreach($files as $file){
2957367b368SAndreas Gohr        if(!@file_exists($file)){
2960d8850c4SAndreas Gohr            $fh = @fopen($file,'a');
2970d8850c4SAndreas Gohr            if($fh){
2987367b368SAndreas Gohr                fclose($fh);
2991ca31cfeSAndreas Gohr                if($conf['fperm']) chmod($file, $conf['fperm']);
3000d8850c4SAndreas Gohr            }else{
3013816dcbcSAndreas Gohr                nice_die("$file is not writable. Check your permissions settings!");
3020d8850c4SAndreas Gohr            }
3037367b368SAndreas Gohr        }
3047367b368SAndreas Gohr    }
3057367b368SAndreas Gohr}
3067367b368SAndreas Gohr
3077367b368SAndreas Gohr/**
3080d8850c4SAndreas Gohr * Returns absolute path
309f62ea8a1Sandi *
3100d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC.
3110d8850c4SAndreas Gohr * Check for accessability on directories as well.
3120d8850c4SAndreas Gohr *
3130d8850c4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
314f62ea8a1Sandi */
315f62ea8a1Sandifunction init_path($path){
3160d8850c4SAndreas Gohr    // check existance
31700976812SAndreas Gohr    $p = fullpath($path);
3180d8850c4SAndreas Gohr    if(!@file_exists($p)){
31900976812SAndreas Gohr        $p = fullpath(DOKU_INC.$path);
3200d8850c4SAndreas Gohr        if(!@file_exists($p)){
3218fc4e739Sandi            return '';
322f62ea8a1Sandi        }
3230d8850c4SAndreas Gohr    }
3240d8850c4SAndreas Gohr
3250d8850c4SAndreas Gohr    // check writability
3260d8850c4SAndreas Gohr    if(!@is_writable($p)){
3270d8850c4SAndreas Gohr        return '';
3280d8850c4SAndreas Gohr    }
3290d8850c4SAndreas Gohr
3300d8850c4SAndreas Gohr    // check accessability (execute bit) for directories
3310d8850c4SAndreas Gohr    if(@is_dir($p) && !@file_exists("$p/.")){
3320d8850c4SAndreas Gohr        return '';
3330d8850c4SAndreas Gohr    }
3340d8850c4SAndreas Gohr
3350d8850c4SAndreas Gohr    return $p;
3360d8850c4SAndreas Gohr}
3378c4f28e8Sjan
338ed7b5f09Sandi/**
3391ca31cfeSAndreas Gohr * Sets the internal config values fperm and dperm which, when set,
3401ca31cfeSAndreas Gohr * will be used to change the permission of a newly created dir or
3411ca31cfeSAndreas Gohr * file with chmod. Considers the influence of the system's umask
3421ca31cfeSAndreas Gohr * setting the values only if needed.
3431ca31cfeSAndreas Gohr */
3441ca31cfeSAndreas Gohrfunction init_creationmodes(){
3451ca31cfeSAndreas Gohr    global $conf;
3461ca31cfeSAndreas Gohr
3471ca31cfeSAndreas Gohr    // Legacy support for old umask/dmask scheme
3481ca31cfeSAndreas Gohr    unset($conf['dmask']);
3491ca31cfeSAndreas Gohr    unset($conf['fmask']);
3501ca31cfeSAndreas Gohr    unset($conf['umask']);
3511ca31cfeSAndreas Gohr    unset($conf['fperm']);
3521ca31cfeSAndreas Gohr    unset($conf['dperm']);
3531ca31cfeSAndreas Gohr
3549f3cdec3SAndreas Gohr    // get system umask, fallback to 0 if none available
3559f3cdec3SAndreas Gohr    $umask = @umask();
3569f3cdec3SAndreas Gohr    if(!$umask) $umask = 0000;
3571ca31cfeSAndreas Gohr
3581ca31cfeSAndreas Gohr    // check what is set automatically by the system on file creation
3591ca31cfeSAndreas Gohr    // and set the fperm param if it's not what we want
3601ca31cfeSAndreas Gohr    $auto_fmode = 0666 & ~$umask;
3611ca31cfeSAndreas Gohr    if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode'];
3621ca31cfeSAndreas Gohr
3631ca31cfeSAndreas Gohr    // check what is set automatically by the system on file creation
3641ca31cfeSAndreas Gohr    // and set the dperm param if it's not what we want
3651ca31cfeSAndreas Gohr    $auto_dmode = $conf['dmode'] & ~$umask;
3661ca31cfeSAndreas Gohr    if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode'];
3671ca31cfeSAndreas Gohr}
3681ca31cfeSAndreas Gohr
3691ca31cfeSAndreas Gohr/**
370ed7b5f09Sandi * remove magic quotes recursivly
371ed7b5f09Sandi *
372ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
373ed7b5f09Sandi */
374ed7b5f09Sandifunction remove_magic_quotes(&$array) {
375ed7b5f09Sandi    foreach (array_keys($array) as $key) {
37681c54349SAndreas Gohr        // handle magic quotes in keynames (breaks order)
37781c54349SAndreas Gohr        $sk = stripslashes($key);
37881c54349SAndreas Gohr        if($sk != $key){
37981c54349SAndreas Gohr            $array[$sk] = $array[$key];
38081c54349SAndreas Gohr            unset($array[$key]);
38181c54349SAndreas Gohr            $key = $sk;
38281c54349SAndreas Gohr        }
38381c54349SAndreas Gohr
38481c54349SAndreas Gohr        // do recursion if needed
385ed7b5f09Sandi        if (is_array($array[$key])) {
386ed7b5f09Sandi            remove_magic_quotes($array[$key]);
387ed7b5f09Sandi        }else {
388ed7b5f09Sandi            $array[$key] = stripslashes($array[$key]);
389ed7b5f09Sandi        }
390ed7b5f09Sandi    }
391ed7b5f09Sandi}
392ed7b5f09Sandi
393ed7b5f09Sandi/**
394ed7b5f09Sandi * Returns the full absolute URL to the directory where
395ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash)
396ed7b5f09Sandi *
397ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
398ed7b5f09Sandi */
3994b1a4e04SAndreas Gohrfunction getBaseURL($abs=null){
400ed7b5f09Sandi    global $conf;
401ed7b5f09Sandi    //if canonical url enabled always return absolute
4024b1a4e04SAndreas Gohr    if(is_null($abs)) $abs = $conf['canonical'];
403ed7b5f09Sandi
40492b83b77Sandi    if($conf['basedir']){
40546c73e01SChris Smith        $dir = $conf['basedir'];
40689aa05dbSAndreas Gohr    }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){
40746c73e01SChris Smith        $dir = dirname($_SERVER['SCRIPT_NAME']);
40889aa05dbSAndreas Gohr    }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){
40946c73e01SChris Smith        $dir = dirname($_SERVER['PHP_SELF']);
410093ec9e4Sandi    }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
411093ec9e4Sandi        $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
412093ec9e4Sandi                $_SERVER['SCRIPT_FILENAME']);
41346c73e01SChris Smith        $dir = dirname('/'.$dir);
41492b83b77Sandi    }else{
41546c73e01SChris Smith        $dir = '.'; //probably wrong
41692b83b77Sandi    }
417ed7b5f09Sandi
41846c73e01SChris Smith    $dir = str_replace('\\','/',$dir);             // bugfix for weird WIN behaviour
41946c73e01SChris Smith    $dir = preg_replace('#//+#','/',"/$dir/");     // ensure leading and trailing slashes
420ed7b5f09Sandi
421f62ea8a1Sandi    //handle script in lib/exe dir
422f62ea8a1Sandi    $dir = preg_replace('!lib/exe/$!','',$dir);
423f62ea8a1Sandi
424488d5fa0SMichael Klier chi@chimeric.de    //handle script in lib/plugins dir
425488d5fa0SMichael Klier chi@chimeric.de    $dir = preg_replace('!lib/plugins/.*$!','',$dir);
426488d5fa0SMichael Klier chi@chimeric.de
427ed7b5f09Sandi    //finish here for relative URLs
428ed7b5f09Sandi    if(!$abs) return $dir;
429ed7b5f09Sandi
43046c73e01SChris Smith    //use config option if available, trim any slash from end of baseurl to avoid multiple consecutive slashes in the path
43146c73e01SChris Smith    if($conf['baseurl']) return rtrim($conf['baseurl'],'/').$dir;
432ef7b3ecdSAndreas Gohr
433e82e3526SAndreas Gohr    //split hostheader into host and port
434c66972f2SAdrian Lang    $addr = explode(':',$_SERVER['HTTP_HOST']);
435c66972f2SAdrian Lang    $host = $addr[0];
436c66972f2SAdrian Lang    $port = '';
437c66972f2SAdrian Lang    if (isset($addr[1])) {
438c66972f2SAdrian Lang        $port = $addr[1];
439c66972f2SAdrian Lang    } elseif (isset($_SERVER['SERVER_PORT'])) {
440c66972f2SAdrian Lang        $port = $_SERVER['SERVER_PORT'];
441c66972f2SAdrian Lang    }
442f5c6743cSAndreas Gohr    if(!is_ssl()){
443ed7b5f09Sandi        $proto = 'http://';
444e82e3526SAndreas Gohr        if ($port == '80') {
445ed7b5f09Sandi            $port = '';
446ed7b5f09Sandi        }
447ed7b5f09Sandi    }else{
448ed7b5f09Sandi        $proto = 'https://';
449e82e3526SAndreas Gohr        if ($port == '443') {
450ed7b5f09Sandi            $port = '';
451ed7b5f09Sandi        }
452ed7b5f09Sandi    }
453ed7b5f09Sandi
454c66972f2SAdrian Lang    if($port !== '') $port = ':'.$port;
455e82e3526SAndreas Gohr
456ed7b5f09Sandi    return $proto.$host.$port.$dir;
457ed7b5f09Sandi}
458ed7b5f09Sandi
459b000c6d4Sandi/**
460f5c6743cSAndreas Gohr * Check if accessed via HTTPS
461f5c6743cSAndreas Gohr *
462f5c6743cSAndreas Gohr * Apache leaves ,$_SERVER['HTTPS'] empty when not available, IIS sets it to 'off'.
463f5c6743cSAndreas Gohr * 'false' and 'disabled' are just guessing
464f5c6743cSAndreas Gohr *
465f5c6743cSAndreas Gohr * @returns bool true when SSL is active
466f5c6743cSAndreas Gohr */
467f5c6743cSAndreas Gohrfunction is_ssl(){
468c66972f2SAdrian Lang    if (!isset($_SERVER['HTTPS']) ||
469c66972f2SAdrian Lang        preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){
470f5c6743cSAndreas Gohr        return false;
471f5c6743cSAndreas Gohr    }else{
472f5c6743cSAndreas Gohr        return true;
473f5c6743cSAndreas Gohr    }
474f5c6743cSAndreas Gohr}
475f5c6743cSAndreas Gohr
476f5c6743cSAndreas Gohr/**
477b000c6d4Sandi * Append a PHP extension to a given file and adds an exit call
478b000c6d4Sandi *
479b000c6d4Sandi * This is used to migrate some old configfiles. An added PHP extension
480b000c6d4Sandi * ensures the contents are not shown to webusers even if .htaccess files
481b000c6d4Sandi * do not work
482b000c6d4Sandi *
483b000c6d4Sandi * @author Jan Decaluwe <jan@jandecaluwe.com>
484b000c6d4Sandi */
4858c4f28e8Sjanfunction scriptify($file) {
4868c4f28e8Sjan    // checks
4878c4f28e8Sjan    if (!is_readable($file)) {
4888c4f28e8Sjan        return;
4898c4f28e8Sjan    }
4908c4f28e8Sjan    $fn = $file.'.php';
4918c4f28e8Sjan    if (@file_exists($fn)) {
4928c4f28e8Sjan        return;
4938c4f28e8Sjan    }
4948c4f28e8Sjan    $fh = fopen($fn, 'w');
4958c4f28e8Sjan    if (!$fh) {
4963816dcbcSAndreas Gohr        nice_die($fn.' is not writable. Check your permission settings!');
4978c4f28e8Sjan    }
4988c4f28e8Sjan    // write php exit hack first
4998c4f28e8Sjan    fwrite($fh, "# $fn\n");
5008c4f28e8Sjan    fwrite($fh, '# <?php exit()?>'."\n");
5018c4f28e8Sjan    fwrite($fh, "# Don't modify the lines above\n");
5028c4f28e8Sjan    fwrite($fh, "#\n");
5038c4f28e8Sjan    // copy existing lines
5048c4f28e8Sjan    $lines = file($file);
5058c4f28e8Sjan    foreach ($lines as $line){
5068c4f28e8Sjan        fwrite($fh, $line);
5078c4f28e8Sjan    }
5083ba793e2Sandi    fclose($fh);
509b000c6d4Sandi    //try to rename the old file
5103aee4c27SAndreas Gohr    io_rename($file,"$file.old");
5118c4f28e8Sjan}
5128c4f28e8Sjan
5133816dcbcSAndreas Gohr/**
5143816dcbcSAndreas Gohr * print a nice message even if no styles are loaded yet.
5153816dcbcSAndreas Gohr */
5163816dcbcSAndreas Gohrfunction nice_die($msg){
5173816dcbcSAndreas Gohr    echo<<<EOT
5183816dcbcSAndreas Gohr<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
5193816dcbcSAndreas Gohr    "http://www.w3.org/TR/html4/loose.dtd">
5203816dcbcSAndreas Gohr<html>
5213816dcbcSAndreas Gohr<head><title>DokuWiki Setup Error</title></head>
5223816dcbcSAndreas Gohr<body style="font-family: Arial, sans-serif">
5233816dcbcSAndreas Gohr    <div style="width:60%; margin: auto; background-color: #fcc;
5243816dcbcSAndreas Gohr                border: 1px solid #faa; padding: 0.5em 1em;">
5253816dcbcSAndreas Gohr        <h1 style="font-size: 120%">DokuWiki Setup Error</h1>
5263816dcbcSAndreas Gohr        <p>$msg</p>
5273816dcbcSAndreas Gohr    </div>
5283816dcbcSAndreas Gohr</body>
5293816dcbcSAndreas Gohr</html>
5303816dcbcSAndreas GohrEOT;
5313816dcbcSAndreas Gohr    exit;
5323816dcbcSAndreas Gohr}
5333816dcbcSAndreas Gohr
53400976812SAndreas Gohr/**
53500976812SAndreas Gohr * A realpath() replacement
53600976812SAndreas Gohr *
53700976812SAndreas Gohr * This function behaves similar to PHP's realpath() but does not resolve
53800976812SAndreas Gohr * symlinks or accesses upper directories
53900976812SAndreas Gohr *
5404761d30cSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
54100976812SAndreas Gohr * @author <richpageau at yahoo dot co dot uk>
54200976812SAndreas Gohr * @link   http://de3.php.net/manual/en/function.realpath.php#75992
54300976812SAndreas Gohr */
544b328697dSAndreas Gohrfunction fullpath($path,$exists=false){
5454761d30cSAndreas Gohr    static $run = 0;
5464761d30cSAndreas Gohr    $root  = '';
547f0a201c5SChris Smith    $iswin = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' || @$GLOBALS['DOKU_UNITTEST_ASSUME_WINDOWS']);
54800976812SAndreas Gohr
5494761d30cSAndreas Gohr    // find the (indestructable) root of the path - keeps windows stuff intact
5504761d30cSAndreas Gohr    if($path{0} == '/'){
5514761d30cSAndreas Gohr        $root = '/';
5524761d30cSAndreas Gohr    }elseif($iswin){
5534761d30cSAndreas Gohr        // match drive letter and UNC paths
5544761d30cSAndreas Gohr        if(preg_match('!^([a-zA-z]:)(.*)!',$path,$match)){
555b9c4302bSAndreas Gohr            $root = $match[1].'/';
5564761d30cSAndreas Gohr            $path = $match[2];
5574761d30cSAndreas Gohr        }else if(preg_match('!^(\\\\\\\\[^\\\\/]+\\\\[^\\\\/]+[\\\\/])(.*)!',$path,$match)){
5584761d30cSAndreas Gohr            $root = $match[1];
5594761d30cSAndreas Gohr            $path = $match[2];
56000976812SAndreas Gohr        }
5614761d30cSAndreas Gohr    }
5624761d30cSAndreas Gohr    $path = str_replace('\\','/',$path);
5634761d30cSAndreas Gohr
5644761d30cSAndreas Gohr    // if the given path wasn't absolute already, prepend the script path and retry
5654761d30cSAndreas Gohr    if(!$root){
5664761d30cSAndreas Gohr        $base = dirname($_SERVER['SCRIPT_FILENAME']);
5674761d30cSAndreas Gohr        $path = $base.'/'.$path;
5684761d30cSAndreas Gohr        if($run == 0){ // avoid endless recursion when base isn't absolute for some reason
5694761d30cSAndreas Gohr            $run++;
570b328697dSAndreas Gohr            return fullpath($path,$exists);
5714761d30cSAndreas Gohr        }
5724761d30cSAndreas Gohr    }
5734761d30cSAndreas Gohr    $run = 0;
57400976812SAndreas Gohr
57500976812SAndreas Gohr    // canonicalize
57600976812SAndreas Gohr    $path=explode('/', $path);
57700976812SAndreas Gohr    $newpath=array();
578ef38bfe8SAndreas Gohr    foreach($path as $p) {
579ef38bfe8SAndreas Gohr        if ($p === '' || $p === '.') continue;
580ef38bfe8SAndreas Gohr        if ($p==='..') {
58100976812SAndreas Gohr            array_pop($newpath);
58200976812SAndreas Gohr            continue;
58300976812SAndreas Gohr        }
584ef38bfe8SAndreas Gohr        array_push($newpath, $p);
58500976812SAndreas Gohr    }
5864761d30cSAndreas Gohr    $finalpath = $root.implode('/', $newpath);
58700976812SAndreas Gohr
588b328697dSAndreas Gohr    // check for existance when needed (except when unit testing)
589b328697dSAndreas Gohr    if($exists && !defined('DOKU_UNITTEST') && !@file_exists($finalpath)) {
5904761d30cSAndreas Gohr        return false;
59100976812SAndreas Gohr    }
5924761d30cSAndreas Gohr    return $finalpath;
59300976812SAndreas Gohr}
59400976812SAndreas Gohr
595