xref: /dokuwiki/inc/init.php (revision 3138b5c7607b434fa9b1b8c563ad461985d5b5c8)
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
19c53ea5f2Sandi  // set up error reporting to sane values
20c53ea5f2Sandi  error_reporting(E_ALL ^ E_NOTICE);
21c53ea5f2Sandi
224724a577Sandi  //prepare config array()
23ee20e7d1Sandi  global $conf;
244724a577Sandi  $conf = array();
254724a577Sandi
26ad15db82Sandi  // load the config file(s)
27e7cb32dcSAndreas Gohr  require_once(DOKU_CONF.'dokuwiki.php');
280a6ead41SAndreas Gohr  if(@file_exists(DOKU_CONF.'local.php')){
290a6ead41SAndreas Gohr    require_once(DOKU_CONF.'local.php');
300a6ead41SAndreas Gohr  }
31ad15db82Sandi
32ad15db82Sandi  //prepare language array
33ee20e7d1Sandi  global $lang;
34ad15db82Sandi  $lang = array();
35ed7b5f09Sandi
3616521111Sandi  //load the language files
37bc3b6aecSandi  require_once(DOKU_INC.'inc/lang/en/lang.php');
38f949a01cSAndreas Gohr  if ( $conf['lang'] && $conf['lang'] != 'en' ) {
39bc3b6aecSandi    require_once(DOKU_INC.'inc/lang/'.$conf['lang'].'/lang.php');
40fc1c55b1Shfuecks  }
4116521111Sandi
42ed7b5f09Sandi  // define baseURL
43ed7b5f09Sandi  if(!defined('DOKU_BASE')) define('DOKU_BASE',getBaseURL());
44ed7b5f09Sandi  if(!defined('DOKU_URL'))  define('DOKU_URL',getBaseURL(true));
45ed7b5f09Sandi
46ee20e7d1Sandi  // define Plugin dir
47f62ea8a1Sandi  if(!defined('DOKU_PLUGIN'))  define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
48ee20e7d1Sandi
49ed7b5f09Sandi  // define main script
50ed7b5f09Sandi  if(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php');
51ed7b5f09Sandi
526b13307fSandi  // define Template baseURL
536b13307fSandi  if(!defined('DOKU_TPL')) define('DOKU_TPL',
54f62ea8a1Sandi                                  DOKU_BASE.'lib/tpl/'.$conf['template'].'/');
556b13307fSandi
5678a6aeb1SAndreas Gohr  // define real Template directory
5778a6aeb1SAndreas Gohr  if(!defined('DOKU_TPLINC')) define('DOKU_TPLINC',
5878a6aeb1SAndreas Gohr                                  DOKU_INC.'lib/tpl/'.$conf['template'].'/');
5978a6aeb1SAndreas Gohr
60ed7b5f09Sandi  // make session rewrites XHTML compliant
613fc74836Sandi  @ini_set('arg_separator.output', '&amp;');
62ed7b5f09Sandi
63*3138b5c7SAndreas Gohr  // enable gzip compression
64*3138b5c7SAndreas Gohr  if ($conf['gzip_output'] &&
65*3138b5c7SAndreas Gohr      !defined('DOKU_DISABLE_GZIP_OUTPUT') &&
66*3138b5c7SAndreas Gohr      function_exists('ob_gzhandler') &&
67*3138b5c7SAndreas Gohr      preg_match('/gzip|deflate/', $_SERVER['HTTP_ACCEPT_ENCODING'])) {
68*3138b5c7SAndreas Gohr    ob_start('ob_gzhandler');
69*3138b5c7SAndreas Gohr  }
70*3138b5c7SAndreas Gohr
71ed7b5f09Sandi  // init session
726534245aSAndreas Gohr  if (!headers_sent() && !defined('NOSESSION')){
73ed7b5f09Sandi    session_name("DokuWiki");
74bad31ae9SAndreas Gohr    session_start();
75bad31ae9SAndreas Gohr  }
76ed7b5f09Sandi
77ed7b5f09Sandi  // kill magic quotes
78e55eb89cSAndreas Gohr  if (get_magic_quotes_gpc() && !defined('MAGIC_QUOTES_STRIPPED')) {
79ed7b5f09Sandi    if (!empty($_GET))    remove_magic_quotes($_GET);
80ed7b5f09Sandi    if (!empty($_POST))   remove_magic_quotes($_POST);
81ed7b5f09Sandi    if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE);
82ed7b5f09Sandi    if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST);
8356146e0dSAndreas Gohr#    if (!empty($_SESSION)) remove_magic_quotes($_SESSION); #FIXME needed ?
843fc74836Sandi    @ini_set('magic_quotes_gpc', 0);
85e55eb89cSAndreas Gohr    define('MAGIC_QUOTES_STRIPPED',1);
86ed7b5f09Sandi  }
873fc74836Sandi  @set_magic_quotes_runtime(0);
883fc74836Sandi  @ini_set('magic_quotes_sybase',0);
89ed7b5f09Sandi
90ed7b5f09Sandi  // disable gzip if not available
91ed7b5f09Sandi  if($conf['usegzip'] && !function_exists('gzopen')){
92ed7b5f09Sandi    $conf['usegzip'] = 0;
93ed7b5f09Sandi  }
94ed7b5f09Sandi
951ca31cfeSAndreas Gohr  // precalculate file creation modes
961ca31cfeSAndreas Gohr  init_creationmodes();
97ed7b5f09Sandi
983dc3a5f1Sandi  // make real paths and check them
9998407a7aSandi  init_paths();
1007367b368SAndreas Gohr  init_files();
101ed7b5f09Sandi
1028c4f28e8Sjan  // automatic upgrade to script versions of certain files
103e7cb32dcSAndreas Gohr  scriptify(DOKU_CONF.'users.auth');
104e7cb32dcSAndreas Gohr  scriptify(DOKU_CONF.'acl.auth');
105f62ea8a1Sandi
106f62ea8a1Sandi
107f62ea8a1Sandi/**
10898407a7aSandi * Checks paths from config file
10998407a7aSandi */
11098407a7aSandifunction init_paths(){
11198407a7aSandi  global $conf;
11298407a7aSandi
11398407a7aSandi  $paths = array('datadir'   => 'pages',
11498407a7aSandi                 'olddir'    => 'attic',
11598407a7aSandi                 'mediadir'  => 'media',
11698407a7aSandi                 'metadir'   => 'meta',
11798407a7aSandi                 'cachedir'  => 'cache',
11898407a7aSandi                 'lockdir'   => 'locks',
11998407a7aSandi                 'changelog' => 'changes.log');
12098407a7aSandi
12198407a7aSandi  foreach($paths as $c => $p){
12298407a7aSandi    if(!$conf[$c])   $conf[$c] = $conf['savedir'].'/'.$p;
12398407a7aSandi    $conf[$c]        = init_path($conf[$c]);
1243816dcbcSAndreas Gohr    if(!$conf[$c])   nice_die("The $c does not exist, isn't accessable or writable.
1253816dcbcSAndreas Gohr                               Check your config and permission settings!");
12698407a7aSandi  }
12798407a7aSandi}
12898407a7aSandi
12998407a7aSandi/**
1300d8850c4SAndreas Gohr * Checks the existance of certain files and creates them if missing.
1317367b368SAndreas Gohr */
1327367b368SAndreas Gohrfunction init_files(){
1337367b368SAndreas Gohr  global $conf;
1340d8850c4SAndreas Gohr
1357367b368SAndreas Gohr  $files = array( $conf['cachedir'].'/word.idx',
1367367b368SAndreas Gohr                  $conf['cachedir'].'/page.idx',
1370d8850c4SAndreas Gohr                  $conf['cachedir'].'/index.idx');
1387367b368SAndreas Gohr
1397367b368SAndreas Gohr  foreach($files as $file){
1407367b368SAndreas Gohr    if(!@file_exists($file)){
1410d8850c4SAndreas Gohr      $fh = @fopen($file,'a');
1420d8850c4SAndreas Gohr      if($fh){
1437367b368SAndreas Gohr        fclose($fh);
1441ca31cfeSAndreas Gohr        if($conf['fperm']) chmod($file, $conf['fperm']);
1450d8850c4SAndreas Gohr      }else{
1463816dcbcSAndreas Gohr        nice_die("$file is not writable. Check your permissions settings!");
1470d8850c4SAndreas Gohr      }
1487367b368SAndreas Gohr    }
1497367b368SAndreas Gohr  }
1507367b368SAndreas Gohr}
1517367b368SAndreas Gohr
1527367b368SAndreas Gohr/**
1530d8850c4SAndreas Gohr * Returns absolute path
154f62ea8a1Sandi *
1550d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC.
1560d8850c4SAndreas Gohr * Check for accessability on directories as well.
1570d8850c4SAndreas Gohr *
1580d8850c4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
159f62ea8a1Sandi */
160f62ea8a1Sandifunction init_path($path){
1610d8850c4SAndreas Gohr  // check existance
162f62ea8a1Sandi  $p = realpath($path);
1630d8850c4SAndreas Gohr  if(!@file_exists($p)){
164f62ea8a1Sandi    $p = realpath(DOKU_INC.$path);
1650d8850c4SAndreas Gohr    if(!@file_exists($p)){
1668fc4e739Sandi      return '';
167f62ea8a1Sandi    }
1680d8850c4SAndreas Gohr  }
1690d8850c4SAndreas Gohr
1700d8850c4SAndreas Gohr  // check writability
1710d8850c4SAndreas Gohr  if(!@is_writable($p)){
1720d8850c4SAndreas Gohr    return '';
1730d8850c4SAndreas Gohr  }
1740d8850c4SAndreas Gohr
1750d8850c4SAndreas Gohr  // check accessability (execute bit) for directories
1760d8850c4SAndreas Gohr  if(@is_dir($p) && !@file_exists("$p/.")){
1770d8850c4SAndreas Gohr    return '';
1780d8850c4SAndreas Gohr  }
1790d8850c4SAndreas Gohr
1800d8850c4SAndreas Gohr  return $p;
1810d8850c4SAndreas Gohr}
1828c4f28e8Sjan
183ed7b5f09Sandi/**
1841ca31cfeSAndreas Gohr * Sets the internal config values fperm and dperm which, when set,
1851ca31cfeSAndreas Gohr * will be used to change the permission of a newly created dir or
1861ca31cfeSAndreas Gohr * file with chmod. Considers the influence of the system's umask
1871ca31cfeSAndreas Gohr * setting the values only if needed.
1881ca31cfeSAndreas Gohr */
1891ca31cfeSAndreas Gohrfunction init_creationmodes(){
1901ca31cfeSAndreas Gohr  global $conf;
1911ca31cfeSAndreas Gohr
1921ca31cfeSAndreas Gohr  // Legacy support for old umask/dmask scheme
1931ca31cfeSAndreas Gohr  unset($conf['dmask']);
1941ca31cfeSAndreas Gohr  unset($conf['fmask']);
1951ca31cfeSAndreas Gohr  unset($conf['umask']);
1961ca31cfeSAndreas Gohr  unset($conf['fperm']);
1971ca31cfeSAndreas Gohr  unset($conf['dperm']);
1981ca31cfeSAndreas Gohr
1999f3cdec3SAndreas Gohr  // get system umask, fallback to 0 if none available
2009f3cdec3SAndreas Gohr  $umask = @umask();
2019f3cdec3SAndreas Gohr  if(!$umask) $umask = 0000;
2021ca31cfeSAndreas Gohr
2031ca31cfeSAndreas Gohr  // check what is set automatically by the system on file creation
2041ca31cfeSAndreas Gohr  // and set the fperm param if it's not what we want
2051ca31cfeSAndreas Gohr  $auto_fmode = 0666 & ~$umask;
2061ca31cfeSAndreas Gohr  if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode'];
2071ca31cfeSAndreas Gohr
2081ca31cfeSAndreas Gohr  // check what is set automatically by the system on file creation
2091ca31cfeSAndreas Gohr  // and set the dperm param if it's not what we want
2101ca31cfeSAndreas Gohr  $auto_dmode = $conf['dmode'] & ~$umask;
2111ca31cfeSAndreas Gohr  if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode'];
2121ca31cfeSAndreas Gohr}
2131ca31cfeSAndreas Gohr
2141ca31cfeSAndreas Gohr/**
215ed7b5f09Sandi * remove magic quotes recursivly
216ed7b5f09Sandi *
217ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
218ed7b5f09Sandi */
219ed7b5f09Sandifunction remove_magic_quotes(&$array) {
220ed7b5f09Sandi  foreach (array_keys($array) as $key) {
221ed7b5f09Sandi    if (is_array($array[$key])) {
222ed7b5f09Sandi      remove_magic_quotes($array[$key]);
223ed7b5f09Sandi    }else {
224ed7b5f09Sandi      $array[$key] = stripslashes($array[$key]);
225ed7b5f09Sandi    }
226ed7b5f09Sandi  }
227ed7b5f09Sandi}
228ed7b5f09Sandi
229ed7b5f09Sandi/**
230ed7b5f09Sandi * Returns the full absolute URL to the directory where
231ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash)
232ed7b5f09Sandi *
233ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
234ed7b5f09Sandi */
235ed7b5f09Sandifunction getBaseURL($abs=false){
236ed7b5f09Sandi  global $conf;
237ed7b5f09Sandi  //if canonical url enabled always return absolute
238ed7b5f09Sandi  if($conf['canonical']) $abs = true;
239ed7b5f09Sandi
24092b83b77Sandi  if($conf['basedir']){
241919eeb46Sandi    $dir = $conf['basedir'].'/';
24289aa05dbSAndreas Gohr  }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){
243bdc127a4Sandi    $dir = dirname($_SERVER['SCRIPT_NAME']).'/';
24489aa05dbSAndreas Gohr  }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){
24589aa05dbSAndreas Gohr    $dir = dirname($_SERVER['PHP_SELF']).'/';
246093ec9e4Sandi  }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
247093ec9e4Sandi    $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
248093ec9e4Sandi                         $_SERVER['SCRIPT_FILENAME']);
249093ec9e4Sandi    $dir = dirname('/'.$dir).'/';
25092b83b77Sandi  }else{
25189aa05dbSAndreas Gohr    $dir = './'; //probably wrong
25292b83b77Sandi  }
253ed7b5f09Sandi
254ed7b5f09Sandi  $dir = str_replace('\\','/',$dir); #bugfix for weird WIN behaviour
255ed7b5f09Sandi  $dir = preg_replace('#//+#','/',$dir);
256ed7b5f09Sandi
257f62ea8a1Sandi  //handle script in lib/exe dir
258f62ea8a1Sandi  $dir = preg_replace('!lib/exe/$!','',$dir);
259f62ea8a1Sandi
260ed7b5f09Sandi  //finish here for relative URLs
261ed7b5f09Sandi  if(!$abs) return $dir;
262ed7b5f09Sandi
263ef7b3ecdSAndreas Gohr  //use config option if available
264ef7b3ecdSAndreas Gohr  if($conf['baseurl']) return $conf['baseurl'].$dir;
265ef7b3ecdSAndreas Gohr
266e82e3526SAndreas Gohr  //split hostheader into host and port
267e82e3526SAndreas Gohr  list($host,$port) = explode(':',$_SERVER['HTTP_HOST']);
268e82e3526SAndreas Gohr  if(!$port)  $port = $_SERVER['SERVER_PORT'];
269e82e3526SAndreas Gohr  if(!$port)  $port = 80;
270ed7b5f09Sandi
271ed7b5f09Sandi  // see if HTTPS is enabled - apache leaves this empty when not available,
272ed7b5f09Sandi  // IIS sets it to 'off', 'false' and 'disabled' are just guessing
273ed7b5f09Sandi  if (preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){
274ed7b5f09Sandi    $proto = 'http://';
275e82e3526SAndreas Gohr    if ($port == '80') {
276ed7b5f09Sandi      $port='';
277ed7b5f09Sandi    }
278ed7b5f09Sandi  }else{
279ed7b5f09Sandi    $proto = 'https://';
280e82e3526SAndreas Gohr    if ($port == '443') {
281ed7b5f09Sandi      $port='';
282ed7b5f09Sandi    }
283ed7b5f09Sandi  }
284ed7b5f09Sandi
285e82e3526SAndreas Gohr  if($port) $port = ':'.$port;
286e82e3526SAndreas Gohr
287ed7b5f09Sandi  return $proto.$host.$port.$dir;
288ed7b5f09Sandi}
289ed7b5f09Sandi
290b000c6d4Sandi/**
291b000c6d4Sandi * Append a PHP extension to a given file and adds an exit call
292b000c6d4Sandi *
293b000c6d4Sandi * This is used to migrate some old configfiles. An added PHP extension
294b000c6d4Sandi * ensures the contents are not shown to webusers even if .htaccess files
295b000c6d4Sandi * do not work
296b000c6d4Sandi *
297b000c6d4Sandi * @author Jan Decaluwe <jan@jandecaluwe.com>
298b000c6d4Sandi */
2998c4f28e8Sjanfunction scriptify($file) {
3008c4f28e8Sjan  // checks
3018c4f28e8Sjan  if (!is_readable($file)) {
3028c4f28e8Sjan    return;
3038c4f28e8Sjan  }
3048c4f28e8Sjan  $fn = $file.'.php';
3058c4f28e8Sjan  if (@file_exists($fn)) {
3068c4f28e8Sjan    return;
3078c4f28e8Sjan  }
3088c4f28e8Sjan  $fh = fopen($fn, 'w');
3098c4f28e8Sjan  if (!$fh) {
3103816dcbcSAndreas Gohr    nice_die($fn.' is not writable. Check your permission settings!');
3118c4f28e8Sjan  }
3128c4f28e8Sjan  // write php exit hack first
3138c4f28e8Sjan  fwrite($fh, "# $fn\n");
3148c4f28e8Sjan  fwrite($fh, '# <?php exit()?>'."\n");
3158c4f28e8Sjan  fwrite($fh, "# Don't modify the lines above\n");
3168c4f28e8Sjan  fwrite($fh, "#\n");
3178c4f28e8Sjan  // copy existing lines
3188c4f28e8Sjan  $lines = file($file);
3198c4f28e8Sjan  foreach ($lines as $line){
3208c4f28e8Sjan    fwrite($fh, $line);
3218c4f28e8Sjan  }
3223ba793e2Sandi  fclose($fh);
323b000c6d4Sandi  //try to rename the old file
3243aee4c27SAndreas Gohr  io_rename($file,"$file.old");
3258c4f28e8Sjan}
3268c4f28e8Sjan
3273816dcbcSAndreas Gohr/**
3283816dcbcSAndreas Gohr * print a nice message even if no styles are loaded yet.
3293816dcbcSAndreas Gohr */
3303816dcbcSAndreas Gohrfunction nice_die($msg){
3313816dcbcSAndreas Gohr  echo<<<EOT
3323816dcbcSAndreas Gohr  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
3333816dcbcSAndreas Gohr   "http://www.w3.org/TR/html4/loose.dtd">
3343816dcbcSAndreas Gohr  <html>
3353816dcbcSAndreas Gohr    <head><title>DokuWiki Setup Error</title></head>
3363816dcbcSAndreas Gohr    <body style="font-family: Arial, sans-serif">
3373816dcbcSAndreas Gohr      <div style="width:60%; margin: auto; background-color: #fcc;
3383816dcbcSAndreas Gohr                  border: 1px solid #faa; padding: 0.5em 1em;">
3393816dcbcSAndreas Gohr      <h1 style="font-size: 120%">DokuWiki Setup Error</h1>
3403816dcbcSAndreas Gohr      <p>$msg</p>
3413816dcbcSAndreas Gohr      </div>
3423816dcbcSAndreas Gohr    </body>
3433816dcbcSAndreas Gohr  </html>
3443816dcbcSAndreas GohrEOT;
3453816dcbcSAndreas Gohr  exit;
3463816dcbcSAndreas Gohr}
3473816dcbcSAndreas Gohr
348ed7b5f09Sandi
349340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
350