PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/log-reader.php

https://bitbucket.org/hanigamal/my-wave-framework
PHP | 288 lines | 179 code | 46 blank | 63 comment | 42 complexity | 16da6896fd6412a626c023d912ed649e MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. * Wave Framework <http://www.waveframework.com>
  4. * Log Reader
  5. *
  6. * This is a simple script that is used to read log files stored by WWW_Logger class.
  7. * It loads a log file defined by specific timestamp formatted as Y-m-d-H. By default
  8. * it displays information about all the requests that have happened in the current
  9. * hour, but if GET variable 'log' is supplied in same timestamp format, then that
  10. * log file data is returned instead.
  11. *
  12. * @package Tools
  13. * @author Kristo Vaher <kristo@waher.net>
  14. * @copyright Copyright (c) 2012, Kristo Vaher
  15. * @license GNU Lesser General Public License Version 3
  16. * @tutorial /doc/pages/guide_tools.htm
  17. * @since 1.0.0
  18. * @version 3.2.1
  19. */
  20. // This initializes tools and authentication
  21. require('.'.DIRECTORY_SEPARATOR.'tools_autoload.php');
  22. // Log is printed out in plain text format
  23. header('Content-Type: text/html;charset=utf-8');
  24. // Checking if logger attempts to read internal log
  25. if(isset($_GET['internal'])){
  26. // Actual log address
  27. $logAddress='..'.DIRECTORY_SEPARATOR.'filesystem'.DIRECTORY_SEPARATOR.'logs'.DIRECTORY_SEPARATOR.'internal.tmp';
  28. // If file is set for deletion
  29. if(isset($_GET['delete']) && file_exists($logAddress)){
  30. unlink($logAddress);
  31. // Redirecting to link without delete flag set
  32. header('Location: log-reader.php?internal');
  33. die();
  34. }
  35. } elseif(isset($_GET['api'])){
  36. // Actual log address
  37. $logAddress='..'.DIRECTORY_SEPARATOR.'filesystem'.DIRECTORY_SEPARATOR.'logs'.DIRECTORY_SEPARATOR.'api.tmp';
  38. // If file is set for deletion
  39. if(isset($_GET['delete']) && file_exists($logAddress)){
  40. unlink($logAddress);
  41. // Redirecting to link without delete flag set
  42. header('Location: log-reader.php?api');
  43. die();
  44. }
  45. } else {
  46. // Log reader can access any log file created by the system
  47. if(isset($_GET['log'])){
  48. // User agent requested input URL is validated against hostile characters
  49. $logFileName=preg_replace('/[^A-Za-z\-\_0-9\/]/i','',$_GET['log']);
  50. } else {
  51. // By default the results are returned from current hour
  52. header('Location: log-reader.php?log='.date('Y-m-d-H'));
  53. die();
  54. }
  55. // This stores the array types to print out
  56. $types=array();
  57. // You can print out only some log information
  58. if(isset($_GET['types'])){
  59. $rawTypes=explode(',',$_GET['types']);
  60. foreach($rawTypes as $t){
  61. $bits=explode('[',$t);
  62. if(isset($bits[1])){
  63. $types[$t]=str_replace(']','',$bits[1]);
  64. } else {
  65. $types[$t]=true;
  66. }
  67. }
  68. } else {
  69. $types['all']=true;
  70. }
  71. // Every day the logs are stored under different log subfolder
  72. $logSubfolder=substr($logFileName,0,10);
  73. // Actual log address
  74. $logAddress='..'.DIRECTORY_SEPARATOR.'filesystem'.DIRECTORY_SEPARATOR.'logs'.DIRECTORY_SEPARATOR.$logSubfolder.DIRECTORY_SEPARATOR.$logFileName.'.tmp';
  75. // If file is set for deletion
  76. if(isset($_GET['delete']) && file_exists($logAddress)){
  77. unlink($logAddress);
  78. unset($_GET['delete']);
  79. // Redirecting to link without delete flag set
  80. if(!empty($_GET)){
  81. header('Location: log-reader.php?'.http_build_query($_GET));
  82. } else {
  83. header('Location: log-reader.php');
  84. }
  85. die();
  86. }
  87. }
  88. ?>
  89. <!DOCTYPE html>
  90. <html lang="en">
  91. <head>
  92. <title><?=(isset($_GET['internal']))?'Internal Log':((isset($_GET['api']))?'API Log':'Log Reader')?></title>
  93. <meta charset="utf-8">
  94. <meta name="viewport" content="width=device-width"/>
  95. <link type="text/css" href="style.css" rel="stylesheet" media="all"/>
  96. <link rel="icon" href="../favicon.ico" type="image/x-icon"/>
  97. <link rel="icon" href="../favicon.ico" type="image/vnd.microsoft.icon"/>
  98. <meta content="noindex,nocache,nofollow,noarchive,noimageindex,nosnippet" name="robots"/>
  99. <meta http-equiv="cache-control" content="no-cache"/>
  100. <meta http-equiv="pragma" content="no-cache"/>
  101. <meta http-equiv="expires" content="0"/>
  102. </head>
  103. <body>
  104. <?php
  105. // Header
  106. if(isset($_GET['internal'])){
  107. echo '<h1>Internal Log</h1>';
  108. } elseif(isset($_GET['api'])){
  109. echo '<h1>API Log</h1>';
  110. } else {
  111. echo '<h1>HTTP Request Log</h1>';
  112. }
  113. echo '<h4 class="highlight">';
  114. foreach($softwareVersions as $software=>$version){
  115. // Adding version numbers
  116. echo '<b>'.$software.'</b> ('.$version.') ';
  117. }
  118. echo '</h4>';
  119. echo '<h2>Log</h2>';
  120. // All logs are stored in /log/ folder, if a folder does not exist
  121. if(file_exists($logAddress)){
  122. // File delete link
  123. echo '<h3 onclick="if(confirm(\'Are you sure?\')){ document.location.href=document.location.href+\'&delete\'; }" class="red bold" style="cursor:pointer;">Click to delete this log</h3>';
  124. // Getting error file size
  125. $filesize=filesize($logAddress);
  126. // Attempting to get memory limit
  127. $memory=ini_get('memory_limit');
  128. if($memory){
  129. $memory=iniSettingToBytes($memory);
  130. // Getting the amount of bytes currently allocated
  131. $memory=$memory-memory_get_usage();
  132. // available memory is 'about half' of the actual memory, just in case the array creation takes a lot of memory
  133. $memory=intval($memory/2);
  134. }
  135. // Output buffer allows to increase peformance due to multiple echo's
  136. ob_start();
  137. // This variable holds various summary statistics
  138. $summary=array();
  139. // Log is only shown if memory limit could not be gotten or is less than the file size
  140. if(!$memory || $filesize<$memory){
  141. // Log files are stored as JSON serialized arrays, separated with line-breaks
  142. $log=explode("\n",file_get_contents($logAddress));
  143. // Printing out every line from the log file
  144. foreach($log as $l){
  145. if(isset($_GET['api']) && $l!=''){
  146. $logDetails=explode("\t",$l);
  147. echo '<div class="block">';
  148. // Printing out log data
  149. echo '<b>'.date('d.m.Y H:i:s',$logDetails[0]).'</b> '.$logDetails[1].' -&gt; '.$logDetails[2].'<br/>';
  150. echo '</div>';
  151. if($logDetails[0]>=($_SERVER['REQUEST_TIME']-2592000)){
  152. $summary['30days'][$logDetails[1]][$logDetails[2]]++;
  153. }
  154. $summary['totals'][$logDetails[1]][$logDetails[2]]++;
  155. } else {
  156. // Log data is deencoded from JSON string
  157. $l=json_decode($l,true);
  158. // Log entry should be an array once decoded
  159. if(is_array($l)){
  160. $accepted=true;
  161. // Breaking out of the loop if the assigned key value is not the one that is required
  162. if(isset($types)){
  163. foreach($types as $key=>$t){
  164. if($key!='all' && $t!==true){
  165. if(!isset($l[str_replace('['.$t.']','',$key)]) || $l[str_replace('['.$t.']','',$key)]!=$t){
  166. $accepted=false;
  167. }
  168. }
  169. }
  170. }
  171. if($accepted){
  172. echo '<div class="border block">';
  173. // Printing out log data
  174. foreach($l as $key=>$entry){
  175. if(isset($_GET['internal']) || isset($types['all']) || isset($types[$key])){
  176. if(!is_array($entry)){
  177. echo '<b>'.$key.':</b> '.$entry.'<br/>';
  178. } else {
  179. echo '<b>'.$key.':</b>';
  180. echo '<pre class="small box disabled">';
  181. print_r($entry);
  182. echo '</pre>';
  183. }
  184. }
  185. }
  186. echo '</div>';
  187. }
  188. }
  189. }
  190. }
  191. } else {
  192. echo '<p class="bold">This log is too large for PHP to handle.</p>';
  193. }
  194. // Getting the content from output buffer
  195. $logContent=ob_get_clean();
  196. // Output buffer allows to increase peformance due to multiple echo's
  197. ob_start();
  198. // If summary data is set
  199. if(!empty($summary)){
  200. echo '<h2>Summary</h2>';
  201. if(isset($_GET['api'])){
  202. // Summary for 30 Days
  203. echo '<h3>Lat 30 Days</h3>';
  204. foreach($summary['30days'] as $profile=>$data){
  205. echo '<h4><b>'.$profile.'</b></h4>';
  206. echo '<ul>';
  207. foreach($data as $command=>$d){
  208. echo '<li><b>'.$command.'</b> '.$d.' calls</li>';
  209. }
  210. echo '</ul>';
  211. }
  212. // Printing out summary for totals
  213. echo '<h3>Totals</h3>';
  214. foreach($summary['totals'] as $profile=>$data){
  215. echo '<h4><b>'.$profile.'</b></h4>';
  216. echo '<ul>';
  217. foreach($data as $command=>$d){
  218. echo '<li><b>'.$command.'</b> '.$d.' calls</li>';
  219. }
  220. echo '</ul>';
  221. }
  222. }
  223. echo '<h2>Entries</h2>';
  224. }
  225. // Getting the content from output buffer
  226. $summaryContent=ob_get_clean();
  227. // Printing out summary and log
  228. echo $summaryContent;
  229. echo $logContent;
  230. } else {
  231. // Log information not found
  232. echo '<p class="red bold">Cannot find log information</p>';
  233. }
  234. echo '<h2>Modes</h2>';
  235. echo '<p><a href="log-reader.php">HTTP Request Log</a> - Log information about HTTP requests</p>';
  236. echo '<p><a href="log-reader.php?internal">Internal Log</a> - Internal log entries</p>';
  237. echo '<p><a href="log-reader.php?api">API Log</a> - API profile call log</p>';
  238. // Footer
  239. echo '<p class="footer small bold">Generated at '.date('d.m.Y h:i').' GMT '.date('P').' for '.$_SERVER['HTTP_HOST'].'</p>';
  240. ?>
  241. </body>
  242. </html>