Location: PHPKode > projects > Php-Logging-System > phplogsys-1.1.4/includes/apacheresult.php
<?php
$inputValError = array();
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(defined('COUNT_ROWS') && COUNT_ROWS == TRUE){
	$query = 'SELECT SQL_CALC_FOUND_ROWS * FROM '.$srcTable.' ';
}
else{
	$query = 'SELECT * FROM '.$srcTable.' ';
}
if($where){
	$query = $query.'WHERE '.$where.' ORDER BY '.$orderby.' '.$order.' LIMIT '.$offset.', '.$limit;
}
else{
	$query = $query.'ORDER BY '.$orderby.' '.$order.' LIMIT '.$offset.', '.$limit;
}

//------------------------------------------------------------------------
// Execute the query
// The FOUND_ROWS function returns the value from the SQL_CALC_FOUND_ROWS
// count.
//------------------------------------------------------------------------
$results = perform_query($query, $dbLink);
if(defined('COUNT_ROWS') && COUNT_ROWS == TRUE){
	$num_results_array = perform_query("SELECT FOUND_ROWS()", $dbLink);
	$num_results_array = fetch_array($num_results_array);
	$num_results = $num_results_array[0];
}
//========================================================================
// END: BUILD AND EXECUTE SQL STATEMENT
// AND BUILD PARAMETER LIST FOR HTML GETS
//========================================================================

//========================================================================
// BEGIN: PREPARE RESULT ARRAY
//========================================================================
$n = 0;
while($row = fetch_array($results)){
	$row['count'] = 1;
	$result_array[$n] = $row;
	$n++;
}
//========================================================================
// END: PREPARE RESULT ARRAY
//========================================================================

//========================================================================
// BEGIN: BUILDING THE HTML PAGE
//========================================================================
// Print result sub-header
require_once(BASE_PATH . '/apache_result_subheader.php');

// If there is a result list then print it
if (count($result_array)){
	//------------------------------------------------------------------------
	// If the query returned some results then start the table with the
	// results
	//------------------------------------------------------------------------
	echo '<table width="100%"><tr class="resultsheader">';
	echo '<td><a href="', $_SERVER['PHP_SELF'], '?orderby=seq&order=', (($orderby=='seq' && $order=='DESC') ? 'ASC' : 'DESC'), '&', $orderParamsGET, '">SEQ</a></td>';
	echo '<td><a href="', $_SERVER['PHP_SELF'], '?orderby=timestamp&order=', (($orderby=='timestamp' && $order=='DESC') ? 'ASC' : 'DESC'), '&', $orderParamsGET, '">TIMESTAMP</a></td>';
	echo '<td><a href="', $_SERVER['PHP_SELF'], '?orderby=servername&order=', (($orderby=='servername' && $order=='DESC') ? 'ASC' : 'DESC'), '&', $orderParamsGET, '">SERVER</a></td>';
	echo '<td><a href="', $_SERVER['PHP_SELF'], '?orderby=requesturl&order=', (($orderby=='requesturl' && $order=='DESC') ? 'ASC' : 'DESC'), '&', $orderParamsGET, '">REQUESTED URL</a></td>';
	echo '<td><a href="', $_SERVER['PHP_SELF'], '?orderby=requestmethod&order=', (($orderby=='requestmethod' && $order=='DESC') ? 'ASC' : 'DESC'), '&', $orderParamsGET, '">REQUEST METHOD</a></td>';
	echo '<td><a href="', $_SERVER['PHP_SELF'], '?orderby=mimetype&order=', (($orderby=='mimetype' && $order=='DESC') ? 'ASC' : 'DESC'), '&', $orderParamsGET, '">MIMETYPE</a></td>';
	echo '<td><a href="', $_SERVER['PHP_SELF'], '?orderby=username&order=', (($orderby=='username' && $order=='DESC') ? 'ASC' : 'DESC'), '&', $orderParamsGET, '">USERNAME</a></td>';
	echo '<td><a href="', $_SERVER['PHP_SELF'], '?orderby=apachestatus&order=', (($orderby=='apachestatus' && $order=='DESC') ? 'ASC' : 'DESC'), '&', $orderParamsGET, '">APACHE STATUS</a></td>';
	echo '<td><a href="', $_SERVER['PHP_SELF'], '?orderby=replysize&order=', (($orderby=='replysize' && $order=='DESC') ? 'ASC' : 'DESC'), '&', $orderParamsGET, '">REPLY SIZE</a></td>';
	echo '<td><a href="', $_SERVER['PHP_SELF'], '?orderby=clientip&order=', (($orderby=='clientip' && $order=='DESC') ? 'ASC' : 'DESC'), '&', $orderParamsGET, '">CLIENT IP</a></td>';
	echo '<td><a href="', $_SERVER['PHP_SELF'], '?orderby=useragent&order=', (($orderby=='useragent' && $order=='DESC') ? 'ASC' : 'DESC'), '&', $orderParamsGET, '">USERAGENT</a></td>';
	echo '<td><a href="', $_SERVER['PHP_SELF'], '?orderby=serverport&order=', (($orderby=='serverport' && $order=='DESC') ? 'ASC' : 'DESC'), '&', $orderParamsGET, '">SERVER PORT</a></td>';
	echo '<td><a href="', $_SERVER['PHP_SELF'], '?orderby=sslprotocol&order=', (($orderby=='sslprotocol' && $order=='DESC') ? 'ASC' : 'DESC'), '&', $orderParamsGET, '">SSL PROTOCOL</a></td>';
	echo '<td><a href="', $_SERVER['PHP_SELF'], '?orderby=sslcipher&order=', (($orderby=='sslcipher' && $order=='DESC') ? 'ASC' : 'DESC'), '&', $orderParamsGET, '">SSL CIPHER</a></td>';
	echo '</tr>';
	//------------------------------------------------------------------------
	// Output the table with the results
	// Use an alternating background and color code the requestmethod column
	//------------------------------------------------------------------------
	$color = 'lighter';
	$today = date('Y-m-d');
	for($i=0; $i < count($result_array); $i++){
		$row = $result_array[$i];
		if($color == 'darker'){
			$color = 'lighter';
		}else{
			$color = 'darker';
		}
		echo '<tr class=', $color, '>';
		echo '<td>', $row['seq'], '</td>';
		echo '<td nowrap>', date('m/d/Y H:i:s', $row['timestamp']), '</td>';
		echo '<td nowrap><a href="', $_SERVER['PHP_SELF'], '?excludeserver=0&server[]=', $row['servername'], '&', $serverParamsGET, '">', $row['servername'], '</a></td>';
		echo '<td nowrap>', htmlspecialchars($row['requesturl']), '</td>';
		echo '<td nowrap><a href="', $_SERVER['PHP_SELF'], '?excluderequestmethod=0&requestmethod[]=', urlencode($row['requestmethod']), '&', $requestmethodParamsGET, '">', $row['requestmethod'], '</a></td>';
		echo '<td nowrap><a href="', $_SERVER['PHP_SELF'], '?excludemimetype=0&mimetype[]=', urlencode($row['mimetype']), '&', $mimetypeParamsGET, '">', $row['mimetype'], '</a></td>';
		echo '<td>', $row['username'], '</td>';
		echo '<td>', $row['apachestatus'], '</td>';
		echo '<td>', $row['replysize'], '</td>';
		echo '<td>', $row['clientip'], '</td>';
		echo '<td nowrap><a href="', $_SERVER['PHP_SELF'], '?excludeuseragent=0&useragent[]=', urlencode($row['useragent']), '&', $useragentParamsGET, '">', htmlspecialchars($row['useragent']), '</a></td>';
		echo '<td>', $row['serverport'], '</td>';
		echo '<td>', $row['sslprotocol'], '</td>';
		echo '<td>', $row['sslcipher'], '</td>';
		echo '</tr>';
	}
	echo '</table>';

	//------------------------------------------------------------------------
	// Create the list with links to other results.
	// The list will show a maximum of 11 pages + first and last
	//------------------------------------------------------------------------
	echo 'Result Page: ';

	if($num_results){

		// If you are not on the first page then show the FIRST link
		if($offset>0){
			echo '<a href="', $_SERVER['PHP_SELF'], '?offset=0', $ParamsGET, '"><BIG>FIRST</BIG></a> ';
		}

		// If you are not on one of the first two pages then also show the PREV link
		if($offset>$limit+1){
			$prevoffset=$offset-$limit ;
			echo '<a href="', $_SERVER['PHP_SELF'], '?offset=', $prevoffset, $ParamsGET, '"><BIG>PREV</BIG></a>';
		}

		// Calculate the total number of pages in the query
		$totalpages=intval($num_results/$limit);
		if($num_results%$limit){
			$totalpages++;
		}

		// Calculate the current page
		$curpage=floor($offset/$limit);

		// Figure out what the first page on the list should be
		if($curpage<5){
			$firstpage = 0;
		}else{
			$firstpage = $curpage - 5;
		}
		if($curpage>$totalpages - 6){
			$firstpage = $totalpages - 11;
		}
		if($firstpage<5){
			$firstpage = 0;
		}
		if($totalpages < 11){
			$listpages = $totalpages;
		}else{
			$listpages = 11;
		}

		// Determine what the last page on the list should be
		$lastpage = $firstpage + $listpages;

		// Output the list of numbered links to the 11 closest pages.
		// The current page is high-lighted and the other are created as links
		for($i=$firstpage;$i<$lastpage;$i++){
			$pageoffset=$i*$limit;
			$pagenum = $i + 1;
			if($curpage==$i){
				echo '<font size=+1>[', $pagenum, ']</font> ';
			}else{
				echo '<a href="', $_SERVER['PHP_SELF'], '?offset=', $pageoffset, $ParamsGET, '">', $pagenum, '</a> ';
			}
		}

		// If there's a page with a higher offset and that page is not the last
		// on the list then create a NEXT link.
		if((intval($offset/$limit)+2)<$totalpages){
			$nextoffset=$offset+$limit;
			echo '<a href="', $_SERVER['PHP_SELF'], '?offset=', $nextoffset, $ParamsGET, '"><BIG>NEXT</BIG></a> ';
		}

		// If you are not currently on the last page then create a LAST link.
		if($totalpages>1 && (intval($offset/$limit)+1)!=$totalpages){
			$lastoffset=($totalpages-1)*$limit;
			echo '<a href="', $_SERVER['PHP_SELF'], '?offset=', $lastoffset, $ParamsGET, '"><BIG>LAST</BIG></a> ';
		}
	}else{
		// This for backwards when total row count is not calculated.
		if($offset>0){
			echo '<a href="', $_SERVER['PHP_SELF'], '?offset=0', $ParamsGET, '"><BIG>FIRST</BIG></a> ';
		}

		// If you are not on one of the first two pages then also show the PREV link
		if($offset>$limit+1){
			$prevoffset=$offset-$limit ;
			echo '<a href="', $_SERVER['PHP_SELF'], '?offset=', $prevoffset, $ParamsGET, '"><BIG>PREV</BIG></a> ';
		}
		$nextoffset=$offset+$limit;
		echo '<a href="', $_SERVER['PHP_SELF'], '?offset=', $nextoffset, $ParamsGET, '"><BIG>NEXT</BIG></a> ';
	}
}else{
	//------------------------------------------------------------------------
	// Else just direct the user back to the form
	//------------------------------------------------------------------------
	echo 'No results found, <br><a href="', $_SERVER["PHP_SELF"], '?pageId=apacheform">BACK TO SEARCH</a>';
}

	//========================================================================
	// END: BUILDING THE HTML PAGE
	//========================================================================
?>
Return current item: Php-Logging-System