xref: /dokuwiki/inc/init.php (revision 3bb35907e94c813b0bd286c6e3d1b12359c84618)
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])   die("$c does not exist, isn't accessable or writable. Check config and permissions!");
132  }
133}
134
135/**
136 * Checks the existance of certain files and creates them if missing.
137 */
138function init_files(){
139  global $conf;
140
141  $files = array( $conf['cachedir'].'/word.idx',
142                  $conf['cachedir'].'/page.idx',
143                  $conf['cachedir'].'/index.idx');
144
145  foreach($files as $file){
146    if(!@file_exists($file)){
147      $fh = @fopen($file,'a');
148      if($fh){
149        fclose($fh);
150        if(isset($conf['fmask'])) { chmod($file, $conf['fmask']); }
151      }else{
152        die("$file is not writable. Check permissions!");
153      }
154    }
155  }
156}
157
158/**
159 * Returns absolute path
160 *
161 * This tries the given path first, then checks in DOKU_INC.
162 * Check for accessability on directories as well.
163 *
164 * @author Andreas Gohr <andi@splitbrain.org>
165 */
166function init_path($path){
167  // check existance
168  $p = realpath($path);
169  if(!@file_exists($p)){
170    $p = realpath(DOKU_INC.$path);
171    if(!@file_exists($p)){
172      return '';
173    }
174  }
175
176  // check writability
177  if(!@is_writable($p)){
178    return '';
179  }
180
181  // check accessability (execute bit) for directories
182  if(@is_dir($p) && !@file_exists("$p/.")){
183    return '';
184  }
185
186  return $p;
187}
188
189/**
190 * remove magic quotes recursivly
191 *
192 * @author Andreas Gohr <andi@splitbrain.org>
193 */
194function remove_magic_quotes(&$array) {
195  foreach (array_keys($array) as $key) {
196    if (is_array($array[$key])) {
197      remove_magic_quotes($array[$key]);
198    }else {
199      $array[$key] = stripslashes($array[$key]);
200    }
201  }
202}
203
204/**
205 * Returns the full absolute URL to the directory where
206 * DokuWiki is installed in (includes a trailing slash)
207 *
208 * @author Andreas Gohr <andi@splitbrain.org>
209 */
210function getBaseURL($abs=false){
211  global $conf;
212  //if canonical url enabled always return absolute
213  if($conf['canonical']) $abs = true;
214
215  if($conf['basedir']){
216    $dir = $conf['basedir'].'/';
217  }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){
218    $dir = dirname($_SERVER['SCRIPT_NAME']).'/';
219  }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){
220    $dir = dirname($_SERVER['PHP_SELF']).'/';
221  }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
222    $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
223                         $_SERVER['SCRIPT_FILENAME']);
224    $dir = dirname('/'.$dir).'/';
225  }else{
226    $dir = './'; //probably wrong
227  }
228
229  $dir = str_replace('\\','/',$dir); #bugfix for weird WIN behaviour
230  $dir = preg_replace('#//+#','/',$dir);
231
232  //handle script in lib/exe dir
233  $dir = preg_replace('!lib/exe/$!','',$dir);
234
235  //finish here for relative URLs
236  if(!$abs) return $dir;
237
238  //use config option if available
239  if($conf['baseurl']) return $conf['baseurl'].$dir;
240
241  //split hostheader into host and port
242  list($host,$port) = explode(':',$_SERVER['HTTP_HOST']);
243  if(!$port)  $port = $_SERVER['SERVER_PORT'];
244  if(!$port)  $port = 80;
245
246  // see if HTTPS is enabled - apache leaves this empty when not available,
247  // IIS sets it to 'off', 'false' and 'disabled' are just guessing
248  if (preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){
249    $proto = 'http://';
250    if ($port == '80') {
251      $port='';
252    }
253  }else{
254    $proto = 'https://';
255    if ($port == '443') {
256      $port='';
257    }
258  }
259
260  if($port) $port = ':'.$port;
261
262  return $proto.$host.$port.$dir;
263}
264
265/**
266 * Append a PHP extension to a given file and adds an exit call
267 *
268 * This is used to migrate some old configfiles. An added PHP extension
269 * ensures the contents are not shown to webusers even if .htaccess files
270 * do not work
271 *
272 * @author Jan Decaluwe <jan@jandecaluwe.com>
273 */
274function scriptify($file) {
275  // checks
276  if (!is_readable($file)) {
277    return;
278  }
279  $fn = $file.'.php';
280  if (@file_exists($fn)) {
281    return;
282  }
283  $fh = fopen($fn, 'w');
284  if (!$fh) {
285    die($fn.' is not writable!');
286  }
287  // write php exit hack first
288  fwrite($fh, "# $fn\n");
289  fwrite($fh, '# <?php exit()?>'."\n");
290  fwrite($fh, "# Don't modify the lines above\n");
291  fwrite($fh, "#\n");
292  // copy existing lines
293  $lines = file($file);
294  foreach ($lines as $line){
295    fwrite($fh, $line);
296  }
297  fclose($fh);
298  //try to rename the old file
299  @rename($file,"$file.old");
300}
301
302
303//Setup VIM: ex: et ts=2 enc=utf-8 :
304