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