xref: /dokuwiki/inc/init.php (revision 4befca0c401cd5d13a1de3ac5e17477757a8c284)
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
185  $umask = umask();
186
187  // check what is set automatically by the system on file creation
188  // and set the fperm param if it's not what we want
189  $auto_fmode = 0666 & ~$umask;
190  if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode'];
191
192  // check what is set automatically by the system on file creation
193  // and set the dperm param if it's not what we want
194  $auto_dmode = $conf['dmode'] & ~$umask;
195  if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode'];
196}
197
198/**
199 * remove magic quotes recursivly
200 *
201 * @author Andreas Gohr <andi@splitbrain.org>
202 */
203function remove_magic_quotes(&$array) {
204  foreach (array_keys($array) as $key) {
205    if (is_array($array[$key])) {
206      remove_magic_quotes($array[$key]);
207    }else {
208      $array[$key] = stripslashes($array[$key]);
209    }
210  }
211}
212
213/**
214 * Returns the full absolute URL to the directory where
215 * DokuWiki is installed in (includes a trailing slash)
216 *
217 * @author Andreas Gohr <andi@splitbrain.org>
218 */
219function getBaseURL($abs=false){
220  global $conf;
221  //if canonical url enabled always return absolute
222  if($conf['canonical']) $abs = true;
223
224  if($conf['basedir']){
225    $dir = $conf['basedir'].'/';
226  }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){
227    $dir = dirname($_SERVER['SCRIPT_NAME']).'/';
228  }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){
229    $dir = dirname($_SERVER['PHP_SELF']).'/';
230  }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
231    $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
232                         $_SERVER['SCRIPT_FILENAME']);
233    $dir = dirname('/'.$dir).'/';
234  }else{
235    $dir = './'; //probably wrong
236  }
237
238  $dir = str_replace('\\','/',$dir); #bugfix for weird WIN behaviour
239  $dir = preg_replace('#//+#','/',$dir);
240
241  //handle script in lib/exe dir
242  $dir = preg_replace('!lib/exe/$!','',$dir);
243
244  //finish here for relative URLs
245  if(!$abs) return $dir;
246
247  //use config option if available
248  if($conf['baseurl']) return $conf['baseurl'].$dir;
249
250  //split hostheader into host and port
251  list($host,$port) = explode(':',$_SERVER['HTTP_HOST']);
252  if(!$port)  $port = $_SERVER['SERVER_PORT'];
253  if(!$port)  $port = 80;
254
255  // see if HTTPS is enabled - apache leaves this empty when not available,
256  // IIS sets it to 'off', 'false' and 'disabled' are just guessing
257  if (preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){
258    $proto = 'http://';
259    if ($port == '80') {
260      $port='';
261    }
262  }else{
263    $proto = 'https://';
264    if ($port == '443') {
265      $port='';
266    }
267  }
268
269  if($port) $port = ':'.$port;
270
271  return $proto.$host.$port.$dir;
272}
273
274/**
275 * Append a PHP extension to a given file and adds an exit call
276 *
277 * This is used to migrate some old configfiles. An added PHP extension
278 * ensures the contents are not shown to webusers even if .htaccess files
279 * do not work
280 *
281 * @author Jan Decaluwe <jan@jandecaluwe.com>
282 */
283function scriptify($file) {
284  // checks
285  if (!is_readable($file)) {
286    return;
287  }
288  $fn = $file.'.php';
289  if (@file_exists($fn)) {
290    return;
291  }
292  $fh = fopen($fn, 'w');
293  if (!$fh) {
294    nice_die($fn.' is not writable. Check your permission settings!');
295  }
296  // write php exit hack first
297  fwrite($fh, "# $fn\n");
298  fwrite($fh, '# <?php exit()?>'."\n");
299  fwrite($fh, "# Don't modify the lines above\n");
300  fwrite($fh, "#\n");
301  // copy existing lines
302  $lines = file($file);
303  foreach ($lines as $line){
304    fwrite($fh, $line);
305  }
306  fclose($fh);
307  //try to rename the old file
308  @rename($file,"$file.old");
309}
310
311/**
312 * print a nice message even if no styles are loaded yet.
313 */
314function nice_die($msg){
315  echo<<<EOT
316  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
317   "http://www.w3.org/TR/html4/loose.dtd">
318  <html>
319    <head><title>DokuWiki Setup Error</title></head>
320    <body style="font-family: Arial, sans-serif">
321      <div style="width:60%; margin: auto; background-color: #fcc;
322                  border: 1px solid #faa; padding: 0.5em 1em;">
323      <h1 style="font-size: 120%">DokuWiki Setup Error</h1>
324      <p>$msg</p>
325      </div>
326    </body>
327  </html>
328EOT;
329  exit;
330}
331
332
333//Setup VIM: ex: et ts=2 enc=utf-8 :
334