1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Framework\Http\Controllers\Website; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Framework\Assets\AssetManager; |
8
|
|
|
use AbterPhp\Framework\Http\Controllers\ControllerAbstract; |
9
|
|
|
use AbterPhp\Framework\Session\FlashService; |
10
|
|
|
use Dflydev\ApacheMimeTypes\FlatRepository; |
11
|
|
|
use Opulence\Http\Responses\Response; |
12
|
|
|
use Opulence\Http\Responses\ResponseHeaders; |
13
|
|
|
|
14
|
|
|
class Assets extends ControllerAbstract |
15
|
|
|
{ |
16
|
|
|
const HEADER_CONTENT_TYPE = 'Content-Type'; |
17
|
|
|
|
18
|
|
|
/** @var AssetManager */ |
19
|
|
|
protected $assetManager; |
20
|
|
|
|
21
|
|
|
/** @var FlatRepository */ |
22
|
|
|
protected $memeRepository; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Assets constructor. |
26
|
|
|
* |
27
|
|
|
* @param FlashService $flashService |
28
|
|
|
* @param AssetManager $cacheManager |
29
|
|
|
* @param FlatRepository $memeRepository |
30
|
|
|
*/ |
31
|
|
|
public function __construct(FlashService $flashService, AssetManager $assetManager, FlatRepository $memeRepository) |
32
|
|
|
{ |
33
|
|
|
parent::__construct($flashService); |
34
|
|
|
|
35
|
|
|
$this->assetManager = $assetManager; |
36
|
|
|
$this->memeRepository = $memeRepository; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* 404 page |
41
|
|
|
* |
42
|
|
|
* @param string $path |
43
|
|
|
* |
44
|
|
|
* @return Response The response |
45
|
|
|
*/ |
46
|
|
|
public function asset(string $path): Response |
47
|
|
|
{ |
48
|
|
|
$content = $this->assetManager->renderRaw($path); |
49
|
|
|
|
50
|
|
|
if (null === $content) { |
51
|
|
|
return $this->createNotFound(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$ext = substr($path, strrpos($path, '.') + 1); |
55
|
|
|
if (!$this->isExtAllowed($ext)) { |
56
|
|
|
return $this->createNotAllowed(); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$contentType = $this->getContentType($ext); |
60
|
|
|
|
61
|
|
|
$response = new Response(); |
62
|
|
|
$response->setContent($content); |
63
|
|
|
$response->getHeaders()->add(static::HEADER_CONTENT_TYPE, $contentType); |
64
|
|
|
|
65
|
|
|
return $response; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $ext |
70
|
|
|
* |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
protected function isExtAllowed(string $ext): bool |
74
|
|
|
{ |
75
|
|
|
switch ($ext) { |
76
|
|
|
case 'php': |
77
|
|
|
case 'php7': |
78
|
|
|
case 'gitignore': |
79
|
|
|
case 'phtml': |
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return true; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $ext |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
protected function getContentType(string $ext): string |
92
|
|
|
{ |
93
|
|
|
$type = $this->memeRepository->findType($ext); |
94
|
|
|
if ($type) { |
95
|
|
|
return $type; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return 'text/plain'; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return Response |
103
|
|
|
* @throws \Throwable |
104
|
|
|
*/ |
105
|
|
|
protected function createNotFound(): Response |
106
|
|
|
{ |
107
|
|
|
$this->view = $this->viewFactory->createView('contents/frontend/404'); |
108
|
|
|
|
109
|
|
|
$response = $this->createResponse('404 Not Found'); |
110
|
|
|
$response->setStatusCode(ResponseHeaders::HTTP_NOT_FOUND); |
111
|
|
|
|
112
|
|
|
return $response; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return Response |
117
|
|
|
* @throws \Throwable |
118
|
|
|
*/ |
119
|
|
|
protected function createForbidden(): Response |
120
|
|
|
{ |
121
|
|
|
$this->view = $this->viewFactory->createView('contents/frontend/403'); |
122
|
|
|
|
123
|
|
|
$response = $this->createResponse('403 Forbidden'); |
124
|
|
|
$response->setStatusCode(ResponseHeaders::HTTP_FORBIDDEN); |
125
|
|
|
|
126
|
|
|
return $response; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.