public function fetchRequest(RemoteContentRequest $request)
 {
     $outHeaders = array();
     if ($request->hasHeaders()) {
         $headers = explode("\n", $request->getHeaders());
         foreach ($headers as $header) {
             if (strpos($header, ':')) {
                 $key = trim(substr($header, 0, strpos($header, ':')));
                 $val = trim(substr($header, strpos($header, ':') + 1));
                 if (strcmp($key, "User-Agent") != 0 && strcasecmp($key, "Transfer-Encoding") != 0 && strcasecmp($key, "Cache-Control") != 0 && strcasecmp($key, "Expries") != 0 && strcasecmp($key, "Content-Length") != 0) {
                     $outHeaders[$key] = $val;
                 }
             }
         }
     }
     $outHeaders['User-Agent'] = "Shindig PHP";
     $options = array();
     $options['timeout'] = Shindig_Config::get('curl_connection_timeout');
     // configure proxy
     $proxyUrl = Shindig_Config::get('proxy');
     if (!empty($proxyUrl)) {
         $options['adapter'] = 'Zend_Http_Client_Adapter_Proxy';
         $proxy = parse_url($proxyUrl);
         if (isset($proxy['host'])) {
             $options['proxy_host'] = $proxy['host'];
         }
         if (isset($proxy['port'])) {
             $options['proxy_port'] = $proxy['port'];
         }
         if (isset($proxy['user'])) {
             $options['proxy_user'] = $proxy['user'];
         }
         if (isset($proxy['pass'])) {
             $options['proxy_pass'] = $proxy['pass'];
         }
     }
     $client = new Zend_Http_Client();
     $client->setConfig($options);
     $client->setUri($request->getUrl());
     $client->setHeaders($outHeaders);
     if ($request->getContentType()) {
         $client->setHeaders(Zend_Http_Client::CONTENT_TYPE, $request->getContentType());
     }
     if ($request->isPost()) {
         $client->setMethod(Zend_Http_Client::POST);
         $client->setRawData($request->getPostBody());
     } else {
         $client->setMethod(Zend_Http_Client::GET);
     }
     $response = $client->request();
     $request->setHttpCode($response->getStatus());
     $request->setContentType($response->getHeader('Content-Type'));
     $request->setResponseHeaders($response->getHeaders());
     $request->setResponseContent($response->getBody());
     $request->setResponseSize(strlen($response->getBody()));
     return $request;
 }
 public function invalidateApplicationResources(array $uris, SecurityToken $token)
 {
     foreach ($uris as $uri) {
         $request = new RemoteContentRequest($uri);
         $this->cache->invalidate($request->toHash());
         // GET
         $request = new RemoteContentRequest($uri);
         $request->createRemoteContentRequestWithUri($uri);
         $this->cache->invalidate($request->toHash());
         // GET & SIGNED
         $request = new RemoteContentRequest($uri);
         $request->setAuthType(RemoteContentRequest::$AUTH_SIGNED);
         $request->setNotSignedUri($uri);
         $this->cache->invalidate($request->toHash());
     }
     if (Doctrine::getTable('SnsConfig')->get('is_use_outer_shindig', false) && Doctrine::getTable('SnsConfig')->get('is_relay_invalidation_notice', true)) {
         require_once 'OAuth.php';
         $shindigUrl = Doctrine::getTable('SnsConfig')->get('shindig_url');
         if (substr($shindigUrl, -1) !== '/') {
             $shindigUrl .= '/';
         }
         $invalidateUrl = $shindigUrl . 'gadgets/api/rest/cache';
         $key = Doctrine::getTable('SnsConfig')->get('shindig_backend_key');
         $secret = Doctrine::getTable('SnsConfig')->get('shindig_backend_secret');
         $consumer = new OAuthConsumer($key, $secret);
         $oauthRequest = OAuthRequest::from_consumer_and_token($consumer, null, 'POST', $invalidateUrl);
         $oauthRequest->set_parameter('xoauth_requestor_id', 1);
         $oauthRequest->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, null);
         $request = new RemoteContentRequest($invalidateUrl . '?xoauth_requestor_id=1');
         $request->setMethod('POST');
         $request->setContentType('application/json');
         $request->setPostBody(json_encode(array('invalidationKeys' => $uris)));
         $request->setHeaders($oauthRequest->to_header());
         $request->getOptions()->ignoreCache = true;
         $remoteContent = Shindig_Config::get('remote_content');
         $fetcher = new $remoteContent();
         $fetcher->fetch($request);
     }
 }
 /**
  * Parses the result content into the headers and body, and retrieves the http code and content type
  *
  * @param RemoteContentRequest $request
  * @param string $content
  */
 protected function parseResult(RemoteContentRequest $request, $content)
 {
     $headers = '';
     $body = '';
     $httpCode = curl_getinfo($request->handle, CURLINFO_HTTP_CODE);
     $contentType = curl_getinfo($request->handle, CURLINFO_CONTENT_TYPE);
     // Attempt to magically convert all text'ish responses to UTF8, especially the xml and json parsers get upset if invalid UTF8 is encountered
     $textTypes = array('text', 'html', 'json', 'xml', 'atom');
     $isTextType = false;
     $isXml = false;
     foreach ($textTypes as $textType) {
         if (strpos($contentType, $textType) !== false) {
             if ($textType === 'xml') {
                 $isXml = true;
             }
             $isTextType = true;
             break;
         }
     }
     if ($isTextType && function_exists('mb_convert_encoding')) {
         // try to retrieve content type out of
         $charset = 'UTF-8';
         $matchedCharset = array();
         if (0 != preg_match("/charset\\s*=\\s*([^\"' >]*)/ix", $content, $matchedCharset) || 0 != preg_match("/encoding\\s*=\\s*[\\'\"]([^\"' >]*)/ix", $content, $matchedCharset)) {
             //xml declaration
             if (trim($matchedCharset[1])) {
                 $charset = trim($matchedCharset[1]);
                 if (($pos = strpos($charset, "\n")) !== false) {
                     $charset = trim(substr($charset, 0, $pos));
                 }
             }
         }
         // the xml and json parsers get very upset if there are invalid UTF8 sequences in the string, by recoding it any bad chars will be filtered out
         $content = mb_convert_encoding($content, 'UTF-8', $charset);
         // if original charset is not utf-8 we now try to rewrite any xml declarations
         if ($isXml === true && strtoupper($charset) !== 'UTF-8') {
             $pattern = 'encoding=\\s*([\'"])' . $charset . '\\s*\\1';
             $content = mb_ereg_replace($pattern, 'encoding="UTF-8"', $content, "i");
         }
     }
     // on redirects and such we get multiple headers back from curl it seems, we really only want the last one
     while (substr($content, 0, strlen('HTTP')) == 'HTTP' && strpos($content, "\r\n\r\n") !== false) {
         $headers = substr($content, 0, strpos($content, "\r\n\r\n"));
         $content = $body = substr($content, strlen($headers) + 4);
     }
     $headers = explode("\n", $headers);
     $parsedHeaders = array();
     foreach ($headers as $header) {
         if (strpos($header, ':')) {
             $key = trim(substr($header, 0, strpos($header, ':')));
             $key = str_replace(' ', '-', ucwords(str_replace('-', ' ', $key)));
             $val = trim(substr($header, strpos($header, ':') + 1));
             $parsedHeaders[$key] = $val;
         }
     }
     if (!$httpCode) {
         $httpCode = '404';
     }
     if (curl_errno($request->handle)) {
         $httpCode = '500';
         $body = 'Curl error: ' . curl_error($request->handle);
     }
     $request->setHttpCode($httpCode);
     $request->setHttpCodeMsg($this->resolveHttpCode($httpCode));
     $request->setContentType($contentType);
     $request->setResponseHeaders($parsedHeaders);
     $request->setResponseContent($body);
     $request->setResponseSize(strlen($content));
 }
 private function fetch(RemoteContentRequest $request)
 {
     if ($request->getUrl() == 'http://test.chabotc.com/ok.html') {
         $request->setHttpCode(200);
         $request->setContentType('text/html; charset=UTF-8');
         $request->setResponseContent('OK');
     } else {
         if ($request->getUrl() == 'http://test.chabotc.com/fail.html') {
             $request->setHttpCode(404);
         } else {
             if (preg_match('/http:\\/\\/test\\.chabotc\\.com\\/valid(\\d)\\.html/', $request->getUrl(), $matches) > 0) {
                 if ($this->valid[intval($matches[1])]) {
                     $this->valid[intval($matches[1])] = false;
                     $request->setHttpCode(200);
                     $request->setContentType('text/html; charset=UTF-8');
                     $request->setResponseContent('OK');
                 } else {
                     $request->setHttpCode(404);
                 }
             } else {
                 if (strpos($request->getUrl(), 'http://test.chabotc.com/signing.html') == 0) {
                     $url = parse_url($request->getUrl());
                     $query = array();
                     parse_str($url['query'], $query);
                     $request->setHttpCode(200);
                     $request->setContentType('text/html; charset=UTF-8');
                     if ($query['xoauth_signature_publickey'] && $query['oauth_signature']) {
                         $request->setResponseContent('OK');
                     } else {
                         $request->setResponseContent('FAILED');
                     }
                 }
             }
         }
     }
 }
 /**
  * Parses the result content into the headers and body, and retrieves the http code and content type
  *
  * @param RemoteContentRequest $request
  * @param string $content
  */
 private function parseResult(RemoteContentRequest $request, $content)
 {
     $headers = '';
     $body = '';
     $httpCode = curl_getinfo($request->handle, CURLINFO_HTTP_CODE);
     $contentType = curl_getinfo($request->handle, CURLINFO_CONTENT_TYPE);
     // Attempt to magically convert all text'ish responses to UTF8, especially the xml and json parsers get upset if invalid UTF8 is encountered
     $textTypes = array('text', 'html', 'json', 'xml', 'atom');
     $isTextType = false;
     foreach ($textTypes as $textType) {
         if (strpos($contentType, $textType) !== false) {
             $isTextType = true;
             break;
         }
     }
     if ($isTextType && function_exists('mb_convert_encoding')) {
         $charset = 'UTF-8';
         preg_match("/charset\\s*=\\s*([^\"' >]*)/ix", $content, $charset);
         if (isset($charset[1])) {
             $charset = trim($charset[1]);
             if (($pos = strpos($charset, "\n")) !== false) {
                 $charset = trim(substr($charset, 0, $pos));
             }
         }
         // the xml and json parsers get very upset if there are invalid UTF8 sequences in the string, by recoding it any bad chars will be filtered out
         $content = mb_convert_encoding($content, 'UTF-8', $charset);
     }
     // on redirects and such we get multiple headers back from curl it seems, we really only want the last one
     while (substr($content, 0, strlen('HTTP')) == 'HTTP' && strpos($content, "\r\n\r\n") !== false) {
         $headers = substr($content, 0, strpos($content, "\r\n\r\n"));
         $content = $body = substr($content, strlen($headers) + 4);
     }
     $headers = explode("\n", $headers);
     $parsedHeaders = array();
     foreach ($headers as $header) {
         if (strpos($header, ':')) {
             $key = trim(substr($header, 0, strpos($header, ':')));
             $key = str_replace(' ', '-', ucwords(str_replace('-', ' ', $key)));
             $val = trim(substr($header, strpos($header, ':') + 1));
             $parsedHeaders[$key] = $val;
         }
     }
     if (!$httpCode) {
         $httpCode = '404';
     }
     $request->setHttpCode($httpCode);
     $request->setHttpCodeMsg($this->resolveHttpCode($httpCode));
     $request->setContentType($contentType);
     $request->setResponseHeaders($parsedHeaders);
     $request->setResponseContent($body);
     $request->setResponseSize(strlen($content));
 }