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