/** * 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 Piwik 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, ClientIp::KEY)) { throw new RuntimeException('Geolocate middleware needs ClientIp executed before'); } $ip = ClientIp::getIp($request); if ($ip !== null) { $request = Middleware::setAttribute($request, self::KEY, $this->geocoder->geocode($ip)); } 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, ClientIp::KEY)) { throw new RuntimeException('Geolocate middleware needs ClientIp executed before'); } $geocoder = $this->geocoder ?: $this->getFromContainer(Geocoder::CLASS, false) ?: $this->getGeocoder(); $ip = ClientIp::getIp($request); if ($ip) { $ip = '123.9.34.23'; $request = Middleware::setAttribute($request, self::KEY, $geocoder->geocode($ip)); } 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, EncodingNegotiator::KEY)) { throw new RuntimeException('Gzip middleware needs EncodingNegotiator executed before'); } $resolver = $this->resolver ?: new Transformers\Encoder(); $transformer = $resolver->resolve(EncodingNegotiator::getEncoding($request)); $response = $next($request, $response); if ($transformer) { $response = $transformer($response); } 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, ClientIp::KEY)) { throw new RuntimeException('AccessLog middleware needs ClientIp executed before'); } $message = $this->combined ? self::combinedFormat($request, $response) : self::commonFormat($request, $response); if ($response->getStatusCode() >= 400 && $response->getStatusCode() < 600) { $this->logger->error($message); } else { $this->logger->info($message); } 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, ClientIp::KEY)) { throw new RuntimeException('Recaptcha middleware needs ClientIp executed before'); } if (Utils\Helpers::isPost($request)) { $recaptcha = new GoogleRecaptcha($this->secret); $data = $request->getParsedBody(); $res = $recaptcha->verify(isset($data['g-recaptcha-response']) ? $data['g-recaptcha-response'] : '', ClientIp::getIp($request)); if (!$res->isSuccess()) { return $response->withStatus(403); } } 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('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) { if (!Middleware::hasAttribute($request, ClientIp::KEY)) { throw new RuntimeException('Firewall middleware needs ClientIp executed before'); } $ips = ClientIp::getIps($request) ?: []; $firewall = new IpFirewall(); if (!empty($this->trusted)) { $firewall->addList($this->trusted, 'trusted', true); } if (!empty($this->untrusted)) { $firewall->addList($this->untrusted, 'untrusted', false); } foreach ($ips as $ip) { $ok = $firewall->setIpAddress($ip)->handle(); if (!$ok) { return $response->withStatus(403); } } 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) { $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; }