Location: PHPKode > scripts > Image Hotlink Log and Control > hotlinklog.php
<html>
<head>
<!-- style for 'image missing' tooltip -->
<style>
.popup
{
	COLOR: #9F141A;
	CURSOR: help;
	TEXT-DECORATION: none
}
</style>
</head>
<body>

<?php

// Hotlink Log and Image Watermarking Script
// Copyright (C) 2005  SillyJokes.co.uk

require 'hotlinklogconfig.php';
require 'hotlinklogfuncs.php';

$me = basename($_SERVER['PHP_SELF']);

set_time_limit(120);


$f = fopen(LOGFILE,'r');
$files = array();
$referrers = array();
while (!feof($f))
{
	$buf = fgets($f);		// defaults to reading a whole line at a time, no matter what it's girth be
	$guff = MungLogLine($buf);
	if ($guff===FALSE)
	{
		continue;
	}
	extract($guff);	// makes $filename, $time, $action, $referrer
	
	if (EntryTooOld($time))
	{
		continue;
	}
		
	// count files
	if (empty($files[$filename]))
	{
		$files[$filename]['count'] = 1;
		// get girth of this image file
		$files[$filename]['girth'] = file_exists($filename)?filesize($filename):0;
	} else {
		$files[$filename]['count']++;
	}
	
	// count referrers
	if (empty($referrers[$referrer]))
	{
		$referrers[$referrer]['count'] = 0;
		$referrers[$referrer]['girth'] = 0;
		
		// get the domain, it'll come in handy later
		$domain = ExtractDomain($referrer);
		if (strlen($domain)<3)	// no sensible domain found?
		{
			$domain = 'INVALID REFERRER DOMAIN';
		}
		$referrers[$referrer]['domain'] = $domain;
	}
	$referrers[$referrer]['count']++;
	$referrers[$referrer]['girth'] += $files[$filename]['girth'];
}

fclose($f);

// list of hotlinkages sorted by bandwidth
$suckers = array();
foreach($referrers as $r)
{
	if (empty($suckers[$r['domain']]))
	{
		$suckers[$r['domain']] = 0;
	}
	$suckers[$r['domain']] += $r['girth'];
}
arsort($suckers);


echo "<p style='font: xx-large verdana'><b>Image Hotlink Stats</b></p>";
echo "<p>How much, and by whom.</p>\n";

// round time to previous midnight
$t = time();
$midnight = $t - $t%ONEDAY;

// bandwidth shown for past x days
$daysago = 3;
$totalgirth = array();
$f = fopen(LOGFILE,'r');
// $d represents 'days ago' (it only goes backwards because I was going to output a table straight from here)
$sucker = empty($_REQUEST['sucker'])?'':$_REQUEST['sucker'];
for ($d = $daysago; $d>0; --$d)
{
	$ttry = $t - (ONEDAY * $d);	// go back in time
	
	$totalgirth[$d] = 0;
	rewind($f);	

	while (!feof($f))
	{
		$buf = fgets($f);		// defaults to reading a whole line at a time, no matter what it's girth be
		$guff = MungLogLine($buf);
		if ($guff===FALSE)
		{
			continue;
		}
		extract($guff);	// makes $filename, $time, $action, $referrer
		
		// did this hit happen inside this particular day? (and for the sux0r)
		if ($time>$ttry && $time<($ttry+ONEDAY))
		{	
			if (!empty($sucker))
			{
				// domain match? if not, skip this
				if (ExtractDomain($referrer)!=$sucker)
				{
					continue;
				}
			}
			
			$totalgirth[$d] += $files[$filename]['girth'];
		}
	}
}

fclose($f);

echo "<p>Bytes leeched over the last few days:</p>\n";
echo "<table border='1' cellpadding='3' cellspacing='0'>\n";
echo "<tr>";
for ($d=0; $d<$daysago; ++$d)
{
	echo '<td>'.date('j M',$midnight-($daysago-$d)*ONEDAY).'</td>';
}
echo "</tr>\n";
echo "<tr>";
for ($d=0; $d<$daysago; ++$d)
{
	echo '<td>'.ByteFormat($totalgirth[$daysago-$d]).'</td>';
}
echo "</tr>\n";
echo "</table>\n";


if (empty($_REQUEST['sucker']))
{
	// element 0 contains the total processed. Element 1 contains the total watermarked.
	$wmcount=file('../logs/hotlinkcount.log');

	echo "<p><table border='1' cellpadding='3' cellspacing='0'>\n";
	
	$total = 0;
	for ($i=0; $i<NUM_ACTIONS; ++$i)
	{
		$total += $wmcount[$i];
		echo "<tr><td>$actionnames[$i]</td><td align='right'>".number_format($wmcount[$i])."</td></tr>\n";
	}
	echo "<tr><td><b>total</b></td><td align='right'>".number_format($total)."</td></tr>\n";
	echo "</table></p>\n";
	echo "<p>List of hotlinkers:</p>\n";
	echo "<p>images: ".number_format(sizeof($files))." referrers: ".number_format(sizeof($referrers))."</p>";
	echo "<table border='1' cellpadding='3' cellspacing='0'>\n";
		
	foreach($suckers as $sucker => $girth)
	{
		if (!empty($sucker))
		{
			echo "<tr><td align='right'>".ByteFormat($girth)."</td><td><a href='$me?sucker=$sucker'>$sucker</a></td></tr>\n";
		}
	}
	
	echo "</table>\n";
} else {
	
	////// SPECIFIC HOTLINKER
	
	echo "<p><a href=$me><<< Return to main overview</a></p>\n";
	
	$sucker = $_REQUEST['sucker'];
	echo "<p>Bandwidth sucked by $sucker. Total: ".ByteFormat($suckers[$sucker])."</p>\n";
	
	// list links with bandwidth and count
	
	echo "<table border='1' cellpadding='3' cellspacing='0'>\n";
	echo "<tr><td>bandwidth</td><td>count</td><td>image</td><td>links (open in new window) </td></tr>\n";
	
	$imagelist = array();
	$girthlist = array();
	
	// scan the logfile. For each image that's linked by this referrer, stash links.
	// not terribly efficient but it's better than scanning the whole file 10000 times.
	$f = fopen(LOGFILE,'r');
	while (!feof($f))
	{
		$buf = fgets($f);		// defaults to reading a whole line at a time, no matter what it's girth be
		$guff = MungLogLine($buf);
		if ($guff===FALSE)
		{
			continue;
		}
		extract($guff);	// makes $filename, $time, $action, $referrer
		
		if (EntryTooOld($time))
		{
			continue;
		}
		
		// domain match?
		if (ExtractDomain($referrer)!=$sucker)
		{
			continue;
		}
		
		// annoying
		if (empty($imagelist[$filename]))
		{
			$imagelist[$filename] = array();
		}
		// grr
		if (empty($imagelist[$filename][$referrer]))
		{
			$imagelist[$filename][$referrer] = 0;
		}
		
		++$imagelist[$filename][$referrer];
		
		// eergh
		if (empty($girthlist[$filename]))
		{
			$girthlist[$filename] = 0;
		}
		$girthlist[$filename] += $files[$filename]['girth'];
	}
	
	// sort $girthlist by size in reverse order
	arsort($girthlist);
		
	foreach($girthlist as $fname => $girth)
	{
		// punt out count, image, list of linkys
		
		$links = $imagelist[$fname];
		
		$numlinks = 0;
		$linkstr = '';
		$maxlinks = 5;
		foreach($links as $link => $count)
		{
			
			if ($numlinks < $maxlinks)
			{
				$linkstr .= "<a href='$link' target='_blank'>$link</a><br/>";
			}
			else
			{
				$remain = sizeof($links)-$numlinks;
				$linkstr .= "<p><a href='linklist.php?img=$fname&sucker=$sucker' target='_blank'>".$remain." more links, click to see all of them</a></p>";
				break;
			}

			++$numlinks;
		}
		
		//$girth = $count * $files[$fname]['girth'];

		echo "<tr><td align='right'>".ByteFormat($girth)."</td><td align='right'>".number_format(sizeof($links))."</td><td>";
		
		if (file_exists($fname))
		{
			list($width, $height) = getimagesize($fname);
			$imglink = stristr($fname,'/images')."?thing=".time();
			$newheight = 64;
			$newwidth = $width * 64/$height;
			echo "<a href='$imglink' target='_blank'><img src='$imglink' width='$newwidth' height='$newheight' border='0'></a>";
		} else {
			// file not found - add a tooltip that shows the filename.
			$imglink = stristr($fname,'/images');
			echo "<SPAN title='$imglink' class='popup'>image missing</SPAN>";
		}
		
		echo "</td>";
		echo "<td>$linkstr</td>";
		echo "</tr>\n";
	}
	echo "</table>\n";

	echo "<p><a href=$me><<< Return to main girth overview</a></p>\n";
}


?>

</body>
</html>
Return current item: Image Hotlink Log and Control