PageRenderTime 36ms CodeModel.GetById 15ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 1ms

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