xref: /dokuwiki/inc/init.php (revision f1986589078b5e2e3aecc37d8f28b03caa3b8c17)
1ed7b5f09Sandi<?php
2ed7b5f09Sandi/**
3ed7b5f09Sandi * Initialize some defaults needed for DokuWiki
4ed7b5f09Sandi */
5ed7b5f09Sandi
6a609a9ccSBen Coburn// start timing Dokuwiki execution
7a609a9ccSBen Coburnfunction delta_time($start=0) {
8a609a9ccSBen Coburn    list($usec, $sec) = explode(" ", microtime());
9a609a9ccSBen Coburn    return ((float)$usec+(float)$sec)-((float)$start);
10a609a9ccSBen Coburn}
11a609a9ccSBen Coburndefine('DOKU_START_TIME', delta_time());
12a609a9ccSBen Coburn
13ccaeaa85SAndreas Gohrglobal $config_cascade;
14ccaeaa85SAndreas Gohr$config_cascade = '';
15ccaeaa85SAndreas Gohr
1648beefecSAndreas Gohr// if available load a preload config file
17e6266454SChris Smith$preload = fullpath(dirname(__FILE__)).'/preload.php';
18e6266454SChris Smithif (@file_exists($preload)) include($preload);
1948beefecSAndreas Gohr
20ed7b5f09Sandi// define the include path
2100976812SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../').'/');
22ad15db82Sandi
23c2a6d816SAndreas Gohr// define Plugin dir
24c2a6d816SAndreas Gohrif(!defined('DOKU_PLUGIN'))  define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
25c2a6d816SAndreas Gohr
26e7cb32dcSAndreas Gohr// define config path (packagers may want to change this to /etc/dokuwiki/)
27b7551a6dSEsther Brunnerif(!defined('DOKU_CONF')) define('DOKU_CONF',DOKU_INC.'conf/');
28e7cb32dcSAndreas Gohr
29bad905f1SBen Coburn// check for error reporting override or set error reporting to sane values
30d8186216SBen Coburnif (!defined('DOKU_E_LEVEL') && @file_exists(DOKU_CONF.'report_e_all')) {
31bad905f1SBen Coburn    define('DOKU_E_LEVEL', E_ALL);
32bad905f1SBen Coburn}
33fc80ed59SAndreas Gohrif (!defined('DOKU_E_LEVEL')) {
34fc80ed59SAndreas Gohr    if(defined('E_DEPRECATED')){ // since php 5.3
35fc80ed59SAndreas Gohr        error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
36fc80ed59SAndreas Gohr    }else{
37fc80ed59SAndreas Gohr        error_reporting(E_ALL ^ E_NOTICE);
38fc80ed59SAndreas Gohr    }
39fc80ed59SAndreas Gohr} else {
40fc80ed59SAndreas Gohr    error_reporting(DOKU_E_LEVEL);
41fc80ed59SAndreas Gohr}
42c53ea5f2Sandi
4350602150SBen Coburn// init memory caches
44db959ae3SAndreas Gohrglobal $cache_revinfo;
45db959ae3SAndreas Gohr       $cache_revinfo = array();
46db959ae3SAndreas Gohrglobal $cache_wikifn;
47db959ae3SAndreas Gohr       $cache_wikifn = array();
48db959ae3SAndreas Gohrglobal $cache_cleanid;
49db959ae3SAndreas Gohr       $cache_cleanid = array();
50db959ae3SAndreas Gohrglobal $cache_authname;
51db959ae3SAndreas Gohr       $cache_authname = array();
52db959ae3SAndreas Gohrglobal $cache_metadata;
53db959ae3SAndreas Gohr       $cache_metadata = array();
5450602150SBen Coburn
55cb043f52SChris Smith//set the configuration cascade - but only if its not already been set in preload.php
56cb043f52SChris Smithif (empty($config_cascade)) {
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
162ed7b5f09Sandi// define main script
163ed7b5f09Sandiif(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php');
164ed7b5f09Sandi
1656b13307fSandi// define Template baseURL
1666b13307fSandiif(!defined('DOKU_TPL')) define('DOKU_TPL',
167f62ea8a1Sandi        DOKU_BASE.'lib/tpl/'.$conf['template'].'/');
1686b13307fSandi
16978a6aeb1SAndreas Gohr// define real Template directory
17078a6aeb1SAndreas Gohrif(!defined('DOKU_TPLINC')) define('DOKU_TPLINC',
17178a6aeb1SAndreas Gohr        DOKU_INC.'lib/tpl/'.$conf['template'].'/');
17278a6aeb1SAndreas Gohr
173ed7b5f09Sandi// make session rewrites XHTML compliant
1743fc74836Sandi@ini_set('arg_separator.output', '&amp;');
175ed7b5f09Sandi
176d7e6bba9SAndreas Gohr// make sure global zlib does not interfere FS#1132
177d7e6bba9SAndreas Gohr@ini_set('zlib.output_compression', 'off');
178d7e6bba9SAndreas Gohr
1796deb5405SAndreas Gohr// increase PCRE backtrack limit
1806deb5405SAndreas Gohr@ini_set('pcre.backtrack_limit', '20971520');
1816deb5405SAndreas Gohr
18298bda4fdSAndreas Gohr// enable gzip compression if supported
18398bda4fdSAndreas Gohr$conf['gzip_output'] &= (strpos($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip') !== false);
1843138b5c7SAndreas Gohrif ($conf['gzip_output'] &&
1853138b5c7SAndreas Gohr        !defined('DOKU_DISABLE_GZIP_OUTPUT') &&
18698bda4fdSAndreas Gohr        function_exists('ob_gzhandler')) {
1873138b5c7SAndreas Gohr    ob_start('ob_gzhandler');
1883138b5c7SAndreas Gohr}
1893138b5c7SAndreas Gohr
190ed7b5f09Sandi// init session
1916534245aSAndreas Gohrif (!headers_sent() && !defined('NOSESSION')){
192ed7b5f09Sandi    session_name("DokuWiki");
193f5c6743cSAndreas Gohr    if (version_compare(PHP_VERSION, '5.2.0', '>')) {
194f5c6743cSAndreas Gohr        session_set_cookie_params(0,DOKU_REL,'',($conf['securecookie'] && is_ssl()),true);
195f5c6743cSAndreas Gohr    }else{
196f5c6743cSAndreas Gohr        session_set_cookie_params(0,DOKU_REL,'',($conf['securecookie'] && is_ssl()));
197f5c6743cSAndreas Gohr    }
198bad31ae9SAndreas Gohr    session_start();
19914a122deSAndreas Gohr
20014a122deSAndreas Gohr    // load left over messages
20114a122deSAndreas Gohr    if(isset($_SESSION[DOKU_COOKIE]['msg'])){
20214a122deSAndreas Gohr        $MSG = $_SESSION[DOKU_COOKIE]['msg'];
20314a122deSAndreas Gohr        unset($_SESSION[DOKU_COOKIE]['msg']);
20414a122deSAndreas Gohr    }
205bad31ae9SAndreas Gohr}
206ed7b5f09Sandi
207ed7b5f09Sandi// kill magic quotes
208e55eb89cSAndreas Gohrif (get_magic_quotes_gpc() && !defined('MAGIC_QUOTES_STRIPPED')) {
209ed7b5f09Sandi    if (!empty($_GET))    remove_magic_quotes($_GET);
210ed7b5f09Sandi    if (!empty($_POST))   remove_magic_quotes($_POST);
211ed7b5f09Sandi    if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE);
212ed7b5f09Sandi    if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST);
2133fc74836Sandi    @ini_set('magic_quotes_gpc', 0);
214e55eb89cSAndreas Gohr    define('MAGIC_QUOTES_STRIPPED',1);
215ed7b5f09Sandi}
2163fc74836Sandi@set_magic_quotes_runtime(0);
2173fc74836Sandi@ini_set('magic_quotes_sybase',0);
218ed7b5f09Sandi
219a1637ffdSAndreas Gohr// don't let cookies ever interfere with request vars
220a1637ffdSAndreas Gohr$_REQUEST = array_merge($_GET,$_POST);
221a1637ffdSAndreas Gohr
2223dea4ebcSAndreas Gohr// we don't want a purge URL to be digged
223c66972f2SAdrian Langif(isset($_REQUEST['purge']) && $_SERVER['HTTP_REFERER']) unset($_REQUEST['purge']);
2243dea4ebcSAndreas Gohr
225ed7b5f09Sandi// disable gzip if not available
2268a447e5cSAndreas Gohrif($conf['compression'] == 'bz2' && !function_exists('bzopen')){
227fe893490SAndreas Gohr    $conf['compression'] = 'gz';
228501252a5SAndreas Gohr}
229fe893490SAndreas Gohrif($conf['compression'] == 'gz' && !function_exists('gzopen')){
230501252a5SAndreas Gohr    $conf['compression'] = 0;
231ed7b5f09Sandi}
232ed7b5f09Sandi
233e656dcd4SAndreas Gohr// fix dateformat for upgraders
234e656dcd4SAndreas Gohrif(strpos($conf['dformat'],'%') === false){
235e656dcd4SAndreas Gohr    $conf['dformat'] = '%Y/%m/%d %H:%M';
236e656dcd4SAndreas Gohr}
237e656dcd4SAndreas Gohr
2381ca31cfeSAndreas Gohr// precalculate file creation modes
2391ca31cfeSAndreas Gohrinit_creationmodes();
240ed7b5f09Sandi
2413dc3a5f1Sandi// make real paths and check them
24298407a7aSandiinit_paths();
2437367b368SAndreas Gohrinit_files();
244ed7b5f09Sandi
2458c4f28e8Sjan// automatic upgrade to script versions of certain files
246e7cb32dcSAndreas Gohrscriptify(DOKU_CONF.'users.auth');
247e7cb32dcSAndreas Gohrscriptify(DOKU_CONF.'acl.auth');
248f62ea8a1Sandi
249*f1986589SMichael Klier// setup plugin controller class (can be overwritten in preload.php)
250*f1986589SMichael Klier$plugin_types = array('admin','syntax','action','renderer', 'helper');
251*f1986589SMichael Klierglobal $plugin_controller_class, $plugin_controller;
252*f1986589SMichael Klierif (empty($plugin_controller_class)) $plugin_controller_class = 'Doku_Plugin_Controller';
253*f1986589SMichael Klier
254c7cb395cSAdrian Lang// load libraries
255c7cb395cSAdrian Langrequire_once(DOKU_INC.'inc/load.php');
256c7cb395cSAdrian Lang
257*f1986589SMichael Klier// initialize plugin controller
258*f1986589SMichael Klier$plugin_controller = new $plugin_controller_class();
259*f1986589SMichael Klier
260*f1986589SMichael Klier// initialize the event handler
261*f1986589SMichael Klierglobal $EVENT_HANDLER;
262*f1986589SMichael Klier$EVENT_HANDLER = new Doku_Event_Handler();
263*f1986589SMichael Klier
26416905344SAndreas Gohr// setup authentication system
265c7cb395cSAdrian Langif (!defined('NOSESSION')) {
26616905344SAndreas Gohr    auth_setup();
267c7cb395cSAdrian Lang}
268f62ea8a1Sandi
269f62ea8a1Sandi/**
27098407a7aSandi * Checks paths from config file
27198407a7aSandi */
27298407a7aSandifunction init_paths(){
27398407a7aSandi    global $conf;
27498407a7aSandi
27598407a7aSandi    $paths = array('datadir'   => 'pages',
27698407a7aSandi            'olddir'    => 'attic',
27798407a7aSandi            'mediadir'  => 'media',
27898407a7aSandi            'metadir'   => 'meta',
27998407a7aSandi            'cachedir'  => 'cache',
280579b0f7eSTNHarris            'indexdir'  => 'index',
281de33a58fSMichael Klier            'lockdir'   => 'locks',
282de33a58fSMichael Klier            'tmpdir'    => 'tmp');
28398407a7aSandi
28498407a7aSandi    foreach($paths as $c => $p){
285bb4866bdSchris        if(empty($conf[$c]))  $conf[$c] = $conf['savedir'].'/'.$p;
28698407a7aSandi        $conf[$c]             = init_path($conf[$c]);
2871983acc2SGuy Brand        if(empty($conf[$c]))  nice_die("The $c ('$p') does not exist, isn't accessible or writable.
28869dc3177SAndreas Gohr                You should check your config and permission settings.
28969dc3177SAndreas Gohr                Or maybe you want to <a href=\"install.php\">run the
29069dc3177SAndreas Gohr                installer</a>?");
29198407a7aSandi    }
29271726d78SBen Coburn
29371726d78SBen Coburn    // path to old changelog only needed for upgrading
29471726d78SBen Coburn    $conf['changelog_old'] = init_path((isset($conf['changelog']))?($conf['changelog']):($conf['savedir'].'/changes.log'));
29571726d78SBen Coburn    if ($conf['changelog_old']=='') { unset($conf['changelog_old']); }
29671726d78SBen Coburn    // hardcoded changelog because it is now a cache that lives in meta
29771726d78SBen Coburn    $conf['changelog'] = $conf['metadir'].'/_dokuwiki.changes';
29899c8d7f2Smichael    $conf['media_changelog'] = $conf['metadir'].'/_media.changes';
29998407a7aSandi}
30098407a7aSandi
30198407a7aSandi/**
3020d8850c4SAndreas Gohr * Checks the existance of certain files and creates them if missing.
3037367b368SAndreas Gohr */
3047367b368SAndreas Gohrfunction init_files(){
3057367b368SAndreas Gohr    global $conf;
3060d8850c4SAndreas Gohr
307579b0f7eSTNHarris    $files = array( $conf['indexdir'].'/page.idx');
3087367b368SAndreas Gohr
3097367b368SAndreas Gohr    foreach($files as $file){
3107367b368SAndreas Gohr        if(!@file_exists($file)){
3110d8850c4SAndreas Gohr            $fh = @fopen($file,'a');
3120d8850c4SAndreas Gohr            if($fh){
3137367b368SAndreas Gohr                fclose($fh);
3141ca31cfeSAndreas Gohr                if($conf['fperm']) chmod($file, $conf['fperm']);
3150d8850c4SAndreas Gohr            }else{
3163816dcbcSAndreas Gohr                nice_die("$file is not writable. Check your permissions settings!");
3170d8850c4SAndreas Gohr            }
3187367b368SAndreas Gohr        }
3197367b368SAndreas Gohr    }
3207367b368SAndreas Gohr}
3217367b368SAndreas Gohr
3227367b368SAndreas Gohr/**
3230d8850c4SAndreas Gohr * Returns absolute path
324f62ea8a1Sandi *
3250d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC.
3260d8850c4SAndreas Gohr * Check for accessability on directories as well.
3270d8850c4SAndreas Gohr *
3280d8850c4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
329f62ea8a1Sandi */
330f62ea8a1Sandifunction init_path($path){
3310d8850c4SAndreas Gohr    // check existance
33200976812SAndreas Gohr    $p = fullpath($path);
3330d8850c4SAndreas Gohr    if(!@file_exists($p)){
33400976812SAndreas Gohr        $p = fullpath(DOKU_INC.$path);
3350d8850c4SAndreas Gohr        if(!@file_exists($p)){
3368fc4e739Sandi            return '';
337f62ea8a1Sandi        }
3380d8850c4SAndreas Gohr    }
3390d8850c4SAndreas Gohr
3400d8850c4SAndreas Gohr    // check writability
3410d8850c4SAndreas Gohr    if(!@is_writable($p)){
3420d8850c4SAndreas Gohr        return '';
3430d8850c4SAndreas Gohr    }
3440d8850c4SAndreas Gohr
3450d8850c4SAndreas Gohr    // check accessability (execute bit) for directories
3460d8850c4SAndreas Gohr    if(@is_dir($p) && !@file_exists("$p/.")){
3470d8850c4SAndreas Gohr        return '';
3480d8850c4SAndreas Gohr    }
3490d8850c4SAndreas Gohr
3500d8850c4SAndreas Gohr    return $p;
3510d8850c4SAndreas Gohr}
3528c4f28e8Sjan
353ed7b5f09Sandi/**
3541ca31cfeSAndreas Gohr * Sets the internal config values fperm and dperm which, when set,
3551ca31cfeSAndreas Gohr * will be used to change the permission of a newly created dir or
3561ca31cfeSAndreas Gohr * file with chmod. Considers the influence of the system's umask
3571ca31cfeSAndreas Gohr * setting the values only if needed.
3581ca31cfeSAndreas Gohr */
3591ca31cfeSAndreas Gohrfunction init_creationmodes(){
3601ca31cfeSAndreas Gohr    global $conf;
3611ca31cfeSAndreas Gohr
3621ca31cfeSAndreas Gohr    // Legacy support for old umask/dmask scheme
3631ca31cfeSAndreas Gohr    unset($conf['dmask']);
3641ca31cfeSAndreas Gohr    unset($conf['fmask']);
3651ca31cfeSAndreas Gohr    unset($conf['umask']);
3661ca31cfeSAndreas Gohr    unset($conf['fperm']);
3671ca31cfeSAndreas Gohr    unset($conf['dperm']);
3681ca31cfeSAndreas Gohr
3699f3cdec3SAndreas Gohr    // get system umask, fallback to 0 if none available
3709f3cdec3SAndreas Gohr    $umask = @umask();
3719f3cdec3SAndreas Gohr    if(!$umask) $umask = 0000;
3721ca31cfeSAndreas Gohr
3731ca31cfeSAndreas Gohr    // check what is set automatically by the system on file creation
3741ca31cfeSAndreas Gohr    // and set the fperm param if it's not what we want
3751ca31cfeSAndreas Gohr    $auto_fmode = 0666 & ~$umask;
3761ca31cfeSAndreas Gohr    if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode'];
3771ca31cfeSAndreas Gohr
3781ca31cfeSAndreas Gohr    // check what is set automatically by the system on file creation
3791ca31cfeSAndreas Gohr    // and set the dperm param if it's not what we want
3801ca31cfeSAndreas Gohr    $auto_dmode = $conf['dmode'] & ~$umask;
3811ca31cfeSAndreas Gohr    if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode'];
3821ca31cfeSAndreas Gohr}
3831ca31cfeSAndreas Gohr
3841ca31cfeSAndreas Gohr/**
385ed7b5f09Sandi * remove magic quotes recursivly
386ed7b5f09Sandi *
387ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
388ed7b5f09Sandi */
389ed7b5f09Sandifunction remove_magic_quotes(&$array) {
390ed7b5f09Sandi    foreach (array_keys($array) as $key) {
39181c54349SAndreas Gohr        // handle magic quotes in keynames (breaks order)
39281c54349SAndreas Gohr        $sk = stripslashes($key);
39381c54349SAndreas Gohr        if($sk != $key){
39481c54349SAndreas Gohr            $array[$sk] = $array[$key];
39581c54349SAndreas Gohr            unset($array[$key]);
39681c54349SAndreas Gohr            $key = $sk;
39781c54349SAndreas Gohr        }
39881c54349SAndreas Gohr
39981c54349SAndreas Gohr        // do recursion if needed
400ed7b5f09Sandi        if (is_array($array[$key])) {
401ed7b5f09Sandi            remove_magic_quotes($array[$key]);
402ed7b5f09Sandi        }else {
403ed7b5f09Sandi            $array[$key] = stripslashes($array[$key]);
404ed7b5f09Sandi        }
405ed7b5f09Sandi    }
406ed7b5f09Sandi}
407ed7b5f09Sandi
408ed7b5f09Sandi/**
409ed7b5f09Sandi * Returns the full absolute URL to the directory where
410ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash)
411ed7b5f09Sandi *
412ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
413ed7b5f09Sandi */
4144b1a4e04SAndreas Gohrfunction getBaseURL($abs=null){
415ed7b5f09Sandi    global $conf;
416ed7b5f09Sandi    //if canonical url enabled always return absolute
4174b1a4e04SAndreas Gohr    if(is_null($abs)) $abs = $conf['canonical'];
418ed7b5f09Sandi
41992b83b77Sandi    if($conf['basedir']){
42046c73e01SChris Smith        $dir = $conf['basedir'];
42189aa05dbSAndreas Gohr    }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){
42246c73e01SChris Smith        $dir = dirname($_SERVER['SCRIPT_NAME']);
42389aa05dbSAndreas Gohr    }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){
42446c73e01SChris Smith        $dir = dirname($_SERVER['PHP_SELF']);
425093ec9e4Sandi    }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
426093ec9e4Sandi        $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
427093ec9e4Sandi                $_SERVER['SCRIPT_FILENAME']);
42846c73e01SChris Smith        $dir = dirname('/'.$dir);
42992b83b77Sandi    }else{
43046c73e01SChris Smith        $dir = '.'; //probably wrong
43192b83b77Sandi    }
432ed7b5f09Sandi
43346c73e01SChris Smith    $dir = str_replace('\\','/',$dir);             // bugfix for weird WIN behaviour
43446c73e01SChris Smith    $dir = preg_replace('#//+#','/',"/$dir/");     // ensure leading and trailing slashes
435ed7b5f09Sandi
436f62ea8a1Sandi    //handle script in lib/exe dir
437f62ea8a1Sandi    $dir = preg_replace('!lib/exe/$!','',$dir);
438f62ea8a1Sandi
439488d5fa0SMichael Klier chi@chimeric.de    //handle script in lib/plugins dir
440488d5fa0SMichael Klier chi@chimeric.de    $dir = preg_replace('!lib/plugins/.*$!','',$dir);
441488d5fa0SMichael Klier chi@chimeric.de
442ed7b5f09Sandi    //finish here for relative URLs
443ed7b5f09Sandi    if(!$abs) return $dir;
444ed7b5f09Sandi
44546c73e01SChris Smith    //use config option if available, trim any slash from end of baseurl to avoid multiple consecutive slashes in the path
44646c73e01SChris Smith    if($conf['baseurl']) return rtrim($conf['baseurl'],'/').$dir;
447ef7b3ecdSAndreas Gohr
448e82e3526SAndreas Gohr    //split hostheader into host and port
449c66972f2SAdrian Lang    $addr = explode(':',$_SERVER['HTTP_HOST']);
450c66972f2SAdrian Lang    $host = $addr[0];
451c66972f2SAdrian Lang    $port = '';
452c66972f2SAdrian Lang    if (isset($addr[1])) {
453c66972f2SAdrian Lang        $port = $addr[1];
454c66972f2SAdrian Lang    } elseif (isset($_SERVER['SERVER_PORT'])) {
455c66972f2SAdrian Lang        $port = $_SERVER['SERVER_PORT'];
456c66972f2SAdrian Lang    }
457f5c6743cSAndreas Gohr    if(!is_ssl()){
458ed7b5f09Sandi        $proto = 'http://';
459e82e3526SAndreas Gohr        if ($port == '80') {
460ed7b5f09Sandi            $port = '';
461ed7b5f09Sandi        }
462ed7b5f09Sandi    }else{
463ed7b5f09Sandi        $proto = 'https://';
464e82e3526SAndreas Gohr        if ($port == '443') {
465ed7b5f09Sandi            $port = '';
466ed7b5f09Sandi        }
467ed7b5f09Sandi    }
468ed7b5f09Sandi
469c66972f2SAdrian Lang    if($port !== '') $port = ':'.$port;
470e82e3526SAndreas Gohr
471ed7b5f09Sandi    return $proto.$host.$port.$dir;
472ed7b5f09Sandi}
473ed7b5f09Sandi
474b000c6d4Sandi/**
475f5c6743cSAndreas Gohr * Check if accessed via HTTPS
476f5c6743cSAndreas Gohr *
477f5c6743cSAndreas Gohr * Apache leaves ,$_SERVER['HTTPS'] empty when not available, IIS sets it to 'off'.
478f5c6743cSAndreas Gohr * 'false' and 'disabled' are just guessing
479f5c6743cSAndreas Gohr *
480f5c6743cSAndreas Gohr * @returns bool true when SSL is active
481f5c6743cSAndreas Gohr */
482f5c6743cSAndreas Gohrfunction is_ssl(){
483c66972f2SAdrian Lang    if (!isset($_SERVER['HTTPS']) ||
484c66972f2SAdrian Lang        preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){
485f5c6743cSAndreas Gohr        return false;
486f5c6743cSAndreas Gohr    }else{
487f5c6743cSAndreas Gohr        return true;
488f5c6743cSAndreas Gohr    }
489f5c6743cSAndreas Gohr}
490f5c6743cSAndreas Gohr
491f5c6743cSAndreas Gohr/**
492b000c6d4Sandi * Append a PHP extension to a given file and adds an exit call
493b000c6d4Sandi *
494b000c6d4Sandi * This is used to migrate some old configfiles. An added PHP extension
495b000c6d4Sandi * ensures the contents are not shown to webusers even if .htaccess files
496b000c6d4Sandi * do not work
497b000c6d4Sandi *
498b000c6d4Sandi * @author Jan Decaluwe <jan@jandecaluwe.com>
499b000c6d4Sandi */
5008c4f28e8Sjanfunction scriptify($file) {
5018c4f28e8Sjan    // checks
5028c4f28e8Sjan    if (!is_readable($file)) {
5038c4f28e8Sjan        return;
5048c4f28e8Sjan    }
5058c4f28e8Sjan    $fn = $file.'.php';
5068c4f28e8Sjan    if (@file_exists($fn)) {
5078c4f28e8Sjan        return;
5088c4f28e8Sjan    }
5098c4f28e8Sjan    $fh = fopen($fn, 'w');
5108c4f28e8Sjan    if (!$fh) {
5113816dcbcSAndreas Gohr        nice_die($fn.' is not writable. Check your permission settings!');
5128c4f28e8Sjan    }
5138c4f28e8Sjan    // write php exit hack first
5148c4f28e8Sjan    fwrite($fh, "# $fn\n");
5158c4f28e8Sjan    fwrite($fh, '# <?php exit()?>'."\n");
5168c4f28e8Sjan    fwrite($fh, "# Don't modify the lines above\n");
5178c4f28e8Sjan    fwrite($fh, "#\n");
5188c4f28e8Sjan    // copy existing lines
5198c4f28e8Sjan    $lines = file($file);
5208c4f28e8Sjan    foreach ($lines as $line){
5218c4f28e8Sjan        fwrite($fh, $line);
5228c4f28e8Sjan    }
5233ba793e2Sandi    fclose($fh);
524b000c6d4Sandi    //try to rename the old file
5253aee4c27SAndreas Gohr    io_rename($file,"$file.old");
5268c4f28e8Sjan}
5278c4f28e8Sjan
5283816dcbcSAndreas Gohr/**
5293816dcbcSAndreas Gohr * print a nice message even if no styles are loaded yet.
5303816dcbcSAndreas Gohr */
5313816dcbcSAndreas Gohrfunction nice_die($msg){
5323816dcbcSAndreas Gohr    echo<<<EOT
5333816dcbcSAndreas Gohr<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
5343816dcbcSAndreas Gohr    "http://www.w3.org/TR/html4/loose.dtd">
5353816dcbcSAndreas Gohr<html>
5363816dcbcSAndreas Gohr<head><title>DokuWiki Setup Error</title></head>
5373816dcbcSAndreas Gohr<body style="font-family: Arial, sans-serif">
5383816dcbcSAndreas Gohr    <div style="width:60%; margin: auto; background-color: #fcc;
5393816dcbcSAndreas Gohr                border: 1px solid #faa; padding: 0.5em 1em;">
5403816dcbcSAndreas Gohr        <h1 style="font-size: 120%">DokuWiki Setup Error</h1>
5413816dcbcSAndreas Gohr        <p>$msg</p>
5423816dcbcSAndreas Gohr    </div>
5433816dcbcSAndreas Gohr</body>
5443816dcbcSAndreas Gohr</html>
5453816dcbcSAndreas GohrEOT;
5463816dcbcSAndreas Gohr    exit;
5473816dcbcSAndreas Gohr}
5483816dcbcSAndreas Gohr
54900976812SAndreas Gohr/**
55000976812SAndreas Gohr * A realpath() replacement
55100976812SAndreas Gohr *
55200976812SAndreas Gohr * This function behaves similar to PHP's realpath() but does not resolve
55300976812SAndreas Gohr * symlinks or accesses upper directories
55400976812SAndreas Gohr *
5554761d30cSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
55600976812SAndreas Gohr * @author <richpageau at yahoo dot co dot uk>
55700976812SAndreas Gohr * @link   http://de3.php.net/manual/en/function.realpath.php#75992
55800976812SAndreas Gohr */
559b328697dSAndreas Gohrfunction fullpath($path,$exists=false){
5604761d30cSAndreas Gohr    static $run = 0;
5614761d30cSAndreas Gohr    $root  = '';
562f0a201c5SChris Smith    $iswin = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' || @$GLOBALS['DOKU_UNITTEST_ASSUME_WINDOWS']);
56300976812SAndreas Gohr
5644761d30cSAndreas Gohr    // find the (indestructable) root of the path - keeps windows stuff intact
5654761d30cSAndreas Gohr    if($path{0} == '/'){
5664761d30cSAndreas Gohr        $root = '/';
5674761d30cSAndreas Gohr    }elseif($iswin){
5684761d30cSAndreas Gohr        // match drive letter and UNC paths
5694761d30cSAndreas Gohr        if(preg_match('!^([a-zA-z]:)(.*)!',$path,$match)){
570b9c4302bSAndreas Gohr            $root = $match[1].'/';
5714761d30cSAndreas Gohr            $path = $match[2];
5724761d30cSAndreas Gohr        }else if(preg_match('!^(\\\\\\\\[^\\\\/]+\\\\[^\\\\/]+[\\\\/])(.*)!',$path,$match)){
5734761d30cSAndreas Gohr            $root = $match[1];
5744761d30cSAndreas Gohr            $path = $match[2];
57500976812SAndreas Gohr        }
5764761d30cSAndreas Gohr    }
5774761d30cSAndreas Gohr    $path = str_replace('\\','/',$path);
5784761d30cSAndreas Gohr
5794761d30cSAndreas Gohr    // if the given path wasn't absolute already, prepend the script path and retry
5804761d30cSAndreas Gohr    if(!$root){
5814761d30cSAndreas Gohr        $base = dirname($_SERVER['SCRIPT_FILENAME']);
5824761d30cSAndreas Gohr        $path = $base.'/'.$path;
5834761d30cSAndreas Gohr        if($run == 0){ // avoid endless recursion when base isn't absolute for some reason
5844761d30cSAndreas Gohr            $run++;
585b328697dSAndreas Gohr            return fullpath($path,$exists);
5864761d30cSAndreas Gohr        }
5874761d30cSAndreas Gohr    }
5884761d30cSAndreas Gohr    $run = 0;
58900976812SAndreas Gohr
59000976812SAndreas Gohr    // canonicalize
59100976812SAndreas Gohr    $path=explode('/', $path);
59200976812SAndreas Gohr    $newpath=array();
593ef38bfe8SAndreas Gohr    foreach($path as $p) {
594ef38bfe8SAndreas Gohr        if ($p === '' || $p === '.') continue;
595ef38bfe8SAndreas Gohr        if ($p==='..') {
59600976812SAndreas Gohr            array_pop($newpath);
59700976812SAndreas Gohr            continue;
59800976812SAndreas Gohr        }
599ef38bfe8SAndreas Gohr        array_push($newpath, $p);
60000976812SAndreas Gohr    }
6014761d30cSAndreas Gohr    $finalpath = $root.implode('/', $newpath);
60200976812SAndreas Gohr
603b328697dSAndreas Gohr    // check for existance when needed (except when unit testing)
604b328697dSAndreas Gohr    if($exists && !defined('DOKU_UNITTEST') && !@file_exists($finalpath)) {
6054761d30cSAndreas Gohr        return false;
60600976812SAndreas Gohr    }
6074761d30cSAndreas Gohr    return $finalpath;
60800976812SAndreas Gohr}
60900976812SAndreas Gohr
610