|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByJG\RestServer\Middleware; |
|
4
|
|
|
|
|
5
|
|
|
use ByJG\RestServer\Exception\Error415Exception; |
|
6
|
|
|
use ByJG\RestServer\Exception\Error500Exception; |
|
7
|
|
|
use ByJG\RestServer\HttpRequest; |
|
8
|
|
|
use ByJG\RestServer\HttpResponse; |
|
9
|
|
|
use ByJG\RestServer\OutputProcessor\HtmlOutputProcessor; |
|
10
|
|
|
use ByJG\RestServer\ResponseBag; |
|
11
|
|
|
use FastRoute\Dispatcher; |
|
12
|
|
|
|
|
13
|
|
|
class ServerStaticMiddleware implements BeforeMiddlewareInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Undocumented function |
|
17
|
|
|
* |
|
18
|
|
|
* @param mixed $dispatcherStatus |
|
19
|
|
|
* @param HttpResponse $response |
|
20
|
|
|
* @param HttpRequest $request |
|
21
|
|
|
* @return MiddlewareResult |
|
22
|
|
|
*/ |
|
23
|
2 |
|
public function beforeProcess( |
|
24
|
|
|
$dispatcherStatus, |
|
25
|
|
|
HttpResponse $response, |
|
26
|
|
|
HttpRequest $request |
|
27
|
|
|
) |
|
28
|
|
|
{ |
|
29
|
2 |
|
if ($dispatcherStatus != Dispatcher::NOT_FOUND) { |
|
30
|
|
|
return MiddlewareResult::continue(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
2 |
|
$file = $_SERVER['SCRIPT_FILENAME']; |
|
34
|
2 |
|
if (!empty($file) && strpos($file, $_SERVER["SCRIPT_NAME"]) !== false && file_exists($file)) { |
|
35
|
2 |
|
$mime = $this->mimeContentType($file); |
|
36
|
|
|
|
|
37
|
1 |
|
if (empty($mime)) { |
|
38
|
|
|
return MiddlewareResult::continue(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
$response->addHeader("Content-Type", $mime); |
|
42
|
1 |
|
$response->emptyResponse(); |
|
43
|
1 |
|
$response->getResponseBag()->setSerializationRule(ResponseBag::RAW); |
|
44
|
1 |
|
$response->write(file_get_contents($file)); |
|
45
|
1 |
|
return MiddlewareResult::stopProcessingOthers(HtmlOutputProcessor::class); |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return MiddlewareResult::continue(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get the Mime Type based on the filename |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $filename |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
6 |
|
public function mimeContentType($filename) |
|
58
|
|
|
{ |
|
59
|
6 |
|
if (!file_exists($filename)) { |
|
60
|
1 |
|
return null; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$prohibitedTypes = [ |
|
64
|
|
|
"php", |
|
65
|
|
|
"vb", |
|
66
|
|
|
"cs", |
|
67
|
|
|
"rb", |
|
68
|
|
|
"py", |
|
69
|
|
|
"py3", |
|
70
|
|
|
"lua" |
|
71
|
|
|
]; |
|
72
|
|
|
|
|
73
|
5 |
|
$ext = substr(strrchr($filename, "."), 1); |
|
74
|
5 |
|
if (in_array($ext, $prohibitedTypes)) { |
|
75
|
1 |
|
throw new Error415Exception("File type not supported"); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
4 |
|
if (!function_exists('finfo_open')) { |
|
79
|
|
|
throw new Error500Exception("ServerStaticMiddleware requires finfo extension"); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
4 |
|
$finfo = finfo_open(FILEINFO_MIME); |
|
83
|
4 |
|
$mimetype = finfo_file($finfo, $filename); |
|
84
|
4 |
|
finfo_close($finfo); |
|
85
|
4 |
|
return $mimetype; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.