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