/** * Retrieves a gadget specification from the Internet, processes its views and * adds it to the cache. */ private function fetchFromWeb($url, $ignoreCache) { $remoteContentRequest = new RemoteContentRequest($url); $remoteContentRequest->getOptions()->ignoreCache = $ignoreCache; $remoteContent = new BasicRemoteContent(); $spec = $remoteContent->fetch($remoteContentRequest); $gadgetSpecParser = new GadgetSpecParser(); $gadgetSpec = $gadgetSpecParser->parse($spec->getResponseContent()); return $gadgetSpec; }
/** * Retrieves a gadget specification from the Internet, processes its views and * adds it to the cache. */ private function fetchFromWeb($url, $ignoreCache) { $remoteContentRequest = new RemoteContentRequest($url); $remoteContentRequest->getOptions()->ignoreCache = $ignoreCache; $remoteContent = new BasicRemoteContent(); $spec = $remoteContent->fetch($remoteContentRequest); $gadgetSpecParserClass = Config::get('gadget_spec_parser'); $gadgetSpecParser = new $gadgetSpecParserClass(); $gadgetSpec = $gadgetSpecParser->parse($spec->getResponseContent(), $this->context); return $gadgetSpec; }
public function getFeatureContent($feature, GadgetContext $context, $isGadgetContext) { if (empty($feature)) { return ''; } if (!isset($this->features[$feature])) { throw new GadgetException("Invalid feature: " . htmlentities($feature)); } $featureName = $feature; $feature = $this->features[$feature]; $filesContext = $isGadgetContext ? 'gadgetJs' : 'containerJs'; if (!isset($feature[$filesContext])) { // no javascript specified for this context return ''; } $ret = ''; if (Config::get('compress_javascript')) { $featureCache = Cache::createCache(Config::get('feature_cache'), 'FeatureCache'); if ($featureContent = $featureCache->get(md5('features:' . $featureName . $isGadgetContext))) { return $featureContent; } } foreach ($feature[$filesContext] as $entry) { switch ($entry['type']) { case 'URL': $request = new RemoteContentRequest($entry['content']); $request->getOptions()->ignoreCache = $context->getIgnoreCache(); $response = $context->getHttpFetcher()->fetch($request); if ($response->getHttpCode() == '200') { $ret .= $response->getResponseContent() . "\n"; } break; case 'FILE': $file = $feature['basePath'] . '/' . $entry['content']; $ret .= file_get_contents($file) . "\n"; break; case 'INLINE': $ret .= $entry['content'] . "\n"; break; } } if (Config::get('compress_javascript')) { $ret = JsMin::minify($ret); $featureCache->set(md5('features:' . $featureName . $isGadgetContext), $ret); } return $ret; }
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); } }
/** * Fetches the gadget xml for the requested URL using the http fetcher * * @param unknown_type $gadgetUrl * @return string gadget's xml content */ protected function fetchGadget($gadgetUrl) { $request = new RemoteContentRequest($gadgetUrl); $request->setToken($this->token); $request->getOptions()->ignoreCache = $this->context->getIgnoreCache(); $xml = $this->context->getHttpFetcher()->fetch($request); if ($xml->getHttpCode() != '200') { throw new GadgetException("Failed to retrieve gadget content (recieved http code " . $xml->getHttpCode() . ")"); } return $xml->getResponseContent(); }
/** * Get honest-to-goodness user data. */ private function fetchData() { try { // TODO: it'd be better using $this->realRequest->getContentType(), but not set before hand. Temporary hack. $postBody = $this->realRequest->getPostBody(); $url = $this->realRequest->getUrl(); $msgParams = array(); if (ShindigOAuthUtil::isFormEncoded($this->realRequest->getHeader("Content-Type")) && strlen($postBody) > 0) { $entries = explode('&', $postBody); foreach ($entries as $entry) { $parts = explode('=', $entry); if (count($parts) == 2) { $msgParams[ShindigOAuthUtil::urldecode_rfc3986($parts[0])] = ShindigOAuthUtil::urldecode_rfc3986($parts[1]); } } } $method = $this->realRequest->getMethod(); $msgParams[self::$XOAUTH_APP_URL] = $this->authToken->getAppUrl(); // Build and sign the message. $oauthRequest = $this->newRequestMessageMethod($method, $url, $msgParams); $oauthParams = $this->filterOAuthParams($oauthRequest); $newHeaders = array(); switch ($method) { case 'POST': if (empty($postBody) || count($postBody) == 0) { $postBody = ShindigOAuthUtil::getPostBodyString($oauthParams); } else { $postBody = $postBody . "&" . ShindigOAuthUtil::getPostBodyString($oauthParams); } // To avoid 417 Response from server, adding empty "Expect" header $newHeaders['Expect'] = ''; break; case 'GET': $url = ShindigOAuthUtil::addParameters($url, $oauthParams); break; } // To choose HTTP method client requested, we don't use $this->createRemoteContentRequest() here. $rcr = new RemoteContentRequest($url); $rcr->createRemoteContentRequest($method, $url, $newHeaders, null, $this->realRequest->getOptions()); $rcr->setPostBody($postBody); $remoteFetcherClass = Config::get('remote_content_fetcher'); $fetcher = new $remoteFetcherClass(); $content = $fetcher->fetchRequest($rcr); $statusCode = $content->getHttpCode(); //TODO is there a better way to detect an SP error? For example: http://wiki.oauth.net/ProblemReporting if ($statusCode == 401) { $tokenKey = $this->buildTokenKey(); $this->tokenStore->removeTokenAndSecret($tokenKey); } else { if ($statusCode >= 400 && $statusCode < 500) { $message = $this->parseAuthHeader(null, $content); if ($message->get_parameter(ShindigOAuth::$OAUTH_PROBLEM) != null) { throw new ShindigOAuthProtocolException($message); } } } // Track metadata on the response $this->addResponseMetadata($content); return $content; } catch (Exception $e) { throw new GadgetException("INTERNAL SERVER ERROR: " . $e); } }
/** * Appends data from <Preload> elements to make them available to * gadgets.io. * * @param gadget */ private function appendPreloads(Gadget $gadget, GadgetContext $context) { $resp = array(); $gadgetSigner = Config::get('security_token_signer'); $gadgetSigner = new $gadgetSigner(); $token = ''; try { $token = $context->extractAndValidateToken($gadgetSigner); } catch (Exception $e) { $token = ''; // no token given, safe to ignore } $unsignedRequests = $unsignedContexts = array(); $signedRequests = array(); foreach ($gadget->getPreloads() as $preload) { try { if (($preload->getAuth() == Auth::$NONE || $token != null) && (count($preload->getViews()) == 0 || in_array($context->getView(), $preload->getViews()))) { $request = new RemoteContentRequest($preload->getHref()); $request->createRemoteContentRequestWithUri($preload->getHref()); $request->getOptions()->ownerSigned = $preload->isSignOwner(); $request->getOptions()->viewerSigned = $preload->isSignViewer(); switch (strtoupper(trim($preload->getAuth()))) { case "NONE": // Unify all unsigned requests to one single multi request $unsignedRequests[] = $request; $unsignedContexts[] = $context; break; case "SIGNED": // Unify all signed requests to one single multi request $signingFetcherFactory = new SigningFetcherFactory(Config::get("private_key_file")); $fetcher = $signingFetcherFactory->getSigningFetcher(new BasicRemoteContentFetcher(), $token); $req = $fetcher->signRequest($preload->getHref(), $request->getMethod()); $req->setNotSignedUri($preload->getHref()); $signedRequests[] = $req; break; default: @ob_end_clean(); header("HTTP/1.0 500 Internal Server Error", true); echo "<html><body><h1>" . "500 - Internal Server Error" . "</h1></body></html>"; die; } } } catch (Exception $e) { throw new Exception($e); } } if (count($unsignedRequests)) { try { $brc = new BasicRemoteContent(); $responses = $brc->multiFetch($unsignedRequests, $unsignedContexts); foreach ($responses as $response) { $resp[$response->getUrl()] = array('body' => $response->getResponseContent(), 'rc' => $response->getHttpCode()); } } catch (Exception $e) { throw new Exception($e); } } if (count($signedRequests)) { try { $fetcher = $signingFetcherFactory->getSigningFetcher(new BasicRemoteContentFetcher(), $token); $responses = $fetcher->multiFetchRequest($signedRequests); foreach ($responses as $response) { $resp[$response->getNotSignedUrl()] = array('body' => $response->getResponseContent(), 'rc' => $response->getHttpCode()); } } catch (Exception $e) { throw new Exception($e); } } $resp = count($resp) ? json_encode($resp) : "{}"; return "gadgets.io.preloaded_ = " . $resp . ";\n"; }
/** * 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; }
/** * Get honest-to-goodness user data. */ private function fetchData() { try { $msgParams = OAuthUtil::isFormEncoded($this->realRequest->getContentType()) ? OAuthUtil::urldecodeRFC3986($this->realRequest->getPostBody()) : array(); $method = $this->realRequest->getMethod(); $msgParams[self::$XOAUTH_APP_URL] = $this->authToken->getAppUrl(); // Build and sign the message. $oauthRequest = $this->newRequestMessageMethod($method, $this->realRequest->getUrl(), $msgParams); $rcr = $this->createRemoteContentRequest($this->filterOAuthParams($oauthRequest), $this->realRequest->getMethod(), $this->realRequest->getUrl(), $this->realRequest->getHeaders(), $this->realRequest->getContentType(), $this->realRequest->getPostBody(), $this->realRequest->getOptions()); //TODO is there a better way to detect an SP error? $remoteFetcherClass = Shindig_Config::get('remote_content_fetcher'); $fetcher = new $remoteFetcherClass(); $content = $fetcher->fetchRequest($rcr); $statusCode = $content->getHttpCode(); if ($statusCode >= 400 && $statusCode < 500) { $message = $this->parseAuthHeader(null, $content); if ($message->get_parameter(OAuth::$OAUTH_PROBLEM) != null) { throw new OAuthProtocolException($message); } } // Track metadata on the response $this->addResponseMetadata($content); return $content; } catch (Exception $e) { throw new GadgetException("INTERNAL SERVER ERROR: " . $e); } }
/** * Renders a 'proxied content' view, for reference see: * http://opensocial-resources.googlecode.com/svn/spec/draft/OpenSocial-Data-Pipelining.xml * * @param Gadget $gadget * @param array $view */ public function renderGadget(Shindig_Gadget $gadget, $view) { $this->setGadget($gadget); if (Shindig_Config::get('P3P') != '') { header("P3P: " . Shindig_Config::get('P3P')); } /* TODO * We should really re-add OAuth fetching support some day, uses these view atributes: * $view['oauthServiceName'], $view['oauthTokenName'], $view['oauthRequestToken'], $view['oauthRequestTokenSecret']; */ $authz = $this->getAuthz($view); $refreshInterval = $this->getRefreshInterval($view); $href = $this->buildHref($view, $authz); if (count($view['dataPipelining'])) { $request = new RemoteContentRequest($href, "Content-type: application/json\n"); $request->setMethod('POST'); $request->getOptions()->ignoreCache = $gadget->gadgetContext->getIgnoreCache(); } else { // no data-pipelining set, use GET and set cache/refresh interval options $request = new RemoteContentRequest($href); $request->setMethod('GET'); $request->setRefreshInterval($refreshInterval); $request->getOptions()->ignoreCache = $gadget->gadgetContext->getIgnoreCache(); } $signingFetcherFactory = $gadgetSigner = false; if ($authz != 'none') { $gadgetSigner = Shindig_Config::get('security_token_signer'); $gadgetSigner = new $gadgetSigner(); $token = $gadget->gadgetContext->extractAndValidateToken($gadgetSigner); $request->setToken($token); $request->setAuthType($authz); $request->getOptions()->ownerSigned = $this->getSignOwner($view); $request->getOptions()->viewerSigned = $this->getSignViewer($view); $signingFetcherFactory = new SigningFetcherFactory(Shindig_Config::get("private_key_file")); } $remoteFetcherClass = Shindig_Config::get('remote_content_fetcher'); $remoteFetcher = new $remoteFetcherClass(); $basicRemoteContent = new BasicRemoteContent($remoteFetcher, $signingFetcherFactory, $gadgetSigner); // Cache POST's as if they were GET's, since we don't want to re-fetch and repost the social data for each view $basicRemoteContent->setCachePostRequest(true); if (($response = $basicRemoteContent->getCachedRequest($request)) == false) { // Don't fetch the data-pipelining social data unless we don't have a cached version of the gadget's content $dataPipeliningResults = DataPipelining::fetch($view['dataPipelining'], $this->context); // spec stats that the proxied content data-pipelinging data is *not* available to templates (to avoid duplicate posting // of the data to the gadget dev's server and once to js space), so we don't assign it to the data context, and just // post the json encoded results to the remote url. $request->setPostBody(json_encode($dataPipeliningResults)); $response = $basicRemoteContent->fetch($request); } if ($response->getHttpCode() != '200') { // an error occured fetching the proxied content's gadget content $content = '<html><body><h1>An error occured fetching the gadget content</h1><p>http error code: ' . $response->getHttpCode() . '</p><p>' . $response->getResponseContent() . '</body></html>'; } else { // fetched ok, build the response document and output it $content = $response->getResponseContent(); $content = $this->parseTemplates($content); $content = $this->rewriteContent($content); $content = $this->addTemplates($content); } echo $content; }
/** * Returns the cached request, or false if there's no cached copy of this request, ignoreCache = true or if it's invalidated * * @param RemoteContentRequest $request * @return unknown */ public function getCachedRequest(RemoteContentRequest $request) { $ignoreCache = $request->getOptions()->ignoreCache; if (!$ignoreCache && ($this->cachePostRequest || !$request->isPost()) && ($cachedRequest = $this->cache->get($request->toHash())) !== false && $this->invalidateService->isValid($cachedRequest)) { return $cachedRequest; } else { return false; } }
private function signRequest(RemoteContentRequest $request) { $url = $request->getUrl(); $method = $request->getMethod(); try { // Parse the request into parameters for OAuth signing, stripping out // any OAuth or OpenSocial parameters injected by the client $parsedUri = parse_url($url); $resource = $url; $contentType = $request->getHeader('Content-Type'); $signBody = stripos($contentType, 'application/x-www-form-urlencoded') !== false || $contentType == null; $msgParams = array(); $postParams = array(); if ($request->getPostBody()) { if ($signBody) { // on normal application/x-www-form-urlencoded type post's encode and parse the post vars parse_str($request->getPostBody(), $postParams); $postParams = $this->sanitize($postParams); } else { // on any other content-type of post (application/{json,xml,xml+atom}) use the body signing hash // see http://oauth.googlecode.com/svn/spec/ext/body_hash/1.0/drafts/4/spec.html for details $msgParams['oauth_body_hash'] = base64_encode(sha1($request->getPostBody(), true)); } } if ($signBody && isset($postParams)) { $msgParams = array_merge($msgParams, $postParams); } $this->addOpenSocialParams($msgParams, $request->getToken(), $request->getOptions()->ownerSigned, $request->getOptions()->viewerSigned); $this->addOAuthParams($msgParams, $request->getToken()); $consumer = new OAuthConsumer(NULL, NULL, NULL); $signatureMethod = new ShindigRsaSha1SignatureMethod($this->privateKeyObject, null); $req_req = OAuthRequest::from_consumer_and_token($consumer, NULL, $method, $resource, $msgParams); $req_req->sign_request($signatureMethod, $consumer, NULL); // Rebuild the query string, including all of the parameters we added. // We have to be careful not to copy POST parameters into the query. // If post and query parameters share a name, they end up being removed // from the query. $forPost = array(); $postData = false; if ($method == 'POST' && $signBody) { foreach ($postParams as $key => $param) { $forPost[$key] = $param; if ($postData === false) { $postData = array(); } $postData[] = OAuthUtil::urlencode_rfc3986($key) . "=" . OAuthUtil::urlencode_rfc3986($param); } if ($postData !== false) { $postData = implode("&", $postData); } } $newQueryParts = array(); foreach ($req_req->get_parameters() as $key => $param) { if (!isset($forPost[$key])) { if (!is_array($param)) { $newQueryParts[] = urlencode($key) . '=' . urlencode($param); } else { foreach ($param as $elem) { $newQueryParts[] = urlencode($key) . '=' . urlencode($elem); } } } $newQuery = implode('&', $newQueryParts); } // Careful here; the OAuth form encoding scheme is slightly different than // the normal form encoding scheme, so we have to use the OAuth library // formEncode method. $url = $parsedUri['scheme'] . '://' . $parsedUri['host'] . (isset($parsedUri['port']) ? ':' . $parsedUri['port'] : '') . (isset($parsedUri['path']) ? $parsedUri['path'] : '') . '?' . $newQuery; $request->setUri($url); if ($signBody) { $request->setPostBody($postData); } } catch (Exception $e) { throw new GadgetException($e); } }