xref: /dokuwiki/inc/init.php (revision e656dcd46abfe069ea83271248dbd7aae36554ca)
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
13ed7b5f09Sandi  // define the include path
1400976812SAndreas Gohr  if(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../').'/');
15ad15db82Sandi
16e7cb32dcSAndreas Gohr  // define config path (packagers may want to change this to /etc/dokuwiki/)
17b7551a6dSEsther Brunner  if(!defined('DOKU_CONF')) define('DOKU_CONF',DOKU_INC.'conf/');
18e7cb32dcSAndreas Gohr
19bad905f1SBen Coburn  // check for error reporting override or set error reporting to sane values
20d8186216SBen Coburn  if (!defined('DOKU_E_LEVEL') && @file_exists(DOKU_CONF.'report_e_all')) {
21bad905f1SBen Coburn    define('DOKU_E_LEVEL', E_ALL);
22bad905f1SBen Coburn  }
23bad905f1SBen Coburn  if (!defined('DOKU_E_LEVEL')) { error_reporting(E_ALL ^ E_NOTICE); }
24bad905f1SBen Coburn  else { error_reporting(DOKU_E_LEVEL); }
25c53ea5f2Sandi
2650602150SBen Coburn  // init memory caches
27bc3e97beSAndreas Gohr  global $cache_revinfo;  $cache_revinfo = array();
28bc3e97beSAndreas Gohr  global $cache_wikifn;   $cache_wikifn = array();
29a424cd8eSchris  global $cache_cleanid;  $cache_cleanid = array();
30a424cd8eSchris  global $cache_authname; $cache_authname = array();
310a7e3bceSchris  global $cache_metadata; $cache_metadata = array();
3250602150SBen Coburn
334724a577Sandi  //prepare config array()
34ee20e7d1Sandi  global $conf;
3503c4aec3Schris  if (!defined('DOKU_UNITTEST')) {
364724a577Sandi    $conf = array();
374724a577Sandi
38ad15db82Sandi    // load the config file(s)
39e7cb32dcSAndreas Gohr    require_once(DOKU_CONF.'dokuwiki.php');
400a6ead41SAndreas Gohr    if(@file_exists(DOKU_CONF.'local.php')){
410a6ead41SAndreas Gohr      require_once(DOKU_CONF.'local.php');
420a6ead41SAndreas Gohr    }
4303c4aec3Schris  }
44ad15db82Sandi
45ad15db82Sandi  //prepare language array
46ee20e7d1Sandi  global $lang;
47ad15db82Sandi  $lang = array();
48ed7b5f09Sandi
4916521111Sandi  //load the language files
50bc3b6aecSandi  require_once(DOKU_INC.'inc/lang/en/lang.php');
51f949a01cSAndreas Gohr  if ( $conf['lang'] && $conf['lang'] != 'en' ) {
52bc3b6aecSandi    require_once(DOKU_INC.'inc/lang/'.$conf['lang'].'/lang.php');
53fc1c55b1Shfuecks  }
5416521111Sandi
55ed7b5f09Sandi  // define baseURL
564b1a4e04SAndreas Gohr  if(!defined('DOKU_REL')) define('DOKU_REL',getBaseURL(false));
57ed7b5f09Sandi  if(!defined('DOKU_URL')) define('DOKU_URL',getBaseURL(true));
584b1a4e04SAndreas Gohr  if(!defined('DOKU_BASE')){
594b1a4e04SAndreas Gohr    if($conf['canonical']){
604b1a4e04SAndreas Gohr      define('DOKU_BASE',DOKU_URL);
614b1a4e04SAndreas Gohr    }else{
624b1a4e04SAndreas Gohr      define('DOKU_BASE',DOKU_REL);
634b1a4e04SAndreas Gohr    }
644b1a4e04SAndreas Gohr  }
654b1a4e04SAndreas Gohr
66b8595a66SAndreas Gohr  // define whitespace
67b8595a66SAndreas Gohr  if(!defined('DOKU_LF')) define ('DOKU_LF',"\n");
68b8595a66SAndreas Gohr  if(!defined('DOKU_TAB')) define ('DOKU_TAB',"\t");
69ed7b5f09Sandi
70e71ce681SAndreas Gohr  // define cookie and session id
718b11f0a5SAndreas Gohr  if (!defined('DOKU_COOKIE')) define('DOKU_COOKIE', 'DW'.md5(DOKU_REL));
72e71ce681SAndreas Gohr
73ee20e7d1Sandi  // define Plugin dir
74f62ea8a1Sandi  if(!defined('DOKU_PLUGIN'))  define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
75ee20e7d1Sandi
76ed7b5f09Sandi  // define main script
77ed7b5f09Sandi  if(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php');
78ed7b5f09Sandi
796b13307fSandi  // define Template baseURL
806b13307fSandi  if(!defined('DOKU_TPL')) define('DOKU_TPL',
81f62ea8a1Sandi                                  DOKU_BASE.'lib/tpl/'.$conf['template'].'/');
826b13307fSandi
8378a6aeb1SAndreas Gohr  // define real Template directory
8478a6aeb1SAndreas Gohr  if(!defined('DOKU_TPLINC')) define('DOKU_TPLINC',
8578a6aeb1SAndreas Gohr                                  DOKU_INC.'lib/tpl/'.$conf['template'].'/');
8678a6aeb1SAndreas Gohr
87ed7b5f09Sandi  // make session rewrites XHTML compliant
883fc74836Sandi  @ini_set('arg_separator.output', '&amp;');
89ed7b5f09Sandi
90d7e6bba9SAndreas Gohr  // make sure global zlib does not interfere FS#1132
91d7e6bba9SAndreas Gohr  @ini_set('zlib.output_compression', 'off');
92d7e6bba9SAndreas Gohr
933138b5c7SAndreas Gohr  // enable gzip compression
943138b5c7SAndreas Gohr  if ($conf['gzip_output'] &&
953138b5c7SAndreas Gohr      !defined('DOKU_DISABLE_GZIP_OUTPUT') &&
963138b5c7SAndreas Gohr      function_exists('ob_gzhandler') &&
973138b5c7SAndreas Gohr      preg_match('/gzip|deflate/', $_SERVER['HTTP_ACCEPT_ENCODING'])) {
983138b5c7SAndreas Gohr    ob_start('ob_gzhandler');
993138b5c7SAndreas Gohr  }
1003138b5c7SAndreas Gohr
101ed7b5f09Sandi  // init session
1026534245aSAndreas Gohr  if (!headers_sent() && !defined('NOSESSION')){
103ed7b5f09Sandi    session_name("DokuWiki");
1044b1a4e04SAndreas Gohr    session_set_cookie_params(0, DOKU_REL);
105bad31ae9SAndreas Gohr    session_start();
106bad31ae9SAndreas Gohr  }
107ed7b5f09Sandi
108ed7b5f09Sandi  // kill magic quotes
109e55eb89cSAndreas Gohr  if (get_magic_quotes_gpc() && !defined('MAGIC_QUOTES_STRIPPED')) {
110ed7b5f09Sandi    if (!empty($_GET))    remove_magic_quotes($_GET);
111ed7b5f09Sandi    if (!empty($_POST))   remove_magic_quotes($_POST);
112ed7b5f09Sandi    if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE);
113ed7b5f09Sandi    if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST);
1143fc74836Sandi    @ini_set('magic_quotes_gpc', 0);
115e55eb89cSAndreas Gohr    define('MAGIC_QUOTES_STRIPPED',1);
116ed7b5f09Sandi  }
1173fc74836Sandi  @set_magic_quotes_runtime(0);
1183fc74836Sandi  @ini_set('magic_quotes_sybase',0);
119ed7b5f09Sandi
120a1637ffdSAndreas Gohr  // don't let cookies ever interfere with request vars
121a1637ffdSAndreas Gohr  $_REQUEST = array_merge($_GET,$_POST);
122a1637ffdSAndreas Gohr
123ed7b5f09Sandi  // disable gzip if not available
1248a447e5cSAndreas Gohr  if($conf['compression'] == 'bz2' && !function_exists('bzopen')){
125fe893490SAndreas Gohr    $conf['compression'] = 'gz';
126501252a5SAndreas Gohr  }
127fe893490SAndreas Gohr  if($conf['compression'] == 'gz' && !function_exists('gzopen')){
128501252a5SAndreas Gohr    $conf['compression'] = 0;
129ed7b5f09Sandi  }
130ed7b5f09Sandi
131*e656dcd4SAndreas Gohr  // fix dateformat for upgraders
132*e656dcd4SAndreas Gohr  if(strpos($conf['dformat'],'%') === false){
133*e656dcd4SAndreas Gohr    $conf['dformat'] = '%Y/%m/%d %H:%M';
134*e656dcd4SAndreas Gohr  }
135*e656dcd4SAndreas Gohr
1361ca31cfeSAndreas Gohr  // precalculate file creation modes
1371ca31cfeSAndreas Gohr  init_creationmodes();
138ed7b5f09Sandi
1393dc3a5f1Sandi  // make real paths and check them
14098407a7aSandi  init_paths();
1417367b368SAndreas Gohr  init_files();
142ed7b5f09Sandi
1438c4f28e8Sjan  // automatic upgrade to script versions of certain files
144e7cb32dcSAndreas Gohr  scriptify(DOKU_CONF.'users.auth');
145e7cb32dcSAndreas Gohr  scriptify(DOKU_CONF.'acl.auth');
146f62ea8a1Sandi
147f62ea8a1Sandi
148f62ea8a1Sandi/**
14998407a7aSandi * Checks paths from config file
15098407a7aSandi */
15198407a7aSandifunction init_paths(){
15298407a7aSandi  global $conf;
15398407a7aSandi
15498407a7aSandi  $paths = array('datadir'   => 'pages',
15598407a7aSandi                 'olddir'    => 'attic',
15698407a7aSandi                 'mediadir'  => 'media',
15798407a7aSandi                 'metadir'   => 'meta',
15898407a7aSandi                 'cachedir'  => 'cache',
159579b0f7eSTNHarris                 'indexdir'  => 'index',
16071726d78SBen Coburn                 'lockdir'   => 'locks');
16198407a7aSandi
16298407a7aSandi  foreach($paths as $c => $p){
163bb4866bdSchris    if(empty($conf[$c]))  $conf[$c] = $conf['savedir'].'/'.$p;
16498407a7aSandi    $conf[$c]             = init_path($conf[$c]);
1651983acc2SGuy Brand    if(empty($conf[$c]))  nice_die("The $c ('$p') does not exist, isn't accessible or writable.
16669dc3177SAndreas Gohr                               You should check your config and permission settings.
16769dc3177SAndreas Gohr                               Or maybe you want to <a href=\"install.php\">run the
16869dc3177SAndreas Gohr                               installer</a>?");
16998407a7aSandi  }
17071726d78SBen Coburn
17171726d78SBen Coburn  // path to old changelog only needed for upgrading
17271726d78SBen Coburn  $conf['changelog_old'] = init_path((isset($conf['changelog']))?($conf['changelog']):($conf['savedir'].'/changes.log'));
17371726d78SBen Coburn  if ($conf['changelog_old']=='') { unset($conf['changelog_old']); }
17471726d78SBen Coburn  // hardcoded changelog because it is now a cache that lives in meta
17571726d78SBen Coburn  $conf['changelog'] = $conf['metadir'].'/_dokuwiki.changes';
17698407a7aSandi}
17798407a7aSandi
17898407a7aSandi/**
1790d8850c4SAndreas Gohr * Checks the existance of certain files and creates them if missing.
1807367b368SAndreas Gohr */
1817367b368SAndreas Gohrfunction init_files(){
1827367b368SAndreas Gohr  global $conf;
1830d8850c4SAndreas Gohr
184579b0f7eSTNHarris  $files = array( $conf['indexdir'].'/page.idx');
1857367b368SAndreas Gohr
1867367b368SAndreas Gohr  foreach($files as $file){
1877367b368SAndreas Gohr    if(!@file_exists($file)){
1880d8850c4SAndreas Gohr      $fh = @fopen($file,'a');
1890d8850c4SAndreas Gohr      if($fh){
1907367b368SAndreas Gohr        fclose($fh);
1911ca31cfeSAndreas Gohr        if($conf['fperm']) chmod($file, $conf['fperm']);
1920d8850c4SAndreas Gohr      }else{
1933816dcbcSAndreas Gohr        nice_die("$file is not writable. Check your permissions settings!");
1940d8850c4SAndreas Gohr      }
1957367b368SAndreas Gohr    }
1967367b368SAndreas Gohr  }
1977367b368SAndreas Gohr}
1987367b368SAndreas Gohr
1997367b368SAndreas Gohr/**
2000d8850c4SAndreas Gohr * Returns absolute path
201f62ea8a1Sandi *
2020d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC.
2030d8850c4SAndreas Gohr * Check for accessability on directories as well.
2040d8850c4SAndreas Gohr *
2050d8850c4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
206f62ea8a1Sandi */
207f62ea8a1Sandifunction init_path($path){
2080d8850c4SAndreas Gohr  // check existance
20900976812SAndreas Gohr  $p = fullpath($path);
2100d8850c4SAndreas Gohr  if(!@file_exists($p)){
21100976812SAndreas Gohr    $p = fullpath(DOKU_INC.$path);
2120d8850c4SAndreas Gohr    if(!@file_exists($p)){
2138fc4e739Sandi      return '';
214f62ea8a1Sandi    }
2150d8850c4SAndreas Gohr  }
2160d8850c4SAndreas Gohr
2170d8850c4SAndreas Gohr  // check writability
2180d8850c4SAndreas Gohr  if(!@is_writable($p)){
2190d8850c4SAndreas Gohr    return '';
2200d8850c4SAndreas Gohr  }
2210d8850c4SAndreas Gohr
2220d8850c4SAndreas Gohr  // check accessability (execute bit) for directories
2230d8850c4SAndreas Gohr  if(@is_dir($p) && !@file_exists("$p/.")){
2240d8850c4SAndreas Gohr    return '';
2250d8850c4SAndreas Gohr  }
2260d8850c4SAndreas Gohr
2270d8850c4SAndreas Gohr  return $p;
2280d8850c4SAndreas Gohr}
2298c4f28e8Sjan
230ed7b5f09Sandi/**
2311ca31cfeSAndreas Gohr * Sets the internal config values fperm and dperm which, when set,
2321ca31cfeSAndreas Gohr * will be used to change the permission of a newly created dir or
2331ca31cfeSAndreas Gohr * file with chmod. Considers the influence of the system's umask
2341ca31cfeSAndreas Gohr * setting the values only if needed.
2351ca31cfeSAndreas Gohr */
2361ca31cfeSAndreas Gohrfunction init_creationmodes(){
2371ca31cfeSAndreas Gohr  global $conf;
2381ca31cfeSAndreas Gohr
2391ca31cfeSAndreas Gohr  // Legacy support for old umask/dmask scheme
2401ca31cfeSAndreas Gohr  unset($conf['dmask']);
2411ca31cfeSAndreas Gohr  unset($conf['fmask']);
2421ca31cfeSAndreas Gohr  unset($conf['umask']);
2431ca31cfeSAndreas Gohr  unset($conf['fperm']);
2441ca31cfeSAndreas Gohr  unset($conf['dperm']);
2451ca31cfeSAndreas Gohr
2469f3cdec3SAndreas Gohr  // get system umask, fallback to 0 if none available
2479f3cdec3SAndreas Gohr  $umask = @umask();
2489f3cdec3SAndreas Gohr  if(!$umask) $umask = 0000;
2491ca31cfeSAndreas Gohr
2501ca31cfeSAndreas Gohr  // check what is set automatically by the system on file creation
2511ca31cfeSAndreas Gohr  // and set the fperm param if it's not what we want
2521ca31cfeSAndreas Gohr  $auto_fmode = 0666 & ~$umask;
2531ca31cfeSAndreas Gohr  if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode'];
2541ca31cfeSAndreas Gohr
2551ca31cfeSAndreas Gohr  // check what is set automatically by the system on file creation
2561ca31cfeSAndreas Gohr  // and set the dperm param if it's not what we want
2571ca31cfeSAndreas Gohr  $auto_dmode = $conf['dmode'] & ~$umask;
2581ca31cfeSAndreas Gohr  if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode'];
2591ca31cfeSAndreas Gohr}
2601ca31cfeSAndreas Gohr
2611ca31cfeSAndreas Gohr/**
262ed7b5f09Sandi * remove magic quotes recursivly
263ed7b5f09Sandi *
264ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
265ed7b5f09Sandi */
266ed7b5f09Sandifunction remove_magic_quotes(&$array) {
267ed7b5f09Sandi  foreach (array_keys($array) as $key) {
26881c54349SAndreas Gohr      // handle magic quotes in keynames (breaks order)
26981c54349SAndreas Gohr      $sk = stripslashes($key);
27081c54349SAndreas Gohr      if($sk != $key){
27181c54349SAndreas Gohr          $array[$sk] = $array[$key];
27281c54349SAndreas Gohr          unset($array[$key]);
27381c54349SAndreas Gohr          $key = $sk;
27481c54349SAndreas Gohr      }
27581c54349SAndreas Gohr
27681c54349SAndreas Gohr      // do recursion if needed
277ed7b5f09Sandi      if (is_array($array[$key])) {
278ed7b5f09Sandi          remove_magic_quotes($array[$key]);
279ed7b5f09Sandi      }else {
280ed7b5f09Sandi          $array[$key] = stripslashes($array[$key]);
281ed7b5f09Sandi      }
282ed7b5f09Sandi  }
283ed7b5f09Sandi}
284ed7b5f09Sandi
285ed7b5f09Sandi/**
286ed7b5f09Sandi * Returns the full absolute URL to the directory where
287ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash)
288ed7b5f09Sandi *
289ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
290ed7b5f09Sandi */
2914b1a4e04SAndreas Gohrfunction getBaseURL($abs=null){
292ed7b5f09Sandi  global $conf;
293ed7b5f09Sandi  //if canonical url enabled always return absolute
2944b1a4e04SAndreas Gohr  if(is_null($abs)) $abs = $conf['canonical'];
295ed7b5f09Sandi
29692b83b77Sandi  if($conf['basedir']){
297919eeb46Sandi    $dir = $conf['basedir'].'/';
29889aa05dbSAndreas Gohr  }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){
299bdc127a4Sandi    $dir = dirname($_SERVER['SCRIPT_NAME']).'/';
30089aa05dbSAndreas Gohr  }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){
30189aa05dbSAndreas Gohr    $dir = dirname($_SERVER['PHP_SELF']).'/';
302093ec9e4Sandi  }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
303093ec9e4Sandi    $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
304093ec9e4Sandi                         $_SERVER['SCRIPT_FILENAME']);
305093ec9e4Sandi    $dir = dirname('/'.$dir).'/';
30692b83b77Sandi  }else{
30789aa05dbSAndreas Gohr    $dir = './'; //probably wrong
30892b83b77Sandi  }
309ed7b5f09Sandi
310ed7b5f09Sandi  $dir = str_replace('\\','/',$dir); #bugfix for weird WIN behaviour
311ed7b5f09Sandi  $dir = preg_replace('#//+#','/',$dir);
312ed7b5f09Sandi
313f62ea8a1Sandi  //handle script in lib/exe dir
314f62ea8a1Sandi  $dir = preg_replace('!lib/exe/$!','',$dir);
315f62ea8a1Sandi
316488d5fa0SMichael Klier chi@chimeric.de  //handle script in lib/plugins dir
317488d5fa0SMichael Klier chi@chimeric.de  $dir = preg_replace('!lib/plugins/.*$!','',$dir);
318488d5fa0SMichael Klier chi@chimeric.de
319ed7b5f09Sandi  //finish here for relative URLs
320ed7b5f09Sandi  if(!$abs) return $dir;
321ed7b5f09Sandi
322ef7b3ecdSAndreas Gohr  //use config option if available
323ef7b3ecdSAndreas Gohr  if($conf['baseurl']) return $conf['baseurl'].$dir;
324ef7b3ecdSAndreas Gohr
325e82e3526SAndreas Gohr  //split hostheader into host and port
326e82e3526SAndreas Gohr  list($host,$port) = explode(':',$_SERVER['HTTP_HOST']);
327e82e3526SAndreas Gohr  if(!$port)  $port = $_SERVER['SERVER_PORT'];
328e82e3526SAndreas Gohr  if(!$port)  $port = 80;
329ed7b5f09Sandi
330ed7b5f09Sandi  // see if HTTPS is enabled - apache leaves this empty when not available,
331ed7b5f09Sandi  // IIS sets it to 'off', 'false' and 'disabled' are just guessing
332ed7b5f09Sandi  if (preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){
333ed7b5f09Sandi    $proto = 'http://';
334e82e3526SAndreas Gohr    if ($port == '80') {
335ed7b5f09Sandi      $port='';
336ed7b5f09Sandi    }
337ed7b5f09Sandi  }else{
338ed7b5f09Sandi    $proto = 'https://';
339e82e3526SAndreas Gohr    if ($port == '443') {
340ed7b5f09Sandi      $port='';
341ed7b5f09Sandi    }
342ed7b5f09Sandi  }
343ed7b5f09Sandi
344e82e3526SAndreas Gohr  if($port) $port = ':'.$port;
345e82e3526SAndreas Gohr
346ed7b5f09Sandi  return $proto.$host.$port.$dir;
347ed7b5f09Sandi}
348ed7b5f09Sandi
349b000c6d4Sandi/**
350b000c6d4Sandi * Append a PHP extension to a given file and adds an exit call
351b000c6d4Sandi *
352b000c6d4Sandi * This is used to migrate some old configfiles. An added PHP extension
353b000c6d4Sandi * ensures the contents are not shown to webusers even if .htaccess files
354b000c6d4Sandi * do not work
355b000c6d4Sandi *
356b000c6d4Sandi * @author Jan Decaluwe <jan@jandecaluwe.com>
357b000c6d4Sandi */
3588c4f28e8Sjanfunction scriptify($file) {
3598c4f28e8Sjan  // checks
3608c4f28e8Sjan  if (!is_readable($file)) {
3618c4f28e8Sjan    return;
3628c4f28e8Sjan  }
3638c4f28e8Sjan  $fn = $file.'.php';
3648c4f28e8Sjan  if (@file_exists($fn)) {
3658c4f28e8Sjan    return;
3668c4f28e8Sjan  }
3678c4f28e8Sjan  $fh = fopen($fn, 'w');
3688c4f28e8Sjan  if (!$fh) {
3693816dcbcSAndreas Gohr    nice_die($fn.' is not writable. Check your permission settings!');
3708c4f28e8Sjan  }
3718c4f28e8Sjan  // write php exit hack first
3728c4f28e8Sjan  fwrite($fh, "# $fn\n");
3738c4f28e8Sjan  fwrite($fh, '# <?php exit()?>'."\n");
3748c4f28e8Sjan  fwrite($fh, "# Don't modify the lines above\n");
3758c4f28e8Sjan  fwrite($fh, "#\n");
3768c4f28e8Sjan  // copy existing lines
3778c4f28e8Sjan  $lines = file($file);
3788c4f28e8Sjan  foreach ($lines as $line){
3798c4f28e8Sjan    fwrite($fh, $line);
3808c4f28e8Sjan  }
3813ba793e2Sandi  fclose($fh);
382b000c6d4Sandi  //try to rename the old file
3833aee4c27SAndreas Gohr  io_rename($file,"$file.old");
3848c4f28e8Sjan}
3858c4f28e8Sjan
3863816dcbcSAndreas Gohr/**
3873816dcbcSAndreas Gohr * print a nice message even if no styles are loaded yet.
3883816dcbcSAndreas Gohr */
3893816dcbcSAndreas Gohrfunction nice_die($msg){
3903816dcbcSAndreas Gohr  echo<<<EOT
3913816dcbcSAndreas Gohr  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
3923816dcbcSAndreas Gohr   "http://www.w3.org/TR/html4/loose.dtd">
3933816dcbcSAndreas Gohr  <html>
3943816dcbcSAndreas Gohr    <head><title>DokuWiki Setup Error</title></head>
3953816dcbcSAndreas Gohr    <body style="font-family: Arial, sans-serif">
3963816dcbcSAndreas Gohr      <div style="width:60%; margin: auto; background-color: #fcc;
3973816dcbcSAndreas Gohr                  border: 1px solid #faa; padding: 0.5em 1em;">
3983816dcbcSAndreas Gohr      <h1 style="font-size: 120%">DokuWiki Setup Error</h1>
3993816dcbcSAndreas Gohr      <p>$msg</p>
4003816dcbcSAndreas Gohr      </div>
4013816dcbcSAndreas Gohr    </body>
4023816dcbcSAndreas Gohr  </html>
4033816dcbcSAndreas GohrEOT;
4043816dcbcSAndreas Gohr  exit;
4053816dcbcSAndreas Gohr}
4063816dcbcSAndreas Gohr
407ed7b5f09Sandi
40800976812SAndreas Gohr/**
40900976812SAndreas Gohr * A realpath() replacement
41000976812SAndreas Gohr *
41100976812SAndreas Gohr * This function behaves similar to PHP's realpath() but does not resolve
41200976812SAndreas Gohr * symlinks or accesses upper directories
41300976812SAndreas Gohr *
41400976812SAndreas Gohr * @author <richpageau at yahoo dot co dot uk>
41500976812SAndreas Gohr * @link   http://de3.php.net/manual/en/function.realpath.php#75992
41600976812SAndreas Gohr */
41700976812SAndreas Gohrfunction fullpath($path){
418ef38bfe8SAndreas Gohr    $iswin = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN');
419ef38bfe8SAndreas Gohr    if($iswin) $path = str_replace('\\','/',$path); // windows compatibility
42000976812SAndreas Gohr
421ef38bfe8SAndreas Gohr    // check if path begins with "/" or "c:" ie. is absolute
42200976812SAndreas Gohr    // if it isnt concat with script path
423ef38bfe8SAndreas Gohr    if ((!$iswin && $path{0} !== '/') ||
424ef38bfe8SAndreas Gohr        ($iswin && $path{1} !== ':')) {
42500976812SAndreas Gohr        $base=dirname($_SERVER['SCRIPT_FILENAME']);
42600976812SAndreas Gohr        $path=$base."/".$path;
42700976812SAndreas Gohr    }
42800976812SAndreas Gohr
42900976812SAndreas Gohr    // canonicalize
43000976812SAndreas Gohr    $path=explode('/', $path);
43100976812SAndreas Gohr    $newpath=array();
432ef38bfe8SAndreas Gohr    foreach($path as $p) {
433ef38bfe8SAndreas Gohr        if ($p === '' || $p === '.') continue;
434ef38bfe8SAndreas Gohr           if ($p==='..') {
43500976812SAndreas Gohr              array_pop($newpath);
43600976812SAndreas Gohr              continue;
43700976812SAndreas Gohr        }
438ef38bfe8SAndreas Gohr        array_push($newpath, $p);
43900976812SAndreas Gohr    }
440ef38bfe8SAndreas Gohr    $finalpath = implode('/', $newpath);
441ef38bfe8SAndreas Gohr    if(!$iswin) $finalpath = '/'.$finalpath;
44200976812SAndreas Gohr
44300976812SAndreas Gohr    // check then return valid path or filename
44400976812SAndreas Gohr    if (file_exists($finalpath)) {
44500976812SAndreas Gohr        return ($finalpath);
44600976812SAndreas Gohr    }
44700976812SAndreas Gohr    else return false;
44800976812SAndreas Gohr}
44900976812SAndreas Gohr
45000976812SAndreas Gohr
45100976812SAndreas Gohr
452340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
453