private function getIframeURL(Shindig_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 Shindig_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()); }
private function renderGadget(Shindig_Gadget $gadget) { $view = $gadget->getView($this->context->getView()); if ($view['type'] == 'URL') { require_once "src/gadgets/render/GadgetUrlRenderer.php"; $gadgetRenderer = new GadgetUrlRenderer($this->context); } elseif ($view['type'] == 'HTML' && empty($view['href'])) { require_once "src/gadgets/render/GadgetHtmlRenderer.php"; $gadgetRenderer = new GadgetHtmlRenderer($this->context); } elseif (empty($view['type']) || !empty($view['href'])) { require_once "src/gadgets/render/GadgetHrefRenderer.php"; $gadgetRenderer = new GadgetHrefRenderer($this->context); } else { throw new GadgetException("Invalid view type"); } $gadgetRenderer->renderGadget($gadget, $view); }
/** * 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 */ private function fetchResources(Shindig_Gadget &$gadget) { $contextLocale = $this->context->getLocale(); $unsignedRequests = $signedRequests = array(); foreach ($gadget->getLocales() 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'])) { // locale matches the current context, add it to the requests queue $request = new RemoteContentRequest($locale['messages']); $request->createRemoteContentRequestWithUri($locale['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 $libraryUrl) { $request = new RemoteContentRequest($libraryUrl); $request->createRemoteContentRequestWithUri($libraryUrl); $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(Shindig_Config::get("private_key_file")); $remoteFetcherClass = Shindig_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; } } }
/** * Appends the javascript features configuration string * * @param Gadget $gadget * @param unknown_type $hasForcedLibs * @return string */ private function appendJsConfig(Shindig_Gadget $gadget, $hasForcedLibs) { $container = $this->context->getContainer(); $containerConfig = $this->context->getContainerConfig(); //TODO some day we should parse the forcedLibs too, and include their config selectivly as well for now we just include everything if forced libs is set. if ($hasForcedLibs) { $gadgetConfig = $containerConfig->getConfig($container, 'gadgets.features'); } else { $gadgetConfig = array(); $featureConfig = $containerConfig->getConfig($container, 'gadgets.features'); foreach ($gadget->getJsLibraries() as $library) { $feature = $library->getFeatureName(); if (!isset($gadgetConfig[$feature]) && !empty($featureConfig[$feature])) { $gadgetConfig[$feature] = $featureConfig[$feature]; } } } // Add gadgets.util support. This is calculated dynamically based on request inputs. // See java/org/apache/shindig/gadgets/render/RenderingContentRewriter.java for reference. $requires = array(); foreach ($gadget->features as $feature) { $requires[$feature] = new EmptyClass(); } $gadgetConfig['core.util'] = $requires; if (isset($gadgetConfig['osml'])) { unset($gadgetConfig['osml']); } if (!isset($gadgetConfig['osapi.services']) || count($gadgetConfig['osapi.services']) == 1) { // this should really be set in config/container.js, but if not, we build a complete default set so at least most of it works out-of-the-box $gadgetConfig['osapi.services'] = array('gadgets.rpc' => array('container.listMethods'), 'http://%host%/social/rpc' => array("messages.update", "albums.update", "activities.delete", "activities.update", "activities.supportedFields", "albums.get", "activities.get", "mediaitems.update", "messages.get", "appdata.get", "system.listMethods", "people.supportedFields", "messages.create", "mediaitems.delete", "mediaitems.create", "people.get", "people.create", "albums.delete", "messages.delete", "appdata.update", "activities.create", "mediaitems.get", "albums.create", "appdata.delete", "people.update", "appdata.create"), 'http://%host%/gadgets/api/rpc' => array('cache.invalidate')); } return "gadgets.config.init(" . json_encode($gadgetConfig) . ");\n"; }