xref: /dokuwiki/inc/init.php (revision c2a6d81662045023bdf1617b6b49f71c274d55ca)
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
23*c2a6d816SAndreas Gohr// define Plugin dir
24*c2a6d816SAndreas Gohrif(!defined('DOKU_PLUGIN'))  define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
25*c2a6d816SAndreas 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
4316905344SAndreas Gohr// load libraries
4416905344SAndreas Gohrrequire_once(DOKU_INC.'inc/load.php');
4516905344SAndreas Gohr
4650602150SBen Coburn// init memory caches
47db959ae3SAndreas Gohrglobal $cache_revinfo;
48db959ae3SAndreas Gohr       $cache_revinfo = array();
49db959ae3SAndreas Gohrglobal $cache_wikifn;
50db959ae3SAndreas Gohr       $cache_wikifn = array();
51db959ae3SAndreas Gohrglobal $cache_cleanid;
52db959ae3SAndreas Gohr       $cache_cleanid = array();
53db959ae3SAndreas Gohrglobal $cache_authname;
54db959ae3SAndreas Gohr       $cache_authname = array();
55db959ae3SAndreas Gohrglobal $cache_metadata;
56db959ae3SAndreas Gohr       $cache_metadata = array();
5750602150SBen Coburn
58cb043f52SChris Smith//set the configuration cascade - but only if its not already been set in preload.php
59cb043f52SChris Smithif (empty($config_cascade)) {
60cb043f52SChris Smith    $config_cascade = array(
61cb043f52SChris Smith            'main' => array(
62cb043f52SChris Smith                'default'   => array(DOKU_CONF.'dokuwiki.php'),
63cb043f52SChris Smith                'local'     => array(DOKU_CONF.'local.php'),
64cb043f52SChris Smith                'protected' => array(DOKU_CONF.'local.protected.php'),
65cb043f52SChris Smith                ),
66cb043f52SChris Smith            'acronyms'  => array(
67b303b92cSChris Smith                'default'   => array(DOKU_CONF.'acronyms.conf'),
68b303b92cSChris Smith                'local'     => array(DOKU_CONF.'acronyms.local.conf'),
69cb043f52SChris Smith                ),
70cb043f52SChris Smith            'entities'  => array(
71b303b92cSChris Smith                'default'   => array(DOKU_CONF.'entities.conf'),
72b303b92cSChris Smith                'local'     => array(DOKU_CONF.'entities.local.conf'),
73cb043f52SChris Smith                ),
74cb043f52SChris Smith            'interwiki' => array(
75b303b92cSChris Smith                'default'   => array(DOKU_CONF.'interwiki.conf'),
76b303b92cSChris Smith                'local'     => array(DOKU_CONF.'interwiki.local.conf'),
77cb043f52SChris Smith                ),
78f8121585SChris Smith            'license' => array(
79f8121585SChris Smith                'default'   => array(DOKU_CONF.'license.php'),
80f8121585SChris Smith                'local'     => array(DOKU_CONF.'license.local.php'),
81f8121585SChris Smith                ),
82f8121585SChris Smith            'mediameta' => array(
83f8121585SChris Smith                    'default'   => array(DOKU_CONF.'mediameta.php'),
84f8121585SChris Smith                    'local'     => array(DOKU_CONF.'mediameta.local.php'),
85f8121585SChris Smith                    ),
86cb043f52SChris Smith            'mime'      => array(
87b303b92cSChris Smith                    'default'   => array(DOKU_CONF.'mime.conf'),
88b303b92cSChris Smith                    'local'     => array(DOKU_CONF.'mime.local.conf'),
89cb043f52SChris Smith                    ),
90cb043f52SChris Smith            'scheme'    => array(
91b303b92cSChris Smith                    'default'   => array(DOKU_CONF.'scheme.conf'),
92b303b92cSChris Smith                    'local'     => array(DOKU_CONF.'scheme.local.conf'),
93cb043f52SChris Smith                    ),
94cb043f52SChris Smith            'smileys'   => array(
95b303b92cSChris Smith                    'default'   => array(DOKU_CONF.'smileys.conf'),
96b303b92cSChris Smith                    'local'     => array(DOKU_CONF.'smileys.local.conf'),
97cb043f52SChris Smith                    ),
98cb043f52SChris Smith            'wordblock' => array(
99b303b92cSChris Smith                    'default'   => array(DOKU_CONF.'wordblock.conf'),
100b303b92cSChris Smith                    'local'     => array(DOKU_CONF.'wordblock.local.conf'),
101cb043f52SChris Smith                    ),
102cb043f52SChris Smith            );
103cb043f52SChris Smith}
104cb043f52SChris Smith
1054724a577Sandi//prepare config array()
106ee20e7d1Sandiglobal $conf;
1074724a577Sandi$conf = array();
1084724a577Sandi
109cb043f52SChris Smith// load the global config file(s)
110b303b92cSChris Smithforeach (array('default','local','protected') as $config_group) {
111f8121585SChris Smith    if (empty($config_cascade['main'][$config_group])) continue;
112b303b92cSChris Smith    foreach ($config_cascade['main'][$config_group] as $config_file) {
113f8121585SChris Smith        if (@file_exists($config_file)) {
114f8121585SChris Smith            include($config_file);
115f8121585SChris Smith        }
116cb043f52SChris Smith    }
1170a6ead41SAndreas Gohr}
118ad15db82Sandi
119ad15db82Sandi//prepare language array
120ee20e7d1Sandiglobal $lang;
121ad15db82Sandi$lang = array();
122ed7b5f09Sandi
12316521111Sandi//load the language files
124bc3b6aecSandirequire_once(DOKU_INC.'inc/lang/en/lang.php');
125f949a01cSAndreas Gohrif ( $conf['lang'] && $conf['lang'] != 'en' ) {
126bc3b6aecSandi    require_once(DOKU_INC.'inc/lang/'.$conf['lang'].'/lang.php');
127fc1c55b1Shfuecks}
12816521111Sandi
129066fee30SAndreas Gohr//prepare license array()
130066fee30SAndreas Gohrglobal $license;
131066fee30SAndreas Gohr$license = array();
132066fee30SAndreas Gohr
133066fee30SAndreas Gohr// load the license file(s)
134f8121585SChris Smithforeach (array('default','local') as $config_group) {
135f8121585SChris Smith    if (empty($config_cascade['license'][$config_group])) continue;
136f8121585SChris Smith    foreach ($config_cascade['license'][$config_group] as $config_file) {
137f8121585SChris Smith        if(@file_exists($config_file)){
138f8121585SChris Smith            include($config_file);
139f8121585SChris Smith        }
140f8121585SChris Smith    }
141066fee30SAndreas Gohr}
142066fee30SAndreas Gohr
1431f8eb24fSAndreas Gohr// set timezone (as in pre 5.3.0 days)
1441f8eb24fSAndreas Gohrdate_default_timezone_set(@date_default_timezone_get());
1451f8eb24fSAndreas Gohr
146ed7b5f09Sandi// define baseURL
1474b1a4e04SAndreas Gohrif(!defined('DOKU_REL')) define('DOKU_REL',getBaseURL(false));
148ed7b5f09Sandiif(!defined('DOKU_URL')) define('DOKU_URL',getBaseURL(true));
1494b1a4e04SAndreas Gohrif(!defined('DOKU_BASE')){
1504b1a4e04SAndreas Gohr    if($conf['canonical']){
1514b1a4e04SAndreas Gohr        define('DOKU_BASE',DOKU_URL);
1524b1a4e04SAndreas Gohr    }else{
1534b1a4e04SAndreas Gohr        define('DOKU_BASE',DOKU_REL);
1544b1a4e04SAndreas Gohr    }
1554b1a4e04SAndreas Gohr}
1564b1a4e04SAndreas Gohr
157b8595a66SAndreas Gohr// define whitespace
158b8595a66SAndreas Gohrif(!defined('DOKU_LF')) define ('DOKU_LF',"\n");
159b8595a66SAndreas Gohrif(!defined('DOKU_TAB')) define ('DOKU_TAB',"\t");
160ed7b5f09Sandi
161656c8fb3SAndreas Gohr// define cookie and session id, append server port when securecookie is configured FS#1664
162656c8fb3SAndreas Gohrif (!defined('DOKU_COOKIE')) define('DOKU_COOKIE', 'DW'.md5(DOKU_REL.(($conf['securecookie'])?$_SERVER['SERVER_PORT']:'')));
163e71ce681SAndreas Gohr
164ee20e7d1Sandi
165ed7b5f09Sandi// define main script
166ed7b5f09Sandiif(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php');
167ed7b5f09Sandi
1686b13307fSandi// define Template baseURL
1696b13307fSandiif(!defined('DOKU_TPL')) define('DOKU_TPL',
170f62ea8a1Sandi        DOKU_BASE.'lib/tpl/'.$conf['template'].'/');
1716b13307fSandi
17278a6aeb1SAndreas Gohr// define real Template directory
17378a6aeb1SAndreas Gohrif(!defined('DOKU_TPLINC')) define('DOKU_TPLINC',
17478a6aeb1SAndreas Gohr        DOKU_INC.'lib/tpl/'.$conf['template'].'/');
17578a6aeb1SAndreas Gohr
176ed7b5f09Sandi// make session rewrites XHTML compliant
1773fc74836Sandi@ini_set('arg_separator.output', '&amp;');
178ed7b5f09Sandi
179d7e6bba9SAndreas Gohr// make sure global zlib does not interfere FS#1132
180d7e6bba9SAndreas Gohr@ini_set('zlib.output_compression', 'off');
181d7e6bba9SAndreas Gohr
1826deb5405SAndreas Gohr// increase PCRE backtrack limit
1836deb5405SAndreas Gohr@ini_set('pcre.backtrack_limit', '20971520');
1846deb5405SAndreas Gohr
18598bda4fdSAndreas Gohr// enable gzip compression if supported
18698bda4fdSAndreas Gohr$conf['gzip_output'] &= (strpos($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip') !== false);
1873138b5c7SAndreas Gohrif ($conf['gzip_output'] &&
1883138b5c7SAndreas Gohr        !defined('DOKU_DISABLE_GZIP_OUTPUT') &&
18998bda4fdSAndreas Gohr        function_exists('ob_gzhandler')) {
1903138b5c7SAndreas Gohr    ob_start('ob_gzhandler');
1913138b5c7SAndreas Gohr}
1923138b5c7SAndreas Gohr
193ed7b5f09Sandi// init session
1946534245aSAndreas Gohrif (!headers_sent() && !defined('NOSESSION')){
195ed7b5f09Sandi    session_name("DokuWiki");
196f5c6743cSAndreas Gohr    if (version_compare(PHP_VERSION, '5.2.0', '>')) {
197f5c6743cSAndreas Gohr        session_set_cookie_params(0,DOKU_REL,'',($conf['securecookie'] && is_ssl()),true);
198f5c6743cSAndreas Gohr    }else{
199f5c6743cSAndreas Gohr        session_set_cookie_params(0,DOKU_REL,'',($conf['securecookie'] && is_ssl()));
200f5c6743cSAndreas Gohr    }
201bad31ae9SAndreas Gohr    session_start();
20214a122deSAndreas Gohr
20314a122deSAndreas Gohr    // load left over messages
20414a122deSAndreas Gohr    if(isset($_SESSION[DOKU_COOKIE]['msg'])){
20514a122deSAndreas Gohr        $MSG = $_SESSION[DOKU_COOKIE]['msg'];
20614a122deSAndreas Gohr        unset($_SESSION[DOKU_COOKIE]['msg']);
20714a122deSAndreas Gohr    }
208bad31ae9SAndreas Gohr}
209ed7b5f09Sandi
210ed7b5f09Sandi// kill magic quotes
211e55eb89cSAndreas Gohrif (get_magic_quotes_gpc() && !defined('MAGIC_QUOTES_STRIPPED')) {
212ed7b5f09Sandi    if (!empty($_GET))    remove_magic_quotes($_GET);
213ed7b5f09Sandi    if (!empty($_POST))   remove_magic_quotes($_POST);
214ed7b5f09Sandi    if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE);
215ed7b5f09Sandi    if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST);
2163fc74836Sandi    @ini_set('magic_quotes_gpc', 0);
217e55eb89cSAndreas Gohr    define('MAGIC_QUOTES_STRIPPED',1);
218ed7b5f09Sandi}
2193fc74836Sandi@set_magic_quotes_runtime(0);
2203fc74836Sandi@ini_set('magic_quotes_sybase',0);
221ed7b5f09Sandi
222a1637ffdSAndreas Gohr// don't let cookies ever interfere with request vars
223a1637ffdSAndreas Gohr$_REQUEST = array_merge($_GET,$_POST);
224a1637ffdSAndreas Gohr
2253dea4ebcSAndreas Gohr// we don't want a purge URL to be digged
226c66972f2SAdrian Langif(isset($_REQUEST['purge']) && $_SERVER['HTTP_REFERER']) unset($_REQUEST['purge']);
2273dea4ebcSAndreas Gohr
228ed7b5f09Sandi// disable gzip if not available
2298a447e5cSAndreas Gohrif($conf['compression'] == 'bz2' && !function_exists('bzopen')){
230fe893490SAndreas Gohr    $conf['compression'] = 'gz';
231501252a5SAndreas Gohr}
232fe893490SAndreas Gohrif($conf['compression'] == 'gz' && !function_exists('gzopen')){
233501252a5SAndreas Gohr    $conf['compression'] = 0;
234ed7b5f09Sandi}
235ed7b5f09Sandi
236e656dcd4SAndreas Gohr// fix dateformat for upgraders
237e656dcd4SAndreas Gohrif(strpos($conf['dformat'],'%') === false){
238e656dcd4SAndreas Gohr    $conf['dformat'] = '%Y/%m/%d %H:%M';
239e656dcd4SAndreas Gohr}
240e656dcd4SAndreas Gohr
2411ca31cfeSAndreas Gohr// precalculate file creation modes
2421ca31cfeSAndreas Gohrinit_creationmodes();
243ed7b5f09Sandi
2443dc3a5f1Sandi// make real paths and check them
24598407a7aSandiinit_paths();
2467367b368SAndreas Gohrinit_files();
247ed7b5f09Sandi
2488c4f28e8Sjan// automatic upgrade to script versions of certain files
249e7cb32dcSAndreas Gohrscriptify(DOKU_CONF.'users.auth');
250e7cb32dcSAndreas Gohrscriptify(DOKU_CONF.'acl.auth');
251f62ea8a1Sandi
25216905344SAndreas Gohr// setup authentication system
25316905344SAndreas Gohrauth_setup();
254f62ea8a1Sandi
255f62ea8a1Sandi/**
25698407a7aSandi * Checks paths from config file
25798407a7aSandi */
25898407a7aSandifunction init_paths(){
25998407a7aSandi    global $conf;
26098407a7aSandi
26198407a7aSandi    $paths = array('datadir'   => 'pages',
26298407a7aSandi            'olddir'    => 'attic',
26398407a7aSandi            'mediadir'  => 'media',
26498407a7aSandi            'metadir'   => 'meta',
26598407a7aSandi            'cachedir'  => 'cache',
266579b0f7eSTNHarris            'indexdir'  => 'index',
267de33a58fSMichael Klier            'lockdir'   => 'locks',
268de33a58fSMichael Klier            'tmpdir'    => 'tmp');
26998407a7aSandi
27098407a7aSandi    foreach($paths as $c => $p){
271bb4866bdSchris        if(empty($conf[$c]))  $conf[$c] = $conf['savedir'].'/'.$p;
27298407a7aSandi        $conf[$c]             = init_path($conf[$c]);
2731983acc2SGuy Brand        if(empty($conf[$c]))  nice_die("The $c ('$p') does not exist, isn't accessible or writable.
27469dc3177SAndreas Gohr                You should check your config and permission settings.
27569dc3177SAndreas Gohr                Or maybe you want to <a href=\"install.php\">run the
27669dc3177SAndreas Gohr                installer</a>?");
27798407a7aSandi    }
27871726d78SBen Coburn
27971726d78SBen Coburn    // path to old changelog only needed for upgrading
28071726d78SBen Coburn    $conf['changelog_old'] = init_path((isset($conf['changelog']))?($conf['changelog']):($conf['savedir'].'/changes.log'));
28171726d78SBen Coburn    if ($conf['changelog_old']=='') { unset($conf['changelog_old']); }
28271726d78SBen Coburn    // hardcoded changelog because it is now a cache that lives in meta
28371726d78SBen Coburn    $conf['changelog'] = $conf['metadir'].'/_dokuwiki.changes';
28499c8d7f2Smichael    $conf['media_changelog'] = $conf['metadir'].'/_media.changes';
28598407a7aSandi}
28698407a7aSandi
28798407a7aSandi/**
2880d8850c4SAndreas Gohr * Checks the existance of certain files and creates them if missing.
2897367b368SAndreas Gohr */
2907367b368SAndreas Gohrfunction init_files(){
2917367b368SAndreas Gohr    global $conf;
2920d8850c4SAndreas Gohr
293579b0f7eSTNHarris    $files = array( $conf['indexdir'].'/page.idx');
2947367b368SAndreas Gohr
2957367b368SAndreas Gohr    foreach($files as $file){
2967367b368SAndreas Gohr        if(!@file_exists($file)){
2970d8850c4SAndreas Gohr            $fh = @fopen($file,'a');
2980d8850c4SAndreas Gohr            if($fh){
2997367b368SAndreas Gohr                fclose($fh);
3001ca31cfeSAndreas Gohr                if($conf['fperm']) chmod($file, $conf['fperm']);
3010d8850c4SAndreas Gohr            }else{
3023816dcbcSAndreas Gohr                nice_die("$file is not writable. Check your permissions settings!");
3030d8850c4SAndreas Gohr            }
3047367b368SAndreas Gohr        }
3057367b368SAndreas Gohr    }
3067367b368SAndreas Gohr}
3077367b368SAndreas Gohr
3087367b368SAndreas Gohr/**
3090d8850c4SAndreas Gohr * Returns absolute path
310f62ea8a1Sandi *
3110d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC.
3120d8850c4SAndreas Gohr * Check for accessability on directories as well.
3130d8850c4SAndreas Gohr *
3140d8850c4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
315f62ea8a1Sandi */
316f62ea8a1Sandifunction init_path($path){
3170d8850c4SAndreas Gohr    // check existance
31800976812SAndreas Gohr    $p = fullpath($path);
3190d8850c4SAndreas Gohr    if(!@file_exists($p)){
32000976812SAndreas Gohr        $p = fullpath(DOKU_INC.$path);
3210d8850c4SAndreas Gohr        if(!@file_exists($p)){
3228fc4e739Sandi            return '';
323f62ea8a1Sandi        }
3240d8850c4SAndreas Gohr    }
3250d8850c4SAndreas Gohr
3260d8850c4SAndreas Gohr    // check writability
3270d8850c4SAndreas Gohr    if(!@is_writable($p)){
3280d8850c4SAndreas Gohr        return '';
3290d8850c4SAndreas Gohr    }
3300d8850c4SAndreas Gohr
3310d8850c4SAndreas Gohr    // check accessability (execute bit) for directories
3320d8850c4SAndreas Gohr    if(@is_dir($p) && !@file_exists("$p/.")){
3330d8850c4SAndreas Gohr        return '';
3340d8850c4SAndreas Gohr    }
3350d8850c4SAndreas Gohr
3360d8850c4SAndreas Gohr    return $p;
3370d8850c4SAndreas Gohr}
3388c4f28e8Sjan
339ed7b5f09Sandi/**
3401ca31cfeSAndreas Gohr * Sets the internal config values fperm and dperm which, when set,
3411ca31cfeSAndreas Gohr * will be used to change the permission of a newly created dir or
3421ca31cfeSAndreas Gohr * file with chmod. Considers the influence of the system's umask
3431ca31cfeSAndreas Gohr * setting the values only if needed.
3441ca31cfeSAndreas Gohr */
3451ca31cfeSAndreas Gohrfunction init_creationmodes(){
3461ca31cfeSAndreas Gohr    global $conf;
3471ca31cfeSAndreas Gohr
3481ca31cfeSAndreas Gohr    // Legacy support for old umask/dmask scheme
3491ca31cfeSAndreas Gohr    unset($conf['dmask']);
3501ca31cfeSAndreas Gohr    unset($conf['fmask']);
3511ca31cfeSAndreas Gohr    unset($conf['umask']);
3521ca31cfeSAndreas Gohr    unset($conf['fperm']);
3531ca31cfeSAndreas Gohr    unset($conf['dperm']);
3541ca31cfeSAndreas Gohr
3559f3cdec3SAndreas Gohr    // get system umask, fallback to 0 if none available
3569f3cdec3SAndreas Gohr    $umask = @umask();
3579f3cdec3SAndreas Gohr    if(!$umask) $umask = 0000;
3581ca31cfeSAndreas Gohr
3591ca31cfeSAndreas Gohr    // check what is set automatically by the system on file creation
3601ca31cfeSAndreas Gohr    // and set the fperm param if it's not what we want
3611ca31cfeSAndreas Gohr    $auto_fmode = 0666 & ~$umask;
3621ca31cfeSAndreas Gohr    if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode'];
3631ca31cfeSAndreas Gohr
3641ca31cfeSAndreas Gohr    // check what is set automatically by the system on file creation
3651ca31cfeSAndreas Gohr    // and set the dperm param if it's not what we want
3661ca31cfeSAndreas Gohr    $auto_dmode = $conf['dmode'] & ~$umask;
3671ca31cfeSAndreas Gohr    if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode'];
3681ca31cfeSAndreas Gohr}
3691ca31cfeSAndreas Gohr
3701ca31cfeSAndreas Gohr/**
371ed7b5f09Sandi * remove magic quotes recursivly
372ed7b5f09Sandi *
373ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
374ed7b5f09Sandi */
375ed7b5f09Sandifunction remove_magic_quotes(&$array) {
376ed7b5f09Sandi    foreach (array_keys($array) as $key) {
37781c54349SAndreas Gohr        // handle magic quotes in keynames (breaks order)
37881c54349SAndreas Gohr        $sk = stripslashes($key);
37981c54349SAndreas Gohr        if($sk != $key){
38081c54349SAndreas Gohr            $array[$sk] = $array[$key];
38181c54349SAndreas Gohr            unset($array[$key]);
38281c54349SAndreas Gohr            $key = $sk;
38381c54349SAndreas Gohr        }
38481c54349SAndreas Gohr
38581c54349SAndreas Gohr        // do recursion if needed
386ed7b5f09Sandi        if (is_array($array[$key])) {
387ed7b5f09Sandi            remove_magic_quotes($array[$key]);
388ed7b5f09Sandi        }else {
389ed7b5f09Sandi            $array[$key] = stripslashes($array[$key]);
390ed7b5f09Sandi        }
391ed7b5f09Sandi    }
392ed7b5f09Sandi}
393ed7b5f09Sandi
394ed7b5f09Sandi/**
395ed7b5f09Sandi * Returns the full absolute URL to the directory where
396ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash)
397ed7b5f09Sandi *
398ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
399ed7b5f09Sandi */
4004b1a4e04SAndreas Gohrfunction getBaseURL($abs=null){
401ed7b5f09Sandi    global $conf;
402ed7b5f09Sandi    //if canonical url enabled always return absolute
4034b1a4e04SAndreas Gohr    if(is_null($abs)) $abs = $conf['canonical'];
404ed7b5f09Sandi
40592b83b77Sandi    if($conf['basedir']){
40646c73e01SChris Smith        $dir = $conf['basedir'];
40789aa05dbSAndreas Gohr    }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){
40846c73e01SChris Smith        $dir = dirname($_SERVER['SCRIPT_NAME']);
40989aa05dbSAndreas Gohr    }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){
41046c73e01SChris Smith        $dir = dirname($_SERVER['PHP_SELF']);
411093ec9e4Sandi    }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
412093ec9e4Sandi        $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
413093ec9e4Sandi                $_SERVER['SCRIPT_FILENAME']);
41446c73e01SChris Smith        $dir = dirname('/'.$dir);
41592b83b77Sandi    }else{
41646c73e01SChris Smith        $dir = '.'; //probably wrong
41792b83b77Sandi    }
418ed7b5f09Sandi
41946c73e01SChris Smith    $dir = str_replace('\\','/',$dir);             // bugfix for weird WIN behaviour
42046c73e01SChris Smith    $dir = preg_replace('#//+#','/',"/$dir/");     // ensure leading and trailing slashes
421ed7b5f09Sandi
422f62ea8a1Sandi    //handle script in lib/exe dir
423f62ea8a1Sandi    $dir = preg_replace('!lib/exe/$!','',$dir);
424f62ea8a1Sandi
425488d5fa0SMichael Klier chi@chimeric.de    //handle script in lib/plugins dir
426488d5fa0SMichael Klier chi@chimeric.de    $dir = preg_replace('!lib/plugins/.*$!','',$dir);
427488d5fa0SMichael Klier chi@chimeric.de
428ed7b5f09Sandi    //finish here for relative URLs
429ed7b5f09Sandi    if(!$abs) return $dir;
430ed7b5f09Sandi
43146c73e01SChris Smith    //use config option if available, trim any slash from end of baseurl to avoid multiple consecutive slashes in the path
43246c73e01SChris Smith    if($conf['baseurl']) return rtrim($conf['baseurl'],'/').$dir;
433ef7b3ecdSAndreas Gohr
434e82e3526SAndreas Gohr    //split hostheader into host and port
435c66972f2SAdrian Lang    $addr = explode(':',$_SERVER['HTTP_HOST']);
436c66972f2SAdrian Lang    $host = $addr[0];
437c66972f2SAdrian Lang    $port = '';
438c66972f2SAdrian Lang    if (isset($addr[1])) {
439c66972f2SAdrian Lang        $port = $addr[1];
440c66972f2SAdrian Lang    } elseif (isset($_SERVER['SERVER_PORT'])) {
441c66972f2SAdrian Lang        $port = $_SERVER['SERVER_PORT'];
442c66972f2SAdrian Lang    }
443f5c6743cSAndreas Gohr    if(!is_ssl()){
444ed7b5f09Sandi        $proto = 'http://';
445e82e3526SAndreas Gohr        if ($port == '80') {
446ed7b5f09Sandi            $port = '';
447ed7b5f09Sandi        }
448ed7b5f09Sandi    }else{
449ed7b5f09Sandi        $proto = 'https://';
450e82e3526SAndreas Gohr        if ($port == '443') {
451ed7b5f09Sandi            $port = '';
452ed7b5f09Sandi        }
453ed7b5f09Sandi    }
454ed7b5f09Sandi
455c66972f2SAdrian Lang    if($port !== '') $port = ':'.$port;
456e82e3526SAndreas Gohr
457ed7b5f09Sandi    return $proto.$host.$port.$dir;
458ed7b5f09Sandi}
459ed7b5f09Sandi
460b000c6d4Sandi/**
461f5c6743cSAndreas Gohr * Check if accessed via HTTPS
462f5c6743cSAndreas Gohr *
463f5c6743cSAndreas Gohr * Apache leaves ,$_SERVER['HTTPS'] empty when not available, IIS sets it to 'off'.
464f5c6743cSAndreas Gohr * 'false' and 'disabled' are just guessing
465f5c6743cSAndreas Gohr *
466f5c6743cSAndreas Gohr * @returns bool true when SSL is active
467f5c6743cSAndreas Gohr */
468f5c6743cSAndreas Gohrfunction is_ssl(){
469c66972f2SAdrian Lang    if (!isset($_SERVER['HTTPS']) ||
470c66972f2SAdrian Lang        preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){
471f5c6743cSAndreas Gohr        return false;
472f5c6743cSAndreas Gohr    }else{
473f5c6743cSAndreas Gohr        return true;
474f5c6743cSAndreas Gohr    }
475f5c6743cSAndreas Gohr}
476f5c6743cSAndreas Gohr
477f5c6743cSAndreas Gohr/**
478b000c6d4Sandi * Append a PHP extension to a given file and adds an exit call
479b000c6d4Sandi *
480b000c6d4Sandi * This is used to migrate some old configfiles. An added PHP extension
481b000c6d4Sandi * ensures the contents are not shown to webusers even if .htaccess files
482b000c6d4Sandi * do not work
483b000c6d4Sandi *
484b000c6d4Sandi * @author Jan Decaluwe <jan@jandecaluwe.com>
485b000c6d4Sandi */
4868c4f28e8Sjanfunction scriptify($file) {
4878c4f28e8Sjan    // checks
4888c4f28e8Sjan    if (!is_readable($file)) {
4898c4f28e8Sjan        return;
4908c4f28e8Sjan    }
4918c4f28e8Sjan    $fn = $file.'.php';
4928c4f28e8Sjan    if (@file_exists($fn)) {
4938c4f28e8Sjan        return;
4948c4f28e8Sjan    }
4958c4f28e8Sjan    $fh = fopen($fn, 'w');
4968c4f28e8Sjan    if (!$fh) {
4973816dcbcSAndreas Gohr        nice_die($fn.' is not writable. Check your permission settings!');
4988c4f28e8Sjan    }
4998c4f28e8Sjan    // write php exit hack first
5008c4f28e8Sjan    fwrite($fh, "# $fn\n");
5018c4f28e8Sjan    fwrite($fh, '# <?php exit()?>'."\n");
5028c4f28e8Sjan    fwrite($fh, "# Don't modify the lines above\n");
5038c4f28e8Sjan    fwrite($fh, "#\n");
5048c4f28e8Sjan    // copy existing lines
5058c4f28e8Sjan    $lines = file($file);
5068c4f28e8Sjan    foreach ($lines as $line){
5078c4f28e8Sjan        fwrite($fh, $line);
5088c4f28e8Sjan    }
5093ba793e2Sandi    fclose($fh);
510b000c6d4Sandi    //try to rename the old file
5113aee4c27SAndreas Gohr    io_rename($file,"$file.old");
5128c4f28e8Sjan}
5138c4f28e8Sjan
5143816dcbcSAndreas Gohr/**
5153816dcbcSAndreas Gohr * print a nice message even if no styles are loaded yet.
5163816dcbcSAndreas Gohr */
5173816dcbcSAndreas Gohrfunction nice_die($msg){
5183816dcbcSAndreas Gohr    echo<<<EOT
5193816dcbcSAndreas Gohr<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
5203816dcbcSAndreas Gohr    "http://www.w3.org/TR/html4/loose.dtd">
5213816dcbcSAndreas Gohr<html>
5223816dcbcSAndreas Gohr<head><title>DokuWiki Setup Error</title></head>
5233816dcbcSAndreas Gohr<body style="font-family: Arial, sans-serif">
5243816dcbcSAndreas Gohr    <div style="width:60%; margin: auto; background-color: #fcc;
5253816dcbcSAndreas Gohr                border: 1px solid #faa; padding: 0.5em 1em;">
5263816dcbcSAndreas Gohr        <h1 style="font-size: 120%">DokuWiki Setup Error</h1>
5273816dcbcSAndreas Gohr        <p>$msg</p>
5283816dcbcSAndreas Gohr    </div>
5293816dcbcSAndreas Gohr</body>
5303816dcbcSAndreas Gohr</html>
5313816dcbcSAndreas GohrEOT;
5323816dcbcSAndreas Gohr    exit;
5333816dcbcSAndreas Gohr}
5343816dcbcSAndreas Gohr
53500976812SAndreas Gohr/**
53600976812SAndreas Gohr * A realpath() replacement
53700976812SAndreas Gohr *
53800976812SAndreas Gohr * This function behaves similar to PHP's realpath() but does not resolve
53900976812SAndreas Gohr * symlinks or accesses upper directories
54000976812SAndreas Gohr *
5414761d30cSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
54200976812SAndreas Gohr * @author <richpageau at yahoo dot co dot uk>
54300976812SAndreas Gohr * @link   http://de3.php.net/manual/en/function.realpath.php#75992
54400976812SAndreas Gohr */
545b328697dSAndreas Gohrfunction fullpath($path,$exists=false){
5464761d30cSAndreas Gohr    static $run = 0;
5474761d30cSAndreas Gohr    $root  = '';
548f0a201c5SChris Smith    $iswin = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' || @$GLOBALS['DOKU_UNITTEST_ASSUME_WINDOWS']);
54900976812SAndreas Gohr
5504761d30cSAndreas Gohr    // find the (indestructable) root of the path - keeps windows stuff intact
5514761d30cSAndreas Gohr    if($path{0} == '/'){
5524761d30cSAndreas Gohr        $root = '/';
5534761d30cSAndreas Gohr    }elseif($iswin){
5544761d30cSAndreas Gohr        // match drive letter and UNC paths
5554761d30cSAndreas Gohr        if(preg_match('!^([a-zA-z]:)(.*)!',$path,$match)){
556b9c4302bSAndreas Gohr            $root = $match[1].'/';
5574761d30cSAndreas Gohr            $path = $match[2];
5584761d30cSAndreas Gohr        }else if(preg_match('!^(\\\\\\\\[^\\\\/]+\\\\[^\\\\/]+[\\\\/])(.*)!',$path,$match)){
5594761d30cSAndreas Gohr            $root = $match[1];
5604761d30cSAndreas Gohr            $path = $match[2];
56100976812SAndreas Gohr        }
5624761d30cSAndreas Gohr    }
5634761d30cSAndreas Gohr    $path = str_replace('\\','/',$path);
5644761d30cSAndreas Gohr
5654761d30cSAndreas Gohr    // if the given path wasn't absolute already, prepend the script path and retry
5664761d30cSAndreas Gohr    if(!$root){
5674761d30cSAndreas Gohr        $base = dirname($_SERVER['SCRIPT_FILENAME']);
5684761d30cSAndreas Gohr        $path = $base.'/'.$path;
5694761d30cSAndreas Gohr        if($run == 0){ // avoid endless recursion when base isn't absolute for some reason
5704761d30cSAndreas Gohr            $run++;
571b328697dSAndreas Gohr            return fullpath($path,$exists);
5724761d30cSAndreas Gohr        }
5734761d30cSAndreas Gohr    }
5744761d30cSAndreas Gohr    $run = 0;
57500976812SAndreas Gohr
57600976812SAndreas Gohr    // canonicalize
57700976812SAndreas Gohr    $path=explode('/', $path);
57800976812SAndreas Gohr    $newpath=array();
579ef38bfe8SAndreas Gohr    foreach($path as $p) {
580ef38bfe8SAndreas Gohr        if ($p === '' || $p === '.') continue;
581ef38bfe8SAndreas Gohr        if ($p==='..') {
58200976812SAndreas Gohr            array_pop($newpath);
58300976812SAndreas Gohr            continue;
58400976812SAndreas Gohr        }
585ef38bfe8SAndreas Gohr        array_push($newpath, $p);
58600976812SAndreas Gohr    }
5874761d30cSAndreas Gohr    $finalpath = $root.implode('/', $newpath);
58800976812SAndreas Gohr
589b328697dSAndreas Gohr    // check for existance when needed (except when unit testing)
590b328697dSAndreas Gohr    if($exists && !defined('DOKU_UNITTEST') && !@file_exists($finalpath)) {
5914761d30cSAndreas Gohr        return false;
59200976812SAndreas Gohr    }
5934761d30cSAndreas Gohr    return $finalpath;
59400976812SAndreas Gohr}
59500976812SAndreas Gohr
596