<?php
/***************************************************************************
* serveads.php
* -------------------
* begin : mier, ago 29, 2007
* copyright : (C)
* email : hide@address.com
* Desc : Ads serving script
*
*
***************************************************************************/
/*
It is invoked in this form
<script language="JavaScript" src="http://serversDNS/serveads.php?wid=###&zone=###"> </script>
Where:
wid = WebSite Id
type = Banner Type
*/
/* Timer */
$time_start = microtime(true);
define('IN_ADSERVER', true);
//Include only what's needed
include_once("./inc/config.inc.php");
//Ips
include_once("ips/config.php");
include_once("./inc/bdconn.inc.php");
//This is repeated here to reduce memory consuption
function record_Debugs($path_Main){
$filename = $path_Main."/DEBUGFILE";
// Let's make sure the file exists and that it is writable first.
if(is_writable($filename)) {
if ($theFile = fopen($filename, 'a')) {
// Get all the data that we want
$dat = getrusage();
$content = sprintf("Time: %s | Memory: %s Kb| Time Used (Ms) %s | Usage: %s \n", date("Y-M-d H:i:s", time()), memory_get_usage() / 1000, $dat["ru_utime.tv_usec"], basename($_SERVER['SCRIPT_FILENAME']));
fwrite($theFile, $content);
fclose($theFile);
}
}
}
//This is used get ip specs, you can include this entire file or just copy paste this function where ever you need to use it. Just remember the credits ;-)
//This is basically all you will be calling from your application, the rest is used to upload the ips database
function get_Ip_Info($ip,
$db_Specs
){
//Convert ip address
$new_Ip = explode(".", $ip);
$decimal = $new_Ip[3];
$decimal += $new_Ip[2] * 256;
$decimal += $new_Ip[1] * 256 * 256;
$decimal += $new_Ip[0] * 256 * 256 * 256;
// 1.2.3.4 = 4 + (3 * 256) + (2 * 256 * 256) + (1 * 256 * 256 * 256)
// # is 4 + 768 + 13,1072 + 16,777,216 = 16,909,060
$q = "
SELECT ".$db_Specs['ips_Name'].".*,
".$db_Specs['countries_Name'].".country, ".$db_Specs['countries_Name'].".cntry
FROM ".$db_Specs['ips_Name']."
INNER JOIN ".$db_Specs['countries_Name']." ON ".$db_Specs['countries_Name'].".ctry = ".$db_Specs['ips_Name'].".ctry
WHERE ip_From <= ".$decimal." AND ip_To >= ".$decimal."
";
$q_Ip_Data = mysql_query($q) or die("Unable to Get Data: " . mysql_error());
if(mysql_affected_rows() > 0){
$row = mysql_fetch_array($q_Ip_Data, MYSQL_ASSOC);
$ip_Data = array(
"lower_Bound" => $row['ip_From'],
"upper_Bound" => $row['ip_To'],
"ctry" => $row['ctry'],
"registry" => $row['registry'],
"cntry" => $row['cntry'],
"country" => $row['country'],
"decimal" => $decimal
);
}
else{
$ip_Data = array(
"lower_Bound" => 0,
"upper_Bound" => 0,
"ctry" => 0,
"registry" => 0,
"cntry" => 0,
"country" => 0,
"decimal" => 0
);
}
return $ip_Data;
}
//Opens db connetion
bdConnect($db_Host, $db_User, $db_Pwd, $db_Db); //Opens the database connection
/* Fraud Prevention, it is not finished, not even sure that it is working, but it will at some point */
if(strpos($_SERVER['SERVER_PROTOCOL'], "HTTP") == 0){
$s_This = "http://".$_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'];
//exit;
}
else
exit;
/* Required */
$s_Fraud = 0;
$s_And_Dont = "";
$i_Time_Viewed = time();
//Fraud Prevention
/*
browscap required, add code to check first if this is available. Advise this somewehere
$ua = get_browser ();
if ($ua->browser == "Lynx" ) {
$s_Fraud = 0;
}
*/
/*else if($_SERVER['REMOTE_ADDR'] == $_SERVER['SERVER_ADDR']){
$s_Fraud = 2;
}*/
//Confirm wid and type and site
if(!isset($_GET['wid']) || $_GET['wid'] == ""
|| !isset($_GET['zone']) || $_GET['zone'] == ""
|| !isset($_GET['site']) || $_GET['site'] == ""
){
$s_Fraud = 1;
}
else {
$i_Time = date("H", time());
//Get Website Category and restrictions, will asume that if it has restrictions, it is because it can.
$q = "
SELECT id_Cat_Is, id_Cat_Dont
FROM ".$db_Pre."websites
WHERE id_Website = ".$_GET['site']."
";
$q_Website_Data = mysql_query($q) or die("Unable to Get Website specifications: " . mysql_error());
$row = mysql_fetch_array($q_Website_Data, MYSQL_ASSOC);
$s_Website_Cat = $row['id_Cat_Is'];
$a_Website_Dont = ($row['id_Cat_Dont'] != "" ? explode("::",$row['id_Cat_Dont']) : "" );
//Create select restrictions, but it must have at least one
if($a_Website_Dont != ""){
for($i = 0; $i < count($a_Website_Dont); $i++){
$s_And_Cat_Dont .= " AND ".$db_Pre."ads.id_Cat_Is != " .$a_Website_Dont[$i];
}
}
//Time limits
$s_And_Time_Dont .= " AND ".$db_Pre."ads.limit_View_Start < " .$i_Time." AND ".$db_Pre."ads.limit_View_End > " .$i_Time;
//Get country
$country = get_Ip_Info($_SERVER['REMOTE_ADDR'], $db_Specs);
$where_Country = "AND (".$db_Pre."ads.countries = ''";
$where_Country .= " OR ( ".$db_Pre."ads.countries LIKE '%".$country['ctry']."%'
AND ( ".$db_Pre."ads.countries NOT LIKE '0:%' OR ".$db_Pre."ads.countries LIKE '1:%')))";
$q = "
SELECT ".$db_Pre."ads.id_Ads, ".$db_Pre."ads.data, ".$db_Pre."ads.alt_Text, ".$db_Pre."ads.title, ".$db_Pre."ads.link, ".$db_Pre."ads.id_User,
".$db_Pre."zones.text
FROM ".$db_Pre."ads
INNER JOIN ".$db_Pre."credits ON ".$db_Pre."credits.id_Zone = ".$db_Pre."ads.id_Zone
INNER JOIN ".$db_Pre."zones ON ".$db_Pre."zones.id_Zone = ".$db_Pre."ads.id_Zone
WHERE ".$db_Pre."ads.id_Zone = ".$_GET['zone']."
AND ".$db_Pre."ads.link != '".$s_This."'
AND ".$db_Pre."ads.active = 1
".$where_Country."
".$s_And_Cat_Dont."
".$s_And_Time_Dont."
AND ".$db_Pre."credits.credits != 0
AND (".$db_Pre."ads.id_Cats_Shown = ''
OR ".$db_Pre."ads.id_Cats_Shown LIKE '%:".$s_Website_Cat.":%')
ORDER BY RAND()
LIMIT 1
";
$q_Ad = mysql_query($q) or die("Unable to Get Ad: " . mysql_error());
if(mysql_num_rows($q_Ad) > 0){
//Output the ad
$row = mysql_fetch_array($q_Ad, MYSQL_ASSOC);
/* Text ad */
if($row['text'] == 1){
echo $s_Ad_Details = "<div style='font-size: 11px;'><a href='logads.php?idad=" . $row['id_Ads'] . "&t=".$i_Time_Viewed."&s=".$_GET['wid']."' target='".$_GET['t']."'><b>" . $row['title'] . "</b></a>
<p>".$row['data']."</p></div>
";
}
/* Image ad */
else{
$this_Filename = $s_path_upload . "/" . $row['id_User'] . "/" . $row['data'];
if(file_exists($this_Filename)){
$s_file_Dimensions = getimagesize($this_Filename);
if(mime_Type("x-shockwave-flash", $s_file_Dimensions['mime']) == "flash"){
$img_Src = $web_address . $s_dir_upload . "/" . $row['id_User'] . "/" . $row['data'];
echo "
<a href=\"http://" . $_SERVER['SERVER_NAME'] . "logads.php?idad=" . $row['id_Ads'] . "&t=".$i_Time_Viewed."&s=".$_GET['wid']."\" target=\"".$_GET['t']."\">
<object width=\"".$s_file_Dimensions[0]."\" height=\"".$s_file_Dimensions[1]."\">
<param name=\"movie\" value=\"".$img_Src."\">
<embed src=\"".$img_Src."\" width=\"".$s_file_Dimensions[0]."\" height=\"".$s_file_Dimensions[1]."\" type=\"application/x-shockwave-flash\">
</embed>
</object>
</a>
";
}
else if(mime_Type($allowed_File_Types, $s_file_Dimensions['mime']) == "img"){
echo $s_Ad_Details = "<a href='logads.php?idad=" . $row['id_Ads'] . "&t=".$i_Time_Viewed."&s=".$_GET['wid']."' target='".$_GET['t']."'><img src='". $web_address . $s_dir_upload . "/" . $row['id_User'] . "/" . $row['data'] . "' alt='".$row['alt_Text']."' title='".$row['title']."' border=0'></a>";
}
}
}
$s_id_User_Viewed = $row['id_User'];
$S_div = "||";
$s_Data_View = $_SERVER['HTTP_HOST'] . $S_div . $_SERVER['HTTP_USER_AGENT'] . $S_div . $_SERVER['REMOTE_ADDR'] . $S_div . $_SERVER['SERVER_ADDR'] . $S_div . $_SERVER['QUERY_STRING'];
/* Register the view */
$q = "
INSERT INTO ".$db_Pre."view_Log
(id_Ads, place, timestamp, clicked, data)
VALUES (".$row['id_Ads'].", '".$s_This."', ".$i_Time_Viewed.",0,'".$s_Data_View."')
";
$q_Register_View = mysql_query($q) or die("Unable to Register View" . mysql_error());
/* Ad the views */
$q = "
UPDATE ".$db_Pre."ads
SET views = views + 1
WHERE id_Ads = ".$row['id_Ads']."
";
$q_Add_View = mysql_query($q) or die("Unable to Add a View" . mysql_error());
/* Discount The View */
$q = "
UPDATE ".$db_Pre."credits
SET credits = credits - 1
WHERE id_Zone = ".$_GET['zone']."
AND id_User = ".$s_id_User_Viewed."
AND credits > 0
";
$q_Disc_View = mysql_query($q) or die("Unable to Discount View" . mysql_error());
/* Ad the views to the viewed */
$q = "SELECT view_Ratio
FROM ".$db_Pre."groups
INNER JOIN ".$db_Pre."users ON ".$db_Pre."users.id_Group = ".$db_Pre."groups.id_Group
WHERE ".$db_Pre."users.id_User = ".$_GET['wid']."
";
$q_View_Ratio = mysql_query($q) or die("Unable to Get View Ratio for user" . mysql_error());
$row = mysql_fetch_array($q_View_Ratio, MYSQL_ASSOC);
$q = "
UPDATE ".$db_Pre."credits
SET credits = credits + ".$row['view_Ratio']."
WHERE id_User = ".$_GET['wid']."
AND id_Zone = ".$_GET['zone']."
AND credits >= 0
";
$q_Add_View_Left = mysql_query($q) or die("Unable to Add Credits" . mysql_error());
}
else{
echo "<a href='$web_address' target='_parent'>".$main_title."</a>";
}
/* Timer End */
$time_end = microtime(true);
/* Debug if wanted */
if($debug == 1){
record_Debugs($path_main, "Serve");
}
//echo "<br>Page generated in: " . $tot_Tme = $time_end - $time_start;
}
?>