xref: /dokuwiki/inc/init.php (revision 16521111dd62031afbcbdadffe0f8f6929fb6056)
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
9c53ea5f2Sandi  // set up error reporting to sane values
10c53ea5f2Sandi  error_reporting(E_ALL ^ E_NOTICE);
11c53ea5f2Sandi
124724a577Sandi  //prepare config array()
13ee20e7d1Sandi  global $conf;
144724a577Sandi  $conf = array();
154724a577Sandi
16ad15db82Sandi  // load the config file(s)
17ed7b5f09Sandi  require_once(DOKU_INC.'conf/dokuwiki.php');
18ad15db82Sandi  @include_once(DOKU_INC.'conf/local.php');
19ad15db82Sandi
20ad15db82Sandi  //prepare language array
21ee20e7d1Sandi  global $lang;
22ad15db82Sandi  $lang = array();
23ed7b5f09Sandi
24*16521111Sandi  //load the language files
25*16521111Sandi  require_once(DOKU_INC.'lang/en/lang.php');
26*16521111Sandi  require_once(DOKU_INC.'lang/'.$conf['lang'].'/lang.php');
27*16521111Sandi
28ed7b5f09Sandi  // define baseURL
29ed7b5f09Sandi  if(!defined('DOKU_BASE')) define('DOKU_BASE',getBaseURL());
30ed7b5f09Sandi  if(!defined('DOKU_URL'))  define('DOKU_URL',getBaseURL(true));
31ed7b5f09Sandi
32ee20e7d1Sandi  // define Plugin dir
33ee20e7d1Sandi  if(!defined('DOKU_PLUGIN'))  define('DOKU_PLUGIN',DOKU_INC.'inc/plugins/');
34ee20e7d1Sandi
35ed7b5f09Sandi  // define main script
36ed7b5f09Sandi  if(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php');
37ed7b5f09Sandi
386b13307fSandi  // define Template baseURL
396b13307fSandi  if(!defined('DOKU_TPL')) define('DOKU_TPL',
406b13307fSandi                                  DOKU_BASE.'tpl/'.$conf['template'].'/');
416b13307fSandi
42ed7b5f09Sandi  // make session rewrites XHTML compliant
433fc74836Sandi  @ini_set('arg_separator.output', '&amp;');
44ed7b5f09Sandi
45ed7b5f09Sandi  // init session
46ed7b5f09Sandi  session_name("DokuWiki");
476b13307fSandi  if (!headers_sent()) session_start();
48ed7b5f09Sandi
49ed7b5f09Sandi  // kill magic quotes
50ed7b5f09Sandi  if (get_magic_quotes_gpc()) {
51ed7b5f09Sandi    if (!empty($_GET))    remove_magic_quotes($_GET);
52ed7b5f09Sandi    if (!empty($_POST))   remove_magic_quotes($_POST);
53ed7b5f09Sandi    if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE);
54ed7b5f09Sandi    if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST);
55ed7b5f09Sandi    if (!empty($_SESSION)) remove_magic_quotes($_SESSION);
563fc74836Sandi    @ini_set('magic_quotes_gpc', 0);
57ed7b5f09Sandi  }
583fc74836Sandi  @set_magic_quotes_runtime(0);
593fc74836Sandi  @ini_set('magic_quotes_sybase',0);
60ed7b5f09Sandi
61ed7b5f09Sandi  // disable gzip if not available
62ed7b5f09Sandi  if($conf['usegzip'] && !function_exists('gzopen')){
63ed7b5f09Sandi    $conf['usegzip'] = 0;
64ed7b5f09Sandi  }
65ed7b5f09Sandi
66ed7b5f09Sandi  // remember original umask
67ed7b5f09Sandi  $conf['oldumask'] = umask();
68ed7b5f09Sandi
69ed7b5f09Sandi  // make absolute mediaweb
70ed7b5f09Sandi  if(!preg_match('#^(https?://|/)#i',$conf['mediaweb'])){
71ed7b5f09Sandi    $conf['mediaweb'] = getBaseURL().$conf['mediaweb'];
72ed7b5f09Sandi  }
73ed7b5f09Sandi
743dc3a5f1Sandi  // make real paths and check them
753dc3a5f1Sandi  $conf['datadir']       = realpath($conf['datadir']);
76e33d39a7Sandi  if(!$conf['datadir'])    die('Wrong datadir! Check config!');
773dc3a5f1Sandi  $conf['olddir']        = realpath($conf['olddir']);
78e33d39a7Sandi  if(!$conf['olddir'])     die('Wrong olddir! Check config!');
793dc3a5f1Sandi  $conf['mediadir']      = realpath($conf['mediadir']);
80b94418bbSjan  if(!$conf['mediadir'])   die('Wrong mediadir! Check config!');
81ed7b5f09Sandi
828c4f28e8Sjan  // automatic upgrade to script versions of certain files
838c4f28e8Sjan  scriptify('conf/users.auth');
848c4f28e8Sjan  scriptify('conf/acl.auth');
858c4f28e8Sjan
86ed7b5f09Sandi/**
87ed7b5f09Sandi * remove magic quotes recursivly
88ed7b5f09Sandi *
89ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
90ed7b5f09Sandi */
91ed7b5f09Sandifunction remove_magic_quotes(&$array) {
92ed7b5f09Sandi  foreach (array_keys($array) as $key) {
93ed7b5f09Sandi    if (is_array($array[$key])) {
94ed7b5f09Sandi      remove_magic_quotes($array[$key]);
95ed7b5f09Sandi    }else {
96ed7b5f09Sandi      $array[$key] = stripslashes($array[$key]);
97ed7b5f09Sandi    }
98ed7b5f09Sandi  }
99ed7b5f09Sandi}
100ed7b5f09Sandi
101ed7b5f09Sandi/**
102ed7b5f09Sandi * Returns the full absolute URL to the directory where
103ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash)
104ed7b5f09Sandi *
105ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
106ed7b5f09Sandi */
107ed7b5f09Sandifunction getBaseURL($abs=false){
108ed7b5f09Sandi  global $conf;
109ed7b5f09Sandi  //if canonical url enabled always return absolute
110ed7b5f09Sandi  if($conf['canonical']) $abs = true;
111ed7b5f09Sandi
11292b83b77Sandi  if($conf['basedir']){
113919eeb46Sandi    $dir = $conf['basedir'].'/';
114bdc127a4Sandi  }elseif($_SERVER['SCRIPT_NAME']){
115bdc127a4Sandi    $dir = dirname($_SERVER['SCRIPT_NAME']).'/';
116093ec9e4Sandi  }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
117093ec9e4Sandi    $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
118093ec9e4Sandi                         $_SERVER['SCRIPT_FILENAME']);
119093ec9e4Sandi    $dir = dirname('/'.$dir).'/';
12092b83b77Sandi  }else{
121ed7b5f09Sandi    $dir = dirname($_SERVER['PHP_SELF']).'/';
12292b83b77Sandi  }
123ed7b5f09Sandi
124ed7b5f09Sandi  $dir = str_replace('\\','/',$dir); #bugfix for weird WIN behaviour
125ed7b5f09Sandi  $dir = preg_replace('#//+#','/',$dir);
126ed7b5f09Sandi
127ed7b5f09Sandi  //finish here for relative URLs
128ed7b5f09Sandi  if(!$abs) return $dir;
129ed7b5f09Sandi
130ed7b5f09Sandi  $port = ':'.$_SERVER['SERVER_PORT'];
131ed7b5f09Sandi  //remove port from hostheader as sent by IE
132ed7b5f09Sandi  $host = preg_replace('/:.*$/','',$_SERVER['HTTP_HOST']);
133ed7b5f09Sandi
134ed7b5f09Sandi  // see if HTTPS is enabled - apache leaves this empty when not available,
135ed7b5f09Sandi  // IIS sets it to 'off', 'false' and 'disabled' are just guessing
136ed7b5f09Sandi  if (preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){
137ed7b5f09Sandi    $proto = 'http://';
138ed7b5f09Sandi    if ($_SERVER['SERVER_PORT'] == '80') {
139ed7b5f09Sandi      $port='';
140ed7b5f09Sandi    }
141ed7b5f09Sandi  }else{
142ed7b5f09Sandi    $proto = 'https://';
143ed7b5f09Sandi    if ($_SERVER['SERVER_PORT'] == '443') {
144ed7b5f09Sandi      $port='';
145ed7b5f09Sandi    }
146ed7b5f09Sandi  }
147ed7b5f09Sandi
148ed7b5f09Sandi  return $proto.$host.$port.$dir;
149ed7b5f09Sandi}
150ed7b5f09Sandi
151b000c6d4Sandi/**
152b000c6d4Sandi * Append a PHP extension to a given file and adds an exit call
153b000c6d4Sandi *
154b000c6d4Sandi * This is used to migrate some old configfiles. An added PHP extension
155b000c6d4Sandi * ensures the contents are not shown to webusers even if .htaccess files
156b000c6d4Sandi * do not work
157b000c6d4Sandi *
158b000c6d4Sandi * @author Jan Decaluwe <jan@jandecaluwe.com>
159b000c6d4Sandi */
1608c4f28e8Sjanfunction scriptify($file) {
1618c4f28e8Sjan  // checks
1628c4f28e8Sjan  if (!is_readable($file)) {
1638c4f28e8Sjan    return;
1648c4f28e8Sjan  }
1658c4f28e8Sjan  $fn = $file.'.php';
1668c4f28e8Sjan  if (@file_exists($fn)) {
1678c4f28e8Sjan    return;
1688c4f28e8Sjan  }
1698c4f28e8Sjan  $fh = fopen($fn, 'w');
1708c4f28e8Sjan  if (!$fh) {
1718c4f28e8Sjan    die($fn.' is not writable!');
1728c4f28e8Sjan  }
1738c4f28e8Sjan  // write php exit hack first
1748c4f28e8Sjan  fwrite($fh, "# $fn\n");
1758c4f28e8Sjan  fwrite($fh, '# <?php exit()?>'."\n");
1768c4f28e8Sjan  fwrite($fh, "# Don't modify the lines above\n");
1778c4f28e8Sjan  fwrite($fh, "#\n");
1788c4f28e8Sjan  // copy existing lines
1798c4f28e8Sjan  $lines = file($file);
1808c4f28e8Sjan  foreach ($lines as $line){
1818c4f28e8Sjan    fwrite($fh, $line);
1828c4f28e8Sjan  }
1833ba793e2Sandi  fclose($fh);
184b000c6d4Sandi  //try to rename the old file
185b000c6d4Sandi  @rename($file,"$file.old");
1868c4f28e8Sjan}
1878c4f28e8Sjan
188ed7b5f09Sandi
189340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
190