xref: /dokuwiki/inc/init.php (revision 36df6fa3069d55db3c35724c4f465bde9c50b53a)
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
75  // automatic upgrade to script versions of certain files
76  scriptify(DOKU_CONF.'users.auth');
77  scriptify(DOKU_CONF.'acl.auth');
78
79
80/**
81 * Checks paths from config file
82 */
83function init_paths(){
84  global $conf;
85
86  $paths = array('datadir'   => 'pages',
87                 'olddir'    => 'attic',
88                 'mediadir'  => 'media',
89                 'metadir'   => 'meta',
90                 'cachedir'  => 'cache',
91                 'lockdir'   => 'locks',
92                 'changelog' => 'changes.log');
93
94  foreach($paths as $c => $p){
95
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 * returns absolute path
104 *
105 * This tries the given path first, then checks in DOKU_INC
106 */
107function init_path($path){
108  $p = realpath($path);
109  if(@file_exists($p)) return $p;
110  $p = realpath(DOKU_INC.$path);
111  if(@file_exists($p)) return $p;
112  return '';
113}
114
115/**
116 * remove magic quotes recursivly
117 *
118 * @author Andreas Gohr <andi@splitbrain.org>
119 */
120function remove_magic_quotes(&$array) {
121  foreach (array_keys($array) as $key) {
122    if (is_array($array[$key])) {
123      remove_magic_quotes($array[$key]);
124    }else {
125      $array[$key] = stripslashes($array[$key]);
126    }
127  }
128}
129
130/**
131 * Returns the full absolute URL to the directory where
132 * DokuWiki is installed in (includes a trailing slash)
133 *
134 * @author Andreas Gohr <andi@splitbrain.org>
135 */
136function getBaseURL($abs=false){
137  global $conf;
138  //if canonical url enabled always return absolute
139  if($conf['canonical']) $abs = true;
140
141  if($conf['basedir']){
142    $dir = $conf['basedir'].'/';
143  }elseif($_SERVER['SCRIPT_NAME']){
144    $dir = dirname($_SERVER['SCRIPT_NAME']).'/';
145  }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
146    $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
147                         $_SERVER['SCRIPT_FILENAME']);
148    $dir = dirname('/'.$dir).'/';
149  }else{
150    $dir = dirname($_SERVER['PHP_SELF']).'/';
151  }
152
153  $dir = str_replace('\\','/',$dir); #bugfix for weird WIN behaviour
154  $dir = preg_replace('#//+#','/',$dir);
155
156  //handle script in lib/exe dir
157  $dir = preg_replace('!lib/exe/$!','',$dir);
158
159  //finish here for relative URLs
160  if(!$abs) return $dir;
161
162  $port = ':'.$_SERVER['SERVER_PORT'];
163  //remove port from hostheader as sent by IE
164  $host = preg_replace('/:.*$/','',$_SERVER['HTTP_HOST']);
165
166  // see if HTTPS is enabled - apache leaves this empty when not available,
167  // IIS sets it to 'off', 'false' and 'disabled' are just guessing
168  if (preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){
169    $proto = 'http://';
170    if ($_SERVER['SERVER_PORT'] == '80') {
171      $port='';
172    }
173  }else{
174    $proto = 'https://';
175    if ($_SERVER['SERVER_PORT'] == '443') {
176      $port='';
177    }
178  }
179
180  return $proto.$host.$port.$dir;
181}
182
183/**
184 * Append a PHP extension to a given file and adds an exit call
185 *
186 * This is used to migrate some old configfiles. An added PHP extension
187 * ensures the contents are not shown to webusers even if .htaccess files
188 * do not work
189 *
190 * @author Jan Decaluwe <jan@jandecaluwe.com>
191 */
192function scriptify($file) {
193  // checks
194  if (!is_readable($file)) {
195    return;
196  }
197  $fn = $file.'.php';
198  if (@file_exists($fn)) {
199    return;
200  }
201  $fh = fopen($fn, 'w');
202  if (!$fh) {
203    die($fn.' is not writable!');
204  }
205  // write php exit hack first
206  fwrite($fh, "# $fn\n");
207  fwrite($fh, '# <?php exit()?>'."\n");
208  fwrite($fh, "# Don't modify the lines above\n");
209  fwrite($fh, "#\n");
210  // copy existing lines
211  $lines = file($file);
212  foreach ($lines as $line){
213    fwrite($fh, $line);
214  }
215  fclose($fh);
216  //try to rename the old file
217  @rename($file,"$file.old");
218}
219
220
221//Setup VIM: ex: et ts=2 enc=utf-8 :
222