AdBlock X
* Author: DDC Inc.
* Author URI: http://www.adblockx.com
* License: GPLv2 or later
*/
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
define( 'DDC_ABX_PLUGIN_VER', '3.0.2' );
define( 'DDC_ABX_PLUGIN_URI', plugins_url('', __FILE__) );
define( 'DDC_ABX_PLUGIN_DIR', trailingslashit( dirname(__FILE__) ) );
define( 'ABX_ADMINSLUG', 'abx-settings' );
define( 'DDC_ABX_SETTINGS', 'ddc-abx-settings');
define( 'DDC_ABX_TRACKING_OPTIONS', 'abx_tracking_options');
define( 'DDC_OPTION_TRACKING_SECTION', 'ddc-tracking-section');
class adblock_x
{
function __construct() {}
public function load_js()
{
$this->options = get_option(DDC_ABX_TRACKING_OPTIONS);
$tracking_code = '';
if ( ! empty($this->options['tracking_code']))
{
$tracking_code = $this->options['tracking_code'];
}
if ( ! empty($tracking_code))
{
// Get file timestamp
$file_path = $this->get_javascript_file_path($tracking_code);
$file_timestamp = 0;
$cache_busting_timestamp = '';
if (isset($file_path) AND file_exists($file_path))
{
$file_timestamp = filemtime($file_path);
$cache_busting_timestamp = $file_timestamp;
}
$diff = time() - $file_timestamp;
if ($diff >= (60*60))
{
$this->refresh_js($tracking_code);
}
if (file_exists($file_path))
{
$new_timestamp = filemtime($file_path);
if ($file_timestamp != $new_timestamp)
{
$cache_busting_timestamp = $new_timestamp;
}
}
wp_enqueue_script('ddc-abx', $this->get_javascript_url($tracking_code), array(), $cache_busting_timestamp, false);
}
}
private function refresh_js($tracking_code)
{
$beacon_url = urlencode(admin_url('admin-ajax.php'));
$file_path = $this->get_javascript_file_path($tracking_code);
if (isset($file_path))
{
$source = 'http://cdn.adblockx.com/js/v3/'.$tracking_code.'/abx.js?b_e_a_c_o_n='.$beacon_url;
$response = wp_remote_get($source);
if( ! is_wp_error( $response ) AND isset($response['body']) )
{
$file = fopen($file_path, "w+");
fputs($file, trim($response['body']));
fclose($file);
}
}
}
private function get_javascript_url($tracking_code)
{
$path = NULL;
$file_name = $this->get_javascript_file_name($tracking_code);
$upload_directory = $this->get_javscript_upload_dir();
$url = isset($upload_directory['baseurl']) ? $upload_directory['baseurl'] : NULL;
if (! empty($url) AND ! empty($file_name))
{
$path = $url."/".$file_name;
}
return $path;
}
private function get_javascript_file_path($tracking_code)
{
$path = NULL;
$file_name = $this->get_javascript_file_name($tracking_code);
$upload_directory = $this->get_javscript_upload_dir();
$dir = ($upload_directory['basedir']) ? $upload_directory['basedir'] : NULL;
if (! empty($dir) AND ! empty($file_name))
{
$path = $dir.DIRECTORY_SEPARATOR.$file_name;
}
return $path;
}
private function get_javascript_file_name($tracking_code)
{
$name = NULL;
if (! empty($tracking_code))
{
$name = md5($tracking_code).".js";
}
return $name;
}
private function get_javscript_upload_dir()
{
$url = wp_upload_dir();
if (is_ssl()) $url = str_replace('http://', 'https://', $url);
return $url;
}
function admin_notice_new_version() {
$args = (object) array( 'slug' => 'adblock-x' );
$request = array( 'action' => 'plugin_information', 'timeout' => 15, 'request' => serialize( $args) );
$url = 'http://api.wordpress.org/plugins/info/1.0/';
$response = wp_remote_post( $url, array( 'body' => $request ) );
if( ! is_wp_error( $response ) AND isset($response['body']) )
{
$plugin_info = unserialize($response['body']);
$repo_plugin_version = $plugin_info->version;
if ($repo_plugin_version != DDC_ABX_PLUGIN_VER)
{
echo <<<_HTML_
There is a new version of Adblock X. Go to Plugins and update to latest version.
_HTML_;
}
}
}
function add_actions()
{
if ( is_admin() )
{
add_action( 'admin_notices', array( $this, 'admin_notice_new_version') );
add_action( 'admin_menu', array( $this, 'abx_admin_options'));
/* is this location ok? HC */
wp_register_style('abx_style', plugins_url('css/style.css', __FILE__));
wp_enqueue_style('abx_style');
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( &$this, 'plugin_settings_link' ), 10, 4 );
add_action( 'wp_ajax_rxn_pageview', array( $this, 'rxn_pageview_callback'));
add_action( 'wp_ajax_nopriv_rxn_pageview', array( $this, 'rxn_pageview_callback'));
add_action( 'wp_ajax_rxn_impression', array( $this, 'rxn_impression_callback'));
add_action( 'wp_ajax_nopriv_rxn_impression', array( $this, 'rxn_impression_callback'));
add_action( 'wp_ajax_rxn_preview', array( $this, 'rxn_preview_callback'));
add_action( 'wp_ajax_nopriv_rxn_preview', array( $this, 'rxn_preview_callback'));
add_action( 'admin_footer', array($this, 'rxn_refresh_javascript'));
add_action( 'wp_ajax_rxn_refresh', array( $this, 'rxn_refresh_callback'));
}
add_action( 'wp_enqueue_scripts', array( $this, 'load_js') );
add_action( 'wp_ajax_rxn_pageview', array( $this, 'rxn_pageview_callback'));
add_action( 'wp_ajax_nopriv_rxn_pageview', array( $this, 'rxn_pageview_callback'));
add_action( 'wp_ajax_rxn_impression', array( $this, 'rxn_impression_callback'));
add_action( 'wp_ajax_nopriv_rxn_impression', array( $this, 'rxn_impression_callback'));
add_action( 'wp_ajax_rxn_preview', array( $this, 'rxn_preview_callback'));
add_action( 'wp_ajax_nopriv_rxn_preview', array( $this, 'rxn_preview_callback'));
}
function plugin_settings_link( $actions, $plugin_file, $plugin_data, $context ) {
// add a 'Settings' link on plugin
return array_merge(
array(
'settings' => '' . __( 'Settings' ) . ''
),
$actions
);
}
function display_abx_options($tracking_code, $error_message)
{
$refresh_message = '';
$refresh_class = '';
if (empty($tracking_code))
{
$options = get_option(DDC_ABX_TRACKING_OPTIONS );
$tracking_code = isset($options['tracking_code']) ? $options['tracking_code'] : NULL;
}
if ( ! empty($tracking_code))
{
$file_name = $this->get_javascript_file_path($tracking_code);
if ( ! file_exists($file_name))
{
$refresh_message = "The required javascript does not exist. Please download it by clicking the 'Manual update' button.";
$refresh_class = 'class="refresh-failure"';
}
else
{
$file_timestamp = date('Y-m-d H:i:s', filemtime($file_name));
$refresh_message = "AdBlock X javascript file last modified on {$file_timestamp}.";
$refresh_class = 'class="refresh-success"';
}
}
require( DDC_ABX_PLUGIN_DIR.'/display-abx-options.php');
}
function create_abx_options()
{
$tracking_code = NULL;
$error_msg = '';
$this->options = get_option( DDC_ABX_TRACKING_OPTIONS );
if (!empty($this->options) && !empty($this->options['tracking_code']))
{
$tracking_code = $this->options['tracking_code'];
}
elseif ($_SERVER["REQUEST_METHOD"] == "POST")
{
$url = "http://admin.adblockx.com/api/v1/user/ab_register";
$post_data = array(
'first_name' => $_POST['first_name'],
'last_name' => empty($_POST['last_name']) ? '' : $_POST['last_name'],
'email' => $_POST['email'],
'password' => $_POST['password'],
'company_name' => $_POST['company_name'],
'property_name' => $_POST['property_name'],
'property_url' => $_POST['property_url'],
'beacon_url' => admin_url('admin-ajax.php'),
);
$response = wp_remote_post( $url, array(
'method' => 'POST',
'headers' => array(),
'timeout' => 20,
'blocking' => true,
'body' => json_encode($post_data),
'cookies' => array(),
)
);
if ( is_wp_error( $response ) )
{
$error_msg = "Something went wrong with registration.";
}
else
{
$output_arr = json_decode(trim($response['body']));
if ($output_arr->error_count > 0)
{
$error_msg = $output_arr->reply->items[0]->error_message;
}
else
{
$tracking_code = $output_arr->reply->items[0]->tracking_code;
if ( ! empty($tracking_code))
{
// Pull latest javascript upon successful registration
$this->refresh_js($tracking_code);
}
$this->update_tracking_code($tracking_code);
}
}
}
$this->display_abx_options($tracking_code, $error_msg);
}
function update_tracking_code($tracking_code)
{
$this->options['tracking_code'] = $tracking_code;
update_option(DDC_ABX_TRACKING_OPTIONS, $this->options);
}
function register_abx_options($options)
{
$this->options = get_option(DDC_ABX_TRACKING_OPTIONS);
if (empty($this->options))
{
add_option(DDC_ABX_TRACKING_OPTIONS);
}
register_setting( DDC_ABX_SETTINGS, DDC_ABX_TRACKING_OPTIONS, array( $this, "settings_validate") );
add_settings_section( DDC_OPTION_TRACKING_SECTION, 'Main Settings', array( $this, 'plugin_section_text'), DDC_ABX_TRACKING_OPTIONS );
add_settings_field('plugin_tracking_string', 'Tracking id', array($this, 'plugin_setting_token'), DDC_ABX_TRACKING_OPTIONS, DDC_OPTION_TRACKING_SECTION );
add_settings_field('plugin_email', 'Email', array($this, 'plugin_setting_email'), DDC_ABX_TRACKING_OPTIONS, DDC_OPTION_TRACKING_SECTION );
}
function settings_validate($inputarr)
{
$newarr = array();
if ($inputarr['id_val'])
{
$newarr['id_val'] = sanitize_text_field($inputarr['id_val']);
}
if ($inputarr['email'])
{
$newarr['email'] = sanitize_text_field($inputarr['email']);
}
return $newarr;
}
function plugin_setting_token()
{
$input_name = DDC_ABX_TRACKING_OPTIONS;
$token_val = isset( $this->options['id_val'] ) ? $this->options['id_val'] : '';
echo "";
}
function plugin_setting_email()
{
$input_name = DDC_ABX_TRACKING_OPTIONS;
$token_val = isset( $this->options['email'] ) ? $this->options['email'] : '';
echo "";
}
function plugin_section_text()
{
}
function abx_admin_options() {
add_options_page("AdBlock X Options", "AdBlock X", 'manage_options', ABX_ADMINSLUG, array( $this, 'create_abx_options') );
}
function rxn_pageview_callback()
{
$this->rxn_beacon_callback();
}
function rxn_impression_callback()
{
$this->rxn_beacon_callback();
}
function rxn_preview_callback()
{
$preview_key = isset($_REQUEST['rxn_preview']) ? $_REQUEST['rxn_preview'] : NULL;
$output = '';
if ( ! empty($preview_key))
{
$url = 'http://cdn.adblockx.com/js/'.$preview_key.'/preview.js';
$response = wp_remote_get($url);
if( ! is_wp_error( $response ) AND isset($response['body']))
{
$output = trim($response['body']);
}
}
header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache");
header("Expires: Thu, 01 Jan 1970 00:00:00 GMT");
header('Content-type: text/javascript');
echo $output;
wp_die();
}
private function rxn_beacon_callback()
{
$this->options = get_option( DDC_ABX_TRACKING_OPTIONS );
if (!empty($this->options) && !empty($this->options['tracking_code']))
{
$tracking_code = $this->options['tracking_code'];
}
if ( ! empty($tracking_code))
{
$ip = '';
if ( ! empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
{
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
}
elseif ( ! empty($_SERVER['HTTP_X_FORWARDED']))
{
$ip = $_SERVER['HTTP_X_FORWARDED'];
}
elseif ( ! empty($_SERVER['HTTP_FORWARDED_FOR']))
{
$ip = $_SERVER['HTTP_FORWARDED_FOR'];
}
elseif ( ! empty($_SERVER['HTTP_FORWARDED']))
{
$ip = $_SERVER['HTTP_FORWARDED'];
}
elseif ( ! empty($_SERVER['REMOTE_ADDR']))
{
$ip = $_SERVER['REMOTE_ADDR'];
}
$user_agent = empty($_SERVER['HTTP_USER_AGENT']) ? '' : $_SERVER['HTTP_USER_AGENT'];
$url = "http://api.jdirectj.com/abl.php";
$url .= '?'.$_SERVER['QUERY_STRING'];
$url .= '&ua='.urlencode($user_agent);
$url .= '&ip='.urlencode($ip);
$url .= '&tracking_code='.$tracking_code;
wp_remote_get($url);
}
header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache");
header("Expires: Thu, 01 Jan 1970 00:00:00 GMT");
header('Content-Type: image/gif');
echo base64_decode('R0lGODlhAQABAJAAAP8AAAAAACH5BAUQAAAALAAAAAABAAEAAAICBAEAOw==');
wp_die();
}
function run()
{
$this->add_actions();
}
function rxn_refresh_javascript() { ?>
refresh_js($tracking_code);
$file_path = $this->get_javascript_file_path($tracking_code);
if (file_exists($file_path))
{
$file_timestamp = date('Y-m-d H:i:s', filemtime($file_path));
$response['msg'] = "File was refreshed on {$file_timestamp}.";
}
else
{
$response['error'] = 'File does not exist and could not be refreshed. Please contact customer support for further assistance.';
}
}
header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache");
header("Expires: Thu, 01 Jan 1970 00:00:00 GMT");
echo json_encode($response);
wp_die();
}
}
$abx = new adblock_x();
$abx->run();