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 // define config path (packagers may want to change this to /etc/dokuwiki/) 10 if(!defined('DOKU_CONF')) define('DOKU_CONF',DOKU_INC.'conf/'); 11 12 // set up error reporting to sane values 13 error_reporting(E_ALL ^ E_NOTICE); 14 15 //prepare config array() 16 global $conf; 17 $conf = array(); 18 19 // load the config file(s) 20 require_once(DOKU_CONF.'dokuwiki.php'); 21 @include_once(DOKU_CONF.'local.php'); 22 23 //prepare language array 24 global $lang; 25 $lang = array(); 26 27 //load the language files 28 require_once(DOKU_INC.'inc/lang/en/lang.php'); 29 require_once(DOKU_INC.'inc/lang/'.$conf['lang'].'/lang.php'); 30 31 // define baseURL 32 if(!defined('DOKU_BASE')) define('DOKU_BASE',getBaseURL()); 33 if(!defined('DOKU_URL')) define('DOKU_URL',getBaseURL(true)); 34 35 // define Plugin dir 36 if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 37 38 // define main script 39 if(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php'); 40 41 // define Template baseURL 42 if(!defined('DOKU_TPL')) define('DOKU_TPL', 43 DOKU_BASE.'lib/tpl/'.$conf['template'].'/'); 44 45 // make session rewrites XHTML compliant 46 @ini_set('arg_separator.output', '&'); 47 48 // init session 49 session_name("DokuWiki"); 50 if (!headers_sent()) session_start(); 51 52 // kill magic quotes 53 if (get_magic_quotes_gpc()) { 54 if (!empty($_GET)) remove_magic_quotes($_GET); 55 if (!empty($_POST)) remove_magic_quotes($_POST); 56 if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE); 57 if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST); 58 if (!empty($_SESSION)) remove_magic_quotes($_SESSION); 59 @ini_set('magic_quotes_gpc', 0); 60 } 61 @set_magic_quotes_runtime(0); 62 @ini_set('magic_quotes_sybase',0); 63 64 // disable gzip if not available 65 if($conf['usegzip'] && !function_exists('gzopen')){ 66 $conf['usegzip'] = 0; 67 } 68 69 // remember original umask 70 $conf['oldumask'] = umask(); 71 72 // make real paths and check them 73 init_paths(); 74 init_files(); 75 76 // automatic upgrade to script versions of certain files 77 scriptify(DOKU_CONF.'users.auth'); 78 scriptify(DOKU_CONF.'acl.auth'); 79 80 81/** 82 * Checks paths from config file 83 */ 84function init_paths(){ 85 global $conf; 86 87 $paths = array('datadir' => 'pages', 88 'olddir' => 'attic', 89 'mediadir' => 'media', 90 'metadir' => 'meta', 91 'cachedir' => 'cache', 92 'lockdir' => 'locks', 93 'changelog' => 'changes.log'); 94 95 foreach($paths as $c => $p){ 96 if(!$conf[$c]) $conf[$c] = $conf['savedir'].'/'.$p; 97 $conf[$c] = init_path($conf[$c]); 98 if(!$conf[$c]) die("$c does not exist or isn't writable. Check config!"); 99 } 100} 101 102/** 103 * Checks the existance of certain files and creates them if missing 104 */ 105function init_files(){ 106 global $conf; 107 $files = array( $conf['cachedir'].'/word.idx', 108 $conf['cachedir'].'/page.idx', 109 $conf['cachedir'].'/index.idx', ); 110 111 foreach($files as $file){ 112 if(!@file_exists($file)){ 113 $fh = fopen($file,'a'); 114 fclose($fh); 115 } 116 } 117} 118 119/** 120 * returns absolute path 121 * 122 * This tries the given path first, then checks in DOKU_INC 123 */ 124function init_path($path){ 125 $p = realpath($path); 126 if(@file_exists($p)) return $p; 127 $p = realpath(DOKU_INC.$path); 128 if(@file_exists($p)) return $p; 129 return ''; 130} 131 132/** 133 * remove magic quotes recursivly 134 * 135 * @author Andreas Gohr <andi@splitbrain.org> 136 */ 137function remove_magic_quotes(&$array) { 138 foreach (array_keys($array) as $key) { 139 if (is_array($array[$key])) { 140 remove_magic_quotes($array[$key]); 141 }else { 142 $array[$key] = stripslashes($array[$key]); 143 } 144 } 145} 146 147/** 148 * Returns the full absolute URL to the directory where 149 * DokuWiki is installed in (includes a trailing slash) 150 * 151 * @author Andreas Gohr <andi@splitbrain.org> 152 */ 153function getBaseURL($abs=false){ 154 global $conf; 155 //if canonical url enabled always return absolute 156 if($conf['canonical']) $abs = true; 157 158 if($conf['basedir']){ 159 $dir = $conf['basedir'].'/'; 160 }elseif($_SERVER['SCRIPT_NAME']){ 161 $dir = dirname($_SERVER['SCRIPT_NAME']).'/'; 162 }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){ 163 $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','', 164 $_SERVER['SCRIPT_FILENAME']); 165 $dir = dirname('/'.$dir).'/'; 166 }else{ 167 $dir = dirname($_SERVER['PHP_SELF']).'/'; 168 } 169 170 $dir = str_replace('\\','/',$dir); #bugfix for weird WIN behaviour 171 $dir = preg_replace('#//+#','/',$dir); 172 173 //handle script in lib/exe dir 174 $dir = preg_replace('!lib/exe/$!','',$dir); 175 176 //finish here for relative URLs 177 if(!$abs) return $dir; 178 179 $port = ':'.$_SERVER['SERVER_PORT']; 180 //remove port from hostheader as sent by IE 181 $host = preg_replace('/:.*$/','',$_SERVER['HTTP_HOST']); 182 183 // see if HTTPS is enabled - apache leaves this empty when not available, 184 // IIS sets it to 'off', 'false' and 'disabled' are just guessing 185 if (preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){ 186 $proto = 'http://'; 187 if ($_SERVER['SERVER_PORT'] == '80') { 188 $port=''; 189 } 190 }else{ 191 $proto = 'https://'; 192 if ($_SERVER['SERVER_PORT'] == '443') { 193 $port=''; 194 } 195 } 196 197 return $proto.$host.$port.$dir; 198} 199 200/** 201 * Append a PHP extension to a given file and adds an exit call 202 * 203 * This is used to migrate some old configfiles. An added PHP extension 204 * ensures the contents are not shown to webusers even if .htaccess files 205 * do not work 206 * 207 * @author Jan Decaluwe <jan@jandecaluwe.com> 208 */ 209function scriptify($file) { 210 // checks 211 if (!is_readable($file)) { 212 return; 213 } 214 $fn = $file.'.php'; 215 if (@file_exists($fn)) { 216 return; 217 } 218 $fh = fopen($fn, 'w'); 219 if (!$fh) { 220 die($fn.' is not writable!'); 221 } 222 // write php exit hack first 223 fwrite($fh, "# $fn\n"); 224 fwrite($fh, '# <?php exit()?>'."\n"); 225 fwrite($fh, "# Don't modify the lines above\n"); 226 fwrite($fh, "#\n"); 227 // copy existing lines 228 $lines = file($file); 229 foreach ($lines as $line){ 230 fwrite($fh, $line); 231 } 232 fclose($fh); 233 //try to rename the old file 234 @rename($file,"$file.old"); 235} 236 237 238//Setup VIM: ex: et ts=2 enc=utf-8 : 239