context = new $contextClass('GADGET'); $gadgetSigner = Config::get('security_token_signer'); $gadgetSigner = new $gadgetSigner(); try { $token = $this->context->extractAndValidateToken($gadgetSigner); } catch (\Exception $e) { // no token given, this is a fatal error if 'render_token_required' is set to true if (Config::get('render_token_required')) { $this->showError($e); } else { $token = ''; } } $factoryClass = Config::get('gadget_factory_class'); $gadgetSpecFactory = new $factoryClass($this->context, $token); $gadget = $gadgetSpecFactory->createGadget(); $this->setCachingHeaders(); $this->renderGadget($gadget); } catch (\Exception $e) { $this->showError($e); } } /** * * @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"); } /** * checks if the current view meets the given gadget renderer constraints * * constraint format: * * array( * attributeName => expectedValue or boolean to indicate if the attribute is * required or not * ) * * @param array $view * @param array $constraints * @return boolean */ public function checkConstraints($view, $constraints) { foreach ($constraints as $attribute => $expected) { if ($expected === false && isset($view[$attribute]) && $view[$attribute]) { return false; } else if ($expected === true && !(isset($view[$attribute]) && $view[$attribute])) { return false; } else if (! is_bool($expected) && $view[$attribute] !== $expected) { return false; } } return true; } /** * */ protected function setCachingHeaders() { $this->setContentType("text/html; charset=UTF-8"); if ($this->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); } } /** * * @param Exception $e */ protected function showError($e) { header("HTTP/1.0 400 Bad Request", true, 400); echo ""; echo "

Error

"; echo $e->getMessage(); if (Config::get('debug')) { echo "

Debug backtrace

";
      print_r(debug_backtrace());
      echo "
>"; } echo ""; die(); } }