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 9e7cb32dcSAndreas Gohr // define config path (packagers may want to change this to /etc/dokuwiki/) 10b7551a6dSEsther Brunner if(!defined('DOKU_CONF')) define('DOKU_CONF',DOKU_INC.'conf/'); 11e7cb32dcSAndreas Gohr 12c53ea5f2Sandi // set up error reporting to sane values 13c53ea5f2Sandi error_reporting(E_ALL ^ E_NOTICE); 14c53ea5f2Sandi 154724a577Sandi //prepare config array() 16ee20e7d1Sandi global $conf; 174724a577Sandi $conf = array(); 184724a577Sandi 19ad15db82Sandi // load the config file(s) 20e7cb32dcSAndreas Gohr require_once(DOKU_CONF.'dokuwiki.php'); 210a6ead41SAndreas Gohr if(@file_exists(DOKU_CONF.'local.php')){ 220a6ead41SAndreas Gohr require_once(DOKU_CONF.'local.php'); 230a6ead41SAndreas Gohr } 24ad15db82Sandi 25ad15db82Sandi //prepare language array 26ee20e7d1Sandi global $lang; 27ad15db82Sandi $lang = array(); 28ed7b5f09Sandi 2916521111Sandi //load the language files 30bc3b6aecSandi require_once(DOKU_INC.'inc/lang/en/lang.php'); 31f949a01cSAndreas Gohr if ( $conf['lang'] && $conf['lang'] != 'en' ) { 32bc3b6aecSandi require_once(DOKU_INC.'inc/lang/'.$conf['lang'].'/lang.php'); 33fc1c55b1Shfuecks } 3416521111Sandi 35ed7b5f09Sandi // define baseURL 36ed7b5f09Sandi if(!defined('DOKU_BASE')) define('DOKU_BASE',getBaseURL()); 37ed7b5f09Sandi if(!defined('DOKU_URL')) define('DOKU_URL',getBaseURL(true)); 38ed7b5f09Sandi 39ee20e7d1Sandi // define Plugin dir 40f62ea8a1Sandi if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 41ee20e7d1Sandi 42ed7b5f09Sandi // define main script 43ed7b5f09Sandi if(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php'); 44ed7b5f09Sandi 456b13307fSandi // define Template baseURL 466b13307fSandi if(!defined('DOKU_TPL')) define('DOKU_TPL', 47f62ea8a1Sandi DOKU_BASE.'lib/tpl/'.$conf['template'].'/'); 486b13307fSandi 4978a6aeb1SAndreas Gohr // define real Template directory 5078a6aeb1SAndreas Gohr if(!defined('DOKU_TPLINC')) define('DOKU_TPLINC', 5178a6aeb1SAndreas Gohr DOKU_INC.'lib/tpl/'.$conf['template'].'/'); 5278a6aeb1SAndreas Gohr 53ed7b5f09Sandi // make session rewrites XHTML compliant 543fc74836Sandi @ini_set('arg_separator.output', '&'); 55ed7b5f09Sandi 56ed7b5f09Sandi // init session 576534245aSAndreas Gohr if (!headers_sent() && !defined('NOSESSION')){ 58ed7b5f09Sandi session_name("DokuWiki"); 59bad31ae9SAndreas Gohr session_start(); 60bad31ae9SAndreas Gohr } 61ed7b5f09Sandi 62ed7b5f09Sandi // kill magic quotes 63ed7b5f09Sandi if (get_magic_quotes_gpc()) { 64ed7b5f09Sandi if (!empty($_GET)) remove_magic_quotes($_GET); 65ed7b5f09Sandi if (!empty($_POST)) remove_magic_quotes($_POST); 66ed7b5f09Sandi if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE); 67ed7b5f09Sandi if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST); 68ed7b5f09Sandi if (!empty($_SESSION)) remove_magic_quotes($_SESSION); 693fc74836Sandi @ini_set('magic_quotes_gpc', 0); 70ed7b5f09Sandi } 713fc74836Sandi @set_magic_quotes_runtime(0); 723fc74836Sandi @ini_set('magic_quotes_sybase',0); 73ed7b5f09Sandi 74ed7b5f09Sandi // disable gzip if not available 75ed7b5f09Sandi if($conf['usegzip'] && !function_exists('gzopen')){ 76ed7b5f09Sandi $conf['usegzip'] = 0; 77ed7b5f09Sandi } 78ed7b5f09Sandi 7944881d27STroels Liebe Bentsen // Legacy support for old umask/dmask scheme 8044881d27STroels Liebe Bentsen if(isset($conf['dmask'])) { 8144881d27STroels Liebe Bentsen unset($conf['dmask']); 8244881d27STroels Liebe Bentsen unset($conf['fmask']); 8344881d27STroels Liebe Bentsen unset($conf['umask']); 8444881d27STroels Liebe Bentsen } 8544881d27STroels Liebe Bentsen 8644881d27STroels Liebe Bentsen // Set defaults for fmode, dmode and umask. 8744881d27STroels Liebe Bentsen if(!isset($conf['fmode'])) { 8844881d27STroels Liebe Bentsen $conf['fmode'] = 0666; 8944881d27STroels Liebe Bentsen } 9044881d27STroels Liebe Bentsen if(!isset($conf['dmode'])) { 9144881d27STroels Liebe Bentsen $conf['dmode'] = 0777; 9244881d27STroels Liebe Bentsen } 9344881d27STroels Liebe Bentsen if(!isset($conf['umask'])) { 9444881d27STroels Liebe Bentsen $conf['umask'] = umask(); 9544881d27STroels Liebe Bentsen } 9644881d27STroels Liebe Bentsen 9744881d27STroels Liebe Bentsen // Precalculate the fmask and dmask, so we can set later. 9844881d27STroels Liebe Bentsen if(($conf['umask'] != umask()) or ($conf['fmode'] != 0666)) { 9944881d27STroels Liebe Bentsen $conf['fmask'] = $conf['fmode'] & ~$conf['umask']; 10044881d27STroels Liebe Bentsen } 10144881d27STroels Liebe Bentsen if(($conf['umask'] != umask()) or ($conf['dmode'] != 0666)) { 10244881d27STroels Liebe Bentsen $conf['dmask'] = $conf['dmode'] & ~$conf['umask']; 10344881d27STroels Liebe Bentsen } 104ed7b5f09Sandi 1053dc3a5f1Sandi // make real paths and check them 10698407a7aSandi init_paths(); 1077367b368SAndreas Gohr init_files(); 108ed7b5f09Sandi 1098c4f28e8Sjan // automatic upgrade to script versions of certain files 110e7cb32dcSAndreas Gohr scriptify(DOKU_CONF.'users.auth'); 111e7cb32dcSAndreas Gohr scriptify(DOKU_CONF.'acl.auth'); 112f62ea8a1Sandi 113f62ea8a1Sandi 114f62ea8a1Sandi/** 11598407a7aSandi * Checks paths from config file 11698407a7aSandi */ 11798407a7aSandifunction init_paths(){ 11898407a7aSandi global $conf; 11998407a7aSandi 12098407a7aSandi $paths = array('datadir' => 'pages', 12198407a7aSandi 'olddir' => 'attic', 12298407a7aSandi 'mediadir' => 'media', 12398407a7aSandi 'metadir' => 'meta', 12498407a7aSandi 'cachedir' => 'cache', 12598407a7aSandi 'lockdir' => 'locks', 12698407a7aSandi 'changelog' => 'changes.log'); 12798407a7aSandi 12898407a7aSandi foreach($paths as $c => $p){ 12998407a7aSandi if(!$conf[$c]) $conf[$c] = $conf['savedir'].'/'.$p; 13098407a7aSandi $conf[$c] = init_path($conf[$c]); 131*0d8850c4SAndreas Gohr if(!$conf[$c]) die("$c does not exist, isn't accessable or writable. Check config and permissions!"); 13298407a7aSandi } 13398407a7aSandi} 13498407a7aSandi 13598407a7aSandi/** 136*0d8850c4SAndreas Gohr * Checks the existance of certain files and creates them if missing. 1377367b368SAndreas Gohr */ 1387367b368SAndreas Gohrfunction init_files(){ 1397367b368SAndreas Gohr global $conf; 140*0d8850c4SAndreas Gohr 1417367b368SAndreas Gohr $files = array( $conf['cachedir'].'/word.idx', 1427367b368SAndreas Gohr $conf['cachedir'].'/page.idx', 143*0d8850c4SAndreas Gohr $conf['cachedir'].'/index.idx'); 1447367b368SAndreas Gohr 1457367b368SAndreas Gohr foreach($files as $file){ 1467367b368SAndreas Gohr if(!@file_exists($file)){ 147*0d8850c4SAndreas Gohr $fh = @fopen($file,'a'); 148*0d8850c4SAndreas Gohr if($fh){ 1497367b368SAndreas Gohr fclose($fh); 15044881d27STroels Liebe Bentsen if(isset($conf['fmask'])) { chmod($file, $conf['fmask']); } 151*0d8850c4SAndreas Gohr }else{ 152*0d8850c4SAndreas Gohr die("$file is not writable. Check permissions!"); 153*0d8850c4SAndreas Gohr } 1547367b368SAndreas Gohr } 1557367b368SAndreas Gohr } 1567367b368SAndreas Gohr} 1577367b368SAndreas Gohr 1587367b368SAndreas Gohr/** 159*0d8850c4SAndreas Gohr * Returns absolute path 160f62ea8a1Sandi * 161*0d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC. 162*0d8850c4SAndreas Gohr * Check for accessability on directories as well. 163*0d8850c4SAndreas Gohr * 164*0d8850c4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 165f62ea8a1Sandi */ 166f62ea8a1Sandifunction init_path($path){ 167*0d8850c4SAndreas Gohr // check existance 168f62ea8a1Sandi $p = realpath($path); 169*0d8850c4SAndreas Gohr if(!@file_exists($p)){ 170f62ea8a1Sandi $p = realpath(DOKU_INC.$path); 171*0d8850c4SAndreas Gohr if(!@file_exists($p)){ 1728fc4e739Sandi return ''; 173f62ea8a1Sandi } 174*0d8850c4SAndreas Gohr } 175*0d8850c4SAndreas Gohr 176*0d8850c4SAndreas Gohr // check writability 177*0d8850c4SAndreas Gohr if(!@is_writable($p)){ 178*0d8850c4SAndreas Gohr return ''; 179*0d8850c4SAndreas Gohr } 180*0d8850c4SAndreas Gohr 181*0d8850c4SAndreas Gohr // check accessability (execute bit) for directories 182*0d8850c4SAndreas Gohr if(@is_dir($p) && !@file_exists("$p/.")){ 183*0d8850c4SAndreas Gohr return ''; 184*0d8850c4SAndreas Gohr } 185*0d8850c4SAndreas Gohr 186*0d8850c4SAndreas Gohr return $p; 187*0d8850c4SAndreas Gohr} 1888c4f28e8Sjan 189ed7b5f09Sandi/** 190ed7b5f09Sandi * remove magic quotes recursivly 191ed7b5f09Sandi * 192ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org> 193ed7b5f09Sandi */ 194ed7b5f09Sandifunction remove_magic_quotes(&$array) { 195ed7b5f09Sandi foreach (array_keys($array) as $key) { 196ed7b5f09Sandi if (is_array($array[$key])) { 197ed7b5f09Sandi remove_magic_quotes($array[$key]); 198ed7b5f09Sandi }else { 199ed7b5f09Sandi $array[$key] = stripslashes($array[$key]); 200ed7b5f09Sandi } 201ed7b5f09Sandi } 202ed7b5f09Sandi} 203ed7b5f09Sandi 204ed7b5f09Sandi/** 205ed7b5f09Sandi * Returns the full absolute URL to the directory where 206ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash) 207ed7b5f09Sandi * 208ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org> 209ed7b5f09Sandi */ 210ed7b5f09Sandifunction getBaseURL($abs=false){ 211ed7b5f09Sandi global $conf; 212ed7b5f09Sandi //if canonical url enabled always return absolute 213ed7b5f09Sandi if($conf['canonical']) $abs = true; 214ed7b5f09Sandi 21592b83b77Sandi if($conf['basedir']){ 216919eeb46Sandi $dir = $conf['basedir'].'/'; 21789aa05dbSAndreas Gohr }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){ 218bdc127a4Sandi $dir = dirname($_SERVER['SCRIPT_NAME']).'/'; 21989aa05dbSAndreas Gohr }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){ 22089aa05dbSAndreas Gohr $dir = dirname($_SERVER['PHP_SELF']).'/'; 221093ec9e4Sandi }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){ 222093ec9e4Sandi $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','', 223093ec9e4Sandi $_SERVER['SCRIPT_FILENAME']); 224093ec9e4Sandi $dir = dirname('/'.$dir).'/'; 22592b83b77Sandi }else{ 22689aa05dbSAndreas Gohr $dir = './'; //probably wrong 22792b83b77Sandi } 228ed7b5f09Sandi 229ed7b5f09Sandi $dir = str_replace('\\','/',$dir); #bugfix for weird WIN behaviour 230ed7b5f09Sandi $dir = preg_replace('#//+#','/',$dir); 231ed7b5f09Sandi 232f62ea8a1Sandi //handle script in lib/exe dir 233f62ea8a1Sandi $dir = preg_replace('!lib/exe/$!','',$dir); 234f62ea8a1Sandi 235ed7b5f09Sandi //finish here for relative URLs 236ed7b5f09Sandi if(!$abs) return $dir; 237ed7b5f09Sandi 238ef7b3ecdSAndreas Gohr //use config option if available 239ef7b3ecdSAndreas Gohr if($conf['baseurl']) return $conf['baseurl'].$dir; 240ef7b3ecdSAndreas Gohr 241e82e3526SAndreas Gohr //split hostheader into host and port 242e82e3526SAndreas Gohr list($host,$port) = explode(':',$_SERVER['HTTP_HOST']); 243e82e3526SAndreas Gohr if(!$port) $port = $_SERVER['SERVER_PORT']; 244e82e3526SAndreas Gohr if(!$port) $port = 80; 245ed7b5f09Sandi 246ed7b5f09Sandi // see if HTTPS is enabled - apache leaves this empty when not available, 247ed7b5f09Sandi // IIS sets it to 'off', 'false' and 'disabled' are just guessing 248ed7b5f09Sandi if (preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){ 249ed7b5f09Sandi $proto = 'http://'; 250e82e3526SAndreas Gohr if ($port == '80') { 251ed7b5f09Sandi $port=''; 252ed7b5f09Sandi } 253ed7b5f09Sandi }else{ 254ed7b5f09Sandi $proto = 'https://'; 255e82e3526SAndreas Gohr if ($port == '443') { 256ed7b5f09Sandi $port=''; 257ed7b5f09Sandi } 258ed7b5f09Sandi } 259ed7b5f09Sandi 260e82e3526SAndreas Gohr if($port) $port = ':'.$port; 261e82e3526SAndreas Gohr 262ed7b5f09Sandi return $proto.$host.$port.$dir; 263ed7b5f09Sandi} 264ed7b5f09Sandi 265b000c6d4Sandi/** 266b000c6d4Sandi * Append a PHP extension to a given file and adds an exit call 267b000c6d4Sandi * 268b000c6d4Sandi * This is used to migrate some old configfiles. An added PHP extension 269b000c6d4Sandi * ensures the contents are not shown to webusers even if .htaccess files 270b000c6d4Sandi * do not work 271b000c6d4Sandi * 272b000c6d4Sandi * @author Jan Decaluwe <jan@jandecaluwe.com> 273b000c6d4Sandi */ 2748c4f28e8Sjanfunction scriptify($file) { 2758c4f28e8Sjan // checks 2768c4f28e8Sjan if (!is_readable($file)) { 2778c4f28e8Sjan return; 2788c4f28e8Sjan } 2798c4f28e8Sjan $fn = $file.'.php'; 2808c4f28e8Sjan if (@file_exists($fn)) { 2818c4f28e8Sjan return; 2828c4f28e8Sjan } 2838c4f28e8Sjan $fh = fopen($fn, 'w'); 2848c4f28e8Sjan if (!$fh) { 2858c4f28e8Sjan die($fn.' is not writable!'); 2868c4f28e8Sjan } 2878c4f28e8Sjan // write php exit hack first 2888c4f28e8Sjan fwrite($fh, "# $fn\n"); 2898c4f28e8Sjan fwrite($fh, '# <?php exit()?>'."\n"); 2908c4f28e8Sjan fwrite($fh, "# Don't modify the lines above\n"); 2918c4f28e8Sjan fwrite($fh, "#\n"); 2928c4f28e8Sjan // copy existing lines 2938c4f28e8Sjan $lines = file($file); 2948c4f28e8Sjan foreach ($lines as $line){ 2958c4f28e8Sjan fwrite($fh, $line); 2968c4f28e8Sjan } 2973ba793e2Sandi fclose($fh); 298b000c6d4Sandi //try to rename the old file 299b000c6d4Sandi @rename($file,"$file.old"); 3008c4f28e8Sjan} 3018c4f28e8Sjan 302ed7b5f09Sandi 303340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 : 304