Problem with php script

Discussion in 'PHP' started by lhmnos, Aug 16, 2013.

  1. #1
    hi i have a php script (adf.ly clone) but i have a problem, it has a file analyzer.class.php that find the ip(country) and more! The problem is that it mark all the views as proxy even if it isn't proxy! Script has a folder /geoip/ and inside /geoip/GeoIP.dat i update this file and /geoip/geoip.php update this also! So the problem must be in the 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']);
        }*/
    }
    PHP:
    need your help guys !
     
    lhmnos, Aug 16, 2013 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,897
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    How much debugging have you already done? Can you narrow it down to a bad sql call?

    seems to be all happening in publicfunction _isProxy() so you need to debug in there
     
    sarahk, Aug 16, 2013 IP
  3. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #3
    Logic seems correct. I would do a echo '<pre>'; print_r($_SERVER) on a test page and post the results here.

    If I would guess, I would say that your hosting company is using a gateway or a switch board that redirects using SERVER FORWARDED string. This is usually the case with websites that have multiple servers on a single entry point.
     
    ThePHPMaster, Aug 16, 2013 IP
  4. lhmnos

    lhmnos Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #4
    thanks for the answer, but i am a noob on php, so can you help me on how to "narrow it down to a bad sql call" maybe in skype or here thanks again!

    thanks you too, but as i said i need help on how to "do a echo '<pre>'; print_r($_SERVER)" or how to see if it is a hosting problem, sorry for my stupid questions !

    also i am on shared host no vps, !
     
    Last edited: Aug 17, 2013
    lhmnos, Aug 17, 2013 IP
  5. lhmnos

    lhmnos Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #5
    ok, i did the echo '<pre>'; print_r($_SERVER), i don't think that this is the problem [​IMG]
     
    lhmnos, Aug 17, 2013 IP
  6. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #6
    From what you posted, server variable HTTP_X_FORWARDED_FOR is set, which means that your isProxy method will always return true.

    Your host seems to be setting that value (or you are accessing that page via a proxy).

    Either remove the HTTP_X_FORWARDED_FOR from the checks or find a way to fix this with your host.
     
    ThePHPMaster, Aug 17, 2013 IP
  7. lhmnos

    lhmnos Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #7
    how can i remove the the HTTP_X_FORWARDED_FOR from my script ?
     
    lhmnos, Aug 17, 2013 IP
  8. sarahk

    sarahk iTamer Staff

    Messages:
    28,897
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #8
    The easiest way would be to edit this function
    
    public function _isProxy() {
           $hd = array('HTTP_VIA', 'HTTP_X_FORWARDED_FOR_REMOVED', '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);
       }
    PHP:
    by leaving the item in the array you can see how it was originally but adding _REMOVED on the end means it will never be true.
     
    sarahk, Aug 17, 2013 IP
  9. lhmnos

    lhmnos Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #9
    thanks it worked :), so no wit will not detect proxies right ?
     
    lhmnos, Aug 19, 2013 IP