function _default()
 {
     // Grab the cookie
     $c = new cookielib();
     $k = $c->get("conv");
     // Update
     if ($k[id] > 0) {
         // Get the ad info
         $adinfo = $this->db->getsql("SELECT id,userid,zone FROM adrev_ads WHERE id=?", array($k['id']));
         $ad = $adinfo[0];
         // Get the zone info
         $zone = $this->db->getsql("SELECT rate,rate_type,aff_percent FROM adrev_zones WHERE id=?", array($ad['zone']));
         $z = $zone[0];
         if ($ad[id] && $_GET[id] == $ad[userid]) {
             $date = date("Y-m-d");
             $t = $this->db->getsql("SELECT * FROM adrev_traffic \n\t\t\t\t\t\t\t\t\t\tWHERE adid=? AND date=? AND keywordid=?", array($k['id'], $date, $k['kid']));
             if (!$t[0][id]) {
                 // Add a new traffic record
                 $i = array();
                 $i[userid] = $t[0][userid];
                 $i[date] = $date;
                 $i[adid] = $ad[id];
                 $i[keywordid] = $k[id];
                 $i[amount] = iif($z[rate_type] == "CPA", $z[rate], 0);
                 $this->db->insert("adrev_traffic", $i);
                 $amount = $i[amount];
             } else {
                 $id = $t[0][id];
                 $amount = iif($z[rate_type] == "CPA", $z[rate], 0);
                 $this->db->getsql("UPDATE adrev_traffic SET orders=orders+1,amount=amount+{$amount} WHERE id=?", array($id));
             }
             // If this is a CPA type, update units
             if ($z[rate_type] == "CPA") {
                 $this->db->getsql("UPDATE adrev_ads SET total_units=total_units+1 WHERE id=?", array($ad['id']));
             }
             // Update the affiliate cut
             if ($z[aff_percent] > 0 && $z[rate_type] == "CPA" && $amount) {
                 $i = array();
                 $i[date] = time();
                 $i[affid] = $k[affid];
                 $i[adtype] = $z[rate_type];
                 $i[adid] = $ad[id];
                 $i[ip] = $_SERVER[REMOTE_ADDR];
                 $i[referer] = $_SERVER[HTTP_REFERER];
                 $i[amount] = $amount * ($z[aff_percent] / 100);
                 $this->db->insert("adrev_aff_traffic", $i);
             }
             // Attempt to expire this ad
             if ($ad[total_units] >= $ad[units] && $ad[units] > 0 || $ad[expires] > 0 && $ad[expires] <= time()) {
                 $this->db->getsql("UPDATE adrev_ads SET status=-2 WHERE id=?", array($ad['id']));
             }
             // Update the running totals for the customer
             if ($amount > 0) {
                 $t = time();
                 $this->db->getsql("UPDATE adrev_users SET balance=balance-{$amount}, balance_update=? WHERE id=?", array($t, $ad['userid']));
             }
             // Clear the conversion cookie
             $c->set("conv", array());
         }
     }
     // Send the Gif
     header("Content-type: image/gif");
     $gifpix = new gifpix();
     echo $gifpix->create();
 }
 function _default()
 {
     // Instantiate the cookie Library
     $c = new cookielib();
     // Get our variables
     $this->zid = $_GET[zid];
     $this->affid = $_GET[affid];
     $this->kid = $_GET[kid];
     // Grab the Ad Information
     $a = $this->db->getsql("SELECT * FROM adrev_ads WHERE zid=?", array($this->zid));
     $ad = $a[0];
     if (!$a[0][id]) {
         header("Location: " . $this->default[adrevenue][default_redir]);
         exit;
     }
     // Grab the zone
     $zone = $this->db->getsql("SELECT * FROM adrev_zones WHERE id=?", array($ad['zone']));
     $this->z = $zone[0];
     // Check if we are outside of the click threshold (for fraud)
     $k = $c->get("_k_" . $this->zid);
     $win = time() - $k;
     if ($k > 0 && $win < $this->default[adrevenue][dup_clicks]) {
         header("Location: " . $ad[url]);
     } else {
         // Setup the cookie
         $c->set("_k_" . $this->zid, time());
         // Set a conversion cookie
         $cad = array('id' => $ad[id], 'affid' => $this->affid, 'kid' => $this->kid);
         $c->set("conv", $cad);
         if ($this->kid > 0) {
             $k = $this->db->getsql("SELECT * FROM adrev_keywords WHERE id=?", array($this->kid));
             $keywordrate = $k[0][mincpc];
         }
         // Try to load this record
         $date = date("Y-m-d");
         $this->kid = $this->kid ? $this->kid : 0;
         $this->affid = $this->affid ? $this->affid : 0;
         $a = $this->db->getsql("SELECT id FROM adrev_traffic \r\n\t\t\t\t\t\t\t\t\t\tWHERE adid=? AND date=? AND keywordid=?", array($ad['id'], $date, $this->kid));
         // Calculate rate to post here
         $t = time();
         $amount = 0;
         if ($this->z[rate_type] == "CPC") {
             if ($this->z[rate] >= $ad[bid]) {
                 $amount = $this->z[rate];
             }
             if ($ad[bid] >= $this->z[rate]) {
                 $amount = $ad[bid];
             }
             if ($keywordrate >= $amount) {
                 $amount = $keywordrate;
             }
             //Added 11-17-04
             if ($this->z[keywords_enable] == 1 and $this->z[ad_sort] == "bid") {
                 $k = $this->db->getsql("SELECT * FROM adrev_keyword_map WHERE keywordid=?", array($this->kid));
                 $amount = $k[0][bid];
             }
             //End-11-16-04
         }
         // Post amounts
         $amount = $amount > 0 ? $amount : 0;
         $id = $a[0][id];
         if (!$ad[id]) {
             // Add a new traffic record
             $i = array();
             $i[userid] = $ad[userid];
             $i[date] = $date;
             $i[adid] = $ad[id];
             $i[keywordid] = $this->kid;
             $i[impressions] = 1;
             $i[clicks] = 1;
             $i[amount] = $amount;
             $this->db->insert("adrev_traffic", $i);
         } else {
             $this->db->getsql("UPDATE adrev_traffic SET clicks=clicks+1, amount=amount+{$amount} WHERE id=?", array($id));
             if ($this->z[rate_type] == "CPC") {
                 // Update ad units
                 $this->db->getsql("UPDATE adrev_ads SET total_units=total_units+1 WHERE id=?", array($ad['id']));
                 $ad[total_units] += 1;
             }
         }
         // Update real time balance if we actually have an amount
         $t = time();
         if ($amount > 0) {
             if ($this->affid > 0 && $this->z[aff_percent] > 0) {
                 $i = array();
                 $i[date] = time();
                 $i[affid] = $this->affid;
                 $i[adtype] = $this->z[rate_type];
                 $i[adid] = $ad[id];
                 $i[ip] = $_SERVER[REMOTE_ADDR];
                 $i[referer] = $_SERVER[HTTP_REFERER];
                 $i[amount] = $amount * ($this->z[aff_percent] / 100);
                 $this->db->insert("adrev_aff_traffic", $i);
             }
             $this->db->getsql("UPDATE adrev_users SET balance=balance-{$amount}, balance_update=? WHERE id=?", array($t, $ad['userid']));
         }
         // Attempt to expire this ad
         if ($ad[total_units] >= $ad[units] && $ad[units] > 0 || $ad[expires] > 0 && $ad[expires] <= time()) {
             $this->db->getsql("UPDATE adrev_ads SET status=-2 WHERE id=?", array($ad['id']));
         }
         // Log into the clicklog
         $fp = fopen("cache/" . date("Ymd") . "_clicks.csv", "a");
         if (flock($fp, LOCK_EX)) {
             $ref = $_SERVER[HTTP_REFERER];
             $req = $_SERVER[REQUEST_URI];
             $uid = $c->cookie[visitorid];
             $ip = $_SERVER[REMOTE_ADDR];
             $ld = date("r");
             $line = "\"{$ld}\",\"{$ip}\",\"{$uid}\",\"{$this->affid}\",\"{$amount}\",\"{$ad['url']}\",\"{$ref}\",\"{$req}\"\n";
             fputs($fp, $line, strlen($line));
             flock($fp, LOCK_UN);
         }
         fclose($fp);
         // Redirect
         header("Location: {$ad['url']}");
         exit;
     }
     exit;
 }
 function impressions()
 {
     // Instantiate the cookielib
     $c = new cookielib();
     // Iterate through the ads
     reset($this->adlist);
     $inlist = array();
     foreach ($this->adlist as $ad) {
         // Check if we are below the impressions window
         $k = $c->get("_i_" . $ad['id'] . "_" . $this->keywordid);
         $win = time() - $k;
         if ($k > 0 && $win < $this->default['adrevenue']['dup_impressions']) {
             continue;
         }
         // We have at least 1 non-duplicate ad
         $this->dup = 0;
         // Save the impressions count
         $c->set("_i_" . $ad['id'] . "_" . $this->keywordid, time());
         // Try to load this record
         $date = date("Y-m-d");
         $a = $this->db->getsql("SELECT * FROM adrev_traffic WHERE adid=? AND date=? AND keywordid=?", array($ad['id'], $date, $this->keywordid));
         // Calculate rate to post here
         $t = time();
         if ($this->z['rate_type'] == "CPM") {
             // Portion of cost per 1000 impressions
             $amount = $ad['bid'] * 1 ? $ad['bid'] / 1000 : $this->z['rate'] / 1000;
             // Update ad units
             $this->db->getsql("UPDATE adrev_ads SET total_units=total_units+1 WHERE id=?", array($ad['id']));
             $ad['total_units'] += 1;
         } elseif ($this->z['rate_type'] == "CPD") {
             // Cost per day
             if (!$a[0]['id']) {
                 $amount = $ad['bid'] > 0 ? $ad['bid'] : $this->z['rate'];
                 $this->db->getsql("UPDATE adrev_ads SET total_units=total_units+1 WHERE id=?", array($ad['id']));
                 $ad['total_units'] += 1;
             } else {
                 $amount = 0;
             }
             // we already posted it today!
         } elseif ($this->z['rate_type'] == 'CPI') {
             // Cost per impression
             $amount = $ad['bid'] * 1 ? $ad['bid'] : $this->z['rate'];
             // Update ad units
             $this->db->getsql("UPDATE adrev_ads SET total_units=total_units+1 WHERE id=?", array($ad['id']));
             $ad['total_units'] += 1;
         } else {
             $amount = 0;
         }
         // Log traffic stats and amounts
         if (!$a[0]['id']) {
             // Add a new traffic record
             $i = array();
             $i['userid'] = $ad['userid'];
             $i['date'] = $date;
             $i['adid'] = $ad['id'];
             $i['keywordid'] = $this->keywordid;
             $i['impressions'] = 1;
             $i['amount'] = $amount;
             $this->db->insert("adrev_traffic", $i);
         } else {
             // Update it
             $amount = $amount ? $amount : 0;
             $id = $a[0]['id'];
             $this->db->getsql("UPDATE adrev_traffic SET impressions=impressions+1, amount=amount+{$amount} WHERE id=?", array($id));
         }
         // Update real time balance if we actually have an amount
         $t = time();
         if ($amount > 0) {
             $this->db->getsql("UPDATE adrev_users SET balance=balance-{$amount}, balance_update=? WHERE id=?", array($t, $ad['userid']));
             if ($this->affid > 0) {
                 $aff_amount = $amount * ($this->z['aff_percent'] / 100);
                 $pub_rates = false;
                 // Check if we have tiered affiliate rates
                 if ($this->z['pub_rates'] && !$aff_amount) {
                     $pub_rates = @unserialize($this->z['pub_rates']);
                     // Zone affiliate tiered rates
                 } elseif ($this->default['adrevenue']['pub_rates'] && !$aff_amount) {
                     // Global affiliate tiered rates
                     $pub_rates = @unserialize($this->default['adrevenue']['pub_rates']);
                 }
                 // Set the apropro tiered affiliate rate
                 if (is_array($pub_rates) && $aff_amount == 0) {
                     $sp = $this->db->getsql('SELECT sum(amount) as total FROM adrev_aff_traffic WHERE affid=?', array($this->affid));
                     if ($sp[0]) {
                         $sp[0]['total'] = $sp[0]['total'] > 0 ? $s[0]['total'] = 0 : 0;
                         foreach ($pub_rates as $rate) {
                             if ($sp[0]['total'] >= $rate[0] && $sp[0]['total'] <= $rate[1] && $rate[2] > 0) {
                                 $aff_amount = $amount * ($rate[2] / 100);
                                 break;
                             }
                         }
                     }
                 }
                 if ($aff_amount > 0) {
                     $i = array();
                     $i['date'] = time();
                     $i['affid'] = $this->affid;
                     $i['adtype'] = $this->z['rate_type'];
                     $i['adid'] = $ad['id'];
                     $i['ip'] = $_SERVER['REMOTE_ADDR'];
                     $i['referer'] = $_SERVER['HTTP_REFERER'];
                     $i['amount'] = $aff_amount;
                     $i['spend'] = $amount;
                     $this->db->insert("adrev_aff_traffic", $i);
                 }
             }
         }
         // Attempt to expire this ad
         if ($ad['total_units'] >= $ad['units'] && $ad['units'] > 0 || $ad['expires'] > 0 && $ad['expires'] <= time()) {
             $this->db->getsql("UPDATE adrev_ads SET status=-2 WHERE id=?", array($ad['id']));
         }
         // Try to expire based on ad spend
         if ($ad['spend_limit'] > 0) {
             // Get the ad balance (this sucks)
             $ab = $this->db->getsql('SELECT sum(amount) as total FROM adrev_traffic WHERE adid=?', array($ad['id']));
             if ($ab[0]['total'] > 0 && $ab[0]['total'] >= $ad['spend_limit']) {
                 # Expire the ad
                 $this->db->getsql("UPDATE adrev_ads SET status=-2 WHERE id=?", array($ad['id']));
             }
         }
     }
     return TRUE;
 }