xref: /dokuwiki/inc/init.php (revision 71726d7801bdcbf41dfdc79d244f09a0988529c0)
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
14ed7b5f09Sandi  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(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
20bad905f1SBen 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
27*71726d78SBen Coburn  $cache_wikifn = array();
28*71726d78SBen Coburn  $cache_cleanid = array();
2950602150SBen Coburn
304724a577Sandi  //prepare config array()
31ee20e7d1Sandi  global $conf;
3203c4aec3Schris  if (!defined('DOKU_UNITTEST')) {
334724a577Sandi    $conf = array();
344724a577Sandi
35ad15db82Sandi    // load the config file(s)
36e7cb32dcSAndreas Gohr    require_once(DOKU_CONF.'dokuwiki.php');
370a6ead41SAndreas Gohr    if(@file_exists(DOKU_CONF.'local.php')){
380a6ead41SAndreas Gohr      require_once(DOKU_CONF.'local.php');
390a6ead41SAndreas Gohr    }
4003c4aec3Schris  }
41ad15db82Sandi
42ad15db82Sandi  //prepare language array
43ee20e7d1Sandi  global $lang;
44ad15db82Sandi  $lang = array();
45ed7b5f09Sandi
4616521111Sandi  //load the language files
47bc3b6aecSandi  require_once(DOKU_INC.'inc/lang/en/lang.php');
48f949a01cSAndreas Gohr  if ( $conf['lang'] && $conf['lang'] != 'en' ) {
49bc3b6aecSandi    require_once(DOKU_INC.'inc/lang/'.$conf['lang'].'/lang.php');
50fc1c55b1Shfuecks  }
5116521111Sandi
52ed7b5f09Sandi  // define baseURL
53ed7b5f09Sandi  if(!defined('DOKU_BASE')) define('DOKU_BASE',getBaseURL());
54ed7b5f09Sandi  if(!defined('DOKU_URL'))  define('DOKU_URL',getBaseURL(true));
55ed7b5f09Sandi
56ee20e7d1Sandi  // define Plugin dir
57f62ea8a1Sandi  if(!defined('DOKU_PLUGIN'))  define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
58ee20e7d1Sandi
59ed7b5f09Sandi  // define main script
60ed7b5f09Sandi  if(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php');
61ed7b5f09Sandi
626b13307fSandi  // define Template baseURL
636b13307fSandi  if(!defined('DOKU_TPL')) define('DOKU_TPL',
64f62ea8a1Sandi                                  DOKU_BASE.'lib/tpl/'.$conf['template'].'/');
656b13307fSandi
6678a6aeb1SAndreas Gohr  // define real Template directory
6778a6aeb1SAndreas Gohr  if(!defined('DOKU_TPLINC')) define('DOKU_TPLINC',
6878a6aeb1SAndreas Gohr                                  DOKU_INC.'lib/tpl/'.$conf['template'].'/');
6978a6aeb1SAndreas Gohr
70ed7b5f09Sandi  // make session rewrites XHTML compliant
713fc74836Sandi  @ini_set('arg_separator.output', '&amp;');
72ed7b5f09Sandi
733138b5c7SAndreas Gohr  // enable gzip compression
743138b5c7SAndreas Gohr  if ($conf['gzip_output'] &&
753138b5c7SAndreas Gohr      !defined('DOKU_DISABLE_GZIP_OUTPUT') &&
763138b5c7SAndreas Gohr      function_exists('ob_gzhandler') &&
773138b5c7SAndreas Gohr      preg_match('/gzip|deflate/', $_SERVER['HTTP_ACCEPT_ENCODING'])) {
783138b5c7SAndreas Gohr    ob_start('ob_gzhandler');
793138b5c7SAndreas Gohr  }
803138b5c7SAndreas Gohr
81ed7b5f09Sandi  // init session
826534245aSAndreas Gohr  if (!headers_sent() && !defined('NOSESSION')){
83ed7b5f09Sandi    session_name("DokuWiki");
84bad31ae9SAndreas Gohr    session_start();
85bad31ae9SAndreas Gohr  }
86ed7b5f09Sandi
87ed7b5f09Sandi  // kill magic quotes
88e55eb89cSAndreas Gohr  if (get_magic_quotes_gpc() && !defined('MAGIC_QUOTES_STRIPPED')) {
89ed7b5f09Sandi    if (!empty($_GET))    remove_magic_quotes($_GET);
90ed7b5f09Sandi    if (!empty($_POST))   remove_magic_quotes($_POST);
91ed7b5f09Sandi    if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE);
92ed7b5f09Sandi    if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST);
9356146e0dSAndreas Gohr#    if (!empty($_SESSION)) remove_magic_quotes($_SESSION); #FIXME needed ?
943fc74836Sandi    @ini_set('magic_quotes_gpc', 0);
95e55eb89cSAndreas Gohr    define('MAGIC_QUOTES_STRIPPED',1);
96ed7b5f09Sandi  }
973fc74836Sandi  @set_magic_quotes_runtime(0);
983fc74836Sandi  @ini_set('magic_quotes_sybase',0);
99ed7b5f09Sandi
100ed7b5f09Sandi  // disable gzip if not available
101fe893490SAndreas Gohr  if($conf['compression'] == 'bz' && !function_exists('bzopen')){
102fe893490SAndreas Gohr    $conf['compression'] = 'gz';
103501252a5SAndreas Gohr  }
104fe893490SAndreas Gohr  if($conf['compression'] == 'gz' && !function_exists('gzopen')){
105501252a5SAndreas Gohr    $conf['compression'] = 0;
106ed7b5f09Sandi  }
107ed7b5f09Sandi
1081ca31cfeSAndreas Gohr  // precalculate file creation modes
1091ca31cfeSAndreas Gohr  init_creationmodes();
110ed7b5f09Sandi
1113dc3a5f1Sandi  // make real paths and check them
11298407a7aSandi  init_paths();
1137367b368SAndreas Gohr  init_files();
114ed7b5f09Sandi
1158c4f28e8Sjan  // automatic upgrade to script versions of certain files
116e7cb32dcSAndreas Gohr  scriptify(DOKU_CONF.'users.auth');
117e7cb32dcSAndreas Gohr  scriptify(DOKU_CONF.'acl.auth');
118f62ea8a1Sandi
119f62ea8a1Sandi
120f62ea8a1Sandi/**
12198407a7aSandi * Checks paths from config file
12298407a7aSandi */
12398407a7aSandifunction init_paths(){
12498407a7aSandi  global $conf;
12598407a7aSandi
12698407a7aSandi  $paths = array('datadir'   => 'pages',
12798407a7aSandi                 'olddir'    => 'attic',
12898407a7aSandi                 'mediadir'  => 'media',
12998407a7aSandi                 'metadir'   => 'meta',
13098407a7aSandi                 'cachedir'  => 'cache',
131*71726d78SBen Coburn                 'lockdir'   => 'locks');
13298407a7aSandi
13398407a7aSandi  foreach($paths as $c => $p){
13498407a7aSandi    if(!$conf[$c])   $conf[$c] = $conf['savedir'].'/'.$p;
13598407a7aSandi    $conf[$c]        = init_path($conf[$c]);
1363816dcbcSAndreas Gohr    if(!$conf[$c])   nice_die("The $c does not exist, isn't accessable or writable.
13769dc3177SAndreas Gohr                               You should check your config and permission settings.
13869dc3177SAndreas Gohr                               Or maybe you want to <a href=\"install.php\">run the
13969dc3177SAndreas Gohr                               installer</a>?");
14098407a7aSandi  }
141*71726d78SBen Coburn
142*71726d78SBen Coburn  // path to old changelog only needed for upgrading
143*71726d78SBen Coburn  $conf['changelog_old'] = init_path((isset($conf['changelog']))?($conf['changelog']):($conf['savedir'].'/changes.log'));
144*71726d78SBen Coburn  if ($conf['changelog_old']=='') { unset($conf['changelog_old']); }
145*71726d78SBen Coburn  // hardcoded changelog because it is now a cache that lives in meta
146*71726d78SBen Coburn  $conf['changelog'] = $conf['metadir'].'/_dokuwiki.changes';
14798407a7aSandi}
14898407a7aSandi
14998407a7aSandi/**
1500d8850c4SAndreas Gohr * Checks the existance of certain files and creates them if missing.
1517367b368SAndreas Gohr */
1527367b368SAndreas Gohrfunction init_files(){
1537367b368SAndreas Gohr  global $conf;
1540d8850c4SAndreas Gohr
1557367b368SAndreas Gohr  $files = array( $conf['cachedir'].'/word.idx',
1567367b368SAndreas Gohr                  $conf['cachedir'].'/page.idx',
1570d8850c4SAndreas Gohr                  $conf['cachedir'].'/index.idx');
1587367b368SAndreas Gohr
1597367b368SAndreas Gohr  foreach($files as $file){
1607367b368SAndreas Gohr    if(!@file_exists($file)){
1610d8850c4SAndreas Gohr      $fh = @fopen($file,'a');
1620d8850c4SAndreas Gohr      if($fh){
1637367b368SAndreas Gohr        fclose($fh);
1641ca31cfeSAndreas Gohr        if($conf['fperm']) chmod($file, $conf['fperm']);
1650d8850c4SAndreas Gohr      }else{
1663816dcbcSAndreas Gohr        nice_die("$file is not writable. Check your permissions settings!");
1670d8850c4SAndreas Gohr      }
1687367b368SAndreas Gohr    }
1697367b368SAndreas Gohr  }
1707367b368SAndreas Gohr}
1717367b368SAndreas Gohr
1727367b368SAndreas Gohr/**
1730d8850c4SAndreas Gohr * Returns absolute path
174f62ea8a1Sandi *
1750d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC.
1760d8850c4SAndreas Gohr * Check for accessability on directories as well.
1770d8850c4SAndreas Gohr *
1780d8850c4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
179f62ea8a1Sandi */
180f62ea8a1Sandifunction init_path($path){
1810d8850c4SAndreas Gohr  // check existance
182f62ea8a1Sandi  $p = realpath($path);
1830d8850c4SAndreas Gohr  if(!@file_exists($p)){
184f62ea8a1Sandi    $p = realpath(DOKU_INC.$path);
1850d8850c4SAndreas Gohr    if(!@file_exists($p)){
1868fc4e739Sandi      return '';
187f62ea8a1Sandi    }
1880d8850c4SAndreas Gohr  }
1890d8850c4SAndreas Gohr
1900d8850c4SAndreas Gohr  // check writability
1910d8850c4SAndreas Gohr  if(!@is_writable($p)){
1920d8850c4SAndreas Gohr    return '';
1930d8850c4SAndreas Gohr  }
1940d8850c4SAndreas Gohr
1950d8850c4SAndreas Gohr  // check accessability (execute bit) for directories
1960d8850c4SAndreas Gohr  if(@is_dir($p) && !@file_exists("$p/.")){
1970d8850c4SAndreas Gohr    return '';
1980d8850c4SAndreas Gohr  }
1990d8850c4SAndreas Gohr
2000d8850c4SAndreas Gohr  return $p;
2010d8850c4SAndreas Gohr}
2028c4f28e8Sjan
203ed7b5f09Sandi/**
2041ca31cfeSAndreas Gohr * Sets the internal config values fperm and dperm which, when set,
2051ca31cfeSAndreas Gohr * will be used to change the permission of a newly created dir or
2061ca31cfeSAndreas Gohr * file with chmod. Considers the influence of the system's umask
2071ca31cfeSAndreas Gohr * setting the values only if needed.
2081ca31cfeSAndreas Gohr */
2091ca31cfeSAndreas Gohrfunction init_creationmodes(){
2101ca31cfeSAndreas Gohr  global $conf;
2111ca31cfeSAndreas Gohr
2121ca31cfeSAndreas Gohr  // Legacy support for old umask/dmask scheme
2131ca31cfeSAndreas Gohr  unset($conf['dmask']);
2141ca31cfeSAndreas Gohr  unset($conf['fmask']);
2151ca31cfeSAndreas Gohr  unset($conf['umask']);
2161ca31cfeSAndreas Gohr  unset($conf['fperm']);
2171ca31cfeSAndreas Gohr  unset($conf['dperm']);
2181ca31cfeSAndreas Gohr
2199f3cdec3SAndreas Gohr  // get system umask, fallback to 0 if none available
2209f3cdec3SAndreas Gohr  $umask = @umask();
2219f3cdec3SAndreas Gohr  if(!$umask) $umask = 0000;
2221ca31cfeSAndreas Gohr
2231ca31cfeSAndreas Gohr  // check what is set automatically by the system on file creation
2241ca31cfeSAndreas Gohr  // and set the fperm param if it's not what we want
2251ca31cfeSAndreas Gohr  $auto_fmode = 0666 & ~$umask;
2261ca31cfeSAndreas Gohr  if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode'];
2271ca31cfeSAndreas Gohr
2281ca31cfeSAndreas Gohr  // check what is set automatically by the system on file creation
2291ca31cfeSAndreas Gohr  // and set the dperm param if it's not what we want
2301ca31cfeSAndreas Gohr  $auto_dmode = $conf['dmode'] & ~$umask;
2311ca31cfeSAndreas Gohr  if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode'];
2321ca31cfeSAndreas Gohr}
2331ca31cfeSAndreas Gohr
2341ca31cfeSAndreas Gohr/**
235ed7b5f09Sandi * remove magic quotes recursivly
236ed7b5f09Sandi *
237ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
238ed7b5f09Sandi */
239ed7b5f09Sandifunction remove_magic_quotes(&$array) {
240ed7b5f09Sandi  foreach (array_keys($array) as $key) {
241ed7b5f09Sandi    if (is_array($array[$key])) {
242ed7b5f09Sandi      remove_magic_quotes($array[$key]);
243ed7b5f09Sandi    }else {
244ed7b5f09Sandi      $array[$key] = stripslashes($array[$key]);
245ed7b5f09Sandi    }
246ed7b5f09Sandi  }
247ed7b5f09Sandi}
248ed7b5f09Sandi
249ed7b5f09Sandi/**
250ed7b5f09Sandi * Returns the full absolute URL to the directory where
251ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash)
252ed7b5f09Sandi *
253ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
254ed7b5f09Sandi */
255ed7b5f09Sandifunction getBaseURL($abs=false){
256ed7b5f09Sandi  global $conf;
257ed7b5f09Sandi  //if canonical url enabled always return absolute
258ed7b5f09Sandi  if($conf['canonical']) $abs = true;
259ed7b5f09Sandi
26092b83b77Sandi  if($conf['basedir']){
261919eeb46Sandi    $dir = $conf['basedir'].'/';
26289aa05dbSAndreas Gohr  }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){
263bdc127a4Sandi    $dir = dirname($_SERVER['SCRIPT_NAME']).'/';
26489aa05dbSAndreas Gohr  }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){
26589aa05dbSAndreas Gohr    $dir = dirname($_SERVER['PHP_SELF']).'/';
266093ec9e4Sandi  }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
267093ec9e4Sandi    $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
268093ec9e4Sandi                         $_SERVER['SCRIPT_FILENAME']);
269093ec9e4Sandi    $dir = dirname('/'.$dir).'/';
27092b83b77Sandi  }else{
27189aa05dbSAndreas Gohr    $dir = './'; //probably wrong
27292b83b77Sandi  }
273ed7b5f09Sandi
274ed7b5f09Sandi  $dir = str_replace('\\','/',$dir); #bugfix for weird WIN behaviour
275ed7b5f09Sandi  $dir = preg_replace('#//+#','/',$dir);
276ed7b5f09Sandi
277f62ea8a1Sandi  //handle script in lib/exe dir
278f62ea8a1Sandi  $dir = preg_replace('!lib/exe/$!','',$dir);
279f62ea8a1Sandi
280488d5fa0SMichael Klier chi@chimeric.de  //handle script in lib/plugins dir
281488d5fa0SMichael Klier chi@chimeric.de  $dir = preg_replace('!lib/plugins/.*$!','',$dir);
282488d5fa0SMichael Klier chi@chimeric.de
283ed7b5f09Sandi  //finish here for relative URLs
284ed7b5f09Sandi  if(!$abs) return $dir;
285ed7b5f09Sandi
286ef7b3ecdSAndreas Gohr  //use config option if available
287ef7b3ecdSAndreas Gohr  if($conf['baseurl']) return $conf['baseurl'].$dir;
288ef7b3ecdSAndreas Gohr
289e82e3526SAndreas Gohr  //split hostheader into host and port
290e82e3526SAndreas Gohr  list($host,$port) = explode(':',$_SERVER['HTTP_HOST']);
291e82e3526SAndreas Gohr  if(!$port)  $port = $_SERVER['SERVER_PORT'];
292e82e3526SAndreas Gohr  if(!$port)  $port = 80;
293ed7b5f09Sandi
294ed7b5f09Sandi  // see if HTTPS is enabled - apache leaves this empty when not available,
295ed7b5f09Sandi  // IIS sets it to 'off', 'false' and 'disabled' are just guessing
296ed7b5f09Sandi  if (preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){
297ed7b5f09Sandi    $proto = 'http://';
298e82e3526SAndreas Gohr    if ($port == '80') {
299ed7b5f09Sandi      $port='';
300ed7b5f09Sandi    }
301ed7b5f09Sandi  }else{
302ed7b5f09Sandi    $proto = 'https://';
303e82e3526SAndreas Gohr    if ($port == '443') {
304ed7b5f09Sandi      $port='';
305ed7b5f09Sandi    }
306ed7b5f09Sandi  }
307ed7b5f09Sandi
308e82e3526SAndreas Gohr  if($port) $port = ':'.$port;
309e82e3526SAndreas Gohr
310ed7b5f09Sandi  return $proto.$host.$port.$dir;
311ed7b5f09Sandi}
312ed7b5f09Sandi
313b000c6d4Sandi/**
314b000c6d4Sandi * Append a PHP extension to a given file and adds an exit call
315b000c6d4Sandi *
316b000c6d4Sandi * This is used to migrate some old configfiles. An added PHP extension
317b000c6d4Sandi * ensures the contents are not shown to webusers even if .htaccess files
318b000c6d4Sandi * do not work
319b000c6d4Sandi *
320b000c6d4Sandi * @author Jan Decaluwe <jan@jandecaluwe.com>
321b000c6d4Sandi */
3228c4f28e8Sjanfunction scriptify($file) {
3238c4f28e8Sjan  // checks
3248c4f28e8Sjan  if (!is_readable($file)) {
3258c4f28e8Sjan    return;
3268c4f28e8Sjan  }
3278c4f28e8Sjan  $fn = $file.'.php';
3288c4f28e8Sjan  if (@file_exists($fn)) {
3298c4f28e8Sjan    return;
3308c4f28e8Sjan  }
3318c4f28e8Sjan  $fh = fopen($fn, 'w');
3328c4f28e8Sjan  if (!$fh) {
3333816dcbcSAndreas Gohr    nice_die($fn.' is not writable. Check your permission settings!');
3348c4f28e8Sjan  }
3358c4f28e8Sjan  // write php exit hack first
3368c4f28e8Sjan  fwrite($fh, "# $fn\n");
3378c4f28e8Sjan  fwrite($fh, '# <?php exit()?>'."\n");
3388c4f28e8Sjan  fwrite($fh, "# Don't modify the lines above\n");
3398c4f28e8Sjan  fwrite($fh, "#\n");
3408c4f28e8Sjan  // copy existing lines
3418c4f28e8Sjan  $lines = file($file);
3428c4f28e8Sjan  foreach ($lines as $line){
3438c4f28e8Sjan    fwrite($fh, $line);
3448c4f28e8Sjan  }
3453ba793e2Sandi  fclose($fh);
346b000c6d4Sandi  //try to rename the old file
3473aee4c27SAndreas Gohr  io_rename($file,"$file.old");
3488c4f28e8Sjan}
3498c4f28e8Sjan
3503816dcbcSAndreas Gohr/**
3513816dcbcSAndreas Gohr * print a nice message even if no styles are loaded yet.
3523816dcbcSAndreas Gohr */
3533816dcbcSAndreas Gohrfunction nice_die($msg){
3543816dcbcSAndreas Gohr  echo<<<EOT
3553816dcbcSAndreas Gohr  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
3563816dcbcSAndreas Gohr   "http://www.w3.org/TR/html4/loose.dtd">
3573816dcbcSAndreas Gohr  <html>
3583816dcbcSAndreas Gohr    <head><title>DokuWiki Setup Error</title></head>
3593816dcbcSAndreas Gohr    <body style="font-family: Arial, sans-serif">
3603816dcbcSAndreas Gohr      <div style="width:60%; margin: auto; background-color: #fcc;
3613816dcbcSAndreas Gohr                  border: 1px solid #faa; padding: 0.5em 1em;">
3623816dcbcSAndreas Gohr      <h1 style="font-size: 120%">DokuWiki Setup Error</h1>
3633816dcbcSAndreas Gohr      <p>$msg</p>
3643816dcbcSAndreas Gohr      </div>
3653816dcbcSAndreas Gohr    </body>
3663816dcbcSAndreas Gohr  </html>
3673816dcbcSAndreas GohrEOT;
3683816dcbcSAndreas Gohr  exit;
3693816dcbcSAndreas Gohr}
3703816dcbcSAndreas Gohr
371ed7b5f09Sandi
372340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
373