-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathDirectory.php
77 lines (63 loc) · 2.21 KB
/
Directory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Upward\Resolver;
use Laminas\Http\Header\ContentType;
use Laminas\Http\Response;
use Laminas\Http\Response\Stream;
use Magento\Upward\Definition;
use Mimey\MimeTypes;
class Directory extends AbstractResolver
{
/**
* {@inheritdoc}
*/
public function getIndicator(): string
{
return 'directory';
}
/**
* {@inheritdoc}
*/
public function isValid(Definition $definition): bool
{
if (!$definition->has($this->getIndicator())) {
return false;
}
$directory = $this->getIterator()->get('directory', $definition);
$upwardRoot = $definition->getBasepath();
$root = realpath($upwardRoot . \DIRECTORY_SEPARATOR . $directory);
if (!$root || !is_dir($root)) {
return false;
}
return true;
}
/**
* {@inheritdoc}
*/
public function resolve($definition)
{
if (!$definition instanceof Definition) {
throw new \InvalidArgumentException('$definition must be an instance of ' . Definition::class);
}
$directory = $this->getIterator()->get('directory', $definition);
$response = new Stream();
$upwardRoot = $this->getIterator()->getRootDefinition()->getBasepath();
$root = realpath($upwardRoot . \DIRECTORY_SEPARATOR . $directory);
$filename = $this->getIterator()->get('request.url.pathname');
$path = realpath($root . $filename);
if (!$path || strpos($path, $root) !== 0 || strpos($path, $upwardRoot) !== 0 || !is_file($path)) {
$response->setStatusCode(Response::STATUS_CODE_404);
} else {
$mimeType = (new MimeTypes())->getMimeType(pathinfo($path, \PATHINFO_EXTENSION));
$response->setStream(fopen($path, 'r'));
$response->getHeaders()->addHeader(new ContentType($mimeType));
// Enforce best practice and make sure static assets are cacheable
$response->getHeaders()->addHeaderLine('Cache-Control', 'max-age=31557600');
}
return $response;
}
}