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