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