PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/debugger.php

https://bitbucket.org/hanigamal/my-wave-framework
PHP | 152 lines | 101 code | 13 blank | 38 comment | 13 complexity | caa2c9b3579ada89787e5cbc65df5d29 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.2.2
  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. // Action is based on GET values
  23. if(empty($_GET)){
  24. // Making sure that the default .empty file is not listed in errors directory
  25. if(file_exists('..'.DIRECTORY_SEPARATOR.'filesystem'.DIRECTORY_SEPARATOR.'errors'.DIRECTORY_SEPARATOR.'.empty')){
  26. unlink('..'.DIRECTORY_SEPARATOR.'filesystem'.DIRECTORY_SEPARATOR.'errors'.DIRECTORY_SEPARATOR.'.empty');
  27. }
  28. $errorLogs=scandir('..'.DIRECTORY_SEPARATOR.'filesystem'.DIRECTORY_SEPARATOR.'errors'.DIRECTORY_SEPARATOR);
  29. // If it found error messages of any kind
  30. foreach($errorLogs as $error){
  31. if($error!='.empty' && is_file('..'.DIRECTORY_SEPARATOR.'filesystem'.DIRECTORY_SEPARATOR.'errors'.DIRECTORY_SEPARATOR.$error)){
  32. header('Location: debugger.php?error='.$error);
  33. die();
  34. }
  35. }
  36. } elseif(isset($_GET['error'],$_GET['alldone'])){
  37. $errorLogs=scandir('..'.DIRECTORY_SEPARATOR.'filesystem'.DIRECTORY_SEPARATOR.'errors'.DIRECTORY_SEPARATOR);
  38. // If it found error messages of any kind
  39. foreach($errorLogs as $error){
  40. if(is_file('..'.DIRECTORY_SEPARATOR.'filesystem'.DIRECTORY_SEPARATOR.'errors'.DIRECTORY_SEPARATOR.$error)){
  41. unlink('..'.DIRECTORY_SEPARATOR.'filesystem'.DIRECTORY_SEPARATOR.'errors'.DIRECTORY_SEPARATOR.$error);
  42. }
  43. }
  44. // User will be redirected back to initial site
  45. header('Location: debugger.php');
  46. die();
  47. } elseif(isset($_GET['error'],$_GET['done'])){
  48. // If error is considered to be 'fixed' then it will be removed, if it exists
  49. if(file_exists('..'.DIRECTORY_SEPARATOR.'filesystem'.DIRECTORY_SEPARATOR.'errors'.DIRECTORY_SEPARATOR.$_GET['error'])){
  50. unlink('..'.DIRECTORY_SEPARATOR.'filesystem'.DIRECTORY_SEPARATOR.'errors'.DIRECTORY_SEPARATOR.$_GET['error']);
  51. }
  52. // User will be redirected back to initial site
  53. header('Location: debugger.php');
  54. die();
  55. } elseif(isset($_GET['error'])){
  56. // If error is found
  57. if(!file_exists('..'.DIRECTORY_SEPARATOR.'filesystem'.DIRECTORY_SEPARATOR.'errors'.DIRECTORY_SEPARATOR.$_GET['error'])){
  58. // User will be redirected back to initial site
  59. header('Location: debugger.php');
  60. die();
  61. }
  62. }
  63. ?>
  64. <!DOCTYPE html>
  65. <html lang="en">
  66. <head>
  67. <title>Debugger</title>
  68. <meta charset="utf-8">
  69. <meta name="viewport" content="width=device-width"/>
  70. <link type="text/css" href="style.css" rel="stylesheet" media="all"/>
  71. <link rel="icon" href="../favicon.ico" type="image/x-icon"/>
  72. <link rel="icon" href="../favicon.ico" type="image/vnd.microsoft.icon"/>
  73. <meta content="noindex,nocache,nofollow,noarchive,noimageindex,nosnippet" name="robots"/>
  74. <meta http-equiv="cache-control" content="no-cache"/>
  75. <meta http-equiv="pragma" content="no-cache"/>
  76. <meta http-equiv="expires" content="0"/>
  77. </head>
  78. <body>
  79. <?php
  80. // Header
  81. echo '<h1>Debugger</h1>';
  82. echo '<h4 class="highlight">';
  83. foreach($softwareVersions as $software=>$version){
  84. // Adding version numbers
  85. echo '<b>'.$software.'</b> ('.$version.') ';
  86. }
  87. echo '</h4>';
  88. // Subheader
  89. echo '<h2>Logged Errors</h2>';
  90. // Action is based on GET values
  91. if(empty($_GET)){
  92. echo '<p class="bold">There are no outstanding errors</p>';
  93. } elseif(isset($_GET['error'])){
  94. 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 delete all error logs</h5>';
  95. 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 delete this error log</h3>';
  96. // Getting error file size
  97. $filesize=filesize('..'.DIRECTORY_SEPARATOR.'filesystem'.DIRECTORY_SEPARATOR.'errors'.DIRECTORY_SEPARATOR.$_GET['error']);
  98. // Attempting to get memory limit
  99. $memory=ini_get('memory_limit');
  100. if($memory){
  101. $memory=iniSettingToBytes($memory);
  102. // Getting the amount of bytes currently allocated
  103. $memory=$memory-memory_get_usage();
  104. // available memory is 'about half' of the actual memory, just in case the array creation takes a lot of memory
  105. $memory=intval($memory/2);
  106. }
  107. // Error is only shown if memory limit could not be gotten or is less than the file size
  108. if(!$memory || $filesize<$memory){
  109. $errorData=file_get_contents('..'.DIRECTORY_SEPARATOR.'filesystem'.DIRECTORY_SEPARATOR.'errors'.DIRECTORY_SEPARATOR.$_GET['error']);
  110. $errorData=explode("\n",$errorData);
  111. foreach($errorData as $nr=>$d){
  112. if(trim($d)!=''){
  113. $d=json_decode($d,true);
  114. // Last element in error log is used as preview of the full error
  115. $time=array_shift($d);
  116. $summary=array_pop($d);
  117. echo '<div class="border block">';
  118. echo '<b>'.$time.'</b>';
  119. echo '<pre>';
  120. print_r($summary);
  121. echo '</pre>';
  122. 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>';
  123. echo '<div id="error'.$nr.'" style="display:none;">';
  124. echo '<pre>';
  125. print_r($d);
  126. echo '</pre>';
  127. echo '</div>';
  128. echo '</div>';
  129. }
  130. }
  131. } else {
  132. echo '<p class="bold">This error log is too large for PHP to handle. Are you using error tracing in your configuration file? If so, then it is recommended to turn that setting off as that will generate smaller error log files.</p>';
  133. }
  134. }
  135. // Footer
  136. echo '<p class="footer small bold">Generated at '.date('d.m.Y h:i').' GMT '.date('P').' for '.$_SERVER['HTTP_HOST'].'</p>';
  137. ?>
  138. </body>
  139. </html>