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