#!/usr/bin/perl use DBI; use DBD::mysql; use URI::URL; use LWP::UserAgent; use CGI_Lite; #use Data::Dumper; # use CGI qw(:standard); #use Net::SMTP; #use utf8; # use feature 'unicode_strings'; my $dsn="DBI:mysql:database=MediaDB;host=38.121.233.148"; my $dsnusername='media'; my $dsnpassword='cyberalert'; my $dbh; my $SecondsBetweenRequest=5; connectDB(); my $cgi = new CGI_Lite; my $form = $cgi->parse_form_data; # $form->{"REQUEST_ClientIP"}= $ENV{"REMOTE_ADDR"}; $ClientIP= $ENV{"REMOTE_ADDR"}; $ServiceID= $form->{"serviceid"}; $checkresult=checkService($ServiceID,$ClientIP); if($checkresult eq "Y") { print "content-type: text/xml\n\n"; $logstring="IP:$ClientIP"; foreach $key (keys(%$form)) { $logstring.="|$key:".$form->{$key}; } logService($ServiceID,$logstring); $ua=new LWP::UserAgent; $RealFeedUrl="http://indexer.cyberalert.com/cgi-bin/texis/NewsFeed/xml.xml"; $result=$ua->post($RealFeedUrl,$form); print $result->{_content}; }elsif($checkresult eq "A") { print "content-type: text/xml\n\n"; $logstring="IP:$ClientIP"; foreach $key (keys(%$form)) { $logstring.="|$key:".$form->{$key}; } logService($ServiceID,$logstring); if ( $form->{"endtime"} eq "") { $form->{"endtime"}= "2011-07-01 00:00:00"; } # if ( $form->{"starttime"} eq "") { $form->{"starttime"}= "2010-07-01 00:00:00"; } $ua=new LWP::UserAgent; $RealFeedUrl="http://indexer.cyberalert.com/cgi-bin/texis/ArchiveFeed/xml.xml"; $result=$ua->post($RealFeedUrl,$form); print $result->{_content}; }else { print "content-type: text/html\n\n"; print ShowMessage($checkresult); } exit; ## testDB(); sub HideMessage { my ($message)=@_; return ""; } sub ShowMessage { my ($message)=@_; return "Request Failed$message"; } sub checkService { my ($serviceid,$ip_address)=@_; if ($serviceid eq "BoardReader2" or $serviceid eq "CYB_Archive") { return "A"; } my ($verified_serviceid) = $dbh->selectrow_array( "select serviceid from feedadm_clients where serviceid='$serviceid' and ip_address='$ip_address'"); if ($verified_serviceid eq $serviceid) { if (my ($last_request_time,$current_time)=$dbh->selectrow_array( "select recorddate,sysdate() from feed_requests where serviceid='$serviceid' and recorddate>timestampadd(second,-$SecondsBetweenRequest,sysdate()) order by logid desc limit 1") ) { return "Too frequent queries: $last_request_time $current_time\n"; }else{ return "Y"; } }else{ return "Invalid request -- wrong service ID ($serviceid) or from wrong IP($ip_address).\n"; } } sub logService { my ($serviceid,$logstring)=@_; $dbh->do(" insert into feed_requests (serviceid, url) values ('$serviceid','$logstring')"); # my ($logid)=$dbh->selectrow_array("select LAST_INSERT_ID()"); # $dbh->do(" insert into feedadm_request_log_params (logid,name,value) values ($logid,?,?) # return $sitelogid; } sub connectDB { if( not $dbh=DBI->connect($dsn,$dsnusername,$dsnpassword) ) { die "Can not connect to MediaDB"; } } sub testDB { my $sqlcmd="select SiteID,SiteName,Url,Status from MediaSiteList where SiteID=701"; my ($siteid,$sitename,$siteurl,$sitestatus)=$dbh->selectrow_array($sqlcmd); print " ($siteid,$sitename,$siteurl,$sitestatus)\n"; }