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 */ 8fa8adffeSAndreas Gohrif(!defined('DOKU_INC')) die('meh.'); 93d7760aaSAndreas Gohrif(!defined('DOKU_MESSAGEURL')) define('DOKU_MESSAGEURL','http://update.dokuwiki.org/check/'); 10c29dc6e4SAndreas Gohr 11c29dc6e4SAndreas Gohr/** 12c29dc6e4SAndreas Gohr * Check for new messages from upstream 13c29dc6e4SAndreas Gohr * 14c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 15c29dc6e4SAndreas Gohr */ 16c29dc6e4SAndreas Gohrfunction checkUpdateMessages(){ 17c29dc6e4SAndreas Gohr global $conf; 18c29dc6e4SAndreas Gohr global $INFO; 19ef362bb8SAnika Henke global $updateVersion; 20c29dc6e4SAndreas Gohr if(!$conf['updatecheck']) return; 21f8cc712eSAndreas Gohr if($conf['useacl'] && !$INFO['ismanager']) return; 22c29dc6e4SAndreas Gohr 23c29dc6e4SAndreas Gohr $cf = $conf['cachedir'].'/messages.txt'; 24c29dc6e4SAndreas Gohr $lm = @filemtime($cf); 25c29dc6e4SAndreas Gohr 26c29dc6e4SAndreas Gohr // check if new messages needs to be fetched 278d3d7569SAnika Henke if($lm < time()-(60*60*24) || $lm < @filemtime(DOKU_INC.DOKU_SCRIPT)){ 2863d9b820SAndreas Gohr @touch($cf); 29d2d7bf0bSChristoph Schindler dbglog("checkUpdateMessages(): downloading messages.txt"); 30c29dc6e4SAndreas Gohr $http = new DokuHTTPClient(); 3163d9b820SAndreas Gohr $http->timeout = 12; 32ef362bb8SAnika Henke $data = $http->get(DOKU_MESSAGEURL.$updateVersion); 33*8f1efc43SAndreas Gohr if(substr(trim($data), -1) != '%') { 34*8f1efc43SAndreas Gohr // this doesn't look like one of our messages, maybe some WiFi login interferred 35*8f1efc43SAndreas Gohr $data = ''; 36*8f1efc43SAndreas Gohr }else { 37c29dc6e4SAndreas Gohr io_saveFile($cf,$data); 38*8f1efc43SAndreas Gohr } 39c29dc6e4SAndreas Gohr }else{ 40d2d7bf0bSChristoph Schindler dbglog("checkUpdateMessages(): messages.txt up to date"); 41c29dc6e4SAndreas Gohr $data = io_readFile($cf); 42c29dc6e4SAndreas Gohr } 43c29dc6e4SAndreas Gohr 44c29dc6e4SAndreas Gohr // show messages through the usual message mechanism 45c29dc6e4SAndreas Gohr $msgs = explode("\n%\n",$data); 46c29dc6e4SAndreas Gohr foreach($msgs as $msg){ 47c29dc6e4SAndreas Gohr if($msg) msg($msg,2); 48c29dc6e4SAndreas Gohr } 49c29dc6e4SAndreas Gohr} 50c29dc6e4SAndreas Gohr 51c29dc6e4SAndreas Gohr 52c29dc6e4SAndreas Gohr/** 5325c07f93SAnika Henke * Return DokuWiki's version (split up in date and type) 54c29dc6e4SAndreas Gohr * 55c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 56c29dc6e4SAndreas Gohr */ 5725c07f93SAnika Henkefunction getVersionData(){ 5825c07f93SAnika Henke $version = array(); 59c29dc6e4SAndreas Gohr //import version string 60e9208eb1SAndreas Gohr if(@file_exists(DOKU_INC.'VERSION')){ 61c29dc6e4SAndreas Gohr //official release 6225c07f93SAnika Henke $version['date'] = trim(io_readfile(DOKU_INC.'VERSION')); 6325c07f93SAnika Henke $version['type'] = 'Release'; 645cf31920SAndreas Gohr }elseif(is_dir(DOKU_INC.'.git')){ 655cf31920SAndreas Gohr $version['type'] = 'Git'; 6625c07f93SAnika Henke $version['date'] = 'unknown'; 67e570ed43SAndreas Gohr 685cf31920SAndreas Gohr $inventory = DOKU_INC.'.git/logs/HEAD'; 695cf31920SAndreas Gohr if(is_file($inventory)){ 70e570ed43SAndreas Gohr $sz = filesize($inventory); 715cf31920SAndreas Gohr $seek = max(0,$sz-2000); // read from back of the file 72e570ed43SAndreas Gohr $fh = fopen($inventory,'rb'); 73c29dc6e4SAndreas Gohr fseek($fh,$seek); 74c29dc6e4SAndreas Gohr $chunk = fread($fh,2000); 75c29dc6e4SAndreas Gohr fclose($fh); 765cf31920SAndreas Gohr $chunk = trim($chunk); 77b838050eSPiyush Mishra $chunk = @array_pop(explode("\n",$chunk)); //last log line 78b838050eSPiyush Mishra $chunk = @array_shift(explode("\t",$chunk)); //strip commit msg 795cf31920SAndreas Gohr $chunk = explode(" ",$chunk); 805cf31920SAndreas Gohr array_pop($chunk); //strip timezone 815cf31920SAndreas Gohr $date = date('Y-m-d',array_pop($chunk)); 825cf31920SAndreas Gohr if($date) $version['date'] = $date; 835cf31920SAndreas Gohr } 84c29dc6e4SAndreas Gohr }else{ 856a34de2dSAnika Henke global $updateVersion; 866a34de2dSAnika Henke $version['date'] = 'update version '.$updateVersion; 8725c07f93SAnika Henke $version['type'] = 'snapshot?'; 88c29dc6e4SAndreas Gohr } 895cf31920SAndreas Gohr return $version; 90c29dc6e4SAndreas Gohr} 91c29dc6e4SAndreas Gohr 92c29dc6e4SAndreas Gohr/** 9325c07f93SAnika Henke * Return DokuWiki's version (as a string) 9425c07f93SAnika Henke * 9525c07f93SAnika Henke * @author Anika Henke <anika@selfthinker.org> 9625c07f93SAnika Henke */ 9725c07f93SAnika Henkefunction getVersion(){ 9825c07f93SAnika Henke $version = getVersionData(); 9925c07f93SAnika Henke return $version['type'].' '.$version['date']; 10025c07f93SAnika Henke} 10125c07f93SAnika Henke 10225c07f93SAnika Henke/** 103c29dc6e4SAndreas Gohr * Run a few sanity checks 104c29dc6e4SAndreas Gohr * 105c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 106c29dc6e4SAndreas Gohr */ 107c29dc6e4SAndreas Gohrfunction check(){ 108c29dc6e4SAndreas Gohr global $conf; 109c29dc6e4SAndreas Gohr global $INFO; 110585bf44eSChristopher Smith /* @var Input $INPUT */ 111585bf44eSChristopher Smith global $INPUT; 112c29dc6e4SAndreas Gohr 1133f803e5eSGina Haeussge if ($INFO['isadmin'] || $INFO['ismanager']){ 114c29dc6e4SAndreas Gohr msg('DokuWiki version: '.getVersion(),1); 115c29dc6e4SAndreas Gohr 11621c9604eSAndreas Gohr if(version_compare(phpversion(),'5.2.0','<')){ 11721c9604eSAndreas Gohr msg('Your PHP version is too old ('.phpversion().' vs. 5.2.0+ needed)',-1); 118c29dc6e4SAndreas Gohr }else{ 119c29dc6e4SAndreas Gohr msg('PHP version '.phpversion(),1); 120c29dc6e4SAndreas Gohr } 12108d1a8dfSAndreas Gohr } else { 12208d1a8dfSAndreas Gohr if(version_compare(phpversion(),'5.2.0','<')){ 12308d1a8dfSAndreas Gohr msg('Your PHP version is too old',-1); 12408d1a8dfSAndreas Gohr } 12508d1a8dfSAndreas Gohr } 126c29dc6e4SAndreas Gohr 12773038c47SAndreas Gohr $mem = (int) php_to_byte(ini_get('memory_limit')); 12873038c47SAndreas Gohr if($mem){ 12973038c47SAndreas Gohr if($mem < 16777216){ 13073038c47SAndreas Gohr msg('PHP is limited to less than 16MB RAM ('.$mem.' bytes). Increase memory_limit in php.ini',-1); 13173038c47SAndreas Gohr }elseif($mem < 20971520){ 13273038c47SAndreas Gohr 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); 13373038c47SAndreas Gohr }elseif($mem < 33554432){ 13473038c47SAndreas Gohr 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); 13573038c47SAndreas Gohr }else{ 13673038c47SAndreas Gohr msg('More than 32MB RAM ('.$mem.' bytes) available.',1); 13773038c47SAndreas Gohr } 13873038c47SAndreas Gohr } 13973038c47SAndreas Gohr 140c29dc6e4SAndreas Gohr if(is_writable($conf['changelog'])){ 141c29dc6e4SAndreas Gohr msg('Changelog is writable',1); 142c29dc6e4SAndreas Gohr }else{ 143c29dc6e4SAndreas Gohr if (@file_exists($conf['changelog'])) { 144c29dc6e4SAndreas Gohr msg('Changelog is not writable',-1); 145c29dc6e4SAndreas Gohr } 146c29dc6e4SAndreas Gohr } 147c29dc6e4SAndreas Gohr 148c29dc6e4SAndreas Gohr if (isset($conf['changelog_old']) && @file_exists($conf['changelog_old'])) { 1492cdbda06SAnika Henke msg('Old changelog exists', 0); 150c29dc6e4SAndreas Gohr } 151c29dc6e4SAndreas Gohr 152c29dc6e4SAndreas Gohr if (@file_exists($conf['changelog'].'_failed')) { 1532cdbda06SAnika Henke msg('Importing old changelog failed', -1); 154c29dc6e4SAndreas Gohr } else if (@file_exists($conf['changelog'].'_importing')) { 155c29dc6e4SAndreas Gohr msg('Importing old changelog now.', 0); 156c29dc6e4SAndreas Gohr } else if (@file_exists($conf['changelog'].'_import_ok')) { 1572cdbda06SAnika Henke msg('Old changelog imported', 1); 158c29dc6e4SAndreas Gohr if (!plugin_isdisabled('importoldchangelog')) { 1592cdbda06SAnika Henke msg('Importoldchangelog plugin not disabled after import', -1); 160c29dc6e4SAndreas Gohr } 161c29dc6e4SAndreas Gohr } 162c29dc6e4SAndreas Gohr 1634c7ecf15SGuy Brand if(is_writable(DOKU_CONF)){ 1644c7ecf15SGuy Brand msg('conf directory is writable',1); 1654c7ecf15SGuy Brand }else{ 1664c7ecf15SGuy Brand msg('conf directory is not writable',-1); 1674c7ecf15SGuy Brand } 1684c7ecf15SGuy Brand 1690d487d8fSAndreas Gohr if($conf['authtype'] == 'plain'){ 170defb7d57SAnika Henke global $config_cascade; 171defb7d57SAnika Henke if(is_writable($config_cascade['plainauth.users']['default'])){ 172c29dc6e4SAndreas Gohr msg('conf/users.auth.php is writable',1); 173c29dc6e4SAndreas Gohr }else{ 174c29dc6e4SAndreas Gohr msg('conf/users.auth.php is not writable',0); 175c29dc6e4SAndreas Gohr } 1760d487d8fSAndreas Gohr } 177c29dc6e4SAndreas Gohr 178c29dc6e4SAndreas Gohr if(function_exists('mb_strpos')){ 179c29dc6e4SAndreas Gohr if(defined('UTF8_NOMBSTRING')){ 180c29dc6e4SAndreas Gohr msg('mb_string extension is available but will not be used',0); 181c29dc6e4SAndreas Gohr }else{ 182c29dc6e4SAndreas Gohr msg('mb_string extension is available and will be used',1); 1834222b898SAndreas Gohr if(ini_get('mbstring.func_overload') != 0){ 1844222b898SAndreas Gohr msg('mb_string function overloading is enabled, this will cause problems and should be disabled',-1); 1854222b898SAndreas Gohr } 186c29dc6e4SAndreas Gohr } 187c29dc6e4SAndreas Gohr }else{ 188c29dc6e4SAndreas Gohr msg('mb_string extension not available - PHP only replacements will be used',0); 189c29dc6e4SAndreas Gohr } 190c29dc6e4SAndreas Gohr 1913161005dSAndreas Gohr if (!UTF8_PREGSUPPORT) { 192a731ed1dSAndreas Gohr msg('PHP is missing UTF-8 support in Perl-Compatible Regular Expressions (PCRE)', -1); 193a731ed1dSAndreas Gohr } 1943161005dSAndreas Gohr if (!UTF8_PROPERTYSUPPORT) { 195a731ed1dSAndreas Gohr msg('PHP is missing Unicode properties support in Perl-Compatible Regular Expressions (PCRE)', -1); 196a731ed1dSAndreas Gohr } 197a731ed1dSAndreas Gohr 198e5ab313fSAndreas Gohr $loc = setlocale(LC_ALL, 0); 199e5ab313fSAndreas Gohr if(!$loc){ 200e5ab313fSAndreas Gohr msg('No valid locale is set for your PHP setup. You should fix this',-1); 201e5ab313fSAndreas Gohr }elseif(stripos($loc,'utf') === false){ 202e5ab313fSAndreas Gohr msg('Your locale <code>'.hsc($loc).'</code> seems not to be a UTF-8 locale, you should fix this if you encounter problems.',0); 203e5ab313fSAndreas Gohr }else{ 204e5ab313fSAndreas Gohr msg('Valid locale '.hsc($loc).' found.', 1); 205e5ab313fSAndreas Gohr } 206e5ab313fSAndreas Gohr 207c29dc6e4SAndreas Gohr if($conf['allowdebug']){ 208c29dc6e4SAndreas Gohr msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1); 209c29dc6e4SAndreas Gohr }else{ 210c29dc6e4SAndreas Gohr msg('Debugging support is disabled',1); 211c29dc6e4SAndreas Gohr } 212c29dc6e4SAndreas Gohr 2133d3c095dSMike Frysinger if($INFO['userinfo']['name']){ 214585bf44eSChristopher Smith msg('You are currently logged in as '.$INPUT->server->str('REMOTE_USER').' ('.$INFO['userinfo']['name'].')',0); 215c1791678SAndreas Gohr msg('You are part of the groups '.join($INFO['userinfo']['grps'],', '),0); 2163d3c095dSMike Frysinger }else{ 2173d3c095dSMike Frysinger msg('You are currently not logged in',0); 2183d3c095dSMike Frysinger } 2193d3c095dSMike Frysinger 220c29dc6e4SAndreas Gohr msg('Your current permission for this page is '.$INFO['perm'],0); 221c29dc6e4SAndreas Gohr 222c29dc6e4SAndreas Gohr if(is_writable($INFO['filepath'])){ 223c29dc6e4SAndreas Gohr msg('The current page is writable by the webserver',0); 224c29dc6e4SAndreas Gohr }else{ 225c29dc6e4SAndreas Gohr msg('The current page is not writable by the webserver',0); 226c29dc6e4SAndreas Gohr } 227c29dc6e4SAndreas Gohr 228c29dc6e4SAndreas Gohr if($INFO['writable']){ 229c29dc6e4SAndreas Gohr msg('The current page is writable by you',0); 230c29dc6e4SAndreas Gohr }else{ 2312cdbda06SAnika Henke msg('The current page is not writable by you',0); 232c29dc6e4SAndreas Gohr } 2333b1dfc83SAndreas Gohr 23426f7dbf5SMichael Hamann // Check for corrupted search index 23526f7dbf5SMichael Hamann $lengths = idx_listIndexLengths(); 23626f7dbf5SMichael Hamann $index_corrupted = false; 23726f7dbf5SMichael Hamann foreach ($lengths as $length) { 23826f7dbf5SMichael Hamann if (count(idx_getIndex('w', $length)) != count(idx_getIndex('i', $length))) { 23926f7dbf5SMichael Hamann $index_corrupted = true; 24026f7dbf5SMichael Hamann break; 24126f7dbf5SMichael Hamann } 24226f7dbf5SMichael Hamann } 24326f7dbf5SMichael Hamann 24426f7dbf5SMichael Hamann foreach (idx_getIndex('metadata', '') as $index) { 24526f7dbf5SMichael Hamann if (count(idx_getIndex($index.'_w', '')) != count(idx_getIndex($index.'_i', ''))) { 24626f7dbf5SMichael Hamann $index_corrupted = true; 24726f7dbf5SMichael Hamann break; 24826f7dbf5SMichael Hamann } 24926f7dbf5SMichael Hamann } 25026f7dbf5SMichael Hamann 25126f7dbf5SMichael Hamann if ($index_corrupted) 2523d94d9edSMichael Hamann msg('The search index is corrupted. It might produce wrong results and most 2533d94d9edSMichael Hamann probably needs to be rebuilt. See 25426f7dbf5SMichael Hamann <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a> 2553d94d9edSMichael Hamann for ways to rebuild the search index.', -1); 25626f7dbf5SMichael Hamann elseif (!empty($lengths)) 2573d94d9edSMichael Hamann msg('The search index seems to be working', 1); 25826f7dbf5SMichael Hamann else 2593d94d9edSMichael Hamann msg('The search index is empty. See 26026f7dbf5SMichael Hamann <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a> 2613d94d9edSMichael Hamann for help on how to fix the search index. If the default indexer 2623d94d9edSMichael Hamann isn\'t used or the wiki is actually empty this is normal.'); 263c29dc6e4SAndreas Gohr} 264c29dc6e4SAndreas Gohr 265c29dc6e4SAndreas Gohr/** 266c29dc6e4SAndreas Gohr * print a message 267c29dc6e4SAndreas Gohr * 268c29dc6e4SAndreas Gohr * If HTTP headers were not sent yet the message is added 269c29dc6e4SAndreas Gohr * to the global message array else it's printed directly 270c29dc6e4SAndreas Gohr * using html_msgarea() 271c29dc6e4SAndreas Gohr * 272c29dc6e4SAndreas Gohr * 273c29dc6e4SAndreas Gohr * Levels can be: 274c29dc6e4SAndreas Gohr * 275c29dc6e4SAndreas Gohr * -1 error 276c29dc6e4SAndreas Gohr * 0 info 277c29dc6e4SAndreas Gohr * 1 success 278c29dc6e4SAndreas Gohr * 279c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 280c29dc6e4SAndreas Gohr * @see html_msgarea 281c29dc6e4SAndreas Gohr */ 282d3bae478SChristopher Smith 283d3bae478SChristopher Smithdefine('MSG_PUBLIC', 0); 284d3bae478SChristopher Smithdefine('MSG_USERS_ONLY', 1); 285d3bae478SChristopher Smithdefine('MSG_MANAGERS_ONLY',2); 286d3bae478SChristopher Smithdefine('MSG_ADMINS_ONLY',4); 287d3bae478SChristopher Smith 2886164d900SAndreas Gohr/** 2896164d900SAndreas Gohr * Display a message to the user 2906164d900SAndreas Gohr * 2916164d900SAndreas Gohr * @param string $message 2926164d900SAndreas Gohr * @param int $lvl -1 = error, 0 = info, 1 = success, 2 = notify 2936164d900SAndreas Gohr * @param string $line line number 2946164d900SAndreas Gohr * @param string $file file number 2956164d900SAndreas Gohr * @param int $allow who's allowed to see the message, see MSG_* constants 2966164d900SAndreas Gohr */ 297f755f9abSChristopher Smithfunction msg($message,$lvl=0,$line='',$file='',$allow=MSG_PUBLIC){ 298cc58224cSMichael Hamann global $MSG, $MSG_shown; 299c29dc6e4SAndreas Gohr $errors[-1] = 'error'; 300c29dc6e4SAndreas Gohr $errors[0] = 'info'; 301c29dc6e4SAndreas Gohr $errors[1] = 'success'; 302c29dc6e4SAndreas Gohr $errors[2] = 'notify'; 303c29dc6e4SAndreas Gohr 3043009a773SAndreas Gohr if($line || $file) $message.=' ['.utf8_basename($file).':'.$line.']'; 305c29dc6e4SAndreas Gohr 306c29dc6e4SAndreas Gohr if(!isset($MSG)) $MSG = array(); 307f755f9abSChristopher Smith $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message, 'allow' => $allow); 308cc58224cSMichael Hamann if(isset($MSG_shown) || headers_sent()){ 309c29dc6e4SAndreas Gohr if(function_exists('html_msgarea')){ 310c29dc6e4SAndreas Gohr html_msgarea(); 311c29dc6e4SAndreas Gohr }else{ 312c29dc6e4SAndreas Gohr print "ERROR($lvl) $message"; 313c29dc6e4SAndreas Gohr } 31469266de5SDominik Eckelmann unset($GLOBALS['MSG']); 315c29dc6e4SAndreas Gohr } 316c29dc6e4SAndreas Gohr} 317f755f9abSChristopher Smith/** 318f755f9abSChristopher Smith * Determine whether the current user is allowed to view the message 319f755f9abSChristopher Smith * in the $msg data structure 320f755f9abSChristopher Smith * 321f755f9abSChristopher Smith * @param $msg array dokuwiki msg structure 322f755f9abSChristopher Smith * msg => string, the message 323f755f9abSChristopher Smith * lvl => int, level of the message (see msg() function) 324f755f9abSChristopher Smith * allow => int, flag used to determine who is allowed to see the message 325f755f9abSChristopher Smith * see MSG_* constants 3266164d900SAndreas Gohr * @return bool 327f755f9abSChristopher Smith */ 328f755f9abSChristopher Smithfunction info_msg_allowed($msg){ 329d3bae478SChristopher Smith global $INFO, $auth; 330d3bae478SChristopher Smith 331d3bae478SChristopher Smith // is the message public? - everyone and anyone can see it 332f755f9abSChristopher Smith if (empty($msg['allow']) || ($msg['allow'] == MSG_PUBLIC)) return true; 333d3bae478SChristopher Smith 334d3bae478SChristopher Smith // restricted msg, but no authentication 335d3bae478SChristopher Smith if (empty($auth)) return false; 336d3bae478SChristopher Smith 337f755f9abSChristopher Smith switch ($msg['allow']){ 338d3bae478SChristopher Smith case MSG_USERS_ONLY: 339d3bae478SChristopher Smith return !empty($INFO['userinfo']); 340d3bae478SChristopher Smith 341d3bae478SChristopher Smith case MSG_MANAGERS_ONLY: 342d3bae478SChristopher Smith return $INFO['ismanager']; 343d3bae478SChristopher Smith 344d3bae478SChristopher Smith case MSG_ADMINS_ONLY: 345d3bae478SChristopher Smith return $INFO['isadmin']; 346d3bae478SChristopher Smith 347d3bae478SChristopher Smith default: 348f755f9abSChristopher Smith trigger_error('invalid msg allow restriction. msg="'.$msg['msg'].'" allow='.$msg['allow'].'"', E_USER_WARNING); 349d3bae478SChristopher Smith return $INFO['isadmin']; 350d3bae478SChristopher Smith } 351d3bae478SChristopher Smith 352d3bae478SChristopher Smith return false; 353d3bae478SChristopher Smith} 354d3bae478SChristopher Smith 355c29dc6e4SAndreas Gohr/** 356c29dc6e4SAndreas Gohr * print debug messages 357c29dc6e4SAndreas Gohr * 358c29dc6e4SAndreas Gohr * little function to print the content of a var 359c29dc6e4SAndreas Gohr * 360c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 361c29dc6e4SAndreas Gohr */ 362c29dc6e4SAndreas Gohrfunction dbg($msg,$hidden=false){ 36313493794SAndreas Gohr if($hidden){ 36413493794SAndreas Gohr echo "<!--\n"; 365c29dc6e4SAndreas Gohr print_r($msg); 36613493794SAndreas Gohr echo "\n-->"; 36713493794SAndreas Gohr }else{ 36813493794SAndreas Gohr echo '<pre class="dbg">'; 36913493794SAndreas Gohr echo hsc(print_r($msg,true)); 37013493794SAndreas Gohr echo '</pre>'; 37113493794SAndreas Gohr } 372c29dc6e4SAndreas Gohr} 373c29dc6e4SAndreas Gohr 374c29dc6e4SAndreas Gohr/** 375c29dc6e4SAndreas Gohr * Print info to a log file 376c29dc6e4SAndreas Gohr * 377c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 378c29dc6e4SAndreas Gohr */ 3790699d739SAndreas Gohrfunction dbglog($msg,$header=''){ 380c29dc6e4SAndreas Gohr global $conf; 381585bf44eSChristopher Smith /* @var Input $INPUT */ 382585bf44eSChristopher Smith global $INPUT; 383585bf44eSChristopher Smith 3845bd930ffSMichael Hamann // The debug log isn't automatically cleaned thus only write it when 3855bd930ffSMichael Hamann // debugging has been enabled by the user. 3865bd930ffSMichael Hamann if($conf['allowdebug'] !== 1) return; 387c7408a63SAndreas Gohr if(is_object($msg) || is_array($msg)){ 388c7408a63SAndreas Gohr $msg = print_r($msg,true); 389c7408a63SAndreas Gohr } 390c7408a63SAndreas Gohr 3910699d739SAndreas Gohr if($header) $msg = "$header\n$msg"; 3920699d739SAndreas Gohr 393c29dc6e4SAndreas Gohr $file = $conf['cachedir'].'/debug.log'; 394c29dc6e4SAndreas Gohr $fh = fopen($file,'a'); 395c29dc6e4SAndreas Gohr if($fh){ 396585bf44eSChristopher Smith fwrite($fh,date('H:i:s ').$INPUT->server->str('REMOTE_ADDR').': '.$msg."\n"); 397c29dc6e4SAndreas Gohr fclose($fh); 398c29dc6e4SAndreas Gohr } 399c29dc6e4SAndreas Gohr} 400c29dc6e4SAndreas Gohr 401db09e31eSAndreas Gohr/** 4021419a485SAndreas Gohr * Log accesses to deprecated fucntions to the debug log 4031419a485SAndreas Gohr * 4041419a485SAndreas Gohr * @param string $alternative The function or method that should be used instead 4051419a485SAndreas Gohr */ 4061419a485SAndreas Gohrfunction dbg_deprecated($alternative = '') { 4071419a485SAndreas Gohr global $conf; 4081419a485SAndreas Gohr if(!$conf['allowdebug']) return; 4091419a485SAndreas Gohr 4101419a485SAndreas Gohr $backtrace = debug_backtrace(); 4111419a485SAndreas Gohr array_shift($backtrace); 4121419a485SAndreas Gohr $self = array_shift($backtrace); 4131419a485SAndreas Gohr $call = array_shift($backtrace); 4141419a485SAndreas Gohr 4151419a485SAndreas Gohr $called = trim($self['class'].'::'.$self['function'].'()', ':'); 4161419a485SAndreas Gohr $caller = trim($call['class'].'::'.$call['function'].'()', ':'); 4171419a485SAndreas Gohr 4181419a485SAndreas Gohr $msg = $called.' is deprecated. It was called from '; 4191419a485SAndreas Gohr $msg .= $caller.' in '.$call['file'].':'.$call['line']; 4201419a485SAndreas Gohr if($alternative) { 4211419a485SAndreas Gohr $msg .= ' '.$alternative.' should be used instead!'; 4221419a485SAndreas Gohr } 4231419a485SAndreas Gohr 4241419a485SAndreas Gohr dbglog($msg); 4251419a485SAndreas Gohr} 4261419a485SAndreas Gohr 4271419a485SAndreas Gohr/** 428db09e31eSAndreas Gohr * Print a reversed, prettyprinted backtrace 429db09e31eSAndreas Gohr * 430db09e31eSAndreas Gohr * @author Gary Owen <gary_owen@bigfoot.com> 431db09e31eSAndreas Gohr */ 432db09e31eSAndreas Gohrfunction dbg_backtrace(){ 433db09e31eSAndreas Gohr // Get backtrace 434db09e31eSAndreas Gohr $backtrace = debug_backtrace(); 435db09e31eSAndreas Gohr 436db09e31eSAndreas Gohr // Unset call to debug_print_backtrace 437db09e31eSAndreas Gohr array_shift($backtrace); 438db09e31eSAndreas Gohr 439db09e31eSAndreas Gohr // Iterate backtrace 440db09e31eSAndreas Gohr $calls = array(); 441db09e31eSAndreas Gohr $depth = count($backtrace) - 1; 442db09e31eSAndreas Gohr foreach ($backtrace as $i => $call) { 443db09e31eSAndreas Gohr $location = $call['file'] . ':' . $call['line']; 444db09e31eSAndreas Gohr $function = (isset($call['class'])) ? 445db09e31eSAndreas Gohr $call['class'] . $call['type'] . $call['function'] : $call['function']; 446db09e31eSAndreas Gohr 4478259f1aaSAndreas Gohr $params = array(); 448db09e31eSAndreas Gohr if (isset($call['args'])){ 4498259f1aaSAndreas Gohr foreach($call['args'] as $arg){ 4508259f1aaSAndreas Gohr if(is_object($arg)){ 4518259f1aaSAndreas Gohr $params[] = '[Object '.get_class($arg).']'; 4528259f1aaSAndreas Gohr }elseif(is_array($arg)){ 4538259f1aaSAndreas Gohr $params[] = '[Array]'; 4548259f1aaSAndreas Gohr }elseif(is_null($arg)){ 4558259f1aaSAndreas Gohr $param[] = '[NULL]'; 4568259f1aaSAndreas Gohr }else{ 4578259f1aaSAndreas Gohr $params[] = (string) '"'.$arg.'"'; 458db09e31eSAndreas Gohr } 4598259f1aaSAndreas Gohr } 4608259f1aaSAndreas Gohr } 4618259f1aaSAndreas Gohr $params = implode(', ',$params); 462db09e31eSAndreas Gohr 4638259f1aaSAndreas Gohr $calls[$depth - $i] = sprintf('%s(%s) called at %s', 464db09e31eSAndreas Gohr $function, 465db09e31eSAndreas Gohr str_replace("\n", '\n', $params), 466db09e31eSAndreas Gohr $location); 467db09e31eSAndreas Gohr } 468db09e31eSAndreas Gohr ksort($calls); 469db09e31eSAndreas Gohr 470db09e31eSAndreas Gohr return implode("\n", $calls); 471db09e31eSAndreas Gohr} 472db09e31eSAndreas Gohr 47324297a69SAndreas Gohr/** 47424297a69SAndreas Gohr * Remove all data from an array where the key seems to point to sensitive data 47524297a69SAndreas Gohr * 47624297a69SAndreas Gohr * This is used to remove passwords, mail addresses and similar data from the 47724297a69SAndreas Gohr * debug output 47824297a69SAndreas Gohr * 47924297a69SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 48024297a69SAndreas Gohr */ 48124297a69SAndreas Gohrfunction debug_guard(&$data){ 48224297a69SAndreas Gohr foreach($data as $key => $value){ 48324297a69SAndreas Gohr if(preg_match('/(notify|pass|auth|secret|ftp|userinfo|token|buid|mail|proxy)/i',$key)){ 48424297a69SAndreas Gohr $data[$key] = '***'; 48524297a69SAndreas Gohr continue; 48624297a69SAndreas Gohr } 48724297a69SAndreas Gohr if(is_array($value)) debug_guard($data[$key]); 48824297a69SAndreas Gohr } 48924297a69SAndreas Gohr} 490