"", "error" => null, "headers" => array(), ); $urlWithScheme = "https:" . $url; $ua = self::$UASuffix; $foundUA = false; foreach ($headers as $key => $header) { if (stripos($header, "user-agent") !== false) { $parts = explode(":", $header, 2); $ua = (isset($parts[1]) ? trim($parts[1]) . " " : "") . $ua; $headers[$key] = "User-Agent: $ua"; $foundUA = true; } } if (!$foundUA) { $headers[] = "User-Agent: $ua"; } if (function_exists("curl_init")) { if (!empty($postBody) && !defined("CURLOPT_SAFE_UPLOAD") && stripos($postBody, "@") === 0) { $res["error"] = array("code" => -99999999, "str" => "Unallowed postBody starting with @", "type" => "self", ); return $res; } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $urlWithScheme); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // we cannot use FOLLOWLOCATION is open_basedir is set //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3); curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1); // try to prevent caching curl_setopt($ch, CURLOPT_FRESH_CONNECT, true); curl_setopt($ch, CURLOPT_FORBID_REUSE, true); $verbose = null; if (function_exists("stream_get_contents")) { curl_setopt($ch, CURLOPT_VERBOSE, true); $verbose = fopen("php://temp", "w+"); curl_setopt($ch, CURLOPT_STDERR, $verbose); } if (!empty($headers)) { curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); } if (!empty($postBody)) { curl_setopt($ch, CURLOPT_POSTFIELDS, $postBody); if (defined("CURLOPT_SAFE_UPLOAD")) { curl_setopt($ch, CURLOPT_SAFE_UPLOAD, 1); } } curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . "/caroots.crt"); $src = curl_exec($ch); if ($src === false) { $err = curl_errno($ch); $str = ""; if (function_exists("curl_strerror")) { $str = curl_strerror($err); } $verboseLog = ""; if (!empty($verbose) && function_exists("rewind")) { rewind($verbose); $verboseLog = stream_get_contents($verbose); } $res["error"] = array("code" => $err, "str" => $str, "type" => "curl", "debug" => $verboseLog, ); } else { $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); if (function_exists("mb_substr")) { $header = mb_substr($src, 0, $headerSize); } else { $header = substr($src, 0, $headerSize); } $recvHeaders = explode("\r\n", $header); foreach ($recvHeaders as $h) { foreach (self::$headersToCopy as $hc) { if (stripos($h, $hc) === 0) { $res["headers"][] = $h; break; } } } //todo: look for "Location" header if (function_exists("mb_substr")) { $res["source"] = mb_substr($src, $headerSize); } else { $res["source"] = substr($src, $headerSize); } } curl_close($ch); } elseif (function_exists("wp_remote_retrieve_body") && function_exists("wp_safe_remote_get") && function_exists("wp_safe_remote_post")) { $args = array( "headers" => $headers, "user-agent" => $ua, ); if (!empty($postBody)) { $args["body"] = $postBody; $resp = wp_safe_remote_post($urlWithScheme, $args); } else { $resp = wp_safe_remote_get($urlWithScheme, $args); } if (function_exists('is_wp_error') && is_wp_error($resp)) { $res["error"] = array("code" => $resp->get_error_code(), "str" => $resp->get_error_message(), "type" => "wp", ); } else { $body = wp_remote_retrieve_body($resp); if (empty($body)) { $res["error"] = array("code" => 0, "str" => "Unknown error but empty body", "type" => "wp", ); } else { $res["source"] = $body; $headers = wp_remote_retrieve_headers($resp); foreach ($headers as $key => $val) { $src["headers"][] = "$key: $val"; } } } } return $res; } public static function registerSettings() { register_setting("admiral_property_settings", self::PROPERTY_OPTION_ID_KEY); register_setting("admiral_property_settings", self::EMBED_OPTION_KEY); register_setting("admiral_property_settings", self::APPEND_PHP_OPTION_KEY); } public static function renderOptions() { if (!current_user_can('manage_options')) { wp_die(__('You do not have sufficient permissions to access this page.')); } $currentPropertyID = self::getPropertyID(); echo '

The Admiral Wordpress Plugin allows you to easily measure how much revenue you are losing to adblock.

'; settings_fields('admiral_property_settings'); do_settings_sections('admiral_property_settings'); echo ''; echo ''; echo '
Property ID'; $pidValue = $currentPropertyID; if (function_exists('htmlspecialchars')) { $pidValue = htmlspecialchars($pidValue); } else { $pidValue = str_replace('"', '"', $pidValue); } echo ''; echo '

The property ID is used to uniquely identify your site. If you already have an account at getadmiral.com enter your property ID found on the property settings page here. If you don’t have an account yet don’t worry, just head to getadmiral.com/a/claim-property to access your analytics.'; echo '

'; // Always clear the embed when properties are updated to safeguard against out of sync embed properties echo ''; submit_button(); echo '
'; } private static function getSecretPromiseCall() { $postData = array( "method" => "Partner.CreateSecretPromise", "jsonrpc" => "2.0", "params" => array( "clientID" => self::$clientID, "clientSecret" => self::$clientSecret ) ); $res = self::httpCall(self::$partnerApiEndpoint, array(), json_encode($postData)); if(empty($res["source"])){ return ""; } $body = json_decode($res["source"]); if(empty($body->result)){ return ""; } return $body->result->secretPromise; } private static function createPropertyCall($secretPromise, $domain) { $postData = array( "method" => "Partner.NewOrphanProperty", "jsonrpc" => "2.0", "params" => array( "clientID" => self::$clientID, "clientSecretPromise" => $secretPromise, "domain" => $domain, "withEmbed" => true ) ); $res = self::httpCall(self::$partnerApiEndpoint, array(), json_encode($postData)); if(empty($res["source"])){ return ""; } $body = json_decode($res["source"]); if(empty($body->result)){ return ""; } return $body->result; } /** * Function to create new anonymous property */ public static function createNewProperty($domain) { $secretPromise = self::getSecretPromiseCall(); if(empty($domain)){ $domain = get_site_url(); } if(!empty($secretPromise)){ $property = self::createPropertyCall($secretPromise, $domain); if(!empty($property)){ self::setPropertyID($property->propertyID); self::setPropertyPromiseID($property->propertyPromiseID); update_option(self::PROPERTY_OPTION_ID_KEY, self::getPropertyID()); update_option(self::PROPERTY_PROMISE_OPTION_ID_KEY, self::getPropertyPromiseID()); update_option(self::PROPERTY_PROMISE_PROPERTY_OPTION_ID_KEY, self::getPropertyID()); // Get embed will handle any potential fetching of embed code at this point self::setEmbed($property->embed); return true; } else { return false; } } } // Make a request to the delivery service to get a script for this property private static function requestEmbedScript() { $propertyID = self::getPropertyID(); if(!self::isPropertyOrphanProperty($propertyID)){ $res = self::httpCall("//delivery.api.getadmiral.com/script/" . $propertyID . "/bootstrap", array()); return ''; } $secretPromise = self::getSecretPromiseCall(); $postData = array( "method" => "Partner.GetEmbedCode", "jsonrpc" => "2.0", "params" => array( "clientID" => self::$clientID, "clientSecretPromise" => $secretPromise, "propertyID" => $propertyID, "propertyPromiseID" => self::getPropertyPromiseID() ) ); $res = self::httpCall(self::$partnerApiEndpoint, array(), json_encode($postData)); if(empty($res["source"])){ return ""; } $body = json_decode($res["source"]); if(empty($body->result)){ return ""; } if(empty($body->result->embed)){ return ""; } return $body->result->embed; } /** * Function to add an alert to the top of the admin dashboard * to let a user know they need to initialize admiral. */ public static function alertAdminToActivate() { add_action('admin_head-index.php', function(){ $html = <<

Block Adblock now comes with advanced adblock analytics from Admiral. Click accept to start collecting data and discover how much revenue you're losing to adblockers. Click here to learn more.

HTML; echo $html; }); } } add_action('admin_post_activate_admiral_adblocks_analytics', function(){ // Call redirect before all so that the user isn't sent to a blank page in case of handled error. $referer = wp_get_referer(); if(!empty($referer)){ wp_redirect($referer); } else { wp_redirect('index.php'); } if(array_key_exists("accept", $_POST)){ AdmiralAdBlockAnalytics::createNewProperty(""); } AdmiralAdBlockAnalytics::hideNotice(); }); /* EOF */