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