<?
/*
	Objective: This file is a generic handler to pop url on different OS and browser.
	parameters
		url: will pop this url
		event: onload, unonload
	
	*** 2006-05-08 ***
	Remove the INSERT in tables "pops_stats" & "pops_stats_noref"
	since they were corrupted and made the error logs explode.
	Anyway, no one uses these stats.
*/

//dependent module that will feed the p_handler.js.php with url and display options
//according to parameters pass to p_handler.js.php with query_string
Header('Cache-Control: no-cache');

define('U_PATH_COMMON', '/fame/htdocs/common/php');
define('U_PATH_PROJECT', U_PATH_COMMON.'/../famedollars/php');
include(U_PATH_PROJECT.'/conf/common.conf.php');
include(U_PATH_PROJECT.'/lib/common.functions.php');
include(U_PATH_COMMON.'/lib/db.functions.php');
include(U_PATH_COMMON.'/lib/common.functions.php');

include('/fame/htdocs/www.gammae.com/famedollars/pops/php/lib/pops.functions.php');
connect_db(U_DB_NAME);
//get from query_string
$site_id = $_GET['site_id'];
if($site_id==0 || $site_id == '')
{
	if($_GET['s_n'] != '')
	  $site_id = get_site_id($_GET['s_n']);
	else
 	  $site_id = get_site_id_custom($_SERVER['HTTP_REFERER']);
}

if(!isset($_GET['adv_id']) || $_GET['adv_id']=='')
	$adv_id = U_DEFAULT_ADV_ID;
else
	$adv_id = $_GET['adv_id'];
	
if($adv_id != 1000)	
  exit;
#$adv_id = $_GET['adv_id'];
$pop_order = get_pop_order($_GET['p_o']);
$pop_group_id = get_pop_group($site_id, $adv_id);
$number_of_pops=get_pop_number($pop_group_id);

# if it's the last pop in gamma chain and we have a custom pop, replace it with the custom pop #
if(isset($_GET['p_o']))
{
  	if($_GET['p_o']== $number_of_pops)
		$pop_data = get_pop_data_custom($pop_group_id, $pop_order, $adv_id);
	else
		$pop_data = get_pop_data($pop_group_id, $pop_order, $adv_id);
}
else
	$pop_data = get_pop_data($pop_group_id, $pop_order, $adv_id);


$pop_id = $pop_data['pop_id'];
$pop_adv_id = $pop_data['pop_adv_id'];
$pop_order_inc = $pop_order + 1;

if($number_of_pops<1 || $number_of_pops>10)
	$number_of_pops = 7;

$question = '&';
if(!strstr($pop_data['url'],"?"))
	$question = '?';


if($_GET['p_o'] != $number_of_pops)
{
   # check if the last pop is a custom on, if yes then we don't want to pass our args to it #
   if($pop_adv_id>0 && $_GET['p_o']>0) # if we have a advertiser's custom pop to pop at the end #
   {
   		// then we don't pass the args!
   }
   else
   {
   		if($pop_adv_id==0)
   			$qstr = $question."adv_id=$adv_id&site_id=$site_id&p_o=$pop_order_inc&p_i=$pop_id";
   }
}
else
{
	if($pop_adv_id==0)
		$qstr = $question."adv_id=$adv_id&site_id=$site_id&p_o=$pop_order_inc&p_i=$pop_id";
}


$url = $pop_data['url'].$qstr;
$pop_options = $pop_data['pop_options'];


//pop up stats for our popup
//note that we are unable to really know how many time the advertiser popups pop
//because he is hosting is pop up
if($_GET['p_i'] != '')
{
   $p_i = $_GET['p_i'];
   $f='pop_id, site_id, date, time, ip';
   $v= "'$p_i', ".
	    "$site_id, ".
        "CURDATE(), ".
		"CURTIME(), ".
         "'$_SERVER[REMOTE_ADDR]'";
   $query_result=insert('pops_stats', $f, $v, 0);
}
$man_blocked=0;
if (($_GET['bypass_popups'] == 1) || ($_GET['bypass_x'] == 1))
  $man_blocked=1;

//will pop only if we found something in pop data
//prevent thief
$stop_js = get_site_id_custom($_SERVER['HTTP_REFERER']);
if($stop_js == 0)
{
  //temporary log how many time we haven't display popup
  //because of a bad http_referer
   $f='date, time, ip, http_referer';
   $v=  "CURDATE(), ".
		"CURTIME(), ".
        "'$_SERVER[REMOTE_ADDR]', ".
        "'$_SERVER[HTTP_REFERER]'";
   $query_result=insert('pops_stats_noref', $f, $v, 0);  
}

if($pop_data != 0 && $stop_js != 0)
{
?>
//basically, only url and display options should be set
//note that the display options will only take effect when its not sp2	
var browserName=navigator.appName;
var nid=0;
var tid=431;
var mid=947;
var full=1;
var usePopDialog = false; //always leave it to false
var u = "6BF52A52-394A-11D3-B153-00C04F79FAA6";


function event_tags(tag, _event, fct)
/*

	will set the 'fct' function 
	to all tags named 'tag' on event '_event'
	
	tag : string for tag 					example : A
	_event : string the event : //no Caps	example : onclick
	fct : string the function //no () 		example : openwin
*/
{

	if(document.body.getElementsByTagName)
	{

		var x = document.body.getElementsByTagName(tag);
	
	}
	else if (document.body.all)
	{
	
		var x = document.body.all.tags(tag);
	}
	toeval="x[i]." + _event + " = " + fct + ";";
	
	for (var i=0;i<x.length;i++)
	{	
		eval(toeval);
	}
	return true;
}

function generic_click_event()
{
   //used to pop when someone click on a link

	wina = window.open('<?print $url;?>','popgav2<?print date('i')?>',"<?print $pop_options?>");
	if(wina)
	{
		wina.blur();
	}
	  
	return true;
}

//used when its firefox only
	<?
	if ($man_blocked == 0)
	{
	?>
	//event_tags('A', 'onclick', 'generic_click_event');
	wina = window.open('<?print $url;?>','popgav2<?print date('i')?>',"<?print $pop_options?>");
	if(wina)
	{
		wina.blur();
	}
	<?
	}
	?>
<?
}



###############
#	functions
###############
function get_site_id_custom($domain)
{
	/*
		Objective:  with the received domain, find a site id.
		            the main difference with this function is that
					it won't return a default site id if no ones found
	*/
	if (eregi('www', $domain) || eregi('members.', $domain))
		list($dummy1, $name, $dummy2) = split("\.", $domain);
	else
		list($name, $dummy1) = split("\.", str_replace("http://", "",$domain));
	$result = select('site_id', 'sites', "WHERE name='$name'", U_DO);
	
	if(mysql_num_rows($result))
	  $site_id = mysql_result($result, 0, 0);	
	# return a 0 #
	if (!$site_id)
		$site_id = 0;
	
	
	return $site_id;
}


?>

