Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if (!class_exists('ubam_oto')) {
- class ubam_oto {
- var $optionsName = 'ubam_oto_options';
- var $localizationDomain = "ubam_oto";
- var $thispluginurl = '';
- var $thispluginpath = '';
- var $options = array();
- function ubam_oto() {
- $locale = get_locale();
- $mo = dirname(__FILE__) . "/languages/" . $this->localizationDomain . "-" . $locale . ".mo";
- load_textdomain($this->localizationDomain, $mo);
- $this->thispluginurl = PLUGIN_URL . '/' . dirname(plugin_basename(__FILE__)) . '/';
- $this->thispluginpath = PLUGIN_PATH . '/' . dirname(plugin_basename(__FILE__)) . '/';
- $this->getOptions();
- add_action("admin_menu", array(&$this, "admin_menu_link"));
- add_action("init", array(&$this, "ubam_oto_handler"));
- }
- function getOptions() {
- if (!$theOptions = get_option($this->optionsName)) {
- $theOptions = array('default' => 'options');
- update_option($this->optionsName, $theOptions);
- }
- $this->options = $theOptions;
- }
- function saveAdminOptions() {
- return update_option($this->optionsName, $this->options);
- }
- function admin_menu_link() {
- add_options_page('One-Time Offer', 'One-Time Offer', 'manage_options', 'one-time-offer.php', array(&$this, 'admin_options_page'));
- add_filter('plugin_action_links_' . plugin_basename(__FILE__), array(&$this, 'filter_plugin_actions'), 10, 2);
- }
- function filter_plugin_actions($links, $file) {
- $settings_link = '<a href="options-general.php?page=' . basename(__FILE__) . '">' . __('Settings') . '</a>';
- array_unshift($links, $settings_link);
- return $links;
- }
- function ubam_oto_handler() {
- global $wpdb;
- if ($_GET['uo-offer']) {
- $offer_query = "SELECT * from {$wpdb->prefix}ubam_oto_offers where offer = '{$_GET['uo-offer']}'";
- $offer_result = $wpdb->get_row($offer_query);
- if (!$offer_result) {
- wp_redirect(get_option('siteurl'));
- } else {
- $offer_start_time = $offer_result->offer_start_date;
- if (!empty($offer_start_time) && $offer_start_time > time()) {
- wp_redirect(get_option('siteurl'));
- } else {
- $offer = $_GET['uo-offer'];
- $expired_page = $offer_result->offer_expired_page;
- $offer_page = $offer_result->offer_page;
- $offer_cookie = $offer_result->offer_cookie;
- $passwd = $offer_result->offer_passwd;
- $offer_end_time = $offer_result->offer_end_date;
- if ($offer_result->offer_time == 0) {
- $offer_time = 0;
- } else {
- $time_multiplier = 1;
- switch ($offer_result->offer_period) {
- case 'W':
- $time_multiplier*= 7;
- case 'D':
- $time_multiplier*= 24;
- case 'H':
- $time_multiplier*= 60;
- case 'M':
- $time_multiplier*= 60;
- }
- $offer_time = $offer_result->offer_time * $time_multiplier;
- }
- $cookie_expires = time() + (60 * 60 * 24 * 30);
- $unique_ip = trim(getenv('HTTP_X_FORWARDED_FOR'));
- $uv_id = $unique_ip;
- if (!ereg("^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$", $unique_ip)) {
- $unique_ip = $_SERVER['REMOTE_ADDR'];
- $uv_id = $unique_ip;
- }
- if (ereg("^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$", $unique_ip)) {
- @$uv_id = gethostbyaddr($unique_ip);
- }
- $query = "SELECT * FROM {$wpdb->prefix}ubam_oto_uniques WHERE uv_id='$uv_id' and offer = '$offer'";
- $unique_result = $wpdb->get_row($query);
- $the_time = time();
- if ((($unique_result && (empty($unique_result->offer_expires) || $unique_result->offer_expires < $the_time)) || (isset($_COOKIE[$offer_cookie]) && $_COOKIE[$offer_cookie] < $the_time) || (!empty($offer_end_time) && $offer_end_time < $the_time)) && (empty($passwd) || !isset($_GET['pw']) || $_GET['pw'] != $passwd)) {
- $output_page = $this->getPageUrl($expired_page);
- } else {
- if (isset($_GET['pw']) && !empty($passwd) && $_GET['pw'] == $passwd) {
- $offer_expires = $the_time + $offer_time;
- } elseif (!empty($unique_result->offer_expires)) {
- $offer_expires = $unique_result->offer_expires;
- } elseif (isset($_COOKIE[$offer_cookie])) {
- $offer_expires = $_COOKIE[$offer_cookie];
- } else {
- $offer_expires = $the_time + $offer_time;
- }
- if (!isset($_COOKIE[$offer_cookie])) {
- setcookie($offer_cookie, $offer_expires, $cookie_expires);
- header("Cache-Control: no-cache, no-store, must-revalidate");
- header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
- header("Pragma: no-cache");
- header("CacheControl: no-cache");
- }
- $output_page = $this->getPageUrl($offer_page);
- if (!$unique_result) {
- $query = "REPLACE INTO {$wpdb->prefix}ubam_oto_uniques ( offer, uv_id, offer_expires ) VALUES ( '$offer', '$uv_id', $offer_expires )";
- $result = $wpdb->query($query);
- }
- }
- $outpage = @wp_remote_fopen($output_page);
- $backcolor = 'white';
- $forecolor = 'black';
- $outpage = str_replace('[OTO_USER_HOSTNAME]', $uv_id, $outpage);
- $iTimerCount = 0;
- $timer_format = '';
- while (preg_match('/\[OTO-TIMER(.*?)\]/i', $outpage, $matches) && iTimerCount < 10) {
- $span_out = "<span id='ubam-oto-cntdwn$iTimerCount'></span>";
- if (empty($timer_format) && count($matches) > 1 && strpos($matches[1], '%%') !== false) $timer_format = $matches[1];
- $outpage = substr_replace($outpage, $span_out, strpos($outpage, $matches[0]), strlen($matches[0]));
- $iTimerCount++;
- }
- if (empty($timer_format)) $timer_format = "%%D%% Day(s) - %%H%%:%%M%%:%%S%%";
- if ($iTimerCount > 0) {
- $js_out = '<script language="JavaScript">' . 'TargetDate = "' . date("m/d/Y h:i:s A TO", $offer_expires) . '";' . 'BackColor = "white";' . 'ForeColor = "black";' . 'CountActive = true;' . 'CountStepper = -1;' . 'LeadingZero = true;' . 'DisplayFormat = "' . $timer_format . '";' . 'RedirectTo = "' . get_option('siteurl') . "?uo-offer=$offer" . '";' . 'NumOtoTimers = ' . $iTimerCount . ';' . 'FinishMessage = "It is finally here!";' . '</script>' . '<script language="JavaScript" src="' . WP_PLUGIN_URL . '/ubam_one_time_offer/countdown.js"></script>';
- $outpage = preg_replace('/<\/body>/i', $js_out . "</body>", $outpage, 1);
- }
- echo $outpage;
- exit;
- }
- }
- }
- }
- function parse_attributes($attrib_string) {
- $regex = '@([^\s=]+)\s*=\s*(\'[^<\']*\'|"[^<"]*"|\S*)@';
- preg_match_all($regex, $attrib_string, $matches);
- $attr = array();
- for ($i = 0;$i < count($matches[0]);$i++) {
- if (!empty($matches[0][$i]) && !empty($matches[1][$i])) {
- if (preg_match("/^'(.*)'$/", $matches[2][$i], $vmatch)) {
- $value = $vmatch[1];
- } else if (preg_match('/^"(.*)"$/', $matches[2][$i], $vmatch)) {
- $value = $vmatch[1];
- } else {
- $value = $matches[2][$i];
- }
- $key = strtolower($matches[1][$i]);
- $attr[$key] = $value;
- }
- }
- return $attr;
- }
- function admin_options_page() {
- global $wpdb;
- if ($_POST['action'] == 'edit') {
- $editmode = true;
- $eoffer = $wpdb->get_row("SELECT * from {$wpdb->prefix}ubam_oto_offers WHERE offer_id={$_POST['offerid']}"); ?><div class="wrap">
- <h2>One Time Offer Manager for WordPress</h2> <?php require_once (OTOMGR_TEMPLATE_DIR . "offer-entry.htm");
- } else {
- if ($_POST['action'] == 'delete') {
- $wpdb->show_errors();
- $delete_results = $wpdb->query("DELETE FROM {$wpdb->prefix}ubam_oto_offers WHERE offer_id={$_POST['offerid']}");
- $wpdb->hide_errors();
- if ($delete_results === false) {
- echo '<div class="updated"><p><strong><span style="color: red;">**ERROR**</span></strong> Unable to delete the offer.</p></div>';
- } else {
- $wpdb->show_errors();
- $delete_results = $wpdb->query("DELETE FROM {$wpdb->prefix}ubam_oto_uniques WHERE offer='{$_POST['offer']}'");
- $wpdb->hide_errors();
- echo '<div class="updated"><p>Success! Offer has been deleted.</p></div>';
- }
- }
- if ($_POST['ubam_oto_add_offer']) {
- $insert_query = "REPLACE INTO {$wpdb->prefix}ubam_oto_offers " . " (offer, offer_time, offer_period, " . " offer_page, offer_expired_page, " . " offer_cookie, offer_passwd, " . " offer_start_date, offer_end_date) " . "VALUES ('{$_POST['ubam_oto_offer']}', {$_POST['ubam_oto_offer_time']}, '{$_POST['ubam_oto_offer_period']}', " . " '{$_POST['ubam_oto_offer_page']}', '{$_POST['ubam_oto_offer_expired_page']}', " . " '{$_POST['ubam_oto_offer_cookie']}', '{$_POST['ubam_oto_offer_passwd']}', " . " " . (!strtotime($_POST['ubam_oto_offer_start_date']) ? 'NULL' : strtotime($_POST['ubam_oto_offer_start_date'])) . ", " . " " . (!strtotime($_POST['ubam_oto_offer_end_date']) ? 'NULL' : strtotime($_POST['ubam_oto_offer_end_date'])) . ") ";
- $wpdb->show_errors();
- $insert_results = $wpdb->query($insert_query);
- $wpdb->hide_errors();
- if ($insert_results === false) {
- echo '<div class="updated"><p><strong><span style="color: red;">**ERROR**</span></strong> Unable to save new offer.</p></div>';
- } else {
- echo '<div class="updated"><p>Success! Offer has been added to the database.</p></div>';
- }
- }
- if ($_POST['ubam_oto_update_offer']) {
- $update_query = "UPDATE {$wpdb->prefix}ubam_oto_offers " . " SET offer = '{$_POST['ubam_oto_offer']}' " . " , offer_time = '{$_POST['ubam_oto_offer_time']}' " . " , offer_period = '{$_POST['ubam_oto_offer_period']}' " . " , offer_page = '{$_POST['ubam_oto_offer_page']}' " . " , offer_expired_page = '{$_POST['ubam_oto_offer_expired_page']}' " . " , offer_cookie = '{$_POST['ubam_oto_offer_cookie']}' " . " , offer_passwd = '{$_POST['ubam_oto_offer_passwd']}' " . " , offer_start_date = " . (!strtotime($_POST['ubam_oto_offer_start_date']) ? 'NULL' : strtotime($_POST['ubam_oto_offer_start_date'])) . " , offer_end_date = " . (!strtotime($_POST['ubam_oto_offer_end_date']) ? 'NULL' : strtotime($_POST['ubam_oto_offer_end_date'])) . " WHERE offer_id = {$_POST['offerid']} ";
- $wpdb->show_errors();
- $update_results = $wpdb->query($update_query);
- $wpdb->hide_errors();
- if ($update_results === false) {
- echo '<div class="updated"><p><strong><span style="color: red;">**ERROR**</span></strong> Unable to save changes.</p></div>';
- } else {
- echo '<div class="updated"><p>Success! Offer has been successfully changed.</p></div>';
- }
- }
- $wpdb->show_errors();
- $offerlist = $wpdb->get_results("SELECT * from {$wpdb->prefix}ubam_oto_offers ORDER BY offer", ARRAY_A);
- $wpdb->hide_errors();
- if (!is_array($offerlist)) $offerlist = array();
- $admin_link_prefix = 'options-general.php?page=';
- include_once (OTOMGR_TEMPLATE_DIR . "options-page.htm");
- }
- }
- function getPageUrl($page) {
- global $wpdb;
- if (eregi('http://', $page) || eregi('https://', $page)) return $page;
- $post_ID = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$page' and post_type in ('page', 'post')");
- return get_permalink($post_ID);
- }
- }
- }
- function ubam_oto_load_css_and_js() {
- }
- if (preg_match("/one-time-offer/i", $_REQUEST['page'])) {
- add_action('admin_head', 'ubam_oto_load_css_and_js');
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement