From 68762df9031e353fe33e9b19d3c52bd9c71d2644 Mon Sep 17 00:00:00 2001 From: Shaharia Azam Date: Sat, 21 Dec 2019 03:35:56 +0600 Subject: [PATCH 1/6] adrift URL updated Previous adrift URL was obsolete. It has been updated and fixed --- test/File/_files/magic.mime | 44 ++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/test/File/_files/magic.mime b/test/File/_files/magic.mime index f1d242d72..40b1370e4 100644 --- a/test/File/_files/magic.mime +++ b/test/File/_files/magic.mime @@ -167,7 +167,7 @@ # depending on version magic continues with 0x93453E6139FA (V 4.0) # 0x9445376139FA (V 3.90) # 0x9445366139FA (V 3.80) -# this is from source (http://www.adrift.org.uk/) and I have some taf +# this is from source (http://www.adrift.co/) and I have some taf # files, and checked them. #0 belong 0x3C423FC9 #>4 belong 0x6A87C2CF Adrift game file @@ -1055,7 +1055,7 @@ >0 byte x GameCube movie, >0x34 ubeshort x %d x >0x36 ubeshort x %d, ->0x26 ubeshort x %dµs, +>0x26 ubeshort x %dµs, >0x42 ubeshort 0 no audio >0x42 ubeshort >0 %dHz audio @@ -1612,7 +1612,7 @@ # AMGC 0 string \xad6" AMGC archive data # NuLIB -0 string NõFélå NuLIB archive data +0 string NõFélÃ¥ NuLIB archive data # PakLeo 0 string LEOLZW PAKLeo archive data # ChArc @@ -1624,7 +1624,7 @@ # Freeze 0 string \x1f\x9f\x4a\x10\x0a Freeze archive data # KBoom -0 string ¨MP¨ KBoom archive data +0 string ¨MP¨ KBoom archive data # NSQ, must go after CDC Codec 0 string \x76\xff NSQ archive data # DPA @@ -1662,7 +1662,7 @@ # MP3 (archiver, not lossy audio compression) 0 string MP3\x1a MP3-Archiver archive data # ZET -0 string OZÝ ZET archive data +0 string OZÝ ZET archive data # TSComp 0 string \x65\x5d\x13\x8c\x08\x01\x03\x00 TSComp archive data # ARQ @@ -1693,7 +1693,7 @@ # Xtreme 0 string ULEB\0 Xtreme archive data # Pack Magic -0 string @â\1\0 Pack Magic archive data +0 string @â\1\0 Pack Magic archive data # BTS 0 belong&0xfeffffff 0x1a034465 BTS archive data # ELI 5750 @@ -1829,7 +1829,7 @@ # XPack Data 0 string xpa XPack archive data # XPack Single Data -0 string Í\ jm XPack single archive data +0 string Í\ jm XPack single archive data # TODO: missing due to unknown magic/magic at end of file: #DWC @@ -2814,7 +2814,7 @@ >>12 ulelong x \b, sample rate %d # adlib sound files -# From Gürkan Sengün , http://www.linuks.mine.nu +# From Gürkan Sengün , http://www.linuks.mine.nu 0 string RAWADATA RdosPlay RAW 1068 string RoR AMUSIC Adlib Tracker @@ -3972,7 +3972,7 @@ >113 string x (%s) #------------------------------------------------------------------------------ -# Microsoft Xbox executables .xbe (Esa Hyytiä ) +# Microsoft Xbox executables .xbe (Esa Hyytiä ) 0 string XBEH XBE, Microsoft Xbox executable # probabilistic checks whether signed or not >0x0004 ulelong =0x0 @@ -4008,7 +4008,7 @@ # From: Serge van den Boom 0 string \x01ZZZZZ\x01 3DO "Opera" file system -# From Gürkan Sengün , www.linuks.mine.nu +# From Gürkan Sengün , www.linuks.mine.nu 0 string GBS Nintendo Gameboy Music/Audio Data 12 string GameBoy\ Music\ Module Nintendo Gameboy Music Module @@ -6468,8 +6468,8 @@ # 10 SS, 8 SPT # 11 DS, 8 SPT # -# 11111001 Double density 3 floppy disk, high density 5 -# 11110000 High density 3 floppy disk +# 11111001 Double density 3½ floppy disk, high density 5¼ +# 11110000 High density 3½ floppy disk # 11111000 Hard disk any format # @@ -6887,7 +6887,7 @@ 0 string OTTO OpenType font data !:mime application/vnd.ms-opentype -# Gürkan Sengün , www.linuks.mine.nu +# Gürkan Sengün , www.linuks.mine.nu 0 string SplineFontDB: Spline Font Database !:mime application/vnd.font-fontforge-sfd >14 string x version %s @@ -8641,7 +8641,7 @@ >5 byte 0x00 (white background) >5 byte 0xFF (black background) -# Gürkan Sengün , www.linuks.mine.nu +# Gürkan Sengün , www.linuks.mine.nu # http://www.atarimax.com/jindroush.atari.org/afmtatr.html 0 leshort 0x0296 Atari ATR image @@ -9039,7 +9039,7 @@ # rom: file(1) magic for BIOS ROM Extensions found in intel machines # mapped into memory between 0xC0000 and 0xFFFFF -# From Gürkan Sengün , www.linuks.mine.nu +# From Gürkan Sengün , www.linuks.mine.nu 0 beshort 0x55AA BIOS (ia32) ROM Ext. >5 string USB USB >7 string LDR UNDI image @@ -9504,7 +9504,7 @@ # # Linux kernel boot images, from Albert Cahalan # and others such as Axel Kohlmeyer -# and Nicols Lichtmaier +# and Nicolás Lichtmaier # All known start with: b8 c0 07 8e d8 b8 00 90 8e c0 b9 00 01 29 f6 29 # Linux kernel boot images (i386 arch) (Wolfram Kleff) 514 string HdrS Linux kernel @@ -9527,10 +9527,10 @@ >0x1e3 string Loading version 1.3.79 or older >0x1e9 string Loading from prehistoric times -# System.map files - Nicols Lichtmaier +# System.map files - Nicolás Lichtmaier 8 search/1 \ A\ _text Linux kernel symbol map text -# LSM entries - Nicols Lichtmaier +# LSM entries - Nicolás Lichtmaier 0 search/1 Begin3 Linux Software Map entry text 0 search/1 Begin4 Linux Software Map entry text (new format) @@ -11718,7 +11718,7 @@ # natinst: file(1) magic for National Instruments Code Files # -# From Enrique Gmez-Flores +# From Enrique Gámez-Flores # version 1 # Many formats still missing, we use, for the moment LabVIEW # We guess VXI format file. VISA, LabWindowsCVI, BridgeVIEW, etc, are missing @@ -12933,7 +12933,7 @@ >4 belong =2 \b, version 2 # Type: Git index file -# From: Frédéric Brière +# From: Frédéric Brière 0 string DIRC Git index >4 belong >0 \b, version %d >>8 belong >0 \b, %d entries @@ -12962,7 +12962,7 @@ # # http://www.seanet.com/users/matts/riffmci/riffmci.htm # -# AVI section extended by Patrik Rdman +# AVI section extended by Patrik Rådman # 0 string RIFF RIFF (little-endian) data # RIFF Palette format @@ -15809,7 +15809,7 @@ 512 string R\0o\0o\0t\0 Hangul (Korean) Word Processor File 2000 !:mime application/x-hwp -# CosmicBook, from Benot Rouits +# CosmicBook, from Benoît Rouits 0 string CSBK Ted Neslson's CosmicBook hypertext file 2 string EYWR AmigaWriter file From 294e523fc76d6efdede0bc2cbb7f7601bc31ed54 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Fri, 27 Dec 2019 17:53:37 -0600 Subject: [PATCH 2/6] docs: adds CHANGELOG entry for #279 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 03e21b0a3..65cb888e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ All notable changes to this project will be documented in this file, in reverse ### Changed -- Nothing. +- [#279](https://github.com/zendframework/zend-validator/pull/279) updates the `magic.mime` file used for file validations. ### Deprecated From cf217e00fd47e575e9a28a811d159d5402747e19 Mon Sep 17 00:00:00 2001 From: Erik Dannenberg Date: Tue, 9 Apr 2019 18:53:10 +0200 Subject: [PATCH 3/6] fix: Make File\Extension usable for non-existing files Currently the validator fails if the given filename is not readable. This prevents valid use cases like checking if a user configurable filename has an allowed file extension before creating said file. As the validator only operates on the given string there is no need for the current readable check. Also fixes File\ExcludeExtension which suffers from the same issue. --- src/File/ExcludeExtension.php | 6 ------ src/File/Extension.php | 6 ------ 2 files changed, 12 deletions(-) diff --git a/src/File/ExcludeExtension.php b/src/File/ExcludeExtension.php index 9f8d3e034..7dd89aaab 100644 --- a/src/File/ExcludeExtension.php +++ b/src/File/ExcludeExtension.php @@ -47,12 +47,6 @@ public function isValid($value, $file = null) $this->setValue($fileInfo['filename']); - // Is file readable ? - if (empty($fileInfo['file']) || false === is_readable($fileInfo['file'])) { - $this->error(self::NOT_FOUND); - return false; - } - $extension = substr($fileInfo['filename'], strrpos($fileInfo['filename'], '.') + 1); $extensions = $this->getExtension(); diff --git a/src/File/Extension.php b/src/File/Extension.php index 0e1cd9ed0..63767619f 100644 --- a/src/File/Extension.php +++ b/src/File/Extension.php @@ -184,12 +184,6 @@ public function isValid($value, $file = null) $this->setValue($fileInfo['filename']); - // Is file readable ? - if (empty($fileInfo['file']) || false === is_readable($fileInfo['file'])) { - $this->error(self::NOT_FOUND); - return false; - } - $extension = substr($fileInfo['filename'], strrpos($fileInfo['filename'], '.') + 1); $extensions = $this->getExtension(); From f27a0c39727066784075000ca9ff967b3f54896b Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Fri, 27 Dec 2019 18:46:11 -0600 Subject: [PATCH 4/6] feat: adds allowNonExistentFile option to Extension and ExcludeExtension validators This builds on the original patch. However, to avoid a BC break, this patch adds a new option to each of the Extension and ExcludeExtension file validators, `allowNonExistentFile`. By default, this flag is `false`, preserving the existing behavior where a file must exist in order to be validated. When `true`, the validator does not automatically return `false` for non-existent files, and will continue to test its extension. --- src/File/ExcludeExtension.php | 12 ++++++++++++ src/File/Extension.php | 31 ++++++++++++++++++++++++++++++ test/File/ExcludeExtensionTest.php | 1 + test/File/ExtensionTest.php | 3 +++ 4 files changed, 47 insertions(+) diff --git a/src/File/ExcludeExtension.php b/src/File/ExcludeExtension.php index 7dd89aaab..60a88f7df 100644 --- a/src/File/ExcludeExtension.php +++ b/src/File/ExcludeExtension.php @@ -45,6 +45,16 @@ public function isValid($value, $file = null) { $fileInfo = $this->getFileInfo($value, $file); + // Is file readable ? + if (! $this->getAllowNonExistentFile() + && (empty($fileInfo['file']) || false === is_readable($fileInfo['file'])) + ) { + if (preg_match('/nofile\.mo$/', $fileInfo['file'])) { + } + $this->error(self::NOT_FOUND); + return false; + } + $this->setValue($fileInfo['filename']); $extension = substr($fileInfo['filename'], strrpos($fileInfo['filename'], '.') + 1); @@ -55,6 +65,8 @@ public function isValid($value, $file = null) } elseif (! $this->getCase()) { foreach ($extensions as $ext) { if (strtolower($ext) == strtolower($extension)) { + if (preg_match('/nofile\.mo$/', $fileInfo['file'])) { + } $this->error(self::FALSE_EXTENSION); return false; } diff --git a/src/File/Extension.php b/src/File/Extension.php index 63767619f..875a48bbc 100644 --- a/src/File/Extension.php +++ b/src/File/Extension.php @@ -44,6 +44,7 @@ class Extension extends AbstractValidator protected $options = [ 'case' => false, // Validate case sensitive 'extension' => '', // List of extensions + 'allowNonExistentFile' => false, // Allow validation even if file does not exist ]; /** @@ -170,6 +171,28 @@ public function addExtension($extension) return $this; } + /** + * Returns whether or not to allow validation of non-existent files. + * + * @return bool + */ + public function getAllowNonExistentFile() + { + return $this->options['allowNonExistentFile']; + } + + /** + * Sets the flag indicating whether or not to allow validation of non-existent files. + * + * @param bool $flag Whether or not to allow validation of non-existent files. + * @return self Provides a fluent interface + */ + public function setAllowNonExistentFile($flag) + { + $this->options['allowNonExistentFile'] = (bool) $flag; + return $this; + } + /** * Returns true if and only if the file extension of $value is included in the * set extension list @@ -182,6 +205,14 @@ public function isValid($value, $file = null) { $fileInfo = $this->getFileInfo($value, $file); + // Is file readable ? + if (! $this->getAllowNonExistentFile() + && (empty($fileInfo['file']) || false === is_readable($fileInfo['file'])) + ) { + $this->error(self::NOT_FOUND); + return false; + } + $this->setValue($fileInfo['filename']); $extension = substr($fileInfo['filename'], strrpos($fileInfo['filename'], '.') + 1); diff --git a/test/File/ExcludeExtensionTest.php b/test/File/ExcludeExtensionTest.php index 5ac2a2243..b7eeb344e 100644 --- a/test/File/ExcludeExtensionTest.php +++ b/test/File/ExcludeExtensionTest.php @@ -38,6 +38,7 @@ public function basicBehaviorDataProvider() $noFileTests = [ // Options, isValid Param, Expected value, message ['mo', $testFile, false, 'fileExcludeExtensionNotFound'], + [['extension' => 'gif', 'allowNonExistentFile' => true], $testFile, true, ''], ]; // Dupe data in File Upload format diff --git a/test/File/ExtensionTest.php b/test/File/ExtensionTest.php index 3b6231f19..01cb69e38 100644 --- a/test/File/ExtensionTest.php +++ b/test/File/ExtensionTest.php @@ -38,6 +38,7 @@ public function basicBehaviorDataProvider() $noFileTests = [ // Options, isValid Param, Expected value, message ['mo', $testFile, false, 'fileExtensionNotFound'], + [['extension' => 'mo', 'allowNonExistentFile' => true], $testFile, true, ''], ]; // Dupe data in File Upload format @@ -49,6 +50,8 @@ public function basicBehaviorDataProvider() ]; $testData[] = [$data[0], $fileUpload, $data[2], $data[3]]; } + + return $testData; } From 5f339ffff2557f17cc8bfe5bf12bcd09739de8c4 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Fri, 27 Dec 2019 18:51:51 -0600 Subject: [PATCH 5/6] docs: adds documentation for new allowNonExistentFile option --- docs/book/validators/file/extension.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/book/validators/file/extension.md b/docs/book/validators/file/extension.md index e53e21162..bcf89de1b 100644 --- a/docs/book/validators/file/extension.md +++ b/docs/book/validators/file/extension.md @@ -14,6 +14,9 @@ The following set of options are supported: against which to test. - `case`: Boolean indicating whether or not extensions should match case sensitively; defaults to `false` (case-insensitive). +- `allowNonExistentFile`: (**Since 2.13.0**) Boolean indicating whether or not + to allow validating a filename for a non-existent file. Defaults to `false` + (will not validate non-existent files). ## Usage Examples From cc8805d9b075b03bccebf33c9ce55075a3570c06 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Fri, 27 Dec 2019 18:53:32 -0600 Subject: [PATCH 6/6] docs: adds CHANGELOG entry for #266 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 65cb888e9..fd729331a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ All notable changes to this project will be documented in this file, in reverse ### Added -- Nothing. +- [#266](https://github.com/zendframework/zend-validator/pull/266) adds a new option to the `File\Extension` and `File\ExcludeExtension` validators, `allowNonExistentFile`. When set to `true`, the validators will continue validating the extension of the filename given even if the file does not exist. The default is `false`, to preserve backwards compatibility with previous versions. ### Changed