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()); }
/** * 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; } } }
/** * Tests GadgetContext->getLocale() */ public function testGetLocale() { $this->assertNotNull($this->GadgetContext->getLocale()); }
/** * Outputs a html content type gadget. * It creates a html page, with the javascripts from the features inline into the page, plus * calls to 'gadgets.config.init' with the container configuration (config/container.js) and * 'gadgets.Prefs.setMessages_' with all the substitutions. For external javascripts it adds * a <script> tag. * * @param Gadget $gadget * @param GadgetContext $context */ private function outputHtmlGadget($gadget, $context, $view) { $content = ''; $externJs = ''; $externFmt = "<script src=\"%s\"></script>"; $forcedLibs = $context->getForcedJsLibs(); // allow the &libs=.. param to override our forced js libs configuration value if (empty($forcedLibs)) { $forcedLibs = Config::get('focedJsLibs'); } $this->setContentType("text/html; charset=UTF-8"); if ($context->getIgnoreCache()) { // no cache was requested, set non-caching-headers $this->setNoCache(true); } elseif (isset($_GET['v'])) { // version was given, cache for a long long time (a year) $this->setCacheTime(365 * 24 * 60 * 60); } else { // no version was given, cache for 5 minutes $this->setCacheTime(5 * 60); } // Was a privacy policy header configured? if so set it if (Config::get('P3P') != '') { header("P3P: " . Config::get('P3P')); } if (!$view->getQuirks()) { $content .= "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n"; } $content .= "<html lang=" . strtolower($context->getLocale()->getLanguage()) . "><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/><style type=\"text/css\">" . Config::get('gadget_css') . "</style></head><body>\n"; // Forced libs first. if (!empty($forcedLibs)) { $libs = explode(':', $forcedLibs); $content .= sprintf($externFmt, Config::get('default_js_prefix') . $this->getJsUrl($libs, $gadget) . "&container=" . $context->getContainer()) . "\n"; } $content .= "<script>\n"; if (!empty($forcedLibs)) { // if some of the feature libraries are externalized (through a browser cachable <script src="/gadgets/js/opensocial-0.7:settitle.js"> // type url), then we don't want to include dependencies twice, so find the complete features chain, so we can skip over those $forcedLibsArray = explode(':', $forcedLibs); $registry = $this->context->getRegistry(); $missing = array(); $registry->getIncludedFeatures($forcedLibsArray, $forcedLibsArray, $missing); } foreach ($gadget->getJsLibraries() as $library) { $type = $library->getType(); if ($type == 'URL') { // TODO: This case needs to be handled more gracefully by the js // servlet. We should probably inline external JS as well. $externJs .= sprintf($externFmt, $library->getContent()) . "\n"; // else check if there are no forcedLibs, or if it wasn't included in their dep chain } elseif (empty($forcedLibs) || !in_array($library->getFeatureName(), $forcedLibsArray)) { $content .= $library->getContent(); } // otherwise it was already included by config.forceJsLibs. } $content .= $this->appendJsConfig($context, $gadget, !empty($forcedLibs)) . $this->appendMessages($gadget) . $this->appendPreloads($gadget, $context) . "</script>"; if (strlen($externJs) > 0) { $content .= $externJs; } $gadgetExceptions = array(); $rewriter = new ContentRewriter(); if ($rewriter->rewriteGadgetView($gadget, $view)) { $content .= $gadget->getSubstitutions()->substitute($view->getRewrittenContent()); } else { $content .= $gadget->getSubstitutions()->substitute($view->getContent()); } if (empty($content)) { // Unknown view $gadgetExceptions[] = "View: '" . $context->getView() . "' invalid for gadget: " . $gadget->getId()->getKey(); } if (count($gadgetExceptions)) { throw new GadgetException(print_r($gadgetExceptions, true)); } $content .= "\n<script>gadgets.util.runOnLoadHandlers();</script></body>\n</html>"; echo $content; }