wp_options);
}
if(get_option(AD_ADS2_OPTIONS)==AD_EMPTY_VALUE || $type==AD_FUNC_TYPE_RESET)
update_option(AD_ADS2_OPTIONS, $ads2->wp_options);
if(get_option(AD_ADS3_OPTIONS)==AD_EMPTY_VALUE || $type==AD_FUNC_TYPE_RESET)
update_option(AD_ADS3_OPTIONS, $ads3->wp_options);
}
//reset all the value
function ad_resetIt(){
ad_DataInitialer(AD_FUNC_TYPE_RESET);
}
//initial the unset value
function ad_initialIt(){
ad_DataInitialer(AD_FUNC_TYPE_INITIAL);
}
function ad_admin_menu() {
add_submenu_page('options-general.php', 'Adsense Daemon Options', 'Adsense Daemon', 8, 'Adsense Daemon', 'ad_menu');
}
function filter_weird_characters($str){
$str = str_replace("\\\"", "\"", $str);
return $str;
}
function ad_menu(){
$ads1 = new Ads1();
$ads2 = new Ads2();
$ads3 = new Ads3();
if (isset($_POST[AD_FORM_SAVE])) {
foreach(array_keys($ads1->wp_options) as $key){
$ads1->wp_options[$key] = filter_weird_characters($_POST[$key]);
}
foreach(array_keys($ads2->wp_options) as $key){
$ads2->wp_options[$key] = filter_weird_characters($_POST[$key]);
}
foreach(array_keys($ads3->wp_options) as $key){
$ads3->wp_options[$key] = filter_weird_characters($_POST[$key]);
}
update_option(AD_ADS1_OPTIONS, $ads1->wp_options);
update_option(AD_ADS2_OPTIONS, $ads2->wp_options);
update_option(AD_ADS3_OPTIONS, $ads3->wp_options);
echo "
";
}else if(isset($_POST[AD_FORM_CLEAR])){
ad_resetIt();
echo "";
}
//load options from db
$ads1->wp_options = get_option(AD_ADS1_OPTIONS);
$ads2->wp_options = get_option(AD_ADS2_OPTIONS);
$ads3->wp_options = get_option(AD_ADS3_OPTIONS);
$ad_array = array($ads1,$ads2,$ads3);
print_ad_menu_form($ad_array);
}
function ad_content_hook($content = ''){
if(!is_single())
return $content;
$ads1 = new Ads1();
$ads2 = new Ads2();
$ads3 = new Ads3();
//load options from db
$ads1->wp_options = get_option(AD_ADS1_OPTIONS);
$ads2->wp_options = get_option(AD_ADS2_OPTIONS);
$ads3->wp_options = get_option(AD_ADS3_OPTIONS);
$ad_all_data = array($ads1,$ads2,$ads3);
//get post published date
$publish_date = the_date('Y-m-d','','',false);
$http_referer = '';
//get referer
if(isset($_SERVER['HTTP_REFERER'])) {
$http_referer = $_SERVER['HTTP_REFERER'];
}
$content = generateAdsenseDaemonDiv($content, $ad_all_data, $publish_date, $http_referer);
return $content;
}
function isAllowDisplayAfterDay($obj, $publish_date){
$after_days = $obj->get_ad_after_day();
$allow_date = mktime(0, 0, 0, date("m"), date("d")-$after_days, date("Y"));
$allow_date = date('Y-m-d', $allow_date);
/*print_r(" allow_date : " . $allow_date);
print_r(" publish_date : " . $publish_date);
print_r("
");*/
//if 0, display immediately
if(trim($after_days) == AD_ZERO_DATA){
return true;
}
if($allow_date >= $publish_date){
return true;
}else{
return false;
}
}
function isRefererAllow($obj, $http_referer){
$websites = explode(",",$obj->get_ad_block_user());
//echo "
http_referer :" . $http_referer . " | " . $obj->get_ad_block_user();
$referer_allow=true;
foreach($websites as $referer){
$referer = trim($referer);
//echo "
referer" . $referer;
//avoid empty value
if($referer==AD_EMPTY_DATA){
continue;
}
if (preg_match("/" . $referer . "/i", $http_referer)) {
// echo " matched";
$referer_allow = false;
}else{
// echo " not match";
}
}
return $referer_allow;
}
function generateAdsenseDaemonDiv($content, $ad_all_data, $publish_date, $http_referer){
foreach($ad_all_data as $obj){
//if empty data, continue next
if($obj->get_ad_data()==AD_EMPTY_DATA){
continue;
}
if(isAllowDisplayAfterDay($obj, $publish_date)==false){
continue;
}
if(isRefererAllow($obj, $http_referer)==false){
continue;
}
if($obj->get_append_type() == AD_SELECT_LEFT_FLOAT){
$content = generateDivLeftFloat($content, $obj);
}else if($obj->get_append_type() == AD_SELECT_RIGHT_FLOAT){
$content = generateDivRightFloat($content, $obj);
}else if($obj->get_append_type() == AD_SELECT_BEFORE_CONTENT){
$content = generateDivBefore($content, $obj);
}else if($obj->get_append_type() == AD_SELECT_AFTER_CONTENT){
$content = generateDivAfter($content, $obj);
}else if($obj->get_append_type() == AD_SELECT_PARA1){
$content = generateDivPara($content, $obj, 1);
}else if($obj->get_append_type() == AD_SELECT_PARA2){
$content = generateDivPara($content, $obj, 2);
}else if($obj->get_append_type() == AD_SELECT_RADOM_PARA){
$content = generateDivRandomPara($content, $obj);
}
}
$content .= AD_AUTHOR_SITE;
return $content;
}
function generateDivLeftFloat($content, $obj){
return "" . $obj->get_ad_data() . "
" . $content;
}
function generateDivRightFloat($content, $obj){
return "" . $obj->get_ad_data() . "
" . $content;
}
function generateDivBefore($content, $obj){
return "" . $obj->get_ad_data() . "
" . $content;
}
function generateDivAfter($content, $obj){
return $content . "" . $obj->get_ad_data() . "
";
}
function generateDivPara($content, $obj, $para){
$poses = array();
$poseslast = array();
$lastpos = -1;
$findchar = "";
if(strpos($content, "
$para)
{
$pickme = $poses[$para];
$content = substr_replace($content, "
" . $obj->get_ad_data() . "
", $pickme, 0);
//reset it
$lastpos = -1;
}
return $content;
}
function generateDivRandomPara($content, $obj){
$poses = array();
$poseslast = array();
$lastpos = -1;
$findchar = "";
if(strpos($content, "
1)
{
$pickme = $poses[rand(0, sizeof($poses)-1)];
$content = substr_replace($content, "
" . $obj->get_ad_data() . "
", $pickme, 0);
//reset it
$lastpos = -1;
}
return $content;
}
?>