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