Esempio n. 1
0
 /**
  * Prepares the environment before running a test.
  */
 protected function setUp()
 {
     parent::setUp();
     $this->gadgetContext = new GadgetContext('GADGET');
     $gadgetSpecFactory = new MockRewriterGadgetFactory($this->gadgetContext, null);
     $gadgetSpecFactory->fetchGadget = null;
     $this->gadget = $gadgetSpecFactory->createGadget();
     $this->gadgetRewriter = new GadgetRewriter($this->gadgetContext);
     $this->view = $this->gadget->getView($this->gadgetContext->getView());
 }
 /**
  *
  * @param Gadget $gadget
  * @throws GadgetException
  */
 protected function renderGadget(Gadget $gadget)
 {
     $view = $gadget->getView($this->context->getView());
     $renderClasses = Config::get('gadget_renderer');
     foreach ($renderClasses as $renderClass => $constraints) {
         // if current view meets the configurated renderer constraints
         // render the gadget and stop checking
         if ($this->checkConstraints($view, $constraints)) {
             $gadgetRenderer = new $renderClass($this->context);
             $gadgetRenderer->renderGadget($gadget, $view);
             return;
         }
     }
     throw new GadgetException("Invalid view type");
 }
 private function getIframeURL(Gadget $gadget, GadgetContext $context)
 {
     $v = $gadget->getChecksum();
     $view = $gadget->getView($context->getView());
     $up = '';
     foreach ($gadget->gadgetSpec->userPrefs as $pref) {
         $up .= '&up_' . urlencode($pref['name']) . '=' . urlencode($pref['value']);
     }
     $locale = $context->getLocale();
     //Note: putting the URL last, else some browsers seem to get confused (reported by hi5)
     return Config::get('default_iframe_prefix') . 'container=' . $context->getContainer() . ($context->getIgnoreCache() ? '&nocache=1' : '&v=' . $v) . ($context->getModuleId() != 0 ? '&mid=' . $context->getModuleId() : '') . '&lang=' . $locale['lang'] . '&country=' . $locale['country'] . '&view=' . $view['view'] . $up . '&url=' . urlencode($context->getUrl());
 }
 /**
  * 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";
 }
 /**
  * Tests GadgetContext->getView()
  */
 public function testGetView()
 {
     $this->assertEquals($this->testData['view'], $this->GadgetContext->getView());
 }
 /**
  * Tests GadgetContext->setView()
  */
 public function testSetView()
 {
     $view = 'Dummie_view';
     $this->GadgetContext->setView($view);
     $this->assertEquals($view, $this->GadgetContext->getView());
 }
 /**
  * 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
  */
 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['views'] && !in_array($this->context->getView(), $locale['views'])) {
             unset($gadget->gadgetSpec->locales[$key]);
             continue;
         }
         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 {
                     $transformedUrl = $gadget->substitutions->substitute($transformedUrl);
                     $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 $id => $preload) {
             if ($preload['views'] && !in_array($this->context->getView(), $preload['views'])) {
                 unset($gadget->gadgetSpec->preloads[$id]);
                 continue;
             }
             if (!empty($preload['href'])) {
                 $preload['href'] = $gadget->substitutions->substitute($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) {
                 $gadget->gadgetSpec->templatesRequireLibraries[$key] = $libraryUrl = $gadget->substitutions->substitute($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;
             }
         }
     }
     $responses = $this->performRequests($unsignedRequests, $signedRequests);
     // 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;
         }
     }
 }