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