####################
# 04022012 BHT add geoip country logic
# 04112012 BHT new centering logic
# 05142012 BHT load balancer masks remote addr we need to use
# HTTP_X_FORWARDED_FOR if its present
# 05142012 BHT use default to only display 'ANY' backgrounds if ip check does not react
# 05192012 BHT use new background array from background_country_list
# 05222012 BHT allow javascript only background
# 06012012 BHT work with bpage for javascript click
# 07202012 BHT rearchitect to use cm_array and cmb_array in choosing backgrounds
# 07272012 BHT trigger filter_homepage for priority only records !!! may leave pool empty
# 07282012 BHT new ANY-NC Global (Non-Core) backgrounds
# 09172012 BHT new 999 FORCE backgrounds
# 09172012 BHT new click handlers for clicks and impressions counts of backgrounds
# 02202013 BHT if tracker is present in record add decoded value into page and sub in [ORD]
# 02242013 BHT new region code geo
# 02252013 BHT we now enforce region codes if set
# 02252013 BHT fix logic error on unset if sessions > seen
##############
##############
# handle click on background
function enc($x){
return base64_encode(serialize($x));
}
function dec($x){
return unserialize((base64_decode($x)));
}
$sbu = $this->{'System/BaseURL'};
$sbu = str_replace('http://','//',$sbu);
$sbu = str_replace('https://','//',$sbu);
#echo 'sbu:'.$sbu;
# are we on the homepage?
$homepage = 'NO';
# $match = " and ((do_where='1') or (do_where='2')) ";
# adjust if this is a home page
$cval = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
# special case homepage
$cval = str_replace('?no_mobile=YES','', $cval);
if ( $cval == $this->{'System/BaseURL'} ) {
# this is homepage
$homepage = 'YES';
# $match = " and ((do_where='1') or (do_where='3')) ";
}
#echo '
hp:'.$homepage;
# read our background_array from background_country_list
$QBA = "SELECT * FROM background_country_list limit 1";
$RBA = $DatabaseObj->fetchAll($QBA);
$BA = $RBA[0];
# setup our array
$backgrounds_array = array();
if ($BA->backgrounds_array != '') {
#$backgrounds_array = unserialize($BA->backgrounds_array);
#echo '
!!!decode';
#echo '
!!!ba:'.$BA->backgrounds_array;
#echo 'strlen:'.strlen($BA->backgrounds_array);
#echo '
b64:'.base64_decode($BA->backgrounds_array);
#echo '
decode:'.dec($BA->backgrounds_array);
$backgrounds_array = dec($BA->backgrounds_array);
}
#echo '
!!! full!!!';
#print_r($backgrounds_array);
#echo '
countries:';
#print_r($backgrounds_array[0]);
# handle empty?
# at least make them arrays
# the countries
if (!is_array($backgrounds_array[0]))
$backgrounds_array[0] = array();
# the backgrounds --- full pool
if (!is_array($backgrounds_array[1]))
$backgrounds_array[1] = array();
# setup the ANY pool
if (!is_array($backgrounds_array[2]))
$backgrounds_array[2] = array();
#print_r($backgrounds_array[2]);
# setup the PRIORITY pool
if (!is_array($backgrounds_array[3]))
$backgrounds_array[3] = array();
# setup the ACTIVE pool (for homepage)
if (!is_array($backgrounds_array[4]))
$backgrounds_array[4] = array();
# setup the ACTIVE pool (for non-homepage)
if (!is_array($backgrounds_array[5]))
$backgrounds_array[5] = array();
# setup the ANY-NC pool
if (!is_array($backgrounds_array[6]))
$backgrounds_array[6] = array();
#################
#################
# the first thing we want to do is see if we have a country for this caller
require_once "Net/GeoIP.php";
#$geoip = Net_GeoIP::getInstance("/var/www/html/recipebridge.com/html/geo/GeoIP.dat");
$geoip = Net_GeoIP::getInstance("/var/www/html/recipebridge.com/html/geo/GeoIP-region.dat");
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$rip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$rip = $_SERVER['REMOTE_ADDR'];
}
try {
#echo $geoip->lookupCountryCode('71.0.170.218');
# $cc = $geoip->lookupCountryCode($rip);
list($cc, $region) = $geoip->lookupRegion($rip);
} catch (Exception $e) {
// Handle exception
}
# for debug
#$region='DC';
#$region='IN';
##################
##################
function filter_homepage($var) {
# do_where = 1 or 3 = homepage
# do_where = 1 or 2 = not homepage
if ($var->do_where == '1') return(1);
if ($var->do_where == '3') return(1);
return(0);
}
function filter_not_homepage($var) {
# do_where = 1 or 3 = homepage
# do_where = 1 or 2 = not homepage
if ($var->do_where == '1') return(1);
if ($var->do_where == '2') return(1);
return(0);
}
# for testing no country
#$cc = '';
#$cc = 'AU';
#$cc = 'CA';
#$cc = 'NZ';
# do we have some defined country?
######################
######################
# defined country
if ($cc != '') {
# we have some defined country
# echo '
Country:'.$cc;
# simple test case for NOT ACTIVE -- if so treat this as no defined country
# check our country array
$pool = $backgrounds_array[0];
#var_dump($backgrounds_array[0]);
# print_r($pool);
# find the matching country
foreach ($pool as $country) {
if ($country->kv == $cc) {
# this is our COUNTRY
# is it active?
if ($country->status != '1') {
# no?
# clear our country code
$cc = '';
break;
}
# this break leaves country set
break;
}
} # end foreach
} # end some defined country
###########################
# !!! we HAVE a defined country
# build tpool of possible backgrounds
###########################
######################
######################
# defined country
if ($cc != '') {
echo '
MS Found Country:'.$cc.'';
# print_r($country);
# we are still active after the simple test
$cid = $country->id;
# is priority enforced for this country?
$ep = $country->enforce_priority;
$hpr = $country->have_priority_records;
# 0 = no 1 = yes
$allow_any = $country->allow_any;
# now we must fill our pool
#$pool = array();
################
################
# do we BOTH enforce priority and have priority records?
if ($ep == '1') {
# we DO enforce priority
if ($hpr == 'Y') {
# we have priority records
# we MUST pick from this pool ONLY
# do not add in the ANY records
$allow_any = 0;
#$tpool = $backgrounds_array[3][$cid];
$pool = $backgrounds_array[3][$cid];
if ($homepage == 'YES') {
$tpool = array_filter($pool,filter_homepage);
} else {
$tpool = array_filter($pool,filter_not_homepage);
}
# echo '
have priority records and do enforce priorty';
# print_r($tpool);
} else {
# we do enforce priority but we have no priority records
# we can choose from the active country records AND any (if allowed)
# bring in the COUNTRY records
# filter the pool for the homepage
if ($homepage == 'YES') {
#echo '
pool before:';
#print_r($pool);
$pool = $backgrounds_array[4][$cid];
$cpool = array_filter($pool,filter_homepage);
#echo '
pool after homepage:';
#print_r($tpool);
} else {
$pool = $backgrounds_array[5][$cid];
$cpool = array_filter($pool,filter_not_homepage);
#echo '
pool after not homepage:';
#print_r($tpool);
}
$tpool = $cpool;
}
# end ep
} else {
# we DONT enforce priority
# bring in the active country records
# filter the pool for the homepage
if ($homepage == 'YES') {
$pool = $backgrounds_array[4][$cid];
# echo '
pool cid:'.$cid;
#echo '
pool before:';
#print_r($pool);
if ($pool == '') $pool = array();
$cpool = array_filter($pool,filter_homepage);
#echo '
pool after homepage:';
#print_r($tpool);
} else {
$pool = $backgrounds_array[5][$cid];
if ($pool == '') $pool = array();
$cpool = array_filter($pool,filter_not_homepage);
#echo '
pool after not homepage:';
#print_r($tpool);
}
$tpool = $cpool;
} # end else ep
# tpool holds what we've gathered so far....
# do we allow any?
if ($allow_any == '1') {
# yes -- merge that in
# bring in the ANY pool
$pool = $backgrounds_array[2];
#print_r($pool);
# filter the ANY pool for the homepage
if ($homepage == 'YES') {
#echo '
pool before:';
#print_r($pool);
$apool = array_filter($pool,filter_homepage);
#echo '
pool after homepage:';
#print_r($tpool);
} else {
$apool = array_filter($pool,filter_not_homepage);
#echo '
pool after not homepage:';
#print_r($tpool);
}
# and merge any pool -- filtered)
$tpool = $tpool + $apool;
} # end if allow any
#echo 'after';
# before we would try to pick a random entry from this pool
# now we just want to clear any session limited backgrounds
foreach ($tpool as $Result) {
$limit = $Result->do_sessions;
#echo '
limit:'.$limit;
if ($limit == 0) continue; #no sessions for this entry
if ($limit != '0') {
# this result must check sessions....
# we have some id
$bid = $Result->id;
if(isset($_SESSION['background_session_check'][$bid])) {
# this background has been seen before!
$seen = $_SESSION['background_session_check'][$bid];
#echo '
seen:'.$seen;
# are we greater than our limit?
if ($seen > $limit ) {
# we ARE greater than our BID -- we CANNOT use this background
# drop it from our pool
# and try again
#echo '
out of pool';
#unset($tpool[$bid]);
# we have to unset based on the entries id
foreach ($tpool as $ti => $te) {
if ($te->id == $bid) unset($tpool[$ti]);
}
}
}
} # end limit
} # end foreach tpool
# clear just in case
$Result = '';
# before we would try to pick a random entry from this pool
# now we just want to clear any region limited backgrounds
#var_dump($tpool);
# we only currently do regions in the US
if ($cc == 'US')
# and only if we have a region!!
if ($region != '')
foreach ($tpool as $Result) {
$enforce = $Result->enforce_region;
#echo '
enforce:'.$enforce;
if ($enforce == 0) continue; #no regions enforced for this entry
if ($enforce != '0') {
# this result must check regions....
# we have some id
$bid = $Result->id;
# what regions are enforced?
$region_array=dec($Result->region_array);
if (!is_array($region_array)) {
$region_array = array();
}
#echo "
checking region against valid:".$region;
#var_dump($region_array);
# ok we have a region $region
# we are enforcing
# and we have an array of choices
# is our region in the choices?
if (!in_array($region,$region_array)) {
# whoops its not -- unset this pool entry
# drop it from our pool
# and try again
#echo '
out of pool'.$bid;
#unset($tpool[$bid]);
# we have to unset based on the entries id
foreach ($tpool as $ti => $te) {
if ($te->id == $bid) unset($tpool[$ti]);
}
}
} # end enforce
} # end foreach tpool
#var_dump($tpool);
} # end handle country
###########################
# !!! we DONT have a defined country
# build tpool of possible backgrounds
###########################
######################
######################
# NO defined country (or country found but not active!
if ($cc == '') {
# we have NO defined country -- use any active record from the ANY pool
# we will NOT enforce priority (any pool) but we might enforce sessions
$pool = $backgrounds_array[2];
# filter the ANY pool for the homepage
if ($homepage == 'YES') {
#echo '
pool before:';
#print_r($pool);
$tpool = array_filter($pool,filter_homepage);
#echo '
pool after homepage:';
#print_r($tpool);
} else {
$tpool = array_filter($pool,filter_not_homepage);
#echo '
pool after not homepage:';
#print_r($tpool);
}
# also (primarily) use the new ANY-NC Non-Core Pool
# we may have duplicates if we have backgrounds in both ANY and ANY-NC -- this is ok
$pool = $backgrounds_array[6];
# filter the ANY pool for the homepage
if ($homepage == 'YES') {
#echo '
pool before:';
#print_r($pool);
$apool = array_filter($pool,filter_homepage);
#echo '
pool after homepage:';
#print_r($tpool);
} else {
$apool = array_filter($pool,filter_not_homepage);
#echo '
pool after not homepage:';
#print_r($tpool);
}
# and merge pool -- filtered)
$tpool = $tpool + $apool;
# print_r($tpool);
#unset($tpool[3]);
#echo '
unset:';
# print_r($tpool);
#exit;
# before we would try to pick a random entry from this pool
# now we just want to clear any session limited backgrounds
foreach ($tpool as $Result) {
$limit = $Result->do_sessions;
#echo '
limit:'.$limit;
if ($limit == 0) continue; #no sessions for this entry
if ($limit != '0') {
# this result must check sessions....
# we have some id
$bid = $Result->id;
if(isset($_SESSION['background_session_check'][$bid])) {
# this background has been seen before!
$seen = $_SESSION['background_session_check'][$bid];
#echo '
seen:'.$seen;
# are we greater than our limit?
if ($seen > $limit ) {
# we ARE greater than our BID -- we CANNOT use this background
# drop it from our pool
# and try again
#echo '
out of pool';
#unset($tpool[$bid]);
# we have to unset based on the entries id
foreach ($tpool as $ti => $te) {
if ($te->id == $bid) unset($tpool[$ti]);
}
}
}
} # end limit
} # end foreach tpool
# clear just in case
$Result = '';
} # end no defined country processing
# print_r($tpool);
#var_dump($tpool);
# we now have a tpool constructed from either the country or no country backgrounds which
# are active and not session limited -- these are the valid backgrounds its possible
# to pick from
# (or an empty tpool and no background)
# clear just in case
$Result = '';
$cmb_array = array();
# kwt is our array of keywords (from the context_match) which this
# read back their ids;
$kwt_array = array();
if (is_array($kwt))
foreach ($kwt as $cmkwval){
#echo '
checking kwt:'.$cmkwval;
$q_kwt = " SELECT * FROM context_match where campaign_keyword='$cmkwval' ;";
$m_kwt = $DatabaseObj->fetchAll($q_kwt);
if ($m_kwt[0]->id != '') $kwt_array[] = $m_kwt[0]->id;
}
#echo '
kwtr:';
#print_r($kwt_array);
# we check first the context_buckets -- because they are specific urls
# does this url
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$url = mysql_real_escape_string($url);
# $url = str_replace('dev','www',$url);
# match any active context buckets?
$q_cmb = " SELECT * FROM cmb_targets left join context_match_bucket cmb on cmb.id=cm_id where cmb_status = '1' and target='$url' ;";
#echo "
checking:".$q_cmb;
$m_qmb = $DatabaseObj->fetchAll($q_cmb);
foreach($m_qmb as $qmbr) {
# the url does match the cmb target
# if its active
if ($qmbr->status != '1') {
# this is not ACTIVE context match bucket
continue;
}
# this is an active bucket
# do we have a background in our tpool
# with this bucket attached?
$context_match_bucket_id = $qmbr->cm_id;
# push it into our array to check
$cmb_array[]=$context_match_bucket_id;
} # end foreach active cmb
#echo "
CMB array
";
#print_r($cmb_array);
#echo '
';
#print_r($tpool);
#echo '
';
#echo '
keywords found:';
#print_r($kwt);
#############
#############
# if we have any work to do
$pc = count($tpool);
if ($pc != '0') {
# yes we have some
# if any of them match our context match we want to pick that one
# otherwise we want a random one
#echo '
Checking context match buckets...';
###############
###############
# check each background in the pool
# against our context_match_buckets
foreach ($tpool as $pr) {
# get out if we found something
if ($Result != '') break;
$bid = $pr->id;
$bname = $pr->background_name;
$check_cmb_array = unserialize($pr->cmb_array);
if (!is_array($check_cmb_array)) $check_cmb_array = array();
##print_r($check_cmb_array);
#echo "
checking: ".$bid.'/'.$bname;
foreach ($check_cmb_array as $target_cmb) {
if(in_array($target_cmb,$cmb_array)) {
#echo '
!!! CMB TARGET was found:'.$target_cmb;
# this is a matching CMB result -- choose this background
$Result = $pr;
break;
}
} # end foreach check_cmb_array
} # end foreach tpool
# if we DID NOT match a bucket we also check keyword matches
if ($Result == '') {
# no match found in buckets
#echo '
Checking context match keywords...';
#foreach ($kwt_array as $kt) {
#echo '
kwt:'.$kt;
#}
###############
###############
# check each background in the pool
# against our context_match
foreach ($tpool as $pr) {
# get out if we found something
if ($Result != '') break;
$cid = $pr->id;
$cname = $pr->background_name;
$check_cm_array = unserialize($pr->cm_array);
if (!is_array($check_cm_array)) $check_cm_array = array();
#print_r($check_cm_array);
#echo "
checking: ".$cid.'/'.$cname;
#print_r($kwt_array);
foreach ($check_cm_array as $target_cm) {
#echo "
checking tcm: ".$target_cm;
if(in_array($target_cm,$kwt_array)) {
#echo '
!!! CM TARGET was found:'.$target_cm;
# this is a matching CM result -- choose this background
$Result = $pr;
break;
}
} # end foreach check_cm_array
} # end foreach tpool
} # end if no result in cmb check cm
if ($Result != '') {
#echo '
we have a matching result:';
#print_r($Result);
} else {
#echo '
NO context matching result/bucket: (selecting random)
';
# pick a random entry from pool
$rand_key = array_rand($tpool);
$Result = $tpool[$rand_key];
#print_r($Result);
} # end else
} # end if pc > 0
##############
# !!! we have picked a specific background -- either from country or no country logic
##############
# we have picked an entry to show
# echo '
Final result:';
# print_r($Result);
#######################
# we should have chosen a normal background here....BUT allow for force
$force = 'NO';
if (isset($_GET['force_background']) && $_GET['force_background']!='') {
$gfb = $_GET['force_background'];
# we have some possible force on the background
if (isset($backgrounds_array[999][$gfb])) {
# force is set and we have something there....try to use it...
$Result = $backgrounds_array[999][$gfb];
echo '
forced result:';
echo '';
$force = 'YES';
}
}
if ((isset($Result)) and ($Result != '')) {
# we have a result to show
# if we check sessions for this up count it
$limit = $Result->do_sessions;
#echo '
limit:'.$limit;
if ($limit != '0') {
$bid = $Result->id;
$seen = 0;
if(isset($_SESSION['background_session_check'][$bid])) {
# this background has been seen before!
$seen = $_SESSION['background_session_check'][$bid];
}
# increment our seen count for this result
$_SESSION['background_session_check'][$bid] = $seen+1;
} # end if limit
#################
# we need to deal with the new background options
# if they are set we will apply them
# !!! note that CENTERING overides some other options
if ($Result->do_background_color != '-1') {
# we have some background color
$the_color = $Result->do_background_color;
# background color -- this is a css modification
?>
} # end background options color
# TOP and POSITION
$position = $Result->do_position;
$top = $Result->do_top;
# we have to modify the top based on the home page or other page
# what is our page?
#echo "
".$this->{'System/BaseURL'}."
";
#print_r($_SERVER);
# adjust positon if FIXED to offset header (and zero top)
$cval = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
if ($position == 'absolute')
if ( $cval == $this->{'System/BaseURL'} ) {
$top = 44;
} else {
$top = 111;
}
?>
# if CENTERING is active do NOT apply LEFT
#$centering = '1';
$centering = $Result->do_centering;
if ($centering == '1'){
# do not apply left
} else {
# apply left
$left = $Result->do_left;
?>
} # end else centering
# if CENTERING is active do NOT apply WIDTH
if ($centering == '1'){
# do not apply left
} else {
# WIDTH -- if zero do not set!
$width = $Result->do_width;
if ($width != '0') {
# we have some set width
?>
} # end width set
} # end else centering
if ($Result->new_window == '0') {
$nw = '';
} else {
$nw = ' target="_blank" ';
}
$t_file = '/var/www/html/recipebridge.com/html/backgrounds/'.$Result->file_name;
$t_url = $this->{'System/BaseURL'}.'backgrounds/'.$Result->file_name;
if ($Result->show_html != '1')
if (file_exists($t_file)) {
# yes show it
# we are showing the image directly...and activating the click logic
#echo ' ';
# if CENTERING is active style differently
if ($centering == '1'){
# centering IS active
$size = getimagesize($t_file);
$width = $size[0];
# default if not provided
if (!is_numeric($width)) $width=1200;
echo '';
echo '
';
echo '
';
} else {
# no centering
echo '';
} # end else centering
############
# script to activate click
?>
# end script
############
} else {
# no such background file?
# use default
if ($Result->show_html != '1')
echo '';
}
if ($Result->show_html == '1') {
# add extra click to my_body if present
?>
# drop out html
$html = $Result->html;
$html = str_replace("\'","'", $html);
$html = str_replace('\"','"', $html);
$html = str_replace('\r\n',"\n", $html);
echo $html;
}
} # end Result (should be only 1)
######################
# now lets add in the 1x1 tracker - if present
# we may need a date/time
$ord = date("dmYHis");
#echo 'ORD:'.$ord;
# like ORD:20022013214759
if ($Result->tracker != '') {
# lets insert the tracker....
$tracker = dec($Result->tracker);
$tracker = str_replace('[ORD]',$ord,$tracker);
echo $tracker;
}
?>