Skip to content

Commit 252e55b

Browse files
committed
Added validation of file type
1 parent de7151c commit 252e55b

File tree

4 files changed

+86
-6
lines changed

4 files changed

+86
-6
lines changed

src/Flow/Basic.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,20 @@ public static function save($destination, $config, RequestInterface $request = n
2222
if (!$config instanceof ConfigInterface) {
2323
$config = new Config(array(
2424
'tempDir' => $config,
25+
'mimeAccept' => array(
26+
'image/gif',
27+
'image/jpeg',
28+
'image/png',
29+
'image/bmp'
30+
)
2531
));
2632
}
2733
$file = new File($config, $request);
28-
34+
if (!$file->checkMime($config->getMimeAccept())){
35+
header("HTTP/1.1 400 Bad Request");
36+
echo "Invalid MIME Type: ".$file->getFileType();
37+
return false;
38+
}
2939
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
3040
if ($file->checkChunk()) {
3141
header("HTTP/1.1 200 Ok");

src/Flow/Config.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,27 @@ public function __construct($config = array())
2121
$this->config = $config;
2222
}
2323

24+
/**
25+
* Set mime accept types
26+
*
27+
* @param $mime
28+
*/
29+
public function setMimeAccept($mime)
30+
{
31+
$this->config['mimeAccept'] = $mime;
32+
}
33+
34+
/**
35+
* Get mime accept types
36+
*
37+
* @return array
38+
*/
39+
public function getMimeAccept()
40+
{
41+
return $this->config['mimeAccept'];
42+
}
43+
44+
2445
/**
2546
* Set path to temporary directory for chunks storage
2647
*

src/Flow/File.php

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Flow;
44

5+
56
class File
67
{
78
/**
@@ -24,7 +25,7 @@ class File
2425
/**
2526
* Constructor
2627
*
27-
* @param ConfigInterface $config
28+
* @param ConfigInterface $config
2829
* @param RequestInterface $request
2930
*/
3031
public function __construct(ConfigInterface $config, RequestInterface $request = null)
@@ -58,7 +59,7 @@ public function getIdentifier()
5859
*/
5960
public function getChunkPath($index)
6061
{
61-
return $this->config->getTempDir().DIRECTORY_SEPARATOR.$this->identifier.'_'.$index;
62+
return $this->config->getTempDir() . DIRECTORY_SEPARATOR . $this->identifier . '_' . $index;
6263
}
6364

6465
/**
@@ -148,7 +149,7 @@ public function save($destination)
148149
{
149150
$fh = fopen($destination, 'wb');
150151
if (!$fh) {
151-
throw new FileOpenException('failed to open destination file: '.$destination);
152+
throw new FileOpenException('failed to open destination file: ' . $destination);
152153
}
153154

154155
if (!flock($fh, LOCK_EX | LOCK_NB, $blocked)) {
@@ -161,7 +162,7 @@ public function save($destination)
161162
}
162163
// @codeCoverageIgnoreEnd
163164

164-
throw new FileLockException('failed to lock file: '.$destination);
165+
throw new FileLockException('failed to lock file: ' . $destination);
165166
}
166167

167168
$totalChunks = $this->request->getTotalChunks();
@@ -174,7 +175,7 @@ public function save($destination)
174175
$chunk = fopen($file, "rb");
175176

176177
if (!$chunk) {
177-
throw new FileOpenException('failed to open chunk: '.$file);
178+
throw new FileOpenException('failed to open chunk: ' . $file);
178179
}
179180

180181
if ($preProcessChunk !== null) {
@@ -230,4 +231,28 @@ public function _move_uploaded_file($filePath, $destinationPath)
230231
{
231232
return move_uploaded_file($filePath, $destinationPath);
232233
}
234+
235+
236+
/**
237+
* Check Mime Type
238+
*/
239+
public function checkMime($acceptMimes)
240+
{
241+
$fileMime = $this->request->getFileType();
242+
243+
foreach ($acceptMimes as $acceptMime) {
244+
if ($fileMime === $acceptMime) {
245+
return true;
246+
}
247+
}
248+
return false;
249+
}
250+
251+
/**
252+
* Get Mime Type
253+
*/
254+
public function getFileType()
255+
{
256+
return $this->request->getFileType();
257+
}
233258
}

src/Flow/Request.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,30 @@ public function __construct($params = null, $file = null)
3838
$this->file = $file;
3939
}
4040

41+
42+
/**
43+
* Get parameter of file
44+
*
45+
* @param string $name
46+
*
47+
* @return string|int|null
48+
*/
49+
protected function getFileParam($name)
50+
{
51+
return isset($this->file[$name]) ? $this->file[$name] : null;
52+
}
53+
54+
/**
55+
* Get uploaded file type
56+
*
57+
* @return string|null
58+
*/
59+
public function getFileType()
60+
{
61+
return $this->getFileParam('type');
62+
}
63+
64+
4165
/**
4266
* Get parameter value
4367
*

0 commit comments

Comments
 (0)