function _default()
 {
     $f = $this->input->f;
     if ($f[email] && $f[subject] && $f[message]) {
         $f[message] .= "\n\n--------------------\nIP:" . $_SERVER[REMOTE_ADDR] . "\n";
         $f[message] .= "Browser:" . $_SERVER[HTTP_USER_AGENT];
         mail($this->default[adrevenue][email], stripslashes($f[subject]), stripslashes($f[message]), "From: <{$f['email']}>");
         $this->output->redirect("Your message was sent", "index.php", 1);
         exit;
     }
     if (!$f[email]) {
         $f[email] = $_SESSION[user][email];
     }
     // Show the form
     $form = new formgen();
     $form->comment(lib_lang("If you have a problem or a question, please contact us using the form below"));
     $form->input("<b>" . lib_lang("Email") . "</b>", "f[email]", $f[email], 30);
     $form->input("<b>" . lib_lang("Subject") . "</b>", "f[subject]", $f[subject], 60);
     $form->textarea("<b>" . lib_lang("Message") . "</b>", "f[message]", $f[message], 15, 62);
     $form->hidden("section", "contact");
     $this->title = lib_lang("Contact Us");
     $this->content = $form->generate("post", lib_lang("Send Message"));
     $this->display();
     $this->printpage();
     exit;
 }
 function send()
 {
     // Grab list of advertisers
     $this->accounts = $this->db->getsql("SELECT * FROM adrev_users WHERE balance <= ? AND status='1' ORDER BY email", array($this->minbal));
     if (count($this->accounts) > 0) {
         $admin = array();
         foreach ($this->accounts as $rec) {
             $tpl = new XTemplate("templates/user_notify_email.txt");
             $tpl->assign("USER", $rec[name]);
             $tpl->assign("SITENAME", $this->default[adrevenue][name]);
             $tpl->assign("URL", $this->default[adrevenue][url]);
             $tpl->assign("LOGIN", $rec[email]);
             $tpl->assign("PASSWORD", $rec[password]);
             $tpl->assign("SITEEMAIL", $this->default[adrevenue][email]);
             $tpl->parse("main");
             $msg = $tpl->text("main");
             $admin[] = "{$rec['name']}\t{$rec['email']}\t{$rec['balance']}";
             mail($rec[email], "[" . $this->default[adrevenue][name] . "] " . lib_lang("Account balance low"), $msg, "From: " . $this->default[adrevenue][name] . "<" . $this->default[adrevenue][email] . ">");
         }
         $amsg = lib_lang("Dear Admin") . ",\n\n";
         $amsg .= lib_lang("The following users have low balances") . ":\n\n";
         foreach ($admin as $a) {
             $amsg .= "{$a}\n";
         }
         // Send message to admin
         mail($this->default[adrevenue][email], "[" . $this->default[adrevenue][name] . "] " . lib_lang("Account Balances Summary"), $amsg, "From: " . $this->default[adrevenue][name] . "<" . $this->default[adrevenue][email] . ">");
     }
     return TRUE;
 }
 function _default()
 {
     $this->output->secure();
     $f = $this->input->f;
     if ($f[email] && $f[password] && $f[name] && $f[url]) {
         if ($_SESSION[user][admin] == 3) {
             $this->db->update("adrev_users", "id", $_REQUEST[id], $f);
             $this->output->redirect(lib_lang("The profile was updated"), "index.php?section=" . $_REQUEST[redir], 1);
         } else {
             $this->db->update("adrev_users", "id", $_SESSION[user][id], $f);
             $this->output->redirect(lib_lang("Your profile was updated"), "index.php?section=profile", 1);
         }
         exit;
     }
     if ($_SESSION[user][admin] == 3 && $_REQUEST[id]) {
         $id = $_REQUEST[id];
     } else {
         $id = $_SESSION[user][id];
     }
     $rec = $this->db->getsql("SELECT * FROM adrev_users WHERE id=?", array($id));
     $f = $rec[0];
     // Grab the list of language modules
     $modules = array();
     if ($handle = opendir("lang")) {
         while (FALSE !== ($file = readdir($handle))) {
             if (preg_match('/^(.*?)\\.lng/i', $file, $match)) {
                 $modules[$match[1]] = $match[1];
             }
         }
         closedir($handle);
     }
     $form = new formgen();
     $form->input("<b>" . lib_lang("Email") . "</b>", "f[email]", stripslashes($f[email]), 40);
     $form->input("<b>" . lib_lang("Password") . "</b>", "f[password]", stripslashes($f[password]), 20);
     $form->input("<b>" . lib_lang("Name") . "</b>", "f[name]", stripslashes($f[name]), 40);
     $form->input(lib_lang("Organization"), "f[organization]", stripslashes($f[organization]), 40);
     $form->dropdown(lib_lang("Country"), "f[country]", lib_htlist_array($this->default[country], $f[country]));
     $form->input(lib_lang("Street"), "f[street]", stripslashes($f[street]), 40);
     $form->input(lib_lang("City"), "f[city]", stripslashes($f[city]), 20);
     $form->input(lib_lang("State"), "f[state]", stripslashes($f[state]), 10);
     $form->input(lib_lang("Zip"), "f[postalcode]", stripslashes($f[postalcode]), 10);
     $form->input("<b>" . lib_lang("Url") . "</b>", "f[url]", stripslashes($f[url]), 50);
     $form->dropdown(lib_lang("Language"), "f[lang]", lib_htlist_array($modules, $f[lang]));
     $form->hidden("section", "profile");
     $form->hidden("id", $id);
     $form->hidden("redir", $_REQUEST[redir]);
     $this->title = lib_lang("Edit Profile");
     $this->content = $form->generate("post", lib_lang("Save Profile"));
     $this->display();
     $this->printpage();
     exit;
 }
 function _default()
 {
     $this->output->secure();
     $f = $this->input->f;
     $uid = $_SESSION[user][id];
     $tpl = new XTemplate("templates/account.html");
     // Compute the balance and update it
     $b = $this->db->getsql("SELECT sum(amount) as spend FROM adrev_traffic WHERE userid=?", array($uid));
     $spend = $b[0][spend];
     // Grab payment history summary
     $h = $this->db->getsql("SELECT sum(amount) as paid FROM adrev_payments WHERE userid=?", array($uid));
     $paid = $h[0][paid];
     if ($_SESSION['user']['admin'] == 2) {
         // Get the publisher balance
         $bal = $this->db->getsql('SELECT sum(amount) as balance FROM adrev_aff_traffic WHERE affid=?', array($uid));
         if ($bal[0]['balance'] > 0) {
             $tpl->assign('PUB_BALANCE', number_format($bal[0]['balance'], 2));
             $tpl->parse('main.transfer');
         }
     }
     // Update balance
     $balance = $paid - $spend;
     $_SESSION[user][balance] = $balance;
     $ts = time();
     $this->db->getsql("UPDATE adrev_users SET balance=?,balance_update=? WHERE id=?", array($balance, $ts, $uid));
     $history = $this->db->getsql("SELECT * FROM adrev_payments WHERE userid=? ORDER BY date DESC", array($uid));
     if (count($history) > 0) {
         foreach ($history as $rec) {
             $bgcolor = iif($bgcolor == "#FFFFFF", "#FFFFEE", "#FFFFFF");
             $tpl->assign("BGCOLOR", $bgcolor);
             $tpl->assign("DATE", date("M d Y", $rec[date]));
             $tpl->assign("TYPE", iif($rec[amount] > 0, "CREDIT", "DEBIT"));
             $tpl->assign("DESC", stripslashes($rec[description]));
             $tpl->assign("AMOUNT", number_format($rec[amount], 2));
             $tpl->parse("main.list");
         }
     }
     $tpl->assign("MIN2", number_format($this->default[adrevenue][min_payment], 2));
     $tpl->assign("MINIMUM", number_format($this->default[adrevenue][min_payment], 2));
     $tpl->assign("BALANCE", number_format($balance, 2));
     $tpl->parse("main");
     $this->title = lib_lang("Manage Payments");
     $this->content = $tpl->text("main");
     $this->display();
     $this->printpage();
     return TRUE;
 }
 function process()
 {
     // Secured
     $this->output->secure();
     // If we have no amount, then go back to the form
     if (!$this->input->f[amount]) {
         $this->form();
     }
     // Save the payment attempt
     $tok = md5(uniqid(rand(), true));
     $i = array();
     $i[userid] = $_SESSION[user][id];
     $i[token] = $tok;
     $i[date] = time();
     $i[amount] = $this->input->f[amount];
     $this->db->insert("adrev_tokens", $i);
     // Get the last insert id
     $last = $this->db->getsql("SELECT id FROM adrev_tokens WHERE token=?", array($tok));
     $invoice = $last[0][id];
     // Setup the URL
     $url = "https://www.paypal.com/xclick/?";
     $i = array();
     $i[business] = $this->default[adrevenue][paypal_email];
     $i[receiver_email] = $this->default[adrevenue][paypal_email];
     $i[item_name] = $this->default[adrevenue][name] . " - " . lib_lang("Advertising");
     $i[item_number] = $this->default[adrevenue][paypal_item];
     $i[quantity] = 1;
     $i[amount] = str_replace(array('$', ',', ' '), "", $this->input->f[amount]);
     $i[page_style] = "PayPal";
     $i[no_shipping] = "1";
     $i['return'] = $this->default[adrevenue][hostname] . "index.php?section=pay&action=success";
     $i['cancel'] = $this->default[adrevenue][hostname] . "index.php?section=pay&action=failure";
     $i[no_note] = 1;
     $i[custom] = $tok;
     $i[currency_code] = $this->default[adrevenue][currency];
     $i[invoice] = $invoice;
     $i[lc] = $_SESSION[user][country];
     $i[notify_url] = $this->default[adrevenue][hostname] . "ipn.php";
     $query = array();
     while (list($key, $val) = each($i)) {
         $query[] = "{$key}=" . urlencode($val);
     }
     $url .= implode("&", $query);
     // Forward payment to Paypal
     header("Location: {$url}");
     exit;
 }
 function _default()
 {
     $this->title = lib_lang("Welcome to ") . $this->default[adrevenue][name];
     $this->heading = $this->title;
     if ($_SESSION[user][admin] == 2) {
         $this->content = $this->default[adrevenue][content_pub_login];
     } elseif ($_SESSION[user][admin] == 1 || $_SESSION[user][admin] == 3) {
         // Do we have ads in our account?
         $uid = $_SESSION['user']['id'];
         $ads = $this->db->getsql("SELECT count(*) as num FROM adrev_ads WHERE userid=?", array($uid));
         if ($ads[0]['num'] > 0) {
             header('Location: ?section=ads');
             return;
         }
         $this->content = $this->default[adrevenue][content_adv_login];
     } else {
         $this->content = $this->default[adrevenue][frontpage];
     }
     $this->display();
     $this->printpage();
     exit;
 }
 function payment_form()
 {
     // Show the form
     $month = array(1 => "01", 2 => "02", 3 => "03", 4 => "04", 5 => "05", 6 => "06", 7 => "07", 8 => "08", 9 => "09", 10 => "10", 11 => "11", 12 => "12");
     $year = array(date("Y"), date("Y") + 1, date("Y") + 2, date("Y") + 3, date("Y") + 4, date("Y") + 5, date("Y") + 6, date("Y") + 7, date("Y") + 8);
     $cards = array('Visa' => lib_lang('Visa'), 'MC' => lib_lang('Mastercard'), 'AMEX' => lib_lang('American Express'), 'Discover' => lib_lang('Discover'));
     $form = new formgen();
     $form->comment(lib_lang("Your Billing Information"));
     $form->input("<b>" . lib_lang("First Name") . "</b>", "f[first_name]", $f[first_name], 30);
     $form->input("<b>" . lib_lang("Last Name") . "</b>", "f[last_name]", $f[last_name], 30);
     $form->dropdown("<b>" . lib_lang("Card Type") . "</b>", "f[cardtype]", lib_htlist_array($cards, $f[cardtype]));
     $form->dropdown("<b>" . lib_lang("Expire Month") . "</b>", "f[last_name]", lib_htlist_array($month, $f[month]));
     $form->dropdown("<b>" . lib_lang("Expire Year") . "</b>", "f[last_year]", lib_htlist_array($year, $f[year]));
     $form->input("<b>" . lib_lang("Card Number") . "</b>", "f[cardnumber]", $f[cardnumber], 20);
     $form->line();
     $form->comment("<font size=3><b>" . lib_lang("Billing Address") . "</b></font>");
     $form->dropdown(lib_lang("Country"), "f[country]", lib_htlist_array($this->default[country], $f[country]));
     $form->input("<b>" . lib_lang("Address 1") . "</b>", "f[address1]", $f[address1], 50);
     $form->input("<b>" . lib_lang("Address 2") . "</b>", "f[address2]", $f[address2], 50);
     $form->input("<b>" . lib_lang("City") . "</b>", "f[city]", $f[city], 25);
     if ($this->default[adrevenue][country] == "US") {
         $form->dropdown(lib_lang("US State"), "f[state]", lib_htlist_array($this->default[states], $f[state]));
     } else {
         $form->input("<b>" . lib_lang("State") . "</b>", "f[state]", $f[state], 20);
     }
     $form->input("<b>" . lib_lang("Phone") . "</b>", "f[phone]", $f[phone], 25);
     $form->input("<b>" . lib_lang("Email") . "</b>", "f[email]", $f[email], 25);
     $form->hidden("section", "pay");
     $form->hidden("action", "form");
     $form->hidden("f[amount]", $f[amount]);
     $this->output->title = lib_lang("Edit Your Profile");
     $this->output->content = $form->generate("post", lib_lang("Save Profile"));
     $this->output->display();
     $this->output->printpage();
     exit;
 }
 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 text($bname)
 {
     global $DEFAULT, $W3LANG;
     if (!isset($bname)) {
         $bname = $this->mainblock;
     }
     $out = str_replace("^^^^", "\n", $this->parsed_blocks[$bname]);
     $search = explode("|", "##" . strtoupper(implode("##|##", array_keys($DEFAULT[adrevenue]))) . "##");
     $replace = array_values($DEFAULT[adrevenue]);
     // Search and replace
     if (preg_match_all('/@@(.*?)@@/ims', $out, $matches)) {
         $n = count($matches[1]);
         for ($x = 0; $x < $n; $x++) {
             $search[] = $matches[0][$x];
             $phrase = str_replace(array("\n", "\r"), " ", $matches[1][$x]);
             $phrase = preg_replace('/[ ]+/', ' ', $phrase);
             $replace[] = lib_lang($phrase);
         }
     }
     return str_replace($search, $replace, $out);
 }
 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 deleteu()
 {
     // Delete an ad
     $this->output->admin();
     $f = $this->input->f;
     if ($_REQUEST[c] == "delete" && $f[id] != 1) {
         $this->db->getsql("DELETE FROM adrev_users WHERE id=?", array($f['id']));
         $this->db->getsql("DELETE FROM adrev_ads WHERE userid=?", array($f['id']));
         $this->db->getsql("DELETE FROM adrev_traffic WHERE userid=?", array($f['id']));
         $this->output->redirect(lib_lang("The user was deleted."), "index.php?section=useradmin", 1);
         exit;
     } else {
         $this->output->redirect(lib_lang("You cannot delete the administrator."), "index.php?section=useradmin", 3);
     }
 }
 function logout()
 {
     global $f;
     $_SESSION['user'] = array();
     $this->output->redirect(lib_lang("You are now logged out"), "index.php");
     exit;
 }
 function redirect($msg = "", $url = "", $timeout = 2)
 {
     $_SESSION['flash'] = lib_lang($msg);
     header('Location: ' . $url);
     exit;
 }
 function pay_settings()
 {
     $this->output->admin();
     $f = $this->input->f;
     // Set the default to be PayPal
     if (!$this->default[adrevenue][payment_module]) {
         $this->default[adrevenue][payment_module] = "paypal";
     }
     $mod = "pay." . $this->default[adrevenue][payment_module] . ".php";
     include_once $mod;
     $opt = payment::mod_vars();
     $info = payment::mod_info();
     if (count($f) > 0) {
         reset($f);
         while (list($key, $val) = each($f)) {
             $this->save($key, $val);
         }
         $this->output->redirect("The payment settings were updated", "index.php?section=settings&action=pay_settings", 1);
         exit;
     }
     // Show the form
     $form = new formgen();
     $form->comment("<b>" . lib_lang("Enter the settings for your {$info['name']} payment module") . "</b><br>&nbsp;");
     if ($info[extern] == TRUE) {
         $form->comment(lib_lang($info[extern_description]) . ": <font color=red>" . $this->default[adrevenue][hostname] . "ipn.php<p>" . "</font>");
     }
     foreach ($opt as $rec) {
         $name = $rec[name];
         $v = $this->db->getsql("SELECT value FROM adrev_settings WHERE name=?", array($name));
         $form->input(lib_lang($rec[label]), "f[{$name}]", stripslashes($v[0][value]), $rec[length]);
     }
     $form->hidden("submit", "1");
     $this->title = lib_lang("Edit {$info['name']} Payment Settings");
     $this->content = $form->generate("post", lib_lang("Save Payment Settings"));
     $this->display();
     $this->printpage();
     exit;
 }
 function external()
 {
     // Load the token record first, this prevents duplicate transactions
     $token = $_GET[t];
     $t = $this->db->getsql("SELECT * FROM adrev_tokens WHERE token=?", array($token));
     // assign posted variables to local variables
     if ($t) {
         $i = array();
         $i[token] = $t;
         $i[txid] = uniqid("");
         $i[status] = "Completed";
         $i[txndate] = time();
         $this->db->update("adrev_tokens", "token", $i[token], $i);
     }
     // Accept the payment only once, just in case IPN flakes out!
     if ($t[0][status] != "Completed") {
         // Add the payment
         $j = array();
         $j[date] = time();
         $j[userid] = $t[0][userid];
         $j[description] = "Payment completed - {$i['txid']}";
         $j[amount] = $t[0][amount];
         $this->db->insert("adrev_payments", $j);
         // Compute the balance and update it
         $uid = $t[0][userid];
         $b = $this->db->getsql("SELECT sum(amount) as spend FROM adrev_traffic WHERE userid=?", array($uid));
         $spend = $b[0][spend];
         // Grab payment history summary
         $h = $this->db->getsql("SELECT sum(amount) as paid FROM adrev_payments WHERE userid=?", array($uid));
         $paid = $h[0][paid];
         // Update balance
         $balance = $paid - $spend;
         $ts = time();
         $this->db->getsql("UPDATE adrev_users SET balance=?,balance_update=? WHERE id=?", array($balance, $ts, $uid));
     }
     $this->output->redirect(lib_lang("Thank You"), "index.php", 2);
     exit;
 }
 function getcsv($sql = "", $filename = "output.csv")
 {
     // Send headers
     header("Pragma: public");
     header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
     header('Cache-Control: no-store, no-cache, must-revalidate');
     header('Cache-Control: pre-check=0, post-check=0, max-age=0');
     header('Content-Transfer-Encoding: none');
     if (preg_match('/MSIE|Explorer|Microsoft|\\.NET|Opera/i', $_SERVER[HTTP_USER_AGENT])) {
         header("Content-type: application/octetstream; name=\"{$filename}\"");
     } else {
         header('Content-Type: application/octet-stream; name="' . $filename . '"');
         header('Content-Disposition: inline; filename="' . $filename . '"');
     }
     $this->query($sql);
     $x = 0;
     while ($rec = $this->nextrow()) {
         // Cleanup UNIX dates
         if ($rec['date']) {
             if (!preg_match('/\\-/', $rec[date])) {
                 $rec['date'] = date("M d Y h:i:s", $rec[date]);
             }
         }
         if ($x == 0) {
             $keys = array_keys($rec);
             $n = count($keys);
             for ($x = 0; $x < $n; $x++) {
                 $keys[$x] = lib_lang(ucfirst($keys[$x]));
             }
             echo '"' . implode('","', array_values($keys)) . '"' . "\r\n";
         }
         echo '"' . implode('","', array_values($rec)) . '"' . "\r\n";
         $x++;
     }
     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;
 }
// Start Session and make it last for 1 day (advertisers hate to be logged out)
session_set_cookie_params(86400);
session_start();
// Deal with annoying Windows Servers that turn on error reporting too high
error_reporting(E_ALL ^ E_NOTICE);
// Loadup the default library files
include_once "libs/lib.php";
include_once "libs/controller.php";
include_once "libs/db.php";
include_once "libs/http.php";
include_once "libs/input.php";
include_once "libs/output.php";
include_once "libs/xtpl.php";
include_once "libs/formgen.php";
include_once "libs/stopwords.php";
// Set some other defaults
$DEFAULT[adrevenue] = array();
// Loadup our settings file
@(include_once "settings.php");
$DEFAULT[ad_types] = array('CPC' => '@@CPC - Cost Per Click@@', 'CPM' => '@@CPM - Cost Per 1000 Impressions@@', 'CPD' => '@@CPD - Cost Per Day@@', 'CPA' => '@@CPA - Cost Per Action/Order@@', 'CPI' => '@@CPI - Cost Per Impression@@');
// Ad Status
$DEFAULT[status] = array(2 => '<font color=#999999>Pending</font>', -1 => '<font color=red>Deactivated</font>', 1 => '<font color=green>Active</font>', 3 => '<font color=#999999>Paused</font>', -2 => '<font color=orange>Expired</font>');
$DEFAULT[status_color] = array(2 => '#999999', -1 => 'red', 1 => 'green', 3 => '#999999', -2 => 'orange');
$DEFAULT[rstatus] = array('all' => '-- All --', -1 => 'Deactivated', 1 => 'Active', 2 => 'Pending', 3 => 'Paused', -2 => 'Expired');
// Main template
$DEFAULT[template] = "templates/main.html";
// Countries
$DEFAULT[country] = array('AF' => 'Afghanistan', 'AL' => 'Albania, People\'s Socialist Republic of', 'DZ' => 'Algeria, People\'s Democratic Republic of', 'AS' => 'American Samoa', 'AD' => 'Andorra, Principality of', 'AO' => 'Angola, Republic of', 'AI' => 'Anguilla', 'AQ' => 'Antarctica (the territory South of 60 deg S)', 'AG' => 'Antigua and Barbuda', 'AR' => 'Argentina, Argentine Republic', 'AM' => 'Armenia', 'AW' => 'Aruba', 'AU' => 'Australia, Commonwealth of', 'AT' => 'Austria, Republic of', 'AZ' => 'Azerbaijan, Republic of', 'BS' => 'Bahamas, Commonwealth of the', 'BH' => 'Bahrain, Kingdom of', 'BD' => 'Bangladesh, People\'s Republic of', 'BB' => 'Barbados', 'BY' => 'Belarus', 'BE' => 'Belgium, Kingdom of', 'BZ' => 'Belize', 'BJ' => 'Benin, People\'s Republic of', 'BM' => 'Bermuda', 'BT' => 'Bhutan, Kingdom of', 'BO' => 'Bolivia, Republic of', 'BA' => 'Bosnia and Herzegovina', 'BW' => 'Botswana, Republic of', 'BV' => 'Bouvet Island (Bouvetoya)', 'BR' => 'Brazil, Federative Republic of', 'IO' => 'British Indian Ocean Territory (Chagos Archipelago)', 'VG' => 'British Virgin Islands', 'BN' => 'Brunei Darussalam', 'BG' => 'Bulgaria, People\'s Republic of', 'BF' => 'Burkina Faso', 'BI' => 'Burundi, Republic of', 'KH' => 'Cambodia, Kingdom of', 'CM' => 'Cameroon, United Republic of', 'CA' => 'Canada', 'CV' => 'Cape Verde, Republic of', 'KY' => 'Cayman Islands', 'CF' => 'Central African Republic', 'TD' => 'Chad, Republic of', 'CL' => 'Chile, Republic of', 'CN' => 'China, People\'s Republic of', 'CX' => 'Christmas Island', 'CC' => 'Cocos (Keeling) Islands', 'CO' => 'Colombia, Republic of', 'KM' => 'Comoros, Federal and Islamic Republic of', 'CD' => 'Congo, Democratic Republic of', 'CG' => 'Congo, People\'s Republic of', 'CK' => 'Cook Islands', 'CR' => 'Costa Rica, Republic of', 'CI' => 'Cote D\'Ivoire, Ivory Coast, Republic of the', 'CU' => 'Cuba, Republic of', 'CY' => 'Cyprus, Republic of', 'CZ' => 'Czech Republic', 'DK' => 'Denmark, Kingdom of', 'DJ' => 'Djibouti, Republic of', 'DM' => 'Dominica, Commonwealth of', 'DO' => 'Dominican Republic', 'TL' => 'Timor-Leste', 'EC' => 'Ecuador, Republic of', 'EG' => 'Egypt, Arab Republic of', 'SV' => 'El Salvador, Republic of', 'GQ' => 'Equatorial Guinea, Republic of', 'ER' => 'Eritrea', 'EE' => 'Estonia', 'ET' => 'Ethiopia', 'FO' => 'Faeroe Islands', 'FK' => 'Falkland Islands (Malvinas)', 'FJ' => 'Fiji, Republic of the Fiji Islands', 'FI' => 'Finland, Republic of', 'FR' => 'France, French Republic', 'GF' => 'French Guiana', 'PF' => 'French Polynesia', 'TF' => 'French Southern Territories', 'GA' => 'Gabon, Gabonese Republic', 'GM' => 'Gambia, Republic of the', 'GE' => 'Georgia', 'DE' => 'Germany', 'GH' => 'Ghana, Republic of', 'GI' => 'Gibraltar', 'GR' => 'Greece, Hellenic Republic', 'GL' => 'Greenland', 'GD' => 'Grenada', 'GP' => 'Guadaloupe', 'GU' => 'Guam', 'GT' => 'Guatemala, Republic of', 'GN' => 'Guinea, Revolutionary People\'s Rep\'c of', 'GW' => 'Guinea-Bissau, Republic of', 'GY' => 'Guyana, Republic of', 'HT' => 'Haiti, Republic of', 'HM' => 'Heard and McDonald Islands', 'VA' => 'Holy See (Vatican City State)', 'HN' => 'Honduras, Republic of', 'HK' => 'Hong Kong, Special Administrative Region of China', 'HR' => 'Hrvatska (Croatia)', 'HU' => 'Hungary, Hungarian People\'s Republic', 'IS' => 'Iceland, Republic of', 'IN' => 'India, Republic of', 'ID' => 'Indonesia, Republic of', 'IR' => 'Iran, Islamic Republic of', 'IQ' => 'Iraq, Republic of', 'IE' => 'Ireland', 'IL' => 'Israel, State of', 'IT' => 'Italy, Italian Republic', 'JM' => 'Jamaica', 'JP' => 'Japan', 'JO' => 'Jordan, Hashemite Kingdom of', 'KZ' => 'Kazakhstan, Republic of', 'KE' => 'Kenya, Republic of', 'KI' => 'Kiribati, Republic of', 'KP' => 'Korea, Democratic People\'s Republic of', 'KR' => 'Korea, Republic of', 'KW' => 'Kuwait, State of', 'KG' => 'Kyrgyz Republic', 'LA' => 'Lao People\'s Democratic Republic', 'LV' => 'Latvia', 'LB' => 'Lebanon, Lebanese Republic', 'LS' => 'Lesotho, Kingdom of', 'LR' => 'Liberia, Republic of', 'LY' => 'Libyan Arab Jamahiriya', 'LI' => 'Liechtenstein, Principality of', 'LT' => 'Lithuania', 'LU' => 'Luxembourg, Grand Duchy of', 'MO' => 'Macao, Special Administrative Region of China', 'MK' => 'Macedonia, the former Yugoslav Republic of', 'MG' => 'Madagascar, Republic of', 'MW' => 'Malawi, Republic of', 'MY' => 'Malaysia', 'MV' => 'Maldives, Republic of', 'ML' => 'Mali, Republic of', 'MT' => 'Malta, Republic of', 'MH' => 'Marshall Islands', 'MQ' => 'Martinique', 'MR' => 'Mauritania, Islamic Republic of', 'MU' => 'Mauritius', 'YT' => 'Mayotte', 'MX' => 'Mexico, United Mexican States', 'FM' => 'Micronesia, Federated States of', 'MD' => 'Moldova, Republic of', 'MC' => 'Monaco, Principality of', 'MN' => 'Mongolia, Mongolian People\'s Republic', 'MS' => 'Montserrat', 'MA' => 'Morocco, Kingdom of', 'MZ' => 'Mozambique, People\'s Republic of', 'MM' => 'Myanmar', 'NA' => 'Namibia', 'NR' => 'Nauru, Republic of', 'NP' => 'Nepal, Kingdom of', 'AN' => 'Netherlands Antilles', 'NL' => 'Netherlands, Kingdom of the', 'NC' => 'New Caledonia', 'NZ' => 'New Zealand', 'NI' => 'Nicaragua, Republic of', 'NE' => 'Niger, Republic of the', 'NG' => 'Nigeria, Federal Republic of', 'NU' => 'Niue, Republic of', 'NF' => 'Norfolk Island', 'MP' => 'Northern Mariana Islands', 'NO' => 'Norway, Kingdom of', 'OM' => 'Oman, Sultanate of', 'PK' => 'Pakistan, Islamic Republic of', 'PW' => 'Palau', 'PS' => 'Palestinian Territory, Occupied', 'PA' => 'Panama, Republic of', 'PG' => 'Papua New Guinea', 'PY' => 'Paraguay, Republic of', 'PE' => 'Peru, Republic of', 'PH' => 'Philippines, Republic of the', 'PN' => 'Pitcairn Island', 'PL' => 'Poland, Polish People\'s Republic', 'PT' => 'Portugal, Portuguese Republic', 'PR' => 'Puerto Rico', 'QA' => 'Qatar, State of', 'RE' => 'Reunion', 'RO' => 'Romania, Socialist Republic of', 'RU' => 'Russian Federation', 'RW' => 'Rwanda, Rwandese Republic', 'SH' => 'St. Helena', 'KN' => 'St. Kitts and Nevis', 'LC' => 'St. Lucia', 'PM' => 'St. Pierre and Miquelon', 'VC' => 'St. Vincent and the Grenadines', 'WS' => 'Samoa, Independent State of', 'SM' => 'San Marino, Republic of', 'ST' => 'Sao Tome and Principe, Democratic Republic of', 'SA' => 'Saudi Arabia, Kingdom of', 'SN' => 'Senegal, Republic of', 'SC' => 'Seychelles, Republic of', 'SL' => 'Sierra Leone, Republic of', 'SG' => 'Singapore, Republic of', 'SK' => 'Slovakia (Slovak Republic)', 'SI' => 'Slovenia', 'SB' => 'Solomon Islands', 'SO' => 'Somalia, Somali Republic', 'ZA' => 'South Africa, Republic of', 'GS' => 'South Georgia and the South Sandwich Islands', 'ES' => 'Spain, Spanish State', 'LK' => 'Sri Lanka, Democratic Socialist Republic of', 'SD' => 'Sudan, Democratic Republic of the', 'SR' => 'Suriname, Republic of', 'SJ' => 'Svalbard & Jan Mayen Islands', 'SZ' => 'Swaziland, Kingdom of', 'SE' => 'Sweden, Kingdom of', 'CH' => 'Switzerland, Swiss Confederation', 'SY' => 'Syrian Arab Republic', 'TW' => 'Taiwan, Province of China', 'TJ' => 'Tajikistan', 'TZ' => 'Tanzania, United Republic of', 'TH' => 'Thailand, Kingdom of', 'TG' => 'Togo, Togolese Republic', 'TK' => 'Tokelau (Tokelau Islands)', 'TO' => 'Tonga, Kingdom of', 'TT' => 'Trinidad and Tobago, Republic of', 'TN' => 'Tunisia, Republic of', 'TR' => 'Turkey, Republic of', 'TM' => 'Turkmenistan', 'TC' => 'Turks and Caicos Islands', 'TV' => 'Tuvalu', 'VI' => 'US Virgin Islands', 'UG' => 'Uganda, Republic of', 'UA' => 'Ukraine', 'AE' => 'United Arab Emirates', 'GB' => 'United Kingdom of Great Britain & N. Ireland', 'UM' => 'United States Minor Outlying Islands', 'US' => 'United States of America', 'UY' => 'Uruguay, Eastern Republic of', 'UZ' => 'Uzbekistan', 'VU' => 'Vanuatu', 'VE' => 'Venezuela, Bolivarian Republic of', 'VN' => 'Viet Nam, Socialist Republic of', 'WF' => 'Wallis and Futuna Islands', 'EH' => 'Western Sahara', 'YE' => 'Yemen', 'YU' => 'Yugoslavia, Socialist Federal Republic of', 'ZM' => 'Zambia, Republic of', 'ZW' => 'Zimbabwe');
$DEFAULT[states] = array('AL' => 'Alabama', 'AK' => 'Alaska', 'AZ' => 'Arizona', 'AR' => 'Arkansas', 'CA' => 'California', 'CO' => 'Colorado', 'CT' => 'Connecticut', 'DE' => 'Delaware', 'DC' => 'District of Columbia', 'FL' => 'Florida', 'GA' => 'Georgia', 'HI' => 'Hawaii', 'ID' => 'Idaho', 'IL' => 'Illinois', 'IN' => 'Indiana', 'IA' => 'Iowa', 'KS' => 'Kansas', 'KY' => 'Kentucky', 'LA' => 'Louisiana', 'ME' => 'Maine', 'MD' => 'Maryland', 'MA' => 'Massachusetts', 'MI' => 'Michigan', 'MN' => 'Minnesota', 'MS' => 'Mississippi', 'MO' => 'Missouri', 'MT' => 'Montana', 'NE' => 'Nebraska', 'NV' => 'Nevada', 'NH' => 'New Hampshire', 'NJ' => 'New Jersey', 'NM' => 'New Mexico', 'NY' => 'New York', 'NC' => 'North Carolina', 'ND' => 'North Dakota', 'OH' => 'Ohio', 'OK' => 'Oklahoma', 'OR' => 'Oregon', 'PA' => 'Pennsylvania', 'RI' => 'Rhode Island', 'SC' => 'South Carolina', 'SD' => 'South Dakota', 'TN' => 'Tennessee', 'TX' => 'Texas', 'UT' => 'Utah', 'VT' => 'Vermont', 'VA' => 'Virginia', 'WA' => 'Washington', 'WV' => 'West Virginia', 'WI' => 'Wisconsin', 'WY' => 'Wyoming');
$DEFAULT[field_types] = array('TITLE' => lib_lang("Title"), 'DESCRIPTION' => lib_lang('Description'), 'URL' => lib_lang('URL'), 'DISPLAY_URL' => lib_lang('Display URL'), 'EMAIL' => lib_lang('Email'), 'PHONE' => lib_lang('Phone'), 'FAX' => lib_lang('Fax'), 'IMAGE' => lib_lang('Image Upload'), 'CONTENT' => lib_lang('Content'), 'CUSTOM1' => lib_lang('Custom Field 1'), 'CUSTOM2' => lib_lang('Custom Field 2'), 'CUSTOM3' => lib_lang('Custom Field 3'), 'CUSTOM4' => lib_lang('Custom Field 4'), 'CUSTOM5' => lib_lang('Custom Field 5'), 'CUSTOM6' => lib_lang('Custom Field 6'));
 function failure()
 {
     $tpl = new XTemplate("templates/account_failure.html");
     $tpl->parse("main");
     $this->title = lib_lang("We're sorry");
     $this->content = $tpl->text("main");
     $this->display();
     $this->printpage();
     exit;
 }
 function zone_keywords()
 {
     $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[keywords] = strtolower($f[keywords]);
         $i[keywords_enable] = $f[keywords_enable] ? 1 : 0;
         $i[keywords_fuzzy] = $f[keywords_fuzzy] ? 1 : 0;
         $i[keywords_max] = $f[keywords_max] ? $f[keywords_max] : 0;
         $this->db->update("adrev_zones", "id", $f[id], $i);
         // Update keywords
         $k = explode("\n", $i[keywords]);
         if (count($k) > 0) {
             foreach ($k as $keyword) {
                 // Look for this keyword
                 $key = trim(strtolower($keyword));
                 $l = $this->db->getsql("SELECT id FROM adrev_keywords WHERE keyword=?", array($key));
                 if (!$l[0][id]) {
                     // Add the keyword
                     $i = array();
                     $i[keyword] = $key;
                     $i[fuzzy_keyword] = metaphone($key);
                     $i[mincpc] = 0;
                     $this->db->insert("adrev_keywords", $i);
                 }
             }
         }
         $this->output->redirect(lib_lang("Keyword options were updated"), "index.php?section=zone&action=zone_keywords&f[id]={$f['id']}", 1);
         exit;
     }
     // Show the form
     $tpl = new XTemplate("templates/zone_keywords.html");
     $tpl->assign("ID", $f[id]);
     $tpl->assign("KEYWORDS_ENABLE", $z[0][keywords_enable] ? "CHECKED" : "");
     $tpl->assign("KEYWORDS_FUZZY_" . $z[0][keywords_fuzzy], "CHECKED");
     $tpl->assign("KEYWORDS", stripslashes($z[0][keywords]));
     $tpl->assign("KEYWORDS_MAX", $z[0][keywords_max]);
     $tpl->assign("ZONE_MENU", $this->zone_menu($f[id]));
     $tpl->parse("main");
     $this->title = lib_lang("Manage") . " [" . stripslashes($z[0][name]) . "] : " . lib_lang("Keywords");
     $this->content = $tpl->text("main");
     $this->display();
     $this->printpage();
     exit;
 }