false, "error" => null, "source" => "", "headers" => array(), "contentType" => "application/javascript", ); if (empty($uri) || $_SERVER["REQUEST_METHOD"] !== "GET" || stripos($uri, AdmiralAdBlockAnalytics::$scriptURIPrefix) !== 0) { return $res; } $offset = strlen(AdmiralAdBlockAnalytics::$scriptURIPrefix); // check if this is a valid filename $basename = substr($uri, $offset); //make sure the filename doesn't already exist first if (AdmiralAdBlockAnalytics::doesWPContentFileExist($basename)) { //error_log("AdmiralAnalytics: Preventing injecting existing file for $basename"); return $res; } $scripts = array( "", // default is empty string ); $chosenName = false; foreach ($scripts as $script) { $seeds = AdmiralAdBlockAnalytics::getSeedsForScript($script, $host); if (AdmiralAdBlockAnalytics::doesURIContainRandomFilename($uri, $seeds)) { $chosenName = $script; break; } } if ($chosenName === false) { return $res; } $res["matched"] = true; $headers = array(); if (!empty($acceptEncoding)) { $headers[] = "Accept-Encoding: $acceptEncoding"; } if (!empty($_SERVER["HTTP_USER_AGENT"])) { $headers[] = "User-Agent: " . $_SERVER["HTTP_USER_AGENT"]; } $testVersion = false; if (!empty($_SERVER["HTTP_ADMIRAL_TEST"])) { $testVersion = true; } else if (!empty($_COOKIE["admiral_test"])) { $testVersion = true; } $headers[] = "X-Forwarded-For: " . admiraladblock_request_ip_string(); $scriptRes = AdmiralAdBlockAnalytics::fetchScript($headers, $_SERVER["HTTP_HOST"], $chosenName, $testVersion); $res["error"] = $scriptRes["error"]; $res["source"] = $scriptRes["source"]; $res["headers"] = $scriptRes["headers"]; return $res; } function admiraladblock_proxy_record($uri, $host) { $res = array("matched" => false, "error" => null, "source" => "", "headers" => array(), "contentType" => "application/json", ); if (empty($uri) || $_SERVER["REQUEST_METHOD"] !== "POST" || substr_count($uri, "/") > 1) { return $res; } $seeds = AdmiralAdBlockAnalytics::getSeedsForRecord($host); if (!AdmiralAdBlockAnalytics::doesURIContainRandomFilename($uri, $seeds)) { return $res; } $res["matched"] = true; $entityBody = ""; try { $entityBody = file_get_contents('php://input'); } catch (Exception $e) { error_log("AdmiralAnalytics: Error reading POST body: " . $e->getMessage()); } $headers = array( "X-Forwarded-For: " . admiraladblock_request_ip_string(), ); $proxyRes = AdmiralAdBlockAnalytics::proxyRecord($entityBody, $headers); $res["error"] = $proxyRes["error"]; $res["source"] = $proxyRes["source"]; $res["headers"] = $proxyRes["headers"]; return $res; } function admiraladblock_inject_proxy($query) { $acceptEncoding = ""; if (!empty($_SERVER["HTTP_ACCEPT_ENCODING"])) { // we only accept gzip encoding, so we can ungzip if headers were sent if (stripos($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip") !== false && function_exists('gzdecode')) { $acceptEncoding = "gzip"; } else { $acceptEncoding = ""; } } $res = admiraladblock_inject_public_script($_SERVER["REQUEST_URI"], $_SERVER["HTTP_HOST"], $acceptEncoding); if (!$res["matched"]) { $res = admiraladblock_proxy_record($_SERVER["REQUEST_URI"], $_SERVER["HTTP_HOST"]); if (!$res["matched"]) { return $query; } // let the proxy request go through for the record proxy } if (empty($res["error"])) { $shouldDecode = false; if (!empty($res["headers"])) { foreach ($res["headers"] as $header) { if (stripos($header, "content-encoding: gzip") !== false) { $shouldDecode = true; } } } else { // we have no idea what the response is, so we must try $shouldDecode = true; } if (!headers_sent()) { if (!empty($res["headers"])) { foreach ($res["headers"] as $header) { header($header, true); } // since we were able to write headers back, we assume we // wrote the Content-Encoding header $shouldDecode = false; } else { // at least tell the browser that its javascript header("Content-Type: {$res["contentType"]}", true); } } if ($shouldDecode) { // now we need to detect if the content is encoded and if it is decode it // unfortunately gzdecode errors if the data isn't gzip $src = @gzdecode($res["source"]); // if this returned false, then we can assume its not gzip if ($src !== false) { $res["source"] = $src; } } } if (!empty($res["error"])) { $errorString = json_encode($res["error"]); error_log("AdmiralAnalytics: Error proxying: $errorString"); if (function_exists("wp_die")) { wp_die("", "", 500); } } else { echo $res["source"]; } die(); } add_action("parse_request", "admiraladblock_inject_proxy"); function admiraladblock_enqueue_script() { $url = AdmiralAdBlockAnalytics::getPublicScriptURL($_SERVER["HTTP_HOST"]); if (!empty($url)) { // no deps, version is the current time / 5, false not in footer // we only want to cache this for at most 5 seconds $version = "" . floor(time() / 5); wp_enqueue_script("admiral-adblock-analytics", $url, array(), $version, false); } } add_action("wp_enqueue_scripts", "admiraladblock_enqueue_script"); /* EOF */