Exemplo n.º 1
0
 /**
  * Does the actual rewrite option scanning and performs the dom parsing
  *
  * @param string $content
  * @param Gadget $gadget
  */
 public function rewrite($content, Shindig_Gadget &$gadget, $checkDocument = false)
 {
     // Check to see if the gadget requested rewriting, or if rewriting is forced in the configuration
     if (is_array($gadget->gadgetSpec->rewrite) || Shindig_Config::get('rewrite_by_default')) {
         require_once "src/gadgets/rewrite/ContentRewriter.php";
         $contentRewriter = new ContentRewriter($this->context, $gadget);
         $contentRewriter->register($this);
     }
     // Are we configured to sanitize certain views? (if so the config should be an array of view names to sanitize, iaw: array('profile', 'home'))
     if (is_array(Shindig_Config::get('sanitize_views'))) {
         require_once "src/gadgets/rewrite/SanitizeRewriter.php";
         $sanitizeRewriter = new SanitizeRewriter($this->context, $gadget);
         $sanitizeRewriter->register($this);
     }
     // no observers registered, return the original content, otherwise parse the DOM tree and call the observers
     if (!count($this->domObservers)) {
         return $content;
     } else {
         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->loadHtml($content)) {
             //TODO parse and output libxml_get_errors();
             libxml_clear_errors();
             // parsing failed, return the unmodified content
             return $content;
         }
         if ($checkDocument) {
             $this->checkDocument();
         }
         // find and parse all nodes in the dom document
         $rootNodes = $this->doc->getElementsByTagName('*');
         $this->parseNodes($rootNodes);
         // DomDocument tries to make the document a valid html document, so added the html/body/head elements to it.. so lets strip them off before returning the content
         $html = $this->doc->saveHTML();
         // If the gadget specified the caja feature, cajole it
         if (in_array('caja', $gadget->features)) {
             //TODO : use the caja daemon to cajole the content (experimental patch is available and will be added soon)
         }
         return $html;
     }
 }
 /**
  * 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><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;
 }
Exemplo n.º 3
0
 private function rewriteContent($gadgetUrl, RemoteContentRequest &$result)
 {
     try {
         // At the moment we're only able to rewrite CSS files, so check the content type and/or the file extension before rewriting
         $headers = $result->getResponseHeaders();
         $isCss = false;
         if (isset($headers['Content-Type']) && strtolower($headers['Content-Type'] == 'text/csss')) {
             $isCss = true;
         } else {
             $ext = substr($_GET['url'], strrpos($_GET['url'], '.') + 1);
             $isCss = strtolower($ext) == 'css';
         }
         if ($isCss) {
             $gadget = $this->createGadget($gadgetUrl);
             $rewrite = $gadget->gadgetSpec->rewrite;
             if (is_array($rewrite)) {
                 $contentRewriter = new ContentRewriter($this->context, $gadget);
                 $result->setResponseContent($contentRewriter->rewriteCSS($result->getResponseContent()));
             }
         }
     } catch (Exception $e) {
         // ignore, not being able to rewrite anything isn't fatal
     }
 }