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