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 display($ad = array())
 {
     if (!$this->zid) {
         return "";
     }
     // Try to grab from the cache
     #		$template = lib_cache_get("ad_html", $this->zid);
     if ($template) {
         return $template;
     }
     // Grab data
     #		$ad = lib_cache_get("ad", $this->zid);
     if (!$ad[id]) {
         $a = $this->db->getsql("SELECT * FROM adrev_ads WHERE zid=?", array($this->zid));
         $ad = $a[0];
         if ($ad[id] && $this->default[adrevenue][cache] > 0) {
             lib_cache_put("ad", $this->zid, $ad);
         }
     }
     #		$zone = lib_cache_get("zone", $ad[zone]);
     if (!$zone) {
         $z = $this->db->getsql("SELECT * FROM adrev_zones WHERE id=?", array($ad['zone']));
         $zone = $z[0];
         if ($z[0][id] && $this->default[adrevenue][cache] > 0) {
             lib_cache_put("zone", $zone[id], $zone);
         }
     }
     if (!$ad[id] || !$zone[id]) {
         return "";
     }
     $ad_format = unserialize(stripslashes($zone[ad_format]));
     $template = stripslashes($zone[template]);
     $search = array();
     $replace = array();
     foreach ($ad_format as $rec) {
         $type = $rec[type];
         $val = stripslashes($ad[strtolower($type)]);
         // Deal with URL based on zone settings
         // We will replace the URLSTRING variable
         if ($type == "URL") {
             $urlstr = "";
             // If we have a keyword, then get its URL
             if ($this->keyid) {
                 $k = $this->db->getsql("SELECT url FROM adrev_keyword_map WHERE keywordid=? AND adid=? LIMIT 1", array($this->keyid, $ad['id']));
                 if ($k[0][url]) {
                     $val = $k[0][url];
                 }
             }
             // Use tracking url or not
             $hostname = rtrim($this->default[adrevenue][hostname], "/") . "/index.php?section=redir&zid={$this->zid}&affid={$this->affid}&kid={$this->keyid}";
             if (!$zone[urls_tracking]) {
                 $urlstr = " href=\"{$hostname}\"";
             } else {
                 $urlstr = " href=\"{$val}\"";
             }
             // Hide URL in the status bar
             if ($zone[urls_hide]) {
                 $parts = parse_url($val);
                 if ($ad[display_url]) {
                     $parts[host] = $ad[display_url];
                 }
                 $urlstr .= " onmouseover=\"window.status='Go to: {$parts['host']}';return true\"";
                 $urlstr .= " onmouseout=\"window.status=''; return true\"";
             }
             // Setup target
             $urlstr .= " target=\"{$zone['urls_target']}\"";
             $search[] = "{" . "URLSTRING" . "}";
             $replace[] = $urlstr;
         } elseif ($type != "IMAGE") {
             // Contrain Length
             if ($rec[max_length] > 0) {
                 $val = substr($val, 0, $rec[max_length]);
             }
             // Set search name
             $search[] = "{" . $type . "}";
             if ($type != 'CONTENT') {
                 // Optionally make value bold
                 if ($rec[bold] == 1 && $type != "URL") {
                     $val = "<b>{$val}</b>";
                 }
                 // Setup the font
                 $val = "<font face=\"{$rec['font']}\" size=\"{$rec['size']}\" color=\"{$rec['color']}\">{$val}</font>";
                 $replace[] = $val;
                 // Font
                 $search[] = "{" . $type . "_FONT" . "}";
                 $replace[] = $rec[font];
                 // Size
                 $search[] = "{" . $type . "_SIZE" . "}";
                 $replace[] = $rec[size];
                 // Color
                 $search[] = "{" . $type . "_COLOR" . "}";
                 $replace[] = $rec[color];
             } else {
                 $replace[] = $val;
             }
         } else {
             // This is an image
             if (preg_match('/\\.swf$/i', $val)) {
                 // Handle flash images
                 $imgstr = "<OBJECT classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" ";
                 $imgstr .= "codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0\"";
                 $imgstr .= "WIDTH=\"{$rec['width']}\" HEIGHT=\"{$rec['height']}\" id=\"{$this->zid}\">";
                 $imgstr .= "<PARAM NAME=movie VALUE=\"{$val}\"><PARAM NAME=quality VALUE=high>";
                 $imgstr .= "<PARAM NAME=bgcolor VALUE=#FFFFFF>";
                 $imgstr .= "<EMBED src=\"{$val}\" quality=high bgcolor=#FFFFFF ";
                 $imgstr .= "WIDTH=\"{$rec['width']}\" HEIGHT=\"{$rec['height']}\" NAME=\"{$this->zid}\"";
                 $imgstr .= "ALIGN=\"\" TYPE=\"application/x-shockwave-flash\"";
                 $imgstr .= "PLUGINSPAGE=\"http://www.macromedia.com/go/getflashplayer\">";
                 $imgstr .= "</EMBED>\n</OBJECT>\n";
             } else {
                 // Deal with alternate images
                 $imgstr = "<img src=\"{$val}\" width=\"{$rec['width']}\" height=\"{$rec['height']}\" alt=\"Click here\" border=\"0\">";
             }
             $search[] = "{" . "IMAGESTRING" . "}";
             $replace[] = $imgstr;
             // Also add image
             $search[] = "{" . "IMAGE" . "}";
             $replace[] = $val;
         }
     }
     // Run through the template and do replacements
     $template = str_replace($search, $replace, $template);
     // Then get rid of any stray template variables
     $template = preg_replace('/\\{.*?\\}/', "", $template);
     // Save the ad in the cache
     lib_cache_put("ad_html", $this->zid, $template);
     return $template;
 }