├── .github └── FUNDING.yml ├── .styleci.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json ├── resources └── lang │ └── en │ └── messages.php └── src ├── Exceptions └── ZipException.php ├── Rules └── ZipContent.php ├── Validator.php └── ZipValidatorServiceProvider.php /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: [orkhanahmadov] 2 | -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: psr12 2 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-zip-validator` will be documented in this file 4 | 5 | ## 1.6.0 - 2020-03-04 6 | 7 | - Added support for Laravel 7 8 | - Dropped support for Laravel 5.8 9 | 10 | ## 1.5.0 - 2020-02-03 11 | 12 | - Validator logic extracted to `Orkhanahmadov\ZipValidator\Validator` class. 13 | 14 | ## 1.4.0 - 2020-01-14 15 | 16 | - "Allow empty" rule introduced as second constructor argument. By default set to `true` 17 | 18 | ## 1.3.0 - 2020-01-13 19 | 20 | - "OR" file validation added. 21 | You can pass multiple files with `|`, 22 | validator will succeed if any of the files exist in ZIP file. Example: `document.pdf|homework.doc|my-doc.pdf` 23 | 24 | ## 1.2.2 - 2020-01-12 25 | 26 | - ZIP content collection simplified with `statIndex()` usage 27 | 28 | ## 1.2.1 - 2020-01-12 29 | 30 | - PHP ZIP helper methods replaced with ZipArchive class methods 31 | 32 | ## 1.2.0 - 2020-01-10 33 | 34 | - Ability to check file size in ZIP 35 | 36 | ## 1.1.0 - 2020-01-10 37 | 38 | - Namespace changes 39 | 40 | ## 1.0.0 - 2020-01-10 41 | 42 | - Initial release 43 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 48 | 49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option. 50 | 51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 52 | 53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 54 | 55 | **Happy coding**! 56 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Orkhan Ahmadov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel ZIP file validator 2 | 3 | [![Buy us a tree](https://img.shields.io/badge/Treeware-%F0%9F%8C%B3-lightgreen)](https://plant.treeware.earth/orkhanahmadov/laravel-zip-validator) 4 | [![Latest Stable Version](https://poser.pugx.org/orkhanahmadov/laravel-zip-validator/v/stable)](https://packagist.org/packages/orkhanahmadov/laravel-zip-validator) 5 | [![Latest Unstable Version](https://poser.pugx.org/orkhanahmadov/laravel-zip-validator/v/unstable)](https://packagist.org/packages/orkhanahmadov/laravel-zip-validator) 6 | [![Total Downloads](https://img.shields.io/packagist/dt/orkhanahmadov/laravel-zip-validator)](https://packagist.org/packages/orkhanahmadov/laravel-zip-validator) 7 | [![GitHub license](https://img.shields.io/github/license/orkhanahmadov/laravel-zip-validator.svg)](https://github.com/orkhanahmadov/laravel-zip-validator/blob/master/LICENSE.md) 8 | 9 | [![Build Status](https://travis-ci.org/orkhanahmadov/laravel-zip-validator.svg?branch=master)](https://travis-ci.org/orkhanahmadov/laravel-zip-validator) 10 | [![Test Coverage](https://api.codeclimate.com/v1/badges/588a51182465fa590e49/test_coverage)](https://codeclimate.com/github/orkhanahmadov/laravel-zip-validator/test_coverage) 11 | [![Maintainability](https://api.codeclimate.com/v1/badges/588a51182465fa590e49/maintainability)](https://codeclimate.com/github/orkhanahmadov/laravel-zip-validator/maintainability) 12 | [![Quality Score](https://img.shields.io/scrutinizer/g/orkhanahmadov/laravel-zip-validator.svg)](https://scrutinizer-ci.com/g/orkhanahmadov/laravel-zip-validator) 13 | [![StyleCI](https://github.styleci.io/repos/232924943/shield?branch=master)](https://github.styleci.io/repos/232924943) 14 | 15 | Custom Laravel validation rule for checking ZIP file content. 16 | 17 | ## Requirements 18 | 19 | - Laravel **9** or higher 20 | - PHP **8.0** or higher with `zip` extension enabled 21 | 22 | ## Installation 23 | 24 | You can install the package via composer: 25 | 26 | ```bash 27 | composer require orkhanahmadov/laravel-zip-validator 28 | ``` 29 | 30 | ## Usage 31 | 32 | Use `ZipContent` rule with list of required files. 33 | 34 | ``` php 35 | use Orkhanahmadov\ZipValidator\Rules\ZipContent; 36 | 37 | public function rules() 38 | { 39 | return [ 40 | 'file' => [ 41 | 'required', 42 | 'file', 43 | 'mimes:zip', 44 | new ZipContent('thumb.jpg', 'assets/logo.png'), 45 | ], 46 | ]; 47 | } 48 | ``` 49 | 50 | Pass list of required files/folders to the constructor of the validator. 51 | 52 | You can pass files as different constructor arguments or as array. 53 | If files are nested inside folders, pass relative path to file. 54 | 55 | Validator will fail if any of the passed files does not exist in ZIP archive. 56 | 57 | ### Validating maximum file size 58 | 59 | Validator also allows checking maximum size of each file inside ZIP archive. 60 | 61 | Simply pass file name as array key and maximum size as value: 62 | ``` php 63 | new ZipContent(['thumb.jpg' => 100000]); 64 | ``` 65 | 66 | Validator in above example will look for `thumb.jpg` file with maximum size of 100000 bytes (100KB). 67 | 68 | You can also mix multiple files with name-only or name+size validation: 69 | ``` php 70 | new ZipContent(['thumb.jpg' => 100000, 'logo.png']); 71 | ``` 72 | 73 | ### Multiple files with "OR" validation 74 | 75 | You can also pass multiple files with `|` symbol, if any of them exist in ZIP file validator will succeed. 76 | ``` php 77 | new ZipContent('thumb.jpg|thumb.png|thumb.svg'); 78 | ``` 79 | 80 | Validator in above example will look if `thumb.jpg` or `thumb.png` or `thumb.svg` file exists in ZIP. 81 | 82 | Of course, you can also validate file size with "OR" validation: 83 | ``` php 84 | new ZipContent(['thumb.jpg|thumb.png' => 100000]); 85 | ``` 86 | 87 | Above example will look if `thumb.jpg` or `thumb.png` file exists in ZIP and its file size is not bigger than 100000 bytes (100KB). 88 | 89 | **Important** to keep in mind that when using "OR" validation with additional file size validation, 90 | validator will compare file size with the first matching element in ZIP archive. 91 | 92 | ### Rejecting empty files 93 | 94 | By default, validator only checks if file with given name exists, 95 | it will return true even if file with matching name is empty (has size of 0 bytes). 96 | 97 | You can pass array of files as first argument and 98 | `false` as second argument to constructor if you want validator to reject files with 0 bytes. 99 | ``` php 100 | new ZipContent(['thumb.jpg', 'style.css'], false); 101 | ``` 102 | 103 | ### Wildcard filename matching 104 | 105 | Additionally, you can use wildcard filename matching with `*` symbol. 106 | ```php 107 | new ZipContent('*.jpg'); 108 | ``` 109 | 110 | This will return true if Zip archive contains any file with `.jpg` extension. 111 | 112 | Filename matching is using [fnmatch](https://www.php.net/manual/en/function.fnmatch.php) function. You can use any of its [patterns](https://www.php.net/manual/en/function.fnmatch.php#refsect1-function.fnmatch-parameters). 113 | 114 | ### Testing 115 | 116 | ``` bash 117 | composer test 118 | ``` 119 | 120 | ### Changelog 121 | 122 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. 123 | 124 | ## Contributing 125 | 126 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 127 | 128 | ### Security 129 | 130 | If you discover any security related issues, please email ahmadov90@gmail.com instead of using the issue tracker. 131 | 132 | ## Credits 133 | 134 | - [Orkhan Ahmadov](https://github.com/orkhanahmadov) 135 | - [All Contributors](../../contributors) 136 | 137 | ## License 138 | 139 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 140 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "orkhanahmadov/laravel-zip-validator", 3 | "description": "Laravel ZIP file content validator", 4 | "keywords": [ 5 | "orkhanahmadov", 6 | "laravel", 7 | "validation", 8 | "zip", 9 | "zip validation" 10 | ], 11 | "homepage": "https://github.com/orkhanahmadov/laravel-zip-validator", 12 | "license": "MIT", 13 | "type": "library", 14 | "authors": [ 15 | { 16 | "name": "Orkhan Ahmadov", 17 | "email": "ahmadov90@gmail.com", 18 | "role": "Developer" 19 | } 20 | ], 21 | "require": { 22 | "php": "^8.0", 23 | "ext-zip": "*", 24 | "illuminate/contracts": "^9.0|^10.0", 25 | "illuminate/support": "^9.0|^10.0" 26 | }, 27 | "require-dev": { 28 | "orchestra/testbench": "^7.0|^8.0", 29 | "phpunit/phpunit": "^9.0|^10.0" 30 | }, 31 | "autoload": { 32 | "psr-4": { 33 | "Orkhanahmadov\\ZipValidator\\": "src" 34 | } 35 | }, 36 | "autoload-dev": { 37 | "psr-4": { 38 | "Orkhanahmadov\\ZipValidator\\Tests\\": "tests" 39 | } 40 | }, 41 | "scripts": { 42 | "test": "vendor/bin/phpunit", 43 | "test-coverage": "vendor/bin/phpunit --coverage-html coverage" 44 | }, 45 | "config": { 46 | "sort-packages": true 47 | }, 48 | "extra": { 49 | "laravel": { 50 | "providers": [ 51 | "Orkhanahmadov\\ZipValidator\\ZipValidatorServiceProvider" 52 | ] 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /resources/lang/en/messages.php: -------------------------------------------------------------------------------- 1 | 'Following files in ZIP archive do not meet requirements: :files', 5 | ]; 6 | -------------------------------------------------------------------------------- /src/Exceptions/ZipException.php: -------------------------------------------------------------------------------- 1 | validator = new Validator( 28 | is_bool($allowEmpty) ? $files : func_get_args(), 29 | $allowEmpty 30 | ); 31 | } 32 | 33 | /** 34 | * Determine if the validation rule passes. 35 | * 36 | * @param string $attribute 37 | * @param \Illuminate\Http\UploadedFile $zipFile 38 | * @return bool 39 | */ 40 | public function passes($attribute, $zipFile): bool 41 | { 42 | $this->failedFiles = $this->validator->validate($zipFile->path()); 43 | 44 | return $this->failedFiles->count() === 0; 45 | } 46 | 47 | /** 48 | * Get the validation error message. 49 | * 50 | * @return string 51 | */ 52 | public function message(): string 53 | { 54 | return __('zipValidator::messages.failed', [ 55 | 'files' => $this->failedFiles->implode(', '), 56 | ]); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/Validator.php: -------------------------------------------------------------------------------- 1 | files = Collection::make($files); 29 | $this->allowEmpty = $allowEmpty; 30 | } 31 | 32 | /** 33 | * Static function instantiate Validator class with given rules 34 | * 35 | * @param array|string $files 36 | * 37 | * @param bool $allowEmpty 38 | * 39 | * @return Validator 40 | */ 41 | public static function rules($files, bool $allowEmpty = true): Validator 42 | { 43 | return new static($files, $allowEmpty); 44 | } 45 | 46 | /** 47 | * Validates ZIP content with given file path. 48 | * 49 | * @param string $filePath 50 | * 51 | * @return Collection 52 | */ 53 | public function validate(string $filePath): Collection 54 | { 55 | $zipContent = $this->readContent($filePath); 56 | 57 | return $this->files 58 | ->reject(function ($value, $key) use ($zipContent) { 59 | return $this->checkFile($zipContent, $value, $key); 60 | })->map(function ($value, $key) { 61 | return is_int($key) ? $value : $key; 62 | }); 63 | } 64 | 65 | /** 66 | * @param Collection $zipContent 67 | * @param string|int $value 68 | * @param string|int $key 69 | * 70 | * @return bool 71 | */ 72 | public function checkFile(Collection $zipContent, $value, $key): bool 73 | { 74 | if (! is_int($value)) { 75 | $entityName = $this->contains($zipContent->pluck('name'), $value); 76 | 77 | if ($this->allowEmpty) { 78 | return (bool) $entityName; 79 | } 80 | 81 | return $zipContent->firstWhere('name', $entityName)['size'] > 0; 82 | } 83 | 84 | $entityName = $this->contains($zipContent->pluck('name'), $key); 85 | if (! $entityName) { 86 | return false; 87 | } 88 | 89 | $entitySize = $zipContent->firstWhere('name', $entityName)['size']; 90 | 91 | if ($this->allowEmpty) { 92 | return $entitySize <= $value; 93 | } 94 | 95 | return $entitySize > 0 && $entitySize <= $value; 96 | } 97 | 98 | /** 99 | * Checks if file name exists in ZIP file. Returns matching file name, null otherwise. 100 | * 101 | * @param Collection $names 102 | * @param string $search 103 | * 104 | * @return string|null 105 | */ 106 | public function contains(Collection $names, string $search): ?string 107 | { 108 | $options = explode('|', $search); 109 | 110 | return $names->first(function ($name) use ($options) { 111 | foreach ($options as $option) { 112 | if (fnmatch($option, $name)) { 113 | return true; 114 | } 115 | } 116 | return false; 117 | }); 118 | } 119 | 120 | /** 121 | * Reads ZIP file content and returns collection with result. 122 | * 123 | * @param string $filePath 124 | * 125 | * @return Collection 126 | */ 127 | private function readContent(string $filePath): Collection 128 | { 129 | $zip = new ZipArchive(); 130 | $zipOpen = $zip->open($filePath); 131 | throw_unless($zipOpen === true, new ZipException($zipOpen)); 132 | 133 | $content = collect(); 134 | for ($i = 0; $i < $zip->count(); $i++) { 135 | $content->add($zip->statIndex($i)); 136 | } 137 | 138 | $zip->close(); 139 | 140 | return $content; 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /src/ZipValidatorServiceProvider.php: -------------------------------------------------------------------------------- 1 | loadTranslationsFrom(__DIR__ . '/../resources/lang', 'zipValidator'); 15 | 16 | if ($this->app->runningInConsole()) { 17 | $this->publishes([ 18 | __DIR__ . '/../resources/lang' => resource_path('lang/vendor/zipValidator'), 19 | ], 'lang'); 20 | } 21 | } 22 | 23 | /** 24 | * Register the application services. 25 | */ 26 | public function register() 27 | { 28 | } 29 | } 30 | --------------------------------------------------------------------------------