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