1<?php 2/** 3 * Information and debugging functions 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Andreas Gohr <andi@splitbrain.org> 7 */ 8if(!defined('DOKU_INC')) die('meh.'); 9if(!defined('DOKU_MESSAGEURL')) define('DOKU_MESSAGEURL','http://update.dokuwiki.org/check/'); 10require_once(DOKU_INC.'inc/HTTPClient.php'); 11 12/** 13 * Check for new messages from upstream 14 * 15 * @author Andreas Gohr <andi@splitbrain.org> 16 */ 17function checkUpdateMessages(){ 18 global $conf; 19 global $INFO; 20 if(!$conf['updatecheck']) return; 21 if($conf['useacl'] && !$INFO['ismanager']) return; 22 23 $cf = $conf['cachedir'].'/messages.txt'; 24 $lm = @filemtime($cf); 25 26 // check if new messages needs to be fetched 27 if($lm < time()-(60*60*24) || $lm < @filemtime(DOKU_CONF.'msg')){ 28 $num = @file(DOKU_CONF.'msg'); 29 $num = is_array($num) ? (int) $num[0] : 0; 30 $http = new DokuHTTPClient(); 31 $http->timeout = 8; 32 $data = $http->get(DOKU_MESSAGEURL.$num); 33 io_saveFile($cf,$data); 34 }else{ 35 $data = io_readFile($cf); 36 } 37 38 // show messages through the usual message mechanism 39 $msgs = explode("\n%\n",$data); 40 foreach($msgs as $msg){ 41 if($msg) msg($msg,2); 42 } 43} 44 45 46/** 47 * Return DokuWikis version 48 * 49 * @author Andreas Gohr <andi@splitbrain.org> 50 */ 51function getVersion(){ 52 //import version string 53 if(@file_exists(DOKU_INC.'VERSION')){ 54 //official release 55 return 'Release '.trim(io_readfile(DOKU_INC.'VERSION')); 56 }elseif(is_dir(DOKU_INC.'_darcs')){ 57 //darcs checkout - read last 2000 bytes of inventory 58 $sz = filesize(DOKU_INC.'_darcs/inventory'); 59 $seek = max(0,$sz-2000); 60 $fh = fopen(DOKU_INC.'_darcs/inventory','rb'); 61 fseek($fh,$seek); 62 $chunk = fread($fh,2000); 63 fclose($fh); 64 $inv = preg_grep('#\*\*\d{14}[\]$]#',explode("\n",$chunk)); 65 $cur = array_pop($inv); 66 preg_match('#\*\*(\d{4})(\d{2})(\d{2})#',$cur,$matches); 67 return 'Darcs '.$matches[1].'-'.$matches[2].'-'.$matches[3]; 68 }else{ 69 return 'snapshot?'; 70 } 71} 72 73/** 74 * Run a few sanity checks 75 * 76 * @author Andreas Gohr <andi@splitbrain.org> 77 */ 78function check(){ 79 global $conf; 80 global $INFO; 81 82 msg('DokuWiki version: '.getVersion(),1); 83 84 if(version_compare(phpversion(),'4.3.3','<')){ 85 msg('Your PHP version is too old ('.phpversion().' vs. 4.3.3+ recommended)',-1); 86 }elseif(version_compare(phpversion(),'4.3.10','<')){ 87 msg('Consider upgrading PHP to 4.3.10 or higher for security reasons (your version: '.phpversion().')',0); 88 }else{ 89 msg('PHP version '.phpversion(),1); 90 } 91 92 $mem = (int) php_to_byte(ini_get('memory_limit')); 93 if($mem){ 94 if($mem < 16777216){ 95 msg('PHP is limited to less than 16MB RAM ('.$mem.' bytes). Increase memory_limit in php.ini',-1); 96 }elseif($mem < 20971520){ 97 msg('PHP is limited to less than 20MB RAM ('.$mem.' bytes), you might encounter problems with bigger pages. Increase memory_limit in php.ini',-1); 98 }elseif($mem < 33554432){ 99 msg('PHP is limited to less than 32MB RAM ('.$mem.' bytes), but that should be enough in most cases. If not, increase memory_limit in php.ini',0); 100 }else{ 101 msg('More than 32MB RAM ('.$mem.' bytes) available.',1); 102 } 103 } 104 105 106 if(is_writable($conf['changelog'])){ 107 msg('Changelog is writable',1); 108 }else{ 109 if (@file_exists($conf['changelog'])) { 110 msg('Changelog is not writable',-1); 111 } 112 } 113 114 if (isset($conf['changelog_old']) && @file_exists($conf['changelog_old'])) { 115 msg('Old changelog exists', 0); 116 } 117 118 if (@file_exists($conf['changelog'].'_failed')) { 119 msg('Importing old changelog failed', -1); 120 } else if (@file_exists($conf['changelog'].'_importing')) { 121 msg('Importing old changelog now.', 0); 122 } else if (@file_exists($conf['changelog'].'_import_ok')) { 123 msg('Old changelog imported', 1); 124 if (!plugin_isdisabled('importoldchangelog')) { 125 msg('Importoldchangelog plugin not disabled after import', -1); 126 } 127 } 128 129 if(is_writable($conf['datadir'])){ 130 msg('Datadir is writable',1); 131 }else{ 132 msg('Datadir is not writable',-1); 133 } 134 135 if(is_writable($conf['olddir'])){ 136 msg('Attic is writable',1); 137 }else{ 138 msg('Attic is not writable',-1); 139 } 140 141 if(is_writable($conf['mediadir'])){ 142 msg('Mediadir is writable',1); 143 }else{ 144 msg('Mediadir is not writable',-1); 145 } 146 147 if(is_writable($conf['cachedir'])){ 148 msg('Cachedir is writable',1); 149 }else{ 150 msg('Cachedir is not writable',-1); 151 } 152 153 if(is_writable($conf['lockdir'])){ 154 msg('Lockdir is writable',1); 155 }else{ 156 msg('Lockdir is not writable',-1); 157 } 158 159 if($conf['authtype'] == 'plain'){ 160 if(is_writable(DOKU_CONF.'users.auth.php')){ 161 msg('conf/users.auth.php is writable',1); 162 }else{ 163 msg('conf/users.auth.php is not writable',0); 164 } 165 } 166 167 if(function_exists('mb_strpos')){ 168 if(defined('UTF8_NOMBSTRING')){ 169 msg('mb_string extension is available but will not be used',0); 170 }else{ 171 msg('mb_string extension is available and will be used',1); 172 if(ini_get('mbstring.func_overload') != 0){ 173 msg('mb_string function overloading is enabled, this will cause problems and should be disabled',-1); 174 } 175 } 176 }else{ 177 msg('mb_string extension not available - PHP only replacements will be used',0); 178 } 179 180 if($conf['allowdebug']){ 181 msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1); 182 }else{ 183 msg('Debugging support is disabled',1); 184 } 185 186 if($INFO['userinfo']['name']){ 187 msg('You are currently logged in as '.$_SERVER['REMOTE_USER'].' ('.$INFO['userinfo']['name'].')',0); 188 msg('You are part of the groups '.join($INFO['userinfo']['grps'],', '),0); 189 }else{ 190 msg('You are currently not logged in',0); 191 } 192 193 msg('Your current permission for this page is '.$INFO['perm'],0); 194 195 if(is_writable($INFO['filepath'])){ 196 msg('The current page is writable by the webserver',0); 197 }else{ 198 msg('The current page is not writable by the webserver',0); 199 } 200 201 if($INFO['writable']){ 202 msg('The current page is writable by you',0); 203 }else{ 204 msg('The current page is not writable by you',0); 205 } 206 207 require_once(DOKU_INC.'inc/HTTPClient.php'); 208 $check = wl('','',true).'data/_dummy'; 209 $http = new DokuHTTPClient(); 210 $http->timeout = 6; 211 $res = $http->get($check); 212 if(strpos($res,'data directory') !== false){ 213 msg('It seems like the data directory is accessible from the web. 214 Make sure this directory is properly protected 215 (See <a href="http://www.dokuwiki.org/security">security</a>)',-1); 216 }elseif($http->status == 404 || $http->status == 403){ 217 msg('The data directory seems to be properly protected',1); 218 }else{ 219 msg('Failed to check if the data directory is accessible from the web. 220 Make sure this directory is properly protected 221 (See <a href="http://www.dokuwiki.org/security">security</a>)',-1); 222 } 223} 224 225/** 226 * print a message 227 * 228 * If HTTP headers were not sent yet the message is added 229 * to the global message array else it's printed directly 230 * using html_msgarea() 231 * 232 * 233 * Levels can be: 234 * 235 * -1 error 236 * 0 info 237 * 1 success 238 * 239 * @author Andreas Gohr <andi@splitbrain.org> 240 * @see html_msgarea 241 */ 242function msg($message,$lvl=0,$line='',$file=''){ 243 global $MSG; 244 $errors[-1] = 'error'; 245 $errors[0] = 'info'; 246 $errors[1] = 'success'; 247 $errors[2] = 'notify'; 248 249 if($line || $file) $message.=' ['.basename($file).':'.$line.']'; 250 251 if(!headers_sent()){ 252 if(!isset($MSG)) $MSG = array(); 253 $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message); 254 }else{ 255 $MSG = array(); 256 $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message); 257 if(function_exists('html_msgarea')){ 258 html_msgarea(); 259 }else{ 260 print "ERROR($lvl) $message"; 261 } 262 } 263} 264 265/** 266 * print debug messages 267 * 268 * little function to print the content of a var 269 * 270 * @author Andreas Gohr <andi@splitbrain.org> 271 */ 272function dbg($msg,$hidden=false){ 273 (!$hidden) ? print '<pre class="dbg">' : print "<!--\n"; 274 print_r($msg); 275 (!$hidden) ? print '</pre>' : print "\n-->"; 276} 277 278/** 279 * Print info to a log file 280 * 281 * @author Andreas Gohr <andi@splitbrain.org> 282 */ 283function dbglog($msg,$header=''){ 284 global $conf; 285 if(is_object($msg) || is_array($msg)){ 286 $msg = print_r($msg,true); 287 } 288 289 if($header) $msg = "$header\n$msg"; 290 291 $file = $conf['cachedir'].'/debug.log'; 292 $fh = fopen($file,'a'); 293 if($fh){ 294 fwrite($fh,date('H:i:s ').$_SERVER['REMOTE_ADDR'].': '.$msg."\n"); 295 fclose($fh); 296 } 297} 298 299/** 300 * Print a reversed, prettyprinted backtrace 301 * 302 * @author Gary Owen <gary_owen@bigfoot.com> 303 */ 304function dbg_backtrace(){ 305 // Get backtrace 306 $backtrace = debug_backtrace(); 307 308 // Unset call to debug_print_backtrace 309 array_shift($backtrace); 310 311 // Iterate backtrace 312 $calls = array(); 313 $depth = count($backtrace) - 1; 314 foreach ($backtrace as $i => $call) { 315 $location = $call['file'] . ':' . $call['line']; 316 $function = (isset($call['class'])) ? 317 $call['class'] . $call['type'] . $call['function'] : $call['function']; 318 319 $params = array(); 320 if (isset($call['args'])){ 321 foreach($call['args'] as $arg){ 322 if(is_object($arg)){ 323 $params[] = '[Object '.get_class($arg).']'; 324 }elseif(is_array($arg)){ 325 $params[] = '[Array]'; 326 }elseif(is_null($arg)){ 327 $param[] = '[NULL]'; 328 }else{ 329 $params[] = (string) '"'.$arg.'"'; 330 } 331 } 332 } 333 $params = implode(', ',$params); 334 335 $calls[$depth - $i] = sprintf('%s(%s) called at %s', 336 $function, 337 str_replace("\n", '\n', $params), 338 $location); 339 } 340 ksort($calls); 341 342 return implode("\n", $calls); 343} 344 345/** 346 * Remove all data from an array where the key seems to point to sensitive data 347 * 348 * This is used to remove passwords, mail addresses and similar data from the 349 * debug output 350 * 351 * @author Andreas Gohr <andi@splitbrain.org> 352 */ 353function debug_guard(&$data){ 354 foreach($data as $key => $value){ 355 if(preg_match('/(notify|pass|auth|secret|ftp|userinfo|token|buid|mail|proxy)/i',$key)){ 356 $data[$key] = '***'; 357 continue; 358 } 359 if(is_array($value)) debug_guard($data[$key]); 360 } 361} 362