Location: PHPKode > projects > Php-Logging-System > phplogsys-1.1.4/includes/apachehilbertgraph.php
<?php
// Hilbert Curve Graph algorithum
require_once(BASE_PATH . '/hilbert.php');
$inputValError = array();
$_GET['excludeclientip'] = 0; //Force the client ips to be include only
$_SERVER["REQUEST_URI"] = preg_replace('/excludeclientip=1/', 'excludeclientip=0', $_SERVER["REQUEST_URI"]);
require_once(BASE_PATH . '/parse_apache.php');

//------------------------------------------------------------------------
// Create the complete SQL statement
// SQL_CALC_FOUND_ROWS is a MySQL 4.0 feature that allows you to get the
// total number of results if you had not used a LIMIT statement. Using
// it saves an extra query to get the total number of rows.
//------------------------------------------------------------------------
if($table){
	$srcTable = $table;
} else{
	$srcTable = DEFAULTAPACHETABLE;
}

if($_GET['hilbertdata'] == 'Number of Requests'){
	$query = 'SELECT count(*) AS resultdata, clientip FROM '.$srcTable.' ';
}else{
	$query = 'SELECT sum(replysize) AS resultdata, clientip FROM '.$srcTable.' ';
}

if($where){
	$query = $query.'WHERE '.$where.' GROUP BY clientip';
}
else{
	$query = $query.' GROUP BY clientip';
}

//------------------------------------------------------------------------
// Execute the query
//------------------------------------------------------------------------
$results = perform_query($query, $dbLink);
//========================================================================
// END: BUILD AND EXECUTE SQL STATEMENT
//========================================================================

//========================================================================
// BEGIN: PREPARE RESULT ARRAY
//========================================================================
$maxval=1;
$find = array('/\./', '/\*/', '/OCT[0-4]/', '/LABEL|DATA/');
$repl = array('\.', '.*', '[0-9]*', '([0-9]*)');
while($row = fetch_array($results)){
	$reg = preg_replace($find, $repl, $_GET['hilbertgraph']);
	$label = preg_replace("/$reg/", '$1', $row['clientip']);
	$data = preg_replace("/$reg/", '$2', $row['clientip']);
	if(!isset($result_array[$label][$data])){
		$result_array[$label][$data] = $row['resultdata'];
	}else{
		$result_array[$label][$data] += $row['resultdata'];
	}
	while($row['resultdata'] >= 1024 * $maxval) $maxval++;
}
//print_r($result_array);
//exit();

//========================================================================
// END: PREPARE RESULT ARRAY
//========================================================================
function adddot($x, $y){
	global $img;
	global $maxval;
	global $result_array;
	global $adddot_params;
	static $num = 0;
	if($num == 256) $num = 0;

	//Move to the correct box
	$x += $adddot_params['x'];
	$y += $adddot_params['y'];

	if(isset($result_array[$adddot_params['oct']][$num])){
		list($r, $g, $b) = rgb(intval($result_array[$adddot_params['oct']][$num] / $maxval));
		$color = imagecolorallocate($img, $r, $g, $b);
		imagefilledrectangle($img, $x, $y, $x+3, $y+3, $color);
		imagecolordeallocate($img, $color);
	}
	$num++;
}

$adddot_params = array('oct' => 0);
function addbox($x, $y){
	global $img;
	global $labelcolor;
	global $result_array;
	global $adddot_params;
	global $imgmap;
	global $imgmap_url;

	//stay off the grid lines
	$x++;
	$y++;

	$imgmap .= '<area shape="rect" coords="' . $x . ',' . $y . ',' . ($x+64) . ',' . ($y+64) . '" href="' . $imgmap_url . $adddot_params['oct'] . '">';

	$adddot_params['x'] = $x;
	$adddot_params['y'] = $y;

	if(isset($result_array[$adddot_params['oct']])){
		$h = new hilbert_square(16, 'adddot');
		$h->scale = 4;
		$h->run();
	}

	imagestring($img, 2, $x+2, $y, $adddot_params['oct'], $labelcolor);
	$adddot_params['oct']++;
}

//========================================================================
// BEGIN: BUILDING THE HTML PAGE
//========================================================================
$img = imagecreatetruecolor(1040, 1040);
$bordercolor1 = imagecolorallocate($img, 128, 16, 0);
$bordercolor2 = imagecolorallocate($img, 194, 16, 0);
$bordercolor3 = imagecolorallocate($img, 255, 16, 0);
$labelcolor = imagecolorallocate($img, 218, 141, 0);

$num = -1;

// Grid
for($x = 0;$x<=1040;$x+=65){
	imageline($img, $x, 0, $x, 1040, $bordercolor1);
	imageline($img, 0, $x, 1040, $x, $bordercolor1);
}
for($x = 0;$x<=1040;$x+=260){
	imageline($img, $x, 0, $x, 1040, $bordercolor2);
	imageline($img, 0, $x, 1040, $x, $bordercolor2);
}
for($x = 0;$x<=1040;$x+=520){
	imageline($img, $x, 0, $x, 1040, $bordercolor3);
	imageline($img, 0, $x, 1040, $x, $bordercolor3);
}

switch($_GET['hilbertgraph']){
	case 'LABEL.DATA.*.*':
		$find = array('/clientipoct1=[0-9]*&/', '/hilbertgraph=LABEL\.DATA\.\*\.\*/');
		$repl = array('', 'hilbertgraph=OCT1.LABEL.DATA.*');
		$imgmap_url = preg_replace($find, $repl, $_SERVER["REQUEST_URI"]) . '&clientipoct1=';
		break;
	case 'OCT1.LABEL.DATA.*':
		$find = array('/clientipoct2=[0-9]*&/', '/hilbertgraph=OCT1\.LABEL\.DATA\.\*/');
		$repl = array('', 'hilbertgraph=OCT1.OCT2.LABEL.DATA');
		$imgmap_url = preg_replace($find, $repl, $_SERVER["REQUEST_URI"]) . '&clientipoct2=';
		break;
	case 'OCT1.OCT2.LABEL.DATA':
		$find = array('/clientipoct3=[0-9]*&/', '/pageId=Apache\+IP\+Map/');
		$repl = array('', 'pageId=Search+Apache');
		$imgmap_url = preg_replace($find, $repl, $_SERVER["REQUEST_URI"]) . '&clientipoct3=';
		break;
}

$label = 0;
$imgmap = '';
$h = new hilbert_square(16, 'addbox');
$h->scale = 65;
$h->run();

require_once(BASE_PATH . '/apache_result_subheader.php');
echo '<map name="hilbert">', $imgmap, '</map>';

$imgscale = imagecreatetruecolor(1024, 1);
for($x=0;$x<=1024;$x++){
	list($r, $g, $b) = rgb($x);
	$c = imagecolorallocate($img, $r, $g, $b);
	imagesetpixel($imgscale, $x, 0, $c);
	imagecolordeallocate($imgscale, $c);
}

ob_start();
imagepng($imgscale);
imagedestroy($imgscale);
$imgscale = base64_encode(ob_get_contents());
ob_end_clean();

echo '<div align="center">1 <img width="1024" height="10" src="data:image/gif;base64,', $imgscale, '" border="1"> ', (1024 * $maxval), '</div>';

ob_start();
imagepng($img);
imagedestroy($img);
$img = base64_encode(ob_get_contents());
ob_end_clean();

echo '<div align="center"><img src="data:image/gif;base64,', $img, '" usemap="#hilbert" border="0"></div>';
?>
Return current item: Php-Logging-System