function ads()
 {
     // Set the order for ads in this zone
     $order = "";
     if ($this->z['ad_sort'] == "asc") {
         $order = "ORDER BY a.id";
     } elseif ($this->z['ad_sort'] == "desc") {
         $order = "ORDER BY a.id DESC";
     } elseif ($this->z['ad_sort'] == "bid") {
         $order = "ORDER BY  a.bid DESC";
     }
     if ($this->z['keywords_enable'] != 1) {
         $ads = lib_cache_get("zoneads", $this->zone);
         // Just grab all the active ads in this zone
         $ads = $this->db->getsql("SELECT a.id,a.userid,a.bid,a.zid,a.daypart_days,\r\n\t\t\t\t\t\t\t\t\t\t\ta.daypart_hours,a.startdate,a.expires, a.units, a.total_units\r\n\t\t\t\t\t\t\t\t\t\tFROM adrev_ads a, adrev_users b \r\n\t\t\t\t\t\t\t\t\t\tWHERE a.userid=b.id AND a.zone=? \r\n\t\t\t\t\t\t\t\t\t\t\tAND a.status = '1' AND b.balance > 0 {$order}", array($this->zone));
         if (count($ads) > 0 && $this->default['adrevenue']['cache'] > 0) {
             lib_cache_put("zoneads", $this->zone, $ads);
         }
     } else {
         // If there is no keyword
         if (!trim($this->keyword)) {
             return FALSE;
         }
         // Try to find a keyword
         $keyword = strtolower(trim($this->keyword));
         if (!$this->z['keywords_fuzzy']) {
             // Exact matching
             $keywordid = $this->db->get_keyword($keyword, 0);
         } else {
             // Fuzzy matching
             // Delete stopwords first
             #$keyword = trim(lib_stopwords($keyword));
             // Compute metaphone now
             $mphone = metaphone($keyword);
             if (!$mphone) {
                 return FALSE;
             }
             $keywordid = "";
             $words = $this->db->getsql("SELECT * FROM adrev_keywords WHERE fuzzy_keyword=?", array($mphone));
             if (count($words) > 0) {
                 // Look for the one that is closest using a reverse bubble sort
                 $keywordid = $words[0]['id'];
                 $z = 100;
                 foreach ($words as $w) {
                     // Lookup the distance between these candidates we got back
                     // for the best fuzzy match
                     $l = levenshtein($w['keyword'], $keyword);
                     if ($l <= $z) {
                         $keywordid = $w['id'];
                         $z = $l;
                     }
                 }
             }
         }
         // We have no keyword ID
         if (!$keywordid) {
             return FALSE;
         } else {
             // Grab the ID and the keyword rate
             $this->keywordid = $keywordid;
             $w = $this->db->getsql("SELECT mincpc FROM adrev_keywords WHERE id=?", array($keywordid));
             $this->keywordrate = $w[0]['mincpc'] ? $w[0]['mincpc'] : $this->default['adrevenue']['min_bid'];
         }
         //Try from the keyword cache
         $ads = lib_cache_get("zoneads" . $keywordid, $this->zone);
         // If the cache is empty, grab from the DB
         // Decide on the sort order
         if ($this->z['ad_sort'] == "bid") {
             $order = "ORDER BY c.bid DESC";
         }
         // Grab ads with a specific keyword id
         $ads = $this->db->getsql("SELECT a.id,a.userid,a.bid,a.zid,a.daypart_days,\r\n\t\t\t\t\t\t\t\t\t\t\ta.daypart_hours,a.startdate,a.expires,a.units,a.total_units\r\n\t\t\t\t\t\t\t\t\t\tFROm adrev_ads a, adrev_users b, adrev_keyword_map c\r\n\t\t\t\t\t\t\t\t\t\tWHERE a.userid=b.id AND a.id=c.adid AND a.zone=? AND a.status='1'\r\n\t\t\t\t\t\t\t\t\t\t\tAND c.keywordid=? AND b.balance >0 {$order}", array($this->zone, $keywordid));
         if (count($ads) > 0 && $this->default['adrevenue']['cache'] > 0) {
             lib_cache_put("zoneads" . $keywordid, $this->zone, $ads);
         }
     }
     $n = count($ads);
     // We have no ads
     if ($n == 0) {
         // Try for a default ad
         if ($this->z['default_ad'] > 0) {
             $ads = $this->db->getsql("SELECT id,userid FROM adrev_ads WHERE id=?", array($this->z['default_ad']));
         }
         if (count($ads) == 0) {
             return FALSE;
         }
     }
     // Setup a simple array of the ads
     // Makes it easier to manipulate later
     $a = array();
     foreach ($ads as $ad) {
         // Are we before start date? Skip the ad then
         if ($ad['startdate'] > 0 && $ad['startdate'] > time()) {
             continue;
         }
         // Do we have dayparting turned on?
         if ($ad['daypart_hours'] > 0 && $ad['daypart_days'] > 0) {
             $day = date("w");
             $hour = date("G");
             if (!in_array($hour, lib_bit_options($ad['daypart_hours'])) && $ad['daypart_hours'] > 0) {
                 continue;
             }
             if (!in_array($day, lib_bit_options($ad['daypart_days'])) && $ad['daypart_days'] > 0) {
                 continue;
             }
         }
         $a[] = $ad;
     }
     // Order the set randomly if we requested it
     reset($a);
     if ($this->z['ad_sort'] == "rand") {
         shuffle($a);
     }
     // Grab the quantity we need
     reset($a);
     if ($n > $this->z['max_display_ads']) {
         $a = array_slice($a, 0, $this->z['max_display_ads']);
     }
     // Set the number of ads to get back
     $this->adlist = $a;
     return TRUE;
 }
 function schedule()
 {
     $this->output->secure();
     $f = $this->input->f;
     $uid = $_SESSION['user']['id'];
     $tpl = new XTemplate("templates/ad_daypart.html");
     if ($_SESSION['user']['admin'] == 3) {
         $xad = $this->db->getsql("SELECT userid FROM adrev_ads WHERE zid=?", array($f['id']));
         $uid = $xad['0']['userid'];
     }
     // Loadup the ad
     $ad = $this->db->getsql("SELECT a.*,b.rate_type FROM adrev_ads a, adrev_zones b\r\n\t\t\t\t\t\t\t\t\tWHERE a.zid=? AND a.userid=? AND a.zone=b.id", array($f['id'], $uid));
     // Update the ad
     if ($f['submit'] && $ad['0']['id']) {
         // Compute the days
         $f['startdate'] = 0;
         $f['expires'] = 0;
         if ($f['start_month'] && $f['start_day'] && $f['start_year']) {
             $f['startdate'] = strtotime("{$f['start_month']}/{$f['start_day']}/{$f['start_year']} 00:00:01");
         }
         if ($f['expire_month'] && $f['expire_day'] && $f['expire_year']) {
             $f['expires'] = strtotime("{$f['expire_month']}/{$f['expire_day']}/{$f['expire_year']} 23:59:59");
         }
         $i = array();
         $i['daypart_days'] = lib_options($f['daypart_days']);
         $i['daypart_hours'] = lib_options($f['daypart_hours']);
         $i['startdate'] = $f['startdate'];
         $i['expires'] = $f['expires'];
         $i['units'] = $f['units'];
         $i['spend_limit'] = $f['spend_limit'];
         $this->db->update("adrev_ads", "zid", $f['id'], $i);
         $this->output->redirect(lib_lang("Daypart options were updated"), "index.php?section=ads&action=schedule&f[id]={$f['id']}", 1);
         exit;
     }
     // Show the form
     $tpl = new XTemplate("templates/ad_daypart.html");
     // Days
     $f['daypart_days'] = lib_bit_options($ad['0']['daypart_days']);
     for ($day = 0; $day < 7; $day++) {
         $tpl->assign("DAY", $day);
         if (in_array($day, $f['daypart_days'])) {
             $tpl->assign("DAYPART_DAY", "CHECKED");
         } else {
             $tpl->assign("DAYPART_DAY", "");
         }
         $tpl->parse("main.days");
     }
     // Hours
     $f['daypart_hours'] = lib_bit_options($ad['0']['daypart_hours']);
     for ($hour = 0; $hour < 24; $hour++) {
         $tpl->assign("HOUR_TITLE", $hour);
         $tpl->parse("main.hour_title");
         $tpl->assign("HOUR", $hour);
         if (in_array($hour, $f['daypart_hours'])) {
             $tpl->assign("DAYPART_HOUR", "CHECKED");
         } else {
             $tpl->assign("DAYPART_HOUR", "");
         }
         $tpl->parse("main.hours");
     }
     $tpl->assign("ID", $f['id']);
     $tpl->assign("TZ", date("T"));
     $tpl->assign("STARTDATE", lib_dateinput("start", $ad['0']['startdate'], date("Y"), 4));
     $tpl->assign("ENDDATE", lib_dateinput("expire", $ad['0']['expires'], date("Y"), 4));
     $tpl->assign("UNITS", $ad['0']['units']);
     $tpl->assign("TOTAL_UNITS", $ad['0']['total_units']);
     $tpl->assign('SPEND_LIMIT', $ad['0']['spend_limit']);
     if ($ad['0']['rate_type'] == "CPC") {
         $tpl->assign("SUNITS", "@@Clicks@@");
     } elseif ($ad['0']['rate_type'] == "CPM") {
         $tpl->assign("SUNITS", "@@Impressions@@");
     } elseif ($ad['0']['rate_type'] == "CPD") {
         $tpl->assign("SUNITS", "@@Days@@");
     } elseif ($ad['0']['rate_type'] == "CPA") {
         $tpl->assign("SUNITS", "@@Orders@@");
     }
     $tpl->assign("ADS_MENU", $this->menu($_REQUEST['f']['id']));
     $tpl->parse("main");
     $this->title = lib_lang("Scheduling");
     $this->content = $tpl->text("main");
     $this->display();
     $this->printpage();
     exit;
 }
 function zone_daypart()
 {
     $this->output->admin();
     $f = $this->input->f;
     // Loadup the zone
     $z = $this->db->getsql("SELECT * FROM adrev_zones WHERE id=?", array($f['id']));
     if (!$z[0][id]) {
         $this->output->redirect(lib_lang("The zone could not be found"), "index.php?section=zone", 3);
         exit;
     }
     // Update the zone
     if ($f[submit]) {
         $i = array();
         $i[daypart_enable] = $f[daypart_enable] ? 1 : 0;
         $i[daypart_days] = lib_options($f[daypart_days]);
         $i[daypart_hours] = lib_options($f[daypart_hours]);
         $this->db->update("adrev_zones", "id", $f[id], $i);
         $this->output->redirect(lib_lang("Daypart options were updated"), "index.php?section=zone&action=zone_daypart&f[id]={$f['id']}", 1);
         exit;
     }
     // Show the form
     $tpl = new XTemplate("templates/zone_daypart.html");
     // Days
     $f[daypart_days] = lib_bit_options($z[0][daypart_days]);
     for ($day = 0; $day < 7; $day++) {
         $tpl->assign("DAY", $day);
         if (in_array($day, $f[daypart_days])) {
             $tpl->assign("DAYPART_DAY", "CHECKED");
         } else {
             $tpl->assign("DAYPART_DAY", "");
         }
         $tpl->parse("main.days");
     }
     // Hours
     $f[daypart_hours] = lib_bit_options($z[0][daypart_hours]);
     for ($hour = 0; $hour < 24; $hour++) {
         $tpl->assign("HOUR_TITLE", $hour);
         $tpl->parse("main.hour_title");
         $tpl->assign("HOUR", $hour);
         if (in_array($hour, $f[daypart_hours])) {
             $tpl->assign("DAYPART_HOUR", "CHECKED");
         } else {
             $tpl->assign("DAYPART_HOUR", "");
         }
         $tpl->parse("main.hours");
     }
     $tpl->assign("ID", $f[id]);
     $tpl->assign("TZ", date("T"));
     $tpl->assign("DAYPART_ENABLE", $z[0][daypart_enable] ? "CHECKED" : "");
     $tpl->assign("ZONE_MENU", $this->zone_menu($f[id]));
     $tpl->parse("main");
     $this->title = lib_lang("Manage") . "[" . stripslashes($z[0][name]) . "] : " . lib_lang("Dayparting");
     $this->content = $tpl->text("main");
     $this->display();
     $this->printpage();
     exit;
 }