@@ -26,7 +26,7 @@ |
||
| 26 | 26 | * Check if the temporary file has a valid MIME type for it's extension. |
| 27 | 27 | * |
| 28 | 28 | * @uses finfo php extension |
| 29 | - * @return boolean|null |
|
| 29 | + * @return boolean |
|
| 30 | 30 | */ |
| 31 | 31 | public function isValidMime() { |
| 32 | 32 | $extension = strtolower(pathinfo($this->tmpFile['name'], PATHINFO_EXTENSION)); |
@@ -29,10 +29,10 @@ |
||
| 29 | 29 | * @return boolean|null |
| 30 | 30 | */ |
| 31 | 31 | public function isValidMime() { |
| 32 | - $extension = strtolower(pathinfo($this->tmpFile['name'], PATHINFO_EXTENSION)); |
|
| 32 | + $extension = strtolower(pathinfo($this->tmpFile['name'], PATHINFO_EXTENSION)); |
|
| 33 | 33 | |
| 34 | - // we can't check filenames without an extension or no temp file path, let them pass validation. |
|
| 35 | - if(!$extension || !$this->tmpFile['tmp_name']) return true; |
|
| 34 | + // we can't check filenames without an extension or no temp file path, let them pass validation. |
|
| 35 | + if(!$extension || !$this->tmpFile['tmp_name']) return true; |
|
| 36 | 36 | |
| 37 | 37 | $expectedMimes = $this->getExpectedMimeTypes($this->tmpFile); |
| 38 | 38 | if(empty($expectedMimes)) { |
@@ -32,10 +32,10 @@ discard block |
||
| 32 | 32 | $extension = strtolower(pathinfo($this->tmpFile['name'], PATHINFO_EXTENSION)); |
| 33 | 33 | |
| 34 | 34 | // we can't check filenames without an extension or no temp file path, let them pass validation. |
| 35 | - if(!$extension || !$this->tmpFile['tmp_name']) return true; |
|
| 35 | + if (!$extension || !$this->tmpFile['tmp_name']) return true; |
|
| 36 | 36 | |
| 37 | 37 | $expectedMimes = $this->getExpectedMimeTypes($this->tmpFile); |
| 38 | - if(empty($expectedMimes)) { |
|
| 38 | + if (empty($expectedMimes)) { |
|
| 39 | 39 | throw new MimeUploadValidator_Exception( |
| 40 | 40 | sprintf('Could not find a MIME type for extension %s', $extension) |
| 41 | 41 | ); |
@@ -43,14 +43,14 @@ discard block |
||
| 43 | 43 | |
| 44 | 44 | $finfo = new finfo(FILEINFO_MIME_TYPE); |
| 45 | 45 | $foundMime = $finfo->file($this->tmpFile['tmp_name']); |
| 46 | - if(!$foundMime) { |
|
| 46 | + if (!$foundMime) { |
|
| 47 | 47 | throw new MimeUploadValidator_Exception( |
| 48 | 48 | sprintf('Could not find a MIME type for file %s', $this->tmpFile['tmp_name']) |
| 49 | 49 | ); |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | - foreach($expectedMimes as $expected) { |
|
| 53 | - if($this->compareMime($foundMime, $expected)) return true; |
|
| 52 | + foreach ($expectedMimes as $expected) { |
|
| 53 | + if ($this->compareMime($foundMime, $expected)) return true; |
|
| 54 | 54 | } |
| 55 | 55 | return false; |
| 56 | 56 | } |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | $extension = strtolower(pathinfo($tmpFile['name'], PATHINFO_EXTENSION)); |
| 65 | 65 | |
| 66 | 66 | // if the finfo php extension isn't loaded, we can't complete this check. |
| 67 | - if(!class_exists('finfo')) { |
|
| 67 | + if (!class_exists('finfo')) { |
|
| 68 | 68 | throw new MimeUploadValidator_Exception('PHP extension finfo is not loaded'); |
| 69 | 69 | } |
| 70 | 70 | |
@@ -73,14 +73,14 @@ discard block |
||
| 73 | 73 | |
| 74 | 74 | // Get the mime types set in framework core |
| 75 | 75 | $knownMimes = Config::inst()->get('HTTP', 'MimeTypes'); |
| 76 | - if(isset($knownMimes[$extension])) { |
|
| 76 | + if (isset($knownMimes[$extension])) { |
|
| 77 | 77 | $expectedMimes[] = $knownMimes[$extension]; |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | // Get the mime types and their variations from mimevalidator |
| 81 | 81 | $knownMimes = Config::inst()->get(get_class($this), 'MimeTypes'); |
| 82 | - if(isset($knownMimes[$extension])) { |
|
| 83 | - if(is_array($knownMimes[$extension])) { |
|
| 82 | + if (isset($knownMimes[$extension])) { |
|
| 83 | + if (is_array($knownMimes[$extension])) { |
|
| 84 | 84 | $expectedMimes += $knownMimes[$extension]; |
| 85 | 85 | } else { |
| 86 | 86 | $expectedMimes[] = $knownMimes[$extension]; |
@@ -109,18 +109,18 @@ discard block |
||
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | public function validate() { |
| 112 | - if(parent::validate() === false) return false; |
|
| 112 | + if (parent::validate() === false) return false; |
|
| 113 | 113 | |
| 114 | 114 | try { |
| 115 | 115 | $result = $this->isValidMime(); |
| 116 | - if($result === false) { |
|
| 116 | + if ($result === false) { |
|
| 117 | 117 | $this->errors[] = _t( |
| 118 | 118 | 'File.INVALIDMIME', |
| 119 | 119 | 'File extension does not match known MIME type' |
| 120 | 120 | ); |
| 121 | 121 | return false; |
| 122 | 122 | } |
| 123 | - } catch(MimeUploadValidator_Exception $e) { |
|
| 123 | + } catch (MimeUploadValidator_Exception $e) { |
|
| 124 | 124 | $this->errors[] = _t( |
| 125 | 125 | 'File.FAILEDMIMECHECK', |
| 126 | 126 | 'MIME validation failed: {message}', |
@@ -32,7 +32,9 @@ discard block |
||
| 32 | 32 | $extension = strtolower(pathinfo($this->tmpFile['name'], PATHINFO_EXTENSION)); |
| 33 | 33 | |
| 34 | 34 | // we can't check filenames without an extension or no temp file path, let them pass validation. |
| 35 | - if(!$extension || !$this->tmpFile['tmp_name']) return true; |
|
| 35 | + if(!$extension || !$this->tmpFile['tmp_name']) { |
|
| 36 | + return true; |
|
| 37 | + } |
|
| 36 | 38 | |
| 37 | 39 | $expectedMimes = $this->getExpectedMimeTypes($this->tmpFile); |
| 38 | 40 | if(empty($expectedMimes)) { |
@@ -50,7 +52,9 @@ discard block |
||
| 50 | 52 | } |
| 51 | 53 | |
| 52 | 54 | foreach($expectedMimes as $expected) { |
| 53 | - if($this->compareMime($foundMime, $expected)) return true; |
|
| 55 | + if($this->compareMime($foundMime, $expected)) { |
|
| 56 | + return true; |
|
| 57 | + } |
|
| 54 | 58 | } |
| 55 | 59 | return false; |
| 56 | 60 | } |
@@ -109,7 +113,9 @@ discard block |
||
| 109 | 113 | } |
| 110 | 114 | |
| 111 | 115 | public function validate() { |
| 112 | - if(parent::validate() === false) return false; |
|
| 116 | + if(parent::validate() === false) { |
|
| 117 | + return false; |
|
| 118 | + } |
|
| 113 | 119 | |
| 114 | 120 | try { |
| 115 | 121 | $result = $this->isValidMime(); |
@@ -6,7 +6,7 @@ discard block |
||
| 6 | 6 | $tmpFileName = 'UploadTest-testUpload.jpg'; |
| 7 | 7 | $tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName; |
| 8 | 8 | $tmpFileContent = ''; |
| 9 | - for($i=0; $i<10000; $i++) $tmpFileContent .= '0'; |
|
| 9 | + for ($i = 0; $i < 10000; $i++) $tmpFileContent .= '0'; |
|
| 10 | 10 | file_put_contents($tmpFilePath, $tmpFileContent); |
| 11 | 11 | |
| 12 | 12 | // emulates the $_FILES array |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | $tmpFileName = 'text.TXT'; |
| 35 | 35 | $tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName; |
| 36 | 36 | $tmpFileContent = ''; |
| 37 | - for($i=0; $i<10000; $i++) $tmpFileContent .= '0'; |
|
| 37 | + for ($i = 0; $i < 10000; $i++) $tmpFileContent .= '0'; |
|
| 38 | 38 | file_put_contents($tmpFilePath, $tmpFileContent); |
| 39 | 39 | |
| 40 | 40 | $validator = new MimeUploadValidator(); |
@@ -6,7 +6,9 @@ discard block |
||
| 6 | 6 | $tmpFileName = 'UploadTest-testUpload.jpg'; |
| 7 | 7 | $tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName; |
| 8 | 8 | $tmpFileContent = ''; |
| 9 | - for($i=0; $i<10000; $i++) $tmpFileContent .= '0'; |
|
| 9 | + for($i=0; $i<10000; $i++) { |
|
| 10 | + $tmpFileContent .= '0'; |
|
| 11 | + } |
|
| 10 | 12 | file_put_contents($tmpFilePath, $tmpFileContent); |
| 11 | 13 | |
| 12 | 14 | // emulates the $_FILES array |
@@ -34,7 +36,9 @@ discard block |
||
| 34 | 36 | $tmpFileName = 'text.TXT'; |
| 35 | 37 | $tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName; |
| 36 | 38 | $tmpFileContent = ''; |
| 37 | - for($i=0; $i<10000; $i++) $tmpFileContent .= '0'; |
|
| 39 | + for($i=0; $i<10000; $i++) { |
|
| 40 | + $tmpFileContent .= '0'; |
|
| 41 | + } |
|
| 38 | 42 | file_put_contents($tmpFilePath, $tmpFileContent); |
| 39 | 43 | |
| 40 | 44 | $validator = new MimeUploadValidator(); |