xref: /dokuwiki/inc/init.php (revision ee4c4a1b5a5840c1b9d2d8c74b3f4298dd52928b)
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  // define config path (packagers may want to change this to /etc/dokuwiki/)
10  if(!defined('DOKU_CONF')) define('DOKU_CONF',DOKU_INC.'conf/');
11
12  // set up error reporting to sane values
13  error_reporting(E_ALL ^ E_NOTICE);
14
15  //prepare config array()
16  global $conf;
17  $conf = array();
18
19  // load the config file(s)
20  require_once(DOKU_CONF.'dokuwiki.php');
21  if(@file_exists(DOKU_CONF.'local.php')){
22    require_once(DOKU_CONF.'local.php');
23  }
24
25  //prepare language array
26  global $lang;
27  $lang = array();
28
29  //load the language files
30  require_once(DOKU_INC.'inc/lang/en/lang.php');
31  if ( $conf['lang'] && $conf['lang'] != 'en' ) {
32    require_once(DOKU_INC.'inc/lang/'.$conf['lang'].'/lang.php');
33  }
34
35  // define baseURL
36  if(!defined('DOKU_BASE')) define('DOKU_BASE',getBaseURL());
37  if(!defined('DOKU_URL'))  define('DOKU_URL',getBaseURL(true));
38
39  // define Plugin dir
40  if(!defined('DOKU_PLUGIN'))  define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
41
42  // define main script
43  if(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php');
44
45  // define Template baseURL
46  if(!defined('DOKU_TPL')) define('DOKU_TPL',
47                                  DOKU_BASE.'lib/tpl/'.$conf['template'].'/');
48
49  // define real Template directory
50  if(!defined('DOKU_TPLINC')) define('DOKU_TPLINC',
51                                  DOKU_INC.'lib/tpl/'.$conf['template'].'/');
52
53  // make session rewrites XHTML compliant
54  @ini_set('arg_separator.output', '&amp;');
55
56  // init session
57  if (!headers_sent() && !defined('NOSESSION')){
58    session_name("DokuWiki");
59    session_start();
60  }
61
62  // kill magic quotes
63  if (get_magic_quotes_gpc() && !defined('MAGIC_QUOTES_STRIPPED')) {
64    if (!empty($_GET))    remove_magic_quotes($_GET);
65    if (!empty($_POST))   remove_magic_quotes($_POST);
66    if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE);
67    if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST);
68#    if (!empty($_SESSION)) remove_magic_quotes($_SESSION); #FIXME needed ?
69    @ini_set('magic_quotes_gpc', 0);
70    define('MAGIC_QUOTES_STRIPPED',1);
71  }
72  @set_magic_quotes_runtime(0);
73  @ini_set('magic_quotes_sybase',0);
74
75  // disable gzip if not available
76  if($conf['usegzip'] && !function_exists('gzopen')){
77    $conf['usegzip'] = 0;
78  }
79
80  // precalculate file creation modes
81  init_creationmodes();
82
83  // make real paths and check them
84  init_paths();
85  init_files();
86
87  // automatic upgrade to script versions of certain files
88  scriptify(DOKU_CONF.'users.auth');
89  scriptify(DOKU_CONF.'acl.auth');
90
91
92/**
93 * Checks paths from config file
94 */
95function init_paths(){
96  global $conf;
97
98  $paths = array('datadir'   => 'pages',
99                 'olddir'    => 'attic',
100                 'mediadir'  => 'media',
101                 'metadir'   => 'meta',
102                 'cachedir'  => 'cache',
103                 'lockdir'   => 'locks',
104                 'changelog' => 'changes.log');
105
106  foreach($paths as $c => $p){
107    if(!$conf[$c])   $conf[$c] = $conf['savedir'].'/'.$p;
108    $conf[$c]        = init_path($conf[$c]);
109    if(!$conf[$c])   nice_die("The $c does not exist, isn't accessable or writable.
110                               Check your config and permission settings!");
111  }
112}
113
114/**
115 * Checks the existance of certain files and creates them if missing.
116 */
117function init_files(){
118  global $conf;
119
120  $files = array( $conf['cachedir'].'/word.idx',
121                  $conf['cachedir'].'/page.idx',
122                  $conf['cachedir'].'/index.idx');
123
124  foreach($files as $file){
125    if(!@file_exists($file)){
126      $fh = @fopen($file,'a');
127      if($fh){
128        fclose($fh);
129        if($conf['fperm']) chmod($file, $conf['fperm']);
130      }else{
131        nice_die("$file is not writable. Check your permissions settings!");
132      }
133    }
134  }
135}
136
137/**
138 * Returns absolute path
139 *
140 * This tries the given path first, then checks in DOKU_INC.
141 * Check for accessability on directories as well.
142 *
143 * @author Andreas Gohr <andi@splitbrain.org>
144 */
145function init_path($path){
146  // check existance
147  $p = realpath($path);
148  if(!@file_exists($p)){
149    $p = realpath(DOKU_INC.$path);
150    if(!@file_exists($p)){
151      return '';
152    }
153  }
154
155  // check writability
156  if(!@is_writable($p)){
157    return '';
158  }
159
160  // check accessability (execute bit) for directories
161  if(@is_dir($p) && !@file_exists("$p/.")){
162    return '';
163  }
164
165  return $p;
166}
167
168/**
169 * Sets the internal config values fperm and dperm which, when set,
170 * will be used to change the permission of a newly created dir or
171 * file with chmod. Considers the influence of the system's umask
172 * setting the values only if needed.
173 */
174function init_creationmodes(){
175  global $conf;
176
177  // Legacy support for old umask/dmask scheme
178  unset($conf['dmask']);
179  unset($conf['fmask']);
180  unset($conf['umask']);
181  unset($conf['fperm']);
182  unset($conf['dperm']);
183
184  // get system umask, fallback to 0 if none available
185  $umask = @umask();
186  if(!$umask) $umask = 0000;
187
188  // check what is set automatically by the system on file creation
189  // and set the fperm param if it's not what we want
190  $auto_fmode = 0666 & ~$umask;
191  if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode'];
192
193  // check what is set automatically by the system on file creation
194  // and set the dperm param if it's not what we want
195  $auto_dmode = $conf['dmode'] & ~$umask;
196  if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode'];
197}
198
199/**
200 * remove magic quotes recursivly
201 *
202 * @author Andreas Gohr <andi@splitbrain.org>
203 */
204function remove_magic_quotes(&$array) {
205  foreach (array_keys($array) as $key) {
206    if (is_array($array[$key])) {
207      remove_magic_quotes($array[$key]);
208    }else {
209      $array[$key] = stripslashes($array[$key]);
210    }
211  }
212}
213
214/**
215 * Returns the full absolute URL to the directory where
216 * DokuWiki is installed in (includes a trailing slash)
217 *
218 * @author Andreas Gohr <andi@splitbrain.org>
219 */
220function getBaseURL($abs=false){
221  global $conf;
222  //if canonical url enabled always return absolute
223  if($conf['canonical']) $abs = true;
224
225  if($conf['basedir']){
226    $dir = $conf['basedir'].'/';
227  }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){
228    $dir = dirname($_SERVER['SCRIPT_NAME']).'/';
229  }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){
230    $dir = dirname($_SERVER['PHP_SELF']).'/';
231  }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
232    $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
233                         $_SERVER['SCRIPT_FILENAME']);
234    $dir = dirname('/'.$dir).'/';
235  }else{
236    $dir = './'; //probably wrong
237  }
238
239  $dir = str_replace('\\','/',$dir); #bugfix for weird WIN behaviour
240  $dir = preg_replace('#//+#','/',$dir);
241
242  //handle script in lib/exe dir
243  $dir = preg_replace('!lib/exe/$!','',$dir);
244
245  //finish here for relative URLs
246  if(!$abs) return $dir;
247
248  //use config option if available
249  if($conf['baseurl']) return $conf['baseurl'].$dir;
250
251  //split hostheader into host and port
252  list($host,$port) = explode(':',$_SERVER['HTTP_HOST']);
253  if(!$port)  $port = $_SERVER['SERVER_PORT'];
254  if(!$port)  $port = 80;
255
256  // see if HTTPS is enabled - apache leaves this empty when not available,
257  // IIS sets it to 'off', 'false' and 'disabled' are just guessing
258  if (preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){
259    $proto = 'http://';
260    if ($port == '80') {
261      $port='';
262    }
263  }else{
264    $proto = 'https://';
265    if ($port == '443') {
266      $port='';
267    }
268  }
269
270  if($port) $port = ':'.$port;
271
272  return $proto.$host.$port.$dir;
273}
274
275/**
276 * Append a PHP extension to a given file and adds an exit call
277 *
278 * This is used to migrate some old configfiles. An added PHP extension
279 * ensures the contents are not shown to webusers even if .htaccess files
280 * do not work
281 *
282 * @author Jan Decaluwe <jan@jandecaluwe.com>
283 */
284function scriptify($file) {
285  // checks
286  if (!is_readable($file)) {
287    return;
288  }
289  $fn = $file.'.php';
290  if (@file_exists($fn)) {
291    return;
292  }
293  $fh = fopen($fn, 'w');
294  if (!$fh) {
295    nice_die($fn.' is not writable. Check your permission settings!');
296  }
297  // write php exit hack first
298  fwrite($fh, "# $fn\n");
299  fwrite($fh, '# <?php exit()?>'."\n");
300  fwrite($fh, "# Don't modify the lines above\n");
301  fwrite($fh, "#\n");
302  // copy existing lines
303  $lines = file($file);
304  foreach ($lines as $line){
305    fwrite($fh, $line);
306  }
307  fclose($fh);
308  //try to rename the old file
309  @rename($file,"$file.old");
310}
311
312/**
313 * print a nice message even if no styles are loaded yet.
314 */
315function nice_die($msg){
316  echo<<<EOT
317  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
318   "http://www.w3.org/TR/html4/loose.dtd">
319  <html>
320    <head><title>DokuWiki Setup Error</title></head>
321    <body style="font-family: Arial, sans-serif">
322      <div style="width:60%; margin: auto; background-color: #fcc;
323                  border: 1px solid #faa; padding: 0.5em 1em;">
324      <h1 style="font-size: 120%">DokuWiki Setup Error</h1>
325      <p>$msg</p>
326      </div>
327    </body>
328  </html>
329EOT;
330  exit;
331}
332
333
334//Setup VIM: ex: et ts=2 enc=utf-8 :
335