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