1c29dc6e4SAndreas Gohr<?php 2c29dc6e4SAndreas Gohr/** 3c29dc6e4SAndreas Gohr * Information and debugging functions 4c29dc6e4SAndreas Gohr * 5c29dc6e4SAndreas Gohr * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 7c29dc6e4SAndreas Gohr */ 824870174SAndreas Gohruse dokuwiki\Extension\Event; 924870174SAndreas Gohruse dokuwiki\Utf8\PhpString; 1024870174SAndreas Gohruse dokuwiki\Debug\DebugHelper; 115a8d6e48SMichael Großeuse dokuwiki\HTTP\DokuHTTPClient; 1231667ec6SAndreas Gohruse dokuwiki\Logger; 13198564abSMichael Große 146c5e3c5eSPhyif (!defined('DOKU_MESSAGEURL')) { 156c5e3c5eSPhy if (in_array('ssl', stream_get_transports())) { 166c5e3c5eSPhy define('DOKU_MESSAGEURL', 'https://update.dokuwiki.org/check/'); 176c5e3c5eSPhy } else { 186c5e3c5eSPhy define('DOKU_MESSAGEURL', 'http://update.dokuwiki.org/check/'); 196c5e3c5eSPhy } 206c5e3c5eSPhy} 21c29dc6e4SAndreas Gohr 22c29dc6e4SAndreas Gohr/** 23c29dc6e4SAndreas Gohr * Check for new messages from upstream 24c29dc6e4SAndreas Gohr * 25c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 26c29dc6e4SAndreas Gohr */ 27d868eb89SAndreas Gohrfunction checkUpdateMessages() 28d868eb89SAndreas Gohr{ 29c29dc6e4SAndreas Gohr global $conf; 30c29dc6e4SAndreas Gohr global $INFO; 31ef362bb8SAnika Henke global $updateVersion; 32c29dc6e4SAndreas Gohr if (!$conf['updatecheck']) return; 33f8cc712eSAndreas Gohr if ($conf['useacl'] && !$INFO['ismanager']) return; 34c29dc6e4SAndreas Gohr 3537b21a1bSAndreas Gohr $cf = getCacheName($updateVersion, '.updmsg'); 36c29dc6e4SAndreas Gohr $lm = @filemtime($cf); 376c5e3c5eSPhy $is_http = substr(DOKU_MESSAGEURL, 0, 5) != 'https'; 38c29dc6e4SAndreas Gohr 39c29dc6e4SAndreas Gohr // check if new messages needs to be fetched 408d3d7569SAnika Henke if ($lm < time() - (60 * 60 * 24) || $lm < @filemtime(DOKU_INC . DOKU_SCRIPT)) { 4163d9b820SAndreas Gohr @touch($cf); 42*90fb952cSAndreas Gohr Logger::debug( 43*90fb952cSAndreas Gohr sprintf( 44*90fb952cSAndreas Gohr 'checkUpdateMessages(): downloading messages to %s%s', 45*90fb952cSAndreas Gohr $cf, 46*90fb952cSAndreas Gohr $is_http ? ' (without SSL)' : ' (with SSL)' 47*90fb952cSAndreas Gohr ) 48*90fb952cSAndreas Gohr ); 49c29dc6e4SAndreas Gohr $http = new DokuHTTPClient(); 5063d9b820SAndreas Gohr $http->timeout = 12; 5186c04d87SAngus Gratton $resp = $http->get(DOKU_MESSAGEURL . $updateVersion); 5286c04d87SAngus Gratton if (is_string($resp) && ($resp == "" || substr(trim($resp), -1) == '%')) { 5386c04d87SAngus Gratton // basic sanity check that this is either an empty string response (ie "no messages") 5486c04d87SAngus Gratton // or it looks like one of our messages, not WiFi login or other interposed response 5586c04d87SAngus Gratton io_saveFile($cf, $resp); 568f1efc43SAndreas Gohr } else { 5731667ec6SAndreas Gohr Logger::debug("checkUpdateMessages(): unexpected HTTP response received", $http->error); 588f1efc43SAndreas Gohr } 59c29dc6e4SAndreas Gohr } else { 6031667ec6SAndreas Gohr Logger::debug("checkUpdateMessages(): messages up to date"); 61c29dc6e4SAndreas Gohr } 62c29dc6e4SAndreas Gohr 6386c04d87SAngus Gratton $data = io_readFile($cf); 64c29dc6e4SAndreas Gohr // show messages through the usual message mechanism 65c29dc6e4SAndreas Gohr $msgs = explode("\n%\n", $data); 66c29dc6e4SAndreas Gohr foreach ($msgs as $msg) { 67c29dc6e4SAndreas Gohr if ($msg) msg($msg, 2); 68c29dc6e4SAndreas Gohr } 69c29dc6e4SAndreas Gohr} 70c29dc6e4SAndreas Gohr 71c29dc6e4SAndreas Gohr 72c29dc6e4SAndreas Gohr/** 7325c07f93SAnika Henke * Return DokuWiki's version (split up in date and type) 74c29dc6e4SAndreas Gohr * 75c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 76c29dc6e4SAndreas Gohr */ 77d868eb89SAndreas Gohrfunction getVersionData() 78d868eb89SAndreas Gohr{ 7924870174SAndreas Gohr $version = []; 80c29dc6e4SAndreas Gohr //import version string 8179e79377SAndreas Gohr if (file_exists(DOKU_INC . 'VERSION')) { 82c29dc6e4SAndreas Gohr //official release 83d6c7b502SAndreas Gohr $version['date'] = trim(io_readFile(DOKU_INC . 'VERSION')); 8425c07f93SAnika Henke $version['type'] = 'Release'; 855cf31920SAndreas Gohr } elseif (is_dir(DOKU_INC . '.git')) { 865cf31920SAndreas Gohr $version['type'] = 'Git'; 8725c07f93SAnika Henke $version['date'] = 'unknown'; 88e570ed43SAndreas Gohr 89b9f5205aSDamien Regad // First try to get date and commit hash by calling Git 90b6f8a5c6SDamien Regad if (function_exists('shell_exec')) { 91b6f8a5c6SDamien Regad $commitInfo = shell_exec("git log -1 --pretty=format:'%h %cd' --date=short"); 92b9f5205aSDamien Regad if ($commitInfo) { 9324870174SAndreas Gohr [$version['sha'], $date] = explode(' ', $commitInfo); 94067b4fb9SMichael Große $version['date'] = hsc($date); 95b9f5205aSDamien Regad return $version; 96b9f5205aSDamien Regad } 97b9f5205aSDamien Regad } 98b9f5205aSDamien Regad 99f519f9dbSMichael Große // we cannot use git on the shell -- let's do it manually! 100b9f5205aSDamien Regad if (file_exists(DOKU_INC . '.git/HEAD')) { 101f519f9dbSMichael Große $headCommit = trim(file_get_contents(DOKU_INC . '.git/HEAD')); 102f519f9dbSMichael Große if (strpos($headCommit, 'ref: ') === 0) { 103f519f9dbSMichael Große // it is something like `ref: refs/heads/master` 10409bf5d22SDamien Regad $headCommit = substr($headCommit, 5); 10509bf5d22SDamien Regad $pathToHead = DOKU_INC . '.git/' . $headCommit; 10609bf5d22SDamien Regad if (file_exists($pathToHead)) { 10709bf5d22SDamien Regad $headCommit = trim(file_get_contents($pathToHead)); 10809bf5d22SDamien Regad } else { 10909bf5d22SDamien Regad $packedRefs = file_get_contents(DOKU_INC . '.git/packed-refs'); 11009bf5d22SDamien Regad if (!preg_match("~([[:xdigit:]]+) $headCommit~", $packedRefs, $matches)) { 11109bf5d22SDamien Regad # ref not found in pack file 11209bf5d22SDamien Regad return $version; 113f519f9dbSMichael Große } 11409bf5d22SDamien Regad $headCommit = $matches[1]; 11509bf5d22SDamien Regad } 11609bf5d22SDamien Regad } 11709bf5d22SDamien Regad // At this point $headCommit is a SHA 118b6f8a5c6SDamien Regad $version['sha'] = $headCommit; 119b6f8a5c6SDamien Regad 12009bf5d22SDamien Regad // Get commit date from Git object 121f519f9dbSMichael Große $subDir = substr($headCommit, 0, 2); 122f519f9dbSMichael Große $fileName = substr($headCommit, 2); 123be2e462dSMichael Große $gitCommitObject = DOKU_INC . ".git/objects/$subDir/$fileName"; 124a8ac20eaSGerrit Uitslag if (file_exists($gitCommitObject) && function_exists('zlib_decode')) { 125be2e462dSMichael Große $commit = zlib_decode(file_get_contents($gitCommitObject)); 126f519f9dbSMichael Große $committerLine = explode("\n", $commit)[3]; 127f519f9dbSMichael Große $committerData = explode(' ', $committerLine); 128f519f9dbSMichael Große end($committerData); 129f519f9dbSMichael Große $ts = prev($committerData); 130f519f9dbSMichael Große if ($ts && $date = date('Y-m-d', $ts)) { 131f519f9dbSMichael Große $version['date'] = $date; 132f519f9dbSMichael Große } 1335cf31920SAndreas Gohr } 13425c07f93SAnika Henke } 135c29dc6e4SAndreas Gohr } else { 1366a34de2dSAnika Henke global $updateVersion; 1376a34de2dSAnika Henke $version['date'] = 'update version ' . $updateVersion; 13825c07f93SAnika Henke $version['type'] = 'snapshot?'; 139c29dc6e4SAndreas Gohr } 1405cf31920SAndreas Gohr return $version; 141c29dc6e4SAndreas Gohr} 142c29dc6e4SAndreas Gohr 143c29dc6e4SAndreas Gohr/** 14425c07f93SAnika Henke * Return DokuWiki's version (as a string) 14525c07f93SAnika Henke * 14625c07f93SAnika Henke * @author Anika Henke <anika@selfthinker.org> 14725c07f93SAnika Henke */ 148d868eb89SAndreas Gohrfunction getVersion() 149d868eb89SAndreas Gohr{ 15025c07f93SAnika Henke $version = getVersionData(); 15124870174SAndreas Gohr $sha = empty($version['sha']) ? '' : ' (' . $version['sha'] . ')'; 152b6f8a5c6SDamien Regad return $version['type'] . ' ' . $version['date'] . $sha; 15325c07f93SAnika Henke} 15425c07f93SAnika Henke 15525c07f93SAnika Henke/** 156c29dc6e4SAndreas Gohr * Run a few sanity checks 157c29dc6e4SAndreas Gohr * 158c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 159c29dc6e4SAndreas Gohr */ 160d868eb89SAndreas Gohrfunction check() 161d868eb89SAndreas Gohr{ 162c29dc6e4SAndreas Gohr global $conf; 163c29dc6e4SAndreas Gohr global $INFO; 164585bf44eSChristopher Smith /* @var Input $INPUT */ 165585bf44eSChristopher Smith global $INPUT; 166c29dc6e4SAndreas Gohr 1673f803e5eSGina Haeussge if ($INFO['isadmin'] || $INFO['ismanager']) { 168c29dc6e4SAndreas Gohr msg('DokuWiki version: ' . getVersion(), 1); 169c49393f5SAndreas Gohr if (version_compare(phpversion(), '7.4.0', '<')) { 170c49393f5SAndreas Gohr msg('Your PHP version is too old (' . phpversion() . ' vs. 7.4+ needed)', -1); 171c29dc6e4SAndreas Gohr } else { 172c29dc6e4SAndreas Gohr msg('PHP version ' . phpversion(), 1); 173c29dc6e4SAndreas Gohr } 17424870174SAndreas Gohr } elseif (version_compare(phpversion(), '7.4.0', '<')) { 17508d1a8dfSAndreas Gohr msg('Your PHP version is too old', -1); 17608d1a8dfSAndreas Gohr } 177c6e971ddSAndreas Gohr 17824870174SAndreas Gohr $mem = php_to_byte(ini_get('memory_limit')); 17973038c47SAndreas Gohr if ($mem) { 180eb2e46caSAndreas Gohr if ($mem === -1) { 1818f8499faSpeterfromearth msg('PHP memory is unlimited', 1); 18224870174SAndreas Gohr } elseif ($mem < 16_777_216) { 1832b9c4a05SAndreas Gohr msg('PHP is limited to less than 16MB RAM (' . filesize_h($mem) . '). 1842b9c4a05SAndreas Gohr Increase memory_limit in php.ini', -1); 18524870174SAndreas Gohr } elseif ($mem < 20_971_520) { 1862b9c4a05SAndreas Gohr msg('PHP is limited to less than 20MB RAM (' . filesize_h($mem) . '), 18764159a61SAndreas Gohr you might encounter problems with bigger pages. Increase memory_limit in php.ini', -1); 18824870174SAndreas Gohr } elseif ($mem < 33_554_432) { 1892b9c4a05SAndreas Gohr msg('PHP is limited to less than 32MB RAM (' . filesize_h($mem) . '), 19064159a61SAndreas Gohr but that should be enough in most cases. If not, increase memory_limit in php.ini', 0); 19173038c47SAndreas Gohr } else { 192c6e971ddSAndreas Gohr msg('More than 32MB RAM (' . filesize_h($mem) . ') available.', 1); 19373038c47SAndreas Gohr } 19473038c47SAndreas Gohr } 19573038c47SAndreas Gohr 196c29dc6e4SAndreas Gohr if (is_writable($conf['changelog'])) { 197c29dc6e4SAndreas Gohr msg('Changelog is writable', 1); 19824870174SAndreas Gohr } elseif (file_exists($conf['changelog'])) { 199c29dc6e4SAndreas Gohr msg('Changelog is not writable', -1); 200c29dc6e4SAndreas Gohr } 201c29dc6e4SAndreas Gohr 20279e79377SAndreas Gohr if (isset($conf['changelog_old']) && file_exists($conf['changelog_old'])) { 2032cdbda06SAnika Henke msg('Old changelog exists', 0); 204c29dc6e4SAndreas Gohr } 205c29dc6e4SAndreas Gohr 20679e79377SAndreas Gohr if (file_exists($conf['changelog'] . '_failed')) { 2072cdbda06SAnika Henke msg('Importing old changelog failed', -1); 20879e79377SAndreas Gohr } elseif (file_exists($conf['changelog'] . '_importing')) { 209c29dc6e4SAndreas Gohr msg('Importing old changelog now.', 0); 21079e79377SAndreas Gohr } elseif (file_exists($conf['changelog'] . '_import_ok')) { 2112cdbda06SAnika Henke msg('Old changelog imported', 1); 212c29dc6e4SAndreas Gohr if (!plugin_isdisabled('importoldchangelog')) { 2132cdbda06SAnika Henke msg('Importoldchangelog plugin not disabled after import', -1); 214c29dc6e4SAndreas Gohr } 215c29dc6e4SAndreas Gohr } 216c29dc6e4SAndreas Gohr 2174c7ecf15SGuy Brand if (is_writable(DOKU_CONF)) { 2184c7ecf15SGuy Brand msg('conf directory is writable', 1); 2194c7ecf15SGuy Brand } else { 2204c7ecf15SGuy Brand msg('conf directory is not writable', -1); 2214c7ecf15SGuy Brand } 2224c7ecf15SGuy Brand 2230d487d8fSAndreas Gohr if ($conf['authtype'] == 'plain') { 224defb7d57SAnika Henke global $config_cascade; 225defb7d57SAnika Henke if (is_writable($config_cascade['plainauth.users']['default'])) { 226c29dc6e4SAndreas Gohr msg('conf/users.auth.php is writable', 1); 227c29dc6e4SAndreas Gohr } else { 228c29dc6e4SAndreas Gohr msg('conf/users.auth.php is not writable', 0); 229c29dc6e4SAndreas Gohr } 2300d487d8fSAndreas Gohr } 231c29dc6e4SAndreas Gohr 232c29dc6e4SAndreas Gohr if (function_exists('mb_strpos')) { 233c29dc6e4SAndreas Gohr if (defined('UTF8_NOMBSTRING')) { 234c29dc6e4SAndreas Gohr msg('mb_string extension is available but will not be used', 0); 235c29dc6e4SAndreas Gohr } else { 236c29dc6e4SAndreas Gohr msg('mb_string extension is available and will be used', 1); 2374222b898SAndreas Gohr if (ini_get('mbstring.func_overload') != 0) { 2384222b898SAndreas Gohr msg('mb_string function overloading is enabled, this will cause problems and should be disabled', -1); 2394222b898SAndreas Gohr } 240c29dc6e4SAndreas Gohr } 241c29dc6e4SAndreas Gohr } else { 242c29dc6e4SAndreas Gohr msg('mb_string extension not available - PHP only replacements will be used', 0); 243c29dc6e4SAndreas Gohr } 244c29dc6e4SAndreas Gohr 2453161005dSAndreas Gohr if (!UTF8_PREGSUPPORT) { 246a731ed1dSAndreas Gohr msg('PHP is missing UTF-8 support in Perl-Compatible Regular Expressions (PCRE)', -1); 247a731ed1dSAndreas Gohr } 2483161005dSAndreas Gohr if (!UTF8_PROPERTYSUPPORT) { 249a731ed1dSAndreas Gohr msg('PHP is missing Unicode properties support in Perl-Compatible Regular Expressions (PCRE)', -1); 250a731ed1dSAndreas Gohr } 251a731ed1dSAndreas Gohr 252e5ab313fSAndreas Gohr $loc = setlocale(LC_ALL, 0); 253e5ab313fSAndreas Gohr if (!$loc) { 254e5ab313fSAndreas Gohr msg('No valid locale is set for your PHP setup. You should fix this', -1); 255e5ab313fSAndreas Gohr } elseif (stripos($loc, 'utf') === false) { 25664159a61SAndreas Gohr msg('Your locale <code>' . hsc($loc) . '</code> seems not to be a UTF-8 locale, 25764159a61SAndreas Gohr you should fix this if you encounter problems.', 0); 258e5ab313fSAndreas Gohr } else { 259e5ab313fSAndreas Gohr msg('Valid locale ' . hsc($loc) . ' found.', 1); 260e5ab313fSAndreas Gohr } 261e5ab313fSAndreas Gohr 262c29dc6e4SAndreas Gohr if ($conf['allowdebug']) { 263c29dc6e4SAndreas Gohr msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0', -1); 264c29dc6e4SAndreas Gohr } else { 265c29dc6e4SAndreas Gohr msg('Debugging support is disabled', 1); 266c29dc6e4SAndreas Gohr } 267c29dc6e4SAndreas Gohr 268a4231b8cSfiwswe if (!empty($INFO['userinfo']['name'])) { 269*90fb952cSAndreas Gohr msg(sprintf( 270*90fb952cSAndreas Gohr "You are currently logged in as %s (%s)", 271*90fb952cSAndreas Gohr $INPUT->server->str('REMOTE_USER'), 272*90fb952cSAndreas Gohr $INFO['userinfo']['name'] 273*90fb952cSAndreas Gohr ), 0); 2748368419bSSyntaxseed msg('You are part of the groups ' . implode(', ', $INFO['userinfo']['grps']), 0); 2753d3c095dSMike Frysinger } else { 2763d3c095dSMike Frysinger msg('You are currently not logged in', 0); 2773d3c095dSMike Frysinger } 2783d3c095dSMike Frysinger 279c29dc6e4SAndreas Gohr msg('Your current permission for this page is ' . $INFO['perm'], 0); 280c29dc6e4SAndreas Gohr 281322ad074SAndreas Gohr if (file_exists($INFO['filepath']) && is_writable($INFO['filepath'])) { 282322ad074SAndreas Gohr msg('The current page is writable by the webserver', 1); 283322ad074SAndreas Gohr } elseif (!file_exists($INFO['filepath']) && is_writable(dirname($INFO['filepath']))) { 284322ad074SAndreas Gohr msg('The current page can be created by the webserver', 1); 285c29dc6e4SAndreas Gohr } else { 286322ad074SAndreas Gohr msg('The current page is not writable by the webserver', -1); 287c29dc6e4SAndreas Gohr } 288c29dc6e4SAndreas Gohr 289c29dc6e4SAndreas Gohr if ($INFO['writable']) { 290322ad074SAndreas Gohr msg('The current page is writable by you', 1); 291c29dc6e4SAndreas Gohr } else { 292322ad074SAndreas Gohr msg('The current page is not writable by you', -1); 293c29dc6e4SAndreas Gohr } 2943b1dfc83SAndreas Gohr 29526f7dbf5SMichael Hamann // Check for corrupted search index 29626f7dbf5SMichael Hamann $lengths = idx_listIndexLengths(); 29726f7dbf5SMichael Hamann $index_corrupted = false; 29826f7dbf5SMichael Hamann foreach ($lengths as $length) { 29924870174SAndreas Gohr if (count(idx_getIndex('w', $length)) !== count(idx_getIndex('i', $length))) { 30026f7dbf5SMichael Hamann $index_corrupted = true; 30126f7dbf5SMichael Hamann break; 30226f7dbf5SMichael Hamann } 30326f7dbf5SMichael Hamann } 30426f7dbf5SMichael Hamann 30526f7dbf5SMichael Hamann foreach (idx_getIndex('metadata', '') as $index) { 30624870174SAndreas Gohr if (count(idx_getIndex($index . '_w', '')) !== count(idx_getIndex($index . '_i', ''))) { 30726f7dbf5SMichael Hamann $index_corrupted = true; 30826f7dbf5SMichael Hamann break; 30926f7dbf5SMichael Hamann } 31026f7dbf5SMichael Hamann } 31126f7dbf5SMichael Hamann 312d6c7b502SAndreas Gohr if ($index_corrupted) { 313d6c7b502SAndreas Gohr msg( 314d6c7b502SAndreas Gohr 'The search index is corrupted. It might produce wrong results and most 3153d94d9edSMichael Hamann probably needs to be rebuilt. See 31626f7dbf5SMichael Hamann <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a> 317dccd6b2bSAndreas Gohr for ways to rebuild the search index.', 318dccd6b2bSAndreas Gohr -1 319d6c7b502SAndreas Gohr ); 320d6c7b502SAndreas Gohr } elseif (!empty($lengths)) { 3213d94d9edSMichael Hamann msg('The search index seems to be working', 1); 322d6c7b502SAndreas Gohr } else { 323d6c7b502SAndreas Gohr msg( 324d6c7b502SAndreas Gohr 'The search index is empty. See 32526f7dbf5SMichael Hamann <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a> 3263d94d9edSMichael Hamann for help on how to fix the search index. If the default indexer 327d6c7b502SAndreas Gohr isn\'t used or the wiki is actually empty this is normal.' 328d6c7b502SAndreas Gohr ); 329d6c7b502SAndreas Gohr } 330d6c7b502SAndreas Gohr 331d6c7b502SAndreas Gohr // rough time check 332d6c7b502SAndreas Gohr $http = new DokuHTTPClient(); 333d6c7b502SAndreas Gohr $http->max_redirect = 0; 334d6c7b502SAndreas Gohr $http->timeout = 3; 335d6c7b502SAndreas Gohr $http->sendRequest('http://www.dokuwiki.org', '', 'HEAD'); 336d6c7b502SAndreas Gohr $now = time(); 337d6c7b502SAndreas Gohr if (isset($http->resp_headers['date'])) { 338d6c7b502SAndreas Gohr $time = strtotime($http->resp_headers['date']); 339d6c7b502SAndreas Gohr $diff = $time - $now; 340d6c7b502SAndreas Gohr 341d6c7b502SAndreas Gohr if (abs($diff) < 4) { 342d6c7b502SAndreas Gohr msg("Server time seems to be okay. Diff: {$diff}s", 1); 343d6c7b502SAndreas Gohr } else { 34464159a61SAndreas Gohr msg("Your server's clock seems to be out of sync! 34564159a61SAndreas Gohr Consider configuring a sync with a NTP server. Diff: {$diff}s"); 346d6c7b502SAndreas Gohr } 347d6c7b502SAndreas Gohr } 348d6c7b502SAndreas Gohr 349c29dc6e4SAndreas Gohr} 350c29dc6e4SAndreas Gohr 351c29dc6e4SAndreas Gohr/** 35246028c4cSAndreas Gohr * Display a message to the user 353c29dc6e4SAndreas Gohr * 354c29dc6e4SAndreas Gohr * If HTTP headers were not sent yet the message is added 355c29dc6e4SAndreas Gohr * to the global message array else it's printed directly 356c29dc6e4SAndreas Gohr * using html_msgarea() 357c29dc6e4SAndreas Gohr * 3580e20480fSPhy * Triggers INFOUTIL_MSG_SHOW 3590e20480fSPhy * 36046028c4cSAndreas Gohr * @see html_msgarea() 3616164d900SAndreas Gohr * @param string $message 3626164d900SAndreas Gohr * @param int $lvl -1 = error, 0 = info, 1 = success, 2 = notify 3636164d900SAndreas Gohr * @param string $line line number 3646164d900SAndreas Gohr * @param string $file file number 3656164d900SAndreas Gohr * @param int $allow who's allowed to see the message, see MSG_* constants 3666164d900SAndreas Gohr */ 367d868eb89SAndreas Gohrfunction msg($message, $lvl = 0, $line = '', $file = '', $allow = MSG_PUBLIC) 368d868eb89SAndreas Gohr{ 369cc58224cSMichael Hamann global $MSG, $MSG_shown; 370556e996eSAndreas Gohr static $errors = [ 371556e996eSAndreas Gohr -1 => 'error', 372556e996eSAndreas Gohr 0 => 'info', 373556e996eSAndreas Gohr 1 => 'success', 374556e996eSAndreas Gohr 2 => 'notify', 375556e996eSAndreas Gohr ]; 376c29dc6e4SAndreas Gohr 377556e996eSAndreas Gohr $msgdata = [ 3780e20480fSPhy 'msg' => $message, 379556e996eSAndreas Gohr 'lvl' => $errors[$lvl], 3800e20480fSPhy 'allow' => $allow, 3810e20480fSPhy 'line' => $line, 3820e20480fSPhy 'file' => $file, 383556e996eSAndreas Gohr ]; 3840e20480fSPhy 38524870174SAndreas Gohr $evt = new Event('INFOUTIL_MSG_SHOW', $msgdata); 3860e20480fSPhy if ($evt->advise_before()) { 3870e20480fSPhy /* Show msg normally - event could suppress message show */ 3880e20480fSPhy if ($msgdata['line'] || $msgdata['file']) { 38924870174SAndreas Gohr $basename = PhpString::basename($msgdata['file']); 3900e20480fSPhy $msgdata['msg'] .= ' [' . $basename . ':' . $msgdata['line'] . ']'; 3910e20480fSPhy } 392c29dc6e4SAndreas Gohr 39324870174SAndreas Gohr if (!isset($MSG)) $MSG = []; 3940e20480fSPhy $MSG[] = $msgdata; 395cc58224cSMichael Hamann if (isset($MSG_shown) || headers_sent()) { 396c29dc6e4SAndreas Gohr if (function_exists('html_msgarea')) { 397c29dc6e4SAndreas Gohr html_msgarea(); 398c29dc6e4SAndreas Gohr } else { 39926dfc232SAndreas Gohr echo "ERROR(" . $msgdata['lvl'] . ") " . $msgdata['msg'] . "\n"; 400c29dc6e4SAndreas Gohr } 40169266de5SDominik Eckelmann unset($GLOBALS['MSG']); 402c29dc6e4SAndreas Gohr } 403c29dc6e4SAndreas Gohr } 4040e20480fSPhy $evt->advise_after(); 4050e20480fSPhy unset($evt); 4060e20480fSPhy} 407f755f9abSChristopher Smith/** 408f755f9abSChristopher Smith * Determine whether the current user is allowed to view the message 409f755f9abSChristopher Smith * in the $msg data structure 410f755f9abSChristopher Smith * 411f755f9abSChristopher Smith * @param $msg array dokuwiki msg structure 412f755f9abSChristopher Smith * msg => string, the message 413f755f9abSChristopher Smith * lvl => int, level of the message (see msg() function) 414f755f9abSChristopher Smith * allow => int, flag used to determine who is allowed to see the message 415f755f9abSChristopher Smith * see MSG_* constants 4166164d900SAndreas Gohr * @return bool 417f755f9abSChristopher Smith */ 418d868eb89SAndreas Gohrfunction info_msg_allowed($msg) 419d868eb89SAndreas Gohr{ 420d3bae478SChristopher Smith global $INFO, $auth; 421d3bae478SChristopher Smith 422d3bae478SChristopher Smith // is the message public? - everyone and anyone can see it 423f755f9abSChristopher Smith if (empty($msg['allow']) || ($msg['allow'] == MSG_PUBLIC)) return true; 424d3bae478SChristopher Smith 425d3bae478SChristopher Smith // restricted msg, but no authentication 426d3bae478SChristopher Smith if (empty($auth)) return false; 427d3bae478SChristopher Smith 428f755f9abSChristopher Smith switch ($msg['allow']) { 429d3bae478SChristopher Smith case MSG_USERS_ONLY: 430d3bae478SChristopher Smith return !empty($INFO['userinfo']); 431d3bae478SChristopher Smith 432d3bae478SChristopher Smith case MSG_MANAGERS_ONLY: 433d3bae478SChristopher Smith return $INFO['ismanager']; 434d3bae478SChristopher Smith 435d3bae478SChristopher Smith case MSG_ADMINS_ONLY: 436d3bae478SChristopher Smith return $INFO['isadmin']; 437d3bae478SChristopher Smith 438d3bae478SChristopher Smith default: 439dccd6b2bSAndreas Gohr trigger_error( 440dccd6b2bSAndreas Gohr 'invalid msg allow restriction. msg="' . $msg['msg'] . '" allow=' . $msg['allow'] . '"', 441dccd6b2bSAndreas Gohr E_USER_WARNING 442dccd6b2bSAndreas Gohr ); 443d3bae478SChristopher Smith return $INFO['isadmin']; 444d3bae478SChristopher Smith } 445d3bae478SChristopher Smith} 446d3bae478SChristopher Smith 447c29dc6e4SAndreas Gohr/** 448c29dc6e4SAndreas Gohr * print debug messages 449c29dc6e4SAndreas Gohr * 450c29dc6e4SAndreas Gohr * little function to print the content of a var 451c29dc6e4SAndreas Gohr * 452c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 453f50a239bSTakamura * 454f50a239bSTakamura * @param string $msg 455f50a239bSTakamura * @param bool $hidden 456c29dc6e4SAndreas Gohr */ 457d868eb89SAndreas Gohrfunction dbg($msg, $hidden = false) 458d868eb89SAndreas Gohr{ 45913493794SAndreas Gohr if ($hidden) { 46013493794SAndreas Gohr echo "<!--\n"; 461c29dc6e4SAndreas Gohr print_r($msg); 46213493794SAndreas Gohr echo "\n-->"; 46313493794SAndreas Gohr } else { 46413493794SAndreas Gohr echo '<pre class="dbg">'; 46513493794SAndreas Gohr echo hsc(print_r($msg, true)); 46613493794SAndreas Gohr echo '</pre>'; 46713493794SAndreas Gohr } 468c29dc6e4SAndreas Gohr} 469c29dc6e4SAndreas Gohr 470c29dc6e4SAndreas Gohr/** 471cad4fbf6SAndreas Gohr * Print info to debug log file 472c29dc6e4SAndreas Gohr * 473c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 4740ecde6ceSAndreas Gohr * @deprecated 2020-08-13 475f50a239bSTakamura * @param string $msg 476f50a239bSTakamura * @param string $header 477c29dc6e4SAndreas Gohr */ 478d868eb89SAndreas Gohrfunction dbglog($msg, $header = '') 479d868eb89SAndreas Gohr{ 4800ecde6ceSAndreas Gohr dbg_deprecated('\\dokuwiki\\Logger'); 481585bf44eSChristopher Smith 4820ecde6ceSAndreas Gohr // was the msg as single line string? use it as header 483cad4fbf6SAndreas Gohr if ($header === '' && is_string($msg) && strpos($msg, "\n") === false) { 4840ecde6ceSAndreas Gohr $header = $msg; 4850ecde6ceSAndreas Gohr $msg = ''; 486c7408a63SAndreas Gohr } 487c7408a63SAndreas Gohr 48831667ec6SAndreas Gohr Logger::getInstance(Logger::LOG_DEBUG)->log( 489dccd6b2bSAndreas Gohr $header, 490dccd6b2bSAndreas Gohr $msg 4910ecde6ceSAndreas Gohr ); 492c29dc6e4SAndreas Gohr} 493c29dc6e4SAndreas Gohr 494db09e31eSAndreas Gohr/** 4951419a485SAndreas Gohr * Log accesses to deprecated fucntions to the debug log 4961419a485SAndreas Gohr * 4971419a485SAndreas Gohr * @param string $alternative The function or method that should be used instead 49885331086SAndreas Gohr * @triggers INFO_DEPRECATION_LOG 4991419a485SAndreas Gohr */ 500d868eb89SAndreas Gohrfunction dbg_deprecated($alternative = '') 501d868eb89SAndreas Gohr{ 50224870174SAndreas Gohr DebugHelper::dbgDeprecatedFunction($alternative, 2); 50344455016SAndreas Gohr} 5041419a485SAndreas Gohr 5051419a485SAndreas Gohr/** 506db09e31eSAndreas Gohr * Print a reversed, prettyprinted backtrace 507db09e31eSAndreas Gohr * 508db09e31eSAndreas Gohr * @author Gary Owen <gary_owen@bigfoot.com> 509db09e31eSAndreas Gohr */ 510d868eb89SAndreas Gohrfunction dbg_backtrace() 511d868eb89SAndreas Gohr{ 512db09e31eSAndreas Gohr // Get backtrace 513db09e31eSAndreas Gohr $backtrace = debug_backtrace(); 514db09e31eSAndreas Gohr 515db09e31eSAndreas Gohr // Unset call to debug_print_backtrace 516db09e31eSAndreas Gohr array_shift($backtrace); 517db09e31eSAndreas Gohr 518db09e31eSAndreas Gohr // Iterate backtrace 51924870174SAndreas Gohr $calls = []; 520db09e31eSAndreas Gohr $depth = count($backtrace) - 1; 521db09e31eSAndreas Gohr foreach ($backtrace as $i => $call) { 522db09e31eSAndreas Gohr $location = $call['file'] . ':' . $call['line']; 523db09e31eSAndreas Gohr $function = (isset($call['class'])) ? 524db09e31eSAndreas Gohr $call['class'] . $call['type'] . $call['function'] : $call['function']; 525db09e31eSAndreas Gohr 52624870174SAndreas Gohr $params = []; 527db09e31eSAndreas Gohr if (isset($call['args'])) { 5288259f1aaSAndreas Gohr foreach ($call['args'] as $arg) { 5298259f1aaSAndreas Gohr if (is_object($arg)) { 5308259f1aaSAndreas Gohr $params[] = '[Object ' . get_class($arg) . ']'; 5318259f1aaSAndreas Gohr } elseif (is_array($arg)) { 5328259f1aaSAndreas Gohr $params[] = '[Array]'; 5338259f1aaSAndreas Gohr } elseif (is_null($arg)) { 53459bc3b48SGerrit Uitslag $params[] = '[NULL]'; 5358259f1aaSAndreas Gohr } else { 53624870174SAndreas Gohr $params[] = '"' . $arg . '"'; 537db09e31eSAndreas Gohr } 5388259f1aaSAndreas Gohr } 5398259f1aaSAndreas Gohr } 5408259f1aaSAndreas Gohr $params = implode(', ', $params); 541db09e31eSAndreas Gohr 542dccd6b2bSAndreas Gohr $calls[$depth - $i] = sprintf( 543dccd6b2bSAndreas Gohr '%s(%s) called at %s', 544db09e31eSAndreas Gohr $function, 545db09e31eSAndreas Gohr str_replace("\n", '\n', $params), 546dccd6b2bSAndreas Gohr $location 547dccd6b2bSAndreas Gohr ); 548db09e31eSAndreas Gohr } 549db09e31eSAndreas Gohr ksort($calls); 550db09e31eSAndreas Gohr 551db09e31eSAndreas Gohr return implode("\n", $calls); 552db09e31eSAndreas Gohr} 553db09e31eSAndreas Gohr 55424297a69SAndreas Gohr/** 55524297a69SAndreas Gohr * Remove all data from an array where the key seems to point to sensitive data 55624297a69SAndreas Gohr * 55724297a69SAndreas Gohr * This is used to remove passwords, mail addresses and similar data from the 55824297a69SAndreas Gohr * debug output 55924297a69SAndreas Gohr * 56024297a69SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 561f50a239bSTakamura * 562f50a239bSTakamura * @param array $data 56324297a69SAndreas Gohr */ 564d868eb89SAndreas Gohrfunction debug_guard(&$data) 565d868eb89SAndreas Gohr{ 56624297a69SAndreas Gohr foreach ($data as $key => $value) { 56724297a69SAndreas Gohr if (preg_match('/(notify|pass|auth|secret|ftp|userinfo|token|buid|mail|proxy)/i', $key)) { 56824297a69SAndreas Gohr $data[$key] = '***'; 56924297a69SAndreas Gohr continue; 57024297a69SAndreas Gohr } 57124297a69SAndreas Gohr if (is_array($value)) debug_guard($data[$key]); 57224297a69SAndreas Gohr } 57324297a69SAndreas Gohr} 574