xref: /dokuwiki/inc/init.php (revision e09875dbfe0a4db06ec7573d954b8403e916bba9)
1<?php
2/**
3 * Initialize some defaults needed for DokuWiki
4 */
5
6  // define the include path
7  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
8
9  // set up error reporting to sane values
10  error_reporting(E_ALL ^ E_NOTICE);
11
12  //prepare config array()
13  global $conf;
14  $conf = array();
15
16  // load the config file(s)
17  require_once(DOKU_INC.'conf/dokuwiki.php');
18  @include_once(DOKU_INC.'conf/local.php');
19
20  //prepare language array
21  global $lang;
22  $lang = array();
23
24  //load the language files
25  require_once(DOKU_INC.'lang/en/lang.php');
26  require_once(DOKU_INC.'lang/'.$conf['lang'].'/lang.php');
27
28  // define baseURL
29  if(!defined('DOKU_BASE')) define('DOKU_BASE',getBaseURL());
30  if(!defined('DOKU_URL'))  define('DOKU_URL',getBaseURL(true));
31
32  // define Plugin dir
33  if(!defined('DOKU_PLUGIN'))  define('DOKU_PLUGIN',DOKU_INC.'inc/plugins/');
34
35  // define main script
36  if(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php');
37
38  // define Template baseURL
39  if(!defined('DOKU_TPL')) define('DOKU_TPL',
40                                  DOKU_BASE.'tpl/'.$conf['template'].'/');
41
42  // make session rewrites XHTML compliant
43  @ini_set('arg_separator.output', '&amp;');
44
45  // init session
46  session_name("DokuWiki");
47  if (!headers_sent()) session_start();
48
49  // kill magic quotes
50  if (get_magic_quotes_gpc()) {
51    if (!empty($_GET))    remove_magic_quotes($_GET);
52    if (!empty($_POST))   remove_magic_quotes($_POST);
53    if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE);
54    if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST);
55    if (!empty($_SESSION)) remove_magic_quotes($_SESSION);
56    @ini_set('magic_quotes_gpc', 0);
57  }
58  @set_magic_quotes_runtime(0);
59  @ini_set('magic_quotes_sybase',0);
60
61  // disable gzip if not available
62  if($conf['usegzip'] && !function_exists('gzopen')){
63    $conf['usegzip'] = 0;
64  }
65
66  // remember original umask
67  $conf['oldumask'] = umask();
68
69  // make absolute mediaweb
70  if(!preg_match('#^(https?://|/)#i',$conf['mediaweb'])){
71    $conf['mediaweb'] = getBaseURL().$conf['mediaweb'];
72  }
73
74  // make real paths and check them
75  $conf['datadir']       = realpath($conf['datadir']);
76  if(!$conf['datadir'])    die('Wrong datadir! Check config!');
77  $conf['olddir']        = realpath($conf['olddir']);
78  if(!$conf['olddir'])     die('Wrong olddir! Check config!');
79  $conf['mediadir']      = realpath($conf['mediadir']);
80  if(!$conf['mediadir'])   die('Wrong mediadir! Check config!');
81
82  // automatic upgrade to script versions of certain files
83  scriptify('conf/users.auth');
84  scriptify('conf/acl.auth');
85
86/**
87 * remove magic quotes recursivly
88 *
89 * @author Andreas Gohr <andi@splitbrain.org>
90 */
91function remove_magic_quotes(&$array) {
92  foreach (array_keys($array) as $key) {
93    if (is_array($array[$key])) {
94      remove_magic_quotes($array[$key]);
95    }else {
96      $array[$key] = stripslashes($array[$key]);
97    }
98  }
99}
100
101/**
102 * Returns the full absolute URL to the directory where
103 * DokuWiki is installed in (includes a trailing slash)
104 *
105 * @author Andreas Gohr <andi@splitbrain.org>
106 */
107function getBaseURL($abs=false){
108  global $conf;
109  //if canonical url enabled always return absolute
110  if($conf['canonical']) $abs = true;
111
112  if($conf['basedir']){
113    $dir = $conf['basedir'].'/';
114  }elseif($_SERVER['SCRIPT_NAME']){
115    $dir = dirname($_SERVER['SCRIPT_NAME']).'/';
116  }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
117    $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
118                         $_SERVER['SCRIPT_FILENAME']);
119    $dir = dirname('/'.$dir).'/';
120  }else{
121    $dir = dirname($_SERVER['PHP_SELF']).'/';
122  }
123
124  $dir = str_replace('\\','/',$dir); #bugfix for weird WIN behaviour
125  $dir = preg_replace('#//+#','/',$dir);
126
127  //finish here for relative URLs
128  if(!$abs) return $dir;
129
130  $port = ':'.$_SERVER['SERVER_PORT'];
131  //remove port from hostheader as sent by IE
132  $host = preg_replace('/:.*$/','',$_SERVER['HTTP_HOST']);
133
134  // see if HTTPS is enabled - apache leaves this empty when not available,
135  // IIS sets it to 'off', 'false' and 'disabled' are just guessing
136  if (preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){
137    $proto = 'http://';
138    if ($_SERVER['SERVER_PORT'] == '80') {
139      $port='';
140    }
141  }else{
142    $proto = 'https://';
143    if ($_SERVER['SERVER_PORT'] == '443') {
144      $port='';
145    }
146  }
147
148  return $proto.$host.$port.$dir;
149}
150
151/**
152 * Append a PHP extension to a given file and adds an exit call
153 *
154 * This is used to migrate some old configfiles. An added PHP extension
155 * ensures the contents are not shown to webusers even if .htaccess files
156 * do not work
157 *
158 * @author Jan Decaluwe <jan@jandecaluwe.com>
159 */
160function scriptify($file) {
161  // checks
162  if (!is_readable($file)) {
163    return;
164  }
165  $fn = $file.'.php';
166  if (@file_exists($fn)) {
167    return;
168  }
169  $fh = fopen($fn, 'w');
170  if (!$fh) {
171    die($fn.' is not writable!');
172  }
173  // write php exit hack first
174  fwrite($fh, "# $fn\n");
175  fwrite($fh, '# <?php exit()?>'."\n");
176  fwrite($fh, "# Don't modify the lines above\n");
177  fwrite($fh, "#\n");
178  // copy existing lines
179  $lines = file($file);
180  foreach ($lines as $line){
181    fwrite($fh, $line);
182  }
183  fclose($fh);
184  //try to rename the old file
185  @rename($file,"$file.old");
186}
187
188
189//Setup VIM: ex: et ts=2 enc=utf-8 :
190