xref: /dokuwiki/inc/init.php (revision 5646f69041af08c6131237b27bd8adedd4a717e7)
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  @include_once(DOKU_CONF.'local.php');
22
23  //prepare language array
24  global $lang;
25  $lang = array();
26
27  //load the language files
28  require_once(DOKU_INC.'inc/lang/en/lang.php');
29  require_once(DOKU_INC.'inc/lang/'.$conf['lang'].'/lang.php');
30
31  // define baseURL
32  if(!defined('DOKU_BASE')) define('DOKU_BASE',getBaseURL());
33  if(!defined('DOKU_URL'))  define('DOKU_URL',getBaseURL(true));
34
35  // define Plugin dir
36  if(!defined('DOKU_PLUGIN'))  define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
37
38  // define main script
39  if(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php');
40
41  // define Template baseURL
42  if(!defined('DOKU_TPL')) define('DOKU_TPL',
43                                  DOKU_BASE.'lib/tpl/'.$conf['template'].'/');
44
45  // define real Template directory
46  if(!defined('DOKU_TPLINC')) define('DOKU_TPLINC',
47                                  DOKU_INC.'lib/tpl/'.$conf['template'].'/');
48
49  // make session rewrites XHTML compliant
50  @ini_set('arg_separator.output', '&amp;');
51
52  // init session
53  if (!headers_sent() && !defined('NOSESSION')){
54    session_name("DokuWiki");
55    session_start();
56  }
57
58  // kill magic quotes
59  if (get_magic_quotes_gpc()) {
60    if (!empty($_GET))    remove_magic_quotes($_GET);
61    if (!empty($_POST))   remove_magic_quotes($_POST);
62    if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE);
63    if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST);
64    if (!empty($_SESSION)) remove_magic_quotes($_SESSION);
65    @ini_set('magic_quotes_gpc', 0);
66  }
67  @set_magic_quotes_runtime(0);
68  @ini_set('magic_quotes_sybase',0);
69
70  // disable gzip if not available
71  if($conf['usegzip'] && !function_exists('gzopen')){
72    $conf['usegzip'] = 0;
73  }
74
75  // remember original umask
76  $conf['oldumask'] = umask();
77
78  // make real paths and check them
79  init_paths();
80  init_files();
81
82  // automatic upgrade to script versions of certain files
83  scriptify(DOKU_CONF.'users.auth');
84  scriptify(DOKU_CONF.'acl.auth');
85
86
87/**
88 * Checks paths from config file
89 */
90function init_paths(){
91  global $conf;
92
93  $paths = array('datadir'   => 'pages',
94                 'olddir'    => 'attic',
95                 'mediadir'  => 'media',
96                 'metadir'   => 'meta',
97                 'cachedir'  => 'cache',
98                 'lockdir'   => 'locks',
99                 'changelog' => 'changes.log');
100
101  foreach($paths as $c => $p){
102    if(!$conf[$c])   $conf[$c] = $conf['savedir'].'/'.$p;
103    $conf[$c]        = init_path($conf[$c]);
104    if(!$conf[$c])   die("$c does not exist or isn't writable. Check config!");
105  }
106}
107
108/**
109 * Checks the existance of certain files and creates them if missing
110 */
111function init_files(){
112  global $conf;
113  $files = array( $conf['cachedir'].'/word.idx',
114                  $conf['cachedir'].'/page.idx',
115                  $conf['cachedir'].'/index.idx', );
116
117  foreach($files as $file){
118    if(!@file_exists($file)){
119      $fh = fopen($file,'a');
120      fclose($fh);
121    }
122  }
123}
124
125/**
126 * returns absolute path
127 *
128 * This tries the given path first, then checks in DOKU_INC
129 */
130function init_path($path){
131  $p = realpath($path);
132  if(@file_exists($p)) return $p;
133  $p = realpath(DOKU_INC.$path);
134  if(@file_exists($p)) return $p;
135  return '';
136}
137
138/**
139 * remove magic quotes recursivly
140 *
141 * @author Andreas Gohr <andi@splitbrain.org>
142 */
143function remove_magic_quotes(&$array) {
144  foreach (array_keys($array) as $key) {
145    if (is_array($array[$key])) {
146      remove_magic_quotes($array[$key]);
147    }else {
148      $array[$key] = stripslashes($array[$key]);
149    }
150  }
151}
152
153/**
154 * Returns the full absolute URL to the directory where
155 * DokuWiki is installed in (includes a trailing slash)
156 *
157 * @author Andreas Gohr <andi@splitbrain.org>
158 */
159function getBaseURL($abs=false){
160  global $conf;
161  //if canonical url enabled always return absolute
162  if($conf['canonical']) $abs = true;
163
164  if($conf['basedir']){
165    $dir = $conf['basedir'].'/';
166  }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){
167    $dir = dirname($_SERVER['SCRIPT_NAME']).'/';
168  }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){
169    $dir = dirname($_SERVER['PHP_SELF']).'/';
170  }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
171    $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
172                         $_SERVER['SCRIPT_FILENAME']);
173    $dir = dirname('/'.$dir).'/';
174  }else{
175    $dir = './'; //probably wrong
176  }
177
178  $dir = str_replace('\\','/',$dir); #bugfix for weird WIN behaviour
179  $dir = preg_replace('#//+#','/',$dir);
180
181  //handle script in lib/exe dir
182  $dir = preg_replace('!lib/exe/$!','',$dir);
183
184  //finish here for relative URLs
185  if(!$abs) return $dir;
186
187  //use config option if available
188  if($conf['baseurl']) return $conf['baseurl'].$dir;
189
190  //split hostheader into host and port
191  list($host,$port) = explode(':',$_SERVER['HTTP_HOST']);
192  if(!$port)  $port = $_SERVER['SERVER_PORT'];
193  if(!$port)  $port = 80;
194
195  // see if HTTPS is enabled - apache leaves this empty when not available,
196  // IIS sets it to 'off', 'false' and 'disabled' are just guessing
197  if (preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){
198    $proto = 'http://';
199    if ($port == '80') {
200      $port='';
201    }
202  }else{
203    $proto = 'https://';
204    if ($port == '443') {
205      $port='';
206    }
207  }
208
209  if($port) $port = ':'.$port;
210
211  return $proto.$host.$port.$dir;
212}
213
214/**
215 * Append a PHP extension to a given file and adds an exit call
216 *
217 * This is used to migrate some old configfiles. An added PHP extension
218 * ensures the contents are not shown to webusers even if .htaccess files
219 * do not work
220 *
221 * @author Jan Decaluwe <jan@jandecaluwe.com>
222 */
223function scriptify($file) {
224  // checks
225  if (!is_readable($file)) {
226    return;
227  }
228  $fn = $file.'.php';
229  if (@file_exists($fn)) {
230    return;
231  }
232  $fh = fopen($fn, 'w');
233  if (!$fh) {
234    die($fn.' is not writable!');
235  }
236  // write php exit hack first
237  fwrite($fh, "# $fn\n");
238  fwrite($fh, '# <?php exit()?>'."\n");
239  fwrite($fh, "# Don't modify the lines above\n");
240  fwrite($fh, "#\n");
241  // copy existing lines
242  $lines = file($file);
243  foreach ($lines as $line){
244    fwrite($fh, $line);
245  }
246  fclose($fh);
247  //try to rename the old file
248  @rename($file,"$file.old");
249}
250
251
252//Setup VIM: ex: et ts=2 enc=utf-8 :
253