1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fracture\Http; |
4
|
|
|
|
5
|
|
|
class UploadedFile |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
private $rawParams = []; |
9
|
|
|
|
10
|
|
|
private $type = null; |
11
|
|
|
|
12
|
|
|
private $isValid = true; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param array $params |
17
|
|
|
*/ |
18
|
9 |
|
public function __construct($params) |
19
|
|
|
{ |
20
|
9 |
|
$this->rawParams = $params; |
21
|
9 |
|
} |
22
|
|
|
|
23
|
|
|
|
24
|
5 |
|
public function prepare() |
25
|
|
|
{ |
26
|
5 |
|
$filename = $this->getPath(); |
27
|
|
|
|
28
|
5 |
|
if ($this->rawParams['error'] !== UPLOAD_ERR_OK || $this->isDubious($filename) === true) { |
29
|
1 |
|
$this->isValid = false; |
30
|
1 |
|
return; |
31
|
|
|
} |
32
|
|
|
|
33
|
4 |
|
if (class_exists('\FInfo')) { |
34
|
4 |
|
$info = new \FInfo(FILEINFO_MIME_TYPE); |
35
|
4 |
|
$this->type = $info->file($filename); |
36
|
|
|
} |
37
|
4 |
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $filename |
42
|
|
|
*/ |
43
|
4 |
|
private function isDubious($filename) |
44
|
|
|
{ |
45
|
|
|
return |
46
|
4 |
|
file_exists($filename) === false || |
47
|
4 |
|
is_readable($filename) === false || |
48
|
4 |
|
filesize($filename) === 0 || |
49
|
4 |
|
$this->seemsTampered($filename); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Verfification for whether file was actually uploaded |
55
|
|
|
* using native PHP function |
56
|
|
|
* |
57
|
|
|
* @codeCoverageIgnore |
58
|
|
|
* @param string $filename |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
protected function seemsTampered($filename) |
62
|
|
|
{ |
63
|
|
|
return is_uploaded_file($filename) === false; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
2 |
|
public function isValid() |
71
|
|
|
{ |
72
|
2 |
|
return $this->isValid; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
3 |
|
public function hasProperExtension() |
80
|
|
|
{ |
81
|
3 |
|
return $this->rawParams['type'] === $this->type; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
1 |
|
public function getName() |
86
|
|
|
{ |
87
|
1 |
|
return $this->rawParams['name']; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
1 |
|
public function getSize() |
92
|
|
|
{ |
93
|
1 |
|
return $this->rawParams['size']; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
4 |
|
public function getMimeType() |
98
|
|
|
{ |
99
|
4 |
|
if ($this->type !== null) { |
100
|
3 |
|
return $this->type; |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
return $this->rawParams['type']; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
3 |
|
public function getExtension() |
108
|
|
|
{ |
109
|
3 |
|
$info = new \SplFileInfo($this->getName()); |
110
|
3 |
|
return $info->getExtension(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
5 |
|
public function getPath() |
115
|
|
|
{ |
116
|
5 |
|
return $this->rawParams['tmp_name']; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|