#!/usr/bin/perl

#<ENC>57</ENC>

package ps_ppc;
 
########################################################################
#
# COPYRIGHT NOTICE:
#
# Copyright 2008 FocalMedia.Net All Rights Reserved.
#
# Selling the code for this program without prior written consent 
# from FocalMedia.Net is expressly forbidden. You may not 
# redistribute this program in any shape or form.
# 
# This program is distributed "as is" and without warranty of any
# kind, either express or implied. In no event shall the liability 
# of FocalMedia.Net for any damages, losses and/or causes of action 
# exceed the total amount paid by the user for this software.
#
########################################################################
#
# PPC FEED PLUGIN MODULE
#

#### EDIT HERE -- FOR WINDOWS/IIS BASED INSTALLATIONS ONLY #######

$config_cgi = "config.cgi"; ## <-- CHANGE THIS LINE TO THE FULL SERVER PATH TO config.cgi

# THE PATH ON A WINDOWS INSTALLATION WILL LOOK SOMETHING LIKE THIS:
# $config_cgi = "c:/inetpub/webpub/cgi-bin/pseek/config.cgi";

#### DO NOT CHANGE ANYTHING BELOW THIS LINE #################

	use CGI qw(param);
	use CGI::Carp qw(fatalsToBrowser);
	use DBI; 
	use LWP::Simple;

require Exporter;
use vars qw/@ISA @EXPORT @EXPORT_OK $copyright $prgname/;

@ISA = qw(Exporter);

@EXPORT = qw(do_kanoodle
			do_searchfeed
			get_setup2
			get_setup_line3
			get_file_contents
			ppc_settings); 

@EXPORT_OK = qw($all_kan_links
				$all_searchfeed_links
				$return_val
				$thefile);


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

sub do_kanoodle
{	
	my ($keywords, $number,$which_search)  = @_;
	&get_setup2;
	$kanoodle_settings = &ppc_settings;
	$q = CGI->new;
	$kanoodle_enalbled =				$kanoodle_settings->{kanoodle_enalbled};
	$aff_id =							$kanoodle_settings->{aff_id};
	$numresults =						$kanoodle_settings->{numresults};
	$sup_runof =						$kanoodle_settings->{sup_runof};
	$backfill =							$kanoodle_settings->{backfill};
	$adultfilter =						$kanoodle_settings->{adultfilter};
	$adultonly =						$kanoodle_settings->{adultonly};
	$kanoodle_template_name =			$kanoodle_settings->{kanoodle_template_name};
	$num_noresult_results = 				$kanoodle_settings->{num_noresult_results};
	$ip = $ENV{"REMOTE_ADDR"};
	if ($which_search == 10) {$numresults = $num_noresult_results;}
	
	if ($number eq "") 
	{
		$number = 0;
	} 
	else
	{
		$number = $number / 10;
		$number = $numresults * $number;
	}
		$pass_string = "http://partner1.kanoodle.com/cgi-bin/partner.cgi?query=$keywords&searchip=$ip&id=$aff_id&numresults=$numresults&first=$number&format=xml1";
		
		#print "-> $pass_string <BR>";
		
		if ($sup_runof == 11) {$pass_string .= "&noros=1";}
		if ($backfill == 11) {$pass_string .= "&backfill=1";}
		if ($adultfilter == 10) {$pass_string .= "&adultfilter=1";}
		if ($adultonly == 11) {$pass_string .= "&adultonly=1";}
	
	#print "===>>PassString is: $pass_string<BR>";
	
		$txt = get("$pass_string");
	
		if ($txt eq "")
		{
			$txt = qq[<p><font color="#FF0000" face="$font" size="3">Remote server could not be reached. $heading will not be displayed.</font></p>];
		}
		else
		{
			$recorder = 0;
			$tag = "record";
			$tag1 = "title";
			$tag2 = "url";
			$tag3 = "description";
			$tag4 = "bidprice";
			$tag5 = "clickurl";
			@records = split(/<\s*\/$tag\s*>/i, $txt);
			$nr_returned = @records; $nr_returned = $nr_returned - 1;
			$to_escape_string = "<?xml version=\"1.0\" encoding=\"windows-1252\"?>";                                                               
			$to_escape_string1 = "<record>";
			$to_escape_string2 = "<title><![CDATA[";
			$to_escape_string3 = "<url><![CDATA[";
			$to_escape_string4 = "<description><![CDATA[";
			$to_escape_string5 = "<bidprice><![CDATA[";
			$to_escape_string6 = "<clickurl><![CDATA[";
			$to_escape_string7 = "]]>";
			$to_escape_string = quotemeta($to_escape_string);
			$to_escape_string1 = quotemeta($to_escape_string1);
			$to_escape_string2 = quotemeta($to_escape_string2);
			$to_escape_string3 = quotemeta($to_escape_string3);
			$to_escape_string4 = quotemeta($to_escape_string4);
			$to_escape_string5 = quotemeta($to_escape_string5);
			$to_escape_string6 = quotemeta($to_escape_string6);
			$to_escape_string7 = quotemeta($to_escape_string7);
			$records[$recorder] =~ s/$to_escape_string//ig;
			while ($recorder < $numresults)
			{
				$kan_search_links = &get_file_contents("$data_dir/$kanoodle_template_name");
				@title = "";
				@url = "";
				@description = "";
				@bidprice = "";
				@clickurl = "";
				$records[$recorder] =~ s/\n//ig;
				if ($recorder == 0)
				{
					@title = split(/<\s*\/$tag1\s*>/i, $records[$recorder]);
					@url = split(/<\s*\/$tag2\s*>/i, $title[$recorder+1]);
					@description = split(/<\s*\/$tag3\s*>/i, $url[$recorder+1]);
					@bidprice = split(/<\s*\/$tag4\s*>/i, $description[$recorder+1]);
					@clickurl = split(/<\s*\/$tag5\s*>/i, $bidprice[$recorder+1]);
				}
				else
				{
					if ($recorder == 1) {$count = 0;}
					if ($recorder == 2) {$count = 1;}
					if ($recorder == 3) {$count = 2;}
					if ($recorder == 4) {$count = 3;}
					if ($recorder == 5) {$count = 4;}
					if ($recorder == 6) {$count = 5;}
					if ($recorder == 7) {$count = 6;}
					if ($recorder == 8) {$count = 7;}
					if ($recorder == 9) {$count = 8;}
					if ($recorder == 10) {$count = 9;}
					if ($recorder == 11) {$count = 10;}
					if ($recorder == 12) {$count = 11;}
					if ($recorder == 13) {$count = 12;}
					if ($recorder == 14) {$count = 13;}
					if ($recorder == 15) {$count = 14;}
					if ($recorder == 16) {$count = 15;}
					if ($recorder == 17) {$count = 16;}
					if ($recorder == 18) {$count = 17;}
					if ($recorder == 19) {$count = 18;}
					if ($recorder == 20) {$count = 19;}
					@title = split(/<\s*\/$tag1\s*>/i, $records[$recorder]);
					@url = split(/<\s*\/$tag2\s*>/i, $title[$recorder-$count]);
					@description = split(/<\s*\/$tag3\s*>/i, $url[$recorder-$count]);
					@bidprice = split(/<\s*\/$tag4\s*>/i, $description[$recorder-$count]);
					@clickurl = split(/<\s*\/$tag5\s*>/i, $bidprice[$recorder-$count]);
				}
				
				$title[0] =~ s/\n//ig;
				$url[0] =~ s/\n//ig;
				$description[0] =~ s/\n//ig;
				$bidprice[0] =~ s/\n//ig;
				$clickurl[0] =~ s/\n//ig;
				$title[0] =~ s/$to_escape_string1//ig;
				$title[0] =~ s/$to_escape_string2//ig;
				$url[0] =~ s/$to_escape_string3//ig;
				$description[0] =~ s/$to_escape_string4//ig;
				$bidprice[0] =~ s/$to_escape_string5//ig;
				$clickurl[0] =~ s/$to_escape_string6//ig;
				$title[0] =~ s/$to_escape_string7//ig;
				$url[0] =~ s/$to_escape_string7//ig;
				$description[0] =~ s/$to_escape_string7//ig;
				$bidprice[0] =~ s/$to_escape_string7//ig;
				$clickurl[0] =~ s/$to_escape_string7//ig;		
				$title_link = qq[<a href="$clickurl[0]" target="_blank">$title[0]</a>];
				if (($title[0] ne "") && ($description[0] ne ""))
				{
					$kan_search_links =~ s/!!kan_title_with_clickurl!!/$title_link/g;
					$kan_search_links =~ s/!!kan_description!!/$description[0]/g;
					$kan_search_links =~ s/!!kan_url!!/$url[0]/g;
					$kan_search_links =~ s/!!kan_bidprice!!/$bidprice[0]/g;
					$kan_search_links =~ s/!!kan_clickurl!!/$clickurl[0]/g;
					$kan_search_links =~ s/!!kan_title!!/$title[0]/g;			
					$all_kan_links .= $kan_search_links;
				}
				$recorder++;
			}
		}
		
		### FETCH BACKFILL LINKS
		if ($nr_returned != $numresults)
			{
			$stillneeded = ($numresults - $nr_returned);
			$all_kan_links = $all_kan_links . &get_backfills($stillneeded, "k");
			}
		
		return ($all_kan_links);
}







sub do_searchfeed
{	
	my ($keywords, $number,$which_search)  = @_;
	
	&get_setup2;
	$kanoodle_settings = &ppc_settings;
	$q = CGI->new;
	
	$searchfeed_enalbled =				$kanoodle_settings->{searchfeed_enalbled};
	$searchfeed_numresults =				$kanoodle_settings->{searchfeed_numresults};
	$track_id =						$kanoodle_settings->{track_id};
	$account_id =						$kanoodle_settings->{account_id};
	$searchfeed_template_name =			$kanoodle_settings->{searchfeed_template_name};
	$searchfeed_num_noresult_results = 		$kanoodle_settings->{searchfeed_num_noresult_results};
	$ip = $ENV{"REMOTE_ADDR"};
	if ($which_search == 10) {$searchfeed_numresults = $searchfeed_num_noresult_results;}
	
	if ($number eq "") 
	{
		$number = 0;
	} 
	else
	{
		$number = $number / 10;
		$number = $number + 1;
	}
		$pass_string = "http://www.searchfeed.com/rd/feed/TextFeed.jsp?trackID=$track_id&pID=$account_id&cat=$keywords&nl=$searchfeed_numresults&page=$number&ip=$ip";
		$txt = get("$pass_string");
		if ($txt eq "")
		{
			$txt = qq[<p>&nbsp;</p>];
		}
		else
		{
			@records = split(/\|/,$txt);
			$my_count = 0;
			@title = "";
			@url = "";
			@description = "";
			@bid = "";
			@uri = "";
			foreach $record_detail (@records)
			{
				if ($record_detail =~ m/Title/ig) {splice (@title, -1, 0, $records[$my_count+2]);}
				if ($record_detail =~ m/URL/ig) {splice (@url, -1, 0, $records[$my_count+2]);}
				if ($record_detail =~ m/URI/ig) {splice (@uri, -1, 0, $records[$my_count+2]);}
				if ($record_detail =~ m/Description/ig) {splice (@description, -1, 0, $records[$my_count+2]);}
				if ($record_detail =~ m/Bid/ig) {splice (@bid, -1, 0, $records[$my_count+2]);}
				$my_count++;
			}
			
			$my_count = 0; $ppclcount = 0;
			while ($my_count < $searchfeed_numresults)
			{
				$searchfeed_search_links = &get_file_contents("$data_dir/$searchfeed_template_name");
				$title_link = qq[<a href="$uri[$my_count]" target="_blank">$title[$my_count]</a>];
				if (($title[$my_count] ne "") && ($description[$my_count] ne ""))
				{
					$searchfeed_search_links =~ s/!!searchfeed_title_with_clickurl!!/$title_link/g;
					$searchfeed_search_links =~ s/!!searchfeed_description!!/$description[$my_count]/g;
					$searchfeed_search_links =~ s/!!searchfeed_url!!/$url[$my_count]/g;
					$searchfeed_search_links =~ s/!!searchfeed_bidprice!!/$bid[$my_count]/g;
					$searchfeed_search_links =~ s/!!searchfeed_clickurl!!/$uri[$my_count]/g;
					$searchfeed_search_links =~ s/!!searchfeed_title!!/$title[$my_count]/g;			
					$all_searchfeed_links .= $searchfeed_search_links;
					$ppclcount++;
				}
				$my_count++;
			}
			
		}


		### FETCH BACKFILL LINKS
		if ($ppclcount != $searchfeed_numresults)
			{
			$stillneeded = ($searchfeed_numresults - $ppclcount);
			$all_searchfeed_links = $all_searchfeed_links . &get_backfills($stillneeded, "s");
			}

#print "returned: $searchfeed_numresults -  $ppclcount : needed: $stillneeded<BR>";

return ($all_searchfeed_links);

}





sub get_setup2
{
my ($line, $r);

my $exists = (-e "$config_cgi");
if ($exists > 0)
	{
	open (STP, "$config_cgi");
		while (defined($line=<STP>))
			{
			if ($line =~ m/#/g)
				{
				$r = pos($line);
				$line = substr($line, 0, $r - 1);
				}
			
				$line =~ s/\n//g;
	
				if ($line =~ /^DATA_DIR/){$data_dir = &get_setup_line3($line, DATA_DIR);}
				if ($line =~ /^DB_NAME/){$db_name = &get_setup_line3($line, DB_NAME);}
				if ($line =~ /^DB_USERNAME/){$db_username = &get_setup_line3($line, DB_USERNAME);}
				if ($line =~ /^DB_PASSWORD/){$db_password = &get_setup_line3($line, DB_PASSWORD);}
				if ($line =~ /^MYSQL_HOSTNAME/){$mysql_hostname = &get_setup_line3($line, MYSQL_HOSTNAME);}
				if ($line =~ /^MYSQL_PORT/){$mysql_port = &get_setup_line3($line, MYSQL_PORT);}				
			}
	close (STP);
	
	}
}

sub get_setup_line3
{
my ($setup_line, $setup_var) = @_;
$crit = "\"";
$setup_line =~ m/$crit/g;
$r1 = pos($setup_line);
$setup_line =~ m/$crit/g;
$r2 = pos($setup_line);
$setup_line = substr($setup_line, $r1, ($r2 - $r1 - 1));
$return_val = $setup_line;
return ($return_val);
}

sub get_file_contents
{

my ($filename) = @_;
my ($filesize, $filesize, $thefile);

if ((-e "$filename") > 0) 
	{
$filesize = (-s "$filename");
open (TFILECNTS, "$filename") || die "$filename";
	read(TFILECNTS,$thefile,$filesize);
close (TFILECNTS);
	}

return ($thefile);

}


sub ppc_settings
{
	&get_setup2;
	my $this = {};

	$scnts = &get_file_contents("$data_dir/ppc_settingsm.dat");
	@all_obj_items = split (/\n/, $scnts);

	foreach $obji (@all_obj_items)
	{
		($obj_prp, $obj_val) = split (/=/, $obji);
		$this->{$obj_prp} = $obj_val;
	}
	bless $this;
	return $this;
}



sub get_backfills
{
my ($nrlinks, $kanoodle_searchfeed) = @_;

#$kanoodle_searchfeed k pr s (kanoodle/searchfeed)

&get_setup2;
			
			if ($mysql_hostname eq ""){$dsn = "DBI:mysql:$db_name";}else{$dsn = "DBI:mysql:$db_name:$mysql_hostname:$mysql_port";}
			$dbh = DBI->connect($dsn, $db_username, $db_password);
			if ( !defined $dbh ) {die "Cannot connect to MySQL server: $DBI::errstr\n"; }
			
			$wsql = "select * from ppcfeed_bf WHERE kansfeed = '$kanoodle_searchfeed' order by rand() limit $nrlinks";
			$sth = $dbh->prepare($wsql);
			$sth->execute;
			#$serror = ""; $serror = $sth->errstr; if ($serror ne "") {die "SQL Syntax Error: $serror \n From: $wsql";}
			$rows = $sth->rows();
				
			if ($kanoodle_searchfeed eq "k")
				{
				$kan_search_links = &get_file_contents("$data_dir/$kanoodle_template_name");
				}
				else
				{
				$kan_search_links = &get_file_contents("$data_dir/$searchfeed_template_name");
				}
			
			while ( @row = $sth->fetchrow() )
					{
					$tmpslnk = $kan_search_links;
					$titleclick = qq[<a href="$row[3]">$row[1]</a>];

					if ($kanoodle_searchfeed eq "k")
						{
						$tmpslnk =~ s/!!kan_title_with_clickurl!!/$titleclick/g;
						$tmpslnk =~ s/!!kan_description!!/$row[2]/g;
						$tmpslnk =~ s/!!kan_url!!/$row[3]/g;
						$tmpslnk =~ s/!!kan_bidprice!!//g;
						$tmpslnk =~ s/!!kan_clickurl!!//g;
						$tmpslnk =~ s/!!kan_title!!//g;
						}
						else
						{
						$tmpslnk =~ s/!!searchfeed_title_with_clickurl!!/$titleclick/g;
						$tmpslnk =~ s/!!searchfeed_description!!/$row[2]/g;
						$tmpslnk =~ s/!!searchfeed_url!!/$row[3]/g;
						$tmpslnk =~ s/!!searchfeed_bidprice!!//g;
						$tmpslnk =~ s/!!searchfeed_clickurl!!//g;
						$tmpslnk =~ s/!!searchfeed_title!!//g;
						}
					
					$all_ret_links = $all_ret_links . $tmpslnk;
					}
			
			$sth->finish;
			$dbh->disconnect;

return ($all_ret_links);
}




1;