function BuildPostData($dicPostData, $dicFiles, &$contentType) { $isMultipart = is_array($dicFiles) && count($dicFiles) > 0; $result = ""; // ** Multipart post if ($isMultipart) { $boundary = "----CrankyAdsBrowserBoundary2a84c2ab17e842b590ca5a0e43e756fa"; $contentType = "multipart/form-data; boundary=" . $boundary; // Write post data (in multipart format) if (is_array($dicPostData)) { foreach ($dicPostData as $key => $value) { if ($this->bPostDataConvertUnderscoreToPeriod) { $key = str_replace("_", ".", $key); } $result .= CrankyAdsHelper::BuildFormDataMultipart($key, $value, $boundary); } } // Write files (in multipart format) if (is_array($dicFiles)) { foreach ($dicFiles as $key => $value) { if ($this->bPostDataConvertUnderscoreToPeriod) { $key = str_replace("_", ".", $key); } if ($value["name"] != null) { $result .= "--" . $boundary . "\r\n"; $result .= "Content-Disposition: form-data; name=\"" . $key . "\"; filename=\"" . $value["name"] . "\"\r\n"; $result .= "Content-Type: " . $value["type"]; $result .= "\r\n\r\n"; $fileContent = file_get_contents($value["tmp_name"]); $result .= $fileContent; $result .= "\r\n"; } } } // Terminator $result .= "--" . $boundary . "--\r\n"; } else { $contentType = "application/x-www-form-urlencoded"; foreach ($dicPostData as $key => $value) { if ($this->bPostDataConvertUnderscoreToPeriod) { $key = str_replace("_", ".", $key); } if (strlen($result) > 0) { $result .= "&"; } $result .= CrankyAdsHelper::BuildFormDataUrlEncoded($key, $value); } } return $result; }
function ReplaceActionUrlPlaceholders($strLocalActionProxyUrl = false) { // Insert the current Url if ($strLocalActionProxyUrl === false) { $strLocalActionProxyUrl = CrankyAdsHelper::GetFullRequestUri("serverurl"); } $this->ReplaceUrlPlaceholder($this->CA_SERVER_ACTION_URL_PLACEHOLDER, $strLocalActionProxyUrl); }
static function BuildFormDataMultipart($key, $value, $boundary) { $result = ""; // ** Array $value if (is_array($value)) { // * Create and Encode a composite key for each array value foreach ($value as $valueKey => $valueValue) { // Create the composite key $compositeKey = $key; if (preg_match("/^\\d+\$/i", $valueKey)) { // int Array Key $compositeKey .= "[{$valueKey}]"; } else { // string Array Key $compositeKey .= ".{$valueKey}"; } // Encode this Value $result .= CrankyAdsHelper::BuildFormDataMultipart($compositeKey, $valueValue, $boundary); } } else { $result .= "--" . $boundary . "\r\n"; $result .= "Content-Disposition: form-data; name=\"" . $key . "\""; $result .= "\r\n\r\n"; $result .= $value; $result .= "\r\n"; } return $result; }
function AddStandardHttpRequestHeaders(&$dicHeaders) { if (isset($_SERVER["HTTP_CACHE_CONTROL"]) && !CrankyAdsHelper::ArrayKeyExistsCaseInsensitive("Cache-Control", $dicHeaders)) { $dicHeaders["Cache-Control"] = stripslashes_deep($_SERVER["HTTP_CACHE_CONTROL"]); } if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"]) && !CrankyAdsHelper::ArrayKeyExistsCaseInsensitive("If-Modified-Since", $dicHeaders)) { $dicHeaders["If-Modified-Since"] = stripslashes_deep($_SERVER["HTTP_IF_MODIFIED_SINCE"]); } if (isset($_SERVER["HTTP_IF_NONE_MATCH"]) && !CrankyAdsHelper::ArrayKeyExistsCaseInsensitive("If-None-Match", $dicHeaders)) { $dicHeaders["If-None-Match"] = stripslashes_deep($_SERVER["HTTP_IF_NONE_MATCH"]); } }
function LoadHeadData($xmlCssData, $xmlScriptData, $flagsCacheBehaviour, &$bIsAvailable, &$bIsTimedOut) { // ** Setup $bIsAvailable = true; $bIsTimedOut = false; // ** Load custom css $customCss = $xmlCssData->getElementsByTagName("custom"); foreach ($customCss as $iCustom) { // * Get the URL for this custom css $cssUrl = $iCustom->attributes->getNamedItem("src")->nodeValue; $cssUrl = CrankyAdsHelper::UrlGetQuerystringValue($cssUrl, "serverurl"); if ($cssUrl === false) { // The file has likely been been linked to a cache file directly continue; } // * Load the css content $cssEntry = $this->Proxy->GetRemoteContent($cssUrl, false, false, 0, 3, false, false, false, $flagsCacheBehaviour, false, "content"); // Unable to load entry if ($cssEntry === false || $cssEntry->HttpResponseCode !== 200) { $bIsAvailable = false; } else { if ($cssEntry->cIsFromCache && $cssEntry->cIsTimedOut) { $bIsTimedOut = true; } } } // ** Short circuit // Can't get any worse than this, so no need to keep checking if (!$bIsAvailable && $bIsTimedOut) { return; } // ** Load custom scripts $customScript = $xmlScriptData->getElementsByTagName("custom"); foreach ($customScript as $iCustom) { // * Get the URL for this custom css $scriptUrl = $iCustom->attributes->getNamedItem("src")->nodeValue; $scriptUrl = CrankyAdsHelper::UrlGetQuerystringValue($scriptUrl, "serverurl"); if ($scriptUrl === false) { // The file has likely been been linked to a cache file directly continue; } // * Check / Refresh the css content $scriptEntry = $this->Proxy->GetRemoteContent($scriptUrl, false, false, 0, 3, false, false, false, $flagsCacheBehaviour, false, "content"); // Unable to load entry if ($scriptEntry === false || $scriptEntry->HttpResponseCode !== 200) { $bIsAvailable = false; } else { if ($scriptEntry->cIsFromCache && $scriptEntry->cIsTimedOut) { $bIsTimedOut = true; } } } }
function GetCache($url, $requestHeaders = false, $ignoreTimeout = false) { // ** NO CACHE if (CRANKY_ADS_DISABLE_CACHE) { return false; } // ** Clean input $url = trim(strtolower($url)); if (strlen($url) <= 0) { return false; } if (is_array($requestHeaders)) { $requestHeaders = CrankyAdsHelper::ArrayKeyToLower($requestHeaders); } // ** Get the cache entry $cacheEntry = $this->DataContext->GetCacheEntryByUrl($url); if ($cacheEntry === false) { return false; } // ** Check Timeout $now = strtotime(gmdate("Y-m-d H:i:s") . " UTC"); $timestamp = strtotime($cacheEntry->timestamp . " UTC"); $timeoutAt = strtotime("+" . $cacheEntry->timeoutSeconds . " seconds", $timestamp); if (!$ignoreTimeout && $now > $timeoutAt) { return false; } // ** Check the response code $responseCode = 200; if (is_array($requestHeaders) && (isset($requestHeaders["if-modified-since"]) || isset($requestHeaders["if-none-match"])) && (!isset($cacheEntry->httpLastModified) || isset($cacheEntry->httpLastModified) && isset($requestHeaders["if-modified-since"]) && trim(strtolower($cacheEntry->httpLastModified)) == trim(strtolower($requestHeaders["if-modified-since"]))) && (!isset($cacheEntry->httpETag) || isset($cacheEntry->httpETag) && isset($requestHeaders["if-none-match"]) && trim(strtolower($cacheEntry->httpETag)) == trim(strtolower($requestHeaders["if-none-match"])))) { $responseCode = 304; } // ** Read the cached data $cacheData = false; if ($responseCode == 304) { $cacheData = ""; } else { if (isset($cacheEntry->dataFilename)) { $cacheData = $this->LoadDataFromFile($cacheEntry->dataFilename); } else { if (isset($cacheEntry->dataDbFileId)) { $cacheData = $this->LoadDataFromDb($cacheEntry->dataDbFileId); } } // Could not read the data - invalid cache? if ($cacheData === false) { // Delete this cache since there is an error with it $this->DeleteCacheForCacheEntry($cacheEntry); return false; } } // ** Construct the HttpResponse // Headers $headers = array(); $headers["Content-Type"][] = $cacheEntry->httpContentType; if (isset($cacheEntry->httpLastModified)) { $headers["Last-Modified"][] = $cacheEntry->httpLastModified; } if (isset($cacheEntry->httpETag)) { $headers["ETag"][] = $cacheEntry->httpETag; } // Response $result = new CrankyAdsHttpResponse($this->DataContext); $result->Init($responseCode, $headers, $cacheData); // Cache parameters $result->cIsFromCache = true; $result->cIsTimedOut = $now > $timeoutAt; if ($result->cIsTimedOut) { $result->cSecondsSinceTimeout = $now - $timeoutAt; } $result->cRenewHeaders = array(); if (isset($cacheEntry->httpLastModified)) { $result->cRenewHeaders["If-Modified-Since"] = $cacheEntry->httpLastModified; } if (isset($cacheEntry->httpETag)) { $result->cRenewHeaders["If-None-Match"] = $cacheEntry->httpETag; } return $result; }
function ServeRemoteContent() { // ** Serve the content $serverUrl = $_GET["serverurl"]; if (isset($serverUrl)) { // Don't let anyone mess with our output $errorReportingSave = error_reporting(0); if (CRANKY_ADS_DISABLE_ERROR_SUPPRESSION) { error_reporting($errorReportingSave); } // Note: // . For security reasons this page will serve content from the server /Content directory or /Plugin directory ONLY where content has been marked as public. // |-> We don't want users accessing plugin settings without being admins // |-> We don't want malicious users using this page as a proxy to hit just any page on the server (and slowing down this blog) // |-> We can't limit this call to admins only because this page needs to serve images and other content to users of the blog. if (!$this->ContentController->ServeRemoteContent($serverUrl, CrankyAdsHelper::GetFullRequestUri("serverurl"), 0)) { echo "Error contacting the server"; // <- Serving at least something tends to keep colorbox happy } error_reporting($errorReportingSave); } else { echo "Invalid URL"; } die; // We've completed successfully so Wordpress shouldn't output anything else }