xref: /dokuwiki/inc/init.php (revision a2c0246e0f8908d1d0c4e7f242a76e38a6615aea)
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);
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  // Legacy support for old umask/dmask scheme
81  if(isset($conf['dmask'])) {
82    unset($conf['dmask']);
83    unset($conf['fmask']);
84    unset($conf['umask']);
85  }
86
87  // Set defaults for fmode, dmode and umask.
88  if(!isset($conf['fmode'])) {
89    $conf['fmode'] = 0666;
90  }
91  if(!isset($conf['dmode'])) {
92    $conf['dmode'] = 0777;
93  }
94  if(!isset($conf['umask'])) {
95    $conf['umask'] = umask();
96  }
97
98  // Precalculate the fmask and dmask, so we can set later.
99  if(($conf['umask'] != umask()) or ($conf['fmode'] != 0666)) {
100    $conf['fmask'] = $conf['fmode'] & ~$conf['umask'];
101  }
102  if(($conf['umask'] != umask()) or ($conf['dmode'] != 0666)) {
103    $conf['dmask'] = $conf['dmode'] & ~$conf['umask'];
104  }
105
106  // make real paths and check them
107  init_paths();
108  init_files();
109
110  // automatic upgrade to script versions of certain files
111  scriptify(DOKU_CONF.'users.auth');
112  scriptify(DOKU_CONF.'acl.auth');
113
114
115/**
116 * Checks paths from config file
117 */
118function init_paths(){
119  global $conf;
120
121  $paths = array('datadir'   => 'pages',
122                 'olddir'    => 'attic',
123                 'mediadir'  => 'media',
124                 'metadir'   => 'meta',
125                 'cachedir'  => 'cache',
126                 'lockdir'   => 'locks',
127                 'changelog' => 'changes.log');
128
129  foreach($paths as $c => $p){
130    if(!$conf[$c])   $conf[$c] = $conf['savedir'].'/'.$p;
131    $conf[$c]        = init_path($conf[$c]);
132    if(!$conf[$c])   nice_die("The $c does not exist, isn't accessable or writable.
133                               Check your config and permission settings!");
134  }
135}
136
137/**
138 * Checks the existance of certain files and creates them if missing.
139 */
140function init_files(){
141  global $conf;
142
143  $files = array( $conf['cachedir'].'/word.idx',
144                  $conf['cachedir'].'/page.idx',
145                  $conf['cachedir'].'/index.idx');
146
147  foreach($files as $file){
148    if(!@file_exists($file)){
149      $fh = @fopen($file,'a');
150      if($fh){
151        fclose($fh);
152        if(isset($conf['fmask'])) { chmod($file, $conf['fmask']); }
153      }else{
154        nice_die("$file is not writable. Check your permissions settings!");
155      }
156    }
157  }
158}
159
160/**
161 * Returns absolute path
162 *
163 * This tries the given path first, then checks in DOKU_INC.
164 * Check for accessability on directories as well.
165 *
166 * @author Andreas Gohr <andi@splitbrain.org>
167 */
168function init_path($path){
169  // check existance
170  $p = realpath($path);
171  if(!@file_exists($p)){
172    $p = realpath(DOKU_INC.$path);
173    if(!@file_exists($p)){
174      return '';
175    }
176  }
177
178  // check writability
179  if(!@is_writable($p)){
180    return '';
181  }
182
183  // check accessability (execute bit) for directories
184  if(@is_dir($p) && !@file_exists("$p/.")){
185    return '';
186  }
187
188  return $p;
189}
190
191/**
192 * remove magic quotes recursivly
193 *
194 * @author Andreas Gohr <andi@splitbrain.org>
195 */
196function remove_magic_quotes(&$array) {
197  foreach (array_keys($array) as $key) {
198    if (is_array($array[$key])) {
199      remove_magic_quotes($array[$key]);
200    }else {
201      $array[$key] = stripslashes($array[$key]);
202    }
203  }
204}
205
206/**
207 * Returns the full absolute URL to the directory where
208 * DokuWiki is installed in (includes a trailing slash)
209 *
210 * @author Andreas Gohr <andi@splitbrain.org>
211 */
212function getBaseURL($abs=false){
213  global $conf;
214  //if canonical url enabled always return absolute
215  if($conf['canonical']) $abs = true;
216
217  if($conf['basedir']){
218    $dir = $conf['basedir'].'/';
219  }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){
220    $dir = dirname($_SERVER['SCRIPT_NAME']).'/';
221  }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){
222    $dir = dirname($_SERVER['PHP_SELF']).'/';
223  }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
224    $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
225                         $_SERVER['SCRIPT_FILENAME']);
226    $dir = dirname('/'.$dir).'/';
227  }else{
228    $dir = './'; //probably wrong
229  }
230
231  $dir = str_replace('\\','/',$dir); #bugfix for weird WIN behaviour
232  $dir = preg_replace('#//+#','/',$dir);
233
234  //handle script in lib/exe dir
235  $dir = preg_replace('!lib/exe/$!','',$dir);
236
237  //finish here for relative URLs
238  if(!$abs) return $dir;
239
240  //use config option if available
241  if($conf['baseurl']) return $conf['baseurl'].$dir;
242
243  //split hostheader into host and port
244  list($host,$port) = explode(':',$_SERVER['HTTP_HOST']);
245  if(!$port)  $port = $_SERVER['SERVER_PORT'];
246  if(!$port)  $port = 80;
247
248  // see if HTTPS is enabled - apache leaves this empty when not available,
249  // IIS sets it to 'off', 'false' and 'disabled' are just guessing
250  if (preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){
251    $proto = 'http://';
252    if ($port == '80') {
253      $port='';
254    }
255  }else{
256    $proto = 'https://';
257    if ($port == '443') {
258      $port='';
259    }
260  }
261
262  if($port) $port = ':'.$port;
263
264  return $proto.$host.$port.$dir;
265}
266
267/**
268 * Append a PHP extension to a given file and adds an exit call
269 *
270 * This is used to migrate some old configfiles. An added PHP extension
271 * ensures the contents are not shown to webusers even if .htaccess files
272 * do not work
273 *
274 * @author Jan Decaluwe <jan@jandecaluwe.com>
275 */
276function scriptify($file) {
277  // checks
278  if (!is_readable($file)) {
279    return;
280  }
281  $fn = $file.'.php';
282  if (@file_exists($fn)) {
283    return;
284  }
285  $fh = fopen($fn, 'w');
286  if (!$fh) {
287    nice_die($fn.' is not writable. Check your permission settings!');
288  }
289  // write php exit hack first
290  fwrite($fh, "# $fn\n");
291  fwrite($fh, '# <?php exit()?>'."\n");
292  fwrite($fh, "# Don't modify the lines above\n");
293  fwrite($fh, "#\n");
294  // copy existing lines
295  $lines = file($file);
296  foreach ($lines as $line){
297    fwrite($fh, $line);
298  }
299  fclose($fh);
300  //try to rename the old file
301  @rename($file,"$file.old");
302}
303
304/**
305 * print a nice message even if no styles are loaded yet.
306 */
307function nice_die($msg){
308  echo<<<EOT
309  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
310   "http://www.w3.org/TR/html4/loose.dtd">
311  <html>
312    <head><title>DokuWiki Setup Error</title></head>
313    <body style="font-family: Arial, sans-serif">
314      <div style="width:60%; margin: auto; background-color: #fcc;
315                  border: 1px solid #faa; padding: 0.5em 1em;">
316      <h1 style="font-size: 120%">DokuWiki Setup Error</h1>
317      <p>$msg</p>
318      </div>
319    </body>
320  </html>
321EOT;
322  exit;
323}
324
325
326//Setup VIM: ex: et ts=2 enc=utf-8 :
327