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());
     }
 }
 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);
     }
 }
Exemplo n.º 3
0
 /**
  * Fetches all remote resources simultaniously using a multiFetchRequest to optimize rendering time.
  *
  * The preloads will be json_encoded to their gadget document injection format, and the locales will
  * be reduced to only the GadgetContext->getLocale matching entries.
  *
  * @param Gadget $gadget
  * @param GadgetContext $context
  */
 protected function fetchResources(Gadget &$gadget)
 {
     $contextLocale = $this->context->getLocale();
     $unsignedRequests = $signedRequests = array();
     foreach ($gadget->gadgetSpec->locales as $key => $locale) {
         // Only fetch the locales that match the current context's language and country
         if ($locale['country'] == 'all' && $locale['lang'] == 'all' || $locale['lang'] == $contextLocale['lang'] && $locale['country'] == 'all' || $locale['lang'] == $contextLocale['lang'] && $locale['country'] == $contextLocale['country']) {
             if (!empty($locale['messages'])) {
                 $transformedUrl = RemoteContentRequest::transformRelativeUrl($locale['messages'], $this->context->getUrl());
                 if (!$transformedUrl) {
                     // remove any locales that are not applicable to this context
                     unset($gadget->gadgetSpec->locales[$key]);
                     continue;
                 } else {
                     $gadget->gadgetSpec->locales[$key]['messages'] = $transformedUrl;
                 }
                 // locale matches the current context, add it to the requests queue
                 $request = new RemoteContentRequest($gadget->gadgetSpec->locales[$key]['messages']);
                 $request->createRemoteContentRequestWithUri($gadget->gadgetSpec->locales[$key]['messages']);
                 $request->getOptions()->ignoreCache = $this->context->getIgnoreCache();
                 $unsignedRequests[] = $request;
             }
         } else {
             // remove any locales that are not applicable to this context
             unset($gadget->gadgetSpec->locales[$key]);
         }
     }
     if (!$gadget->gadgetContext instanceof MetadataGadgetContext) {
         // Add preloads to the request queue
         foreach ($gadget->getPreloads() as $preload) {
             if (!empty($preload['href'])) {
                 $request = new RemoteContentRequest($preload['href']);
                 if (!empty($preload['authz']) && $preload['authz'] == 'SIGNED') {
                     if ($this->token == '') {
                         throw new GadgetException("Signed preloading requested, but no valid security token set");
                     }
                     $request = new RemoteContentRequest($preload['href']);
                     $request->setAuthType(RemoteContentRequest::$AUTH_SIGNED);
                     $request->setNotSignedUri($preload['href']);
                     $request->setToken($this->token);
                     $request->getOptions()->ignoreCache = $this->context->getIgnoreCache();
                     if (strcasecmp($preload['signViewer'], 'false') == 0) {
                         $request->getOptions()->viewerSigned = false;
                     }
                     if (strcasecmp($preload['signOwner'], 'false') == 0) {
                         $request->getOptions()->ownerSigned = false;
                     }
                     $signedRequests[] = $request;
                 } else {
                     $request->createRemoteContentRequestWithUri($preload['href']);
                     $request->getOptions()->ignoreCache = $this->context->getIgnoreCache();
                     $unsignedRequests[] = $request;
                 }
             }
         }
         // Add template libraries to the request queue
         if ($gadget->gadgetSpec->templatesRequireLibraries) {
             foreach ($gadget->gadgetSpec->templatesRequireLibraries as $key => $libraryUrl) {
                 $request = new RemoteContentRequest($libraryUrl);
                 $transformedUrl = RemoteContentRequest::transformRelativeUrl($libraryUrl, $this->context->getUrl());
                 if (!$transformedUrl) {
                     continue;
                 } else {
                     $gadget->gadgetSpec->templatesRequireLibraries[$key] = $transformedUrl;
                 }
                 $request->createRemoteContentRequestWithUri($gadget->gadgetSpec->templatesRequireLibraries[$key]);
                 $request->getOptions()->ignoreCache = $this->context->getIgnoreCache();
                 $unsignedRequests[] = $request;
             }
         }
     }
     // Perform the non-signed requests
     $responses = array();
     if (count($unsignedRequests)) {
         $brc = new BasicRemoteContent();
         $resps = $brc->multiFetch($unsignedRequests);
         foreach ($resps as $response) {
             $responses[$response->getUrl()] = array('body' => $response->getResponseContent(), 'rc' => $response->getHttpCode());
         }
     }
     // Perform the signed requests
     if (count($signedRequests)) {
         $signingFetcherFactory = new SigningFetcherFactory(Config::get("private_key_file"));
         $remoteFetcherClass = Config::get('remote_content_fetcher');
         $remoteFetcher = new $remoteFetcherClass();
         $remoteContent = new BasicRemoteContent($remoteFetcher, $signingFetcherFactory);
         $resps = $remoteContent->multiFetch($signedRequests);
         foreach ($resps as $response) {
             $responses[$response->getNotSignedUrl()] = array('body' => $response->getResponseContent(), 'rc' => $response->getHttpCode());
         }
     }
     // assign the results to the gadget locales and preloads (using the url as the key)
     foreach ($gadget->gadgetSpec->locales as $key => $locale) {
         if (!empty($locale['messages']) && isset($responses[$locale['messages']]) && $responses[$locale['messages']]['rc'] == 200) {
             $gadget->gadgetSpec->locales[$key]['messageBundle'] = $this->parseMessageBundle($responses[$locale['messages']]['body']);
         }
     }
     if (!$gadget->gadgetContext instanceof MetadataGadgetContext) {
         $preloads = array();
         foreach ($gadget->gadgetSpec->preloads as $key => $preload) {
             if (!empty($preload['href']) && isset($responses[$preload['href']]) && $responses[$preload['href']]['rc'] == 200) {
                 $preloads[] = array_merge(array('id' => $preload['href']), $responses[$preload['href']]);
             }
         }
         $gadget->gadgetSpec->preloads = $preloads;
         if ($gadget->gadgetSpec->templatesRequireLibraries) {
             $requiredLibraries = array();
             foreach ($gadget->gadgetSpec->templatesRequireLibraries as $key => $libraryUrl) {
                 if (isset($responses[$libraryUrl]) && $responses[$libraryUrl]['rc'] == 200) {
                     $requiredLibraries[$libraryUrl] = $responses[$libraryUrl]['body'];
                 }
             }
             $gadget->gadgetSpec->templatesRequireLibraries = $requiredLibraries;
         }
     }
 }
Exemplo n.º 4
0
 /**
  * Peforms the actual http fetching of the data-pipelining requests, all social requests
  * are made to $_SERVER['HTTP_HOST'] (the virtual host name of this server) / (optional) web_prefix / social / rpc, and
  * the httpRequest's are made to $_SERVER['HTTP_HOST'] (the virtual host name of this server) / (optional) web_prefix / gadgets / makeRequest
  * both request types use the current security token ($_GET['st']) when performing the requests so they happen in the correct context
  *
  * @param array $requests
  * @return array response
  */
 private static function performRequests($requests, $context)
 {
     $jsonRequests = array();
     $httpRequests = array();
     $decodedResponse = array();
     // Using the same gadget security token for all social & http requests so everything happens in the right context
     if (!isset($_GET['st'])) {
         throw new ExpressionException("No security token set, required for data-pipeling");
     }
     $securityToken = $_GET['st'];
     foreach ($requests as $request) {
         switch ($request['type']) {
             case 'os:DataRequest':
                 // Add to the social request batch
                 $id = $request['key'];
                 $method = $request['method'];
                 // remove our internal fields so we can use the remainder as params
                 unset($request['key']);
                 unset($request['method']);
                 unset($request['type']);
                 if (isset($request['fields'])) {
                     $request['fields'] = explode(',', $request['fields']);
                 }
                 $jsonRequests[] = array('method' => $method, 'id' => $id, 'params' => $request);
                 break;
             case 'os:HttpRequest':
                 $id = $request['key'];
                 $url = $request['href'];
                 $format = isset($request['format']) ? $request['format'] : 'json';
                 unset($request['key']);
                 unset($request['type']);
                 unset($request['href']);
                 $httpRequests[$url] = array('id' => $id, 'url' => $url, 'format' => $format, 'queryStr' => implode('&', $request));
                 break;
         }
     }
     if (count($jsonRequests)) {
         // perform social api requests
         $request = new RemoteContentRequest('http://' . $_SERVER['HTTP_HOST'] . Config::get('web_prefix') . '/rpc?st=' . urlencode($securityToken) . '&format=json', "Content-Type: application/json\n", json_encode($jsonRequests));
         $request->setMethod('POST');
         $remoteFetcherClass = Config::get('remote_content_fetcher');
         $remoteFetcher = new $remoteFetcherClass();
         $basicRemoteContent = new BasicRemoteContent($remoteFetcher);
         $response = $basicRemoteContent->fetch($request);
         $decodedResponse = json_decode($response->getResponseContent(), true);
     }
     if (count($httpRequests)) {
         $requestQueue = array();
         foreach ($httpRequests as $request) {
             $req = new RemoteContentRequest($_SERVER['HTTP_HOST'] . Config::get('web_prefix') . '/gadgets/makeRequest?url=' . urlencode($request['url']) . '&st=' . urlencode($securityToken) . (!empty($request['queryStr']) ? '&' . $request['queryStr'] : ''));
             $req->getOptions()->ignoreCache = $context->getIgnoreCache();
             $req->setNotSignedUri($request['url']);
             $requestQueue[] = $req;
         }
         $basicRemoteContent = new BasicRemoteContent();
         $resps = $basicRemoteContent->multiFetch($requestQueue);
         foreach ($resps as $response) {
             //FIXME: this isn't completely correct yet since this picks up the status code and headers
             // as they are returned by the makeRequest handler and not the ones from the original request
             $url = $response->getNotSignedUrl();
             $id = $httpRequests[$url]['id'];
             // strip out the UNPARSEABLE_CRUFT (see makeRequestHandler.php) on assigning the body
             $resp = json_decode(str_replace("throw 1; < don't be evil' >", '', $response->getResponseContent()), true);
             if (is_array($resp)) {
                 $statusCode = $response->getHttpCode();
                 $statusCodeMessage = $response->getHttpCodeMsg();
                 $headers = $response->getHeaders();
                 if (intval($statusCode) == 200) {
                     $content = $httpRequests[$url]['format'] == 'json' ? json_decode($resp[$url]['body'], true) : $resp[$url]['body'];
                     $toAdd = array('result' => array('content' => $content, 'status' => $statusCode, 'headers' => $headers));
                 } else {
                     $content = $resp[$url]['body'];
                     $toAdd = array('error' => array('code' => $statusCode, 'message' => $statusCodeMessage, 'result' => array('content' => $content, 'headers' => $headers)));
                 }
                 //$toAdd[$id] = array('id' => $id, 'result' => $httpRequests[$url]['format'] == 'json' ? json_decode($resp[$url]['body'], true) : $resp[$url]['body']);
                 $decodedResponse[] = array('id' => $id, 'result' => $toAdd);
             }
         }
     }
     return $decodedResponse;
 }