####################
# 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
# 02242013 BHT new region code geo
##############
##############
# handle click on background
function enc($x){
return base64_encode(serialize($x));
}
function dec($x){
return unserialize((base64_decode($x)));
}
# 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'];
if ( $cval == $this->{'System/BaseURL'} ) {
# this is homepage
$homepage = 'YES';
# $match = " and ((do_where='1') or (do_where='3')) ";
}
# 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);
$backgrounds_array = dec($BA->backgrounds_array);
}
#echo '
!!! full!!!';
#print_r($backgrounds_array);
# 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();
#################
#################
# 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
}
##################
##################
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';
# 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];
# 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];
# echo '
have priority records and do enforce priorty';
# print_r($pool);
} 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') {
#echo '
pool before:';
#print_r($pool);
$pool = $backgrounds_array[4][$cid];
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]);
}
}
} # end limit
} # end foreach tpool
# clear just in case
$Result = '';
} # 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);
}
# 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]);
}
}
} # end limit
} # end foreach tpool
# clear just in case
$Result = '';
} # end no defined country processing
# 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
# 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);
# 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' ;";
$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
print_r($cmb_array);
# 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
# check the cmb_array
} # end if pc > 0
# pick a random entry from pool
$rand_key = array_rand($tpool);
$Result = $tpool[$rand_key];
##############
# !!! 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);
if ((isset($Result)) and ($Result != '')) {
# we have a result to show
#################
# 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)
?>