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 */ 8198564abSMichael Große 95a8d6e48SMichael Großeuse dokuwiki\HTTP\DokuHTTPClient; 10198564abSMichael Große 116c5e3c5eSPhyif(!defined('DOKU_MESSAGEURL')){ 126c5e3c5eSPhy if(in_array('ssl', stream_get_transports())) { 136c5e3c5eSPhy define('DOKU_MESSAGEURL','https://update.dokuwiki.org/check/'); 146c5e3c5eSPhy }else{ 156c5e3c5eSPhy define('DOKU_MESSAGEURL','http://update.dokuwiki.org/check/'); 166c5e3c5eSPhy } 176c5e3c5eSPhy} 18c29dc6e4SAndreas Gohr 19c29dc6e4SAndreas Gohr/** 20c29dc6e4SAndreas Gohr * Check for new messages from upstream 21c29dc6e4SAndreas Gohr * 22c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 23c29dc6e4SAndreas Gohr */ 24c29dc6e4SAndreas Gohrfunction checkUpdateMessages(){ 25c29dc6e4SAndreas Gohr global $conf; 26c29dc6e4SAndreas Gohr global $INFO; 27ef362bb8SAnika Henke global $updateVersion; 28c29dc6e4SAndreas Gohr if(!$conf['updatecheck']) return; 29f8cc712eSAndreas Gohr if($conf['useacl'] && !$INFO['ismanager']) return; 30c29dc6e4SAndreas Gohr 3137b21a1bSAndreas Gohr $cf = getCacheName($updateVersion, '.updmsg'); 32c29dc6e4SAndreas Gohr $lm = @filemtime($cf); 336c5e3c5eSPhy $is_http = substr(DOKU_MESSAGEURL, 0, 5) != 'https'; 34c29dc6e4SAndreas Gohr 35c29dc6e4SAndreas Gohr // check if new messages needs to be fetched 368d3d7569SAnika Henke if($lm < time()-(60*60*24) || $lm < @filemtime(DOKU_INC.DOKU_SCRIPT)){ 3763d9b820SAndreas Gohr @touch($cf); 386c5e3c5eSPhy dbglog("checkUpdateMessages(): downloading messages to ".$cf.($is_http?' (without SSL)':' (with SSL)')); 39c29dc6e4SAndreas Gohr $http = new DokuHTTPClient(); 4063d9b820SAndreas Gohr $http->timeout = 12; 4186c04d87SAngus Gratton $resp = $http->get(DOKU_MESSAGEURL.$updateVersion); 4286c04d87SAngus Gratton if(is_string($resp) && ($resp == "" || substr(trim($resp), -1) == '%')) { 4386c04d87SAngus Gratton // basic sanity check that this is either an empty string response (ie "no messages") 4486c04d87SAngus Gratton // or it looks like one of our messages, not WiFi login or other interposed response 4586c04d87SAngus Gratton io_saveFile($cf,$resp); 468f1efc43SAndreas Gohr } else { 4786c04d87SAngus Gratton dbglog("checkUpdateMessages(): unexpected HTTP response received"); 488f1efc43SAndreas Gohr } 49c29dc6e4SAndreas Gohr }else{ 5037b21a1bSAndreas Gohr dbglog("checkUpdateMessages(): messages up to date"); 51c29dc6e4SAndreas Gohr } 52c29dc6e4SAndreas Gohr 5386c04d87SAngus Gratton $data = io_readFile($cf); 54c29dc6e4SAndreas Gohr // show messages through the usual message mechanism 55c29dc6e4SAndreas Gohr $msgs = explode("\n%\n",$data); 56c29dc6e4SAndreas Gohr foreach($msgs as $msg){ 57c29dc6e4SAndreas Gohr if($msg) msg($msg,2); 58c29dc6e4SAndreas Gohr } 59c29dc6e4SAndreas Gohr} 60c29dc6e4SAndreas Gohr 61c29dc6e4SAndreas Gohr 62c29dc6e4SAndreas Gohr/** 6325c07f93SAnika Henke * Return DokuWiki's version (split up in date and type) 64c29dc6e4SAndreas Gohr * 65c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 66c29dc6e4SAndreas Gohr */ 6725c07f93SAnika Henkefunction getVersionData(){ 6825c07f93SAnika Henke $version = array(); 69c29dc6e4SAndreas Gohr //import version string 7079e79377SAndreas Gohr if(file_exists(DOKU_INC.'VERSION')){ 71c29dc6e4SAndreas Gohr //official release 72d6c7b502SAndreas Gohr $version['date'] = trim(io_readFile(DOKU_INC.'VERSION')); 7325c07f93SAnika Henke $version['type'] = 'Release'; 745cf31920SAndreas Gohr }elseif(is_dir(DOKU_INC.'.git')){ 755cf31920SAndreas Gohr $version['type'] = 'Git'; 7625c07f93SAnika Henke $version['date'] = 'unknown'; 77e570ed43SAndreas Gohr 78067b4fb9SMichael Große if ($date = shell_exec("git log -1 --pretty=format:'%cd' --date=short")) { 79067b4fb9SMichael Große $version['date'] = hsc($date); 80f519f9dbSMichael Große } else if (file_exists(DOKU_INC . '.git/HEAD')) { 81f519f9dbSMichael Große // we cannot use git on the shell -- let's do it manually! 82f519f9dbSMichael Große $headCommit = trim(file_get_contents(DOKU_INC . '.git/HEAD')); 83f519f9dbSMichael Große if (strpos($headCommit, 'ref: ') === 0) { 84f519f9dbSMichael Große // it is something like `ref: refs/heads/master` 85f519f9dbSMichael Große $pathToHead = substr($headCommit, 5); 86f519f9dbSMichael Große $headCommit = trim(file_get_contents(DOKU_INC . '.git/' . $pathToHead)); 87f519f9dbSMichael Große } 88f519f9dbSMichael Große $subDir = substr($headCommit, 0, 2); 89f519f9dbSMichael Große $fileName = substr($headCommit, 2); 90be2e462dSMichael Große $gitCommitObject = DOKU_INC . ".git/objects/$subDir/$fileName"; 9156f47fdaSMichael Große if (file_exists($gitCommitObject) && method_exists(zlib_decode)) { 92be2e462dSMichael Große $commit = zlib_decode(file_get_contents($gitCommitObject)); 93f519f9dbSMichael Große $committerLine = explode("\n", $commit)[3]; 94f519f9dbSMichael Große $committerData = explode(' ', $committerLine); 95f519f9dbSMichael Große end($committerData); 96f519f9dbSMichael Große $ts = prev($committerData); 97f519f9dbSMichael Große if ($ts && $date = date('Y-m-d', $ts)) { 98f519f9dbSMichael Große $version['date'] = $date; 99f519f9dbSMichael Große } 1005cf31920SAndreas Gohr } 10125c07f93SAnika Henke } 102c29dc6e4SAndreas Gohr }else{ 1036a34de2dSAnika Henke global $updateVersion; 1046a34de2dSAnika Henke $version['date'] = 'update version '.$updateVersion; 10525c07f93SAnika Henke $version['type'] = 'snapshot?'; 106c29dc6e4SAndreas Gohr } 1075cf31920SAndreas Gohr return $version; 108c29dc6e4SAndreas Gohr} 109c29dc6e4SAndreas Gohr 110c29dc6e4SAndreas Gohr/** 11125c07f93SAnika Henke * Return DokuWiki's version (as a string) 11225c07f93SAnika Henke * 11325c07f93SAnika Henke * @author Anika Henke <anika@selfthinker.org> 11425c07f93SAnika Henke */ 11525c07f93SAnika Henkefunction getVersion(){ 11625c07f93SAnika Henke $version = getVersionData(); 11725c07f93SAnika Henke return $version['type'].' '.$version['date']; 11825c07f93SAnika Henke} 11925c07f93SAnika Henke 12025c07f93SAnika Henke/** 121c29dc6e4SAndreas Gohr * Run a few sanity checks 122c29dc6e4SAndreas Gohr * 123c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 124c29dc6e4SAndreas Gohr */ 125c29dc6e4SAndreas Gohrfunction check(){ 126c29dc6e4SAndreas Gohr global $conf; 127c29dc6e4SAndreas Gohr global $INFO; 128585bf44eSChristopher Smith /* @var Input $INPUT */ 129585bf44eSChristopher Smith global $INPUT; 130c29dc6e4SAndreas Gohr 1313f803e5eSGina Haeussge if ($INFO['isadmin'] || $INFO['ismanager']){ 132c29dc6e4SAndreas Gohr msg('DokuWiki version: '.getVersion(),1); 133c29dc6e4SAndreas Gohr 1343f874cb3SAndreas Gohr if(version_compare(phpversion(),'7.2.0','<')){ 1353f874cb3SAndreas Gohr msg('Your PHP version is too old ('.phpversion().' vs. 7.2+ needed)',-1); 136c29dc6e4SAndreas Gohr }else{ 137c29dc6e4SAndreas Gohr msg('PHP version '.phpversion(),1); 138c29dc6e4SAndreas Gohr } 13908d1a8dfSAndreas Gohr } else { 1403f874cb3SAndreas Gohr if(version_compare(phpversion(),'7.2.0','<')){ 14108d1a8dfSAndreas Gohr msg('Your PHP version is too old',-1); 14208d1a8dfSAndreas Gohr } 14308d1a8dfSAndreas Gohr } 144c6e971ddSAndreas Gohr 145c6e971ddSAndreas Gohr $mem = (int) php_to_byte(ini_get('memory_limit')); 14673038c47SAndreas Gohr if($mem){ 147eb2e46caSAndreas Gohr if ($mem === -1) { 1488f8499faSpeterfromearth msg('PHP memory is unlimited', 1); 1498f8499faSpeterfromearth } else if ($mem < 16777216) { 1502b9c4a05SAndreas Gohr msg('PHP is limited to less than 16MB RAM (' . filesize_h($mem) . '). 1512b9c4a05SAndreas Gohr Increase memory_limit in php.ini', -1); 15273038c47SAndreas Gohr } else if ($mem < 20971520) { 1532b9c4a05SAndreas Gohr msg('PHP is limited to less than 20MB RAM (' . filesize_h($mem) . '), 15464159a61SAndreas Gohr you might encounter problems with bigger pages. Increase memory_limit in php.ini', -1); 15573038c47SAndreas Gohr } else if ($mem < 33554432) { 1562b9c4a05SAndreas Gohr msg('PHP is limited to less than 32MB RAM (' . filesize_h($mem) . '), 15764159a61SAndreas Gohr but that should be enough in most cases. If not, increase memory_limit in php.ini', 0); 15873038c47SAndreas Gohr } else { 159c6e971ddSAndreas Gohr msg('More than 32MB RAM (' . filesize_h($mem) . ') available.', 1); 16073038c47SAndreas Gohr } 16173038c47SAndreas Gohr } 16273038c47SAndreas Gohr 163c29dc6e4SAndreas Gohr if(is_writable($conf['changelog'])){ 164c29dc6e4SAndreas Gohr msg('Changelog is writable',1); 165c29dc6e4SAndreas Gohr }else{ 16679e79377SAndreas Gohr if (file_exists($conf['changelog'])) { 167c29dc6e4SAndreas Gohr msg('Changelog is not writable',-1); 168c29dc6e4SAndreas Gohr } 169c29dc6e4SAndreas Gohr } 170c29dc6e4SAndreas Gohr 17179e79377SAndreas Gohr if (isset($conf['changelog_old']) && file_exists($conf['changelog_old'])) { 1722cdbda06SAnika Henke msg('Old changelog exists', 0); 173c29dc6e4SAndreas Gohr } 174c29dc6e4SAndreas Gohr 17579e79377SAndreas Gohr if (file_exists($conf['changelog'].'_failed')) { 1762cdbda06SAnika Henke msg('Importing old changelog failed', -1); 17779e79377SAndreas Gohr } else if (file_exists($conf['changelog'].'_importing')) { 178c29dc6e4SAndreas Gohr msg('Importing old changelog now.', 0); 17979e79377SAndreas Gohr } else if (file_exists($conf['changelog'].'_import_ok')) { 1802cdbda06SAnika Henke msg('Old changelog imported', 1); 181c29dc6e4SAndreas Gohr if (!plugin_isdisabled('importoldchangelog')) { 1822cdbda06SAnika Henke msg('Importoldchangelog plugin not disabled after import', -1); 183c29dc6e4SAndreas Gohr } 184c29dc6e4SAndreas Gohr } 185c29dc6e4SAndreas Gohr 1864c7ecf15SGuy Brand if(is_writable(DOKU_CONF)){ 1874c7ecf15SGuy Brand msg('conf directory is writable',1); 1884c7ecf15SGuy Brand }else{ 1894c7ecf15SGuy Brand msg('conf directory is not writable',-1); 1904c7ecf15SGuy Brand } 1914c7ecf15SGuy Brand 1920d487d8fSAndreas Gohr if($conf['authtype'] == 'plain'){ 193defb7d57SAnika Henke global $config_cascade; 194defb7d57SAnika Henke if(is_writable($config_cascade['plainauth.users']['default'])){ 195c29dc6e4SAndreas Gohr msg('conf/users.auth.php is writable',1); 196c29dc6e4SAndreas Gohr }else{ 197c29dc6e4SAndreas Gohr msg('conf/users.auth.php is not writable',0); 198c29dc6e4SAndreas Gohr } 1990d487d8fSAndreas Gohr } 200c29dc6e4SAndreas Gohr 201c29dc6e4SAndreas Gohr if(function_exists('mb_strpos')){ 202c29dc6e4SAndreas Gohr if(defined('UTF8_NOMBSTRING')){ 203c29dc6e4SAndreas Gohr msg('mb_string extension is available but will not be used',0); 204c29dc6e4SAndreas Gohr }else{ 205c29dc6e4SAndreas Gohr msg('mb_string extension is available and will be used',1); 2064222b898SAndreas Gohr if(ini_get('mbstring.func_overload') != 0){ 2074222b898SAndreas Gohr msg('mb_string function overloading is enabled, this will cause problems and should be disabled',-1); 2084222b898SAndreas Gohr } 209c29dc6e4SAndreas Gohr } 210c29dc6e4SAndreas Gohr }else{ 211c29dc6e4SAndreas Gohr msg('mb_string extension not available - PHP only replacements will be used',0); 212c29dc6e4SAndreas Gohr } 213c29dc6e4SAndreas Gohr 2143161005dSAndreas Gohr if (!UTF8_PREGSUPPORT) { 215a731ed1dSAndreas Gohr msg('PHP is missing UTF-8 support in Perl-Compatible Regular Expressions (PCRE)', -1); 216a731ed1dSAndreas Gohr } 2173161005dSAndreas Gohr if (!UTF8_PROPERTYSUPPORT) { 218a731ed1dSAndreas Gohr msg('PHP is missing Unicode properties support in Perl-Compatible Regular Expressions (PCRE)', -1); 219a731ed1dSAndreas Gohr } 220a731ed1dSAndreas Gohr 221e5ab313fSAndreas Gohr $loc = setlocale(LC_ALL, 0); 222e5ab313fSAndreas Gohr if(!$loc){ 223e5ab313fSAndreas Gohr msg('No valid locale is set for your PHP setup. You should fix this',-1); 224e5ab313fSAndreas Gohr }elseif(stripos($loc,'utf') === false){ 22564159a61SAndreas Gohr msg('Your locale <code>'.hsc($loc).'</code> seems not to be a UTF-8 locale, 22664159a61SAndreas Gohr you should fix this if you encounter problems.',0); 227e5ab313fSAndreas Gohr }else{ 228e5ab313fSAndreas Gohr msg('Valid locale '.hsc($loc).' found.', 1); 229e5ab313fSAndreas Gohr } 230e5ab313fSAndreas Gohr 231c29dc6e4SAndreas Gohr if($conf['allowdebug']){ 232c29dc6e4SAndreas Gohr msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1); 233c29dc6e4SAndreas Gohr }else{ 234c29dc6e4SAndreas Gohr msg('Debugging support is disabled',1); 235c29dc6e4SAndreas Gohr } 236c29dc6e4SAndreas Gohr 2373d3c095dSMike Frysinger if($INFO['userinfo']['name']){ 238585bf44eSChristopher Smith msg('You are currently logged in as '.$INPUT->server->str('REMOTE_USER').' ('.$INFO['userinfo']['name'].')',0); 239*8368419bSSyntaxseed msg('You are part of the groups '.implode(', ', $INFO['userinfo']['grps']),0); 2403d3c095dSMike Frysinger }else{ 2413d3c095dSMike Frysinger msg('You are currently not logged in',0); 2423d3c095dSMike Frysinger } 2433d3c095dSMike Frysinger 244c29dc6e4SAndreas Gohr msg('Your current permission for this page is '.$INFO['perm'],0); 245c29dc6e4SAndreas Gohr 246322ad074SAndreas Gohr if (file_exists($INFO['filepath']) && is_writable($INFO['filepath'])) { 247322ad074SAndreas Gohr msg('The current page is writable by the webserver', 1); 248322ad074SAndreas Gohr } elseif (!file_exists($INFO['filepath']) && is_writable(dirname($INFO['filepath']))) { 249322ad074SAndreas Gohr msg('The current page can be created by the webserver', 1); 250c29dc6e4SAndreas Gohr } else { 251322ad074SAndreas Gohr msg('The current page is not writable by the webserver', -1); 252c29dc6e4SAndreas Gohr } 253c29dc6e4SAndreas Gohr 254c29dc6e4SAndreas Gohr if ($INFO['writable']) { 255322ad074SAndreas Gohr msg('The current page is writable by you', 1); 256c29dc6e4SAndreas Gohr } else { 257322ad074SAndreas Gohr msg('The current page is not writable by you', -1); 258c29dc6e4SAndreas Gohr } 2593b1dfc83SAndreas Gohr 26026f7dbf5SMichael Hamann // Check for corrupted search index 26126f7dbf5SMichael Hamann $lengths = idx_listIndexLengths(); 26226f7dbf5SMichael Hamann $index_corrupted = false; 26326f7dbf5SMichael Hamann foreach ($lengths as $length) { 26426f7dbf5SMichael Hamann if (count(idx_getIndex('w', $length)) != count(idx_getIndex('i', $length))) { 26526f7dbf5SMichael Hamann $index_corrupted = true; 26626f7dbf5SMichael Hamann break; 26726f7dbf5SMichael Hamann } 26826f7dbf5SMichael Hamann } 26926f7dbf5SMichael Hamann 27026f7dbf5SMichael Hamann foreach (idx_getIndex('metadata', '') as $index) { 27126f7dbf5SMichael Hamann if (count(idx_getIndex($index.'_w', '')) != count(idx_getIndex($index.'_i', ''))) { 27226f7dbf5SMichael Hamann $index_corrupted = true; 27326f7dbf5SMichael Hamann break; 27426f7dbf5SMichael Hamann } 27526f7dbf5SMichael Hamann } 27626f7dbf5SMichael Hamann 277d6c7b502SAndreas Gohr if($index_corrupted) { 278d6c7b502SAndreas Gohr msg( 279d6c7b502SAndreas Gohr 'The search index is corrupted. It might produce wrong results and most 2803d94d9edSMichael Hamann probably needs to be rebuilt. See 28126f7dbf5SMichael Hamann <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a> 282d6c7b502SAndreas Gohr for ways to rebuild the search index.', -1 283d6c7b502SAndreas Gohr ); 284d6c7b502SAndreas Gohr } elseif(!empty($lengths)) { 2853d94d9edSMichael Hamann msg('The search index seems to be working', 1); 286d6c7b502SAndreas Gohr } else { 287d6c7b502SAndreas Gohr msg( 288d6c7b502SAndreas Gohr 'The search index is empty. See 28926f7dbf5SMichael Hamann <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a> 2903d94d9edSMichael Hamann for help on how to fix the search index. If the default indexer 291d6c7b502SAndreas Gohr isn\'t used or the wiki is actually empty this is normal.' 292d6c7b502SAndreas Gohr ); 293d6c7b502SAndreas Gohr } 294d6c7b502SAndreas Gohr 295d6c7b502SAndreas Gohr // rough time check 296d6c7b502SAndreas Gohr $http = new DokuHTTPClient(); 297d6c7b502SAndreas Gohr $http->max_redirect = 0; 298d6c7b502SAndreas Gohr $http->timeout = 3; 299d6c7b502SAndreas Gohr $http->sendRequest('http://www.dokuwiki.org', '', 'HEAD'); 300d6c7b502SAndreas Gohr $now = time(); 301d6c7b502SAndreas Gohr if(isset($http->resp_headers['date'])) { 302d6c7b502SAndreas Gohr $time = strtotime($http->resp_headers['date']); 303d6c7b502SAndreas Gohr $diff = $time - $now; 304d6c7b502SAndreas Gohr 305d6c7b502SAndreas Gohr if(abs($diff) < 4) { 306d6c7b502SAndreas Gohr msg("Server time seems to be okay. Diff: {$diff}s", 1); 307d6c7b502SAndreas Gohr } else { 30864159a61SAndreas Gohr msg("Your server's clock seems to be out of sync! 30964159a61SAndreas Gohr Consider configuring a sync with a NTP server. Diff: {$diff}s"); 310d6c7b502SAndreas Gohr } 311d6c7b502SAndreas Gohr } 312d6c7b502SAndreas Gohr 313c29dc6e4SAndreas Gohr} 314c29dc6e4SAndreas Gohr 315c29dc6e4SAndreas Gohr/** 31646028c4cSAndreas Gohr * Display a message to the user 317c29dc6e4SAndreas Gohr * 318c29dc6e4SAndreas Gohr * If HTTP headers were not sent yet the message is added 319c29dc6e4SAndreas Gohr * to the global message array else it's printed directly 320c29dc6e4SAndreas Gohr * using html_msgarea() 321c29dc6e4SAndreas Gohr * 3220e20480fSPhy * Triggers INFOUTIL_MSG_SHOW 3230e20480fSPhy * 32446028c4cSAndreas Gohr * @see html_msgarea() 3256164d900SAndreas Gohr * @param string $message 3266164d900SAndreas Gohr * @param int $lvl -1 = error, 0 = info, 1 = success, 2 = notify 3276164d900SAndreas Gohr * @param string $line line number 3286164d900SAndreas Gohr * @param string $file file number 3296164d900SAndreas Gohr * @param int $allow who's allowed to see the message, see MSG_* constants 3306164d900SAndreas Gohr */ 331f755f9abSChristopher Smithfunction msg($message,$lvl=0,$line='',$file='',$allow=MSG_PUBLIC){ 332cc58224cSMichael Hamann global $MSG, $MSG_shown; 333556e996eSAndreas Gohr static $errors = [ 334556e996eSAndreas Gohr -1 => 'error', 335556e996eSAndreas Gohr 0 => 'info', 336556e996eSAndreas Gohr 1 => 'success', 337556e996eSAndreas Gohr 2 => 'notify', 338556e996eSAndreas Gohr ]; 339c29dc6e4SAndreas Gohr 340556e996eSAndreas Gohr $msgdata = [ 3410e20480fSPhy 'msg' => $message, 342556e996eSAndreas Gohr 'lvl' => $errors[$lvl], 3430e20480fSPhy 'allow' => $allow, 3440e20480fSPhy 'line' => $line, 3450e20480fSPhy 'file' => $file, 346556e996eSAndreas Gohr ]; 3470e20480fSPhy 3480e20480fSPhy $evt = new \dokuwiki\Extension\Event('INFOUTIL_MSG_SHOW', $msgdata); 3490e20480fSPhy if ($evt->advise_before()) { 3500e20480fSPhy /* Show msg normally - event could suppress message show */ 3510e20480fSPhy if($msgdata['line'] || $msgdata['file']) { 3520e20480fSPhy $basename = \dokuwiki\Utf8\PhpString::basename($msgdata['file']); 3530e20480fSPhy $msgdata['msg'] .=' ['.$basename.':'.$msgdata['line'].']'; 3540e20480fSPhy } 355c29dc6e4SAndreas Gohr 356c29dc6e4SAndreas Gohr if(!isset($MSG)) $MSG = array(); 3570e20480fSPhy $MSG[] = $msgdata; 358cc58224cSMichael Hamann if(isset($MSG_shown) || headers_sent()){ 359c29dc6e4SAndreas Gohr if(function_exists('html_msgarea')){ 360c29dc6e4SAndreas Gohr html_msgarea(); 361c29dc6e4SAndreas Gohr }else{ 3620e20480fSPhy print "ERROR(".$msgdata['lvl'].") ".$msgdata['msg']."\n"; 363c29dc6e4SAndreas Gohr } 36469266de5SDominik Eckelmann unset($GLOBALS['MSG']); 365c29dc6e4SAndreas Gohr } 366c29dc6e4SAndreas Gohr } 3670e20480fSPhy $evt->advise_after(); 3680e20480fSPhy unset($evt); 3690e20480fSPhy} 370f755f9abSChristopher Smith/** 371f755f9abSChristopher Smith * Determine whether the current user is allowed to view the message 372f755f9abSChristopher Smith * in the $msg data structure 373f755f9abSChristopher Smith * 374f755f9abSChristopher Smith * @param $msg array dokuwiki msg structure 375f755f9abSChristopher Smith * msg => string, the message 376f755f9abSChristopher Smith * lvl => int, level of the message (see msg() function) 377f755f9abSChristopher Smith * allow => int, flag used to determine who is allowed to see the message 378f755f9abSChristopher Smith * see MSG_* constants 3796164d900SAndreas Gohr * @return bool 380f755f9abSChristopher Smith */ 381f755f9abSChristopher Smithfunction info_msg_allowed($msg){ 382d3bae478SChristopher Smith global $INFO, $auth; 383d3bae478SChristopher Smith 384d3bae478SChristopher Smith // is the message public? - everyone and anyone can see it 385f755f9abSChristopher Smith if (empty($msg['allow']) || ($msg['allow'] == MSG_PUBLIC)) return true; 386d3bae478SChristopher Smith 387d3bae478SChristopher Smith // restricted msg, but no authentication 388d3bae478SChristopher Smith if (empty($auth)) return false; 389d3bae478SChristopher Smith 390f755f9abSChristopher Smith switch ($msg['allow']){ 391d3bae478SChristopher Smith case MSG_USERS_ONLY: 392d3bae478SChristopher Smith return !empty($INFO['userinfo']); 393d3bae478SChristopher Smith 394d3bae478SChristopher Smith case MSG_MANAGERS_ONLY: 395d3bae478SChristopher Smith return $INFO['ismanager']; 396d3bae478SChristopher Smith 397d3bae478SChristopher Smith case MSG_ADMINS_ONLY: 398d3bae478SChristopher Smith return $INFO['isadmin']; 399d3bae478SChristopher Smith 400d3bae478SChristopher Smith default: 40164159a61SAndreas Gohr trigger_error('invalid msg allow restriction. msg="'.$msg['msg'].'" allow='.$msg['allow'].'"', 40264159a61SAndreas Gohr E_USER_WARNING); 403d3bae478SChristopher Smith return $INFO['isadmin']; 404d3bae478SChristopher Smith } 405d3bae478SChristopher Smith 406d3bae478SChristopher Smith return false; 407d3bae478SChristopher Smith} 408d3bae478SChristopher Smith 409c29dc6e4SAndreas Gohr/** 410c29dc6e4SAndreas Gohr * print debug messages 411c29dc6e4SAndreas Gohr * 412c29dc6e4SAndreas Gohr * little function to print the content of a var 413c29dc6e4SAndreas Gohr * 414c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 415f50a239bSTakamura * 416f50a239bSTakamura * @param string $msg 417f50a239bSTakamura * @param bool $hidden 418c29dc6e4SAndreas Gohr */ 419c29dc6e4SAndreas Gohrfunction dbg($msg,$hidden=false){ 42013493794SAndreas Gohr if($hidden){ 42113493794SAndreas Gohr echo "<!--\n"; 422c29dc6e4SAndreas Gohr print_r($msg); 42313493794SAndreas Gohr echo "\n-->"; 42413493794SAndreas Gohr }else{ 42513493794SAndreas Gohr echo '<pre class="dbg">'; 42613493794SAndreas Gohr echo hsc(print_r($msg,true)); 42713493794SAndreas Gohr echo '</pre>'; 42813493794SAndreas Gohr } 429c29dc6e4SAndreas Gohr} 430c29dc6e4SAndreas Gohr 431c29dc6e4SAndreas Gohr/** 432c29dc6e4SAndreas Gohr * Print info to a log file 433c29dc6e4SAndreas Gohr * 434c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 435f50a239bSTakamura * 436f50a239bSTakamura * @param string $msg 437f50a239bSTakamura * @param string $header 438c29dc6e4SAndreas Gohr */ 4390699d739SAndreas Gohrfunction dbglog($msg,$header=''){ 440c29dc6e4SAndreas Gohr global $conf; 441585bf44eSChristopher Smith /* @var Input $INPUT */ 442585bf44eSChristopher Smith global $INPUT; 443585bf44eSChristopher Smith 4445bd930ffSMichael Hamann // The debug log isn't automatically cleaned thus only write it when 4455bd930ffSMichael Hamann // debugging has been enabled by the user. 4465bd930ffSMichael Hamann if($conf['allowdebug'] !== 1) return; 447c7408a63SAndreas Gohr if(is_object($msg) || is_array($msg)){ 448c7408a63SAndreas Gohr $msg = print_r($msg,true); 449c7408a63SAndreas Gohr } 450c7408a63SAndreas Gohr 4510699d739SAndreas Gohr if($header) $msg = "$header\n$msg"; 4520699d739SAndreas Gohr 453c29dc6e4SAndreas Gohr $file = $conf['cachedir'].'/debug.log'; 454c29dc6e4SAndreas Gohr $fh = fopen($file,'a'); 455c29dc6e4SAndreas Gohr if($fh){ 456585bf44eSChristopher Smith fwrite($fh,date('H:i:s ').$INPUT->server->str('REMOTE_ADDR').': '.$msg."\n"); 457c29dc6e4SAndreas Gohr fclose($fh); 458c29dc6e4SAndreas Gohr } 459c29dc6e4SAndreas Gohr} 460c29dc6e4SAndreas Gohr 461db09e31eSAndreas Gohr/** 4621419a485SAndreas Gohr * Log accesses to deprecated fucntions to the debug log 4631419a485SAndreas Gohr * 4641419a485SAndreas Gohr * @param string $alternative The function or method that should be used instead 46585331086SAndreas Gohr * @triggers INFO_DEPRECATION_LOG 4661419a485SAndreas Gohr */ 4671419a485SAndreas Gohrfunction dbg_deprecated($alternative = '') { 4680c5eb5e2SMichael Große \dokuwiki\Debug\DebugHelper::dbgDeprecatedFunction($alternative, 2); 46944455016SAndreas Gohr} 4701419a485SAndreas Gohr 4711419a485SAndreas Gohr/** 472db09e31eSAndreas Gohr * Print a reversed, prettyprinted backtrace 473db09e31eSAndreas Gohr * 474db09e31eSAndreas Gohr * @author Gary Owen <gary_owen@bigfoot.com> 475db09e31eSAndreas Gohr */ 476db09e31eSAndreas Gohrfunction dbg_backtrace(){ 477db09e31eSAndreas Gohr // Get backtrace 478db09e31eSAndreas Gohr $backtrace = debug_backtrace(); 479db09e31eSAndreas Gohr 480db09e31eSAndreas Gohr // Unset call to debug_print_backtrace 481db09e31eSAndreas Gohr array_shift($backtrace); 482db09e31eSAndreas Gohr 483db09e31eSAndreas Gohr // Iterate backtrace 484db09e31eSAndreas Gohr $calls = array(); 485db09e31eSAndreas Gohr $depth = count($backtrace) - 1; 486db09e31eSAndreas Gohr foreach ($backtrace as $i => $call) { 487db09e31eSAndreas Gohr $location = $call['file'] . ':' . $call['line']; 488db09e31eSAndreas Gohr $function = (isset($call['class'])) ? 489db09e31eSAndreas Gohr $call['class'] . $call['type'] . $call['function'] : $call['function']; 490db09e31eSAndreas Gohr 4918259f1aaSAndreas Gohr $params = array(); 492db09e31eSAndreas Gohr if (isset($call['args'])){ 4938259f1aaSAndreas Gohr foreach($call['args'] as $arg){ 4948259f1aaSAndreas Gohr if(is_object($arg)){ 4958259f1aaSAndreas Gohr $params[] = '[Object '.get_class($arg).']'; 4968259f1aaSAndreas Gohr }elseif(is_array($arg)){ 4978259f1aaSAndreas Gohr $params[] = '[Array]'; 4988259f1aaSAndreas Gohr }elseif(is_null($arg)){ 49959bc3b48SGerrit Uitslag $params[] = '[NULL]'; 5008259f1aaSAndreas Gohr }else{ 5018259f1aaSAndreas Gohr $params[] = (string) '"'.$arg.'"'; 502db09e31eSAndreas Gohr } 5038259f1aaSAndreas Gohr } 5048259f1aaSAndreas Gohr } 5058259f1aaSAndreas Gohr $params = implode(', ',$params); 506db09e31eSAndreas Gohr 5078259f1aaSAndreas Gohr $calls[$depth - $i] = sprintf('%s(%s) called at %s', 508db09e31eSAndreas Gohr $function, 509db09e31eSAndreas Gohr str_replace("\n", '\n', $params), 510db09e31eSAndreas Gohr $location); 511db09e31eSAndreas Gohr } 512db09e31eSAndreas Gohr ksort($calls); 513db09e31eSAndreas Gohr 514db09e31eSAndreas Gohr return implode("\n", $calls); 515db09e31eSAndreas Gohr} 516db09e31eSAndreas Gohr 51724297a69SAndreas Gohr/** 51824297a69SAndreas Gohr * Remove all data from an array where the key seems to point to sensitive data 51924297a69SAndreas Gohr * 52024297a69SAndreas Gohr * This is used to remove passwords, mail addresses and similar data from the 52124297a69SAndreas Gohr * debug output 52224297a69SAndreas Gohr * 52324297a69SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 524f50a239bSTakamura * 525f50a239bSTakamura * @param array $data 52624297a69SAndreas Gohr */ 52724297a69SAndreas Gohrfunction debug_guard(&$data){ 52824297a69SAndreas Gohr foreach($data as $key => $value){ 52924297a69SAndreas Gohr if(preg_match('/(notify|pass|auth|secret|ftp|userinfo|token|buid|mail|proxy)/i',$key)){ 53024297a69SAndreas Gohr $data[$key] = '***'; 53124297a69SAndreas Gohr continue; 53224297a69SAndreas Gohr } 53324297a69SAndreas Gohr if(is_array($value)) debug_guard($data[$key]); 53424297a69SAndreas Gohr } 53524297a69SAndreas Gohr} 536