xref: /dokuwiki/inc/init.php (revision 99c8d7f21203dc68bb191d77e01c2f8a6f0cf5a0)
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
1448beefecSAndreas Gohr  @include(fullpath(dirname(__FILE__)).'/preload.php');
1548beefecSAndreas Gohr
16ed7b5f09Sandi  // define the include path
1700976812SAndreas Gohr  if(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../').'/');
18ad15db82Sandi
19e7cb32dcSAndreas Gohr  // define config path (packagers may want to change this to /etc/dokuwiki/)
20b7551a6dSEsther Brunner  if(!defined('DOKU_CONF')) define('DOKU_CONF',DOKU_INC.'conf/');
21e7cb32dcSAndreas Gohr
22bad905f1SBen Coburn  // check for error reporting override or set error reporting to sane values
23d8186216SBen Coburn  if (!defined('DOKU_E_LEVEL') && @file_exists(DOKU_CONF.'report_e_all')) {
24bad905f1SBen Coburn    define('DOKU_E_LEVEL', E_ALL);
25bad905f1SBen Coburn  }
26fc80ed59SAndreas Gohr  if (!defined('DOKU_E_LEVEL')) {
27fc80ed59SAndreas Gohr    if(defined('E_DEPRECATED')){ // since php 5.3
28fc80ed59SAndreas Gohr      error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
29fc80ed59SAndreas Gohr    }else{
30fc80ed59SAndreas Gohr      error_reporting(E_ALL ^ E_NOTICE);
31fc80ed59SAndreas Gohr    }
32fc80ed59SAndreas Gohr  } else {
33fc80ed59SAndreas Gohr    error_reporting(DOKU_E_LEVEL);
34fc80ed59SAndreas Gohr  }
35c53ea5f2Sandi
3650602150SBen Coburn  // init memory caches
37bc3e97beSAndreas Gohr  global $cache_revinfo;  $cache_revinfo = array();
38bc3e97beSAndreas Gohr  global $cache_wikifn;   $cache_wikifn = array();
39a424cd8eSchris  global $cache_cleanid;  $cache_cleanid = array();
40a424cd8eSchris  global $cache_authname; $cache_authname = array();
410a7e3bceSchris  global $cache_metadata; $cache_metadata = array();
4250602150SBen Coburn
434724a577Sandi  //prepare config array()
44ee20e7d1Sandi  global $conf;
454724a577Sandi  $conf = array();
464724a577Sandi
47ad15db82Sandi  // load the config file(s)
48e7cb32dcSAndreas Gohr  require_once(DOKU_CONF.'dokuwiki.php');
490a6ead41SAndreas Gohr  if(@file_exists(DOKU_CONF.'local.php')){
500a6ead41SAndreas Gohr    require_once(DOKU_CONF.'local.php');
510a6ead41SAndreas Gohr  }
52ad15db82Sandi
53ad15db82Sandi  //prepare language array
54ee20e7d1Sandi  global $lang;
55ad15db82Sandi  $lang = array();
56ed7b5f09Sandi
5716521111Sandi  //load the language files
58bc3b6aecSandi  require_once(DOKU_INC.'inc/lang/en/lang.php');
59f949a01cSAndreas Gohr  if ( $conf['lang'] && $conf['lang'] != 'en' ) {
60bc3b6aecSandi    require_once(DOKU_INC.'inc/lang/'.$conf['lang'].'/lang.php');
61fc1c55b1Shfuecks  }
6216521111Sandi
63066fee30SAndreas Gohr  //prepare license array()
64066fee30SAndreas Gohr  global $license;
65066fee30SAndreas Gohr  $license = array();
66066fee30SAndreas Gohr
67066fee30SAndreas Gohr  // load the license file(s)
68066fee30SAndreas Gohr  require_once(DOKU_CONF.'license.php');
69066fee30SAndreas Gohr  if(@file_exists(DOKU_CONF.'license.php')){
70066fee30SAndreas Gohr    require_once(DOKU_CONF.'license.php');
71066fee30SAndreas Gohr  }
72066fee30SAndreas Gohr
73ed7b5f09Sandi  // define baseURL
744b1a4e04SAndreas Gohr  if(!defined('DOKU_REL')) define('DOKU_REL',getBaseURL(false));
75ed7b5f09Sandi  if(!defined('DOKU_URL')) define('DOKU_URL',getBaseURL(true));
764b1a4e04SAndreas Gohr  if(!defined('DOKU_BASE')){
774b1a4e04SAndreas Gohr    if($conf['canonical']){
784b1a4e04SAndreas Gohr      define('DOKU_BASE',DOKU_URL);
794b1a4e04SAndreas Gohr    }else{
804b1a4e04SAndreas Gohr      define('DOKU_BASE',DOKU_REL);
814b1a4e04SAndreas Gohr    }
824b1a4e04SAndreas Gohr  }
834b1a4e04SAndreas Gohr
84b8595a66SAndreas Gohr  // define whitespace
85b8595a66SAndreas Gohr  if(!defined('DOKU_LF')) define ('DOKU_LF',"\n");
86b8595a66SAndreas Gohr  if(!defined('DOKU_TAB')) define ('DOKU_TAB',"\t");
87ed7b5f09Sandi
88e71ce681SAndreas Gohr  // define cookie and session id
898b11f0a5SAndreas Gohr  if (!defined('DOKU_COOKIE')) define('DOKU_COOKIE', 'DW'.md5(DOKU_REL));
90e71ce681SAndreas Gohr
91ee20e7d1Sandi  // define Plugin dir
92f62ea8a1Sandi  if(!defined('DOKU_PLUGIN'))  define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
93ee20e7d1Sandi
94ed7b5f09Sandi  // define main script
95ed7b5f09Sandi  if(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php');
96ed7b5f09Sandi
976b13307fSandi  // define Template baseURL
986b13307fSandi  if(!defined('DOKU_TPL')) define('DOKU_TPL',
99f62ea8a1Sandi                                  DOKU_BASE.'lib/tpl/'.$conf['template'].'/');
1006b13307fSandi
10178a6aeb1SAndreas Gohr  // define real Template directory
10278a6aeb1SAndreas Gohr  if(!defined('DOKU_TPLINC')) define('DOKU_TPLINC',
10378a6aeb1SAndreas Gohr                                  DOKU_INC.'lib/tpl/'.$conf['template'].'/');
10478a6aeb1SAndreas Gohr
105ed7b5f09Sandi  // make session rewrites XHTML compliant
1063fc74836Sandi  @ini_set('arg_separator.output', '&amp;');
107ed7b5f09Sandi
108d7e6bba9SAndreas Gohr  // make sure global zlib does not interfere FS#1132
109d7e6bba9SAndreas Gohr  @ini_set('zlib.output_compression', 'off');
110d7e6bba9SAndreas Gohr
1116deb5405SAndreas Gohr  // increase PCRE backtrack limit
1126deb5405SAndreas Gohr  @ini_set('pcre.backtrack_limit', '20971520');
1136deb5405SAndreas Gohr
1143138b5c7SAndreas Gohr  // enable gzip compression
1153138b5c7SAndreas Gohr  if ($conf['gzip_output'] &&
1163138b5c7SAndreas Gohr      !defined('DOKU_DISABLE_GZIP_OUTPUT') &&
1173138b5c7SAndreas Gohr      function_exists('ob_gzhandler') &&
1183138b5c7SAndreas Gohr      preg_match('/gzip|deflate/', $_SERVER['HTTP_ACCEPT_ENCODING'])) {
1193138b5c7SAndreas Gohr    ob_start('ob_gzhandler');
1203138b5c7SAndreas Gohr  }
1213138b5c7SAndreas Gohr
122ed7b5f09Sandi  // init session
1236534245aSAndreas Gohr  if (!headers_sent() && !defined('NOSESSION')){
124ed7b5f09Sandi    session_name("DokuWiki");
125f5c6743cSAndreas Gohr    if (version_compare(PHP_VERSION, '5.2.0', '>')) {
126f5c6743cSAndreas Gohr      session_set_cookie_params(0,DOKU_REL,'',($conf['securecookie'] && is_ssl()),true);
127f5c6743cSAndreas Gohr    }else{
128f5c6743cSAndreas Gohr      session_set_cookie_params(0,DOKU_REL,'',($conf['securecookie'] && is_ssl()));
129f5c6743cSAndreas Gohr    }
130bad31ae9SAndreas Gohr    session_start();
13114a122deSAndreas Gohr
13214a122deSAndreas Gohr    // load left over messages
13314a122deSAndreas Gohr    if(isset($_SESSION[DOKU_COOKIE]['msg'])){
13414a122deSAndreas Gohr      $MSG = $_SESSION[DOKU_COOKIE]['msg'];
13514a122deSAndreas Gohr      unset($_SESSION[DOKU_COOKIE]['msg']);
13614a122deSAndreas Gohr    }
137bad31ae9SAndreas Gohr  }
138ed7b5f09Sandi
139ed7b5f09Sandi  // kill magic quotes
140e55eb89cSAndreas Gohr  if (get_magic_quotes_gpc() && !defined('MAGIC_QUOTES_STRIPPED')) {
141ed7b5f09Sandi    if (!empty($_GET))    remove_magic_quotes($_GET);
142ed7b5f09Sandi    if (!empty($_POST))   remove_magic_quotes($_POST);
143ed7b5f09Sandi    if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE);
144ed7b5f09Sandi    if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST);
1453fc74836Sandi    @ini_set('magic_quotes_gpc', 0);
146e55eb89cSAndreas Gohr    define('MAGIC_QUOTES_STRIPPED',1);
147ed7b5f09Sandi  }
1483fc74836Sandi  @set_magic_quotes_runtime(0);
1493fc74836Sandi  @ini_set('magic_quotes_sybase',0);
150ed7b5f09Sandi
151a1637ffdSAndreas Gohr  // don't let cookies ever interfere with request vars
152a1637ffdSAndreas Gohr  $_REQUEST = array_merge($_GET,$_POST);
153a1637ffdSAndreas Gohr
1543dea4ebcSAndreas Gohr  // we don't want a purge URL to be digged
1553dea4ebcSAndreas Gohr  if($_REQUEST['purge'] && $_SERVER['HTTP_REFERER']) unset($_REQUEST['purge']);
1563dea4ebcSAndreas Gohr
157ed7b5f09Sandi  // disable gzip if not available
1588a447e5cSAndreas Gohr  if($conf['compression'] == 'bz2' && !function_exists('bzopen')){
159fe893490SAndreas Gohr    $conf['compression'] = 'gz';
160501252a5SAndreas Gohr  }
161fe893490SAndreas Gohr  if($conf['compression'] == 'gz' && !function_exists('gzopen')){
162501252a5SAndreas Gohr    $conf['compression'] = 0;
163ed7b5f09Sandi  }
164ed7b5f09Sandi
165e656dcd4SAndreas Gohr  // fix dateformat for upgraders
166e656dcd4SAndreas Gohr  if(strpos($conf['dformat'],'%') === false){
167e656dcd4SAndreas Gohr    $conf['dformat'] = '%Y/%m/%d %H:%M';
168e656dcd4SAndreas Gohr  }
169e656dcd4SAndreas Gohr
1701ca31cfeSAndreas Gohr  // precalculate file creation modes
1711ca31cfeSAndreas Gohr  init_creationmodes();
172ed7b5f09Sandi
1733dc3a5f1Sandi  // make real paths and check them
17498407a7aSandi  init_paths();
1757367b368SAndreas Gohr  init_files();
176ed7b5f09Sandi
1778c4f28e8Sjan  // automatic upgrade to script versions of certain files
178e7cb32dcSAndreas Gohr  scriptify(DOKU_CONF.'users.auth');
179e7cb32dcSAndreas Gohr  scriptify(DOKU_CONF.'acl.auth');
180f62ea8a1Sandi
181f62ea8a1Sandi
182f62ea8a1Sandi/**
18398407a7aSandi * Checks paths from config file
18498407a7aSandi */
18598407a7aSandifunction init_paths(){
18698407a7aSandi  global $conf;
18798407a7aSandi
18898407a7aSandi  $paths = array('datadir'   => 'pages',
18998407a7aSandi                 'olddir'    => 'attic',
19098407a7aSandi                 'mediadir'  => 'media',
19198407a7aSandi                 'metadir'   => 'meta',
19298407a7aSandi                 'cachedir'  => 'cache',
193579b0f7eSTNHarris                 'indexdir'  => 'index',
194de33a58fSMichael Klier                 'lockdir'   => 'locks',
195de33a58fSMichael Klier                 'tmpdir'    => 'tmp');
19698407a7aSandi
19798407a7aSandi  foreach($paths as $c => $p){
198bb4866bdSchris    if(empty($conf[$c]))  $conf[$c] = $conf['savedir'].'/'.$p;
19998407a7aSandi    $conf[$c]             = init_path($conf[$c]);
2001983acc2SGuy Brand    if(empty($conf[$c]))  nice_die("The $c ('$p') does not exist, isn't accessible or writable.
20169dc3177SAndreas Gohr                               You should check your config and permission settings.
20269dc3177SAndreas Gohr                               Or maybe you want to <a href=\"install.php\">run the
20369dc3177SAndreas Gohr                               installer</a>?");
20498407a7aSandi  }
20571726d78SBen Coburn
20671726d78SBen Coburn  // path to old changelog only needed for upgrading
20771726d78SBen Coburn  $conf['changelog_old'] = init_path((isset($conf['changelog']))?($conf['changelog']):($conf['savedir'].'/changes.log'));
20871726d78SBen Coburn  if ($conf['changelog_old']=='') { unset($conf['changelog_old']); }
20971726d78SBen Coburn  // hardcoded changelog because it is now a cache that lives in meta
21071726d78SBen Coburn  $conf['changelog'] = $conf['metadir'].'/_dokuwiki.changes';
211*99c8d7f2Smichael  $conf['media_changelog'] = $conf['metadir'].'/_media.changes';
21298407a7aSandi}
21398407a7aSandi
21498407a7aSandi/**
2150d8850c4SAndreas Gohr * Checks the existance of certain files and creates them if missing.
2167367b368SAndreas Gohr */
2177367b368SAndreas Gohrfunction init_files(){
2187367b368SAndreas Gohr  global $conf;
2190d8850c4SAndreas Gohr
220579b0f7eSTNHarris  $files = array( $conf['indexdir'].'/page.idx');
2217367b368SAndreas Gohr
2227367b368SAndreas Gohr  foreach($files as $file){
2237367b368SAndreas Gohr    if(!@file_exists($file)){
2240d8850c4SAndreas Gohr      $fh = @fopen($file,'a');
2250d8850c4SAndreas Gohr      if($fh){
2267367b368SAndreas Gohr        fclose($fh);
2271ca31cfeSAndreas Gohr        if($conf['fperm']) chmod($file, $conf['fperm']);
2280d8850c4SAndreas Gohr      }else{
2293816dcbcSAndreas Gohr        nice_die("$file is not writable. Check your permissions settings!");
2300d8850c4SAndreas Gohr      }
2317367b368SAndreas Gohr    }
2327367b368SAndreas Gohr  }
2337367b368SAndreas Gohr}
2347367b368SAndreas Gohr
2357367b368SAndreas Gohr/**
2360d8850c4SAndreas Gohr * Returns absolute path
237f62ea8a1Sandi *
2380d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC.
2390d8850c4SAndreas Gohr * Check for accessability on directories as well.
2400d8850c4SAndreas Gohr *
2410d8850c4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
242f62ea8a1Sandi */
243f62ea8a1Sandifunction init_path($path){
2440d8850c4SAndreas Gohr  // check existance
24500976812SAndreas Gohr  $p = fullpath($path);
2460d8850c4SAndreas Gohr  if(!@file_exists($p)){
24700976812SAndreas Gohr    $p = fullpath(DOKU_INC.$path);
2480d8850c4SAndreas Gohr    if(!@file_exists($p)){
2498fc4e739Sandi      return '';
250f62ea8a1Sandi    }
2510d8850c4SAndreas Gohr  }
2520d8850c4SAndreas Gohr
2530d8850c4SAndreas Gohr  // check writability
2540d8850c4SAndreas Gohr  if(!@is_writable($p)){
2550d8850c4SAndreas Gohr    return '';
2560d8850c4SAndreas Gohr  }
2570d8850c4SAndreas Gohr
2580d8850c4SAndreas Gohr  // check accessability (execute bit) for directories
2590d8850c4SAndreas Gohr  if(@is_dir($p) && !@file_exists("$p/.")){
2600d8850c4SAndreas Gohr    return '';
2610d8850c4SAndreas Gohr  }
2620d8850c4SAndreas Gohr
2630d8850c4SAndreas Gohr  return $p;
2640d8850c4SAndreas Gohr}
2658c4f28e8Sjan
266ed7b5f09Sandi/**
2671ca31cfeSAndreas Gohr * Sets the internal config values fperm and dperm which, when set,
2681ca31cfeSAndreas Gohr * will be used to change the permission of a newly created dir or
2691ca31cfeSAndreas Gohr * file with chmod. Considers the influence of the system's umask
2701ca31cfeSAndreas Gohr * setting the values only if needed.
2711ca31cfeSAndreas Gohr */
2721ca31cfeSAndreas Gohrfunction init_creationmodes(){
2731ca31cfeSAndreas Gohr  global $conf;
2741ca31cfeSAndreas Gohr
2751ca31cfeSAndreas Gohr  // Legacy support for old umask/dmask scheme
2761ca31cfeSAndreas Gohr  unset($conf['dmask']);
2771ca31cfeSAndreas Gohr  unset($conf['fmask']);
2781ca31cfeSAndreas Gohr  unset($conf['umask']);
2791ca31cfeSAndreas Gohr  unset($conf['fperm']);
2801ca31cfeSAndreas Gohr  unset($conf['dperm']);
2811ca31cfeSAndreas Gohr
2829f3cdec3SAndreas Gohr  // get system umask, fallback to 0 if none available
2839f3cdec3SAndreas Gohr  $umask = @umask();
2849f3cdec3SAndreas Gohr  if(!$umask) $umask = 0000;
2851ca31cfeSAndreas Gohr
2861ca31cfeSAndreas Gohr  // check what is set automatically by the system on file creation
2871ca31cfeSAndreas Gohr  // and set the fperm param if it's not what we want
2881ca31cfeSAndreas Gohr  $auto_fmode = 0666 & ~$umask;
2891ca31cfeSAndreas Gohr  if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode'];
2901ca31cfeSAndreas Gohr
2911ca31cfeSAndreas Gohr  // check what is set automatically by the system on file creation
2921ca31cfeSAndreas Gohr  // and set the dperm param if it's not what we want
2931ca31cfeSAndreas Gohr  $auto_dmode = $conf['dmode'] & ~$umask;
2941ca31cfeSAndreas Gohr  if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode'];
2951ca31cfeSAndreas Gohr}
2961ca31cfeSAndreas Gohr
2971ca31cfeSAndreas Gohr/**
298ed7b5f09Sandi * remove magic quotes recursivly
299ed7b5f09Sandi *
300ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
301ed7b5f09Sandi */
302ed7b5f09Sandifunction remove_magic_quotes(&$array) {
303ed7b5f09Sandi  foreach (array_keys($array) as $key) {
30481c54349SAndreas Gohr      // handle magic quotes in keynames (breaks order)
30581c54349SAndreas Gohr      $sk = stripslashes($key);
30681c54349SAndreas Gohr      if($sk != $key){
30781c54349SAndreas Gohr          $array[$sk] = $array[$key];
30881c54349SAndreas Gohr          unset($array[$key]);
30981c54349SAndreas Gohr          $key = $sk;
31081c54349SAndreas Gohr      }
31181c54349SAndreas Gohr
31281c54349SAndreas Gohr      // do recursion if needed
313ed7b5f09Sandi      if (is_array($array[$key])) {
314ed7b5f09Sandi          remove_magic_quotes($array[$key]);
315ed7b5f09Sandi      }else {
316ed7b5f09Sandi          $array[$key] = stripslashes($array[$key]);
317ed7b5f09Sandi      }
318ed7b5f09Sandi  }
319ed7b5f09Sandi}
320ed7b5f09Sandi
321ed7b5f09Sandi/**
322ed7b5f09Sandi * Returns the full absolute URL to the directory where
323ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash)
324ed7b5f09Sandi *
325ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
326ed7b5f09Sandi */
3274b1a4e04SAndreas Gohrfunction getBaseURL($abs=null){
328ed7b5f09Sandi  global $conf;
329ed7b5f09Sandi  //if canonical url enabled always return absolute
3304b1a4e04SAndreas Gohr  if(is_null($abs)) $abs = $conf['canonical'];
331ed7b5f09Sandi
33292b83b77Sandi  if($conf['basedir']){
33346c73e01SChris Smith    $dir = $conf['basedir'];
33489aa05dbSAndreas Gohr  }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){
33546c73e01SChris Smith    $dir = dirname($_SERVER['SCRIPT_NAME']);
33689aa05dbSAndreas Gohr  }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){
33746c73e01SChris Smith    $dir = dirname($_SERVER['PHP_SELF']);
338093ec9e4Sandi  }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
339093ec9e4Sandi    $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
340093ec9e4Sandi                         $_SERVER['SCRIPT_FILENAME']);
34146c73e01SChris Smith    $dir = dirname('/'.$dir);
34292b83b77Sandi  }else{
34346c73e01SChris Smith    $dir = '.'; //probably wrong
34492b83b77Sandi  }
345ed7b5f09Sandi
34646c73e01SChris Smith  $dir = str_replace('\\','/',$dir);             // bugfix for weird WIN behaviour
34746c73e01SChris Smith  $dir = preg_replace('#//+#','/',"/$dir/");     // ensure leading and trailing slashes
348ed7b5f09Sandi
349f62ea8a1Sandi  //handle script in lib/exe dir
350f62ea8a1Sandi  $dir = preg_replace('!lib/exe/$!','',$dir);
351f62ea8a1Sandi
352488d5fa0SMichael Klier chi@chimeric.de  //handle script in lib/plugins dir
353488d5fa0SMichael Klier chi@chimeric.de  $dir = preg_replace('!lib/plugins/.*$!','',$dir);
354488d5fa0SMichael Klier chi@chimeric.de
355ed7b5f09Sandi  //finish here for relative URLs
356ed7b5f09Sandi  if(!$abs) return $dir;
357ed7b5f09Sandi
35846c73e01SChris Smith  //use config option if available, trim any slash from end of baseurl to avoid multiple consecutive slashes in the path
35946c73e01SChris Smith  if($conf['baseurl']) return rtrim($conf['baseurl'],'/').$dir;
360ef7b3ecdSAndreas Gohr
361e82e3526SAndreas Gohr  //split hostheader into host and port
362e82e3526SAndreas Gohr  list($host,$port) = explode(':',$_SERVER['HTTP_HOST']);
363e82e3526SAndreas Gohr  if(!$port)  $port = $_SERVER['SERVER_PORT'];
364e82e3526SAndreas Gohr  if(!$port)  $port = 80;
365ed7b5f09Sandi
366f5c6743cSAndreas Gohr  if(!is_ssl()){
367ed7b5f09Sandi    $proto = 'http://';
368e82e3526SAndreas Gohr    if ($port == '80') {
369ed7b5f09Sandi      $port='';
370ed7b5f09Sandi    }
371ed7b5f09Sandi  }else{
372ed7b5f09Sandi    $proto = 'https://';
373e82e3526SAndreas Gohr    if ($port == '443') {
374ed7b5f09Sandi      $port='';
375ed7b5f09Sandi    }
376ed7b5f09Sandi  }
377ed7b5f09Sandi
378e82e3526SAndreas Gohr  if($port) $port = ':'.$port;
379e82e3526SAndreas Gohr
380ed7b5f09Sandi  return $proto.$host.$port.$dir;
381ed7b5f09Sandi}
382ed7b5f09Sandi
383b000c6d4Sandi/**
384f5c6743cSAndreas Gohr * Check if accessed via HTTPS
385f5c6743cSAndreas Gohr *
386f5c6743cSAndreas Gohr * Apache leaves ,$_SERVER['HTTPS'] empty when not available, IIS sets it to 'off'.
387f5c6743cSAndreas Gohr * 'false' and 'disabled' are just guessing
388f5c6743cSAndreas Gohr *
389f5c6743cSAndreas Gohr * @returns bool true when SSL is active
390f5c6743cSAndreas Gohr */
391f5c6743cSAndreas Gohrfunction is_ssl(){
392f5c6743cSAndreas Gohr    if (preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){
393f5c6743cSAndreas Gohr        return false;
394f5c6743cSAndreas Gohr    }else{
395f5c6743cSAndreas Gohr        return true;
396f5c6743cSAndreas Gohr    }
397f5c6743cSAndreas Gohr}
398f5c6743cSAndreas Gohr
399f5c6743cSAndreas Gohr/**
400b000c6d4Sandi * Append a PHP extension to a given file and adds an exit call
401b000c6d4Sandi *
402b000c6d4Sandi * This is used to migrate some old configfiles. An added PHP extension
403b000c6d4Sandi * ensures the contents are not shown to webusers even if .htaccess files
404b000c6d4Sandi * do not work
405b000c6d4Sandi *
406b000c6d4Sandi * @author Jan Decaluwe <jan@jandecaluwe.com>
407b000c6d4Sandi */
4088c4f28e8Sjanfunction scriptify($file) {
4098c4f28e8Sjan  // checks
4108c4f28e8Sjan  if (!is_readable($file)) {
4118c4f28e8Sjan    return;
4128c4f28e8Sjan  }
4138c4f28e8Sjan  $fn = $file.'.php';
4148c4f28e8Sjan  if (@file_exists($fn)) {
4158c4f28e8Sjan    return;
4168c4f28e8Sjan  }
4178c4f28e8Sjan  $fh = fopen($fn, 'w');
4188c4f28e8Sjan  if (!$fh) {
4193816dcbcSAndreas Gohr    nice_die($fn.' is not writable. Check your permission settings!');
4208c4f28e8Sjan  }
4218c4f28e8Sjan  // write php exit hack first
4228c4f28e8Sjan  fwrite($fh, "# $fn\n");
4238c4f28e8Sjan  fwrite($fh, '# <?php exit()?>'."\n");
4248c4f28e8Sjan  fwrite($fh, "# Don't modify the lines above\n");
4258c4f28e8Sjan  fwrite($fh, "#\n");
4268c4f28e8Sjan  // copy existing lines
4278c4f28e8Sjan  $lines = file($file);
4288c4f28e8Sjan  foreach ($lines as $line){
4298c4f28e8Sjan    fwrite($fh, $line);
4308c4f28e8Sjan  }
4313ba793e2Sandi  fclose($fh);
432b000c6d4Sandi  //try to rename the old file
4333aee4c27SAndreas Gohr  io_rename($file,"$file.old");
4348c4f28e8Sjan}
4358c4f28e8Sjan
4363816dcbcSAndreas Gohr/**
4373816dcbcSAndreas Gohr * print a nice message even if no styles are loaded yet.
4383816dcbcSAndreas Gohr */
4393816dcbcSAndreas Gohrfunction nice_die($msg){
4403816dcbcSAndreas Gohr  echo<<<EOT
4413816dcbcSAndreas Gohr  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
4423816dcbcSAndreas Gohr   "http://www.w3.org/TR/html4/loose.dtd">
4433816dcbcSAndreas Gohr  <html>
4443816dcbcSAndreas Gohr    <head><title>DokuWiki Setup Error</title></head>
4453816dcbcSAndreas Gohr    <body style="font-family: Arial, sans-serif">
4463816dcbcSAndreas Gohr      <div style="width:60%; margin: auto; background-color: #fcc;
4473816dcbcSAndreas Gohr                  border: 1px solid #faa; padding: 0.5em 1em;">
4483816dcbcSAndreas Gohr      <h1 style="font-size: 120%">DokuWiki Setup Error</h1>
4493816dcbcSAndreas Gohr      <p>$msg</p>
4503816dcbcSAndreas Gohr      </div>
4513816dcbcSAndreas Gohr    </body>
4523816dcbcSAndreas Gohr  </html>
4533816dcbcSAndreas GohrEOT;
4543816dcbcSAndreas Gohr  exit;
4553816dcbcSAndreas Gohr}
4563816dcbcSAndreas Gohr
457ed7b5f09Sandi
45800976812SAndreas Gohr/**
45900976812SAndreas Gohr * A realpath() replacement
46000976812SAndreas Gohr *
46100976812SAndreas Gohr * This function behaves similar to PHP's realpath() but does not resolve
46200976812SAndreas Gohr * symlinks or accesses upper directories
46300976812SAndreas Gohr *
4644761d30cSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
46500976812SAndreas Gohr * @author <richpageau at yahoo dot co dot uk>
46600976812SAndreas Gohr * @link   http://de3.php.net/manual/en/function.realpath.php#75992
46700976812SAndreas Gohr */
468b328697dSAndreas Gohrfunction fullpath($path,$exists=false){
4694761d30cSAndreas Gohr    static $run = 0;
4704761d30cSAndreas Gohr    $root  = '';
4714761d30cSAndreas Gohr    $iswin = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' || $GLOBALS['DOKU_UNITTEST_ASSUME_WINDOWS']);
47200976812SAndreas Gohr
4734761d30cSAndreas Gohr    // find the (indestructable) root of the path - keeps windows stuff intact
4744761d30cSAndreas Gohr    if($path{0} == '/'){
4754761d30cSAndreas Gohr        $root = '/';
4764761d30cSAndreas Gohr    }elseif($iswin){
4774761d30cSAndreas Gohr        // match drive letter and UNC paths
4784761d30cSAndreas Gohr        if(preg_match('!^([a-zA-z]:)(.*)!',$path,$match)){
4794761d30cSAndreas Gohr            $root = $match[1];
4804761d30cSAndreas Gohr            $path = $match[2];
4814761d30cSAndreas Gohr        }else if(preg_match('!^(\\\\\\\\[^\\\\/]+\\\\[^\\\\/]+[\\\\/])(.*)!',$path,$match)){
4824761d30cSAndreas Gohr            $root = $match[1];
4834761d30cSAndreas Gohr            $path = $match[2];
48400976812SAndreas Gohr        }
4854761d30cSAndreas Gohr    }
4864761d30cSAndreas Gohr    $path = str_replace('\\','/',$path);
4874761d30cSAndreas Gohr
4884761d30cSAndreas Gohr    // if the given path wasn't absolute already, prepend the script path and retry
4894761d30cSAndreas Gohr    if(!$root){
4904761d30cSAndreas Gohr        $base = dirname($_SERVER['SCRIPT_FILENAME']);
4914761d30cSAndreas Gohr        $path = $base.'/'.$path;
4924761d30cSAndreas Gohr        if($run == 0){ // avoid endless recursion when base isn't absolute for some reason
4934761d30cSAndreas Gohr            $run++;
494b328697dSAndreas Gohr            return fullpath($path,$exists);
4954761d30cSAndreas Gohr        }
4964761d30cSAndreas Gohr    }
4974761d30cSAndreas Gohr    $run = 0;
49800976812SAndreas Gohr
49900976812SAndreas Gohr    // canonicalize
50000976812SAndreas Gohr    $path=explode('/', $path);
50100976812SAndreas Gohr    $newpath=array();
502ef38bfe8SAndreas Gohr    foreach($path as $p) {
503ef38bfe8SAndreas Gohr        if ($p === '' || $p === '.') continue;
504ef38bfe8SAndreas Gohr           if ($p==='..') {
50500976812SAndreas Gohr              array_pop($newpath);
50600976812SAndreas Gohr              continue;
50700976812SAndreas Gohr        }
508ef38bfe8SAndreas Gohr        array_push($newpath, $p);
50900976812SAndreas Gohr    }
5104761d30cSAndreas Gohr    $finalpath = $root.implode('/', $newpath);
51100976812SAndreas Gohr
512b328697dSAndreas Gohr    // check for existance when needed (except when unit testing)
513b328697dSAndreas Gohr    if($exists && !defined('DOKU_UNITTEST') && !@file_exists($finalpath)) {
5144761d30cSAndreas Gohr        return false;
51500976812SAndreas Gohr    }
5164761d30cSAndreas Gohr    return $finalpath;
51700976812SAndreas Gohr}
51800976812SAndreas Gohr
51900976812SAndreas Gohr
52000976812SAndreas Gohr
521340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
522