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