/var/www/vhosts/ihelp.ro/httpdocs/vendor/cakephp/cakephp/src/Routing/Middleware
Edit: /var/www/vhosts/ihelp.ro/httpdocs/vendor/cakephp/cakephp/src/Routing/Middleware/AssetMiddleware.php (5505B)
$options The options to use
*/
public function __construct(array $options = [])
{
if (!empty($options['cacheTime'])) {
$this->cacheTime = $options['cacheTime'];
}
}
/**
* Serve assets if the path matches one.
*
* @param \Psr\Http\Message\ServerRequestInterface $request The request.
* @param \Psr\Http\Server\RequestHandlerInterface $handler The request handler.
* @return \Psr\Http\Message\ResponseInterface A response.
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$url = $request->getUri()->getPath();
if (strpos($url, '..') !== false || strpos($url, '.') === false) {
return $handler->handle($request);
}
if (strpos($url, '/.') !== false) {
return $handler->handle($request);
}
$assetFile = $this->_getAssetFile($url);
if ($assetFile === null || !is_file($assetFile)) {
return $handler->handle($request);
}
$file = new SplFileInfo($assetFile);
$modifiedTime = $file->getMTime();
if ($this->isNotModified($request, $file)) {
return (new Response())
->withStringBody('')
->withStatus(304)
->withHeader(
'Last-Modified',
date(DATE_RFC850, $modifiedTime)
);
}
return $this->deliverAsset($request, $file);
}
/**
* Check the not modified header.
*
* @param \Psr\Http\Message\ServerRequestInterface $request The request to check.
* @param \SplFileInfo $file The file object to compare.
* @return bool
*/
protected function isNotModified(ServerRequestInterface $request, SplFileInfo $file): bool
{
$modifiedSince = $request->getHeaderLine('If-Modified-Since');
if (!$modifiedSince) {
return false;
}
return strtotime($modifiedSince) === $file->getMTime();
}
/**
* Builds asset file path based off url
*
* @param string $url Asset URL
* @return string|null Absolute path for asset file, null on failure
*/
protected function _getAssetFile(string $url): ?string
{
$parts = explode('/', ltrim($url, '/'));
$pluginPart = [];
for ($i = 0; $i < 2; $i++) {
if (!isset($parts[$i])) {
break;
}
$pluginPart[] = Inflector::camelize($parts[$i]);
$plugin = implode('/', $pluginPart);
if (Plugin::isLoaded($plugin)) {
$parts = array_slice($parts, $i + 1);
$fileFragment = implode(DIRECTORY_SEPARATOR, $parts);
$pluginWebroot = Plugin::path($plugin) . 'webroot' . DIRECTORY_SEPARATOR;
return $pluginWebroot . $fileFragment;
}
}
return null;
}
/**
* Sends an asset file to the client
*
* @param \Psr\Http\Message\ServerRequestInterface $request The request object to use.
* @param \SplFileInfo $file The file wrapper for the file.
* @return \Cake\Http\Response The response with the file & headers.
*/
protected function deliverAsset(ServerRequestInterface $request, SplFileInfo $file): Response
{
$stream = new Stream(fopen($file->getPathname(), 'rb'));
$response = new Response(['stream' => $stream]);
$contentType = (array)($response->getMimeType($file->getExtension()) ?: 'application/octet-stream');
$modified = $file->getMTime();
$expire = strtotime($this->cacheTime);
$maxAge = $expire - time();
return $response
->withHeader('Content-Type', $contentType[0])
->withHeader('Cache-Control', 'public,max-age=' . $maxAge)
->withHeader('Date', gmdate(DATE_RFC7231, time()))
->withHeader('Last-Modified', gmdate(DATE_RFC7231, $modified))
->withHeader('Expires', gmdate(DATE_RFC7231, $expire));
}
}