xref: /dokuwiki/inc/init.php (revision 56146e0d4b3dcb7513c647f22d147496aed617b7)
1ed7b5f09Sandi<?php
2ed7b5f09Sandi/**
3ed7b5f09Sandi * Initialize some defaults needed for DokuWiki
4ed7b5f09Sandi */
5ed7b5f09Sandi
6ed7b5f09Sandi  // define the include path
7ed7b5f09Sandi  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
8ad15db82Sandi
9e7cb32dcSAndreas Gohr  // define config path (packagers may want to change this to /etc/dokuwiki/)
10b7551a6dSEsther Brunner  if(!defined('DOKU_CONF')) define('DOKU_CONF',DOKU_INC.'conf/');
11e7cb32dcSAndreas Gohr
12c53ea5f2Sandi  // set up error reporting to sane values
13c53ea5f2Sandi  error_reporting(E_ALL ^ E_NOTICE);
14c53ea5f2Sandi
154724a577Sandi  //prepare config array()
16ee20e7d1Sandi  global $conf;
174724a577Sandi  $conf = array();
184724a577Sandi
19ad15db82Sandi  // load the config file(s)
20e7cb32dcSAndreas Gohr  require_once(DOKU_CONF.'dokuwiki.php');
210a6ead41SAndreas Gohr  if(@file_exists(DOKU_CONF.'local.php')){
220a6ead41SAndreas Gohr    require_once(DOKU_CONF.'local.php');
230a6ead41SAndreas Gohr  }
24ad15db82Sandi
25ad15db82Sandi  //prepare language array
26ee20e7d1Sandi  global $lang;
27ad15db82Sandi  $lang = array();
28ed7b5f09Sandi
2916521111Sandi  //load the language files
30bc3b6aecSandi  require_once(DOKU_INC.'inc/lang/en/lang.php');
31f949a01cSAndreas Gohr  if ( $conf['lang'] && $conf['lang'] != 'en' ) {
32bc3b6aecSandi    require_once(DOKU_INC.'inc/lang/'.$conf['lang'].'/lang.php');
33fc1c55b1Shfuecks  }
3416521111Sandi
35ed7b5f09Sandi  // define baseURL
36ed7b5f09Sandi  if(!defined('DOKU_BASE')) define('DOKU_BASE',getBaseURL());
37ed7b5f09Sandi  if(!defined('DOKU_URL'))  define('DOKU_URL',getBaseURL(true));
38ed7b5f09Sandi
39ee20e7d1Sandi  // define Plugin dir
40f62ea8a1Sandi  if(!defined('DOKU_PLUGIN'))  define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
41ee20e7d1Sandi
42ed7b5f09Sandi  // define main script
43ed7b5f09Sandi  if(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php');
44ed7b5f09Sandi
456b13307fSandi  // define Template baseURL
466b13307fSandi  if(!defined('DOKU_TPL')) define('DOKU_TPL',
47f62ea8a1Sandi                                  DOKU_BASE.'lib/tpl/'.$conf['template'].'/');
486b13307fSandi
4978a6aeb1SAndreas Gohr  // define real Template directory
5078a6aeb1SAndreas Gohr  if(!defined('DOKU_TPLINC')) define('DOKU_TPLINC',
5178a6aeb1SAndreas Gohr                                  DOKU_INC.'lib/tpl/'.$conf['template'].'/');
5278a6aeb1SAndreas Gohr
53ed7b5f09Sandi  // make session rewrites XHTML compliant
543fc74836Sandi  @ini_set('arg_separator.output', '&amp;');
55ed7b5f09Sandi
56ed7b5f09Sandi  // init session
576534245aSAndreas Gohr  if (!headers_sent() && !defined('NOSESSION')){
58ed7b5f09Sandi    session_name("DokuWiki");
59bad31ae9SAndreas Gohr    session_start();
60bad31ae9SAndreas Gohr  }
61ed7b5f09Sandi
62ed7b5f09Sandi  // kill magic quotes
63e55eb89cSAndreas Gohr  if (get_magic_quotes_gpc() && !defined('MAGIC_QUOTES_STRIPPED')) {
64ed7b5f09Sandi    if (!empty($_GET))    remove_magic_quotes($_GET);
65ed7b5f09Sandi    if (!empty($_POST))   remove_magic_quotes($_POST);
66ed7b5f09Sandi    if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE);
67ed7b5f09Sandi    if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST);
68*56146e0dSAndreas Gohr#    if (!empty($_SESSION)) remove_magic_quotes($_SESSION); #FIXME needed ?
693fc74836Sandi    @ini_set('magic_quotes_gpc', 0);
70e55eb89cSAndreas Gohr    define('MAGIC_QUOTES_STRIPPED',1);
71ed7b5f09Sandi  }
723fc74836Sandi  @set_magic_quotes_runtime(0);
733fc74836Sandi  @ini_set('magic_quotes_sybase',0);
74ed7b5f09Sandi
75ed7b5f09Sandi  // disable gzip if not available
76ed7b5f09Sandi  if($conf['usegzip'] && !function_exists('gzopen')){
77ed7b5f09Sandi    $conf['usegzip'] = 0;
78ed7b5f09Sandi  }
79ed7b5f09Sandi
8044881d27STroels Liebe Bentsen  // Legacy support for old umask/dmask scheme
8144881d27STroels Liebe Bentsen  if(isset($conf['dmask'])) {
8244881d27STroels Liebe Bentsen    unset($conf['dmask']);
8344881d27STroels Liebe Bentsen    unset($conf['fmask']);
8444881d27STroels Liebe Bentsen    unset($conf['umask']);
8544881d27STroels Liebe Bentsen  }
8644881d27STroels Liebe Bentsen
8744881d27STroels Liebe Bentsen  // Set defaults for fmode, dmode and umask.
88*56146e0dSAndreas Gohr  if(!isset($conf['fmode']) || $conf['fmode'] === '') {
8944881d27STroels Liebe Bentsen    $conf['fmode'] = 0666;
9044881d27STroels Liebe Bentsen  }
91*56146e0dSAndreas Gohr  if(!isset($conf['dmode']) || $conf['dmode'] === '') {
9244881d27STroels Liebe Bentsen    $conf['dmode'] = 0777;
9344881d27STroels Liebe Bentsen  }
94*56146e0dSAndreas Gohr  if(!isset($conf['umask']) || $conf['umask'] === '') {
9544881d27STroels Liebe Bentsen    $conf['umask'] = umask();
9644881d27STroels Liebe Bentsen  }
9744881d27STroels Liebe Bentsen
9844881d27STroels Liebe Bentsen  // Precalculate the fmask and dmask, so we can set later.
9944881d27STroels Liebe Bentsen  if(($conf['umask'] != umask()) or ($conf['fmode'] != 0666)) {
10044881d27STroels Liebe Bentsen    $conf['fmask'] = $conf['fmode'] & ~$conf['umask'];
10144881d27STroels Liebe Bentsen  }
10244881d27STroels Liebe Bentsen  if(($conf['umask'] != umask()) or ($conf['dmode'] != 0666)) {
10344881d27STroels Liebe Bentsen    $conf['dmask'] = $conf['dmode'] & ~$conf['umask'];
10444881d27STroels Liebe Bentsen  }
105ed7b5f09Sandi
1063dc3a5f1Sandi  // make real paths and check them
10798407a7aSandi  init_paths();
1087367b368SAndreas Gohr  init_files();
109ed7b5f09Sandi
1108c4f28e8Sjan  // automatic upgrade to script versions of certain files
111e7cb32dcSAndreas Gohr  scriptify(DOKU_CONF.'users.auth');
112e7cb32dcSAndreas Gohr  scriptify(DOKU_CONF.'acl.auth');
113f62ea8a1Sandi
114f62ea8a1Sandi
115f62ea8a1Sandi/**
11698407a7aSandi * Checks paths from config file
11798407a7aSandi */
11898407a7aSandifunction init_paths(){
11998407a7aSandi  global $conf;
12098407a7aSandi
12198407a7aSandi  $paths = array('datadir'   => 'pages',
12298407a7aSandi                 'olddir'    => 'attic',
12398407a7aSandi                 'mediadir'  => 'media',
12498407a7aSandi                 'metadir'   => 'meta',
12598407a7aSandi                 'cachedir'  => 'cache',
12698407a7aSandi                 'lockdir'   => 'locks',
12798407a7aSandi                 'changelog' => 'changes.log');
12898407a7aSandi
12998407a7aSandi  foreach($paths as $c => $p){
13098407a7aSandi    if(!$conf[$c])   $conf[$c] = $conf['savedir'].'/'.$p;
13198407a7aSandi    $conf[$c]        = init_path($conf[$c]);
1323816dcbcSAndreas Gohr    if(!$conf[$c])   nice_die("The $c does not exist, isn't accessable or writable.
1333816dcbcSAndreas Gohr                               Check your config and permission settings!");
13498407a7aSandi  }
13598407a7aSandi}
13698407a7aSandi
13798407a7aSandi/**
1380d8850c4SAndreas Gohr * Checks the existance of certain files and creates them if missing.
1397367b368SAndreas Gohr */
1407367b368SAndreas Gohrfunction init_files(){
1417367b368SAndreas Gohr  global $conf;
1420d8850c4SAndreas Gohr
1437367b368SAndreas Gohr  $files = array( $conf['cachedir'].'/word.idx',
1447367b368SAndreas Gohr                  $conf['cachedir'].'/page.idx',
1450d8850c4SAndreas Gohr                  $conf['cachedir'].'/index.idx');
1467367b368SAndreas Gohr
1477367b368SAndreas Gohr  foreach($files as $file){
1487367b368SAndreas Gohr    if(!@file_exists($file)){
1490d8850c4SAndreas Gohr      $fh = @fopen($file,'a');
1500d8850c4SAndreas Gohr      if($fh){
1517367b368SAndreas Gohr        fclose($fh);
15244881d27STroels Liebe Bentsen        if(isset($conf['fmask'])) { chmod($file, $conf['fmask']); }
1530d8850c4SAndreas Gohr      }else{
1543816dcbcSAndreas Gohr        nice_die("$file is not writable. Check your permissions settings!");
1550d8850c4SAndreas Gohr      }
1567367b368SAndreas Gohr    }
1577367b368SAndreas Gohr  }
1587367b368SAndreas Gohr}
1597367b368SAndreas Gohr
1607367b368SAndreas Gohr/**
1610d8850c4SAndreas Gohr * Returns absolute path
162f62ea8a1Sandi *
1630d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC.
1640d8850c4SAndreas Gohr * Check for accessability on directories as well.
1650d8850c4SAndreas Gohr *
1660d8850c4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
167f62ea8a1Sandi */
168f62ea8a1Sandifunction init_path($path){
1690d8850c4SAndreas Gohr  // check existance
170f62ea8a1Sandi  $p = realpath($path);
1710d8850c4SAndreas Gohr  if(!@file_exists($p)){
172f62ea8a1Sandi    $p = realpath(DOKU_INC.$path);
1730d8850c4SAndreas Gohr    if(!@file_exists($p)){
1748fc4e739Sandi      return '';
175f62ea8a1Sandi    }
1760d8850c4SAndreas Gohr  }
1770d8850c4SAndreas Gohr
1780d8850c4SAndreas Gohr  // check writability
1790d8850c4SAndreas Gohr  if(!@is_writable($p)){
1800d8850c4SAndreas Gohr    return '';
1810d8850c4SAndreas Gohr  }
1820d8850c4SAndreas Gohr
1830d8850c4SAndreas Gohr  // check accessability (execute bit) for directories
1840d8850c4SAndreas Gohr  if(@is_dir($p) && !@file_exists("$p/.")){
1850d8850c4SAndreas Gohr    return '';
1860d8850c4SAndreas Gohr  }
1870d8850c4SAndreas Gohr
1880d8850c4SAndreas Gohr  return $p;
1890d8850c4SAndreas Gohr}
1908c4f28e8Sjan
191ed7b5f09Sandi/**
192ed7b5f09Sandi * remove magic quotes recursivly
193ed7b5f09Sandi *
194ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
195ed7b5f09Sandi */
196ed7b5f09Sandifunction remove_magic_quotes(&$array) {
197ed7b5f09Sandi  foreach (array_keys($array) as $key) {
198ed7b5f09Sandi    if (is_array($array[$key])) {
199ed7b5f09Sandi      remove_magic_quotes($array[$key]);
200ed7b5f09Sandi    }else {
201ed7b5f09Sandi      $array[$key] = stripslashes($array[$key]);
202ed7b5f09Sandi    }
203ed7b5f09Sandi  }
204ed7b5f09Sandi}
205ed7b5f09Sandi
206ed7b5f09Sandi/**
207ed7b5f09Sandi * Returns the full absolute URL to the directory where
208ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash)
209ed7b5f09Sandi *
210ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
211ed7b5f09Sandi */
212ed7b5f09Sandifunction getBaseURL($abs=false){
213ed7b5f09Sandi  global $conf;
214ed7b5f09Sandi  //if canonical url enabled always return absolute
215ed7b5f09Sandi  if($conf['canonical']) $abs = true;
216ed7b5f09Sandi
21792b83b77Sandi  if($conf['basedir']){
218919eeb46Sandi    $dir = $conf['basedir'].'/';
21989aa05dbSAndreas Gohr  }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){
220bdc127a4Sandi    $dir = dirname($_SERVER['SCRIPT_NAME']).'/';
22189aa05dbSAndreas Gohr  }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){
22289aa05dbSAndreas Gohr    $dir = dirname($_SERVER['PHP_SELF']).'/';
223093ec9e4Sandi  }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
224093ec9e4Sandi    $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
225093ec9e4Sandi                         $_SERVER['SCRIPT_FILENAME']);
226093ec9e4Sandi    $dir = dirname('/'.$dir).'/';
22792b83b77Sandi  }else{
22889aa05dbSAndreas Gohr    $dir = './'; //probably wrong
22992b83b77Sandi  }
230ed7b5f09Sandi
231ed7b5f09Sandi  $dir = str_replace('\\','/',$dir); #bugfix for weird WIN behaviour
232ed7b5f09Sandi  $dir = preg_replace('#//+#','/',$dir);
233ed7b5f09Sandi
234f62ea8a1Sandi  //handle script in lib/exe dir
235f62ea8a1Sandi  $dir = preg_replace('!lib/exe/$!','',$dir);
236f62ea8a1Sandi
237ed7b5f09Sandi  //finish here for relative URLs
238ed7b5f09Sandi  if(!$abs) return $dir;
239ed7b5f09Sandi
240ef7b3ecdSAndreas Gohr  //use config option if available
241ef7b3ecdSAndreas Gohr  if($conf['baseurl']) return $conf['baseurl'].$dir;
242ef7b3ecdSAndreas Gohr
243e82e3526SAndreas Gohr  //split hostheader into host and port
244e82e3526SAndreas Gohr  list($host,$port) = explode(':',$_SERVER['HTTP_HOST']);
245e82e3526SAndreas Gohr  if(!$port)  $port = $_SERVER['SERVER_PORT'];
246e82e3526SAndreas Gohr  if(!$port)  $port = 80;
247ed7b5f09Sandi
248ed7b5f09Sandi  // see if HTTPS is enabled - apache leaves this empty when not available,
249ed7b5f09Sandi  // IIS sets it to 'off', 'false' and 'disabled' are just guessing
250ed7b5f09Sandi  if (preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){
251ed7b5f09Sandi    $proto = 'http://';
252e82e3526SAndreas Gohr    if ($port == '80') {
253ed7b5f09Sandi      $port='';
254ed7b5f09Sandi    }
255ed7b5f09Sandi  }else{
256ed7b5f09Sandi    $proto = 'https://';
257e82e3526SAndreas Gohr    if ($port == '443') {
258ed7b5f09Sandi      $port='';
259ed7b5f09Sandi    }
260ed7b5f09Sandi  }
261ed7b5f09Sandi
262e82e3526SAndreas Gohr  if($port) $port = ':'.$port;
263e82e3526SAndreas Gohr
264ed7b5f09Sandi  return $proto.$host.$port.$dir;
265ed7b5f09Sandi}
266ed7b5f09Sandi
267b000c6d4Sandi/**
268b000c6d4Sandi * Append a PHP extension to a given file and adds an exit call
269b000c6d4Sandi *
270b000c6d4Sandi * This is used to migrate some old configfiles. An added PHP extension
271b000c6d4Sandi * ensures the contents are not shown to webusers even if .htaccess files
272b000c6d4Sandi * do not work
273b000c6d4Sandi *
274b000c6d4Sandi * @author Jan Decaluwe <jan@jandecaluwe.com>
275b000c6d4Sandi */
2768c4f28e8Sjanfunction scriptify($file) {
2778c4f28e8Sjan  // checks
2788c4f28e8Sjan  if (!is_readable($file)) {
2798c4f28e8Sjan    return;
2808c4f28e8Sjan  }
2818c4f28e8Sjan  $fn = $file.'.php';
2828c4f28e8Sjan  if (@file_exists($fn)) {
2838c4f28e8Sjan    return;
2848c4f28e8Sjan  }
2858c4f28e8Sjan  $fh = fopen($fn, 'w');
2868c4f28e8Sjan  if (!$fh) {
2873816dcbcSAndreas Gohr    nice_die($fn.' is not writable. Check your permission settings!');
2888c4f28e8Sjan  }
2898c4f28e8Sjan  // write php exit hack first
2908c4f28e8Sjan  fwrite($fh, "# $fn\n");
2918c4f28e8Sjan  fwrite($fh, '# <?php exit()?>'."\n");
2928c4f28e8Sjan  fwrite($fh, "# Don't modify the lines above\n");
2938c4f28e8Sjan  fwrite($fh, "#\n");
2948c4f28e8Sjan  // copy existing lines
2958c4f28e8Sjan  $lines = file($file);
2968c4f28e8Sjan  foreach ($lines as $line){
2978c4f28e8Sjan    fwrite($fh, $line);
2988c4f28e8Sjan  }
2993ba793e2Sandi  fclose($fh);
300b000c6d4Sandi  //try to rename the old file
301b000c6d4Sandi  @rename($file,"$file.old");
3028c4f28e8Sjan}
3038c4f28e8Sjan
3043816dcbcSAndreas Gohr/**
3053816dcbcSAndreas Gohr * print a nice message even if no styles are loaded yet.
3063816dcbcSAndreas Gohr */
3073816dcbcSAndreas Gohrfunction nice_die($msg){
3083816dcbcSAndreas Gohr  echo<<<EOT
3093816dcbcSAndreas Gohr  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
3103816dcbcSAndreas Gohr   "http://www.w3.org/TR/html4/loose.dtd">
3113816dcbcSAndreas Gohr  <html>
3123816dcbcSAndreas Gohr    <head><title>DokuWiki Setup Error</title></head>
3133816dcbcSAndreas Gohr    <body style="font-family: Arial, sans-serif">
3143816dcbcSAndreas Gohr      <div style="width:60%; margin: auto; background-color: #fcc;
3153816dcbcSAndreas Gohr                  border: 1px solid #faa; padding: 0.5em 1em;">
3163816dcbcSAndreas Gohr      <h1 style="font-size: 120%">DokuWiki Setup Error</h1>
3173816dcbcSAndreas Gohr      <p>$msg</p>
3183816dcbcSAndreas Gohr      </div>
3193816dcbcSAndreas Gohr    </body>
3203816dcbcSAndreas Gohr  </html>
3213816dcbcSAndreas GohrEOT;
3223816dcbcSAndreas Gohr  exit;
3233816dcbcSAndreas Gohr}
3243816dcbcSAndreas Gohr
325ed7b5f09Sandi
326340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
327