Ejemplo n.º 1
0
 /**
  * Parse the gadget views
  *
  * @param DOMDocument $doc
  * @param GadgetSpec $gadget
  */
 private function parseViews(DOMDocument &$doc, GadgetSpec &$gadget)
 {
     $views = $doc->getElementsByTagName('Content');
     if (!$views || $views->length < 1) {
         throw new GadgetSpecException("A gadget needs to have at least one view");
     }
     $gadget->views = array();
     foreach ($views as $viewNode) {
         if ($viewNode->getAttribute('type' == 'url') && $viewNode->getAttribute('href') == null) {
             throw new GadgetSpecException("Malformed <Content> href value");
         }
         foreach (explode(',', $viewNode->getAttribute('view')) as $view) {
             $view = trim($view);
             $href = trim($viewNode->getAttribute('href'));
             $type = trim(strtoupper($viewNode->getAttribute('type')));
             if (empty($type)) {
                 $type = 'html';
             }
             $dataPipeliningRequests = array();
             if (!empty($href) && $type == 'HTML') {
                 require_once 'src/gadgets/templates/DataPipelining.php';
                 // a non empty href & type == 'HTML' means there might be data-pipelining tags in the content section
                 $dataPipeliningRequests = DataPipelining::parse($viewNode);
             }
             if (isset($gadget->views[$view])) {
                 $gadget->views[$view]['content'] .= $viewNode->nodeValue;
             } else {
                 $gadget->views[$view] = array('view' => $view, 'type' => $type, 'href' => $href, 'preferedHeight' => $viewNode->getAttribute('prefered_height'), 'preferedWidth' => $viewNode->getAttribute('prefered_width'), 'quirks' => $viewNode->getAttribute('quirks'), 'content' => $viewNode->nodeValue, 'authz' => $viewNode->getAttribute('authz'), 'oauthServiceName' => $viewNode->getAttribute('oauth_service_name'), 'oauthTokenName' => $viewNode->getAttribute('oauth_token_name'), 'oauthRequestToken' => $viewNode->getAttribute('oauth_request_token'), 'oauthRequestTokenSecret' => $viewNode->getAttribute('oauth_request_token_secret'), 'signOwner' => $viewNode->getAttribute('sign_owner'), 'signViewer' => $viewNode->getAttribute('sign_viewer'), 'refreshInterval' => $viewNode->getAttribute('refresh_interval'), 'dataPipelining' => $dataPipeliningRequests);
             }
         }
     }
 }
 /**
  * Parses and performs the (combined) os-data requests
  *
  * @param string $osDataRequests
  */
 private function performDataRequests($osDataRequests)
 {
     //TODO check with the java implementation guys if they do a caching strategy here (same as with data-pipelining),
     // would result in a much higher render performance..
     libxml_use_internal_errors(true);
     $this->doc = new DOMDocument(null, 'utf-8');
     $this->doc->preserveWhiteSpace = true;
     $this->doc->formatOutput = false;
     $this->doc->strictErrorChecking = false;
     $this->doc->recover = false;
     $this->doc->resolveExternals = false;
     if ($this->doc->loadXML($osDataRequests)) {
         $dataPipeliningRequests = array();
         // walk the one or multiple script tags, and build a combined request array
         foreach ($this->doc->childNodes as $childNode) {
             if ($childNode->tagName == 'script') {
                 $dataPipeliningRequests = array_merge($dataPipeliningRequests, DataPipelining::Parse($childNode));
             }
         }
         // and perform the requests
         if (count($dataPipeliningRequests)) {
             $this->dataInserts = DataPipelining::fetch($dataPipeliningRequests, $this->context);
             $this->addContextData($this->dataInserts);
         }
     } else {
         echo "Error parsing os-data:\n" . XmlError::getErrors($osDataRequests);
     }
 }
 /**
  * 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;
 }