Needed help in adf.ly clone

Discussion in 'Programming' started by Wol, Jul 11, 2013.

  1. #1
    Hello.

    Needed help in adf.ly clone
    • Limit 1 view per ip in 24 hours
    analyzer.class.php

    <?php
    /*******************************************************************************
    * Link analytic system
    *

    ******************************************************************************/

    define('ANALYZER_CLASS_PHP_INCLUDED', true);
    define('GEOIP_LOC', realpath(dirname(__FILE__)) . '/geoip/');
    include_once GEOIP_LOC . 'geoip.php';
    include_once '../global.php';

    class Analyzer {
    protected $table = '';
    protected $db = null;
    protected $lid = 0;
    protected $oid = 0;
    protected $aid = 0;
    protected $cip = 0;
    protected $geo = null;

    protected $client = array(
    'ipaddr' => '0.0.0.0',
    'country' => '',
    'referrer' => ''
    );

    public function __construct($lid, $oid, $aid, $ref = '') {
    global $_GLOBAL;
    $this->table = $_GLOBAL['TABLES']['ANALYZER'];
    $this->db = System::getDB();
    $this->lid = $lid;
    $this->oid = $oid;
    $this->aid = $aid;
    $this->cip = mb_substr($this->ip(), 0, 15);
    $this->geo = geoip_open(GEOIP_LOC . 'GeoIP.dat', GEOIP_STANDARD);
    $this->client['referrer'] = $ref;
    }

    public function __destruct() {
    geoip_close($this->geo);
    }

    public function _record($adtype = 'none', $view_time = 0) {
    global $_GLOBAL;
    if ($this->_isCrawler() || !$this->db || !$this->isValid()) return;
    $_u = $this->isUnique();

    $this->client['ipaddr'] = $this->cip;
    $this->client['country'] = $this->country($this->client['ipaddr']);

    $shrinker_id = $this->db->getField($_GLOBAL['TABLES']['LINKS'], 'user', "`id`='{$this->lid}'");

    $sip = $this->db->getField($_GLOBAL['TABLES']['USERS'], 'ipaddr', "`id`='{$shrinker_id}'");
    if (!$_u && $this->client['ipaddr'] == $sip) {
    return;
    }

    try {
    $pu = parse_url($this->client['referrer']);
    if (CURRENT_HOSTNAME == $pu['host']) $this->client['referrer'] = 'IM, Applications, and Direct';
    } catch(Exception $e) { }

    $earned = 0;
    $adtype = strtolower($adtype);
    if ($adtype != 'none') {
    $earned = $this->getEarning(($_u ? 'u_' : 'r_') . $adtype, $this->client['country']);
    if ($adtype == 'top_banner') $earned *= .70;
    else if ($view_time < 6) $earned /= 6 - $view_time;
    }

    $rhits = $this->db->getField($this->table, 'hits', "`lid`='{$this->lid}' AND `aid`='{$this->aid}' AND `ipaddr`='{$this->cip}'");
    if ($rhits > 3) $earned /= $rhits;

    $data = array_merge($this->client, array('lid' => $this->lid, 'oid' => $this->oid, 'aid' => $this->aid,
    'date' => date('Y-m-d'), 'earned' => $earned,
    'hits' => 1));

    $_p = false;
    if ($_u) {
    $_p = $this->db->insert($this->table, $data);
    } else {
    $_p = $this->db->update($this->table, array('earned' => 'earned+' . $earned, 'hits' => 'hits+1'),
    "`lid`='{$this->lid}' AND `aid`='{$this->aid}' AND `ipaddr`='{$this->cip}' "
    . "AND `date`='" . date('Y-m-d') . "'",
    false);
    }

    if ($_p) {
    $data = array('views' => 'views+1', 'earned' => 'earned+' . $earned);
    $this->db->update($_GLOBAL['TABLES']['LINKS'], $data, "`id`='{$this->lid}'", false);
    $this->db->update($_GLOBAL['TABLES']['USERS'], array('available_earning' => 'available_earning+' . $earned),
    "`id`='{$shrinker_id}'", false);
    if ($this->aid) {
    $c = $this->db->getRows($_GLOBAL['TABLES']['CAMPAIGNS'], "`id`='{$this->aid}'", '', '1');
    if ($c['spent_today'] < $c['daily_budget'] && $c['daily_budget'] != 0) {
    $cdat = array('views_left'=>'views_left-1', 'spent_today'=>'spent_today+' . $earned);
    if ($c['views_left'] <= 1) {
    $cdat['status'] = 3;
    $cdat['views_left'] = '0';
    }
    $this->db->update($_GLOBAL['TABLES']['CAMPAIGNS'], $cdat, "`id`='{$this->aid}'", false);
    }
    }
    } else {
    System::log($this->db->error());
    }
    }

    public function _recordClick() {
    global $_GLOBAL;
    if (!$this->isValid()) return false;
    $earned = $this->getEarning(($this->isUnique(true) ? 'u' : 'r') . '_top_banner',
    $this->country($this->ip())) * .30;
    $cdat = array('total_clicks' => 'total_clicks+1', 'spent_today' => 'spent_today+' . $earned);
    $adat = array('earned' => 'earned+' . $earned, 'banner_click' => "'1'");
    return $this->db->update($_GLOBAL['TABLES']['CAMPAIGNS'], $cdat, "`id`='{$this->aid}'", false)
    && $this->db->update($this->table, $adat, "`lid`='{$this->lid}' AND `ipaddr`='{$this->cip}'", false)
    && System::getUser()->raiseEarning($earned);
    }

    public function isUnique($click = false) {
    $date = date('Y-m-d');
    $where = "`lid`='{$this->lid}' AND `aid`='{$this->aid}' AND `ipaddr`='{$this->cip}'"
    . ($click ? " AND `banner_click`='1'" : '');
    return !$this->db->rowCount($this->table, $where . " AND `date`='{$date}'");
    }

    public function isValid() {
    global $_GLOBAL;
    return $this->db->rowCount($_GLOBAL['TABLES']['LINKS'], "`id`='{$this->lid}' AND `user`='{$this->oid}'");
    }

    public function getEarning($adtype, $gccn) {
    global $_GLOBAL;
    $adtype = str_replace(' ', '_', strtolower($adtype));
    $pkgs = $this->db->getRows($_GLOBAL['TABLES']['PAYOUTS'], "`active`='1'");
    $prox = isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] != '';
    $eccn = $prox ? $pkgs[1] : $pkgs[0];
    if (!$prox) {
    for ($i = 0; $i < count($pkgs); $i++) {
    if ($pkgs[$i]['code'] == $gccn) {
    $eccn = $pkgs[$i];
    break;
    }
    }
    }
    return $eccn[$adtype] / 1000;
    }

    // ***

    private function _toUTF8($str) {
    $e = strtoupper(mb_detect_encoding($str));
    return $e == false || $e == 'UTF-8' || $e == 'ASCII' ? $str : iconv($e, 'UTF-8', $str);
    }

    // ***

    public function _isCrawler() {
    $ua = $_SERVER['HTTP_USER_AGENT'];
    $crawlers = 'Google|msnbot|Rambler|Yahoo|AbachoBOT|accoona|' .
    'AcioRobot|ASPSeek|CocoCrawler|Dumbot|FAST-WebCrawler|' .
    'GeonaBot|Gigabot|Lycos|MSRBOT|Scooter|AltaVista|IDBot|eStyle|Scrubby';
    return preg_match("/{$crawlers}/", $ua) > 0;
    }

    public function _isProxy() {
    $hd = array('HTTP_VIA', 'HTTP_X_FORWARDED_FOR', 'HTTP_FORWARDED_FOR',
    'HTTP_X_FORWARDED', 'HTTP_FORWARDED', 'HTTP_CLIENT_IP',
    'HTTP_FORWARDED_FOR_IP', 'VIA', 'X_FORWARDED_FOR',
    'FORWARDED_FOR', 'X_FORWARDED', 'FORWARDED',
    'CLIENT_IP', 'FORWARDED_FOR_IP', 'HTTP_PROXY_CONNECTION');
    $detected = false;
    foreach($hd as $i){
    if(isset($_SERVER[$i]) && $_SERVER[$i]) {
    return true;
    }
    }
    return in_array($_SERVER['REMOTE_PORT'], array(8080, 80, 6588, 8000, 3128, 553, 554))
    || @fsockopen($this->cip, 80, $errno, $errstr, 0.1);
    }

    public function ip() {
    $ip = $_SERVER['REMOTE_ADDR'];

    if (($ip == '127.0.0.1' || $ip == '::1' || $ip == $_SERVER['SERVER_ADDR'])
    && isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] != '') {
    $ips = explode(', ', $_SERVER['HTTP_X_FORWARDED_FOR']);
    $ip = $ips[0];
    }

    return $ip;
    }

    public function country($ip) {
    return $this->_isProxy() ? 'A1' : geoip_country_code_by_addr($this->geo, $ip);
    }

    /*
    function browser() {
    $a = array('Windows', 'Mac', 'Linux', 'FreeBSD', 'DoCoMo', 'iPod', 'iPad', 'iPhone',
    'Android', 'Symbian', 'Nintendo', 'PlayStation');
    $a = $_SERVER['HTTP_USER_AGENT'];
    $b = get_browser($a, true);
    $o = '';

    foreach ($a as $b) {
    if (preg_match('/' . $b . '/', $u)) {
    $o = $b;
    break;
    }
    }

    return array('os' => $o, 'browser' => '' $b['parent']);
    }*/
    }
     
    Wol, Jul 11, 2013 IP