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