<?php
#########################################################################################################
#  runq.php
#
#  Copyright (C) Softnik Technologies. All rights reserved.
#
#  Process the Lookup Queue and Do Automatic Domain Lookups
#
#########################################################################################################

require_once("config.check.php");
require_once("../../config.php");
require_once("base.php");
require_once("lookup.php");
require_once("version.php");

/////////////////////////////////////////////////////////////////////////////////////////////////////////

# Ajax script. 
# Check authentication and quit with http error code if auth fails.

session_start();
check_authentication_error_quit();
doSessionSwipeIn();

#########################################################################################################

connect_to_mysql_and_select_db();

#########################################################################################################

if(isset($_REQUEST['q']))
{
	global $db_config_lookup_queue_table;
	
	$sqlCount = "SELECT COUNT(*) AS count from " . $db_config_lookup_queue_table;
	$countResult = mysql_query( $sqlCount ) or die("Couldn't execute your query.".mysql_error());
	$queue = mysql_fetch_array($countResult);  
	
	$sqlCount = "SELECT COUNT(*) AS count from " . $db_config_lookup_queue_table . " where type=" . LOOKUP_IP_WHOIS;
	$countResult = mysql_query( $sqlCount ) or die("Couldn't execute your query.".mysql_error());
	$ipwcount = mysql_fetch_array($countResult);  
	
	$sqlCount = "SELECT COUNT(*) AS count from " . $db_config_lookup_queue_table . " where type=" . LOOKUP_HTTP_HOMEPAGE;
	$countResult = mysql_query( $sqlCount ) or die("Couldn't execute your query.".mysql_error());
	$httpcount = mysql_fetch_array($countResult);  
	
	$sqlCount = "SELECT COUNT(*) AS count from " . $db_config_lookup_queue_table . " where type=" . LOOKUP_MX_RECORDS;
	$countResult = mysql_query( $sqlCount ) or die("Couldn't execute your query.".mysql_error());
	$mxcount = mysql_fetch_array($countResult);  
	
	$sqlCount = "SELECT COUNT(*) AS count from " . $db_config_lookup_queue_table . " where type=" . LOOKUP_DOMAIN_WHOIS;
	$countResult = mysql_query( $sqlCount ) or die("Couldn't execute your query.".mysql_error());
	$dwcount = mysql_fetch_array($countResult);  
	
	$sqlCount = "SELECT COUNT(*) AS count from " . $db_config_lookup_queue_table . " where type=" . LOOKUP_SEC_DOMAIN_WHOIS;
	$countResult = mysql_query( $sqlCount ) or die("Couldn't execute your query.".mysql_error());
	$sdwcount = mysql_fetch_array($countResult);  
	
	$sqlCount = "SELECT COUNT(*) AS count from " . $db_config_lookup_queue_table . " where type=" . LOOKUP_PING_RESPONSE;
	$countResult = mysql_query( $sqlCount ) or die("Couldn't execute your query.".mysql_error());
	$pingcount = mysql_fetch_array($countResult); 
	
	$sqlCount = "SELECT COUNT(*) AS count from " . $db_config_lookup_queue_table . " where type=" . LOOKUP_ALEXA_DATA;
	$countResult = mysql_query( $sqlCount ) or die("Couldn't execute your query.".mysql_error());
	$alexacount = mysql_fetch_array($countResult);  
	
	$sqlCount = "SELECT COUNT(*) AS count from " . $db_config_lookup_queue_table . " where type=" . LOOKUP_GOOGLE_PR;
	$countResult = mysql_query( $sqlCount ) or die("Couldn't execute your query.".mysql_error());
	$gprcount = mysql_fetch_array($countResult);  
	
	$sqlCount = "SELECT COUNT(*) AS count from " . $db_config_lookup_queue_table . " where type=" . LOOKUP_IP_ADDRESS;
	$countResult = mysql_query( $sqlCount ) or die("Couldn't execute your query.".mysql_error());
	$iplcount = mysql_fetch_array($countResult);  
	
	$sqlCount = "SELECT COUNT(*) AS count from " . $db_config_lookup_queue_table . " where type=" . LOOKUP_SSL_CERTS;
	$countResult = mysql_query( $sqlCount ) or die("Couldn't execute your query.".mysql_error());
	$sslcount = mysql_fetch_array($countResult); 
	
	$sqlCount = "SELECT COUNT(*) AS count from " . $db_config_lookup_queue_table . " where type=" . LOOKUP_SUB_DOMAINS;
	$countResult = mysql_query( $sqlCount ) or die("Couldn't execute your query.".mysql_error());
	$dnscount = mysql_fetch_array($countResult); 
	
	$sqlCount = "SELECT COUNT(*) AS count from " . $db_config_lookup_queue_table . " where type=" . LOOKUP_GOOGLE_INDEX;
	$countResult = mysql_query( $sqlCount ) or die("Couldn't execute your query.".mysql_error());
	$gicount = mysql_fetch_array($countResult); 
	
	$responce = new stdClass();
	$responce->status = "ok";
	$responce->server_time = date("Y-m-d H:i:s");
	
	$responce->queue_size = $queue['count'];
	$responce->ipw_size   = $ipwcount['count'];
	$responce->dw_size    = $dwcount['count'];
	$responce->ping_size  = $pingcount['count'];
	$responce->http_size  = $httpcount['count'];
	$responce->gpr_size   = $gprcount['count'];
	$responce->alexa_size = $alexacount['count'];
	$responce->mx_size    = $mxcount['count'];
	$responce->ip_size    = $iplcount['count'];
	$responce->sdw_size   = $sdwcount['count'];
	$responce->ssl_size   = $sslcount['count'];
	$responce->dns_size   = $dnscount['count'];
	$responce->gi_size    = $gicount['count'];
	
	$responce->last_queue_proc = get_queue_proc_timestamp();
	echo json_encode($responce);
	exit;
}

#########################################################################################################

?>
