query_vars; if ( array_key_exists( 'adblade-catch', $queryVars ) ) { $url = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); // Fix MIME types for JavaScript requests. if (preg_match('/\.js\/?$/', $queryVars['adblade-catch'])) { header('Content-Type: application/javascript'); } switch($queryVars['adblade-catch']) { case 'show': adblade_proxy( 'web.adblade.com', '/js/ads/async/show.js'); break; case 'css': $cssUrl = str_replace('/' . ADBLADE_PREFIX . '/', '/', $url); adblade_proxy( 'staticd.cdn.adblade.com', $cssUrl); break; case 'image': $parts = explode('/', $url); $parts = array_splice($parts, 2); $extension = array_pop($parts); $path = implode('/', $parts); adblade_proxy( 'staticd.cdn.adblade.com', '/banners/' . $path . '.' . $extension); break; case '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; default: include WP_PLUGIN_DIR . '/adblade-publisher-tools/' . $queryVars['adblade-catch']; }; 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() { // These should not be added until after jQuery load. wp_enqueue_script( 'blockadblock', '/' . ADBLADE_PREFIX . '/js/bootstrap.js', array( 'jquery' ) ); wp_enqueue_script( 'log-ab', '/' . ADBLADE_PREFIX . '/js/main.js', array( 'blockadblock', 'jquery' ) ); } 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 ) { $bypassUrl = '/' . ADBLADE_PREFIX; $defaultDisclosure = 'Advertisement'; $replacements = array( '/\Qimpsc.php\E/' => 'index.html', '#\Qhttp\Es?\Q://staticd.cdn.adblade.com\E#' => $bypassUrl, '/\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/' => $bypassUrl . '/css/zones/_common_dz.css', '#banners/images/(\d+x\d+)/([^.]+)\.([a-z]{3,4})#' => 'images/\1/\2/\3', ); // 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; }