public function dispatch(ServerRequestInterface $request, ResponseInterface $response) : ResponseInterface { $lang = LanguageNegotiator::getLanguage($request); $query = $this->db->prepare('SELECT n.id, n.title, n.parent_id from nodes n INNER JOIN node_attributes na ON na.node_id = n.id INNER JOIN attribute_values av on na.`attribute_value_id` = av.`id` where av.`attribute_value` = :language;'); $query->bindParam(':language', $lang); $query->execute(); $data = $query->fetchAll(\PDO::FETCH_ASSOC); $neg = new FormatNegotiator(); $contentType = $neg->getFormat($request); switch ($contentType) { case 'xml': $xml = new \SimpleXMLElement('<root/>'); array_walk_recursive($this->treeBuilder->build($data), array($xml, 'addChild')); $response->getBody()->write($xml->asXML()); break; default: $response->getBody()->write(json_encode($this->treeBuilder->build($data))); break; } return $response->withoutHeader('Content-Type'); // Remove the Content-Type Header }
/** * Execute the middleware. * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { if (!Middleware::hasAttribute($request, FormatNegotiator::KEY)) { throw new RuntimeException('This middleware needs FormatNegotiator executed before'); } $ajax = Utils\Helpers::isAjax($request); $debugBar = $this->debugBar ?: new StandardDebugBar(); //Redirection response if (Utils\Helpers::isRedirect($response)) { if ($debugBar->isDataPersisted() || session_status() === PHP_SESSION_ACTIVE) { $debugBar->stackData(); } //Html response } elseif (FormatNegotiator::getFormat($request) === 'html') { $renderer = $debugBar->getJavascriptRenderer(); ob_start(); echo '<style>'; $renderer->dumpCssAssets(); echo '</style>'; echo '<script>'; $renderer->dumpJsAssets(); echo '</script>'; echo $renderer->render(!$ajax); $response = $this->inject($response, ob_get_clean()); //Ajax response } elseif ($ajax && $this->captureAjax) { $headers = $debugBar->getDataAsHeaders(); foreach ($headers as $name => $value) { $response = $response->withHeader($name, $value); } } return $next($request, $response); }
/** * Execute the middleware. * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { if (!Middleware::hasAttribute($request, FormatNegotiator::KEY)) { throw new RuntimeException('Csrf middleware needs FormatNegotiator executed before'); } if (!Middleware::hasAttribute($request, ClientIp::KEY)) { throw new RuntimeException('Csrf middleware needs ClientIp executed before'); } if ($this->storage === null) { if (session_status() !== PHP_SESSION_ACTIVE) { throw new RuntimeException('Csrf middleware needs an active php session or a storage defined'); } if (!isset($_SESSION[$this->sessionIndex])) { $_SESSION[$this->sessionIndex] = []; } $this->storage =& $_SESSION[$this->sessionIndex]; } if (FormatNegotiator::getFormat($request) !== 'html') { return $next($request, $response); } if (Utils\Helpers::isPost($request) && !$this->validateRequest($request)) { return $response->withStatus(403); } $response = $next($request, $response); return $this->insertIntoPostForms($response, function ($match) use($request) { preg_match('/action=["\']?([^"\'\\s]+)["\']?/i', $match[0], $matches); $action = empty($matches[1]) ? $request->getUri()->getPath() : $matches[1]; return $match[0] . $this->generateTokens($request, $action); }); }
/** * Execute the middleware. * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { if (!Middleware::hasAttribute($request, FormatNegotiator::KEY)) { throw new RuntimeException('The GoogleAnalytics middleware needs FormatNegotiator executed before'); } if (FormatNegotiator::getFormat($request) === 'html' && !Utils\Helpers::isAjax($request)) { $response = $this->inject($response, $this->getCode()); } return $next($request, $response); }
/** * Execute the middleware. * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { if (!Middleware::hasAttribute($request, FormatNegotiator::KEY)) { throw new RuntimeException('Minify middleware needs FormatNegotiator executed before'); } $resolver = $this->resolver ?: new Transformers\Minifier(); $transformer = $resolver->resolve(FormatNegotiator::getFormat($request)); if ($transformer) { $response = $transformer($response); } return $next($request, $response); }
/** * Execute the middleware. * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { if (!Middleware::hasAttribute($request, FormatNegotiator::KEY)) { throw new RuntimeException('FormTimestamp middleware needs FormatNegotiator executed before'); } if (FormatNegotiator::getFormat($request) !== 'html') { return $next($request, $response); } if ($this->isPost($request) && !$this->isValid($request)) { return $response->withStatus(403); } $response = $next($request, $response); return $this->insertIntoPostForms($response, '<input type="hidden" name="' . $this->inputName . '" value="' . $this->encrypt(time()) . '">'); }
/** * Execute the middleware. * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { if (!Middleware::hasAttribute($request, FormatNegotiator::KEY)) { throw new RuntimeException('Honeypot middleware needs FormatNegotiator executed before'); } if (FormatNegotiator::getFormat($request) !== 'html') { return $next($request, $response); } if (Utils\Helpers::isPost($request) && !$this->isValid($request)) { return $response->withStatus(403); } $response = $next($request, $response); return $this->insertIntoPostForms($response, function ($match) { return $match[0] . '<input type="text" name="' . $this->inputName . '" class="' . $this->inputClass . '">'; }); }
/** * Execute the middleware. * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { if (!Middleware::hasAttribute($request, FormatNegotiator::KEY)) { throw new RuntimeException('ResponsiveImage middleware needs FormatNegotiator executed before'); } //If it's not an image or basePath does not match or invalid transform values, don't do anything if (!in_array(FormatNegotiator::getFormat($request), ['jpg', 'jpeg', 'gif', 'png']) || !$this->testBasePath($request->getUri()->getPath()) || !($info = $this->parsePath($request->getUri()->getPath()))) { return $next($request, $response); } list($path, $transform) = $info; $uri = $request->getUri()->withPath($path); $request = $request->withUri($uri); $response = $next($request, $response); //Check the response and transform the image if ($transform && $response->getStatusCode() === 200 && $response->getBody()->getSize()) { return $this->transform($response, $transform); } return $response; }
/** * Execute the middleware. * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { if ($this->forCache && !static::isCacheable($request, $response)) { return $next($request, $response); } if (!Middleware::hasAttribute($request, FormatNegotiator::KEY)) { throw new RuntimeException('Minify middleware needs FormatNegotiator executed before'); } switch (FormatNegotiator::getFormat($request)) { case 'css': return $next($request, $this->minifyCss($response)); case 'js': return $next($request, $this->minifyJs($response)); case 'html': return $next($request, $this->minifyHtml($response)); default: return $next($request, $response); } }
/** * Execute the middleware. * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { if (!Middleware::hasAttribute($request, FormatNegotiator::KEY)) { throw new RuntimeException('This middleware needs FormatNegotiator executed before'); } $renderer = $this->debugBar->getJavascriptRenderer(); //Is an asset? $path = $request->getUri()->getPath(); $renderPath = $renderer->getBaseUrl(); if (strpos($path, $renderPath) === 0) { $file = $renderer->getBasePath() . substr($path, strlen($renderPath)); if (file_exists($file)) { $body = Middleware::createStream(); $body->write(file_get_contents($file)); return $response->withBody($body); } } $response = $next($request, $response); //Fix the render baseUrl $renderPath = Utils\Helpers::joinPath(BasePath::getBasePath($request), $renderer->getBaseUrl()); $renderer->setBaseUrl($renderPath); $ajax = Utils\Helpers::isAjax($request); //Redirection response if (Utils\Helpers::isRedirect($response)) { if ($this->debugBar->isDataPersisted() || session_status() === PHP_SESSION_ACTIVE) { $this->debugBar->stackData(); } //Html response } elseif (FormatNegotiator::getFormat($request) === 'html') { if (!$ajax) { $response = $this->inject($response, $renderer->renderHead(), 'head'); } $response = $this->inject($response, $renderer->render(!$ajax), 'body'); //Ajax response } elseif ($ajax && $this->captureAjax) { $headers = $this->debugBar->getDataAsHeaders(); foreach ($headers as $name => $value) { $response = $response->withHeader($name, $value); } } return $response; }
/** * Execute the middleware. * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { $response = $next($request, $response); if (!Middleware::hasAttribute($request, FormatNegotiator::KEY)) { throw new RuntimeException('DebugBar middleware needs FormatNegotiator executed before'); } if (FormatNegotiator::getFormat($request) === 'html') { $debugBar = $this->debugBar ?: $this->getFromContainer(Bar::CLASS); $renderer = $debugBar->getJavascriptRenderer(); ob_start(); echo '<style>'; $renderer->dumpCssAssets(); echo '</style>'; echo '<script>'; $renderer->dumpJsAssets(); echo '</script>'; echo $renderer->render(); $response->getBody()->write(ob_get_clean()); } return $response; }
/** * Execute the middleware. * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { if (!Middleware::hasAttribute($request, FormatNegotiator::KEY)) { throw new RuntimeException('ResponsiveImage middleware needs FormatNegotiator executed before'); } $format = FormatNegotiator::getFormat($request); switch ($format) { case 'jpg': case 'jpeg': case 'gif': case 'png': $key = $this->getCacheKey($request); //Get from the cache if ($cached = $this->getFromCache($key, $response)) { return $cached; } $info = $this->parsePath($request->getUri()->getPath()); if (!$info) { break; } //Removes the transform from the path list($path, $transform) = $info; $request = $request->withUri($request->getUri()->withPath($path)); $response = $next($request, $response); //Transform if ($response->getStatusCode() === 200 && $response->getBody()->getSize()) { $response = $this->transform($request, $response, $transform); //Save in the cache $this->saveIntoCache($key, $response); } return $response; } $response = $next($request, $response); if ($format === 'html' && !empty($this->clientHints)) { return $response->withHeader('Accept-CH', implode(',', $this->clientHints)); } return $response; }
/** * Returns the whoops instance or create one. * * @param ServerRequestInterface $request * * @return Run */ private function getWhoopsInstance(ServerRequestInterface $request) { if ($this->whoops) { return $this->whoops; } $whoops = new Run(); if (php_sapi_name() === 'cli') { $whoops->pushHandler(new PlainTextHandler()); return $whoops; } $format = FormatNegotiator::getFormat($request); switch ($format) { case 'json': $whoops->pushHandler(new JsonResponseHandler()); break; case 'html': $whoops->pushHandler(new PrettyPageHandler()); break; case 'xml': $whoops->pushHandler(new XmlResponseHandler()); break; case 'txt': case 'css': case 'js': $whoops->pushHandler(new PlainTextHandler()); break; default: if (empty($format)) { $whoops->pushHandler(new PrettyPageHandler()); } else { $whoops->pushHandler(new PlainTextHandler()); } break; } return $whoops; }