Location: PHPKode > projects > logViewSQL > apache/apache-common.php
<?php
/* 
 * This is a php frontend for mod_log_sql. It interfaces
 * with the table structure as defined in a MySql database and has been
 * tested only with version 1.16 of mod_log_sql.
 *
 * contact hide@address.com
 *
 */
require('apache-settings.php'); // Open this file to check/change settings
require('http-status-codes.php');
include("geoip.inc");

$validTables = getValidTables();

function myDate($date){
	global $dateFormat;
	return date($dateFormat, $date);
}

function getNumberStatus($aStatus, $dbs){
	$aQuery = "select count(*) from ".mysql_Escape_string($dbs)." where status='".$aStatus."'".sensibleFilter(2).customFilter(1);
	$tempResults = mysql_query($aQuery);
	$tempResult = mysql_fetch_row($tempResults);
	return $tempResult[0];
}

function geoCountry($addr){
	global $geoFile, $geoEnable;	
	if($geoEnable&& !is_null($addr)){				
		$geo = geoip_open($geoFile);
		$geoString = geoip_country_name_by_addr($geo, $addr);
		geoip_close($geo);
		return $geoString;
	} else {return "N/A";}
}

function geoCode($addr){
	global $geoFile, $geoEnable;	
	if($geoEnable&& !is_null($addr)){				
		$geo = geoip_open($geoFile);
		$geoString = geoip_country_code_by_addr($geo, $addr);
		geoip_close($geo);
		return $geoString;
	} else {return "N/A";}
}

function getShortString($aString){
	global $truncLimit;
	$fuzz = round((($truncLimit/2)-5),0);
	if(strlen($aString) > $truncLimit){
		$start = substr($aString, 0, $fuzz);
		$end = substr($aString, (strlen($aString)-$fuzz), strlen($aString));
		$aString = $start.' <img src="resources/images/truncated.gif" align=bottom border=0 alt="'.$aString.'"> 
		'.$end;
	}
	return $aString;
}

function getBytes($aString){
	if(is_null($aString)){
		$aString = "0K";
	}
	else if($aString > 1073741824){
		$aString = round(((($aString/1024)/1024)/1024),2)."Gb";
	}
	else if($aString > 1048576){
		$aString = round((($aString/1024)/1024),2)."Mb";
	}
	else if($aString > 1024){
		$aString = round(($aString/1024),2)."Kb";
	}
	else {
		$aString = ($aString)."b";
	}
	return $aString;
}

function getStatusImage($status){
	global $displayStatusImages;
	if(file_exists("resources/images/".$status.".gif")&&$displayStatusImages){
		return '<img src="resources/images/'.$status.'.gif" alt="'.getHttpCode($status).'">';
	} else return $status;
}

function queryWithPage($query, $offset){
	global $pageLimit;
	$limit = $pageLimit;
	if($offset=="all"){}
	else if(!isset($offset)||$offset==0){$query = $query." limit ".$limit;}
	else {
		$l_offset = $offset*$limit;		    
		$query = $query." limit ".$l_offset.",".$limit;
	}	    
	return mysql_query($query);
}

function totalResults($query){
	global $dbs;
	$num_result = mysql_query($query);
	//@mysql_free_result($num_result);
	return mysql_num_rows($num_result); 
}

function showPageNav($offset, $query, $args){
	global $dbs, $pageLimit;
	$limit = $pageLimit;
	$link = '<a href="'.$PHP_SELF.'?dbs='.$dbs;
	
	$next = "&nbsp;";
	$previous = "&nbsp;";
	$allList = $link."&offset=0".$args."\">Less</a>";
	
	if ($offset != "all") {		    
		$num_p = totalResults($query);		    
		$p_offset = $offset-1;
		if ($offset>0){
			$previous =  $link."&offset=".$p_offset.$args."\"><img src=\"resources/images/previous.gif\" alt=\"Previous\" border=0 /></a>";
		}
		$n_offset = $offset+1;
		$allList =  $link."&offset=all".$args."\"> All Results ($num_p)</a>";		    
		if (($offset*$limit) < $num_p && ($offset*$limit+$limit) < $num_p){
			$next = $link."&offset=".$n_offset.$args."\"><img src=\"resources/images/next.gif\" alt=\"Next\" border=0 /></a>";
		}
		
	} 
	print "<center><table border=0 width=160><tr><td align=left width=20>$previous</td><td align=center>$allList</td><td align=right width=20>$next</td></tr></table></center>";	    
}

function getUniqueIp(){
	global $dbs;
	$aQuery = "select distinct(remote_host) from ".mysql_Escape_string($dbs).sensibleFilter(1).customFilter(1);
	$tempResults = mysql_query($aQuery);
	$tempResult = mysql_num_rows($tempResults);
	return $tempResult;
}

function customFilterOn(){
	global $customFilter, $cf;
	return ($customFilter && is_null($cf));
}

function customFilter($where){
	global $customFilters;
	
	$listSize = sizeof($customFilters);
	
	if(customFilterOn()){
		$filterString = "";
		for($i=0; $i < $listSize; $i=$i+2){
			if(strlen($filterString) > 2){
				$filterString .= " and ";
			}
			$filterString .= " ".$customFilters[$i]." not like '".$customFilters[($i+1)]."'";
		}
	}
	
	if(strlen($filterString)>2){
		$filterString=" and ".$filterString;
	}
	
	return str_replace("*", "%", $filterString);
}

function sensibleOn(){
	global $sFilter, $sf;
	return ($sFilter && is_null($sf));
}


function sensibleFilter($where){
	global $sFilter, $level1, $level2, $level3, $sFilterLevel, $sf;
	if(sensibleOn()){
		$filterString = "";
		switch($sFilterLevel){
			case 3:
				$filterString .= levelFilter($level3, null);
			case 2:
				if(strlen($filterString) > 2){
					$filterString.=" and ";
				}
				$filterString .= levelFilter($level2);
			case 1:
				if(strlen($filterString) > 2){
					$filterString.=" and ";
				}

				$filterString .= levelFilter($level1);
		}
	}	
	if($filterString && $where==1){
		$filterString = " where ".$filterString;
	} else if($filterString && $where==2){
		$filterString = " and ".$filterString;
	}
	return $filterString;
}

function levelFilter($array){
	$listSize = sizeof($array);
	if($listSize == 1){
		$returnValue="request_uri not like '".current($array)."'";
	} else{
		for($i=0; $i <$listSize; $i++ ){
			$item=$array[$i];
			$returnValue.="request_uri not like '".$item."'";
			if(next($array)){
				$returnValue.=" and ";
			}
		}
	}
	return str_replace("*", "%", $returnValue);
}

function getBrowser($remoteHost){
	global $dbs;
	$query = "select distinct(agent) from ".$dbs." where remote_host='".$remoteHost."'";
	$results = mysql_query($query);
	$number = mysql_num_rows($results);
	if($number > 1){
		$i=0;
		while($item = mysql_fetch_row($results)){
			$array[$i] = $item[0];
			$i++;	
		}
	} else{
		$result = mysql_fetch_row($results);
		$array[0] = $result[0];
	}
	return $array;	
}

function getValidTables(){
	global $tableDetect, $accessTables;
	
	switch(strtolower($tableDetect)){
		case "auto":
			return getAllTablesMatchingSchema();
			break;
		case "manual":
			return $accessTables;
			break;
		case "none":
			return getAllTablesAsArray();
			break;
		default :
			return array();
			break;
	}
}

function getAllTablesAsArray(){
	global $database;
	$tbls = mysql_list_tables($database);
	$returnArray = array();
	
	while($list=mysql_fetch_array($tbls)){
		array_push($returnArray, $list[0]);		
	}	
	return $returnArray;
}

function getAllTablesMatchingSchema(){
	global $database,$requiredFields;
	
	$returnArray = array();
	$arrayList = getAllTablesAsArray();	
		
	while(list($key, $value)=each($arrayList)){
		$results = mysql_query("desc $value");
		$matchCounter=0;
		
		while($item = mysql_fetch_array($results)){			
			if(in_array($item[0], $requiredFields)){
				$matchCounter++;				
			}			
		}
		reset($requiredFields);
				
		if($matchCounter==sizeof($requiredFields)){
			array_push($returnArray, $value);
		}
	}
	return $returnArray;
}

function betweenToday($where){
	global $timeIs24;
	$endDate = mktime();
	if($timeIs24){
		$startDate = strtotime("-24 hours 1 second");
	} else {
		$startDate = strtotime("00:00:01 today");
	}
	
	if($where){
		$where = "where ";
	} else{
		$where = " and ";
	}
	return $where."time_stamp >= $startDate and time_stamp <= $endDate ";
}

function betweenYesterday($where){
	global $timeIs24;
	$endDate = mktime();	
	if($timeIs24){
		$startDate = strtotime("-48 hours");
		$endDate = strtotime("+24 hours",$startDate);
	} else {
		$startDate = strtotime("00:00:00 yesterday");
		$endDate = strtotime("00:00:00 today");
	}
	
	if($where){
		$where = "where ";
	} else{
		$where = " and ";
	}
	return $where."time_stamp >= $startDate and time_stamp <= $endDate ";
}
?>
Return current item: logViewSQL