function main()
 {
     global $DEFAULT;
     // Set defaults
     $this->default = $DEFAULT;
     $this->tz = lib_getmicrotime();
     // Setup database lib
     $this->db = new database();
     if ($this->default['engine'] == "pg") {
         $this->db->pg_database($this->default['host'], $this->default['database'], $this->default['user'], $this->default['password'], $this->default['port']);
     }
     if ($this->default['engine'] == "mysql") {
         $this->db->mysql_database($this->default['host'], $this->default['database'], $this->default['user'], $this->default['password']);
     }
     // Loadup whatever settings we have
     $settings = array();
     /*
     if(file_exists("cache/settings.cache"))
     {
     	$settings = @unserialize(@join("", @file("cache/settings.cache")));
     	$s = stat("cache/settings.cache");
     	if($s['ctime'] + 300 < time())
     		unlink("cache/settings.cache");
     }
     */
     // Grab from the DB
     if (@count($settings) == 0) {
         $settings = $this->db->getsql("SELECT * FROM adrev_settings");
         #$fp = fopen("cache/settings.cache", "w");
         #fputs($fp, serialize($settings));
         #fclose($fp);
     }
     if (@count($settings) > 0) {
         foreach ($settings as $rec) {
             $this->default['revsense'][stripslashes($rec['name'])] = stripslashes($rec['value']);
         }
         $DEFAULT['adrevenue'] = $this->default['revsense'];
     }
     // Process input
     $this->http = new http();
     $this->input = new input();
     $this->output = new output();
     return 0;
 }
 function _default()
 {
     // Our Duplicate flag
     $this->dup = 1;
     // Get variables
     if (!$this->zone) {
         $this->zone = $_GET['id'];
     }
     if (!$this->affid) {
         $this->affid = $_GET['affid'] ? $_GET['affid'] : 0;
     }
     if (!$this->rows) {
         $this->rows = $_GET['rows'] ? $_GET['rows'] : 0;
     }
     if (!$this->cols) {
         $this->cols = $_GET['cols'] ? $_GET['cols'] : 0;
     }
     if (!$this->ad_sort) {
         $this->ad_sort = $_GET['ad_sort'] ? $_GET['ad_sort'] : "asc";
     }
     if (!$this->format) {
         $this->format = $_GET['output'] ? $_GET['output'] : "html";
     }
     if (!$this->keyword) {
         $this->keyword = $_GET['keyword'];
     }
     $this->keywordid = 0;
     $this->keywordrate = 0.0;
     // Setup default headers
     // P3P: CP="NOI DSP COR PSAo PSDo OUR BUS OTC"
     if (!$this->default['adrevenue']['p3p']) {
         header('P3P: CP="NOI DSP COR PSAo PSDo OUR BUS OTC"');
     } else {
         header('P3P: CP="' . $this->default['adrevenue']['p3p'] . '"');
     }
     header("Cache-Control: no-store, no-cache, must-revalidate");
     header("Pragma: no-cache");
     header("X-Vendor: W3matter LLC | RevSense | http://www.revsense.com");
     // Get the zone
     if (!$this->zone) {
         return "";
     }
     $this->z = lib_cache_get("zone", $this->zone);
     if (!$this->z) {
         $p = $this->db->getsql("SELECT * FROM adrev_zones WHERE id=?", array($this->zone));
         $this->z = $p[0];
         lib_cache_put("zone", $this->zone, $p[0]);
     }
     // Check if this zone is active
     if ($this->z['status'] == 0) {
         return "";
     }
     // Check dayparting
     if ($this->z['daypart_enable'] == 1) {
         $day = date("w");
         $hour = date("G");
         if (!in_array($hour, lib_bit_options($this->z['daypart_hours']))) {
             return "";
         }
         if (!in_array($day, lib_bit_options($this->z['daypart_days']))) {
             return "";
         }
     }
     // Check geotargetting
     // **** This function will be added in a point release **** //
     // Override some zone settings that a user might have passed in
     if ($_GET['rows'] > 0) {
         $this->z['rows'] = $_GET['rows'];
     }
     if ($_GET['cols'] > 0) {
         $this->z['cols'] = $_GET['cols'];
     }
     if ($_GET['max_display_ads'] > 0) {
         $this->z['max_display_ads'] = $_GET['max_display_ads'];
     }
     if ($_GET['ad_sort']) {
         $this->z['ad_sort'] = $_GET['ad_sort'];
     }
     if ($_GET['rows'] && $_GET['cols']) {
         $this->z['max_display_ads'] = $_GET['rows'] * $_GET['cols'];
     }
     if (!$_GET['fuzzy']) {
         $this->z['keywords_fuzzy'] = $this->fuzzy ? $this->fuzzy : $this->z['keywords_fuzzy'];
     }
     // Get ads
     if (!$this->ads()) {
         return "";
     }
     // Update the log and billing info
     $this->impressions();
     include_once "modules/preview.php";
     $x = 0;
     $c = new preview();
     $c->affid = $this->affid;
     $c->keyid = $this->keywordid;
     if ($this->format == "js" || $this->format == "html" || $this->format == "raw") {
         reset($this->adlist);
         if ($this->z['layout_template']) {
             $tpl = new XTemplate("MEM", "<!-- BEGIN: main -->\n" . $this->z['layout_template'] . "\n<!-- END: main -->");
         } else {
             $tpl = new XTemplate("MEM", "<!-- BEGIN: main -->\n" . join("", file("templates/zone_default_layout.html")) . "\n<!-- END: main -->\n");
         }
         foreach ($this->adlist as $rec) {
             $x++;
             $parsed = 0;
             $c->zid = $rec['zid'];
             $oz = $c->format_ad("html");
             $tpl->assign("AD", $oz);
             $tpl->parse("main.row.col");
             if ($x >= $this->z['cols']) {
                 $x = 0;
                 $parsed = 1;
                 $tpl->parse("main.row");
             }
         }
         // Finish up parsing
         if (!$parsed) {
             $tpl->parse("main.row");
         }
         $tpl->parse("main");
         $out = $tpl->text("main");
         if ($this->format == "js") {
             $o1 = str_replace("'", "\\'", $out);
             $o1 = str_ireplace('<script', "<scr'+'ipt", $o1);
             $o1 = str_ireplace('</script>', "</scr'+'ipt>", $o1);
             $o = explode("\n", $o1);
             $oo = array();
             if (count($o) > 0) {
                 foreach ($o as $rec) {
                     if (strlen(trim($rec)) > 0) {
                         $oo[] = trim($rec);
                     }
                 }
             }
             $date = date("r");
             $nocache = md5(uniqid("")) . " | " . $_SERVER['REMOTE_ADDR'] . " | " . iif($this->dup, 'DUP', '');
             $out = "// W3matter.com | RevSense | http://www.w3matter.com\n";
             $out .= "// {$date}\n";
             $out .= "// Ad Code: {$nocache}\n\n";
             $out .= "document.write('" . implode("');\ndocument.write('", $oo) . "');\n";
             header("Content-type: text/javascript");
             echo $out;
             exit;
         }
     } elseif ($this->format == "xml") {
         // XML output
         $out = '<?xml version="1.0" encoding="UTF-16"?>' . "\n";
         reset($this->adlist);
         foreach ($this->adlist as $ad) {
             $c->zid = $ad['zid'];
             $out .= $c->format_ad("xml");
         }
         header("Content-type: text/xml");
     } else {
         // CSV output
         reset($this->adlist);
         foreach ($this->adlist as $ad) {
             $c->zid = $ad['zid'];
             $out .= $c->format_ad("csv");
         }
         header("Content-type: text/csv");
     }
     $net = lib_getmicrotime() - $this->tz;
     header("X-Server-Time: {$net}");
     echo $out;
     return "";
 }