xref: /dokuwiki/inc/init.php (revision f0a201c5703333fa2b120cda8e145fb405283908)
1ed7b5f09Sandi<?php
2ed7b5f09Sandi/**
3ed7b5f09Sandi * Initialize some defaults needed for DokuWiki
4ed7b5f09Sandi */
5ed7b5f09Sandi
6a609a9ccSBen Coburn  // start timing Dokuwiki execution
7a609a9ccSBen Coburn  function delta_time($start=0) {
8a609a9ccSBen Coburn    list($usec, $sec) = explode(" ", microtime());
9a609a9ccSBen Coburn    return ((float)$usec+(float)$sec)-((float)$start);
10a609a9ccSBen Coburn  }
11a609a9ccSBen Coburn  define('DOKU_START_TIME', delta_time());
12a609a9ccSBen Coburn
1348beefecSAndreas Gohr  // if available load a preload config file
14e6266454SChris Smith  $preload = fullpath(dirname(__FILE__)).'/preload.php';
15e6266454SChris Smith  if (@file_exists($preload)) include($preload);
1648beefecSAndreas Gohr
17ed7b5f09Sandi  // define the include path
1800976812SAndreas Gohr  if(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../').'/');
19ad15db82Sandi
20e7cb32dcSAndreas Gohr  // define config path (packagers may want to change this to /etc/dokuwiki/)
21b7551a6dSEsther Brunner  if(!defined('DOKU_CONF')) define('DOKU_CONF',DOKU_INC.'conf/');
22e7cb32dcSAndreas Gohr
23bad905f1SBen Coburn  // check for error reporting override or set error reporting to sane values
24d8186216SBen Coburn  if (!defined('DOKU_E_LEVEL') && @file_exists(DOKU_CONF.'report_e_all')) {
25bad905f1SBen Coburn    define('DOKU_E_LEVEL', E_ALL);
26bad905f1SBen Coburn  }
27fc80ed59SAndreas Gohr  if (!defined('DOKU_E_LEVEL')) {
28fc80ed59SAndreas Gohr    if(defined('E_DEPRECATED')){ // since php 5.3
29fc80ed59SAndreas Gohr      error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
30fc80ed59SAndreas Gohr    }else{
31fc80ed59SAndreas Gohr      error_reporting(E_ALL ^ E_NOTICE);
32fc80ed59SAndreas Gohr    }
33fc80ed59SAndreas Gohr  } else {
34fc80ed59SAndreas Gohr    error_reporting(DOKU_E_LEVEL);
35fc80ed59SAndreas Gohr  }
36c53ea5f2Sandi
3750602150SBen Coburn  // init memory caches
38bc3e97beSAndreas Gohr  global $cache_revinfo;  $cache_revinfo = array();
39bc3e97beSAndreas Gohr  global $cache_wikifn;   $cache_wikifn = array();
40a424cd8eSchris  global $cache_cleanid;  $cache_cleanid = array();
41a424cd8eSchris  global $cache_authname; $cache_authname = array();
420a7e3bceSchris  global $cache_metadata; $cache_metadata = array();
4350602150SBen Coburn
44cb043f52SChris Smith  //set the configuration cascade - but only if its not already been set in preload.php
45cb043f52SChris Smith  global $config_cascade;
46cb043f52SChris Smith  if (empty($config_cascade)) {
47cb043f52SChris Smith    $config_cascade = array(
48cb043f52SChris Smith      'main' => array(
49cb043f52SChris Smith        'default'   => array(DOKU_CONF.'dokuwiki.php'),
50cb043f52SChris Smith        'local'     => array(DOKU_CONF.'local.php'),
51cb043f52SChris Smith        'protected' => array(DOKU_CONF.'local.protected.php'),
52cb043f52SChris Smith      ),
53cb043f52SChris Smith      'acronyms'  => array(
54b303b92cSChris Smith        'default'   => array(DOKU_CONF.'acronyms.conf'),
55b303b92cSChris Smith        'local'     => array(DOKU_CONF.'acronyms.local.conf'),
56cb043f52SChris Smith      ),
57cb043f52SChris Smith      'entities'  => array(
58b303b92cSChris Smith        'default'   => array(DOKU_CONF.'entities.conf'),
59b303b92cSChris Smith        'local'     => array(DOKU_CONF.'entities.local.conf'),
60cb043f52SChris Smith      ),
61cb043f52SChris Smith      'interwiki' => array(
62b303b92cSChris Smith        'default'   => array(DOKU_CONF.'interwiki.conf'),
63b303b92cSChris Smith        'local'     => array(DOKU_CONF.'interwiki.local.conf'),
64cb043f52SChris Smith      ),
65cb043f52SChris Smith      'mime'      => array(
66b303b92cSChris Smith        'default'   => array(DOKU_CONF.'mime.conf'),
67b303b92cSChris Smith        'local'     => array(DOKU_CONF.'mime.local.conf'),
68cb043f52SChris Smith      ),
69cb043f52SChris Smith      'scheme'    => array(
70b303b92cSChris Smith        'default'   => array(DOKU_CONF.'scheme.conf'),
71b303b92cSChris Smith        'local'     => array(DOKU_CONF.'scheme.local.conf'),
72cb043f52SChris Smith      ),
73cb043f52SChris Smith      'smileys'   => array(
74b303b92cSChris Smith        'default'   => array(DOKU_CONF.'smileys.conf'),
75b303b92cSChris Smith        'local'     => array(DOKU_CONF.'smileys.local.conf'),
76cb043f52SChris Smith      ),
77cb043f52SChris Smith      'wordblock' => array(
78b303b92cSChris Smith        'default'   => array(DOKU_CONF.'wordblock.conf'),
79b303b92cSChris Smith        'local'     => array(DOKU_CONF.'wordblock.local.conf'),
80cb043f52SChris Smith      ),
81cb043f52SChris Smith    );
82cb043f52SChris Smith  }
83cb043f52SChris Smith
844724a577Sandi  //prepare config array()
85ee20e7d1Sandi  global $conf;
864724a577Sandi  $conf = array();
874724a577Sandi
88cb043f52SChris Smith  // load the global config file(s)
89b303b92cSChris Smith  foreach (array('default','local','protected') as $config_group) {
90b303b92cSChris Smith    foreach ($config_cascade['main'][$config_group] as $config_file) {
91cb043f52SChris Smith      @include($config_file);
92cb043f52SChris Smith    }
930a6ead41SAndreas Gohr  }
94ad15db82Sandi
95ad15db82Sandi  //prepare language array
96ee20e7d1Sandi  global $lang;
97ad15db82Sandi  $lang = array();
98ed7b5f09Sandi
9916521111Sandi  //load the language files
100bc3b6aecSandi  require_once(DOKU_INC.'inc/lang/en/lang.php');
101f949a01cSAndreas Gohr  if ( $conf['lang'] && $conf['lang'] != 'en' ) {
102bc3b6aecSandi    require_once(DOKU_INC.'inc/lang/'.$conf['lang'].'/lang.php');
103fc1c55b1Shfuecks  }
10416521111Sandi
105066fee30SAndreas Gohr  //prepare license array()
106066fee30SAndreas Gohr  global $license;
107066fee30SAndreas Gohr  $license = array();
108066fee30SAndreas Gohr
109066fee30SAndreas Gohr  // load the license file(s)
110066fee30SAndreas Gohr  require_once(DOKU_CONF.'license.php');
111066fee30SAndreas Gohr  if(@file_exists(DOKU_CONF.'license.php')){
112066fee30SAndreas Gohr    require_once(DOKU_CONF.'license.php');
113066fee30SAndreas Gohr  }
114066fee30SAndreas Gohr
115ed7b5f09Sandi  // define baseURL
1164b1a4e04SAndreas Gohr  if(!defined('DOKU_REL')) define('DOKU_REL',getBaseURL(false));
117ed7b5f09Sandi  if(!defined('DOKU_URL')) define('DOKU_URL',getBaseURL(true));
1184b1a4e04SAndreas Gohr  if(!defined('DOKU_BASE')){
1194b1a4e04SAndreas Gohr    if($conf['canonical']){
1204b1a4e04SAndreas Gohr      define('DOKU_BASE',DOKU_URL);
1214b1a4e04SAndreas Gohr    }else{
1224b1a4e04SAndreas Gohr      define('DOKU_BASE',DOKU_REL);
1234b1a4e04SAndreas Gohr    }
1244b1a4e04SAndreas Gohr  }
1254b1a4e04SAndreas Gohr
126b8595a66SAndreas Gohr  // define whitespace
127b8595a66SAndreas Gohr  if(!defined('DOKU_LF')) define ('DOKU_LF',"\n");
128b8595a66SAndreas Gohr  if(!defined('DOKU_TAB')) define ('DOKU_TAB',"\t");
129ed7b5f09Sandi
130e71ce681SAndreas Gohr  // define cookie and session id
1318b11f0a5SAndreas Gohr  if (!defined('DOKU_COOKIE')) define('DOKU_COOKIE', 'DW'.md5(DOKU_REL));
132e71ce681SAndreas Gohr
133ee20e7d1Sandi  // define Plugin dir
134f62ea8a1Sandi  if(!defined('DOKU_PLUGIN'))  define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
135ee20e7d1Sandi
136ed7b5f09Sandi  // define main script
137ed7b5f09Sandi  if(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php');
138ed7b5f09Sandi
1396b13307fSandi  // define Template baseURL
1406b13307fSandi  if(!defined('DOKU_TPL')) define('DOKU_TPL',
141f62ea8a1Sandi                                  DOKU_BASE.'lib/tpl/'.$conf['template'].'/');
1426b13307fSandi
14378a6aeb1SAndreas Gohr  // define real Template directory
14478a6aeb1SAndreas Gohr  if(!defined('DOKU_TPLINC')) define('DOKU_TPLINC',
14578a6aeb1SAndreas Gohr                                  DOKU_INC.'lib/tpl/'.$conf['template'].'/');
14678a6aeb1SAndreas Gohr
147ed7b5f09Sandi  // make session rewrites XHTML compliant
1483fc74836Sandi  @ini_set('arg_separator.output', '&amp;');
149ed7b5f09Sandi
150d7e6bba9SAndreas Gohr  // make sure global zlib does not interfere FS#1132
151d7e6bba9SAndreas Gohr  @ini_set('zlib.output_compression', 'off');
152d7e6bba9SAndreas Gohr
1536deb5405SAndreas Gohr  // increase PCRE backtrack limit
1546deb5405SAndreas Gohr  @ini_set('pcre.backtrack_limit', '20971520');
1556deb5405SAndreas Gohr
1563138b5c7SAndreas Gohr  // enable gzip compression
1573138b5c7SAndreas Gohr  if ($conf['gzip_output'] &&
1583138b5c7SAndreas Gohr      !defined('DOKU_DISABLE_GZIP_OUTPUT') &&
1593138b5c7SAndreas Gohr      function_exists('ob_gzhandler') &&
1603138b5c7SAndreas Gohr      preg_match('/gzip|deflate/', $_SERVER['HTTP_ACCEPT_ENCODING'])) {
1613138b5c7SAndreas Gohr    ob_start('ob_gzhandler');
1623138b5c7SAndreas Gohr  }
1633138b5c7SAndreas Gohr
164ed7b5f09Sandi  // init session
1656534245aSAndreas Gohr  if (!headers_sent() && !defined('NOSESSION')){
166ed7b5f09Sandi    session_name("DokuWiki");
167f5c6743cSAndreas Gohr    if (version_compare(PHP_VERSION, '5.2.0', '>')) {
168f5c6743cSAndreas Gohr      session_set_cookie_params(0,DOKU_REL,'',($conf['securecookie'] && is_ssl()),true);
169f5c6743cSAndreas Gohr    }else{
170f5c6743cSAndreas Gohr      session_set_cookie_params(0,DOKU_REL,'',($conf['securecookie'] && is_ssl()));
171f5c6743cSAndreas Gohr    }
172bad31ae9SAndreas Gohr    session_start();
17314a122deSAndreas Gohr
17414a122deSAndreas Gohr    // load left over messages
17514a122deSAndreas Gohr    if(isset($_SESSION[DOKU_COOKIE]['msg'])){
17614a122deSAndreas Gohr      $MSG = $_SESSION[DOKU_COOKIE]['msg'];
17714a122deSAndreas Gohr      unset($_SESSION[DOKU_COOKIE]['msg']);
17814a122deSAndreas Gohr    }
179bad31ae9SAndreas Gohr  }
180ed7b5f09Sandi
181ed7b5f09Sandi  // kill magic quotes
182e55eb89cSAndreas Gohr  if (get_magic_quotes_gpc() && !defined('MAGIC_QUOTES_STRIPPED')) {
183ed7b5f09Sandi    if (!empty($_GET))    remove_magic_quotes($_GET);
184ed7b5f09Sandi    if (!empty($_POST))   remove_magic_quotes($_POST);
185ed7b5f09Sandi    if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE);
186ed7b5f09Sandi    if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST);
1873fc74836Sandi    @ini_set('magic_quotes_gpc', 0);
188e55eb89cSAndreas Gohr    define('MAGIC_QUOTES_STRIPPED',1);
189ed7b5f09Sandi  }
1903fc74836Sandi  @set_magic_quotes_runtime(0);
1913fc74836Sandi  @ini_set('magic_quotes_sybase',0);
192ed7b5f09Sandi
193a1637ffdSAndreas Gohr  // don't let cookies ever interfere with request vars
194a1637ffdSAndreas Gohr  $_REQUEST = array_merge($_GET,$_POST);
195a1637ffdSAndreas Gohr
1963dea4ebcSAndreas Gohr  // we don't want a purge URL to be digged
1973dea4ebcSAndreas Gohr  if($_REQUEST['purge'] && $_SERVER['HTTP_REFERER']) unset($_REQUEST['purge']);
1983dea4ebcSAndreas Gohr
199ed7b5f09Sandi  // disable gzip if not available
2008a447e5cSAndreas Gohr  if($conf['compression'] == 'bz2' && !function_exists('bzopen')){
201fe893490SAndreas Gohr    $conf['compression'] = 'gz';
202501252a5SAndreas Gohr  }
203fe893490SAndreas Gohr  if($conf['compression'] == 'gz' && !function_exists('gzopen')){
204501252a5SAndreas Gohr    $conf['compression'] = 0;
205ed7b5f09Sandi  }
206ed7b5f09Sandi
207e656dcd4SAndreas Gohr  // fix dateformat for upgraders
208e656dcd4SAndreas Gohr  if(strpos($conf['dformat'],'%') === false){
209e656dcd4SAndreas Gohr    $conf['dformat'] = '%Y/%m/%d %H:%M';
210e656dcd4SAndreas Gohr  }
211e656dcd4SAndreas Gohr
2121ca31cfeSAndreas Gohr  // precalculate file creation modes
2131ca31cfeSAndreas Gohr  init_creationmodes();
214ed7b5f09Sandi
2153dc3a5f1Sandi  // make real paths and check them
21698407a7aSandi  init_paths();
2177367b368SAndreas Gohr  init_files();
218ed7b5f09Sandi
2198c4f28e8Sjan  // automatic upgrade to script versions of certain files
220e7cb32dcSAndreas Gohr  scriptify(DOKU_CONF.'users.auth');
221e7cb32dcSAndreas Gohr  scriptify(DOKU_CONF.'acl.auth');
222f62ea8a1Sandi
223f62ea8a1Sandi
224f62ea8a1Sandi/**
22598407a7aSandi * Checks paths from config file
22698407a7aSandi */
22798407a7aSandifunction init_paths(){
22898407a7aSandi  global $conf;
22998407a7aSandi
23098407a7aSandi  $paths = array('datadir'   => 'pages',
23198407a7aSandi                 'olddir'    => 'attic',
23298407a7aSandi                 'mediadir'  => 'media',
23398407a7aSandi                 'metadir'   => 'meta',
23498407a7aSandi                 'cachedir'  => 'cache',
235579b0f7eSTNHarris                 'indexdir'  => 'index',
236de33a58fSMichael Klier                 'lockdir'   => 'locks',
237de33a58fSMichael Klier                 'tmpdir'    => 'tmp');
23898407a7aSandi
23998407a7aSandi  foreach($paths as $c => $p){
240bb4866bdSchris    if(empty($conf[$c]))  $conf[$c] = $conf['savedir'].'/'.$p;
24198407a7aSandi    $conf[$c]             = init_path($conf[$c]);
2421983acc2SGuy Brand    if(empty($conf[$c]))  nice_die("The $c ('$p') does not exist, isn't accessible or writable.
24369dc3177SAndreas Gohr                               You should check your config and permission settings.
24469dc3177SAndreas Gohr                               Or maybe you want to <a href=\"install.php\">run the
24569dc3177SAndreas Gohr                               installer</a>?");
24698407a7aSandi  }
24771726d78SBen Coburn
24871726d78SBen Coburn  // path to old changelog only needed for upgrading
24971726d78SBen Coburn  $conf['changelog_old'] = init_path((isset($conf['changelog']))?($conf['changelog']):($conf['savedir'].'/changes.log'));
25071726d78SBen Coburn  if ($conf['changelog_old']=='') { unset($conf['changelog_old']); }
25171726d78SBen Coburn  // hardcoded changelog because it is now a cache that lives in meta
25271726d78SBen Coburn  $conf['changelog'] = $conf['metadir'].'/_dokuwiki.changes';
25399c8d7f2Smichael  $conf['media_changelog'] = $conf['metadir'].'/_media.changes';
25498407a7aSandi}
25598407a7aSandi
25698407a7aSandi/**
2570d8850c4SAndreas Gohr * Checks the existance of certain files and creates them if missing.
2587367b368SAndreas Gohr */
2597367b368SAndreas Gohrfunction init_files(){
2607367b368SAndreas Gohr  global $conf;
2610d8850c4SAndreas Gohr
262579b0f7eSTNHarris  $files = array( $conf['indexdir'].'/page.idx');
2637367b368SAndreas Gohr
2647367b368SAndreas Gohr  foreach($files as $file){
2657367b368SAndreas Gohr    if(!@file_exists($file)){
2660d8850c4SAndreas Gohr      $fh = @fopen($file,'a');
2670d8850c4SAndreas Gohr      if($fh){
2687367b368SAndreas Gohr        fclose($fh);
2691ca31cfeSAndreas Gohr        if($conf['fperm']) chmod($file, $conf['fperm']);
2700d8850c4SAndreas Gohr      }else{
2713816dcbcSAndreas Gohr        nice_die("$file is not writable. Check your permissions settings!");
2720d8850c4SAndreas Gohr      }
2737367b368SAndreas Gohr    }
2747367b368SAndreas Gohr  }
2757367b368SAndreas Gohr}
2767367b368SAndreas Gohr
2777367b368SAndreas Gohr/**
2780d8850c4SAndreas Gohr * Returns absolute path
279f62ea8a1Sandi *
2800d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC.
2810d8850c4SAndreas Gohr * Check for accessability on directories as well.
2820d8850c4SAndreas Gohr *
2830d8850c4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
284f62ea8a1Sandi */
285f62ea8a1Sandifunction init_path($path){
2860d8850c4SAndreas Gohr  // check existance
28700976812SAndreas Gohr  $p = fullpath($path);
2880d8850c4SAndreas Gohr  if(!@file_exists($p)){
28900976812SAndreas Gohr    $p = fullpath(DOKU_INC.$path);
2900d8850c4SAndreas Gohr    if(!@file_exists($p)){
2918fc4e739Sandi      return '';
292f62ea8a1Sandi    }
2930d8850c4SAndreas Gohr  }
2940d8850c4SAndreas Gohr
2950d8850c4SAndreas Gohr  // check writability
2960d8850c4SAndreas Gohr  if(!@is_writable($p)){
2970d8850c4SAndreas Gohr    return '';
2980d8850c4SAndreas Gohr  }
2990d8850c4SAndreas Gohr
3000d8850c4SAndreas Gohr  // check accessability (execute bit) for directories
3010d8850c4SAndreas Gohr  if(@is_dir($p) && !@file_exists("$p/.")){
3020d8850c4SAndreas Gohr    return '';
3030d8850c4SAndreas Gohr  }
3040d8850c4SAndreas Gohr
3050d8850c4SAndreas Gohr  return $p;
3060d8850c4SAndreas Gohr}
3078c4f28e8Sjan
308ed7b5f09Sandi/**
3091ca31cfeSAndreas Gohr * Sets the internal config values fperm and dperm which, when set,
3101ca31cfeSAndreas Gohr * will be used to change the permission of a newly created dir or
3111ca31cfeSAndreas Gohr * file with chmod. Considers the influence of the system's umask
3121ca31cfeSAndreas Gohr * setting the values only if needed.
3131ca31cfeSAndreas Gohr */
3141ca31cfeSAndreas Gohrfunction init_creationmodes(){
3151ca31cfeSAndreas Gohr  global $conf;
3161ca31cfeSAndreas Gohr
3171ca31cfeSAndreas Gohr  // Legacy support for old umask/dmask scheme
3181ca31cfeSAndreas Gohr  unset($conf['dmask']);
3191ca31cfeSAndreas Gohr  unset($conf['fmask']);
3201ca31cfeSAndreas Gohr  unset($conf['umask']);
3211ca31cfeSAndreas Gohr  unset($conf['fperm']);
3221ca31cfeSAndreas Gohr  unset($conf['dperm']);
3231ca31cfeSAndreas Gohr
3249f3cdec3SAndreas Gohr  // get system umask, fallback to 0 if none available
3259f3cdec3SAndreas Gohr  $umask = @umask();
3269f3cdec3SAndreas Gohr  if(!$umask) $umask = 0000;
3271ca31cfeSAndreas Gohr
3281ca31cfeSAndreas Gohr  // check what is set automatically by the system on file creation
3291ca31cfeSAndreas Gohr  // and set the fperm param if it's not what we want
3301ca31cfeSAndreas Gohr  $auto_fmode = 0666 & ~$umask;
3311ca31cfeSAndreas Gohr  if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode'];
3321ca31cfeSAndreas Gohr
3331ca31cfeSAndreas Gohr  // check what is set automatically by the system on file creation
3341ca31cfeSAndreas Gohr  // and set the dperm param if it's not what we want
3351ca31cfeSAndreas Gohr  $auto_dmode = $conf['dmode'] & ~$umask;
3361ca31cfeSAndreas Gohr  if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode'];
3371ca31cfeSAndreas Gohr}
3381ca31cfeSAndreas Gohr
3391ca31cfeSAndreas Gohr/**
340ed7b5f09Sandi * remove magic quotes recursivly
341ed7b5f09Sandi *
342ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
343ed7b5f09Sandi */
344ed7b5f09Sandifunction remove_magic_quotes(&$array) {
345ed7b5f09Sandi  foreach (array_keys($array) as $key) {
34681c54349SAndreas Gohr      // handle magic quotes in keynames (breaks order)
34781c54349SAndreas Gohr      $sk = stripslashes($key);
34881c54349SAndreas Gohr      if($sk != $key){
34981c54349SAndreas Gohr          $array[$sk] = $array[$key];
35081c54349SAndreas Gohr          unset($array[$key]);
35181c54349SAndreas Gohr          $key = $sk;
35281c54349SAndreas Gohr      }
35381c54349SAndreas Gohr
35481c54349SAndreas Gohr      // do recursion if needed
355ed7b5f09Sandi      if (is_array($array[$key])) {
356ed7b5f09Sandi          remove_magic_quotes($array[$key]);
357ed7b5f09Sandi      }else {
358ed7b5f09Sandi          $array[$key] = stripslashes($array[$key]);
359ed7b5f09Sandi      }
360ed7b5f09Sandi  }
361ed7b5f09Sandi}
362ed7b5f09Sandi
363ed7b5f09Sandi/**
364ed7b5f09Sandi * Returns the full absolute URL to the directory where
365ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash)
366ed7b5f09Sandi *
367ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
368ed7b5f09Sandi */
3694b1a4e04SAndreas Gohrfunction getBaseURL($abs=null){
370ed7b5f09Sandi  global $conf;
371ed7b5f09Sandi  //if canonical url enabled always return absolute
3724b1a4e04SAndreas Gohr  if(is_null($abs)) $abs = $conf['canonical'];
373ed7b5f09Sandi
37492b83b77Sandi  if($conf['basedir']){
37546c73e01SChris Smith    $dir = $conf['basedir'];
37689aa05dbSAndreas Gohr  }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){
37746c73e01SChris Smith    $dir = dirname($_SERVER['SCRIPT_NAME']);
37889aa05dbSAndreas Gohr  }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){
37946c73e01SChris Smith    $dir = dirname($_SERVER['PHP_SELF']);
380093ec9e4Sandi  }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
381093ec9e4Sandi    $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
382093ec9e4Sandi                         $_SERVER['SCRIPT_FILENAME']);
38346c73e01SChris Smith    $dir = dirname('/'.$dir);
38492b83b77Sandi  }else{
38546c73e01SChris Smith    $dir = '.'; //probably wrong
38692b83b77Sandi  }
387ed7b5f09Sandi
38846c73e01SChris Smith  $dir = str_replace('\\','/',$dir);             // bugfix for weird WIN behaviour
38946c73e01SChris Smith  $dir = preg_replace('#//+#','/',"/$dir/");     // ensure leading and trailing slashes
390ed7b5f09Sandi
391f62ea8a1Sandi  //handle script in lib/exe dir
392f62ea8a1Sandi  $dir = preg_replace('!lib/exe/$!','',$dir);
393f62ea8a1Sandi
394488d5fa0SMichael Klier chi@chimeric.de  //handle script in lib/plugins dir
395488d5fa0SMichael Klier chi@chimeric.de  $dir = preg_replace('!lib/plugins/.*$!','',$dir);
396488d5fa0SMichael Klier chi@chimeric.de
397ed7b5f09Sandi  //finish here for relative URLs
398ed7b5f09Sandi  if(!$abs) return $dir;
399ed7b5f09Sandi
40046c73e01SChris Smith  //use config option if available, trim any slash from end of baseurl to avoid multiple consecutive slashes in the path
40146c73e01SChris Smith  if($conf['baseurl']) return rtrim($conf['baseurl'],'/').$dir;
402ef7b3ecdSAndreas Gohr
403e82e3526SAndreas Gohr  //split hostheader into host and port
404e82e3526SAndreas Gohr  list($host,$port) = explode(':',$_SERVER['HTTP_HOST']);
405e82e3526SAndreas Gohr  if(!$port)  $port = $_SERVER['SERVER_PORT'];
406e82e3526SAndreas Gohr  if(!$port)  $port = 80;
407ed7b5f09Sandi
408f5c6743cSAndreas Gohr  if(!is_ssl()){
409ed7b5f09Sandi    $proto = 'http://';
410e82e3526SAndreas Gohr    if ($port == '80') {
411ed7b5f09Sandi      $port='';
412ed7b5f09Sandi    }
413ed7b5f09Sandi  }else{
414ed7b5f09Sandi    $proto = 'https://';
415e82e3526SAndreas Gohr    if ($port == '443') {
416ed7b5f09Sandi      $port='';
417ed7b5f09Sandi    }
418ed7b5f09Sandi  }
419ed7b5f09Sandi
420e82e3526SAndreas Gohr  if($port) $port = ':'.$port;
421e82e3526SAndreas Gohr
422ed7b5f09Sandi  return $proto.$host.$port.$dir;
423ed7b5f09Sandi}
424ed7b5f09Sandi
425b000c6d4Sandi/**
426f5c6743cSAndreas Gohr * Check if accessed via HTTPS
427f5c6743cSAndreas Gohr *
428f5c6743cSAndreas Gohr * Apache leaves ,$_SERVER['HTTPS'] empty when not available, IIS sets it to 'off'.
429f5c6743cSAndreas Gohr * 'false' and 'disabled' are just guessing
430f5c6743cSAndreas Gohr *
431f5c6743cSAndreas Gohr * @returns bool true when SSL is active
432f5c6743cSAndreas Gohr */
433f5c6743cSAndreas Gohrfunction is_ssl(){
434f5c6743cSAndreas Gohr    if (preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){
435f5c6743cSAndreas Gohr        return false;
436f5c6743cSAndreas Gohr    }else{
437f5c6743cSAndreas Gohr        return true;
438f5c6743cSAndreas Gohr    }
439f5c6743cSAndreas Gohr}
440f5c6743cSAndreas Gohr
441f5c6743cSAndreas Gohr/**
442b000c6d4Sandi * Append a PHP extension to a given file and adds an exit call
443b000c6d4Sandi *
444b000c6d4Sandi * This is used to migrate some old configfiles. An added PHP extension
445b000c6d4Sandi * ensures the contents are not shown to webusers even if .htaccess files
446b000c6d4Sandi * do not work
447b000c6d4Sandi *
448b000c6d4Sandi * @author Jan Decaluwe <jan@jandecaluwe.com>
449b000c6d4Sandi */
4508c4f28e8Sjanfunction scriptify($file) {
4518c4f28e8Sjan  // checks
4528c4f28e8Sjan  if (!is_readable($file)) {
4538c4f28e8Sjan    return;
4548c4f28e8Sjan  }
4558c4f28e8Sjan  $fn = $file.'.php';
4568c4f28e8Sjan  if (@file_exists($fn)) {
4578c4f28e8Sjan    return;
4588c4f28e8Sjan  }
4598c4f28e8Sjan  $fh = fopen($fn, 'w');
4608c4f28e8Sjan  if (!$fh) {
4613816dcbcSAndreas Gohr    nice_die($fn.' is not writable. Check your permission settings!');
4628c4f28e8Sjan  }
4638c4f28e8Sjan  // write php exit hack first
4648c4f28e8Sjan  fwrite($fh, "# $fn\n");
4658c4f28e8Sjan  fwrite($fh, '# <?php exit()?>'."\n");
4668c4f28e8Sjan  fwrite($fh, "# Don't modify the lines above\n");
4678c4f28e8Sjan  fwrite($fh, "#\n");
4688c4f28e8Sjan  // copy existing lines
4698c4f28e8Sjan  $lines = file($file);
4708c4f28e8Sjan  foreach ($lines as $line){
4718c4f28e8Sjan    fwrite($fh, $line);
4728c4f28e8Sjan  }
4733ba793e2Sandi  fclose($fh);
474b000c6d4Sandi  //try to rename the old file
4753aee4c27SAndreas Gohr  io_rename($file,"$file.old");
4768c4f28e8Sjan}
4778c4f28e8Sjan
4783816dcbcSAndreas Gohr/**
4793816dcbcSAndreas Gohr * print a nice message even if no styles are loaded yet.
4803816dcbcSAndreas Gohr */
4813816dcbcSAndreas Gohrfunction nice_die($msg){
4823816dcbcSAndreas Gohr  echo<<<EOT
4833816dcbcSAndreas Gohr  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
4843816dcbcSAndreas Gohr   "http://www.w3.org/TR/html4/loose.dtd">
4853816dcbcSAndreas Gohr  <html>
4863816dcbcSAndreas Gohr    <head><title>DokuWiki Setup Error</title></head>
4873816dcbcSAndreas Gohr    <body style="font-family: Arial, sans-serif">
4883816dcbcSAndreas Gohr      <div style="width:60%; margin: auto; background-color: #fcc;
4893816dcbcSAndreas Gohr                  border: 1px solid #faa; padding: 0.5em 1em;">
4903816dcbcSAndreas Gohr      <h1 style="font-size: 120%">DokuWiki Setup Error</h1>
4913816dcbcSAndreas Gohr      <p>$msg</p>
4923816dcbcSAndreas Gohr      </div>
4933816dcbcSAndreas Gohr    </body>
4943816dcbcSAndreas Gohr  </html>
4953816dcbcSAndreas GohrEOT;
4963816dcbcSAndreas Gohr  exit;
4973816dcbcSAndreas Gohr}
4983816dcbcSAndreas Gohr
499ed7b5f09Sandi
50000976812SAndreas Gohr/**
50100976812SAndreas Gohr * A realpath() replacement
50200976812SAndreas Gohr *
50300976812SAndreas Gohr * This function behaves similar to PHP's realpath() but does not resolve
50400976812SAndreas Gohr * symlinks or accesses upper directories
50500976812SAndreas Gohr *
5064761d30cSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
50700976812SAndreas Gohr * @author <richpageau at yahoo dot co dot uk>
50800976812SAndreas Gohr * @link   http://de3.php.net/manual/en/function.realpath.php#75992
50900976812SAndreas Gohr */
510b328697dSAndreas Gohrfunction fullpath($path,$exists=false){
5114761d30cSAndreas Gohr    static $run = 0;
5124761d30cSAndreas Gohr    $root  = '';
513*f0a201c5SChris Smith    $iswin = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' || @$GLOBALS['DOKU_UNITTEST_ASSUME_WINDOWS']);
51400976812SAndreas Gohr
5154761d30cSAndreas Gohr    // find the (indestructable) root of the path - keeps windows stuff intact
5164761d30cSAndreas Gohr    if($path{0} == '/'){
5174761d30cSAndreas Gohr        $root = '/';
5184761d30cSAndreas Gohr    }elseif($iswin){
5194761d30cSAndreas Gohr        // match drive letter and UNC paths
5204761d30cSAndreas Gohr        if(preg_match('!^([a-zA-z]:)(.*)!',$path,$match)){
5214761d30cSAndreas Gohr            $root = $match[1];
5224761d30cSAndreas Gohr            $path = $match[2];
5234761d30cSAndreas Gohr        }else if(preg_match('!^(\\\\\\\\[^\\\\/]+\\\\[^\\\\/]+[\\\\/])(.*)!',$path,$match)){
5244761d30cSAndreas Gohr            $root = $match[1];
5254761d30cSAndreas Gohr            $path = $match[2];
52600976812SAndreas Gohr        }
5274761d30cSAndreas Gohr    }
5284761d30cSAndreas Gohr    $path = str_replace('\\','/',$path);
5294761d30cSAndreas Gohr
5304761d30cSAndreas Gohr    // if the given path wasn't absolute already, prepend the script path and retry
5314761d30cSAndreas Gohr    if(!$root){
5324761d30cSAndreas Gohr        $base = dirname($_SERVER['SCRIPT_FILENAME']);
5334761d30cSAndreas Gohr        $path = $base.'/'.$path;
5344761d30cSAndreas Gohr        if($run == 0){ // avoid endless recursion when base isn't absolute for some reason
5354761d30cSAndreas Gohr            $run++;
536b328697dSAndreas Gohr            return fullpath($path,$exists);
5374761d30cSAndreas Gohr        }
5384761d30cSAndreas Gohr    }
5394761d30cSAndreas Gohr    $run = 0;
54000976812SAndreas Gohr
54100976812SAndreas Gohr    // canonicalize
54200976812SAndreas Gohr    $path=explode('/', $path);
54300976812SAndreas Gohr    $newpath=array();
544ef38bfe8SAndreas Gohr    foreach($path as $p) {
545ef38bfe8SAndreas Gohr        if ($p === '' || $p === '.') continue;
546ef38bfe8SAndreas Gohr           if ($p==='..') {
54700976812SAndreas Gohr              array_pop($newpath);
54800976812SAndreas Gohr              continue;
54900976812SAndreas Gohr        }
550ef38bfe8SAndreas Gohr        array_push($newpath, $p);
55100976812SAndreas Gohr    }
5524761d30cSAndreas Gohr    $finalpath = $root.implode('/', $newpath);
55300976812SAndreas Gohr
554b328697dSAndreas Gohr    // check for existance when needed (except when unit testing)
555b328697dSAndreas Gohr    if($exists && !defined('DOKU_UNITTEST') && !@file_exists($finalpath)) {
5564761d30cSAndreas Gohr        return false;
55700976812SAndreas Gohr    }
5584761d30cSAndreas Gohr    return $finalpath;
55900976812SAndreas Gohr}
56000976812SAndreas Gohr
56100976812SAndreas Gohr
56200976812SAndreas Gohr
563340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
564