xref: /dokuwiki/inc/init.php (revision c69534d490d446200a70cac57ac202974409a8bc)
1<?php
2/**
3 * Initialize some defaults needed for DokuWiki
4 */
5
6  // start timing Dokuwiki execution
7  function delta_time($start=0) {
8    list($usec, $sec) = explode(" ", microtime());
9    return ((float)$usec+(float)$sec)-((float)$start);
10  }
11  define('DOKU_START_TIME', delta_time());
12
13  // if available load a preload config file
14  @include(fullpath(dirname(__FILE__)).'/preload.php');
15
16  // define the include path
17  if(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../').'/');
18
19  // define config path (packagers may want to change this to /etc/dokuwiki/)
20  if(!defined('DOKU_CONF')) define('DOKU_CONF',DOKU_INC.'conf/');
21
22  // check for error reporting override or set error reporting to sane values
23  if (!defined('DOKU_E_LEVEL') && @file_exists(DOKU_CONF.'report_e_all')) {
24    define('DOKU_E_LEVEL', E_ALL);
25  }
26  if (!defined('DOKU_E_LEVEL')) {
27    if(defined('E_DEPRECATED')){ // since php 5.3
28      error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
29    }else{
30      error_reporting(E_ALL ^ E_NOTICE);
31    }
32  } else {
33    error_reporting(DOKU_E_LEVEL);
34  }
35
36  // init memory caches
37  global $cache_revinfo;  $cache_revinfo = array();
38  global $cache_wikifn;   $cache_wikifn = array();
39  global $cache_cleanid;  $cache_cleanid = array();
40  global $cache_authname; $cache_authname = array();
41  global $cache_metadata; $cache_metadata = array();
42
43  //prepare config array()
44  global $conf;
45  $conf = array();
46
47  // load the config file(s)
48  require_once(DOKU_CONF.'dokuwiki.php');
49  if(@file_exists(DOKU_CONF.'local.php')){
50    require_once(DOKU_CONF.'local.php');
51  }
52
53  //prepare language array
54  global $lang;
55  $lang = array();
56
57  //load the language files
58  require_once(DOKU_INC.'inc/lang/en/lang.php');
59  if ( $conf['lang'] && $conf['lang'] != 'en' ) {
60    require_once(DOKU_INC.'inc/lang/'.$conf['lang'].'/lang.php');
61  }
62
63  //prepare license array()
64  global $license;
65  $license = array();
66
67  // load the license file(s)
68  require_once(DOKU_CONF.'license.php');
69  if(@file_exists(DOKU_CONF.'license.php')){
70    require_once(DOKU_CONF.'license.php');
71  }
72
73  // define baseURL
74  if(!defined('DOKU_REL')) define('DOKU_REL',getBaseURL(false));
75  if(!defined('DOKU_URL')) define('DOKU_URL',getBaseURL(true));
76  if(!defined('DOKU_BASE')){
77    if($conf['canonical']){
78      define('DOKU_BASE',DOKU_URL);
79    }else{
80      define('DOKU_BASE',DOKU_REL);
81    }
82  }
83
84  // define whitespace
85  if(!defined('DOKU_LF')) define ('DOKU_LF',"\n");
86  if(!defined('DOKU_TAB')) define ('DOKU_TAB',"\t");
87
88  // define cookie and session id
89  if (!defined('DOKU_COOKIE')) define('DOKU_COOKIE', 'DW'.md5(DOKU_REL));
90
91  // define Plugin dir
92  if(!defined('DOKU_PLUGIN'))  define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
93
94  // define main script
95  if(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php');
96
97  // define Template baseURL
98  if(!defined('DOKU_TPL')) define('DOKU_TPL',
99                                  DOKU_BASE.'lib/tpl/'.$conf['template'].'/');
100
101  // define real Template directory
102  if(!defined('DOKU_TPLINC')) define('DOKU_TPLINC',
103                                  DOKU_INC.'lib/tpl/'.$conf['template'].'/');
104
105  // make session rewrites XHTML compliant
106  @ini_set('arg_separator.output', '&amp;');
107
108  // make sure global zlib does not interfere FS#1132
109  @ini_set('zlib.output_compression', 'off');
110
111  // increase PCRE backtrack limit
112  @ini_set('pcre.backtrack_limit', '20971520');
113
114  // enable gzip compression
115  if ($conf['gzip_output'] &&
116      !defined('DOKU_DISABLE_GZIP_OUTPUT') &&
117      function_exists('ob_gzhandler') &&
118      preg_match('/gzip|deflate/', $_SERVER['HTTP_ACCEPT_ENCODING'])) {
119    ob_start('ob_gzhandler');
120  }
121
122  // init session
123  if (!headers_sent() && !defined('NOSESSION')){
124    session_name("DokuWiki");
125    if (version_compare(PHP_VERSION, '5.2.0', '>')) {
126      session_set_cookie_params(0,DOKU_REL,'',($conf['securecookie'] && is_ssl()),true);
127    }else{
128      session_set_cookie_params(0,DOKU_REL,'',($conf['securecookie'] && is_ssl()));
129    }
130    session_start();
131
132    // load left over messages
133    if(isset($_SESSION[DOKU_COOKIE]['msg'])){
134      $MSG = $_SESSION[DOKU_COOKIE]['msg'];
135      unset($_SESSION[DOKU_COOKIE]['msg']);
136    }
137  }
138
139  // kill magic quotes
140  if (get_magic_quotes_gpc() && !defined('MAGIC_QUOTES_STRIPPED')) {
141    if (!empty($_GET))    remove_magic_quotes($_GET);
142    if (!empty($_POST))   remove_magic_quotes($_POST);
143    if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE);
144    if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST);
145    @ini_set('magic_quotes_gpc', 0);
146    define('MAGIC_QUOTES_STRIPPED',1);
147  }
148  @set_magic_quotes_runtime(0);
149  @ini_set('magic_quotes_sybase',0);
150
151  // don't let cookies ever interfere with request vars
152  $_REQUEST = array_merge($_GET,$_POST);
153
154  // we don't want a purge URL to be digged
155  if($_REQUEST['purge'] && $_SERVER['HTTP_REFERER']) unset($_REQUEST['purge']);
156
157  // disable gzip if not available
158  if($conf['compression'] == 'bz2' && !function_exists('bzopen')){
159    $conf['compression'] = 'gz';
160  }
161  if($conf['compression'] == 'gz' && !function_exists('gzopen')){
162    $conf['compression'] = 0;
163  }
164
165  // fix dateformat for upgraders
166  if(strpos($conf['dformat'],'%') === false){
167    $conf['dformat'] = '%Y/%m/%d %H:%M';
168  }
169
170  // precalculate file creation modes
171  init_creationmodes();
172
173  // make real paths and check them
174  init_paths();
175  init_files();
176
177  // automatic upgrade to script versions of certain files
178  scriptify(DOKU_CONF.'users.auth');
179  scriptify(DOKU_CONF.'acl.auth');
180
181
182/**
183 * Checks paths from config file
184 */
185function init_paths(){
186  global $conf;
187
188  $paths = array('datadir'   => 'pages',
189                 'olddir'    => 'attic',
190                 'mediadir'  => 'media',
191                 'metadir'   => 'meta',
192                 'cachedir'  => 'cache',
193                 'indexdir'  => 'index',
194                 'lockdir'   => 'locks',
195                 'tmpdir'    => 'tmp');
196
197  foreach($paths as $c => $p){
198    if(empty($conf[$c]))  $conf[$c] = $conf['savedir'].'/'.$p;
199    $conf[$c]             = init_path($conf[$c]);
200    if(empty($conf[$c]))  nice_die("The $c ('$p') does not exist, isn't accessible or writable.
201                               You should check your config and permission settings.
202                               Or maybe you want to <a href=\"install.php\">run the
203                               installer</a>?");
204  }
205
206  // path to old changelog only needed for upgrading
207  $conf['changelog_old'] = init_path((isset($conf['changelog']))?($conf['changelog']):($conf['savedir'].'/changes.log'));
208  if ($conf['changelog_old']=='') { unset($conf['changelog_old']); }
209  // hardcoded changelog because it is now a cache that lives in meta
210  $conf['changelog'] = $conf['metadir'].'/_dokuwiki.changes';
211}
212
213/**
214 * Checks the existance of certain files and creates them if missing.
215 */
216function init_files(){
217  global $conf;
218
219  $files = array( $conf['indexdir'].'/page.idx');
220
221  foreach($files as $file){
222    if(!@file_exists($file)){
223      $fh = @fopen($file,'a');
224      if($fh){
225        fclose($fh);
226        if($conf['fperm']) chmod($file, $conf['fperm']);
227      }else{
228        nice_die("$file is not writable. Check your permissions settings!");
229      }
230    }
231  }
232}
233
234/**
235 * Returns absolute path
236 *
237 * This tries the given path first, then checks in DOKU_INC.
238 * Check for accessability on directories as well.
239 *
240 * @author Andreas Gohr <andi@splitbrain.org>
241 */
242function init_path($path){
243  // check existance
244  $p = fullpath($path);
245  if(!@file_exists($p)){
246    $p = fullpath(DOKU_INC.$path);
247    if(!@file_exists($p)){
248      return '';
249    }
250  }
251
252  // check writability
253  if(!@is_writable($p)){
254    return '';
255  }
256
257  // check accessability (execute bit) for directories
258  if(@is_dir($p) && !@file_exists("$p/.")){
259    return '';
260  }
261
262  return $p;
263}
264
265/**
266 * Sets the internal config values fperm and dperm which, when set,
267 * will be used to change the permission of a newly created dir or
268 * file with chmod. Considers the influence of the system's umask
269 * setting the values only if needed.
270 */
271function init_creationmodes(){
272  global $conf;
273
274  // Legacy support for old umask/dmask scheme
275  unset($conf['dmask']);
276  unset($conf['fmask']);
277  unset($conf['umask']);
278  unset($conf['fperm']);
279  unset($conf['dperm']);
280
281  // get system umask, fallback to 0 if none available
282  $umask = @umask();
283  if(!$umask) $umask = 0000;
284
285  // check what is set automatically by the system on file creation
286  // and set the fperm param if it's not what we want
287  $auto_fmode = 0666 & ~$umask;
288  if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode'];
289
290  // check what is set automatically by the system on file creation
291  // and set the dperm param if it's not what we want
292  $auto_dmode = $conf['dmode'] & ~$umask;
293  if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode'];
294}
295
296/**
297 * remove magic quotes recursivly
298 *
299 * @author Andreas Gohr <andi@splitbrain.org>
300 */
301function remove_magic_quotes(&$array) {
302  foreach (array_keys($array) as $key) {
303      // handle magic quotes in keynames (breaks order)
304      $sk = stripslashes($key);
305      if($sk != $key){
306          $array[$sk] = $array[$key];
307          unset($array[$key]);
308          $key = $sk;
309      }
310
311      // do recursion if needed
312      if (is_array($array[$key])) {
313          remove_magic_quotes($array[$key]);
314      }else {
315          $array[$key] = stripslashes($array[$key]);
316      }
317  }
318}
319
320/**
321 * Returns the full absolute URL to the directory where
322 * DokuWiki is installed in (includes a trailing slash)
323 *
324 * @author Andreas Gohr <andi@splitbrain.org>
325 */
326function getBaseURL($abs=null){
327  global $conf;
328  //if canonical url enabled always return absolute
329  if(is_null($abs)) $abs = $conf['canonical'];
330
331  if($conf['basedir']){
332    $dir = $conf['basedir'];
333  }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){
334    $dir = dirname($_SERVER['SCRIPT_NAME']);
335  }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){
336    $dir = dirname($_SERVER['PHP_SELF']);
337  }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
338    $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
339                         $_SERVER['SCRIPT_FILENAME']);
340    $dir = dirname('/'.$dir);
341  }else{
342    $dir = '.'; //probably wrong
343  }
344
345  $dir = str_replace('\\','/',$dir);             // bugfix for weird WIN behaviour
346  $dir = preg_replace('#//+#','/',"/$dir/");     // ensure leading and trailing slashes
347
348  //handle script in lib/exe dir
349  $dir = preg_replace('!lib/exe/$!','',$dir);
350
351  //handle script in lib/plugins dir
352  $dir = preg_replace('!lib/plugins/.*$!','',$dir);
353
354  //finish here for relative URLs
355  if(!$abs) return $dir;
356
357  //use config option if available, trim any slash from end of baseurl to avoid multiple consecutive slashes in the path
358  if($conf['baseurl']) return rtrim($conf['baseurl'],'/').$dir;
359
360  //split hostheader into host and port
361  list($host,$port) = explode(':',$_SERVER['HTTP_HOST']);
362  if(!$port)  $port = $_SERVER['SERVER_PORT'];
363  if(!$port)  $port = 80;
364
365  if(!is_ssl()){
366    $proto = 'http://';
367    if ($port == '80') {
368      $port='';
369    }
370  }else{
371    $proto = 'https://';
372    if ($port == '443') {
373      $port='';
374    }
375  }
376
377  if($port) $port = ':'.$port;
378
379  return $proto.$host.$port.$dir;
380}
381
382/**
383 * Check if accessed via HTTPS
384 *
385 * Apache leaves ,$_SERVER['HTTPS'] empty when not available, IIS sets it to 'off'.
386 * 'false' and 'disabled' are just guessing
387 *
388 * @returns bool true when SSL is active
389 */
390function is_ssl(){
391    if (preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){
392        return false;
393    }else{
394        return true;
395    }
396}
397
398/**
399 * Append a PHP extension to a given file and adds an exit call
400 *
401 * This is used to migrate some old configfiles. An added PHP extension
402 * ensures the contents are not shown to webusers even if .htaccess files
403 * do not work
404 *
405 * @author Jan Decaluwe <jan@jandecaluwe.com>
406 */
407function scriptify($file) {
408  // checks
409  if (!is_readable($file)) {
410    return;
411  }
412  $fn = $file.'.php';
413  if (@file_exists($fn)) {
414    return;
415  }
416  $fh = fopen($fn, 'w');
417  if (!$fh) {
418    nice_die($fn.' is not writable. Check your permission settings!');
419  }
420  // write php exit hack first
421  fwrite($fh, "# $fn\n");
422  fwrite($fh, '# <?php exit()?>'."\n");
423  fwrite($fh, "# Don't modify the lines above\n");
424  fwrite($fh, "#\n");
425  // copy existing lines
426  $lines = file($file);
427  foreach ($lines as $line){
428    fwrite($fh, $line);
429  }
430  fclose($fh);
431  //try to rename the old file
432  io_rename($file,"$file.old");
433}
434
435/**
436 * print a nice message even if no styles are loaded yet.
437 */
438function nice_die($msg){
439  echo<<<EOT
440  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
441   "http://www.w3.org/TR/html4/loose.dtd">
442  <html>
443    <head><title>DokuWiki Setup Error</title></head>
444    <body style="font-family: Arial, sans-serif">
445      <div style="width:60%; margin: auto; background-color: #fcc;
446                  border: 1px solid #faa; padding: 0.5em 1em;">
447      <h1 style="font-size: 120%">DokuWiki Setup Error</h1>
448      <p>$msg</p>
449      </div>
450    </body>
451  </html>
452EOT;
453  exit;
454}
455
456
457/**
458 * A realpath() replacement
459 *
460 * This function behaves similar to PHP's realpath() but does not resolve
461 * symlinks or accesses upper directories
462 *
463 * @author Andreas Gohr <andi@splitbrain.org>
464 * @author <richpageau at yahoo dot co dot uk>
465 * @link   http://de3.php.net/manual/en/function.realpath.php#75992
466 */
467function fullpath($path,$exists=false){
468    static $run = 0;
469    $root  = '';
470    $iswin = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' || $GLOBALS['DOKU_UNITTEST_ASSUME_WINDOWS']);
471
472    // find the (indestructable) root of the path - keeps windows stuff intact
473    if($path{0} == '/'){
474        $root = '/';
475    }elseif($iswin){
476        // match drive letter and UNC paths
477        if(preg_match('!^([a-zA-z]:)(.*)!',$path,$match)){
478            $root = $match[1];
479            $path = $match[2];
480        }else if(preg_match('!^(\\\\\\\\[^\\\\/]+\\\\[^\\\\/]+[\\\\/])(.*)!',$path,$match)){
481            $root = $match[1];
482            $path = $match[2];
483        }
484    }
485    $path = str_replace('\\','/',$path);
486
487    // if the given path wasn't absolute already, prepend the script path and retry
488    if(!$root){
489        $base = dirname($_SERVER['SCRIPT_FILENAME']);
490        $path = $base.'/'.$path;
491        if($run == 0){ // avoid endless recursion when base isn't absolute for some reason
492            $run++;
493            return fullpath($path,$exists);
494        }
495    }
496    $run = 0;
497
498    // canonicalize
499    $path=explode('/', $path);
500    $newpath=array();
501    foreach($path as $p) {
502        if ($p === '' || $p === '.') continue;
503           if ($p==='..') {
504              array_pop($newpath);
505              continue;
506        }
507        array_push($newpath, $p);
508    }
509    $finalpath = $root.implode('/', $newpath);
510
511    // check for existance when needed (except when unit testing)
512    if($exists && !defined('DOKU_UNITTEST') && !@file_exists($finalpath)) {
513        return false;
514    }
515    return $finalpath;
516}
517
518
519
520//Setup VIM: ex: et ts=2 enc=utf-8 :
521