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