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/'); 10 11/** 12 * Check for new messages from upstream 13 * 14 * @author Andreas Gohr <andi@splitbrain.org> 15 */ 16function checkUpdateMessages(){ 17 global $conf; 18 global $INFO; 19 global $updateVersion; 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_INC.DOKU_SCRIPT)){ 28 @touch($cf); 29 dbglog("checkUpdateMessages(): downloading messages.txt"); 30 $http = new DokuHTTPClient(); 31 $http->timeout = 12; 32 $data = $http->get(DOKU_MESSAGEURL.$updateVersion); 33 if(substr(trim($data), -1) != '%') { 34 // this doesn't look like one of our messages, maybe some WiFi login interferred 35 $data = ''; 36 }else { 37 io_saveFile($cf,$data); 38 } 39 }else{ 40 dbglog("checkUpdateMessages(): messages.txt up to date"); 41 $data = io_readFile($cf); 42 } 43 44 // show messages through the usual message mechanism 45 $msgs = explode("\n%\n",$data); 46 foreach($msgs as $msg){ 47 if($msg) msg($msg,2); 48 } 49} 50 51 52/** 53 * Return DokuWiki's version (split up in date and type) 54 * 55 * @author Andreas Gohr <andi@splitbrain.org> 56 */ 57function getVersionData(){ 58 $version = array(); 59 //import version string 60 if(@file_exists(DOKU_INC.'VERSION')){ 61 //official release 62 $version['date'] = trim(io_readfile(DOKU_INC.'VERSION')); 63 $version['type'] = 'Release'; 64 }elseif(is_dir(DOKU_INC.'.git')){ 65 $version['type'] = 'Git'; 66 $version['date'] = 'unknown'; 67 68 $inventory = DOKU_INC.'.git/logs/HEAD'; 69 if(is_file($inventory)){ 70 $sz = filesize($inventory); 71 $seek = max(0,$sz-2000); // read from back of the file 72 $fh = fopen($inventory,'rb'); 73 fseek($fh,$seek); 74 $chunk = fread($fh,2000); 75 fclose($fh); 76 $chunk = trim($chunk); 77 $chunk = @array_pop(explode("\n",$chunk)); //last log line 78 $chunk = @array_shift(explode("\t",$chunk)); //strip commit msg 79 $chunk = explode(" ",$chunk); 80 array_pop($chunk); //strip timezone 81 $date = date('Y-m-d',array_pop($chunk)); 82 if($date) $version['date'] = $date; 83 } 84 }else{ 85 global $updateVersion; 86 $version['date'] = 'update version '.$updateVersion; 87 $version['type'] = 'snapshot?'; 88 } 89 return $version; 90} 91 92/** 93 * Return DokuWiki's version (as a string) 94 * 95 * @author Anika Henke <anika@selfthinker.org> 96 */ 97function getVersion(){ 98 $version = getVersionData(); 99 return $version['type'].' '.$version['date']; 100} 101 102/** 103 * Run a few sanity checks 104 * 105 * @author Andreas Gohr <andi@splitbrain.org> 106 */ 107function check(){ 108 global $conf; 109 global $INFO; 110 /* @var Input $INPUT */ 111 global $INPUT; 112 113 if ($INFO['isadmin'] || $INFO['ismanager']){ 114 msg('DokuWiki version: '.getVersion(),1); 115 116 if(version_compare(phpversion(),'5.2.0','<')){ 117 msg('Your PHP version is too old ('.phpversion().' vs. 5.2.0+ needed)',-1); 118 }else{ 119 msg('PHP version '.phpversion(),1); 120 } 121 } else { 122 if(version_compare(phpversion(),'5.2.0','<')){ 123 msg('Your PHP version is too old',-1); 124 } 125 } 126 127 $mem = (int) php_to_byte(ini_get('memory_limit')); 128 if($mem){ 129 if($mem < 16777216){ 130 msg('PHP is limited to less than 16MB RAM ('.$mem.' bytes). Increase memory_limit in php.ini',-1); 131 }elseif($mem < 20971520){ 132 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); 133 }elseif($mem < 33554432){ 134 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); 135 }else{ 136 msg('More than 32MB RAM ('.$mem.' bytes) available.',1); 137 } 138 } 139 140 if(is_writable($conf['changelog'])){ 141 msg('Changelog is writable',1); 142 }else{ 143 if (@file_exists($conf['changelog'])) { 144 msg('Changelog is not writable',-1); 145 } 146 } 147 148 if (isset($conf['changelog_old']) && @file_exists($conf['changelog_old'])) { 149 msg('Old changelog exists', 0); 150 } 151 152 if (@file_exists($conf['changelog'].'_failed')) { 153 msg('Importing old changelog failed', -1); 154 } else if (@file_exists($conf['changelog'].'_importing')) { 155 msg('Importing old changelog now.', 0); 156 } else if (@file_exists($conf['changelog'].'_import_ok')) { 157 msg('Old changelog imported', 1); 158 if (!plugin_isdisabled('importoldchangelog')) { 159 msg('Importoldchangelog plugin not disabled after import', -1); 160 } 161 } 162 163 if(is_writable(DOKU_CONF)){ 164 msg('conf directory is writable',1); 165 }else{ 166 msg('conf directory is not writable',-1); 167 } 168 169 if($conf['authtype'] == 'plain'){ 170 global $config_cascade; 171 if(is_writable($config_cascade['plainauth.users']['default'])){ 172 msg('conf/users.auth.php is writable',1); 173 }else{ 174 msg('conf/users.auth.php is not writable',0); 175 } 176 } 177 178 if(function_exists('mb_strpos')){ 179 if(defined('UTF8_NOMBSTRING')){ 180 msg('mb_string extension is available but will not be used',0); 181 }else{ 182 msg('mb_string extension is available and will be used',1); 183 if(ini_get('mbstring.func_overload') != 0){ 184 msg('mb_string function overloading is enabled, this will cause problems and should be disabled',-1); 185 } 186 } 187 }else{ 188 msg('mb_string extension not available - PHP only replacements will be used',0); 189 } 190 191 if (!UTF8_PREGSUPPORT) { 192 msg('PHP is missing UTF-8 support in Perl-Compatible Regular Expressions (PCRE)', -1); 193 } 194 if (!UTF8_PROPERTYSUPPORT) { 195 msg('PHP is missing Unicode properties support in Perl-Compatible Regular Expressions (PCRE)', -1); 196 } 197 198 $loc = setlocale(LC_ALL, 0); 199 if(!$loc){ 200 msg('No valid locale is set for your PHP setup. You should fix this',-1); 201 }elseif(stripos($loc,'utf') === false){ 202 msg('Your locale <code>'.hsc($loc).'</code> seems not to be a UTF-8 locale, you should fix this if you encounter problems.',0); 203 }else{ 204 msg('Valid locale '.hsc($loc).' found.', 1); 205 } 206 207 if($conf['allowdebug']){ 208 msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1); 209 }else{ 210 msg('Debugging support is disabled',1); 211 } 212 213 if($INFO['userinfo']['name']){ 214 msg('You are currently logged in as '.$INPUT->server->str('REMOTE_USER').' ('.$INFO['userinfo']['name'].')',0); 215 msg('You are part of the groups '.join($INFO['userinfo']['grps'],', '),0); 216 }else{ 217 msg('You are currently not logged in',0); 218 } 219 220 msg('Your current permission for this page is '.$INFO['perm'],0); 221 222 if(is_writable($INFO['filepath'])){ 223 msg('The current page is writable by the webserver',0); 224 }else{ 225 msg('The current page is not writable by the webserver',0); 226 } 227 228 if($INFO['writable']){ 229 msg('The current page is writable by you',0); 230 }else{ 231 msg('The current page is not writable by you',0); 232 } 233 234 // Check for corrupted search index 235 $lengths = idx_listIndexLengths(); 236 $index_corrupted = false; 237 foreach ($lengths as $length) { 238 if (count(idx_getIndex('w', $length)) != count(idx_getIndex('i', $length))) { 239 $index_corrupted = true; 240 break; 241 } 242 } 243 244 foreach (idx_getIndex('metadata', '') as $index) { 245 if (count(idx_getIndex($index.'_w', '')) != count(idx_getIndex($index.'_i', ''))) { 246 $index_corrupted = true; 247 break; 248 } 249 } 250 251 if ($index_corrupted) 252 msg('The search index is corrupted. It might produce wrong results and most 253 probably needs to be rebuilt. See 254 <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a> 255 for ways to rebuild the search index.', -1); 256 elseif (!empty($lengths)) 257 msg('The search index seems to be working', 1); 258 else 259 msg('The search index is empty. See 260 <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a> 261 for help on how to fix the search index. If the default indexer 262 isn\'t used or the wiki is actually empty this is normal.'); 263} 264 265/** 266 * print a message 267 * 268 * If HTTP headers were not sent yet the message is added 269 * to the global message array else it's printed directly 270 * using html_msgarea() 271 * 272 * 273 * Levels can be: 274 * 275 * -1 error 276 * 0 info 277 * 1 success 278 * 279 * @author Andreas Gohr <andi@splitbrain.org> 280 * @see html_msgarea 281 */ 282 283define('MSG_PUBLIC', 0); 284define('MSG_USERS_ONLY', 1); 285define('MSG_MANAGERS_ONLY',2); 286define('MSG_ADMINS_ONLY',4); 287 288/** 289 * Display a message to the user 290 * 291 * @param string $message 292 * @param int $lvl -1 = error, 0 = info, 1 = success, 2 = notify 293 * @param string $line line number 294 * @param string $file file number 295 * @param int $allow who's allowed to see the message, see MSG_* constants 296 */ 297function msg($message,$lvl=0,$line='',$file='',$allow=MSG_PUBLIC){ 298 global $MSG, $MSG_shown; 299 $errors = array(); 300 $errors[-1] = 'error'; 301 $errors[0] = 'info'; 302 $errors[1] = 'success'; 303 $errors[2] = 'notify'; 304 305 if($line || $file) $message.=' ['.utf8_basename($file).':'.$line.']'; 306 307 if(!isset($MSG)) $MSG = array(); 308 $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message, 'allow' => $allow); 309 if(isset($MSG_shown) || headers_sent()){ 310 if(function_exists('html_msgarea')){ 311 html_msgarea(); 312 }else{ 313 print "ERROR($lvl) $message"; 314 } 315 unset($GLOBALS['MSG']); 316 } 317} 318/** 319 * Determine whether the current user is allowed to view the message 320 * in the $msg data structure 321 * 322 * @param $msg array dokuwiki msg structure 323 * msg => string, the message 324 * lvl => int, level of the message (see msg() function) 325 * allow => int, flag used to determine who is allowed to see the message 326 * see MSG_* constants 327 * @return bool 328 */ 329function info_msg_allowed($msg){ 330 global $INFO, $auth; 331 332 // is the message public? - everyone and anyone can see it 333 if (empty($msg['allow']) || ($msg['allow'] == MSG_PUBLIC)) return true; 334 335 // restricted msg, but no authentication 336 if (empty($auth)) return false; 337 338 switch ($msg['allow']){ 339 case MSG_USERS_ONLY: 340 return !empty($INFO['userinfo']); 341 342 case MSG_MANAGERS_ONLY: 343 return $INFO['ismanager']; 344 345 case MSG_ADMINS_ONLY: 346 return $INFO['isadmin']; 347 348 default: 349 trigger_error('invalid msg allow restriction. msg="'.$msg['msg'].'" allow='.$msg['allow'].'"', E_USER_WARNING); 350 return $INFO['isadmin']; 351 } 352 353 return false; 354} 355 356/** 357 * print debug messages 358 * 359 * little function to print the content of a var 360 * 361 * @author Andreas Gohr <andi@splitbrain.org> 362 */ 363function dbg($msg,$hidden=false){ 364 if($hidden){ 365 echo "<!--\n"; 366 print_r($msg); 367 echo "\n-->"; 368 }else{ 369 echo '<pre class="dbg">'; 370 echo hsc(print_r($msg,true)); 371 echo '</pre>'; 372 } 373} 374 375/** 376 * Print info to a log file 377 * 378 * @author Andreas Gohr <andi@splitbrain.org> 379 */ 380function dbglog($msg,$header=''){ 381 global $conf; 382 /* @var Input $INPUT */ 383 global $INPUT; 384 385 // The debug log isn't automatically cleaned thus only write it when 386 // debugging has been enabled by the user. 387 if($conf['allowdebug'] !== 1) return; 388 if(is_object($msg) || is_array($msg)){ 389 $msg = print_r($msg,true); 390 } 391 392 if($header) $msg = "$header\n$msg"; 393 394 $file = $conf['cachedir'].'/debug.log'; 395 $fh = fopen($file,'a'); 396 if($fh){ 397 fwrite($fh,date('H:i:s ').$INPUT->server->str('REMOTE_ADDR').': '.$msg."\n"); 398 fclose($fh); 399 } 400} 401 402/** 403 * Log accesses to deprecated fucntions to the debug log 404 * 405 * @param string $alternative The function or method that should be used instead 406 */ 407function dbg_deprecated($alternative = '') { 408 global $conf; 409 if(!$conf['allowdebug']) return; 410 411 $backtrace = debug_backtrace(); 412 array_shift($backtrace); 413 $self = array_shift($backtrace); 414 $call = array_shift($backtrace); 415 416 $called = trim($self['class'].'::'.$self['function'].'()', ':'); 417 $caller = trim($call['class'].'::'.$call['function'].'()', ':'); 418 419 $msg = $called.' is deprecated. It was called from '; 420 $msg .= $caller.' in '.$call['file'].':'.$call['line']; 421 if($alternative) { 422 $msg .= ' '.$alternative.' should be used instead!'; 423 } 424 425 dbglog($msg); 426} 427 428/** 429 * Print a reversed, prettyprinted backtrace 430 * 431 * @author Gary Owen <gary_owen@bigfoot.com> 432 */ 433function dbg_backtrace(){ 434 // Get backtrace 435 $backtrace = debug_backtrace(); 436 437 // Unset call to debug_print_backtrace 438 array_shift($backtrace); 439 440 // Iterate backtrace 441 $calls = array(); 442 $depth = count($backtrace) - 1; 443 foreach ($backtrace as $i => $call) { 444 $location = $call['file'] . ':' . $call['line']; 445 $function = (isset($call['class'])) ? 446 $call['class'] . $call['type'] . $call['function'] : $call['function']; 447 448 $params = array(); 449 if (isset($call['args'])){ 450 foreach($call['args'] as $arg){ 451 if(is_object($arg)){ 452 $params[] = '[Object '.get_class($arg).']'; 453 }elseif(is_array($arg)){ 454 $params[] = '[Array]'; 455 }elseif(is_null($arg)){ 456 $params[] = '[NULL]'; 457 }else{ 458 $params[] = (string) '"'.$arg.'"'; 459 } 460 } 461 } 462 $params = implode(', ',$params); 463 464 $calls[$depth - $i] = sprintf('%s(%s) called at %s', 465 $function, 466 str_replace("\n", '\n', $params), 467 $location); 468 } 469 ksort($calls); 470 471 return implode("\n", $calls); 472} 473 474/** 475 * Remove all data from an array where the key seems to point to sensitive data 476 * 477 * This is used to remove passwords, mail addresses and similar data from the 478 * debug output 479 * 480 * @author Andreas Gohr <andi@splitbrain.org> 481 */ 482function debug_guard(&$data){ 483 foreach($data as $key => $value){ 484 if(preg_match('/(notify|pass|auth|secret|ftp|userinfo|token|buid|mail|proxy)/i',$key)){ 485 $data[$key] = '***'; 486 continue; 487 } 488 if(is_array($value)) debug_guard($data[$key]); 489 } 490} 491