xref: /dokuwiki/inc/init.php (revision ee20e7d16637625e900f518b6b46a61bfa30fe6e)
1ed7b5f09Sandi<?php
2ed7b5f09Sandi/**
3ed7b5f09Sandi * Initialize some defaults needed for DokuWiki
4ed7b5f09Sandi */
5ed7b5f09Sandi
6ed7b5f09Sandi  // define the include path
7ed7b5f09Sandi  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
8ad15db82Sandi
9c53ea5f2Sandi  // set up error reporting to sane values
10c53ea5f2Sandi  error_reporting(E_ALL ^ E_NOTICE);
11c53ea5f2Sandi
124724a577Sandi  //prepare config array()
13*ee20e7d1Sandi  global $conf;
144724a577Sandi  $conf = array();
154724a577Sandi
16ad15db82Sandi  // load the config file(s)
17ed7b5f09Sandi  require_once(DOKU_INC.'conf/dokuwiki.php');
18ad15db82Sandi  @include_once(DOKU_INC.'conf/local.php');
19ad15db82Sandi
20ad15db82Sandi  //prepare language array
21*ee20e7d1Sandi  global $lang;
22ad15db82Sandi  $lang = array();
23ed7b5f09Sandi
24ed7b5f09Sandi  // define baseURL
25ed7b5f09Sandi  if(!defined('DOKU_BASE')) define('DOKU_BASE',getBaseURL());
26ed7b5f09Sandi  if(!defined('DOKU_URL'))  define('DOKU_URL',getBaseURL(true));
27ed7b5f09Sandi
28*ee20e7d1Sandi  // define Plugin dir
29*ee20e7d1Sandi  if(!defined('DOKU_PLUGIN'))  define('DOKU_PLUGIN',DOKU_INC.'inc/plugins/');
30*ee20e7d1Sandi
31ed7b5f09Sandi  // define main script
32ed7b5f09Sandi  if(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php');
33ed7b5f09Sandi
346b13307fSandi  // define Template baseURL
356b13307fSandi  if(!defined('DOKU_TPL')) define('DOKU_TPL',
366b13307fSandi                                  DOKU_BASE.'tpl/'.$conf['template'].'/');
376b13307fSandi
38ed7b5f09Sandi  // make session rewrites XHTML compliant
393fc74836Sandi  @ini_set('arg_separator.output', '&amp;');
40ed7b5f09Sandi
41ed7b5f09Sandi  // init session
42ed7b5f09Sandi  session_name("DokuWiki");
436b13307fSandi  if (!headers_sent()) session_start();
44ed7b5f09Sandi
45ed7b5f09Sandi  // kill magic quotes
46ed7b5f09Sandi  if (get_magic_quotes_gpc()) {
47ed7b5f09Sandi    if (!empty($_GET))    remove_magic_quotes($_GET);
48ed7b5f09Sandi    if (!empty($_POST))   remove_magic_quotes($_POST);
49ed7b5f09Sandi    if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE);
50ed7b5f09Sandi    if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST);
51ed7b5f09Sandi    if (!empty($_SESSION)) remove_magic_quotes($_SESSION);
523fc74836Sandi    @ini_set('magic_quotes_gpc', 0);
53ed7b5f09Sandi  }
543fc74836Sandi  @set_magic_quotes_runtime(0);
553fc74836Sandi  @ini_set('magic_quotes_sybase',0);
56ed7b5f09Sandi
57ed7b5f09Sandi  // disable gzip if not available
58ed7b5f09Sandi  if($conf['usegzip'] && !function_exists('gzopen')){
59ed7b5f09Sandi    $conf['usegzip'] = 0;
60ed7b5f09Sandi  }
61ed7b5f09Sandi
62ed7b5f09Sandi  // remember original umask
63ed7b5f09Sandi  $conf['oldumask'] = umask();
64ed7b5f09Sandi
65ed7b5f09Sandi  // make absolute mediaweb
66ed7b5f09Sandi  if(!preg_match('#^(https?://|/)#i',$conf['mediaweb'])){
67ed7b5f09Sandi    $conf['mediaweb'] = getBaseURL().$conf['mediaweb'];
68ed7b5f09Sandi  }
69ed7b5f09Sandi
703dc3a5f1Sandi  // make real paths and check them
713dc3a5f1Sandi  $conf['datadir']       = realpath($conf['datadir']);
72e33d39a7Sandi  if(!$conf['datadir'])    die('Wrong datadir! Check config!');
733dc3a5f1Sandi  $conf['olddir']        = realpath($conf['olddir']);
74e33d39a7Sandi  if(!$conf['olddir'])     die('Wrong olddir! Check config!');
753dc3a5f1Sandi  $conf['mediadir']      = realpath($conf['mediadir']);
76b94418bbSjan  if(!$conf['mediadir'])   die('Wrong mediadir! Check config!');
77ed7b5f09Sandi
788c4f28e8Sjan  // automatic upgrade to script versions of certain files
798c4f28e8Sjan  scriptify('conf/users.auth');
808c4f28e8Sjan  scriptify('conf/acl.auth');
818c4f28e8Sjan
82ed7b5f09Sandi/**
83ed7b5f09Sandi * remove magic quotes recursivly
84ed7b5f09Sandi *
85ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
86ed7b5f09Sandi */
87ed7b5f09Sandifunction remove_magic_quotes(&$array) {
88ed7b5f09Sandi  foreach (array_keys($array) as $key) {
89ed7b5f09Sandi    if (is_array($array[$key])) {
90ed7b5f09Sandi      remove_magic_quotes($array[$key]);
91ed7b5f09Sandi    }else {
92ed7b5f09Sandi      $array[$key] = stripslashes($array[$key]);
93ed7b5f09Sandi    }
94ed7b5f09Sandi  }
95ed7b5f09Sandi}
96ed7b5f09Sandi
97ed7b5f09Sandi/**
98ed7b5f09Sandi * Returns the full absolute URL to the directory where
99ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash)
100ed7b5f09Sandi *
101ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org>
102ed7b5f09Sandi */
103ed7b5f09Sandifunction getBaseURL($abs=false){
104ed7b5f09Sandi  global $conf;
105ed7b5f09Sandi  //if canonical url enabled always return absolute
106ed7b5f09Sandi  if($conf['canonical']) $abs = true;
107ed7b5f09Sandi
10892b83b77Sandi  if($conf['basedir']){
109919eeb46Sandi    $dir = $conf['basedir'].'/';
110bdc127a4Sandi  }elseif($_SERVER['SCRIPT_NAME']){
111bdc127a4Sandi    $dir = dirname($_SERVER['SCRIPT_NAME']).'/';
112093ec9e4Sandi  }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
113093ec9e4Sandi    $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
114093ec9e4Sandi                         $_SERVER['SCRIPT_FILENAME']);
115093ec9e4Sandi    $dir = dirname('/'.$dir).'/';
11692b83b77Sandi  }else{
117ed7b5f09Sandi    $dir = dirname($_SERVER['PHP_SELF']).'/';
11892b83b77Sandi  }
119ed7b5f09Sandi
120ed7b5f09Sandi  $dir = str_replace('\\','/',$dir); #bugfix for weird WIN behaviour
121ed7b5f09Sandi  $dir = preg_replace('#//+#','/',$dir);
122ed7b5f09Sandi
123ed7b5f09Sandi  //finish here for relative URLs
124ed7b5f09Sandi  if(!$abs) return $dir;
125ed7b5f09Sandi
126ed7b5f09Sandi  $port = ':'.$_SERVER['SERVER_PORT'];
127ed7b5f09Sandi  //remove port from hostheader as sent by IE
128ed7b5f09Sandi  $host = preg_replace('/:.*$/','',$_SERVER['HTTP_HOST']);
129ed7b5f09Sandi
130ed7b5f09Sandi  // see if HTTPS is enabled - apache leaves this empty when not available,
131ed7b5f09Sandi  // IIS sets it to 'off', 'false' and 'disabled' are just guessing
132ed7b5f09Sandi  if (preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){
133ed7b5f09Sandi    $proto = 'http://';
134ed7b5f09Sandi    if ($_SERVER['SERVER_PORT'] == '80') {
135ed7b5f09Sandi      $port='';
136ed7b5f09Sandi    }
137ed7b5f09Sandi  }else{
138ed7b5f09Sandi    $proto = 'https://';
139ed7b5f09Sandi    if ($_SERVER['SERVER_PORT'] == '443') {
140ed7b5f09Sandi      $port='';
141ed7b5f09Sandi    }
142ed7b5f09Sandi  }
143ed7b5f09Sandi
144ed7b5f09Sandi  return $proto.$host.$port.$dir;
145ed7b5f09Sandi}
146ed7b5f09Sandi
147b000c6d4Sandi/**
148b000c6d4Sandi * Append a PHP extension to a given file and adds an exit call
149b000c6d4Sandi *
150b000c6d4Sandi * This is used to migrate some old configfiles. An added PHP extension
151b000c6d4Sandi * ensures the contents are not shown to webusers even if .htaccess files
152b000c6d4Sandi * do not work
153b000c6d4Sandi *
154b000c6d4Sandi * @author Jan Decaluwe <jan@jandecaluwe.com>
155b000c6d4Sandi */
1568c4f28e8Sjanfunction scriptify($file) {
1578c4f28e8Sjan  // checks
1588c4f28e8Sjan  if (!is_readable($file)) {
1598c4f28e8Sjan    return;
1608c4f28e8Sjan  }
1618c4f28e8Sjan  $fn = $file.'.php';
1628c4f28e8Sjan  if (@file_exists($fn)) {
1638c4f28e8Sjan    return;
1648c4f28e8Sjan  }
1658c4f28e8Sjan  $fh = fopen($fn, 'w');
1668c4f28e8Sjan  if (!$fh) {
1678c4f28e8Sjan    die($fn.' is not writable!');
1688c4f28e8Sjan  }
1698c4f28e8Sjan  // write php exit hack first
1708c4f28e8Sjan  fwrite($fh, "# $fn\n");
1718c4f28e8Sjan  fwrite($fh, '# <?php exit()?>'."\n");
1728c4f28e8Sjan  fwrite($fh, "# Don't modify the lines above\n");
1738c4f28e8Sjan  fwrite($fh, "#\n");
1748c4f28e8Sjan  // copy existing lines
1758c4f28e8Sjan  $lines = file($file);
1768c4f28e8Sjan  foreach ($lines as $line){
1778c4f28e8Sjan    fwrite($fh, $line);
1788c4f28e8Sjan  }
1793ba793e2Sandi  fclose($fh);
180b000c6d4Sandi  //try to rename the old file
181b000c6d4Sandi  @rename($file,"$file.old");
1828c4f28e8Sjan}
1838c4f28e8Sjan
184ed7b5f09Sandi
185340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
186