PageRenderTime 58ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/debugger.php

https://bitbucket.org/kristovaher/wave-framework
PHP | 213 lines | 131 code | 34 blank | 48 comment | 25 complexity | 2b856d77148be88ccb33a8c0424e3e15 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. * Wave Framework <http://www.waveframework.com>
  4. * Errors and Warnings Debugger
  5. *
  6. * This is a script that collects error messages that have been written to filesystem. It also provides method
  7. * to easily delete the error log about a specific error message, once it is considered 'fixed'. This script
  8. * should be checked every now and then to test and make sure that there are no outstanding problems in the system.
  9. *
  10. * @package Tools
  11. * @author Kristo Vaher <kristo@waher.net>
  12. * @copyright Copyright (c) 2012, Kristo Vaher
  13. * @license GNU Lesser General Public License Version 3
  14. * @tutorial /doc/pages/guide_tools.htm
  15. * @since 1.0.0
  16. * @version 3.6.0
  17. */
  18. // This initializes tools and authentication
  19. require('.'.DIRECTORY_SEPARATOR.'tools_autoload.php');
  20. // Log is printed out in plain text format
  21. header('Content-Type: text/html;charset=utf-8');
  22. // Debugger actions are based on whether signature and error are set
  23. if(isset($_GET['signature']) && isset($_GET['error'])){
  24. // Replacing potentially harmful characters
  25. $signature=preg_replace('[^a-zA-Z0-9]','',$_GET['signature']);
  26. $error=preg_replace('[^a-zA-Z0-9]','',$_GET['error']);
  27. $folder='..'.DIRECTORY_SEPARATOR.'filesystem'.DIRECTORY_SEPARATOR.'errors'.DIRECTORY_SEPARATOR.$signature.DIRECTORY_SEPARATOR;
  28. // If the entry is marked for removal
  29. if(isset($_GET['done'])){
  30. // Making sure that the file exists
  31. if(file_exists($folder.$error.'.tmp')){
  32. unlink($folder.$error.'.tmp');
  33. }
  34. // Redirecting to signature itself, this finds the next error
  35. header('Location: debugger.php?signature='.$signature);
  36. die();
  37. } elseif(isset($_GET['alldone'])){
  38. // Making sure that the folder exists
  39. if(is_dir($folder)){
  40. // Cleaning all the files in the folder
  41. dirCleaner($folder);
  42. // Removing the directory itself
  43. rmdir($folder);
  44. }
  45. // Redirecting to Debugger main menu
  46. header('Location: debugger.php');
  47. die();
  48. }
  49. } elseif(isset($_GET['signature'])){
  50. // Replacing potentially harmful characters
  51. $signature=preg_replace('[^a-zA-Z0-9]','',$_GET['signature']);
  52. // This is the folder that should store the error messages
  53. $folder='..'.DIRECTORY_SEPARATOR.'filesystem'.DIRECTORY_SEPARATOR.'errors'.DIRECTORY_SEPARATOR.$signature.DIRECTORY_SEPARATOR;
  54. // Making sue that the file exists and that there is more than one file in the folder
  55. if(file_exists($folder.'signature.tmp') && fileCount($folder)>1){
  56. // Finding the first non signature file in the folder
  57. $filenames=fileIndex($folder,'filenames');
  58. foreach($filenames as $file){
  59. if($file!='signature.tmp'){
  60. header('Location: debugger.php?signature='.$signature.'&error='.str_replace('.tmp','',$filenames[0]));
  61. die();
  62. }
  63. }
  64. } else {
  65. // Otherwise redirect to main Debugger page
  66. header('Location: debugger.php');
  67. die();
  68. }
  69. } else {
  70. // This array stores all the error developer signatures
  71. $signatures=array();
  72. // This is the list of all errors based on signatures
  73. $folders=fileIndex('..'.DIRECTORY_SEPARATOR.'filesystem'.DIRECTORY_SEPARATOR.'errors'.DIRECTORY_SEPARATOR,'folders');
  74. // Looping over each folder that includes errors
  75. foreach($folders as $f){
  76. // Making sure that this is a proper file with errors
  77. if(file_exists($f.'signature.tmp')){
  78. $fileCount=fileCount($f);
  79. if($fileCount==1){
  80. // Removing the error signature and folder, since it is empty
  81. unlink($f.'signature.tmp');
  82. rmdir($f);
  83. } else {
  84. $signatures[$f]=file_get_contents($f.'signature.tmp');
  85. }
  86. }
  87. }
  88. }
  89. ?>
  90. <!DOCTYPE html>
  91. <html lang="en">
  92. <head>
  93. <title>Debugger</title>
  94. <meta charset="utf-8">
  95. <meta name="viewport" content="width=device-width"/>
  96. <link type="text/css" href="style.css" rel="stylesheet" media="all"/>
  97. <link rel="icon" href="../favicon.ico" type="image/x-icon"/>
  98. <link rel="icon" href="../favicon.ico" type="image/vnd.microsoft.icon"/>
  99. <meta content="noindex,nocache,nofollow,noarchive,noimageindex,nosnippet" name="robots"/>
  100. <meta http-equiv="cache-control" content="no-cache"/>
  101. <meta http-equiv="pragma" content="no-cache"/>
  102. <meta http-equiv="expires" content="0"/>
  103. </head>
  104. <body>
  105. <?php
  106. // Pops up an alert about default password
  107. passwordNotification($config['http-authentication-password']);
  108. // Header
  109. echo '<h1>Debugger</h1>';
  110. echo '<h4 class="highlight">';
  111. foreach($softwareVersions as $software=>$version){
  112. // Adding version numbers
  113. echo '<b>'.$software.'</b> ('.$version.') ';
  114. }
  115. echo '</h4>';
  116. if(isset($_GET['signature']) && isset($_GET['error'])){
  117. echo '<h2>Logged Error</h2>';
  118. echo '<h5 onclick="if(confirm(\'Are you sure? This deletes all error log entries!\')){ document.location.href=document.location.href+\'&alldone\'; }" class="red bold" style="cursor:pointer;float:right;">or remove all error entries with this signature</h5>';
  119. echo '<h3 onclick="if(confirm(\'Are you sure?\')){ document.location.href=document.location.href+\'&done\'; }" class="red bold" style="cursor:pointer;width:400px;">Click to remove this entry</h3>';
  120. // Getting error file size
  121. $filesize=filesize('..'.DIRECTORY_SEPARATOR.'filesystem'.DIRECTORY_SEPARATOR.'errors'.DIRECTORY_SEPARATOR.$error);
  122. // Attempting to get memory limit
  123. $memory=ini_get('memory_limit');
  124. if($memory){
  125. $memory=iniSettingToBytes($memory);
  126. // Getting the amount of bytes currently allocated
  127. $memory=$memory-memory_get_usage();
  128. // available memory is 'about half' of the actual memory, just in case the array creation takes a lot of memory
  129. $memory=intval($memory/2);
  130. }
  131. // Error is only shown if memory limit could not be gotten or is less than the file size
  132. if(!$memory || $filesize<$memory){
  133. $errorData=file_get_contents('..'.DIRECTORY_SEPARATOR.'filesystem'.DIRECTORY_SEPARATOR.'errors'.DIRECTORY_SEPARATOR.$signature.DIRECTORY_SEPARATOR.$error.'.tmp');
  134. $errorData=explode("\n",$errorData);
  135. foreach($errorData as $nr=>$d){
  136. if(trim($d)!=''){
  137. $d=json_decode($d,true);
  138. // Last element in error log is used as preview of the full error
  139. $time=array_shift($d);
  140. $summary=array_pop($d);
  141. echo '<div class="border block">';
  142. echo '<b>'.$time.'</b>';
  143. echo '<pre>';
  144. print_r($summary);
  145. echo '</pre>';
  146. echo '<span class="bold" style="cursor:pointer;" onclick="if(document.getElementById(\'error'.$nr.'\').style.display==\'\'){document.getElementById(\'error'.$nr.'\').style.display=\'none\';} else {document.getElementById(\'error'.$nr.'\').style.display=\'\';}">MORE DETAILS</span>';
  147. echo '<div id="error'.$nr.'" style="display:none;">';
  148. echo '<pre>';
  149. print_r($d);
  150. echo '</pre>';
  151. echo '</div>';
  152. echo '</div>';
  153. }
  154. }
  155. echo '<a href="debugger.php">Back to Debugger menu</a>';
  156. } else {
  157. echo '<p class="bold">This error log is too large for PHP to handle. Are you using \'trace-errors\' setting in your configuration file? If so, then it is recommended to turn that off. This will generate smaller error log files.</p>';
  158. }
  159. } else {
  160. echo '<h3>Available Signatures</h3>';
  161. echo '<h4>Signature is a pair of IP address and user agent string that generated the error. This helps you keep different log entries separate from one another based on client. Click on signature to see relevant error messages.</h4>';
  162. // If there are error signatures found
  163. if(!empty($signatures)){
  164. foreach($signatures as $folder=>$signature){
  165. if($signature==$_SERVER['REMOTE_ADDR'].' '.$_SERVER['HTTP_USER_AGENT']){
  166. echo '<a href="debugger.php?signature='.md5($signature).'"><span class="red bold">'.($fileCount-1).'</span> - '.$signature.' <span class="orange">(this matches your signature)</span></a><br/>';
  167. } else {
  168. echo '<a href="debugger.php?signature='.md5($signature).'"><span class="red bold">'.($fileCount-1).'</span> - '.$signature.'</a><br/>';
  169. }
  170. }
  171. } else {
  172. echo '<p class="bold">There are no logged errors</p>';
  173. }
  174. }
  175. // Footer
  176. echo '<p class="footer small bold">Generated at '.date('d.m.Y h:i').' GMT '.date('P').' for '.$_SERVER['HTTP_HOST'].'</p>';
  177. ?>
  178. </body>
  179. </html>