xref: /dokuwiki/inc/init.php (revision 44881d272282937c9bb745f462c947319d404dd0)
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
63ed7b5f09Sandi  if (get_magic_quotes_gpc()) {
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);
68ed7b5f09Sandi    if (!empty($_SESSION)) remove_magic_quotes($_SESSION);
693fc74836Sandi    @ini_set('magic_quotes_gpc', 0);
70ed7b5f09Sandi  }
713fc74836Sandi  @set_magic_quotes_runtime(0);
723fc74836Sandi  @ini_set('magic_quotes_sybase',0);
73ed7b5f09Sandi
74ed7b5f09Sandi  // disable gzip if not available
75ed7b5f09Sandi  if($conf['usegzip'] && !function_exists('gzopen')){
76ed7b5f09Sandi    $conf['usegzip'] = 0;
77ed7b5f09Sandi  }
78ed7b5f09Sandi
79*44881d27STroels Liebe Bentsen  // Legacy support for old umask/dmask scheme
80*44881d27STroels Liebe Bentsen  if(isset($conf['dmask'])) {
81*44881d27STroels Liebe Bentsen    unset($conf['dmask']);
82*44881d27STroels Liebe Bentsen    unset($conf['fmask']);
83*44881d27STroels Liebe Bentsen    unset($conf['umask']);
84*44881d27STroels Liebe Bentsen  }
85*44881d27STroels Liebe Bentsen
86*44881d27STroels Liebe Bentsen  // Set defaults for fmode, dmode and umask.
87*44881d27STroels Liebe Bentsen  if(!isset($conf['fmode'])) {
88*44881d27STroels Liebe Bentsen    $conf['fmode'] = 0666;
89*44881d27STroels Liebe Bentsen  }
90*44881d27STroels Liebe Bentsen  if(!isset($conf['dmode'])) {
91*44881d27STroels Liebe Bentsen    $conf['dmode'] = 0777;
92*44881d27STroels Liebe Bentsen  }
93*44881d27STroels Liebe Bentsen  if(!isset($conf['umask'])) {
94*44881d27STroels Liebe Bentsen    $conf['umask'] = umask();
95*44881d27STroels Liebe Bentsen  }
96*44881d27STroels Liebe Bentsen
97*44881d27STroels Liebe Bentsen  // Precalculate the fmask and dmask, so we can set later.
98*44881d27STroels Liebe Bentsen  if(($conf['umask'] != umask()) or ($conf['fmode'] != 0666)) {
99*44881d27STroels Liebe Bentsen    $conf['fmask'] = $conf['fmode'] & ~$conf['umask'];
100*44881d27STroels Liebe Bentsen  }
101*44881d27STroels Liebe Bentsen  if(($conf['umask'] != umask()) or ($conf['dmode'] != 0666)) {
102*44881d27STroels Liebe Bentsen    $conf['dmask'] = $conf['dmode'] & ~$conf['umask'];
103*44881d27STroels Liebe Bentsen  }
104*44881d27STroels Liebe Bentsen#  print "$name:".sprintf("dmask:%04o<br>\n",$conf['dmode'])."\n";
105*44881d27STroels Liebe Bentsen#  print "$name:".sprintf("umask:%04o<br>\n",$conf['umask'])."\n";
106*44881d27STroels Liebe Bentsen#  print "$name:".sprintf("dmask:%04o<br>\n",$conf['dmask'])."\n";
107*44881d27STroels Liebe Bentsen#  exit;
108ed7b5f09Sandi
1093dc3a5f1Sandi  // make real paths and check them
11098407a7aSandi  init_paths();
1117367b368SAndreas Gohr  init_files();
112ed7b5f09Sandi
1138c4f28e8Sjan  // automatic upgrade to script versions of certain files
114e7cb32dcSAndreas Gohr  scriptify(DOKU_CONF.'users.auth');
115e7cb32dcSAndreas Gohr  scriptify(DOKU_CONF.'acl.auth');
116f62ea8a1Sandi
117f62ea8a1Sandi
118f62ea8a1Sandi/**
11998407a7aSandi * Checks paths from config file
12098407a7aSandi */
12198407a7aSandifunction init_paths(){
12298407a7aSandi  global $conf;
12398407a7aSandi
12498407a7aSandi  $paths = array('datadir'   => 'pages',
12598407a7aSandi                 'olddir'    => 'attic',
12698407a7aSandi                 'mediadir'  => 'media',
12798407a7aSandi                 'metadir'   => 'meta',
12898407a7aSandi                 'cachedir'  => 'cache',
12998407a7aSandi                 'lockdir'   => 'locks',
13098407a7aSandi                 'changelog' => 'changes.log');
13198407a7aSandi
13298407a7aSandi  foreach($paths as $c => $p){
13398407a7aSandi    if(!$conf[$c])   $conf[$c] = $conf['savedir'].'/'.$p;
13498407a7aSandi    $conf[$c]        = init_path($conf[$c]);
13598407a7aSandi    if(!$conf[$c])   die("$c does not exist or isn't writable. Check config!");
13698407a7aSandi  }
13798407a7aSandi}
13898407a7aSandi
13998407a7aSandi/**
1407367b368SAndreas Gohr * Checks the existance of certain files and creates them if missing
1417367b368SAndreas Gohr */
1427367b368SAndreas Gohrfunction init_files(){
1437367b368SAndreas Gohr  global $conf;
1447367b368SAndreas Gohr  $files = array( $conf['cachedir'].'/word.idx',
1457367b368SAndreas Gohr                  $conf['cachedir'].'/page.idx',
1467367b368SAndreas Gohr                  $conf['cachedir'].'/index.idx', );
1477367b368SAndreas Gohr
1487367b368SAndreas Gohr  foreach($files as $file){
1497367b368SAndreas Gohr    if(!@file_exists($file)){
1507367b368SAndreas Gohr      $fh = fopen($file,'a');
1517367b368SAndreas Gohr      fclose($fh);
152*44881d27STroels Liebe Bentsen      if(isset($conf['fmask'])) { chmod($file, $conf['fmask']); }
1537367b368SAndreas Gohr    }
1547367b368SAndreas Gohr  }
1557367b368SAndreas Gohr}
1567367b368SAndreas Gohr
1577367b368SAndreas Gohr/**
158f62ea8a1Sandi * returns absolute path
159f62ea8a1Sandi *
16098407a7aSandi * This tries the given path first, then checks in DOKU_INC
161f62ea8a1Sandi */
162f62ea8a1Sandifunction init_path($path){
163f62ea8a1Sandi  $p = realpath($path);
16498407a7aSandi  if(@file_exists($p)) return $p;
165f62ea8a1Sandi  $p = realpath(DOKU_INC.$path);
16698407a7aSandi  if(@file_exists($p)) return $p;
1678fc4e739Sandi  return '';
168f62ea8a1Sandi}
1698c4f28e8Sjan
170ed7b5f09Sandi/**
171ed7b5f09Sandi * remove magic quotes recursivly
172ed7b5f09Sandi *
173ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
174ed7b5f09Sandi */
175ed7b5f09Sandifunction remove_magic_quotes(&$array) {
176ed7b5f09Sandi  foreach (array_keys($array) as $key) {
177ed7b5f09Sandi    if (is_array($array[$key])) {
178ed7b5f09Sandi      remove_magic_quotes($array[$key]);
179ed7b5f09Sandi    }else {
180ed7b5f09Sandi      $array[$key] = stripslashes($array[$key]);
181ed7b5f09Sandi    }
182ed7b5f09Sandi  }
183ed7b5f09Sandi}
184ed7b5f09Sandi
185ed7b5f09Sandi/**
186ed7b5f09Sandi * Returns the full absolute URL to the directory where
187ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash)
188ed7b5f09Sandi *
189ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
190ed7b5f09Sandi */
191ed7b5f09Sandifunction getBaseURL($abs=false){
192ed7b5f09Sandi  global $conf;
193ed7b5f09Sandi  //if canonical url enabled always return absolute
194ed7b5f09Sandi  if($conf['canonical']) $abs = true;
195ed7b5f09Sandi
19692b83b77Sandi  if($conf['basedir']){
197919eeb46Sandi    $dir = $conf['basedir'].'/';
19889aa05dbSAndreas Gohr  }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){
199bdc127a4Sandi    $dir = dirname($_SERVER['SCRIPT_NAME']).'/';
20089aa05dbSAndreas Gohr  }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){
20189aa05dbSAndreas Gohr    $dir = dirname($_SERVER['PHP_SELF']).'/';
202093ec9e4Sandi  }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
203093ec9e4Sandi    $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
204093ec9e4Sandi                         $_SERVER['SCRIPT_FILENAME']);
205093ec9e4Sandi    $dir = dirname('/'.$dir).'/';
20692b83b77Sandi  }else{
20789aa05dbSAndreas Gohr    $dir = './'; //probably wrong
20892b83b77Sandi  }
209ed7b5f09Sandi
210ed7b5f09Sandi  $dir = str_replace('\\','/',$dir); #bugfix for weird WIN behaviour
211ed7b5f09Sandi  $dir = preg_replace('#//+#','/',$dir);
212ed7b5f09Sandi
213f62ea8a1Sandi  //handle script in lib/exe dir
214f62ea8a1Sandi  $dir = preg_replace('!lib/exe/$!','',$dir);
215f62ea8a1Sandi
216ed7b5f09Sandi  //finish here for relative URLs
217ed7b5f09Sandi  if(!$abs) return $dir;
218ed7b5f09Sandi
219ef7b3ecdSAndreas Gohr  //use config option if available
220ef7b3ecdSAndreas Gohr  if($conf['baseurl']) return $conf['baseurl'].$dir;
221ef7b3ecdSAndreas Gohr
222e82e3526SAndreas Gohr  //split hostheader into host and port
223e82e3526SAndreas Gohr  list($host,$port) = explode(':',$_SERVER['HTTP_HOST']);
224e82e3526SAndreas Gohr  if(!$port)  $port = $_SERVER['SERVER_PORT'];
225e82e3526SAndreas Gohr  if(!$port)  $port = 80;
226ed7b5f09Sandi
227ed7b5f09Sandi  // see if HTTPS is enabled - apache leaves this empty when not available,
228ed7b5f09Sandi  // IIS sets it to 'off', 'false' and 'disabled' are just guessing
229ed7b5f09Sandi  if (preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){
230ed7b5f09Sandi    $proto = 'http://';
231e82e3526SAndreas Gohr    if ($port == '80') {
232ed7b5f09Sandi      $port='';
233ed7b5f09Sandi    }
234ed7b5f09Sandi  }else{
235ed7b5f09Sandi    $proto = 'https://';
236e82e3526SAndreas Gohr    if ($port == '443') {
237ed7b5f09Sandi      $port='';
238ed7b5f09Sandi    }
239ed7b5f09Sandi  }
240ed7b5f09Sandi
241e82e3526SAndreas Gohr  if($port) $port = ':'.$port;
242e82e3526SAndreas Gohr
243ed7b5f09Sandi  return $proto.$host.$port.$dir;
244ed7b5f09Sandi}
245ed7b5f09Sandi
246b000c6d4Sandi/**
247b000c6d4Sandi * Append a PHP extension to a given file and adds an exit call
248b000c6d4Sandi *
249b000c6d4Sandi * This is used to migrate some old configfiles. An added PHP extension
250b000c6d4Sandi * ensures the contents are not shown to webusers even if .htaccess files
251b000c6d4Sandi * do not work
252b000c6d4Sandi *
253b000c6d4Sandi * @author Jan Decaluwe <jan@jandecaluwe.com>
254b000c6d4Sandi */
2558c4f28e8Sjanfunction scriptify($file) {
2568c4f28e8Sjan  // checks
2578c4f28e8Sjan  if (!is_readable($file)) {
2588c4f28e8Sjan    return;
2598c4f28e8Sjan  }
2608c4f28e8Sjan  $fn = $file.'.php';
2618c4f28e8Sjan  if (@file_exists($fn)) {
2628c4f28e8Sjan    return;
2638c4f28e8Sjan  }
2648c4f28e8Sjan  $fh = fopen($fn, 'w');
2658c4f28e8Sjan  if (!$fh) {
2668c4f28e8Sjan    die($fn.' is not writable!');
2678c4f28e8Sjan  }
2688c4f28e8Sjan  // write php exit hack first
2698c4f28e8Sjan  fwrite($fh, "# $fn\n");
2708c4f28e8Sjan  fwrite($fh, '# <?php exit()?>'."\n");
2718c4f28e8Sjan  fwrite($fh, "# Don't modify the lines above\n");
2728c4f28e8Sjan  fwrite($fh, "#\n");
2738c4f28e8Sjan  // copy existing lines
2748c4f28e8Sjan  $lines = file($file);
2758c4f28e8Sjan  foreach ($lines as $line){
2768c4f28e8Sjan    fwrite($fh, $line);
2778c4f28e8Sjan  }
2783ba793e2Sandi  fclose($fh);
279b000c6d4Sandi  //try to rename the old file
280b000c6d4Sandi  @rename($file,"$file.old");
2818c4f28e8Sjan}
2828c4f28e8Sjan
283ed7b5f09Sandi
284340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
285