xref: /dokuwiki/inc/init.php (revision cca94fbcfc035dabe5597e8565671c84862268e9)
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;
14*cca94fbcSRoland Hager$config_cascade = array();
15ccaeaa85SAndreas Gohr
1648beefecSAndreas Gohr// if available load a preload config file
17e6266454SChris Smith$preload = fullpath(dirname(__FILE__)).'/preload.php';
18e6266454SChris Smithif (@file_exists($preload)) include($preload);
1948beefecSAndreas Gohr
20ed7b5f09Sandi// define the include path
2100976812SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../').'/');
22ad15db82Sandi
23c2a6d816SAndreas Gohr// define Plugin dir
24c2a6d816SAndreas Gohrif(!defined('DOKU_PLUGIN'))  define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
25c2a6d816SAndreas Gohr
26e7cb32dcSAndreas Gohr// define config path (packagers may want to change this to /etc/dokuwiki/)
27b7551a6dSEsther Brunnerif(!defined('DOKU_CONF')) define('DOKU_CONF',DOKU_INC.'conf/');
28e7cb32dcSAndreas Gohr
29bad905f1SBen Coburn// check for error reporting override or set error reporting to sane values
30d8186216SBen Coburnif (!defined('DOKU_E_LEVEL') && @file_exists(DOKU_CONF.'report_e_all')) {
31bad905f1SBen Coburn    define('DOKU_E_LEVEL', E_ALL);
32bad905f1SBen Coburn}
33fc80ed59SAndreas Gohrif (!defined('DOKU_E_LEVEL')) {
34fc80ed59SAndreas Gohr    if(defined('E_DEPRECATED')){ // since php 5.3
35fc80ed59SAndreas Gohr        error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
36fc80ed59SAndreas Gohr    }else{
37fc80ed59SAndreas Gohr        error_reporting(E_ALL ^ E_NOTICE);
38fc80ed59SAndreas Gohr    }
39fc80ed59SAndreas Gohr} else {
40fc80ed59SAndreas Gohr    error_reporting(DOKU_E_LEVEL);
41fc80ed59SAndreas Gohr}
42c53ea5f2Sandi
4350602150SBen Coburn// init memory caches
44db959ae3SAndreas Gohrglobal $cache_revinfo;
45db959ae3SAndreas Gohr       $cache_revinfo = array();
46db959ae3SAndreas Gohrglobal $cache_wikifn;
47db959ae3SAndreas Gohr       $cache_wikifn = array();
48db959ae3SAndreas Gohrglobal $cache_cleanid;
49db959ae3SAndreas Gohr       $cache_cleanid = array();
50db959ae3SAndreas Gohrglobal $cache_authname;
51db959ae3SAndreas Gohr       $cache_authname = array();
52db959ae3SAndreas Gohrglobal $cache_metadata;
53db959ae3SAndreas Gohr       $cache_metadata = array();
5450602150SBen Coburn
55*cca94fbcSRoland Hager// always include 'inc/config_cascade.php'
56*cca94fbcSRoland Hager// previously in preload.php set fields of $config_cascade will be merged with the defaults
57e6a6dbfeSAndreas Gohrinclude(DOKU_INC.'inc/config_cascade.php');
58cb043f52SChris Smith
594724a577Sandi//prepare config array()
60ee20e7d1Sandiglobal $conf;
614724a577Sandi$conf = array();
624724a577Sandi
63cb043f52SChris Smith// load the global config file(s)
64b303b92cSChris Smithforeach (array('default','local','protected') as $config_group) {
65f8121585SChris Smith    if (empty($config_cascade['main'][$config_group])) continue;
66b303b92cSChris Smith    foreach ($config_cascade['main'][$config_group] as $config_file) {
67f8121585SChris Smith        if (@file_exists($config_file)) {
68f8121585SChris Smith            include($config_file);
69f8121585SChris Smith        }
70cb043f52SChris Smith    }
710a6ead41SAndreas Gohr}
72ad15db82Sandi
73ad15db82Sandi//prepare language array
74ee20e7d1Sandiglobal $lang;
75ad15db82Sandi$lang = array();
76ed7b5f09Sandi
7716521111Sandi//load the language files
78bc3b6aecSandirequire_once(DOKU_INC.'inc/lang/en/lang.php');
79f949a01cSAndreas Gohrif ( $conf['lang'] && $conf['lang'] != 'en' ) {
80bc3b6aecSandi    require_once(DOKU_INC.'inc/lang/'.$conf['lang'].'/lang.php');
81fc1c55b1Shfuecks}
8216521111Sandi
83066fee30SAndreas Gohr//prepare license array()
84066fee30SAndreas Gohrglobal $license;
85066fee30SAndreas Gohr$license = array();
86066fee30SAndreas Gohr
87066fee30SAndreas Gohr// load the license file(s)
88f8121585SChris Smithforeach (array('default','local') as $config_group) {
89f8121585SChris Smith    if (empty($config_cascade['license'][$config_group])) continue;
90f8121585SChris Smith    foreach ($config_cascade['license'][$config_group] as $config_file) {
91f8121585SChris Smith        if(@file_exists($config_file)){
92f8121585SChris Smith            include($config_file);
93f8121585SChris Smith        }
94f8121585SChris Smith    }
95066fee30SAndreas Gohr}
96066fee30SAndreas Gohr
971f8eb24fSAndreas Gohr// set timezone (as in pre 5.3.0 days)
981f8eb24fSAndreas Gohrdate_default_timezone_set(@date_default_timezone_get());
991f8eb24fSAndreas Gohr
100ed7b5f09Sandi// define baseURL
1014b1a4e04SAndreas Gohrif(!defined('DOKU_REL')) define('DOKU_REL',getBaseURL(false));
102ed7b5f09Sandiif(!defined('DOKU_URL')) define('DOKU_URL',getBaseURL(true));
1034b1a4e04SAndreas Gohrif(!defined('DOKU_BASE')){
1044b1a4e04SAndreas Gohr    if($conf['canonical']){
1054b1a4e04SAndreas Gohr        define('DOKU_BASE',DOKU_URL);
1064b1a4e04SAndreas Gohr    }else{
1074b1a4e04SAndreas Gohr        define('DOKU_BASE',DOKU_REL);
1084b1a4e04SAndreas Gohr    }
1094b1a4e04SAndreas Gohr}
1104b1a4e04SAndreas Gohr
111b8595a66SAndreas Gohr// define whitespace
112b8595a66SAndreas Gohrif(!defined('DOKU_LF')) define ('DOKU_LF',"\n");
113b8595a66SAndreas Gohrif(!defined('DOKU_TAB')) define ('DOKU_TAB',"\t");
114ed7b5f09Sandi
115656c8fb3SAndreas Gohr// define cookie and session id, append server port when securecookie is configured FS#1664
116656c8fb3SAndreas Gohrif (!defined('DOKU_COOKIE')) define('DOKU_COOKIE', 'DW'.md5(DOKU_REL.(($conf['securecookie'])?$_SERVER['SERVER_PORT']:'')));
117e71ce681SAndreas Gohr
118ee20e7d1Sandi
119ed7b5f09Sandi// define main script
120ed7b5f09Sandiif(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php');
121ed7b5f09Sandi
1226b13307fSandi// define Template baseURL
1236b13307fSandiif(!defined('DOKU_TPL')) define('DOKU_TPL',
124f62ea8a1Sandi        DOKU_BASE.'lib/tpl/'.$conf['template'].'/');
1256b13307fSandi
12678a6aeb1SAndreas Gohr// define real Template directory
12778a6aeb1SAndreas Gohrif(!defined('DOKU_TPLINC')) define('DOKU_TPLINC',
12878a6aeb1SAndreas Gohr        DOKU_INC.'lib/tpl/'.$conf['template'].'/');
12978a6aeb1SAndreas Gohr
130ed7b5f09Sandi// make session rewrites XHTML compliant
1313fc74836Sandi@ini_set('arg_separator.output', '&amp;');
132ed7b5f09Sandi
133d7e6bba9SAndreas Gohr// make sure global zlib does not interfere FS#1132
134d7e6bba9SAndreas Gohr@ini_set('zlib.output_compression', 'off');
135d7e6bba9SAndreas Gohr
1366deb5405SAndreas Gohr// increase PCRE backtrack limit
1376deb5405SAndreas Gohr@ini_set('pcre.backtrack_limit', '20971520');
1386deb5405SAndreas Gohr
13998bda4fdSAndreas Gohr// enable gzip compression if supported
14098bda4fdSAndreas Gohr$conf['gzip_output'] &= (strpos($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip') !== false);
1413138b5c7SAndreas Gohrif ($conf['gzip_output'] &&
1423138b5c7SAndreas Gohr        !defined('DOKU_DISABLE_GZIP_OUTPUT') &&
14398bda4fdSAndreas Gohr        function_exists('ob_gzhandler')) {
1443138b5c7SAndreas Gohr    ob_start('ob_gzhandler');
1453138b5c7SAndreas Gohr}
1463138b5c7SAndreas Gohr
147ed7b5f09Sandi// init session
1486534245aSAndreas Gohrif (!headers_sent() && !defined('NOSESSION')){
149ed7b5f09Sandi    session_name("DokuWiki");
150f5c6743cSAndreas Gohr    if (version_compare(PHP_VERSION, '5.2.0', '>')) {
151f5c6743cSAndreas Gohr        session_set_cookie_params(0,DOKU_REL,'',($conf['securecookie'] && is_ssl()),true);
152f5c6743cSAndreas Gohr    }else{
153f5c6743cSAndreas Gohr        session_set_cookie_params(0,DOKU_REL,'',($conf['securecookie'] && is_ssl()));
154f5c6743cSAndreas Gohr    }
155bad31ae9SAndreas Gohr    session_start();
15614a122deSAndreas Gohr
15714a122deSAndreas Gohr    // load left over messages
15814a122deSAndreas Gohr    if(isset($_SESSION[DOKU_COOKIE]['msg'])){
15914a122deSAndreas Gohr        $MSG = $_SESSION[DOKU_COOKIE]['msg'];
16014a122deSAndreas Gohr        unset($_SESSION[DOKU_COOKIE]['msg']);
16114a122deSAndreas Gohr    }
162bad31ae9SAndreas Gohr}
163ed7b5f09Sandi
164ed7b5f09Sandi// kill magic quotes
165e55eb89cSAndreas Gohrif (get_magic_quotes_gpc() && !defined('MAGIC_QUOTES_STRIPPED')) {
166ed7b5f09Sandi    if (!empty($_GET))    remove_magic_quotes($_GET);
167ed7b5f09Sandi    if (!empty($_POST))   remove_magic_quotes($_POST);
168ed7b5f09Sandi    if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE);
169ed7b5f09Sandi    if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST);
1703fc74836Sandi    @ini_set('magic_quotes_gpc', 0);
171e55eb89cSAndreas Gohr    define('MAGIC_QUOTES_STRIPPED',1);
172ed7b5f09Sandi}
1733fc74836Sandi@set_magic_quotes_runtime(0);
1743fc74836Sandi@ini_set('magic_quotes_sybase',0);
175ed7b5f09Sandi
176a1637ffdSAndreas Gohr// don't let cookies ever interfere with request vars
177a1637ffdSAndreas Gohr$_REQUEST = array_merge($_GET,$_POST);
178a1637ffdSAndreas Gohr
1793dea4ebcSAndreas Gohr// we don't want a purge URL to be digged
180c66972f2SAdrian Langif(isset($_REQUEST['purge']) && $_SERVER['HTTP_REFERER']) unset($_REQUEST['purge']);
1813dea4ebcSAndreas Gohr
182ed7b5f09Sandi// disable gzip if not available
1838a447e5cSAndreas Gohrif($conf['compression'] == 'bz2' && !function_exists('bzopen')){
184fe893490SAndreas Gohr    $conf['compression'] = 'gz';
185501252a5SAndreas Gohr}
186fe893490SAndreas Gohrif($conf['compression'] == 'gz' && !function_exists('gzopen')){
187501252a5SAndreas Gohr    $conf['compression'] = 0;
188ed7b5f09Sandi}
189ed7b5f09Sandi
190e656dcd4SAndreas Gohr// fix dateformat for upgraders
191e656dcd4SAndreas Gohrif(strpos($conf['dformat'],'%') === false){
192e656dcd4SAndreas Gohr    $conf['dformat'] = '%Y/%m/%d %H:%M';
193e656dcd4SAndreas Gohr}
194e656dcd4SAndreas Gohr
1951ca31cfeSAndreas Gohr// precalculate file creation modes
1961ca31cfeSAndreas Gohrinit_creationmodes();
197ed7b5f09Sandi
1983dc3a5f1Sandi// make real paths and check them
19998407a7aSandiinit_paths();
2007367b368SAndreas Gohrinit_files();
201ed7b5f09Sandi
202f1986589SMichael Klier// setup plugin controller class (can be overwritten in preload.php)
203f1986589SMichael Klier$plugin_types = array('admin','syntax','action','renderer', 'helper');
204f1986589SMichael Klierglobal $plugin_controller_class, $plugin_controller;
205f1986589SMichael Klierif (empty($plugin_controller_class)) $plugin_controller_class = 'Doku_Plugin_Controller';
206f1986589SMichael Klier
207c7cb395cSAdrian Lang// load libraries
208c7cb395cSAdrian Langrequire_once(DOKU_INC.'inc/load.php');
209c7cb395cSAdrian Lang
210f1986589SMichael Klier// initialize plugin controller
211f1986589SMichael Klier$plugin_controller = new $plugin_controller_class();
212f1986589SMichael Klier
213f1986589SMichael Klier// initialize the event handler
214f1986589SMichael Klierglobal $EVENT_HANDLER;
215f1986589SMichael Klier$EVENT_HANDLER = new Doku_Event_Handler();
216f1986589SMichael Klier
21716905344SAndreas Gohr// setup authentication system
218c7cb395cSAdrian Langif (!defined('NOSESSION')) {
21916905344SAndreas Gohr    auth_setup();
220c7cb395cSAdrian Lang}
221f62ea8a1Sandi
2225ec3fefcSAndreas Gohr// setup mail system
2235ec3fefcSAndreas Gohrmail_setup();
2245ec3fefcSAndreas Gohr
225f62ea8a1Sandi/**
22698407a7aSandi * Checks paths from config file
22798407a7aSandi */
22898407a7aSandifunction init_paths(){
22998407a7aSandi    global $conf;
23098407a7aSandi
23198407a7aSandi    $paths = array('datadir'   => 'pages',
23298407a7aSandi            'olddir'    => 'attic',
23398407a7aSandi            'mediadir'  => 'media',
23498407a7aSandi            'metadir'   => 'meta',
23598407a7aSandi            'cachedir'  => 'cache',
236579b0f7eSTNHarris            'indexdir'  => 'index',
237de33a58fSMichael Klier            'lockdir'   => 'locks',
238de33a58fSMichael Klier            'tmpdir'    => 'tmp');
23998407a7aSandi
24098407a7aSandi    foreach($paths as $c => $p){
241bb4866bdSchris        if(empty($conf[$c]))  $conf[$c] = $conf['savedir'].'/'.$p;
24298407a7aSandi        $conf[$c]             = init_path($conf[$c]);
2431983acc2SGuy Brand        if(empty($conf[$c]))  nice_die("The $c ('$p') does not exist, isn't accessible or writable.
24469dc3177SAndreas Gohr                You should check your config and permission settings.
24569dc3177SAndreas Gohr                Or maybe you want to <a href=\"install.php\">run the
24669dc3177SAndreas Gohr                installer</a>?");
24798407a7aSandi    }
24871726d78SBen Coburn
24971726d78SBen Coburn    // path to old changelog only needed for upgrading
25071726d78SBen Coburn    $conf['changelog_old'] = init_path((isset($conf['changelog']))?($conf['changelog']):($conf['savedir'].'/changes.log'));
25171726d78SBen Coburn    if ($conf['changelog_old']=='') { unset($conf['changelog_old']); }
25271726d78SBen Coburn    // hardcoded changelog because it is now a cache that lives in meta
25371726d78SBen Coburn    $conf['changelog'] = $conf['metadir'].'/_dokuwiki.changes';
25499c8d7f2Smichael    $conf['media_changelog'] = $conf['metadir'].'/_media.changes';
25598407a7aSandi}
25698407a7aSandi
25798407a7aSandi/**
2580d8850c4SAndreas Gohr * Checks the existance of certain files and creates them if missing.
2597367b368SAndreas Gohr */
2607367b368SAndreas Gohrfunction init_files(){
2617367b368SAndreas Gohr    global $conf;
2620d8850c4SAndreas Gohr
263345b1674SAndreas Gohr    $files = array($conf['indexdir'].'/page.idx');
2647367b368SAndreas Gohr
2657367b368SAndreas Gohr    foreach($files as $file){
2667367b368SAndreas Gohr        if(!@file_exists($file)){
2670d8850c4SAndreas Gohr            $fh = @fopen($file,'a');
2680d8850c4SAndreas Gohr            if($fh){
2697367b368SAndreas Gohr                fclose($fh);
2701ca31cfeSAndreas Gohr                if($conf['fperm']) chmod($file, $conf['fperm']);
2710d8850c4SAndreas Gohr            }else{
2723816dcbcSAndreas Gohr                nice_die("$file is not writable. Check your permissions settings!");
2730d8850c4SAndreas Gohr            }
2747367b368SAndreas Gohr        }
2757367b368SAndreas Gohr    }
276345b1674SAndreas Gohr
277345b1674SAndreas Gohr    # create title index (needs to have same length as page.idx)
278345b1674SAndreas Gohr    $file = $conf['indexdir'].'/title.idx';
279345b1674SAndreas Gohr    if(!@file_exists($file)){
280345b1674SAndreas Gohr        $pages = file($conf['indexdir'].'/page.idx');
281345b1674SAndreas Gohr        $pages = count($pages);
282345b1674SAndreas Gohr        $fh = @fopen($file,'a');
283345b1674SAndreas Gohr        if($fh){
284345b1674SAndreas Gohr            for($i=0; $i<$pages; $i++){
285345b1674SAndreas Gohr                fwrite($fh,"\n");
286345b1674SAndreas Gohr            }
287345b1674SAndreas Gohr            fclose($fh);
288345b1674SAndreas Gohr        }else{
289345b1674SAndreas Gohr            nice_die("$file is not writable. Check your permissions settings!");
290345b1674SAndreas Gohr        }
291345b1674SAndreas Gohr    }
2927367b368SAndreas Gohr}
2937367b368SAndreas Gohr
2947367b368SAndreas Gohr/**
2950d8850c4SAndreas Gohr * Returns absolute path
296f62ea8a1Sandi *
2970d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC.
2980d8850c4SAndreas Gohr * Check for accessability on directories as well.
2990d8850c4SAndreas Gohr *
3000d8850c4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
301f62ea8a1Sandi */
302f62ea8a1Sandifunction init_path($path){
3030d8850c4SAndreas Gohr    // check existance
30400976812SAndreas Gohr    $p = fullpath($path);
3050d8850c4SAndreas Gohr    if(!@file_exists($p)){
30600976812SAndreas Gohr        $p = fullpath(DOKU_INC.$path);
3070d8850c4SAndreas Gohr        if(!@file_exists($p)){
3088fc4e739Sandi            return '';
309f62ea8a1Sandi        }
3100d8850c4SAndreas Gohr    }
3110d8850c4SAndreas Gohr
3120d8850c4SAndreas Gohr    // check writability
3130d8850c4SAndreas Gohr    if(!@is_writable($p)){
3140d8850c4SAndreas Gohr        return '';
3150d8850c4SAndreas Gohr    }
3160d8850c4SAndreas Gohr
3170d8850c4SAndreas Gohr    // check accessability (execute bit) for directories
3180d8850c4SAndreas Gohr    if(@is_dir($p) && !@file_exists("$p/.")){
3190d8850c4SAndreas Gohr        return '';
3200d8850c4SAndreas Gohr    }
3210d8850c4SAndreas Gohr
3220d8850c4SAndreas Gohr    return $p;
3230d8850c4SAndreas Gohr}
3248c4f28e8Sjan
325ed7b5f09Sandi/**
3261ca31cfeSAndreas Gohr * Sets the internal config values fperm and dperm which, when set,
3271ca31cfeSAndreas Gohr * will be used to change the permission of a newly created dir or
3281ca31cfeSAndreas Gohr * file with chmod. Considers the influence of the system's umask
3291ca31cfeSAndreas Gohr * setting the values only if needed.
3301ca31cfeSAndreas Gohr */
3311ca31cfeSAndreas Gohrfunction init_creationmodes(){
3321ca31cfeSAndreas Gohr    global $conf;
3331ca31cfeSAndreas Gohr
3341ca31cfeSAndreas Gohr    // Legacy support for old umask/dmask scheme
3351ca31cfeSAndreas Gohr    unset($conf['dmask']);
3361ca31cfeSAndreas Gohr    unset($conf['fmask']);
3371ca31cfeSAndreas Gohr    unset($conf['umask']);
3381ca31cfeSAndreas Gohr    unset($conf['fperm']);
3391ca31cfeSAndreas Gohr    unset($conf['dperm']);
3401ca31cfeSAndreas Gohr
3419f3cdec3SAndreas Gohr    // get system umask, fallback to 0 if none available
3429f3cdec3SAndreas Gohr    $umask = @umask();
3439f3cdec3SAndreas Gohr    if(!$umask) $umask = 0000;
3441ca31cfeSAndreas Gohr
3451ca31cfeSAndreas Gohr    // check what is set automatically by the system on file creation
3461ca31cfeSAndreas Gohr    // and set the fperm param if it's not what we want
3471ca31cfeSAndreas Gohr    $auto_fmode = 0666 & ~$umask;
3481ca31cfeSAndreas Gohr    if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode'];
3491ca31cfeSAndreas Gohr
3501ca31cfeSAndreas Gohr    // check what is set automatically by the system on file creation
3511ca31cfeSAndreas Gohr    // and set the dperm param if it's not what we want
3521ca31cfeSAndreas Gohr    $auto_dmode = $conf['dmode'] & ~$umask;
3531ca31cfeSAndreas Gohr    if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode'];
3541ca31cfeSAndreas Gohr}
3551ca31cfeSAndreas Gohr
3561ca31cfeSAndreas Gohr/**
357ed7b5f09Sandi * remove magic quotes recursivly
358ed7b5f09Sandi *
359ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
360ed7b5f09Sandi */
361ed7b5f09Sandifunction remove_magic_quotes(&$array) {
362ed7b5f09Sandi    foreach (array_keys($array) as $key) {
36381c54349SAndreas Gohr        // handle magic quotes in keynames (breaks order)
36481c54349SAndreas Gohr        $sk = stripslashes($key);
36581c54349SAndreas Gohr        if($sk != $key){
36681c54349SAndreas Gohr            $array[$sk] = $array[$key];
36781c54349SAndreas Gohr            unset($array[$key]);
36881c54349SAndreas Gohr            $key = $sk;
36981c54349SAndreas Gohr        }
37081c54349SAndreas Gohr
37181c54349SAndreas Gohr        // do recursion if needed
372ed7b5f09Sandi        if (is_array($array[$key])) {
373ed7b5f09Sandi            remove_magic_quotes($array[$key]);
374ed7b5f09Sandi        }else {
375ed7b5f09Sandi            $array[$key] = stripslashes($array[$key]);
376ed7b5f09Sandi        }
377ed7b5f09Sandi    }
378ed7b5f09Sandi}
379ed7b5f09Sandi
380ed7b5f09Sandi/**
381ed7b5f09Sandi * Returns the full absolute URL to the directory where
382ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash)
383ed7b5f09Sandi *
384ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
385ed7b5f09Sandi */
3864b1a4e04SAndreas Gohrfunction getBaseURL($abs=null){
387ed7b5f09Sandi    global $conf;
388ed7b5f09Sandi    //if canonical url enabled always return absolute
3894b1a4e04SAndreas Gohr    if(is_null($abs)) $abs = $conf['canonical'];
390ed7b5f09Sandi
39192b83b77Sandi    if($conf['basedir']){
39246c73e01SChris Smith        $dir = $conf['basedir'];
39389aa05dbSAndreas Gohr    }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){
39446c73e01SChris Smith        $dir = dirname($_SERVER['SCRIPT_NAME']);
39589aa05dbSAndreas Gohr    }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){
39646c73e01SChris Smith        $dir = dirname($_SERVER['PHP_SELF']);
397093ec9e4Sandi    }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
398093ec9e4Sandi        $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
399093ec9e4Sandi                $_SERVER['SCRIPT_FILENAME']);
40046c73e01SChris Smith        $dir = dirname('/'.$dir);
40192b83b77Sandi    }else{
40246c73e01SChris Smith        $dir = '.'; //probably wrong
40392b83b77Sandi    }
404ed7b5f09Sandi
40546c73e01SChris Smith    $dir = str_replace('\\','/',$dir);             // bugfix for weird WIN behaviour
40646c73e01SChris Smith    $dir = preg_replace('#//+#','/',"/$dir/");     // ensure leading and trailing slashes
407ed7b5f09Sandi
408f62ea8a1Sandi    //handle script in lib/exe dir
409f62ea8a1Sandi    $dir = preg_replace('!lib/exe/$!','',$dir);
410f62ea8a1Sandi
411488d5fa0SMichael Klier chi@chimeric.de    //handle script in lib/plugins dir
412488d5fa0SMichael Klier chi@chimeric.de    $dir = preg_replace('!lib/plugins/.*$!','',$dir);
413488d5fa0SMichael Klier chi@chimeric.de
414ed7b5f09Sandi    //finish here for relative URLs
415ed7b5f09Sandi    if(!$abs) return $dir;
416ed7b5f09Sandi
41746c73e01SChris Smith    //use config option if available, trim any slash from end of baseurl to avoid multiple consecutive slashes in the path
41846c73e01SChris Smith    if($conf['baseurl']) return rtrim($conf['baseurl'],'/').$dir;
419ef7b3ecdSAndreas Gohr
420e82e3526SAndreas Gohr    //split hostheader into host and port
4215627186cSAndreas Gohr    if(isset($_SERVER['HTTP_HOST'])){
422204b27c8SMichael Hamann        $parsed_host = parse_url('http://'.$_SERVER['HTTP_HOST']);
423204b27c8SMichael Hamann        $host = $parsed_host['host'];
424204b27c8SMichael Hamann        $port = $parsed_host['port'];
4255627186cSAndreas Gohr    }elseif(isset($_SERVER['SERVER_NAME'])){
426204b27c8SMichael Hamann        $parsed_host = parse_url('http://'.$_SERVER['SERVER_NAME']);
427204b27c8SMichael Hamann        $host = $parsed_host['host'];
428204b27c8SMichael Hamann        $port = $parsed_host['port'];
4295627186cSAndreas Gohr    }else{
4305627186cSAndreas Gohr        $host = php_uname('n');
431c66972f2SAdrian Lang        $port = '';
4325627186cSAndreas Gohr    }
4335627186cSAndreas Gohr
4345627186cSAndreas Gohr    if(!$port && isset($_SERVER['SERVER_PORT'])) {
435c66972f2SAdrian Lang        $port = $_SERVER['SERVER_PORT'];
436c66972f2SAdrian Lang    }
437204b27c8SMichael Hamann
438204b27c8SMichael Hamann    if(is_null($port)){
439204b27c8SMichael Hamann        $port = '';
440204b27c8SMichael Hamann    }
441204b27c8SMichael Hamann
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/**
4773816dcbcSAndreas Gohr * print a nice message even if no styles are loaded yet.
4783816dcbcSAndreas Gohr */
4793816dcbcSAndreas Gohrfunction nice_die($msg){
4803816dcbcSAndreas Gohr    echo<<<EOT
4813816dcbcSAndreas Gohr<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
4823816dcbcSAndreas Gohr    "http://www.w3.org/TR/html4/loose.dtd">
4833816dcbcSAndreas Gohr<html>
4843816dcbcSAndreas Gohr<head><title>DokuWiki Setup Error</title></head>
4853816dcbcSAndreas Gohr<body style="font-family: Arial, sans-serif">
4863816dcbcSAndreas Gohr    <div style="width:60%; margin: auto; background-color: #fcc;
4873816dcbcSAndreas Gohr                border: 1px solid #faa; padding: 0.5em 1em;">
4883816dcbcSAndreas Gohr        <h1 style="font-size: 120%">DokuWiki Setup Error</h1>
4893816dcbcSAndreas Gohr        <p>$msg</p>
4903816dcbcSAndreas Gohr    </div>
4913816dcbcSAndreas Gohr</body>
4923816dcbcSAndreas Gohr</html>
4933816dcbcSAndreas GohrEOT;
4943816dcbcSAndreas Gohr    exit;
4953816dcbcSAndreas Gohr}
4963816dcbcSAndreas Gohr
49700976812SAndreas Gohr/**
49800976812SAndreas Gohr * A realpath() replacement
49900976812SAndreas Gohr *
50000976812SAndreas Gohr * This function behaves similar to PHP's realpath() but does not resolve
50100976812SAndreas Gohr * symlinks or accesses upper directories
50200976812SAndreas Gohr *
5034761d30cSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
50400976812SAndreas Gohr * @author <richpageau at yahoo dot co dot uk>
50500976812SAndreas Gohr * @link   http://de3.php.net/manual/en/function.realpath.php#75992
50600976812SAndreas Gohr */
507b328697dSAndreas Gohrfunction fullpath($path,$exists=false){
5084761d30cSAndreas Gohr    static $run = 0;
5094761d30cSAndreas Gohr    $root  = '';
510f0a201c5SChris Smith    $iswin = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' || @$GLOBALS['DOKU_UNITTEST_ASSUME_WINDOWS']);
51100976812SAndreas Gohr
5124761d30cSAndreas Gohr    // find the (indestructable) root of the path - keeps windows stuff intact
5134761d30cSAndreas Gohr    if($path{0} == '/'){
5144761d30cSAndreas Gohr        $root = '/';
5154761d30cSAndreas Gohr    }elseif($iswin){
5164761d30cSAndreas Gohr        // match drive letter and UNC paths
5174761d30cSAndreas Gohr        if(preg_match('!^([a-zA-z]:)(.*)!',$path,$match)){
518b9c4302bSAndreas Gohr            $root = $match[1].'/';
5194761d30cSAndreas Gohr            $path = $match[2];
5204761d30cSAndreas Gohr        }else if(preg_match('!^(\\\\\\\\[^\\\\/]+\\\\[^\\\\/]+[\\\\/])(.*)!',$path,$match)){
5214761d30cSAndreas Gohr            $root = $match[1];
5224761d30cSAndreas Gohr            $path = $match[2];
52300976812SAndreas Gohr        }
5244761d30cSAndreas Gohr    }
5254761d30cSAndreas Gohr    $path = str_replace('\\','/',$path);
5264761d30cSAndreas Gohr
5274761d30cSAndreas Gohr    // if the given path wasn't absolute already, prepend the script path and retry
5284761d30cSAndreas Gohr    if(!$root){
5294761d30cSAndreas Gohr        $base = dirname($_SERVER['SCRIPT_FILENAME']);
5304761d30cSAndreas Gohr        $path = $base.'/'.$path;
5314761d30cSAndreas Gohr        if($run == 0){ // avoid endless recursion when base isn't absolute for some reason
5324761d30cSAndreas Gohr            $run++;
533b328697dSAndreas Gohr            return fullpath($path,$exists);
5344761d30cSAndreas Gohr        }
5354761d30cSAndreas Gohr    }
5364761d30cSAndreas Gohr    $run = 0;
53700976812SAndreas Gohr
53800976812SAndreas Gohr    // canonicalize
53900976812SAndreas Gohr    $path=explode('/', $path);
54000976812SAndreas Gohr    $newpath=array();
541ef38bfe8SAndreas Gohr    foreach($path as $p) {
542ef38bfe8SAndreas Gohr        if ($p === '' || $p === '.') continue;
543ef38bfe8SAndreas Gohr        if ($p==='..') {
54400976812SAndreas Gohr            array_pop($newpath);
54500976812SAndreas Gohr            continue;
54600976812SAndreas Gohr        }
547ef38bfe8SAndreas Gohr        array_push($newpath, $p);
54800976812SAndreas Gohr    }
5494761d30cSAndreas Gohr    $finalpath = $root.implode('/', $newpath);
55000976812SAndreas Gohr
551b328697dSAndreas Gohr    // check for existance when needed (except when unit testing)
552b328697dSAndreas Gohr    if($exists && !defined('DOKU_UNITTEST') && !@file_exists($finalpath)) {
5534761d30cSAndreas Gohr        return false;
55400976812SAndreas Gohr    }
5554761d30cSAndreas Gohr    return $finalpath;
55600976812SAndreas Gohr}
55700976812SAndreas Gohr
558