xref: /dokuwiki/inc/init.php (revision 48beefec86c6c69e0f79fc5cb1c7b6f1fb937973)
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
13*48beefecSAndreas Gohr  // if available load a preload config file
14*48beefecSAndreas Gohr  @include(fullpath(dirname(__FILE__)).'/preload.php');
15*48beefecSAndreas 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  }
26bad905f1SBen Coburn  if (!defined('DOKU_E_LEVEL')) { error_reporting(E_ALL ^ E_NOTICE); }
27bad905f1SBen Coburn  else { error_reporting(DOKU_E_LEVEL); }
28c53ea5f2Sandi
2950602150SBen Coburn  // init memory caches
30bc3e97beSAndreas Gohr  global $cache_revinfo;  $cache_revinfo = array();
31bc3e97beSAndreas Gohr  global $cache_wikifn;   $cache_wikifn = array();
32a424cd8eSchris  global $cache_cleanid;  $cache_cleanid = array();
33a424cd8eSchris  global $cache_authname; $cache_authname = array();
340a7e3bceSchris  global $cache_metadata; $cache_metadata = array();
3550602150SBen Coburn
364724a577Sandi  //prepare config array()
37ee20e7d1Sandi  global $conf;
3803c4aec3Schris  if (!defined('DOKU_UNITTEST')) {
394724a577Sandi    $conf = array();
404724a577Sandi
41ad15db82Sandi    // load the config file(s)
42e7cb32dcSAndreas Gohr    require_once(DOKU_CONF.'dokuwiki.php');
430a6ead41SAndreas Gohr    if(@file_exists(DOKU_CONF.'local.php')){
440a6ead41SAndreas Gohr      require_once(DOKU_CONF.'local.php');
450a6ead41SAndreas Gohr    }
4603c4aec3Schris  }
47ad15db82Sandi
48ad15db82Sandi  //prepare language array
49ee20e7d1Sandi  global $lang;
50ad15db82Sandi  $lang = array();
51ed7b5f09Sandi
5216521111Sandi  //load the language files
53bc3b6aecSandi  require_once(DOKU_INC.'inc/lang/en/lang.php');
54f949a01cSAndreas Gohr  if ( $conf['lang'] && $conf['lang'] != 'en' ) {
55bc3b6aecSandi    require_once(DOKU_INC.'inc/lang/'.$conf['lang'].'/lang.php');
56fc1c55b1Shfuecks  }
5716521111Sandi
58ed7b5f09Sandi  // define baseURL
594b1a4e04SAndreas Gohr  if(!defined('DOKU_REL')) define('DOKU_REL',getBaseURL(false));
60ed7b5f09Sandi  if(!defined('DOKU_URL')) define('DOKU_URL',getBaseURL(true));
614b1a4e04SAndreas Gohr  if(!defined('DOKU_BASE')){
624b1a4e04SAndreas Gohr    if($conf['canonical']){
634b1a4e04SAndreas Gohr      define('DOKU_BASE',DOKU_URL);
644b1a4e04SAndreas Gohr    }else{
654b1a4e04SAndreas Gohr      define('DOKU_BASE',DOKU_REL);
664b1a4e04SAndreas Gohr    }
674b1a4e04SAndreas Gohr  }
684b1a4e04SAndreas Gohr
69b8595a66SAndreas Gohr  // define whitespace
70b8595a66SAndreas Gohr  if(!defined('DOKU_LF')) define ('DOKU_LF',"\n");
71b8595a66SAndreas Gohr  if(!defined('DOKU_TAB')) define ('DOKU_TAB',"\t");
72ed7b5f09Sandi
73e71ce681SAndreas Gohr  // define cookie and session id
748b11f0a5SAndreas Gohr  if (!defined('DOKU_COOKIE')) define('DOKU_COOKIE', 'DW'.md5(DOKU_REL));
75e71ce681SAndreas Gohr
76ee20e7d1Sandi  // define Plugin dir
77f62ea8a1Sandi  if(!defined('DOKU_PLUGIN'))  define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
78ee20e7d1Sandi
79ed7b5f09Sandi  // define main script
80ed7b5f09Sandi  if(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php');
81ed7b5f09Sandi
826b13307fSandi  // define Template baseURL
836b13307fSandi  if(!defined('DOKU_TPL')) define('DOKU_TPL',
84f62ea8a1Sandi                                  DOKU_BASE.'lib/tpl/'.$conf['template'].'/');
856b13307fSandi
8678a6aeb1SAndreas Gohr  // define real Template directory
8778a6aeb1SAndreas Gohr  if(!defined('DOKU_TPLINC')) define('DOKU_TPLINC',
8878a6aeb1SAndreas Gohr                                  DOKU_INC.'lib/tpl/'.$conf['template'].'/');
8978a6aeb1SAndreas Gohr
90ed7b5f09Sandi  // make session rewrites XHTML compliant
913fc74836Sandi  @ini_set('arg_separator.output', '&amp;');
92ed7b5f09Sandi
93d7e6bba9SAndreas Gohr  // make sure global zlib does not interfere FS#1132
94d7e6bba9SAndreas Gohr  @ini_set('zlib.output_compression', 'off');
95d7e6bba9SAndreas Gohr
966deb5405SAndreas Gohr  // increase PCRE backtrack limit
976deb5405SAndreas Gohr  @ini_set('pcre.backtrack_limit', '20971520');
986deb5405SAndreas Gohr
993138b5c7SAndreas Gohr  // enable gzip compression
1003138b5c7SAndreas Gohr  if ($conf['gzip_output'] &&
1013138b5c7SAndreas Gohr      !defined('DOKU_DISABLE_GZIP_OUTPUT') &&
1023138b5c7SAndreas Gohr      function_exists('ob_gzhandler') &&
1033138b5c7SAndreas Gohr      preg_match('/gzip|deflate/', $_SERVER['HTTP_ACCEPT_ENCODING'])) {
1043138b5c7SAndreas Gohr    ob_start('ob_gzhandler');
1053138b5c7SAndreas Gohr  }
1063138b5c7SAndreas Gohr
107ed7b5f09Sandi  // init session
1086534245aSAndreas Gohr  if (!headers_sent() && !defined('NOSESSION')){
109ed7b5f09Sandi    session_name("DokuWiki");
1104b1a4e04SAndreas Gohr    session_set_cookie_params(0, DOKU_REL);
111bad31ae9SAndreas Gohr    session_start();
112bad31ae9SAndreas Gohr  }
113ed7b5f09Sandi
114ed7b5f09Sandi  // kill magic quotes
115e55eb89cSAndreas Gohr  if (get_magic_quotes_gpc() && !defined('MAGIC_QUOTES_STRIPPED')) {
116ed7b5f09Sandi    if (!empty($_GET))    remove_magic_quotes($_GET);
117ed7b5f09Sandi    if (!empty($_POST))   remove_magic_quotes($_POST);
118ed7b5f09Sandi    if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE);
119ed7b5f09Sandi    if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST);
1203fc74836Sandi    @ini_set('magic_quotes_gpc', 0);
121e55eb89cSAndreas Gohr    define('MAGIC_QUOTES_STRIPPED',1);
122ed7b5f09Sandi  }
1233fc74836Sandi  @set_magic_quotes_runtime(0);
1243fc74836Sandi  @ini_set('magic_quotes_sybase',0);
125ed7b5f09Sandi
126a1637ffdSAndreas Gohr  // don't let cookies ever interfere with request vars
127a1637ffdSAndreas Gohr  $_REQUEST = array_merge($_GET,$_POST);
128a1637ffdSAndreas Gohr
129ed7b5f09Sandi  // disable gzip if not available
1308a447e5cSAndreas Gohr  if($conf['compression'] == 'bz2' && !function_exists('bzopen')){
131fe893490SAndreas Gohr    $conf['compression'] = 'gz';
132501252a5SAndreas Gohr  }
133fe893490SAndreas Gohr  if($conf['compression'] == 'gz' && !function_exists('gzopen')){
134501252a5SAndreas Gohr    $conf['compression'] = 0;
135ed7b5f09Sandi  }
136ed7b5f09Sandi
137e656dcd4SAndreas Gohr  // fix dateformat for upgraders
138e656dcd4SAndreas Gohr  if(strpos($conf['dformat'],'%') === false){
139e656dcd4SAndreas Gohr    $conf['dformat'] = '%Y/%m/%d %H:%M';
140e656dcd4SAndreas Gohr  }
141e656dcd4SAndreas Gohr
1421ca31cfeSAndreas Gohr  // precalculate file creation modes
1431ca31cfeSAndreas Gohr  init_creationmodes();
144ed7b5f09Sandi
1453dc3a5f1Sandi  // make real paths and check them
14698407a7aSandi  init_paths();
1477367b368SAndreas Gohr  init_files();
148ed7b5f09Sandi
1498c4f28e8Sjan  // automatic upgrade to script versions of certain files
150e7cb32dcSAndreas Gohr  scriptify(DOKU_CONF.'users.auth');
151e7cb32dcSAndreas Gohr  scriptify(DOKU_CONF.'acl.auth');
152f62ea8a1Sandi
153f62ea8a1Sandi
154f62ea8a1Sandi/**
15598407a7aSandi * Checks paths from config file
15698407a7aSandi */
15798407a7aSandifunction init_paths(){
15898407a7aSandi  global $conf;
15998407a7aSandi
16098407a7aSandi  $paths = array('datadir'   => 'pages',
16198407a7aSandi                 'olddir'    => 'attic',
16298407a7aSandi                 'mediadir'  => 'media',
16398407a7aSandi                 'metadir'   => 'meta',
16498407a7aSandi                 'cachedir'  => 'cache',
165579b0f7eSTNHarris                 'indexdir'  => 'index',
166de33a58fSMichael Klier                 'lockdir'   => 'locks',
167de33a58fSMichael Klier                 'tmpdir'    => 'tmp');
16898407a7aSandi
16998407a7aSandi  foreach($paths as $c => $p){
170bb4866bdSchris    if(empty($conf[$c]))  $conf[$c] = $conf['savedir'].'/'.$p;
17198407a7aSandi    $conf[$c]             = init_path($conf[$c]);
1721983acc2SGuy Brand    if(empty($conf[$c]))  nice_die("The $c ('$p') does not exist, isn't accessible or writable.
17369dc3177SAndreas Gohr                               You should check your config and permission settings.
17469dc3177SAndreas Gohr                               Or maybe you want to <a href=\"install.php\">run the
17569dc3177SAndreas Gohr                               installer</a>?");
17698407a7aSandi  }
17771726d78SBen Coburn
17871726d78SBen Coburn  // path to old changelog only needed for upgrading
17971726d78SBen Coburn  $conf['changelog_old'] = init_path((isset($conf['changelog']))?($conf['changelog']):($conf['savedir'].'/changes.log'));
18071726d78SBen Coburn  if ($conf['changelog_old']=='') { unset($conf['changelog_old']); }
18171726d78SBen Coburn  // hardcoded changelog because it is now a cache that lives in meta
18271726d78SBen Coburn  $conf['changelog'] = $conf['metadir'].'/_dokuwiki.changes';
18398407a7aSandi}
18498407a7aSandi
18598407a7aSandi/**
1860d8850c4SAndreas Gohr * Checks the existance of certain files and creates them if missing.
1877367b368SAndreas Gohr */
1887367b368SAndreas Gohrfunction init_files(){
1897367b368SAndreas Gohr  global $conf;
1900d8850c4SAndreas Gohr
191579b0f7eSTNHarris  $files = array( $conf['indexdir'].'/page.idx');
1927367b368SAndreas Gohr
1937367b368SAndreas Gohr  foreach($files as $file){
1947367b368SAndreas Gohr    if(!@file_exists($file)){
1950d8850c4SAndreas Gohr      $fh = @fopen($file,'a');
1960d8850c4SAndreas Gohr      if($fh){
1977367b368SAndreas Gohr        fclose($fh);
1981ca31cfeSAndreas Gohr        if($conf['fperm']) chmod($file, $conf['fperm']);
1990d8850c4SAndreas Gohr      }else{
2003816dcbcSAndreas Gohr        nice_die("$file is not writable. Check your permissions settings!");
2010d8850c4SAndreas Gohr      }
2027367b368SAndreas Gohr    }
2037367b368SAndreas Gohr  }
2047367b368SAndreas Gohr}
2057367b368SAndreas Gohr
2067367b368SAndreas Gohr/**
2070d8850c4SAndreas Gohr * Returns absolute path
208f62ea8a1Sandi *
2090d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC.
2100d8850c4SAndreas Gohr * Check for accessability on directories as well.
2110d8850c4SAndreas Gohr *
2120d8850c4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
213f62ea8a1Sandi */
214f62ea8a1Sandifunction init_path($path){
2150d8850c4SAndreas Gohr  // check existance
21600976812SAndreas Gohr  $p = fullpath($path);
2170d8850c4SAndreas Gohr  if(!@file_exists($p)){
21800976812SAndreas Gohr    $p = fullpath(DOKU_INC.$path);
2190d8850c4SAndreas Gohr    if(!@file_exists($p)){
2208fc4e739Sandi      return '';
221f62ea8a1Sandi    }
2220d8850c4SAndreas Gohr  }
2230d8850c4SAndreas Gohr
2240d8850c4SAndreas Gohr  // check writability
2250d8850c4SAndreas Gohr  if(!@is_writable($p)){
2260d8850c4SAndreas Gohr    return '';
2270d8850c4SAndreas Gohr  }
2280d8850c4SAndreas Gohr
2290d8850c4SAndreas Gohr  // check accessability (execute bit) for directories
2300d8850c4SAndreas Gohr  if(@is_dir($p) && !@file_exists("$p/.")){
2310d8850c4SAndreas Gohr    return '';
2320d8850c4SAndreas Gohr  }
2330d8850c4SAndreas Gohr
2340d8850c4SAndreas Gohr  return $p;
2350d8850c4SAndreas Gohr}
2368c4f28e8Sjan
237ed7b5f09Sandi/**
2381ca31cfeSAndreas Gohr * Sets the internal config values fperm and dperm which, when set,
2391ca31cfeSAndreas Gohr * will be used to change the permission of a newly created dir or
2401ca31cfeSAndreas Gohr * file with chmod. Considers the influence of the system's umask
2411ca31cfeSAndreas Gohr * setting the values only if needed.
2421ca31cfeSAndreas Gohr */
2431ca31cfeSAndreas Gohrfunction init_creationmodes(){
2441ca31cfeSAndreas Gohr  global $conf;
2451ca31cfeSAndreas Gohr
2461ca31cfeSAndreas Gohr  // Legacy support for old umask/dmask scheme
2471ca31cfeSAndreas Gohr  unset($conf['dmask']);
2481ca31cfeSAndreas Gohr  unset($conf['fmask']);
2491ca31cfeSAndreas Gohr  unset($conf['umask']);
2501ca31cfeSAndreas Gohr  unset($conf['fperm']);
2511ca31cfeSAndreas Gohr  unset($conf['dperm']);
2521ca31cfeSAndreas Gohr
2539f3cdec3SAndreas Gohr  // get system umask, fallback to 0 if none available
2549f3cdec3SAndreas Gohr  $umask = @umask();
2559f3cdec3SAndreas Gohr  if(!$umask) $umask = 0000;
2561ca31cfeSAndreas Gohr
2571ca31cfeSAndreas Gohr  // check what is set automatically by the system on file creation
2581ca31cfeSAndreas Gohr  // and set the fperm param if it's not what we want
2591ca31cfeSAndreas Gohr  $auto_fmode = 0666 & ~$umask;
2601ca31cfeSAndreas Gohr  if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode'];
2611ca31cfeSAndreas Gohr
2621ca31cfeSAndreas Gohr  // check what is set automatically by the system on file creation
2631ca31cfeSAndreas Gohr  // and set the dperm param if it's not what we want
2641ca31cfeSAndreas Gohr  $auto_dmode = $conf['dmode'] & ~$umask;
2651ca31cfeSAndreas Gohr  if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode'];
2661ca31cfeSAndreas Gohr}
2671ca31cfeSAndreas Gohr
2681ca31cfeSAndreas Gohr/**
269ed7b5f09Sandi * remove magic quotes recursivly
270ed7b5f09Sandi *
271ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
272ed7b5f09Sandi */
273ed7b5f09Sandifunction remove_magic_quotes(&$array) {
274ed7b5f09Sandi  foreach (array_keys($array) as $key) {
27581c54349SAndreas Gohr      // handle magic quotes in keynames (breaks order)
27681c54349SAndreas Gohr      $sk = stripslashes($key);
27781c54349SAndreas Gohr      if($sk != $key){
27881c54349SAndreas Gohr          $array[$sk] = $array[$key];
27981c54349SAndreas Gohr          unset($array[$key]);
28081c54349SAndreas Gohr          $key = $sk;
28181c54349SAndreas Gohr      }
28281c54349SAndreas Gohr
28381c54349SAndreas Gohr      // do recursion if needed
284ed7b5f09Sandi      if (is_array($array[$key])) {
285ed7b5f09Sandi          remove_magic_quotes($array[$key]);
286ed7b5f09Sandi      }else {
287ed7b5f09Sandi          $array[$key] = stripslashes($array[$key]);
288ed7b5f09Sandi      }
289ed7b5f09Sandi  }
290ed7b5f09Sandi}
291ed7b5f09Sandi
292ed7b5f09Sandi/**
293ed7b5f09Sandi * Returns the full absolute URL to the directory where
294ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash)
295ed7b5f09Sandi *
296ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
297ed7b5f09Sandi */
2984b1a4e04SAndreas Gohrfunction getBaseURL($abs=null){
299ed7b5f09Sandi  global $conf;
300ed7b5f09Sandi  //if canonical url enabled always return absolute
3014b1a4e04SAndreas Gohr  if(is_null($abs)) $abs = $conf['canonical'];
302ed7b5f09Sandi
30392b83b77Sandi  if($conf['basedir']){
30446c73e01SChris Smith    $dir = $conf['basedir'];
30589aa05dbSAndreas Gohr  }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){
30646c73e01SChris Smith    $dir = dirname($_SERVER['SCRIPT_NAME']);
30789aa05dbSAndreas Gohr  }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){
30846c73e01SChris Smith    $dir = dirname($_SERVER['PHP_SELF']);
309093ec9e4Sandi  }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
310093ec9e4Sandi    $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
311093ec9e4Sandi                         $_SERVER['SCRIPT_FILENAME']);
31246c73e01SChris Smith    $dir = dirname('/'.$dir);
31392b83b77Sandi  }else{
31446c73e01SChris Smith    $dir = '.'; //probably wrong
31592b83b77Sandi  }
316ed7b5f09Sandi
31746c73e01SChris Smith  $dir = str_replace('\\','/',$dir);             // bugfix for weird WIN behaviour
31846c73e01SChris Smith  $dir = preg_replace('#//+#','/',"/$dir/");     // ensure leading and trailing slashes
319ed7b5f09Sandi
320f62ea8a1Sandi  //handle script in lib/exe dir
321f62ea8a1Sandi  $dir = preg_replace('!lib/exe/$!','',$dir);
322f62ea8a1Sandi
323488d5fa0SMichael Klier chi@chimeric.de  //handle script in lib/plugins dir
324488d5fa0SMichael Klier chi@chimeric.de  $dir = preg_replace('!lib/plugins/.*$!','',$dir);
325488d5fa0SMichael Klier chi@chimeric.de
326ed7b5f09Sandi  //finish here for relative URLs
327ed7b5f09Sandi  if(!$abs) return $dir;
328ed7b5f09Sandi
32946c73e01SChris Smith  //use config option if available, trim any slash from end of baseurl to avoid multiple consecutive slashes in the path
33046c73e01SChris Smith  if($conf['baseurl']) return rtrim($conf['baseurl'],'/').$dir;
331ef7b3ecdSAndreas Gohr
332e82e3526SAndreas Gohr  //split hostheader into host and port
333e82e3526SAndreas Gohr  list($host,$port) = explode(':',$_SERVER['HTTP_HOST']);
334e82e3526SAndreas Gohr  if(!$port)  $port = $_SERVER['SERVER_PORT'];
335e82e3526SAndreas Gohr  if(!$port)  $port = 80;
336ed7b5f09Sandi
337ed7b5f09Sandi  // see if HTTPS is enabled - apache leaves this empty when not available,
338ed7b5f09Sandi  // IIS sets it to 'off', 'false' and 'disabled' are just guessing
339ed7b5f09Sandi  if (preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){
340ed7b5f09Sandi    $proto = 'http://';
341e82e3526SAndreas Gohr    if ($port == '80') {
342ed7b5f09Sandi      $port='';
343ed7b5f09Sandi    }
344ed7b5f09Sandi  }else{
345ed7b5f09Sandi    $proto = 'https://';
346e82e3526SAndreas Gohr    if ($port == '443') {
347ed7b5f09Sandi      $port='';
348ed7b5f09Sandi    }
349ed7b5f09Sandi  }
350ed7b5f09Sandi
351e82e3526SAndreas Gohr  if($port) $port = ':'.$port;
352e82e3526SAndreas Gohr
353ed7b5f09Sandi  return $proto.$host.$port.$dir;
354ed7b5f09Sandi}
355ed7b5f09Sandi
356b000c6d4Sandi/**
357b000c6d4Sandi * Append a PHP extension to a given file and adds an exit call
358b000c6d4Sandi *
359b000c6d4Sandi * This is used to migrate some old configfiles. An added PHP extension
360b000c6d4Sandi * ensures the contents are not shown to webusers even if .htaccess files
361b000c6d4Sandi * do not work
362b000c6d4Sandi *
363b000c6d4Sandi * @author Jan Decaluwe <jan@jandecaluwe.com>
364b000c6d4Sandi */
3658c4f28e8Sjanfunction scriptify($file) {
3668c4f28e8Sjan  // checks
3678c4f28e8Sjan  if (!is_readable($file)) {
3688c4f28e8Sjan    return;
3698c4f28e8Sjan  }
3708c4f28e8Sjan  $fn = $file.'.php';
3718c4f28e8Sjan  if (@file_exists($fn)) {
3728c4f28e8Sjan    return;
3738c4f28e8Sjan  }
3748c4f28e8Sjan  $fh = fopen($fn, 'w');
3758c4f28e8Sjan  if (!$fh) {
3763816dcbcSAndreas Gohr    nice_die($fn.' is not writable. Check your permission settings!');
3778c4f28e8Sjan  }
3788c4f28e8Sjan  // write php exit hack first
3798c4f28e8Sjan  fwrite($fh, "# $fn\n");
3808c4f28e8Sjan  fwrite($fh, '# <?php exit()?>'."\n");
3818c4f28e8Sjan  fwrite($fh, "# Don't modify the lines above\n");
3828c4f28e8Sjan  fwrite($fh, "#\n");
3838c4f28e8Sjan  // copy existing lines
3848c4f28e8Sjan  $lines = file($file);
3858c4f28e8Sjan  foreach ($lines as $line){
3868c4f28e8Sjan    fwrite($fh, $line);
3878c4f28e8Sjan  }
3883ba793e2Sandi  fclose($fh);
389b000c6d4Sandi  //try to rename the old file
3903aee4c27SAndreas Gohr  io_rename($file,"$file.old");
3918c4f28e8Sjan}
3928c4f28e8Sjan
3933816dcbcSAndreas Gohr/**
3943816dcbcSAndreas Gohr * print a nice message even if no styles are loaded yet.
3953816dcbcSAndreas Gohr */
3963816dcbcSAndreas Gohrfunction nice_die($msg){
3973816dcbcSAndreas Gohr  echo<<<EOT
3983816dcbcSAndreas Gohr  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
3993816dcbcSAndreas Gohr   "http://www.w3.org/TR/html4/loose.dtd">
4003816dcbcSAndreas Gohr  <html>
4013816dcbcSAndreas Gohr    <head><title>DokuWiki Setup Error</title></head>
4023816dcbcSAndreas Gohr    <body style="font-family: Arial, sans-serif">
4033816dcbcSAndreas Gohr      <div style="width:60%; margin: auto; background-color: #fcc;
4043816dcbcSAndreas Gohr                  border: 1px solid #faa; padding: 0.5em 1em;">
4053816dcbcSAndreas Gohr      <h1 style="font-size: 120%">DokuWiki Setup Error</h1>
4063816dcbcSAndreas Gohr      <p>$msg</p>
4073816dcbcSAndreas Gohr      </div>
4083816dcbcSAndreas Gohr    </body>
4093816dcbcSAndreas Gohr  </html>
4103816dcbcSAndreas GohrEOT;
4113816dcbcSAndreas Gohr  exit;
4123816dcbcSAndreas Gohr}
4133816dcbcSAndreas Gohr
414ed7b5f09Sandi
41500976812SAndreas Gohr/**
41600976812SAndreas Gohr * A realpath() replacement
41700976812SAndreas Gohr *
41800976812SAndreas Gohr * This function behaves similar to PHP's realpath() but does not resolve
41900976812SAndreas Gohr * symlinks or accesses upper directories
42000976812SAndreas Gohr *
42100976812SAndreas Gohr * @author <richpageau at yahoo dot co dot uk>
42200976812SAndreas Gohr * @link   http://de3.php.net/manual/en/function.realpath.php#75992
42300976812SAndreas Gohr */
42400976812SAndreas Gohrfunction fullpath($path){
425ef38bfe8SAndreas Gohr    $iswin = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN');
426b934b851Silya    $isunc = 0===strpos($path,'\\\\');
427ef38bfe8SAndreas Gohr    if($iswin) $path = str_replace('\\','/',$path); // windows compatibility
42800976812SAndreas Gohr
429ef38bfe8SAndreas Gohr    // check if path begins with "/" or "c:" ie. is absolute
43000976812SAndreas Gohr    // if it isnt concat with script path
431b934b851Silya    if (!$isunc &&
432b934b851Silya        ((!$iswin && $path{0} !== '/') ||
433b934b851Silya         ($iswin && $path{1} !== ':'))) {
43400976812SAndreas Gohr        $base=dirname($_SERVER['SCRIPT_FILENAME']);
43500976812SAndreas Gohr        $path=$base."/".$path;
43600976812SAndreas Gohr    }
43700976812SAndreas Gohr
43800976812SAndreas Gohr    // canonicalize
43900976812SAndreas Gohr    $path=explode('/', $path);
44000976812SAndreas Gohr    $newpath=array();
441ef38bfe8SAndreas Gohr    foreach($path as $p) {
442ef38bfe8SAndreas Gohr        if ($p === '' || $p === '.') continue;
443ef38bfe8SAndreas Gohr           if ($p==='..') {
44400976812SAndreas Gohr              array_pop($newpath);
44500976812SAndreas Gohr              continue;
44600976812SAndreas Gohr        }
447ef38bfe8SAndreas Gohr        array_push($newpath, $p);
44800976812SAndreas Gohr    }
449ef38bfe8SAndreas Gohr    $finalpath = implode('/', $newpath);
450b934b851Silya    if($isunc) $finalpath = '//'.$finalpath;
451ef38bfe8SAndreas Gohr    if(!$iswin) $finalpath = '/'.$finalpath;
45200976812SAndreas Gohr
45300976812SAndreas Gohr    // check then return valid path or filename
45400976812SAndreas Gohr    if (file_exists($finalpath)) {
45500976812SAndreas Gohr        return ($finalpath);
45600976812SAndreas Gohr    }
45700976812SAndreas Gohr    else return false;
45800976812SAndreas Gohr}
45900976812SAndreas Gohr
46000976812SAndreas Gohr
46100976812SAndreas Gohr
462340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
463