xref: /dokuwiki/inc/init.php (revision ccc4c71ca88c25bcefb7f42eb01f0c040487e3a9)
1ed7b5f09Sandi<?php
2ed7b5f09Sandi/**
3ed7b5f09Sandi * Initialize some defaults needed for DokuWiki
4ed7b5f09Sandi */
5ed7b5f09Sandi
63272d797SAndreas Gohr/**
73272d797SAndreas Gohr * timing Dokuwiki execution
8f50a239bSTakamura *
9f50a239bSTakamura * @param integer $start
10f50a239bSTakamura *
11f50a239bSTakamura * @return mixed
123272d797SAndreas Gohr */
13a609a9ccSBen Coburnfunction delta_time($start=0) {
14ac4be4d7SPiyush Mishra    return microtime(true)-((float)$start);
15a609a9ccSBen Coburn}
16a609a9ccSBen Coburndefine('DOKU_START_TIME', delta_time());
17a609a9ccSBen Coburn
18ccaeaa85SAndreas Gohrglobal $config_cascade;
19cca94fbcSRoland Hager$config_cascade = array();
20ccaeaa85SAndreas Gohr
2148beefecSAndreas Gohr// if available load a preload config file
22e6266454SChris Smith$preload = fullpath(dirname(__FILE__)).'/preload.php';
2379e79377SAndreas Gohrif (file_exists($preload)) include($preload);
2448beefecSAndreas Gohr
25ed7b5f09Sandi// define the include path
2600976812SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../').'/');
27ad15db82Sandi
28c2a6d816SAndreas Gohr// define Plugin dir
29c2a6d816SAndreas Gohrif(!defined('DOKU_PLUGIN'))  define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
30c2a6d816SAndreas Gohr
31e7cb32dcSAndreas Gohr// define config path (packagers may want to change this to /etc/dokuwiki/)
32b7551a6dSEsther Brunnerif(!defined('DOKU_CONF')) define('DOKU_CONF',DOKU_INC.'conf/');
33e7cb32dcSAndreas Gohr
34bad905f1SBen Coburn// check for error reporting override or set error reporting to sane values
3579e79377SAndreas Gohrif (!defined('DOKU_E_LEVEL') && file_exists(DOKU_CONF.'report_e_all')) {
36bad905f1SBen Coburn    define('DOKU_E_LEVEL', E_ALL);
37bad905f1SBen Coburn}
38fc80ed59SAndreas Gohrif (!defined('DOKU_E_LEVEL')) {
394fcd684aSMichael Hamann    error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT);
40fc80ed59SAndreas Gohr} else {
41fc80ed59SAndreas Gohr    error_reporting(DOKU_E_LEVEL);
42fc80ed59SAndreas Gohr}
43c53ea5f2Sandi
44a69722b3SAndreas Gohr// avoid caching issues #1594
45a69722b3SAndreas Gohrheader('Vary: Cookie');
46a69722b3SAndreas Gohr
4750602150SBen Coburn// init memory caches
48db959ae3SAndreas Gohrglobal $cache_revinfo;
49db959ae3SAndreas Gohr       $cache_revinfo = array();
50db959ae3SAndreas Gohrglobal $cache_wikifn;
51db959ae3SAndreas Gohr       $cache_wikifn = array();
52db959ae3SAndreas Gohrglobal $cache_cleanid;
53db959ae3SAndreas Gohr       $cache_cleanid = array();
54db959ae3SAndreas Gohrglobal $cache_authname;
55db959ae3SAndreas Gohr       $cache_authname = array();
56db959ae3SAndreas Gohrglobal $cache_metadata;
57db959ae3SAndreas Gohr       $cache_metadata = array();
5850602150SBen Coburn
59cca94fbcSRoland Hager// always include 'inc/config_cascade.php'
60cca94fbcSRoland Hager// previously in preload.php set fields of $config_cascade will be merged with the defaults
61e6a6dbfeSAndreas Gohrinclude(DOKU_INC.'inc/config_cascade.php');
62cb043f52SChris Smith
634724a577Sandi//prepare config array()
64ee20e7d1Sandiglobal $conf;
654724a577Sandi$conf = array();
664724a577Sandi
67cb043f52SChris Smith// load the global config file(s)
68b303b92cSChris Smithforeach (array('default','local','protected') as $config_group) {
69f8121585SChris Smith    if (empty($config_cascade['main'][$config_group])) continue;
70b303b92cSChris Smith    foreach ($config_cascade['main'][$config_group] as $config_file) {
7179e79377SAndreas Gohr        if (file_exists($config_file)) {
72f8121585SChris Smith            include($config_file);
73f8121585SChris Smith        }
74cb043f52SChris Smith    }
750a6ead41SAndreas Gohr}
76ad15db82Sandi
77066fee30SAndreas Gohr//prepare license array()
78066fee30SAndreas Gohrglobal $license;
79066fee30SAndreas Gohr$license = array();
80066fee30SAndreas Gohr
81066fee30SAndreas Gohr// load the license file(s)
82f8121585SChris Smithforeach (array('default','local') as $config_group) {
83f8121585SChris Smith    if (empty($config_cascade['license'][$config_group])) continue;
84f8121585SChris Smith    foreach ($config_cascade['license'][$config_group] as $config_file) {
8579e79377SAndreas Gohr        if(file_exists($config_file)){
86f8121585SChris Smith            include($config_file);
87f8121585SChris Smith        }
88f8121585SChris Smith    }
89066fee30SAndreas Gohr}
90066fee30SAndreas Gohr
911f8eb24fSAndreas Gohr// set timezone (as in pre 5.3.0 days)
921f8eb24fSAndreas Gohrdate_default_timezone_set(@date_default_timezone_get());
931f8eb24fSAndreas Gohr
94ed7b5f09Sandi// define baseURL
954b1a4e04SAndreas Gohrif(!defined('DOKU_REL')) define('DOKU_REL',getBaseURL(false));
96ed7b5f09Sandiif(!defined('DOKU_URL')) define('DOKU_URL',getBaseURL(true));
974b1a4e04SAndreas Gohrif(!defined('DOKU_BASE')){
984b1a4e04SAndreas Gohr    if($conf['canonical']){
994b1a4e04SAndreas Gohr        define('DOKU_BASE',DOKU_URL);
1004b1a4e04SAndreas Gohr    }else{
1014b1a4e04SAndreas Gohr        define('DOKU_BASE',DOKU_REL);
1024b1a4e04SAndreas Gohr    }
1034b1a4e04SAndreas Gohr}
1044b1a4e04SAndreas Gohr
105b8595a66SAndreas Gohr// define whitespace
106b4f2363aSAndreas Gohrif(!defined('NL')) define ('NL',"\n");
107b8595a66SAndreas Gohrif(!defined('DOKU_LF')) define ('DOKU_LF',"\n");
108b8595a66SAndreas Gohrif(!defined('DOKU_TAB')) define ('DOKU_TAB',"\t");
109ed7b5f09Sandi
110656c8fb3SAndreas Gohr// define cookie and session id, append server port when securecookie is configured FS#1664
11164159a61SAndreas Gohrif(!defined('DOKU_COOKIE')) define(
11264159a61SAndreas Gohr    'DOKU_COOKIE',
11364159a61SAndreas Gohr    'DW' . md5(DOKU_REL . (($conf['securecookie']) ? $_SERVER['SERVER_PORT'] : ''))
11464159a61SAndreas Gohr);
115e71ce681SAndreas Gohr
116ee20e7d1Sandi
117ed7b5f09Sandi// define main script
118ed7b5f09Sandiif(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php');
119ed7b5f09Sandi
120c163dbefSMichael Großeif(!defined('DOKU_TPL')) {
121c163dbefSMichael Große    /**
122c163dbefSMichael Große     * @deprecated 2012-10-13 replaced by more dynamic method
123c163dbefSMichael Große     * @see tpl_basedir()
124c163dbefSMichael Große     */
125c163dbefSMichael Große    define('DOKU_TPL', DOKU_BASE.'lib/tpl/'.$conf['template'].'/');
126c163dbefSMichael Große}
1276b13307fSandi
128c163dbefSMichael Großeif(!defined('DOKU_TPLINC')) {
129c163dbefSMichael Große    /**
130c163dbefSMichael Große     * @deprecated 2012-10-13 replaced by more dynamic method
131c163dbefSMichael Große     * @see tpl_incdir()
132c163dbefSMichael Große     */
133c163dbefSMichael Große    define('DOKU_TPLINC', DOKU_INC.'lib/tpl/'.$conf['template'].'/');
134c163dbefSMichael Große}
13578a6aeb1SAndreas Gohr
136ed7b5f09Sandi// make session rewrites XHTML compliant
1373fc74836Sandi@ini_set('arg_separator.output', '&amp;');
138ed7b5f09Sandi
139d7e6bba9SAndreas Gohr// make sure global zlib does not interfere FS#1132
140d7e6bba9SAndreas Gohr@ini_set('zlib.output_compression', 'off');
141d7e6bba9SAndreas Gohr
1426deb5405SAndreas Gohr// increase PCRE backtrack limit
1436deb5405SAndreas Gohr@ini_set('pcre.backtrack_limit', '20971520');
1446deb5405SAndreas Gohr
14598bda4fdSAndreas Gohr// enable gzip compression if supported
14698bda4fdSAndreas Gohr$conf['gzip_output'] &= (strpos($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip') !== false);
14765f6e7d6SMichael Hamannglobal $ACT;
1483138b5c7SAndreas Gohrif ($conf['gzip_output'] &&
1493138b5c7SAndreas Gohr        !defined('DOKU_DISABLE_GZIP_OUTPUT') &&
15065f6e7d6SMichael Hamann        function_exists('ob_gzhandler') &&
15199e10b7fSMichael Hamann        // Disable compression when a (compressed) sitemap might be delivered
15265f6e7d6SMichael Hamann        // See https://bugs.dokuwiki.org/index.php?do=details&task_id=2576
15399e10b7fSMichael Hamann        $ACT != 'sitemap') {
1543138b5c7SAndreas Gohr    ob_start('ob_gzhandler');
1553138b5c7SAndreas Gohr}
1563138b5c7SAndreas Gohr
157ed7b5f09Sandi// init session
1586534245aSAndreas Gohrif(!headers_sent() && !defined('NOSESSION')) {
159c09f0eb1SGerrit Uitslag    if(!defined('DOKU_SESSION_NAME'))     define ('DOKU_SESSION_NAME', "DokuWiki");
160c09f0eb1SGerrit Uitslag    if(!defined('DOKU_SESSION_LIFETIME')) define ('DOKU_SESSION_LIFETIME', 0);
16155a71a16SGerrit Uitslag    if(!defined('DOKU_SESSION_PATH')) {
16273ab87deSGabriel Birke        $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir'];
16355a71a16SGerrit Uitslag        define ('DOKU_SESSION_PATH', $cookieDir);
164f5c6743cSAndreas Gohr    }
165c09f0eb1SGerrit Uitslag    if(!defined('DOKU_SESSION_DOMAIN'))   define ('DOKU_SESSION_DOMAIN', '');
166c09f0eb1SGerrit Uitslag
1676eb3cdf6SAndreas Gohr    // start the session
1686eb3cdf6SAndreas Gohr    init_session();
16914a122deSAndreas Gohr
17014a122deSAndreas Gohr    // load left over messages
17114a122deSAndreas Gohr    if(isset($_SESSION[DOKU_COOKIE]['msg'])) {
17214a122deSAndreas Gohr        $MSG = $_SESSION[DOKU_COOKIE]['msg'];
17314a122deSAndreas Gohr        unset($_SESSION[DOKU_COOKIE]['msg']);
17414a122deSAndreas Gohr    }
175bad31ae9SAndreas Gohr}
176ed7b5f09Sandi
177a1637ffdSAndreas Gohr// don't let cookies ever interfere with request vars
178a1637ffdSAndreas Gohr$_REQUEST = array_merge($_GET,$_POST);
179a1637ffdSAndreas Gohr
1803dea4ebcSAndreas Gohr// we don't want a purge URL to be digged
1810e80bb5eSChristopher Smithif(isset($_REQUEST['purge']) && !empty($_SERVER['HTTP_REFERER'])) unset($_REQUEST['purge']);
1823dea4ebcSAndreas Gohr
1831ca31cfeSAndreas Gohr// precalculate file creation modes
1841ca31cfeSAndreas Gohrinit_creationmodes();
185ed7b5f09Sandi
1863dc3a5f1Sandi// make real paths and check them
18798407a7aSandiinit_paths();
1887367b368SAndreas Gohrinit_files();
189ed7b5f09Sandi
190f1986589SMichael Klier// setup plugin controller class (can be overwritten in preload.php)
19193a7873eSAndreas Gohr$plugin_types = array('auth', 'admin','syntax','action','renderer', 'helper','remote');
192f1986589SMichael Klierglobal $plugin_controller_class, $plugin_controller;
193f1986589SMichael Klierif (empty($plugin_controller_class)) $plugin_controller_class = 'Doku_Plugin_Controller';
194f1986589SMichael Klier
195c7cb395cSAdrian Lang// load libraries
196605f8e8dSAndreas Gohrrequire_once(DOKU_INC.'vendor/autoload.php');
197c7cb395cSAdrian Langrequire_once(DOKU_INC.'inc/load.php');
198c7cb395cSAdrian Lang
1990f8f7aaaSDanny Lin// disable gzip if not available
20013c37900SAndreas Gohrdefine('DOKU_HAS_BZIP', function_exists('bzopen'));
20113c37900SAndreas Gohrdefine('DOKU_HAS_GZIP', function_exists('gzopen'));
20213c37900SAndreas Gohrif($conf['compression'] == 'bz2' && !DOKU_HAS_BZIP) {
2030f8f7aaaSDanny Lin    $conf['compression'] = 'gz';
2040f8f7aaaSDanny Lin}
20513c37900SAndreas Gohrif($conf['compression'] == 'gz' && !DOKU_HAS_GZIP) {
2060f8f7aaaSDanny Lin    $conf['compression'] = 0;
2070f8f7aaaSDanny Lin}
2080f8f7aaaSDanny Lin
20989177306SAndreas Gohr// input handle class
21089177306SAndreas Gohrglobal $INPUT;
211*ccc4c71cSAndreas Gohr$INPUT = new \dokuwiki\Input\Input();
21289177306SAndreas Gohr
213f1986589SMichael Klier// initialize plugin controller
214f1986589SMichael Klier$plugin_controller = new $plugin_controller_class();
215f1986589SMichael Klier
216f1986589SMichael Klier// initialize the event handler
217f1986589SMichael Klierglobal $EVENT_HANDLER;
218f1986589SMichael Klier$EVENT_HANDLER = new Doku_Event_Handler();
219f1986589SMichael Klier
2206d06b26aSDominik Eckelmann$local = $conf['lang'];
2216d06b26aSDominik Eckelmanntrigger_event('INIT_LANG_LOAD', $local, 'init_lang', true);
2226d06b26aSDominik Eckelmann
2236d06b26aSDominik Eckelmann
22416905344SAndreas Gohr// setup authentication system
225c7cb395cSAdrian Langif (!defined('NOSESSION')) {
22616905344SAndreas Gohr    auth_setup();
227c7cb395cSAdrian Lang}
228f62ea8a1Sandi
2295ec3fefcSAndreas Gohr// setup mail system
2305ec3fefcSAndreas Gohrmail_setup();
2315ec3fefcSAndreas Gohr
232f62ea8a1Sandi/**
2336eb3cdf6SAndreas Gohr * Initializes the session
2346eb3cdf6SAndreas Gohr *
2356eb3cdf6SAndreas Gohr * Makes sure the passed session cookie is valid, invalid ones are ignored an a new session ID is issued
2366eb3cdf6SAndreas Gohr *
2376eb3cdf6SAndreas Gohr * @link http://stackoverflow.com/a/33024310/172068
238924e477eSAndreas Gohr * @link http://php.net/manual/en/session.configuration.php#ini.session.sid-length
2396eb3cdf6SAndreas Gohr */
2406eb3cdf6SAndreas Gohrfunction init_session() {
2416eb3cdf6SAndreas Gohr    global $conf;
2426eb3cdf6SAndreas Gohr    session_name(DOKU_SESSION_NAME);
24364159a61SAndreas Gohr    session_set_cookie_params(
24464159a61SAndreas Gohr        DOKU_SESSION_LIFETIME,
24564159a61SAndreas Gohr        DOKU_SESSION_PATH,
24664159a61SAndreas Gohr        DOKU_SESSION_DOMAIN,
24764159a61SAndreas Gohr        ($conf['securecookie'] && is_ssl()),
24864159a61SAndreas Gohr        true
24964159a61SAndreas Gohr    );
2506eb3cdf6SAndreas Gohr
2516eb3cdf6SAndreas Gohr    // make sure the session cookie contains a valid session ID
252924e477eSAndreas Gohr    if(isset($_COOKIE[DOKU_SESSION_NAME]) && !preg_match('/^[-,a-zA-Z0-9]{22,256}$/', $_COOKIE[DOKU_SESSION_NAME])) {
2536eb3cdf6SAndreas Gohr        unset($_COOKIE[DOKU_SESSION_NAME]);
2546eb3cdf6SAndreas Gohr    }
2556eb3cdf6SAndreas Gohr
2566eb3cdf6SAndreas Gohr    session_start();
2576eb3cdf6SAndreas Gohr}
2586eb3cdf6SAndreas Gohr
2596eb3cdf6SAndreas Gohr
2606eb3cdf6SAndreas Gohr/**
26198407a7aSandi * Checks paths from config file
26298407a7aSandi */
26398407a7aSandifunction init_paths(){
26498407a7aSandi    global $conf;
26598407a7aSandi
26698407a7aSandi    $paths = array('datadir'   => 'pages',
26798407a7aSandi            'olddir'    => 'attic',
26898407a7aSandi            'mediadir'  => 'media',
269e4f389efSKate Arzamastseva            'mediaolddir' => 'media_attic',
27098407a7aSandi            'metadir'   => 'meta',
271e4f389efSKate Arzamastseva            'mediametadir' => 'media_meta',
27298407a7aSandi            'cachedir'  => 'cache',
273579b0f7eSTNHarris            'indexdir'  => 'index',
274de33a58fSMichael Klier            'lockdir'   => 'locks',
275de33a58fSMichael Klier            'tmpdir'    => 'tmp');
27698407a7aSandi
27798407a7aSandi    foreach($paths as $c => $p) {
2787f086b67SAnika Henke        $path = empty($conf[$c]) ? $conf['savedir'].'/'.$p : $conf[$c];
2796b9c156cSAnika Henke        $conf[$c] = init_path($path);
2806b9c156cSAnika Henke        if(empty($conf[$c]))
2816b9c156cSAnika Henke            nice_die("The $c ('$p') at $path is not found, isn't accessible or writable.
28269dc3177SAndreas Gohr                You should check your config and permission settings.
28369dc3177SAndreas Gohr                Or maybe you want to <a href=\"install.php\">run the
28469dc3177SAndreas Gohr                installer</a>?");
28598407a7aSandi    }
28671726d78SBen Coburn
28771726d78SBen Coburn    // path to old changelog only needed for upgrading
28864159a61SAndreas Gohr    $conf['changelog_old'] = init_path(
28964159a61SAndreas Gohr        (isset($conf['changelog'])) ? ($conf['changelog']) : ($conf['savedir'] . '/changes.log')
29064159a61SAndreas Gohr    );
29171726d78SBen Coburn    if ($conf['changelog_old']=='') { unset($conf['changelog_old']); }
29271726d78SBen Coburn    // hardcoded changelog because it is now a cache that lives in meta
29371726d78SBen Coburn    $conf['changelog'] = $conf['metadir'].'/_dokuwiki.changes';
29499c8d7f2Smichael    $conf['media_changelog'] = $conf['metadir'].'/_media.changes';
29598407a7aSandi}
29698407a7aSandi
29738fb1fc7SGerrit Uitslag/**
29838fb1fc7SGerrit Uitslag * Load the language strings
29938fb1fc7SGerrit Uitslag *
30038fb1fc7SGerrit Uitslag * @param string $langCode language code, as passed by event handler
30138fb1fc7SGerrit Uitslag */
3026d06b26aSDominik Eckelmannfunction init_lang($langCode) {
3036d06b26aSDominik Eckelmann    //prepare language array
304dd7a6159SGerrit Uitslag    global $lang, $config_cascade;
3056d06b26aSDominik Eckelmann    $lang = array();
3066d06b26aSDominik Eckelmann
3076d06b26aSDominik Eckelmann    //load the language files
3081d82c8d3SChristopher Smith    require(DOKU_INC.'inc/lang/en/lang.php');
309dd7a6159SGerrit Uitslag    foreach ($config_cascade['lang']['core'] as $config_file) {
31079e79377SAndreas Gohr        if (file_exists($config_file . 'en/lang.php')) {
311dd7a6159SGerrit Uitslag            include($config_file . 'en/lang.php');
312dd7a6159SGerrit Uitslag        }
313dd7a6159SGerrit Uitslag    }
314dd7a6159SGerrit Uitslag
3156d06b26aSDominik Eckelmann    if ($langCode && $langCode != 'en') {
3166d06b26aSDominik Eckelmann        if (file_exists(DOKU_INC."inc/lang/$langCode/lang.php")) {
3171d82c8d3SChristopher Smith            require(DOKU_INC."inc/lang/$langCode/lang.php");
3186d06b26aSDominik Eckelmann        }
319dd7a6159SGerrit Uitslag        foreach ($config_cascade['lang']['core'] as $config_file) {
32079e79377SAndreas Gohr            if (file_exists($config_file . "$langCode/lang.php")) {
321dd7a6159SGerrit Uitslag                include($config_file . "$langCode/lang.php");
3226d06b26aSDominik Eckelmann            }
323dd7a6159SGerrit Uitslag        }
3246d06b26aSDominik Eckelmann    }
3256d06b26aSDominik Eckelmann}
3266d06b26aSDominik Eckelmann
32798407a7aSandi/**
3286b9c156cSAnika Henke * Checks the existence of certain files and creates them if missing.
3297367b368SAndreas Gohr */
3307367b368SAndreas Gohrfunction init_files(){
3317367b368SAndreas Gohr    global $conf;
3320d8850c4SAndreas Gohr
333345b1674SAndreas Gohr    $files = array($conf['indexdir'].'/page.idx');
3347367b368SAndreas Gohr
3357367b368SAndreas Gohr    foreach($files as $file){
33679e79377SAndreas Gohr        if(!file_exists($file)){
3370d8850c4SAndreas Gohr            $fh = @fopen($file,'a');
3380d8850c4SAndreas Gohr            if($fh){
3397367b368SAndreas Gohr                fclose($fh);
340443e135dSChristopher Smith                if(!empty($conf['fperm'])) chmod($file, $conf['fperm']);
3410d8850c4SAndreas Gohr            }else{
3423816dcbcSAndreas Gohr                nice_die("$file is not writable. Check your permissions settings!");
3430d8850c4SAndreas Gohr            }
3447367b368SAndreas Gohr        }
3457367b368SAndreas Gohr    }
3467367b368SAndreas Gohr}
3477367b368SAndreas Gohr
3487367b368SAndreas Gohr/**
3490d8850c4SAndreas Gohr * Returns absolute path
350f62ea8a1Sandi *
3510d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC.
3527f086b67SAnika Henke * Check for accessibility on directories as well.
3530d8850c4SAndreas Gohr *
3540d8850c4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
355f50a239bSTakamura *
356f50a239bSTakamura * @param string $path
357f50a239bSTakamura *
358f50a239bSTakamura * @return bool|string
359f62ea8a1Sandi */
360f62ea8a1Sandifunction init_path($path){
3616b9c156cSAnika Henke    // check existence
36200976812SAndreas Gohr    $p = fullpath($path);
36379e79377SAndreas Gohr    if(!file_exists($p)){
36400976812SAndreas Gohr        $p = fullpath(DOKU_INC.$path);
36579e79377SAndreas Gohr        if(!file_exists($p)){
3668fc4e739Sandi            return '';
367f62ea8a1Sandi        }
3680d8850c4SAndreas Gohr    }
3690d8850c4SAndreas Gohr
3700d8850c4SAndreas Gohr    // check writability
3710d8850c4SAndreas Gohr    if(!@is_writable($p)){
3720d8850c4SAndreas Gohr        return '';
3730d8850c4SAndreas Gohr    }
3740d8850c4SAndreas Gohr
3750d8850c4SAndreas Gohr    // check accessability (execute bit) for directories
37679e79377SAndreas Gohr    if(@is_dir($p) && !file_exists("$p/.")){
3770d8850c4SAndreas Gohr        return '';
3780d8850c4SAndreas Gohr    }
3790d8850c4SAndreas Gohr
3800d8850c4SAndreas Gohr    return $p;
3810d8850c4SAndreas Gohr}
3828c4f28e8Sjan
383ed7b5f09Sandi/**
3841ca31cfeSAndreas Gohr * Sets the internal config values fperm and dperm which, when set,
3851ca31cfeSAndreas Gohr * will be used to change the permission of a newly created dir or
3861ca31cfeSAndreas Gohr * file with chmod. Considers the influence of the system's umask
3871ca31cfeSAndreas Gohr * setting the values only if needed.
3881ca31cfeSAndreas Gohr */
3891ca31cfeSAndreas Gohrfunction init_creationmodes(){
3901ca31cfeSAndreas Gohr    global $conf;
3911ca31cfeSAndreas Gohr
3921ca31cfeSAndreas Gohr    // Legacy support for old umask/dmask scheme
3931ca31cfeSAndreas Gohr    unset($conf['dmask']);
3941ca31cfeSAndreas Gohr    unset($conf['fmask']);
3951ca31cfeSAndreas Gohr    unset($conf['umask']);
3961ca31cfeSAndreas Gohr    unset($conf['fperm']);
3971ca31cfeSAndreas Gohr    unset($conf['dperm']);
3981ca31cfeSAndreas Gohr
3999f3cdec3SAndreas Gohr    // get system umask, fallback to 0 if none available
4009f3cdec3SAndreas Gohr    $umask = @umask();
4019f3cdec3SAndreas Gohr    if(!$umask) $umask = 0000;
4021ca31cfeSAndreas Gohr
4031ca31cfeSAndreas Gohr    // check what is set automatically by the system on file creation
4041ca31cfeSAndreas Gohr    // and set the fperm param if it's not what we want
4051ca31cfeSAndreas Gohr    $auto_fmode = 0666 & ~$umask;
4061ca31cfeSAndreas Gohr    if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode'];
4071ca31cfeSAndreas Gohr
4081ca31cfeSAndreas Gohr    // check what is set automatically by the system on file creation
4091ca31cfeSAndreas Gohr    // and set the dperm param if it's not what we want
4101ca31cfeSAndreas Gohr    $auto_dmode = $conf['dmode'] & ~$umask;
4111ca31cfeSAndreas Gohr    if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode'];
4121ca31cfeSAndreas Gohr}
4131ca31cfeSAndreas Gohr
4141ca31cfeSAndreas Gohr/**
415ed7b5f09Sandi * Returns the full absolute URL to the directory where
416ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash)
417ed7b5f09Sandi *
418585bf44eSChristopher Smith * !! Can not access $_SERVER values through $INPUT
419585bf44eSChristopher Smith * !! here as this function is called before $INPUT is
420585bf44eSChristopher Smith * !! initialized.
421585bf44eSChristopher Smith *
422ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
423f50a239bSTakamura *
424f50a239bSTakamura * @param null|string $abs
425f50a239bSTakamura *
426f50a239bSTakamura * @return string
427ed7b5f09Sandi */
4284b1a4e04SAndreas Gohrfunction getBaseURL($abs=null){
429ed7b5f09Sandi    global $conf;
430ed7b5f09Sandi    //if canonical url enabled always return absolute
4314b1a4e04SAndreas Gohr    if(is_null($abs)) $abs = $conf['canonical'];
432ed7b5f09Sandi
4331858e4d7SGerry Weißbach    if(!empty($conf['basedir'])){
43446c73e01SChris Smith        $dir = $conf['basedir'];
43589aa05dbSAndreas Gohr    }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){
43646c73e01SChris Smith        $dir = dirname($_SERVER['SCRIPT_NAME']);
43789aa05dbSAndreas Gohr    }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){
43846c73e01SChris Smith        $dir = dirname($_SERVER['PHP_SELF']);
439093ec9e4Sandi    }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
440093ec9e4Sandi        $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
441093ec9e4Sandi                $_SERVER['SCRIPT_FILENAME']);
44246c73e01SChris Smith        $dir = dirname('/'.$dir);
44392b83b77Sandi    }else{
44446c73e01SChris Smith        $dir = '.'; //probably wrong
44592b83b77Sandi    }
446ed7b5f09Sandi
44746c73e01SChris Smith    $dir = str_replace('\\','/',$dir);             // bugfix for weird WIN behaviour
44846c73e01SChris Smith    $dir = preg_replace('#//+#','/',"/$dir/");     // ensure leading and trailing slashes
449ed7b5f09Sandi
450f62ea8a1Sandi    //handle script in lib/exe dir
451f62ea8a1Sandi    $dir = preg_replace('!lib/exe/$!','',$dir);
452f62ea8a1Sandi
453488d5fa0SMichael Klier chi@chimeric.de    //handle script in lib/plugins dir
454488d5fa0SMichael Klier chi@chimeric.de    $dir = preg_replace('!lib/plugins/.*$!','',$dir);
455488d5fa0SMichael Klier chi@chimeric.de
456ed7b5f09Sandi    //finish here for relative URLs
457ed7b5f09Sandi    if(!$abs) return $dir;
458ed7b5f09Sandi
45964159a61SAndreas Gohr    //use config if available, trim any slash from end of baseurl to avoid multiple consecutive slashes in the path
4601858e4d7SGerry Weißbach    if(!empty($conf['baseurl'])) return rtrim($conf['baseurl'],'/').$dir;
461ef7b3ecdSAndreas Gohr
462e82e3526SAndreas Gohr    //split hostheader into host and port
4635627186cSAndreas Gohr    if(isset($_SERVER['HTTP_HOST'])){
464204b27c8SMichael Hamann        $parsed_host = parse_url('http://'.$_SERVER['HTTP_HOST']);
465f87b5dbbSChristopher Smith        $host = isset($parsed_host['host']) ? $parsed_host['host'] : null;
466f87b5dbbSChristopher Smith        $port = isset($parsed_host['port']) ? $parsed_host['port'] : null;
4675627186cSAndreas Gohr    }elseif(isset($_SERVER['SERVER_NAME'])){
468204b27c8SMichael Hamann        $parsed_host = parse_url('http://'.$_SERVER['SERVER_NAME']);
469f87b5dbbSChristopher Smith        $host = isset($parsed_host['host']) ? $parsed_host['host'] : null;
470f87b5dbbSChristopher Smith        $port = isset($parsed_host['port']) ? $parsed_host['port'] : null;
4715627186cSAndreas Gohr    }else{
4725627186cSAndreas Gohr        $host = php_uname('n');
473c66972f2SAdrian Lang        $port = '';
4745627186cSAndreas Gohr    }
4755627186cSAndreas Gohr
476204b27c8SMichael Hamann    if(is_null($port)){
477204b27c8SMichael Hamann        $port = '';
478204b27c8SMichael Hamann    }
479204b27c8SMichael Hamann
480f5c6743cSAndreas Gohr    if(!is_ssl()){
481ed7b5f09Sandi        $proto = 'http://';
482e82e3526SAndreas Gohr        if ($port == '80') {
483ed7b5f09Sandi            $port = '';
484ed7b5f09Sandi        }
485ed7b5f09Sandi    }else{
486ed7b5f09Sandi        $proto = 'https://';
487e82e3526SAndreas Gohr        if ($port == '443') {
488ed7b5f09Sandi            $port = '';
489ed7b5f09Sandi        }
490ed7b5f09Sandi    }
491ed7b5f09Sandi
492c66972f2SAdrian Lang    if($port !== '') $port = ':'.$port;
493e82e3526SAndreas Gohr
494ed7b5f09Sandi    return $proto.$host.$port.$dir;
495ed7b5f09Sandi}
496ed7b5f09Sandi
497b000c6d4Sandi/**
498f5c6743cSAndreas Gohr * Check if accessed via HTTPS
499f5c6743cSAndreas Gohr *
500f5c6743cSAndreas Gohr * Apache leaves ,$_SERVER['HTTPS'] empty when not available, IIS sets it to 'off'.
501f5c6743cSAndreas Gohr * 'false' and 'disabled' are just guessing
502f5c6743cSAndreas Gohr *
503f5c6743cSAndreas Gohr * @returns bool true when SSL is active
504f5c6743cSAndreas Gohr */
505f5c6743cSAndreas Gohrfunction is_ssl() {
50619738e65SEnrico Tagliavini    // check if we are behind a reverse proxy
50719738e65SEnrico Tagliavini    if(isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
50819738e65SEnrico Tagliavini        if($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
50919738e65SEnrico Tagliavini            return true;
51019738e65SEnrico Tagliavini        } else {
51119738e65SEnrico Tagliavini            return false;
51219738e65SEnrico Tagliavini        }
51319738e65SEnrico Tagliavini    }
514c66972f2SAdrian Lang    if(!isset($_SERVER['HTTPS']) ||
515c66972f2SAdrian Lang        preg_match('/^(|off|false|disabled)$/i', $_SERVER['HTTPS'])) {
516f5c6743cSAndreas Gohr        return false;
517f5c6743cSAndreas Gohr    } else {
518f5c6743cSAndreas Gohr        return true;
519f5c6743cSAndreas Gohr    }
520f5c6743cSAndreas Gohr}
521f5c6743cSAndreas Gohr
522f5c6743cSAndreas Gohr/**
52326714386SAndreas Gohr * checks it is windows OS
52426714386SAndreas Gohr * @return bool
52526714386SAndreas Gohr */
52626714386SAndreas Gohrfunction isWindows() {
52726714386SAndreas Gohr    return (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? true : false;
52826714386SAndreas Gohr}
52926714386SAndreas Gohr
53026714386SAndreas Gohr/**
5313816dcbcSAndreas Gohr * print a nice message even if no styles are loaded yet.
532f50a239bSTakamura *
533f50a239bSTakamura * @param integer|string $msg
5343816dcbcSAndreas Gohr */
5353816dcbcSAndreas Gohrfunction nice_die($msg){
5363816dcbcSAndreas Gohr    echo<<<EOT
537c8839c22SAnika Henke<!DOCTYPE html>
5383816dcbcSAndreas Gohr<html>
5393816dcbcSAndreas Gohr<head><title>DokuWiki Setup Error</title></head>
5403816dcbcSAndreas Gohr<body style="font-family: Arial, sans-serif">
5413816dcbcSAndreas Gohr    <div style="width:60%; margin: auto; background-color: #fcc;
5423816dcbcSAndreas Gohr                border: 1px solid #faa; padding: 0.5em 1em;">
5433816dcbcSAndreas Gohr        <h1 style="font-size: 120%">DokuWiki Setup Error</h1>
5443816dcbcSAndreas Gohr        <p>$msg</p>
5453816dcbcSAndreas Gohr    </div>
5463816dcbcSAndreas Gohr</body>
5473816dcbcSAndreas Gohr</html>
5483816dcbcSAndreas GohrEOT;
5493862da0eSAndreas Gohr    if(defined('DOKU_UNITTEST')) {
5503862da0eSAndreas Gohr        throw new RuntimeException('nice_die: '.$msg);
5513862da0eSAndreas Gohr    }
5520a4266d4SElan Ruusamäe    exit(1);
5533816dcbcSAndreas Gohr}
5543816dcbcSAndreas Gohr
55500976812SAndreas Gohr/**
55600976812SAndreas Gohr * A realpath() replacement
55700976812SAndreas Gohr *
55800976812SAndreas Gohr * This function behaves similar to PHP's realpath() but does not resolve
55900976812SAndreas Gohr * symlinks or accesses upper directories
56000976812SAndreas Gohr *
5614761d30cSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
56200976812SAndreas Gohr * @author <richpageau at yahoo dot co dot uk>
56359752844SAnders Sandblad * @link   http://php.net/manual/en/function.realpath.php#75992
564f50a239bSTakamura *
565f50a239bSTakamura * @param string $path
566f50a239bSTakamura * @param bool $exists
567f50a239bSTakamura *
568f50a239bSTakamura * @return bool|string
56900976812SAndreas Gohr */
570b328697dSAndreas Gohrfunction fullpath($path,$exists=false){
5714761d30cSAndreas Gohr    static $run = 0;
5724761d30cSAndreas Gohr    $root  = '';
573724970e6SAndreas Gohr    $iswin = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' || !empty($GLOBALS['DOKU_UNITTEST_ASSUME_WINDOWS']));
57400976812SAndreas Gohr
5754761d30cSAndreas Gohr    // find the (indestructable) root of the path - keeps windows stuff intact
5764761d30cSAndreas Gohr    if($path{0} == '/'){
5774761d30cSAndreas Gohr        $root = '/';
5784761d30cSAndreas Gohr    }elseif($iswin){
5794761d30cSAndreas Gohr        // match drive letter and UNC paths
5804761d30cSAndreas Gohr        if(preg_match('!^([a-zA-z]:)(.*)!',$path,$match)){
581b9c4302bSAndreas Gohr            $root = $match[1].'/';
5824761d30cSAndreas Gohr            $path = $match[2];
5834761d30cSAndreas Gohr        }else if(preg_match('!^(\\\\\\\\[^\\\\/]+\\\\[^\\\\/]+[\\\\/])(.*)!',$path,$match)){
5844761d30cSAndreas Gohr            $root = $match[1];
5854761d30cSAndreas Gohr            $path = $match[2];
58600976812SAndreas Gohr        }
5874761d30cSAndreas Gohr    }
5884761d30cSAndreas Gohr    $path = str_replace('\\','/',$path);
5894761d30cSAndreas Gohr
5904761d30cSAndreas Gohr    // if the given path wasn't absolute already, prepend the script path and retry
5914761d30cSAndreas Gohr    if(!$root){
5924761d30cSAndreas Gohr        $base = dirname($_SERVER['SCRIPT_FILENAME']);
5934761d30cSAndreas Gohr        $path = $base.'/'.$path;
5944761d30cSAndreas Gohr        if($run == 0){ // avoid endless recursion when base isn't absolute for some reason
5954761d30cSAndreas Gohr            $run++;
596b328697dSAndreas Gohr            return fullpath($path,$exists);
5974761d30cSAndreas Gohr        }
5984761d30cSAndreas Gohr    }
5994761d30cSAndreas Gohr    $run = 0;
60000976812SAndreas Gohr
60100976812SAndreas Gohr    // canonicalize
60200976812SAndreas Gohr    $path=explode('/', $path);
60300976812SAndreas Gohr    $newpath=array();
604ef38bfe8SAndreas Gohr    foreach($path as $p) {
605ef38bfe8SAndreas Gohr        if ($p === '' || $p === '.') continue;
606ef38bfe8SAndreas Gohr        if ($p==='..') {
60700976812SAndreas Gohr            array_pop($newpath);
60800976812SAndreas Gohr            continue;
60900976812SAndreas Gohr        }
610ef38bfe8SAndreas Gohr        array_push($newpath, $p);
61100976812SAndreas Gohr    }
6124761d30cSAndreas Gohr    $finalpath = $root.implode('/', $newpath);
61300976812SAndreas Gohr
6146b9c156cSAnika Henke    // check for existence when needed (except when unit testing)
61579e79377SAndreas Gohr    if($exists && !defined('DOKU_UNITTEST') && !file_exists($finalpath)) {
6164761d30cSAndreas Gohr        return false;
61700976812SAndreas Gohr    }
6184761d30cSAndreas Gohr    return $finalpath;
61900976812SAndreas Gohr}
62000976812SAndreas Gohr
621