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