uniqid(), 'blockadblock' => uniqid(), 'show' => uniqid(), 'impsc' => uniqid(), 'static' => uniqid(), ); update_option( ADBLADE_OPTIONS_KEY, $options, 'yes' ); } } add_action('adblade_cron_event', 'adblade_regenerate_bypass_rules'); function adblade_deactivation() { wp_clear_scheduled_hook( ADBLADE_CRON ); } register_deactivation_hook(__FILE__, 'adblade_deactivation'); // But WordPress has a whitelist of variables it allows, so we must put it on that list function adblade_query_vars( $queryVars ) { $options = get_option( ADBLADE_OPTIONS_KEY ); if (array_key_exists( 'query_var', $options )) { $queryVars[] = $options['query_var']; $queryVars[] = $options['query_var'] . 'path'; } return $queryVars; } add_action( 'query_vars', 'adblade_query_vars' ); // add "settings" link on plugins page function add_action_links ( $links ) { $links[] = 'Settings'; return $links; } add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), 'add_action_links' ); // If this is done, we can access it later // This example checks very early in the process: // if the variable is set, we include our page and stop execution after it function adblade_parse_request( &$wp ) { $options = get_option( ADBLADE_OPTIONS_KEY ); if (do_adblade_bypass() && array_key_exists( 'query_var', $options )) { $queryVars = $wp->query_vars; $queryVar = $options['query_var']; if ( array_key_exists($queryVar, $queryVars ) ) { $url = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); switch($queryVars[$queryVar]) { case $options['redirect_actions']['blockadblock']: header('Content-Type: application/javascript'); echo file_get_contents( WP_PLUGIN_DIR . '/adblade-publisher-tools/js/blockadblock.js' ); break; case $options['redirect_actions']['logab']: header('Content-Type: application/javascript'); include WP_PLUGIN_DIR . '/adblade-publisher-tools/js/log-ab.php'; break; case $options['redirect_actions']['show']: header('Content-Type: application/javascript'); adblade_proxy( 'web.adblade.com', '/js/ads/async/show.js'); break; case $options['redirect_actions']['impsc']: $queryString = filter_input( INPUT_SERVER, 'QUERY_STRING' ); if ( ! empty( $queryString ) ) { adblade_proxy( 'web.adblade.com', sprintf( '/impsc.php?%s=1&%s', ADBLADE_URL_PARAM, $queryString ), true ); } break; case $options['redirect_actions']['static']: $path = $queryVars[$queryVar . 'path']; adblade_proxy( 'staticd.cdn.adblade.com', $path); break; default: include WP_PLUGIN_DIR . '/adblade-publisher-tools/' . $queryVars[$queryVar]; }; exit(); } } } add_action( 'parse_request', 'adblade_parse_request' ); /** * Add shortcode for Adblade ads. * e.g.[adblade container_id="####-##########"] * @param array $atts - The short code attribures. * @return An ad tag */ function adblade_shortcode( $atts ) { $a = shortcode_atts( array( 'container_id' => '', 'host' => 'web.adblade.com', 'protocol' => 'http', 'type' => 2, 'width' => 1, 'height' => 1, ), $atts ); // Do nothing if no container id was set. if ( empty( $a['container_id'] ) ) { return ''; } return sprintf( '', $a['container_id'], $a['host'], $a['protocol'], $a['width'], $a['height'], $a['type'], $a['protocol'], $a['host'] ); } add_shortcode( 'adblade', 'adblade_shortcode' ); /** * Replace "[AdsWithin] with a specified ad tag * @param array $atts - The short code attribures. * @return The replacement text */ function adblade_ads_within_shortcode( $atts ) { $options = get_option( ADBLADE_OPTIONS_KEY ); if ( array_key_exists( 'adsWithinTag', $options ) && ! empty( $options['adsWithinTag'] ) ) { return $options['adsWithinTag']; } return ''; } add_shortcode( 'AdsWithin', 'adblade_ads_within_shortcode' ); /** * Filter posts so we can add tags before and after the content. * @param string $content The content of the post. * @return filtered content */ function adblade_content_filter( $content ) { if ( ! is_single() ) { return $content; } $options = get_option( ADBLADE_OPTIONS_KEY ); $beforeTag = ''; $afterTag = ''; if ( array_key_exists( 'beforePostTag', $options ) && ! empty( $options['beforePostTag'] ) ) { $beforeTag = stripslashes( $options['beforePostTag'] ); } if ( array_key_exists( 'afterPostTag', $options ) && ! empty( $options['afterPostTag'] ) ) { $afterTag = stripslashes( $options['afterPostTag'] ); } return $beforeTag . $content . $afterTag; } add_filter( 'the_content', 'adblade_content_filter' ); /** * Add scripts to page. */ function adblade_enqueue_scripts() { if (do_adblade_bypass()) { $options = get_option( ADBLADE_OPTIONS_KEY ); // we only want to do this if the query_var was successfully made. if (array_key_exists( 'query_var', $options )) { // These should not be added until after jQuery load. wp_enqueue_script( 'blockadblock', '/index.php?' . $options['query_var'] . '=' . $options['redirect_actions']['blockadblock'], array( 'jquery' ), null, true ); wp_enqueue_script( 'log-ab', '/index.php?' . $options['query_var'] . '=' . $options['redirect_actions']['logab'], array( 'blockadblock', 'jquery' ), null, true ); } } } add_action( 'wp_enqueue_scripts', 'adblade_enqueue_scripts' ); /** * Make requests to the adblade servers. * @param string $host The host the request is going to. * @param string $path The path of the URL. * @param boolean $skipCache Whether or not we should skip the cache. */ function adblade_proxy( $host, $path, $skipCache = false ) { // Make sure it is an adblade domain. $validDomains = array( 'web.adblade.com', 'web.industrybrains.com', 'static.adblade.com', 'staticd.cdn.adblade.com', 'static.industrybrains.com', 'staticd.cdn.industrybrains.com', ); if ( ! in_array( $host, $validDomains ) ) { return; } // Make sure the request matches one we are familiar with. $requestMatch = false; $validRequestPatterns = array( '#^/banners/images/\d+x\d+/.*\.[a-z]{3,4}$#', '#^/css/.*css$#', '#^/impsc?.php#', '#^/js/ads/async/show.js$#', ); foreach ( $validRequestPatterns as $pattern ) { if ( preg_match( $pattern, $path ) ) { $requestMatch = true; break; } } if ( ! $requestMatch ) { return; } $url = sprintf( 'http://%s%s', $host, $path ); /** * A callback function to proxy requests. * @param $url * @return mixed (array|false) false if there was an error */ $proxy = function ( $url ) { $options = get_option( ADBLADE_OPTIONS_KEY ); $defaultDisclosure = 'Advertisement'; $replacements = array( '/\Qimpsc.php?\E/' => sprintf( '/index.php?%s=%s&', $options['query_var'], $options['redirect_actions']['impsc'] ), '#\Qhttp\Es?\Q://staticd.cdn.adblade.com\E#' => sprintf( '/index.php?%s=%s&%spath=', $options['query_var'], $options['redirect_actions']['static'], $options['query_var'] ), '/\Qad_type_1\E/' => 'blade_type_1', '/\QAds by Adblade\E/' => $defaultDisclosure, '/\Qclicks.php?\E/' => sprintf( 'clicks.php?%s=1&', ADBLADE_URL_PARAM ), '/\Q_common_dz.css\E/' => sprintf( '/index.php?%s=%s&%spath=/css/zones/_common_dz.css', $options['query_var'], $options['redirect_actions']['static'], $options['query_var'] ), ); // Make the request. $response = wp_safe_remote_get( $url, array( 'user-agent' => 'Adblade WordPress Plugin', 'headers' => array( 'X-Forwarded-For' => array_key_exists( 'HTTP_X_FORWARDED_FOR', $_SERVER ) ? filter_input( INPUT_SERVER, 'HTTP_X_FORWARDED_FOR' ) : filter_input( INPUT_SERVER, 'REMOTE_ADDR' ), ), 'cookies' => array( '__tuid' => '6198487235506032324', ), ) ); if ( is_wp_error( $response ) ) { header( 'HTTP/1.0 500 Internal Server Error' ); echo $response->get_error_message(); return false; } $result = array( // Replace anything that an ad blocker might not like. 'body' => preg_replace( array_keys( $replacements ), array_values( $replacements ), $response['body'] ), ); // Add the content type header, if one was sent from adblade. if ( array_key_exists( 'content-type', $response['headers'] ) ) { $result['headers'] = array( 'content-type' => $response['headers']['content-type'], ); } return $result; }; $responseHandler = function ( $response ) { if ( $response ) { if ( array_key_exists( 'headers', $response ) && array_key_exists( 'content-type', $response['headers'] ) ) { header( 'Content-type: ' . $response['headers']['content-type'] ); } echo $response['body']; return; } }; if ( $skipCache ) { $response = $proxy($url); $responseHandler($response); return; } else { // This is a static call, use a cache. header( 'Cache-Control: public' ); header( sprintf( 'Expires: %s GMT', gmdate( 'D, d M Y H:i:s', time() + HOUR_IN_SECONDS ) ) ); $transient = get_transient( $url ); if ( ! $transient ) { // Get transient and set it. $response = $proxy($url); if ( ! $response ) { set_transient( $url, $response, MINUTE_IN_SECONDS ); } $responseHandler($response); return; } else { $responseHandler($transient); return; } } } /** * Whether or not bypass is enabled. * @return boolean -True if enabled. */ function do_adblade_bypass() { $options = get_option( ADBLADE_OPTIONS_KEY ); return array_key_exists( 'bypass', $options ) && intval( $options['bypass'] ) === 1; }