3 * Initialize some defaults needed for DokuWiki
6 // start timing Dokuwiki execution
7 function delta_time($start=0) {
8 list($usec, $sec) = explode(" ", microtime());
9 return ((float)$usec+
(float)$sec)-((float)$start);
11 define('DOKU_START_TIME', delta_time());
13 global $config_cascade;
16 // if available load a preload config file
17 $preload = fullpath(dirname(__FILE__
)).'/preload.php';
18 if (@file_exists
($preload)) include($preload);
20 // define the include path
21 if(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__
).'/../').'/');
23 // define config path (packagers may want to change this to /etc/dokuwiki/)
24 if(!defined('DOKU_CONF')) define('DOKU_CONF',DOKU_INC
.'conf/');
26 // check for error reporting override or set error reporting to sane values
27 if (!defined('DOKU_E_LEVEL') && @file_exists
(DOKU_CONF
.'report_e_all')) {
28 define('DOKU_E_LEVEL', E_ALL
);
30 if (!defined('DOKU_E_LEVEL')) {
31 if(defined('E_DEPRECATED')){ // since php 5.3
32 error_reporting(E_ALL
& ~E_NOTICE
& ~E_DEPRECATED
);
34 error_reporting(E_ALL ^ E_NOTICE
);
37 error_reporting(DOKU_E_LEVEL
);
41 global $cache_revinfo;
42 $cache_revinfo = array();
44 $cache_wikifn = array();
45 global $cache_cleanid;
46 $cache_cleanid = array();
47 global $cache_authname;
48 $cache_authname = array();
49 global $cache_metadata;
50 $cache_metadata = array();
52 //set the configuration cascade - but only if its not already been set in preload.php
53 if (empty($config_cascade)) {
54 $config_cascade = array(
56 'default' => array(DOKU_CONF
.'dokuwiki.php'),
57 'local' => array(DOKU_CONF
.'local.php'),
58 'protected' => array(DOKU_CONF
.'local.protected.php'),
61 'default' => array(DOKU_CONF
.'acronyms.conf'),
62 'local' => array(DOKU_CONF
.'acronyms.local.conf'),
65 'default' => array(DOKU_CONF
.'entities.conf'),
66 'local' => array(DOKU_CONF
.'entities.local.conf'),
69 'default' => array(DOKU_CONF
.'interwiki.conf'),
70 'local' => array(DOKU_CONF
.'interwiki.local.conf'),
73 'default' => array(DOKU_CONF
.'license.php'),
74 'local' => array(DOKU_CONF
.'license.local.php'),
77 'default' => array(DOKU_CONF
.'mediameta.php'),
78 'local' => array(DOKU_CONF
.'mediameta.local.php'),
81 'default' => array(DOKU_CONF
.'mime.conf'),
82 'local' => array(DOKU_CONF
.'mime.local.conf'),
85 'default' => array(DOKU_CONF
.'scheme.conf'),
86 'local' => array(DOKU_CONF
.'scheme.local.conf'),
89 'default' => array(DOKU_CONF
.'smileys.conf'),
90 'local' => array(DOKU_CONF
.'smileys.local.conf'),
93 'default' => array(DOKU_CONF
.'wordblock.conf'),
94 'local' => array(DOKU_CONF
.'wordblock.local.conf'),
99 //prepare config array()
103 // load the global config file(s)
104 foreach (array('default','local','protected') as $config_group) {
105 if (empty($config_cascade['main'][$config_group])) continue;
106 foreach ($config_cascade['main'][$config_group] as $config_file) {
107 if (@file_exists
($config_file)) {
108 include($config_file);
113 //prepare language array
117 //load the language files
118 require_once(DOKU_INC
.'inc/lang/en/lang.php');
119 if ( $conf['lang'] && $conf['lang'] != 'en' ) {
120 require_once(DOKU_INC
.'inc/lang/'.$conf['lang'].'/lang.php');
123 //prepare license array()
127 // load the license file(s)
128 foreach (array('default','local') as $config_group) {
129 if (empty($config_cascade['license'][$config_group])) continue;
130 foreach ($config_cascade['license'][$config_group] as $config_file) {
131 if(@file_exists
($config_file)){
132 include($config_file);
137 // set timezone (as in pre 5.3.0 days)
138 date_default_timezone_set(@date_default_timezone_get
());
141 if(!defined('DOKU_REL')) define('DOKU_REL',getBaseURL(false));
142 if(!defined('DOKU_URL')) define('DOKU_URL',getBaseURL(true));
143 if(!defined('DOKU_BASE')){
144 if($conf['canonical']){
145 define('DOKU_BASE',DOKU_URL
);
147 define('DOKU_BASE',DOKU_REL
);
152 if(!defined('DOKU_LF')) define ('DOKU_LF',"\n");
153 if(!defined('DOKU_TAB')) define ('DOKU_TAB',"\t");
155 // define cookie and session id, append server port when securecookie is configured FS#1664
156 if (!defined('DOKU_COOKIE')) define('DOKU_COOKIE', 'DW'.md5(DOKU_REL
.(($conf['securecookie'])?
$_SERVER['SERVER_PORT']:'')));
159 if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC
.'lib/plugins/');
161 // define main script
162 if(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php');
164 // define Template baseURL
165 if(!defined('DOKU_TPL')) define('DOKU_TPL',
166 DOKU_BASE
.'lib/tpl/'.$conf['template'].'/');
168 // define real Template directory
169 if(!defined('DOKU_TPLINC')) define('DOKU_TPLINC',
170 DOKU_INC
.'lib/tpl/'.$conf['template'].'/');
172 // make session rewrites XHTML compliant
173 @ini_set
('arg_separator.output', '&');
175 // make sure global zlib does not interfere FS#1132
176 @ini_set
('zlib.output_compression', 'off');
178 // increase PCRE backtrack limit
179 @ini_set
('pcre.backtrack_limit', '20971520');
181 // enable gzip compression if supported
182 $conf['gzip_output'] &= (strpos($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip') !== false);
183 if ($conf['gzip_output'] &&
184 !defined('DOKU_DISABLE_GZIP_OUTPUT') &&
185 function_exists('ob_gzhandler')) {
186 ob_start('ob_gzhandler');
190 if (!headers_sent() && !defined('NOSESSION')){
191 session_name("DokuWiki");
192 if (version_compare(PHP_VERSION
, '5.2.0', '>')) {
193 session_set_cookie_params(0,DOKU_REL
,'',($conf['securecookie'] && is_ssl()),true);
195 session_set_cookie_params(0,DOKU_REL
,'',($conf['securecookie'] && is_ssl()));
199 // load left over messages
200 if(isset($_SESSION[DOKU_COOKIE
]['msg'])){
201 $MSG = $_SESSION[DOKU_COOKIE
]['msg'];
202 unset($_SESSION[DOKU_COOKIE
]['msg']);
207 if (get_magic_quotes_gpc() && !defined('MAGIC_QUOTES_STRIPPED')) {
208 if (!empty($_GET)) remove_magic_quotes($_GET);
209 if (!empty($_POST)) remove_magic_quotes($_POST);
210 if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE);
211 if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST);
212 @ini_set
('magic_quotes_gpc', 0);
213 define('MAGIC_QUOTES_STRIPPED',1);
215 @set_magic_quotes_runtime
(0);
216 @ini_set
('magic_quotes_sybase',0);
218 // don't let cookies ever interfere with request vars
219 $_REQUEST = array_merge($_GET,$_POST);
221 // we don't want a purge URL to be digged
222 if(isset($_REQUEST['purge']) && $_SERVER['HTTP_REFERER']) unset($_REQUEST['purge']);
224 // disable gzip if not available
225 if($conf['compression'] == 'bz2' && !function_exists('bzopen')){
226 $conf['compression'] = 'gz';
228 if($conf['compression'] == 'gz' && !function_exists('gzopen')){
229 $conf['compression'] = 0;
232 // fix dateformat for upgraders
233 if(strpos($conf['dformat'],'%') === false){
234 $conf['dformat'] = '%Y/%m/%d %H:%M';
237 // precalculate file creation modes
238 init_creationmodes();
240 // make real paths and check them
244 // automatic upgrade to script versions of certain files
245 scriptify(DOKU_CONF
.'users.auth');
246 scriptify(DOKU_CONF
.'acl.auth');
250 * Checks paths from config file
252 function init_paths(){
255 $paths = array('datadir' => 'pages',
257 'mediadir' => 'media',
259 'cachedir' => 'cache',
260 'indexdir' => 'index',
261 'lockdir' => 'locks',
264 foreach($paths as $c => $p){
265 if(empty($conf[$c])) $conf[$c] = $conf['savedir'].'/'.$p;
266 $conf[$c] = init_path($conf[$c]);
267 if(empty($conf[$c])) nice_die("The $c ('$p') does not exist, isn't accessible or writable.
268 You should check your config and permission settings.
269 Or maybe you want to <a href=\"install.php\">run the
273 // path to old changelog only needed for upgrading
274 $conf['changelog_old'] = init_path((isset($conf['changelog']))?
($conf['changelog']):($conf['savedir'].'/changes.log'));
275 if ($conf['changelog_old']=='') { unset($conf['changelog_old']); }
276 // hardcoded changelog because it is now a cache that lives in meta
277 $conf['changelog'] = $conf['metadir'].'/_dokuwiki.changes';
278 $conf['media_changelog'] = $conf['metadir'].'/_media.changes';
282 * Checks the existance of certain files and creates them if missing.
284 function init_files(){
287 $files = array( $conf['indexdir'].'/page.idx');
289 foreach($files as $file){
290 if(!@file_exists
($file)){
291 $fh = @fopen
($file,'a');
294 if($conf['fperm']) chmod($file, $conf['fperm']);
296 nice_die("$file is not writable. Check your permissions settings!");
303 * Returns absolute path
305 * This tries the given path first, then checks in DOKU_INC.
306 * Check for accessability on directories as well.
308 * @author Andreas Gohr <andi@splitbrain.org>
310 function init_path($path){
312 $p = fullpath($path);
313 if(!@file_exists
($p)){
314 $p = fullpath(DOKU_INC
.$path);
315 if(!@file_exists
($p)){
321 if(!@is_writable
($p)){
325 // check accessability (execute bit) for directories
326 if(@is_dir
($p) && !@file_exists
("$p/.")){
334 * Sets the internal config values fperm and dperm which, when set,
335 * will be used to change the permission of a newly created dir or
336 * file with chmod. Considers the influence of the system's umask
337 * setting the values only if needed.
339 function init_creationmodes(){
342 // Legacy support for old umask/dmask scheme
343 unset($conf['dmask']);
344 unset($conf['fmask']);
345 unset($conf['umask']);
346 unset($conf['fperm']);
347 unset($conf['dperm']);
349 // get system umask, fallback to 0 if none available
351 if(!$umask) $umask = 0000;
353 // check what is set automatically by the system on file creation
354 // and set the fperm param if it's not what we want
355 $auto_fmode = 0666 & ~
$umask;
356 if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode'];
358 // check what is set automatically by the system on file creation
359 // and set the dperm param if it's not what we want
360 $auto_dmode = $conf['dmode'] & ~
$umask;
361 if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode'];
365 * remove magic quotes recursivly
367 * @author Andreas Gohr <andi@splitbrain.org>
369 function remove_magic_quotes(&$array) {
370 foreach (array_keys($array) as $key) {
371 // handle magic quotes in keynames (breaks order)
372 $sk = stripslashes($key);
374 $array[$sk] = $array[$key];
379 // do recursion if needed
380 if (is_array($array[$key])) {
381 remove_magic_quotes($array[$key]);
383 $array[$key] = stripslashes($array[$key]);
389 * Returns the full absolute URL to the directory where
390 * DokuWiki is installed in (includes a trailing slash)
392 * @author Andreas Gohr <andi@splitbrain.org>
394 function getBaseURL($abs=null){
396 //if canonical url enabled always return absolute
397 if(is_null($abs)) $abs = $conf['canonical'];
399 if($conf['basedir']){
400 $dir = $conf['basedir'];
401 }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){
402 $dir = dirname($_SERVER['SCRIPT_NAME']);
403 }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){
404 $dir = dirname($_SERVER['PHP_SELF']);
405 }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
406 $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
407 $_SERVER['SCRIPT_FILENAME']);
408 $dir = dirname('/'.$dir);
410 $dir = '.'; //probably wrong
413 $dir = str_replace('\\','/',$dir); // bugfix for weird WIN behaviour
414 $dir = preg_replace('#//+#','/',"/$dir/"); // ensure leading and trailing slashes
416 //handle script in lib/exe dir
417 $dir = preg_replace('!lib/exe/$!','',$dir);
419 //handle script in lib/plugins dir
420 $dir = preg_replace('!lib/plugins/.*$!','',$dir);
422 //finish here for relative URLs
423 if(!$abs) return $dir;
425 //use config option if available, trim any slash from end of baseurl to avoid multiple consecutive slashes in the path
426 if($conf['baseurl']) return rtrim($conf['baseurl'],'/').$dir;
428 //split hostheader into host and port
429 $addr = explode(':',$_SERVER['HTTP_HOST']);
432 if (isset($addr[1])) {
434 } elseif (isset($_SERVER['SERVER_PORT'])) {
435 $port = $_SERVER['SERVER_PORT'];
444 if ($port == '443') {
449 if($port !== '') $port = ':'.$port;
451 return $proto.$host.$port.$dir;
455 * Check if accessed via HTTPS
457 * Apache leaves ,$_SERVER['HTTPS'] empty when not available, IIS sets it to 'off'.
458 * 'false' and 'disabled' are just guessing
460 * @returns bool true when SSL is active
463 if (!isset($_SERVER['HTTPS']) ||
464 preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){
472 * Append a PHP extension to a given file and adds an exit call
474 * This is used to migrate some old configfiles. An added PHP extension
475 * ensures the contents are not shown to webusers even if .htaccess files
478 * @author Jan Decaluwe <jan@jandecaluwe.com>
480 function scriptify($file) {
482 if (!is_readable($file)) {
486 if (@file_exists
($fn)) {
489 $fh = fopen($fn, 'w');
491 nice_die($fn.' is not writable. Check your permission settings!');
493 // write php exit hack first
494 fwrite($fh, "# $fn\n");
495 fwrite($fh, '# <?php exit()?>'."\n");
496 fwrite($fh, "# Don't modify the lines above\n");
498 // copy existing lines
499 $lines = file($file);
500 foreach ($lines as $line){
504 //try to rename the old file
505 io_rename($file,"$file.old");
509 * print a nice message even if no styles are loaded yet.
511 function nice_die($msg){
513 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
514 "http://www.w3.org/TR/html4/loose.dtd">
516 <head><title>DokuWiki Setup Error</title></head>
517 <body style="font-family: Arial, sans-serif">
518 <div style="width:60%; margin: auto; background-color: #fcc;
519 border: 1px solid #faa; padding: 0.5em 1em;">
520 <h1 style="font-size: 120%">DokuWiki Setup Error</h1>
531 * A realpath() replacement
533 * This function behaves similar to PHP's realpath() but does not resolve
534 * symlinks or accesses upper directories
536 * @author Andreas Gohr <andi@splitbrain.org>
537 * @author <richpageau at yahoo dot co dot uk>
538 * @link http://de3.php.net/manual/en/function.realpath.php#75992
540 function fullpath($path,$exists=false){
543 $iswin = (strtoupper(substr(PHP_OS
, 0, 3)) === 'WIN' ||
@$GLOBALS['DOKU_UNITTEST_ASSUME_WINDOWS']);
545 // find the (indestructable) root of the path - keeps windows stuff intact
549 // match drive letter and UNC paths
550 if(preg_match('!^([a-zA-z]:)(.*)!',$path,$match)){
551 $root = $match[1].'/';
553 }else if(preg_match('!^(\\\\\\\\[^\\\\/]+\\\\[^\\\\/]+[\\\\/])(.*)!',$path,$match)){
558 $path = str_replace('\\','/',$path);
560 // if the given path wasn't absolute already, prepend the script path and retry
562 $base = dirname($_SERVER['SCRIPT_FILENAME']);
563 $path = $base.'/'.$path;
564 if($run == 0){ // avoid endless recursion when base isn't absolute for some reason
566 return fullpath($path,$exists);
572 $path=explode('/', $path);
574 foreach($path as $p) {
575 if ($p === '' ||
$p === '.') continue;
580 array_push($newpath, $p);
582 $finalpath = $root.implode('/', $newpath);
584 // check for existance when needed (except when unit testing)
585 if($exists && !defined('DOKU_UNITTEST') && !@file_exists
($finalpath)) {