function history()
 {
     $this->output->secure();
     $f = $this->input->f;
     $uid = $_SESSION[user][id];
     $gen = new formgen();
     $gen->startrow("#CCCCCC");
     $gen->column("<b>" . lib_lang("Date") . "</b>");
     $gen->column("<b>" . lib_lang("Description") . "</b>");
     $gen->column("<b>" . lib_lang("Amount") . "</b>", "", "", "", "right");
     $gen->endrow();
     // Get the data
     $data = $this->db->getsql("SELECT * FROM adrev_payments WHERE userid=? ORDER BY date", array($uid));
     $total = 0;
     if (count($data) > 0) {
         foreach ($data as $rec) {
             $bgcolor = $bgcolor == "#FFFFFF" ? "#FFFFEE" : "#FFFFFF";
             $gen->startrow($bgcolor);
             $gen->column(date("M d Y", $rec[date]));
             $gen->column($rec[description]);
             $gen->column(number_format($rec[amount], 2), "", "", "", "right");
             $gen->endrow();
             $total += $rec[amount];
         }
         $gen->startrow("#FFFFFF");
         $gen->column();
         $gen->column();
         $gen->column(number_format($total, 2), "#CCCCCC", "", "", "right");
         $gen->endrow();
     }
     $this->title = lib_lang("Transaction History");
     $this->content = $gen->gentable("400", 0, 1, 3, "#FFFFFF");
     $this->display();
     $this->printpage();
     exit;
 }
 function stats()
 {
     $this->output->admin();
     $f = $this->input->f;
     $tpl = new XTemplate("templates/pub_user.html");
     if (!$f[page]) {
         $f[page] = 1;
     }
     if (!$f[date]) {
         $f[date] = "thismonth";
     }
     if (!$f[sort]) {
         $f[sort] = "date DESC,ip";
     }
     list($startdate, $enddate) = lib_date_range($f['date']);
     // Grab the data for that page
     $limit = 100;
     $offset = ($f[page] - 1) * $limit;
     // Count records in set first
     $sdate = strtotime($startdate);
     $edate = strtotime($enddate) + 86400;
     $recs = $this->db->getsql("SELECT count(*) as num, sum(amount) as amount FROM adrev_aff_traffic \r\n\t\t\t\t\t\t\t\t\tWHERE affid=? AND date BETWEEN ? AND ?", array($f['id'], $sdate, $edate));
     $z = $recs[0][num];
     $amount = $recs[0][amount];
     $pages = ceil($z / $limit);
     $prevpage = $f[page] - 1;
     $nextpage = $f[page] + 1;
     if ($f[page] > 1) {
         $pager .= "<a href=?section=pubadmin&action=stats&f[id]={$f['id']}&f[page]={$prevpage}&f[date]={$f['date']}&f[sort]={$f['sort']}&f[ip]={$f['ip']}>&laquo;Previous</a>&nbsp;";
     }
     $pager .= "<b>" . number_format($z) . "</b> records. Page <font color=red>{$f['page']}</font> of <b>{$pages}</b> pages. ";
     if ($pages > $f[page]) {
         $pager .= "&nbsp;<a href=?section=pubadmin&action=stats&f[id]={$f['id']}&f[page]={$nextpage}&f[date]={$f['date']}&f[sort]={$f['sort']}&f[ip]={$f['ip']}>Next&raquo;</a>";
     }
     // Show up to 20 page selector
     $pagelist = "";
     for ($x = 1; $x <= $pages; $x++) {
         if ($x == $f[page]) {
             $pagelist .= "&nbsp;<b>{$x}</b>";
         } else {
             $pagelist .= "&nbsp;<a href=?section=pubadmin&action=stats&f[id]={$f['id']}&f[page]={$x}&f[date]={$f['date']}&f[sort]={$f['sort']}&f[ip]={$f['ip']}>{$x}</a>";
         }
         if ($x >= 20) {
             break;
         }
     }
     $pager .= "&nbsp;&nbsp;&nbsp;{$pagelist}";
     $tpl->assign("PAGER", $pager);
     if ($z > 0) {
         if ($f[ip]) {
             $extra = "AND ip='{$f['ip']}'";
         }
         $recs = $this->db->getsql("SELECT * FROM adrev_aff_traffic \r\n\t\t\t\t\t\t\t\t\tWHERE affid=? AND (date BETWEEN ? AND ?) {$extra}\r\n\t\t\t\t\t\t\t\t\tORDER BY ? LIMIT {$limit} OFFSET {$offset}", array($f['id'], $sdate, $edate, $f['sort']));
         $gen = new formgen();
         $gen->startrow("#CCCCCC");
         $gen->column("<b>" . lib_lang("Date") . "</b>");
         $gen->column("<b>" . lib_lang("Type") . "</b>");
         $gen->column("<b>" . lib_lang("IP") . "</b>");
         $gen->column("<b>" . lib_lang("Amount") . "</b>", "", "", "", "right");
         $gen->column("<b>" . lib_lang("Page") . "</b>");
         $gen->endrow();
         foreach ($recs as $rec) {
             $ref = stripslashes($rec[referer]);
             if (strlen($ref) > 60) {
                 $ref = substr($ref, 0, 30) . "..." . substr($ref, -30);
             }
             $bgcolor = $bgcolor == "#FFFFFF" ? "#FFFFEE" : "#FFFFFF";
             $gen->startrow($bgcolor);
             $gen->column(date("m-d-Y h:i:sa", $rec[date]));
             $gen->column($rec[adtype]);
             $gen->column($rec[ip]);
             $gen->column(number_format($rec[amount], 3), "", "", "", "right");
             $gen->column("<A href=\"{$rec['referer']}\" title=\"Open in new window\" target=\"_new\">{$ref}</a>");
             $gen->endrow();
         }
         $tpl->assign("TABLE", $gen->gentable("100%", 0, 1, 3, "#FFFFFF"));
     }
     $dates = array("today" => lib_lang('Today'), "yesterday" => lib_lang('Yesterday'), "thisweek" => lib_lang('This Week'), "lastweek" => lib_lang('Last Week'), "thismonth" => lib_lang('This Month'), "lastmonth" => lib_lang('Last Month'), all => lib_lang('All Time'));
     $tpl->assign("DATELIST", lib_htlist_array($dates, $f[date]));
     $tpl->assign("SORTLIST", lib_htlist_array(array('ip,date DESC' => 'IP Address', 'date DESC,ip' => 'Date'), $f[sort]));
     $tpl->assign("ID", $f[id]);
     $tpl->assign("IP", $f[ip]);
     $tpl->assign("BALANCE", number_format($amount, 3));
     $tpl->parse("stats");
     $this->title = lib_lang("Publisher Statistics");
     $this->content = $tpl->text("stats");
     $this->display();
     $this->printpage();
     exit;
 }
 function keyword_stats()
 {
     $this->output->secure();
     $f = $this->input->f;
     $uid = $_SESSION['user']['id'];
     // Grab the ad info
     $ad = $this->db->getsql("SELECT * FROM adrev_ads WHERE zid=? AND userid=?", array($f['id'], $uid));
     $id = $ad['0']['id'];
     $zoneid = $ad['0']['zone'];
     $z = $this->db->getsql("SELECT * FROM adrev_zones WHERE id=?", array($zoneid));
     $zone = $z['0'];
     // Lets update bids
     if (count($_REQUEST['bid']) > 0) {
         $minbid = $zone['rate'];
         while (list($xid, $rec) = each($_REQUEST['bid'])) {
             if (!$this->default['adrevenue']['max_bid']) {
                 $this->default['adrevenue']['max_bid'] = 50;
             }
             if ($rec['bid'] >= $minbid && $rec['bid'] <= $this->default['adrevenue']['max_bid']) {
                 $this->db->getsql("UPDATE adrev_keyword_map SET bid=?, url=? WHERE id=? AND adid=?", array($rec['bid'], $rec['url'], $xid, $id));
             }
         }
     }
     // Delete a keyword
     if ($_REQUEST['delete']) {
         $kid = $_GET['delete'];
         $this->db->getsql("DELETE FROM adrev_keyword_map WHERE adid=? AND keywordid=?", array($id, $kid));
     }
     $tpl = new XTemplate("templates/keywords.html");
     // Check if we can add more keywords
     if ($zone['keywords_max'] > 0 && $f['keyword']) {
         $num = $this->db->getsql("SELECT count(*) as num FROM adrev_keyword_map WHERE adid=?", array($id));
         if ($num['0']['num'] >= $zone['keywords_max']) {
             $this->output->redirect("@@You cannot add any more keywords@@", "?section=ads&action=keyword_stats&f[id]={$f['id']}", 3);
             exit;
         }
     }
     // Add a new keyword
     if ($f['keyword']) {
         $keywordid = $this->db->get_keyword($f['keyword']);
         $exists = $this->db->getsql("SELECT id FROM adrev_keyword_map WHERE keywordid=? AND adid=?", array($keywordid, $id));
         if (!$exists['0']['id']) {
             $i = array();
             $i['zoneid'] = $zoneid;
             $i['adid'] = $id;
             $i['keywordid'] = $keywordid;
             $i['url'] = $f['url'];
             $i['bid'] = 0;
             if ($zone['rtype'] == 2 && $zone['rate_type'] == "CPC") {
                 $i['bid'] = $zone['rate'];
             }
             $this->db->insert("adrev_keyword_map", $i);
         }
     }
     // Setup some defaults
     list($startdate, $enddate) = lib_date_range($f['period']);
     $dates = array("today" => lib_lang('Today'), "yesterday" => lib_lang('Yesterday'), "thisweek" => lib_lang('This Week'), "lastweek" => lib_lang('Last Week'), "thismonth" => lib_lang('This Month'), "lastmonth" => lib_lang('Last Month'), all => lib_lang('All Time'));
     // Show the keywords, bids and other things
     $existing = $this->db->getsql("SELECT b.keyword,a.* FROM adrev_keyword_map a, adrev_keywords b\r\n\t\t\t\t\t\t\t\t\t\tWHERE a.keywordid=b.id AND a.adid=?\r\n\t\t\t\t\t\t\t\t\t\tORDER BY b.keyword", array($id));
     if (count($existing) > 0) {
         $gen = new formgen();
         $gen->startrow("#EEEEEE");
         $gen->column("<b>@@Keyword@@</b>");
         $gen->column("<b>@@URL@@</b>");
         if ($zone['rtype'] == 2 && $zone['rate_type'] == "CPC") {
             $gen->column("<b>@@Bid@@</b>", "", 1, "", "center", "");
             $gen->column("1", "", 1, "", "center", "");
             $gen->column("2", "", 1, "", "center", "");
             $gen->column("3", "", 1, "", "center", "");
             $gen->column("4", "", 1, "", "center", "");
             $gen->column("5", "", 1, "", "center", "");
         }
         #column($content="&nbsp;", $bgcolor="", $width="", $valign="", $align="",$style="")
         $gen->column("<b>@@Clicks@@</b>", "", 1, "", "right", "");
         $gen->column("<b>@@Views@@</b>", "", 1, "", "right", "");
         $gen->column("<b>@@CTR%@@</b>", "", 1, "", "right", "");
         $gen->column("<b>@@Actions@@</b>", "", 1, "", "right", "");
         $gen->column("<b>@@ROI%@@</b>", "", 1, "", "right", "");
         $gen->column("<b>@@Spend@@</b>", "", 1, "", "right", "");
         $gen->column("");
         $gen->endrow();
         foreach ($existing as $rec) {
             $bgcolor = $bgcolor == "#FFFFFF" ? "#FFFFEE" : "#FFFFFF";
             $gen->startrow($bgcolor);
             $gen->column("<a href=?section=ads&action=stats&f[id]={$f['id']}&f[kid]={$rec['keywordid']}>{$rec['keyword']}</a>");
             $rec['url'] = iif(!$rec['url'], $ad['0']['url'], $rec['url']);
             $gen->column("<input type=text name=\"bid[{$rec['id']}][url]\" value=\"{$rec['url']}\" size=30>", "", 1, "", "center", "");
             if ($zone['rtype'] == 2 && $zone['rate_type'] == "CPC") {
                 // Grab other keywords like this one
                 $rates = $this->db->getsql("SELECT bid,adid FROM adrev_keyword_map \r\n\t\t\t\t\t\t\t\t\t\t\t\tWHERE keywordid=? AND zoneid=?\r\n\t\t\t\t\t\t\t\t\t\t\t\tORDER BY bid DESC LIMIT 5", array($rec['keywordid'], $zone['id']));
                 $ranking = array(1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0);
                 if (count($rates) > 0) {
                     $x = 0;
                     foreach ($rates as $r) {
                         $x++;
                         $ranking[$x] = array($r['adid'], $r['bid']);
                     }
                 }
                 $bid = number_format($rec['bid'], 2);
                 $gen->column("<input type=text name=\"bid[{$rec['id']}][bid]\" value=\"{$bid}\" size=5>", "", 1, "", "center", "");
                 foreach ($ranking as $r) {
                     if ($id != $r['0']) {
                         $gen->column(number_format($r['1'], 2), "", 1, "", "right", "");
                     } else {
                         $gen->column("<b>" . number_format($r['1'], 2) . "</b>", "", 1, "", "right", "");
                     }
                 }
             }
             // Grab any stats for this keyword
             $uid = $_SESSION['user']['id'];
             $stats = $this->db->getsql("SELECT count(id) as num, sum(clicks) as clicks, \r\n\t\t\t\t\t\t\t\t\t\t\t\tsum(impressions) as impressions, sum(orders) as orders,\r\n\t\t\t\t\t\t\t\t\t\t\t\tsum(amount) as spend\r\n\t\t\t\t\t\t\t\t\t\t\tFROM adrev_traffic\r\n\t\t\t\t\t\t\t\t\t\t\tWHERE adid=? AND userid=? AND keywordid=? AND date BETWEEN ? AND ?\r\n\t\t\t\t\t\t\t\t\t\t\tGROUP BY adid", array($id, $uid, $rec['keywordid'], $startdate, $enddate));
             $gen->column(number_format($stats['0']['clicks']), "", 1, "", "right", "");
             $gen->column(number_format($stats['0']['impressions']), "", 1, "", "right", "");
             $gen->column(number_format($stats['0']['clicks'] * 100 / iif(!$stats['0']['impressions'], 1, $stats['0']['impressions']), 2), "", 1, "", "right", "");
             $gen->column(number_format($stats['0']['orders']), "", 1, "", "right", "");
             $gen->column(number_format($stats['0']['orders'] * 100 / iif(!$stats['0']['clicks'], 1, $stats['0']['clicks']), 2), "", 1, "", "right", "");
             $gen->column(number_format($stats['0']['spend'], 2), "", 1, "", "right", "");
             $gen->column("<A href=\"?section=ads&action=keyword_stats&f[id]={$f['id']}&delete={$rec['keywordid']}\">@@delete@@</a>", "", 1, "", "center", "");
             $t_impressions += $stats['0']['impressions'];
             $t_clicks += $stats['0']['clicks'];
             $t_orders += $stats['0']['orders'];
             $t_spend += $stats['0']['spend'];
         }
         $gen->startrow("#EEEEEE");
         $gen->column("", "", 1, "", "right", "", 2);
         if ($zone['rtype'] == 2 && $zone['rate_type'] == "CPC") {
             $gen->column("<input type=submit name=f[bidchange] value=\"@@Bid@@\">", "", 1, "", "center");
             $gen->column("", "", 1, "", "right", "", 5);
         }
         $gen->column("<b>" . number_format($t_clicks) . "</b>", "", 1, "", "right", "");
         $gen->column("<b>" . number_format($t_impressions) . "</b>", "", 1, "", "right", "");
         $gen->column("<b>" . number_format($t_clicks * 100 / iif(!$t_impressions, 1, $t_impressions), 2) . "</b>", "", 1, "", "right", "");
         $gen->column("<b>" . number_format($t_orders) . "</b>", "", 1, "", "right", "");
         $gen->column("<b>" . number_format($t_orders * 100 / iif(!$t_clicks, 1, $t_clicks), 2) . "</b>", "", 1, "", "right", "");
         $gen->column("<b>" . number_format($t_spend, 2) . "</b>", "", 1, "", "right", "");
         $gen->column("", "", "", 1, "", "center", "");
         $gen->endrow();
         $tpl->assign("STATS", $gen->gentable("100%", 0, 1, 3, "#CCCCCC"));
     }
     // Show the top part
     $tpl->assign("PERIOD", date("D M d, Y", strtotime($startdate)) . " - " . date("D M d, Y", strtotime($enddate)));
     $tpl->assign("ID", $f['id']);
     $tpl->assign("KID", $f['kid']);
     $tpl->assign("PERIODLIST", lib_htlist_array($dates, $f['period']));
     $tpl->assign("ADS_MENU", $this->menu($_REQUEST['f']['id']));
     // See if we have pre-defined keywords
     if (trim($zone['keywords'])) {
         $exist = array();
         if (count($existing) > 0) {
             foreach ($existing as $rec) {
                 $exist[$rec['keyword']] = 1;
             }
         }
         // We have predefined keywords
         reset($existing);
         $keywords = preg_split('/[\\n,]/', $zone['keywords'], -1, PREG_SPLIT_NO_EMPTY);
         $k = array();
         if (count($keywords) > 0) {
             foreach ($keywords as $keyword) {
                 $keyword = trim(strtolower($keyword));
                 if (!$exist[$keyword]) {
                     $k[$keyword] = $keyword;
                 }
             }
         }
         $options = lib_htlist_array($k);
         $field = "<select name=f[keyword]>{$options}</select>";
     } else {
         // There are no pre-defined keywords
         $field = "<input type=text name=f[keyword] size=20>";
     }
     $tpl->assign("KEYWORD", $field);
     $tpl->parse("main");
     $this->title = lib_lang("Manage Keywords");
     $this->content = $tpl->text("main");
     $this->display();
     $this->printpage();
     exit;
 }
if (!is_writable('banners')) {
    $writable[] = array("banners", "directory");
}
if (!is_writable('lang/EN.lng')) {
    $writable('lang/EN.lng', 'file');
}
if (count($writable) > 0) {
    $gen = new formgen();
    foreach ($writable as $rec) {
        $gen->startrow("#FFFFEE");
        $gen->column("{$rec['0']} ({$rec['1']})");
        $gen->endrow();
    }
    $out = "<font size=4 face=Arial,Helvetica,sans-serif><b>Set File & Directory Permissions</b></font><p>";
    $out .= "You need proper permissions for the webserver to write to the following files or directories.<p>";
    $out .= $gen->gentable("300", 0, 1, 3, "#EEEEEE") . "<p>";
    $out .= "You need to fix this problem before continuing.<br>";
    $out .= "<b>For UNIX servers:</b><br>";
    $out .= "<code>> chmod 0777 <b>filename</b></code><br>";
    $out .= "<code>> chmod 0777 <b>directory</b></code><p>";
    $out .= "<b>For Windows servers:</b><br>";
    $out .= "Use your FTP software to change the settings.<p>";
    $out .= "<A href=install.php>Continue when you are done.</a><p>&nbsp;";
    $tpl->assign("BODY", $out);
    $tpl->assign("TITLE", "INSTALLATION STEP 1: File Permissions");
    $tpl->parse("main");
    echo $tpl->text("main");
    exit;
}
// Step 2
// Decide what database we want to use, and create the settings file