From 462834e06199797a8a93fd5a1035a90881cb3c16 Mon Sep 17 00:00:00 2001 From: Sascha Greuel Date: Mon, 15 Oct 2018 07:30:35 +0200 Subject: [PATCH] Initial commit --- .gitignore | 4 + .gitmodules | 3 + .travis.yml | 18 + CONTRIBUTING.md | 55 + LICENSE.txt | 165 ++ README.md | 83 + composer.json | 53 + composer.lock | 1428 +++++++++++++++++ phpunit.xml.dist | 21 + src/SoftCreatR/MimeDetector/MimeDetector.php | 1090 +++++++++++++ .../MimeDetector/MimeDetectorException.php | 17 + .../MimeDetector/MimeDetectorTest.php | 323 ++++ .../MimeDetector/MimeDetectorTestUtil.php | 125 ++ tests/SoftCreatR/MimeDetector/fixtures | 1 + 14 files changed, 3386 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 .travis.yml create mode 100644 CONTRIBUTING.md create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 composer.json create mode 100644 composer.lock create mode 100644 phpunit.xml.dist create mode 100644 src/SoftCreatR/MimeDetector/MimeDetector.php create mode 100644 src/SoftCreatR/MimeDetector/MimeDetectorException.php create mode 100644 tests/SoftCreatR/MimeDetector/MimeDetectorTest.php create mode 100644 tests/SoftCreatR/MimeDetector/MimeDetectorTestUtil.php create mode 160000 tests/SoftCreatR/MimeDetector/fixtures diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..572d030 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +composer.phar +/vendor/ +.idea +phpunit.xml diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..11891d6 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "tests/SoftCreatR/MimeDetector/fixtures"] + path = tests/SoftCreatR/MimeDetector/fixtures + url = https://github.com/SoftCreatR/mime-detector-fixtures diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..2580691 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,18 @@ +language: php +php: + - 7.1 + - 7.2 +install: + - travis_retry composer self-update + - travis_retry composer install --no-interaction --no-suggest +script: + - vendor/bin/phpunit --coverage-clover=coverage.xml +after_success: + - bash <(curl -s https://codecov.io/bash) +branches: + only: + - master + - dev +cache: + directories: + - $HOME/.composer/cache diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..4da74e3 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,55 @@ +# Contributing + +Contributions are **welcome** and will be fully **credited**. + +Please read and understand the contribution guide before creating an issue or pull request. + +## Etiquette + +This project is open source, and as such, the maintainers give their free time to build and maintain the source code +held within. They make the code freely available in the hope that it will be of use to other developers. It would be +extremely unfair for them to suffer abuse or anger for their hard work. + +Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the +world that developers are civilized and selfless people. + +It's the duty of the maintainer to ensure that all submissions to the project are of sufficient +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. + +## Viability + +When requesting or submitting new features, first consider whether it might be useful to others. Open +source projects are used by many developers, who may have entirely different needs to your own. Think about +whether or not your feature is likely to be used by other users of the project. + +## Procedure + +Before filing an issue: + +- Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. +- Check to make sure your feature suggestion isn't already present within the project. +- Check the pull requests tab to ensure that the bug doesn't have a fix in progress. +- Check the pull requests tab to ensure that the feature isn't already in progress. + +Before submitting a pull request: + +- Check the codebase to ensure that your feature doesn't already exist. +- Check the pull requests to ensure that another person hasn't already submitted the feature or fix. + +## Requirements + +If the project maintainer has any additional requirements, you will find them listed here. + +- **[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](http://pear.php.net/package/PHP_CodeSniffer). + +- **Add tests!** - Your patch won't be accepted if it doesn't have tests. + +- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. + +- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. + +- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. + +- **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](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. + +**Happy coding**! diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..153d416 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,165 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..cd803fc --- /dev/null +++ b/README.md @@ -0,0 +1,83 @@ +# PHP Mime Detector [![Build Status](https://travis-ci.com/SoftCreatR/php-mime-detector.svg?branch=master)](https://travis-ci.com/SoftCreatR/php-mime-detector) + +Detecting the real type of a (binary) file doesn't have to be hard. Checking a file's extension is not reliable and can cause serious security issues. + +This package helps you to determine the correct type of a file, by reading it byte for byte (up to 4096) and check for [magic numbers](http://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files). + +However, this package isn't a replacement for any security software. It just aims to produce less false positives, than a simple extension check would produce. + +A list of supported file types can be found on [this Wiki page](https://github.com/SoftCreatR/php-mime-detector/wiki/Supported-file-types). + +## Requirements + +- PHP 7.1 or newer +- HTTP server with PHP support (eg: Apache, Nginx, Caddy) +- [Composer](https://getcomposer.org) + +## Installation + +Require this package using [Composer](https://getcomposer.org/), in the root directory of your project: + +``` bash +$ composer require softcreatr/php-mime-detector +``` + +## Usage + +Here is an example on how this package makes it very easy to determine the mime type (and it's corresponding file extension) of a given file: + +```php +use SoftCreatR\MimeDetector\MimeDetector; +use SoftCreatR\MimeDetector\MimeDetectorException; + +// create an instance of the MimeDetector +$mimeDetector = MimeDetector::getInstance(); + +// set our file to read +try { + $mimeDetector->setFile('foo.bar'); +} catch (MimeDetectorException $e) { + die('An error occured while trying to load the given file.'); +} + +// try to determine it's mime type and the correct file extension +$fileData = $mimeDetector->getFileType(); + +// print the result +echo '
' . print_r($fileData, true) . '
'; +``` + +## Testing + +Testing utilizes PHPUnit (what else?) by running this command: + +``` bash +$ composer test +``` + +However, you may check out a bunch of test files for a full test. Test files are no longer included in the composer package nor the Git repository itself, so you have to perform a checkout of this repository and install it's submodules: + +``` bash +$ git clone https://github.com/SoftCreatR/php-mime-detector +$ cd php-mime-detector +$ git submodule update --init --recursive +``` + +When done, perform a `composer install` and run PHPUnit as described above. + +## Contributing + +Please see [CONTRIBUTING](CONTRIBUTING.md) for details. + +When addding new detections, please make sure to provide at least one sample file. + +## License + +[![License: LGPL v3](https://img.shields.io/badge/License-LGPL%20v3-blue.svg)](https://www.gnu.org/licenses/lgpl-3.0) + +Free Software, Hell Yeah! + +## Support on BMC +Hey! Help us out with some cups of :coffee:! + +[![BMC](https://www.buymeacoffee.com/assets/img/guidelines/download-assets-sm-2.svg)](https://www.buymeacoff.ee/softcreatr) diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..c762b73 --- /dev/null +++ b/composer.json @@ -0,0 +1,53 @@ +{ + "name": "softcreatr/php-mime-detector", + "description": "Mime Detector Decoder", + "license": "LGPL-3.0", + "keywords": [ + "Mime", + "File Type", + "Magic Number" + ], + "homepage": "https://www.softcreatr.com", + "authors": [ + { + "name": "Sascha Greuel", + "email": "sascha@softcreatr.de" + }, + { + "name": "Sebastian Zimmer", + "email": "morik@softcreatr.de" + } + ], + "support": { + "source": "https://github.com/SoftCreatR/php-mime-detector", + "issues": "https://github.com/SoftCreatR/php-mime-detector/issues", + "forum": "https://support.softcreatr.com" + }, + "version": "1.0.0", + "require": { + "php": ">=7.1.0" + }, + "require-dev": { + "phpunit/phpunit": "~7.0" + }, + "autoload": { + "psr-0": { + "SoftCreatR\\MimeDetector\\": "src" + } + }, + "autoload-dev": { + "psr-0": { + "SoftCreatR\\Tests\\MimeDetector\\": "tests" + } + }, + "scripts": { + "test": "vendor/bin/phpunit" + }, + "config": { + "preferred-install": "dist", + "optimize-autoloader": true, + "sort-packages": true + }, + "minimum-stability": "stable", + "prefer-stable": true +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..7e1d59f --- /dev/null +++ b/composer.lock @@ -0,0 +1,1428 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "f4a8a8793429e01c07f3b7ac1eb42569", + "packages": [], + "packages-dev": [ + { + "name": "doctrine/instantiator", + "version": "1.1.0", + "source": { + "type": "git", + "url": "https://github.com/doctrine/instantiator.git", + "reference": "185b8868aa9bf7159f5f953ed5afb2d7fcdc3bda" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/instantiator/zipball/185b8868aa9bf7159f5f953ed5afb2d7fcdc3bda", + "reference": "185b8868aa9bf7159f5f953ed5afb2d7fcdc3bda", + "shasum": "" + }, + "require": { + "php": "^7.1" + }, + "require-dev": { + "athletic/athletic": "~0.1.8", + "ext-pdo": "*", + "ext-phar": "*", + "phpunit/phpunit": "^6.2.3", + "squizlabs/php_codesniffer": "^3.0.2" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.2.x-dev" + } + }, + "autoload": { + "psr-4": { + "Doctrine\\Instantiator\\": "src/Doctrine/Instantiator/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Marco Pivetta", + "email": "ocramius@gmail.com", + "homepage": "http://ocramius.github.com/" + } + ], + "description": "A small, lightweight utility to instantiate objects in PHP without invoking their constructors", + "homepage": "https://github.com/doctrine/instantiator", + "keywords": [ + "constructor", + "instantiate" + ], + "time": "2017-07-22T11:58:36+00:00" + }, + { + "name": "myclabs/deep-copy", + "version": "1.8.1", + "source": { + "type": "git", + "url": "https://github.com/myclabs/DeepCopy.git", + "reference": "3e01bdad3e18354c3dce54466b7fbe33a9f9f7f8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/3e01bdad3e18354c3dce54466b7fbe33a9f9f7f8", + "reference": "3e01bdad3e18354c3dce54466b7fbe33a9f9f7f8", + "shasum": "" + }, + "require": { + "php": "^7.1" + }, + "replace": { + "myclabs/deep-copy": "self.version" + }, + "require-dev": { + "doctrine/collections": "^1.0", + "doctrine/common": "^2.6", + "phpunit/phpunit": "^7.1" + }, + "type": "library", + "autoload": { + "psr-4": { + "DeepCopy\\": "src/DeepCopy/" + }, + "files": [ + "src/DeepCopy/deep_copy.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Create deep copies (clones) of your objects", + "keywords": [ + "clone", + "copy", + "duplicate", + "object", + "object graph" + ], + "time": "2018-06-11T23:09:50+00:00" + }, + { + "name": "phar-io/manifest", + "version": "1.0.3", + "source": { + "type": "git", + "url": "https://github.com/phar-io/manifest.git", + "reference": "7761fcacf03b4d4f16e7ccb606d4879ca431fcf4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phar-io/manifest/zipball/7761fcacf03b4d4f16e7ccb606d4879ca431fcf4", + "reference": "7761fcacf03b4d4f16e7ccb606d4879ca431fcf4", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "ext-phar": "*", + "phar-io/version": "^2.0", + "php": "^5.6 || ^7.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Arne Blankerts", + "email": "arne@blankerts.de", + "role": "Developer" + }, + { + "name": "Sebastian Heuer", + "email": "sebastian@phpeople.de", + "role": "Developer" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "Developer" + } + ], + "description": "Component for reading phar.io manifest information from a PHP Archive (PHAR)", + "time": "2018-07-08T19:23:20+00:00" + }, + { + "name": "phar-io/version", + "version": "2.0.1", + "source": { + "type": "git", + "url": "https://github.com/phar-io/version.git", + "reference": "45a2ec53a73c70ce41d55cedef9063630abaf1b6" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phar-io/version/zipball/45a2ec53a73c70ce41d55cedef9063630abaf1b6", + "reference": "45a2ec53a73c70ce41d55cedef9063630abaf1b6", + "shasum": "" + }, + "require": { + "php": "^5.6 || ^7.0" + }, + "type": "library", + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Arne Blankerts", + "email": "arne@blankerts.de", + "role": "Developer" + }, + { + "name": "Sebastian Heuer", + "email": "sebastian@phpeople.de", + "role": "Developer" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "Developer" + } + ], + "description": "Library for handling version information and constraints", + "time": "2018-07-08T19:19:57+00:00" + }, + { + "name": "phpdocumentor/reflection-common", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/phpDocumentor/ReflectionCommon.git", + "reference": "21bdeb5f65d7ebf9f43b1b25d404f87deab5bfb6" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionCommon/zipball/21bdeb5f65d7ebf9f43b1b25d404f87deab5bfb6", + "reference": "21bdeb5f65d7ebf9f43b1b25d404f87deab5bfb6", + "shasum": "" + }, + "require": { + "php": ">=5.5" + }, + "require-dev": { + "phpunit/phpunit": "^4.6" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "phpDocumentor\\Reflection\\": [ + "src" + ] + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jaap van Otterdijk", + "email": "opensource@ijaap.nl" + } + ], + "description": "Common reflection classes used by phpdocumentor to reflect the code structure", + "homepage": "http://www.phpdoc.org", + "keywords": [ + "FQSEN", + "phpDocumentor", + "phpdoc", + "reflection", + "static analysis" + ], + "time": "2017-09-11T18:02:19+00:00" + }, + { + "name": "phpdocumentor/reflection-docblock", + "version": "4.3.0", + "source": { + "type": "git", + "url": "https://github.com/phpDocumentor/ReflectionDocBlock.git", + "reference": "94fd0001232e47129dd3504189fa1c7225010d08" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/94fd0001232e47129dd3504189fa1c7225010d08", + "reference": "94fd0001232e47129dd3504189fa1c7225010d08", + "shasum": "" + }, + "require": { + "php": "^7.0", + "phpdocumentor/reflection-common": "^1.0.0", + "phpdocumentor/type-resolver": "^0.4.0", + "webmozart/assert": "^1.0" + }, + "require-dev": { + "doctrine/instantiator": "~1.0.5", + "mockery/mockery": "^1.0", + "phpunit/phpunit": "^6.4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.x-dev" + } + }, + "autoload": { + "psr-4": { + "phpDocumentor\\Reflection\\": [ + "src/" + ] + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mike van Riel", + "email": "me@mikevanriel.com" + } + ], + "description": "With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.", + "time": "2017-11-30T07:14:17+00:00" + }, + { + "name": "phpdocumentor/type-resolver", + "version": "0.4.0", + "source": { + "type": "git", + "url": "https://github.com/phpDocumentor/TypeResolver.git", + "reference": "9c977708995954784726e25d0cd1dddf4e65b0f7" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/9c977708995954784726e25d0cd1dddf4e65b0f7", + "reference": "9c977708995954784726e25d0cd1dddf4e65b0f7", + "shasum": "" + }, + "require": { + "php": "^5.5 || ^7.0", + "phpdocumentor/reflection-common": "^1.0" + }, + "require-dev": { + "mockery/mockery": "^0.9.4", + "phpunit/phpunit": "^5.2||^4.8.24" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "phpDocumentor\\Reflection\\": [ + "src/" + ] + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mike van Riel", + "email": "me@mikevanriel.com" + } + ], + "time": "2017-07-14T14:27:02+00:00" + }, + { + "name": "phpspec/prophecy", + "version": "1.8.0", + "source": { + "type": "git", + "url": "https://github.com/phpspec/prophecy.git", + "reference": "4ba436b55987b4bf311cb7c6ba82aa528aac0a06" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpspec/prophecy/zipball/4ba436b55987b4bf311cb7c6ba82aa528aac0a06", + "reference": "4ba436b55987b4bf311cb7c6ba82aa528aac0a06", + "shasum": "" + }, + "require": { + "doctrine/instantiator": "^1.0.2", + "php": "^5.3|^7.0", + "phpdocumentor/reflection-docblock": "^2.0|^3.0.2|^4.0", + "sebastian/comparator": "^1.1|^2.0|^3.0", + "sebastian/recursion-context": "^1.0|^2.0|^3.0" + }, + "require-dev": { + "phpspec/phpspec": "^2.5|^3.2", + "phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.5 || ^7.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.8.x-dev" + } + }, + "autoload": { + "psr-0": { + "Prophecy\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Konstantin Kudryashov", + "email": "ever.zet@gmail.com", + "homepage": "http://everzet.com" + }, + { + "name": "Marcello Duarte", + "email": "marcello.duarte@gmail.com" + } + ], + "description": "Highly opinionated mocking framework for PHP 5.3+", + "homepage": "https://github.com/phpspec/prophecy", + "keywords": [ + "Double", + "Dummy", + "fake", + "mock", + "spy", + "stub" + ], + "time": "2018-08-05T17:53:17+00:00" + }, + { + "name": "phpunit/php-code-coverage", + "version": "6.0.8", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-code-coverage.git", + "reference": "848f78b3309780fef7ec8c4666b7ab4e6b09b22f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/848f78b3309780fef7ec8c4666b7ab4e6b09b22f", + "reference": "848f78b3309780fef7ec8c4666b7ab4e6b09b22f", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "ext-xmlwriter": "*", + "php": "^7.1", + "phpunit/php-file-iterator": "^2.0", + "phpunit/php-text-template": "^1.2.1", + "phpunit/php-token-stream": "^3.0", + "sebastian/code-unit-reverse-lookup": "^1.0.1", + "sebastian/environment": "^3.1", + "sebastian/version": "^2.0.1", + "theseer/tokenizer": "^1.1" + }, + "require-dev": { + "phpunit/phpunit": "^7.0" + }, + "suggest": { + "ext-xdebug": "^2.6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "6.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Library that provides collection, processing, and rendering functionality for PHP code coverage information.", + "homepage": "https://github.com/sebastianbergmann/php-code-coverage", + "keywords": [ + "coverage", + "testing", + "xunit" + ], + "time": "2018-10-04T03:41:23+00:00" + }, + { + "name": "phpunit/php-file-iterator", + "version": "2.0.2", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-file-iterator.git", + "reference": "050bedf145a257b1ff02746c31894800e5122946" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/050bedf145a257b1ff02746c31894800e5122946", + "reference": "050bedf145a257b1ff02746c31894800e5122946", + "shasum": "" + }, + "require": { + "php": "^7.1" + }, + "require-dev": { + "phpunit/phpunit": "^7.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "FilterIterator implementation that filters files based on a list of suffixes.", + "homepage": "https://github.com/sebastianbergmann/php-file-iterator/", + "keywords": [ + "filesystem", + "iterator" + ], + "time": "2018-09-13T20:33:42+00:00" + }, + { + "name": "phpunit/php-text-template", + "version": "1.2.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-text-template.git", + "reference": "31f8b717e51d9a2afca6c9f046f5d69fc27c8686" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-text-template/zipball/31f8b717e51d9a2afca6c9f046f5d69fc27c8686", + "reference": "31f8b717e51d9a2afca6c9f046f5d69fc27c8686", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Simple template engine.", + "homepage": "https://github.com/sebastianbergmann/php-text-template/", + "keywords": [ + "template" + ], + "time": "2015-06-21T13:50:34+00:00" + }, + { + "name": "phpunit/php-timer", + "version": "2.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-timer.git", + "reference": "8b8454ea6958c3dee38453d3bd571e023108c91f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-timer/zipball/8b8454ea6958c3dee38453d3bd571e023108c91f", + "reference": "8b8454ea6958c3dee38453d3bd571e023108c91f", + "shasum": "" + }, + "require": { + "php": "^7.1" + }, + "require-dev": { + "phpunit/phpunit": "^7.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Utility class for timing", + "homepage": "https://github.com/sebastianbergmann/php-timer/", + "keywords": [ + "timer" + ], + "time": "2018-02-01T13:07:23+00:00" + }, + { + "name": "phpunit/php-token-stream", + "version": "3.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-token-stream.git", + "reference": "21ad88bbba7c3d93530d93994e0a33cd45f02ace" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/21ad88bbba7c3d93530d93994e0a33cd45f02ace", + "reference": "21ad88bbba7c3d93530d93994e0a33cd45f02ace", + "shasum": "" + }, + "require": { + "ext-tokenizer": "*", + "php": "^7.1" + }, + "require-dev": { + "phpunit/phpunit": "^7.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Wrapper around PHP's tokenizer extension.", + "homepage": "https://github.com/sebastianbergmann/php-token-stream/", + "keywords": [ + "tokenizer" + ], + "time": "2018-02-01T13:16:43+00:00" + }, + { + "name": "phpunit/phpunit", + "version": "7.4.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/phpunit.git", + "reference": "f3837fa1e07758057ae06e8ddec6d06ba183f126" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/f3837fa1e07758057ae06e8ddec6d06ba183f126", + "reference": "f3837fa1e07758057ae06e8ddec6d06ba183f126", + "shasum": "" + }, + "require": { + "doctrine/instantiator": "^1.1", + "ext-dom": "*", + "ext-json": "*", + "ext-libxml": "*", + "ext-mbstring": "*", + "ext-xml": "*", + "myclabs/deep-copy": "^1.7", + "phar-io/manifest": "^1.0.2", + "phar-io/version": "^2.0", + "php": "^7.1", + "phpspec/prophecy": "^1.7", + "phpunit/php-code-coverage": "^6.0.7", + "phpunit/php-file-iterator": "^2.0.1", + "phpunit/php-text-template": "^1.2.1", + "phpunit/php-timer": "^2.0", + "sebastian/comparator": "^3.0", + "sebastian/diff": "^3.0", + "sebastian/environment": "^3.1", + "sebastian/exporter": "^3.1", + "sebastian/global-state": "^2.0", + "sebastian/object-enumerator": "^3.0.3", + "sebastian/resource-operations": "^2.0", + "sebastian/version": "^2.0.1" + }, + "conflict": { + "phpunit/phpunit-mock-objects": "*" + }, + "require-dev": { + "ext-pdo": "*" + }, + "suggest": { + "ext-soap": "*", + "ext-xdebug": "*", + "phpunit/php-invoker": "^2.0" + }, + "bin": [ + "phpunit" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "7.4-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "The PHP Unit Testing framework.", + "homepage": "https://phpunit.de/", + "keywords": [ + "phpunit", + "testing", + "xunit" + ], + "time": "2018-10-05T04:05:24+00:00" + }, + { + "name": "sebastian/code-unit-reverse-lookup", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/code-unit-reverse-lookup.git", + "reference": "4419fcdb5eabb9caa61a27c7a1db532a6b55dd18" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/code-unit-reverse-lookup/zipball/4419fcdb5eabb9caa61a27c7a1db532a6b55dd18", + "reference": "4419fcdb5eabb9caa61a27c7a1db532a6b55dd18", + "shasum": "" + }, + "require": { + "php": "^5.6 || ^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^5.7 || ^6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Looks up which function or method a line of code belongs to", + "homepage": "https://github.com/sebastianbergmann/code-unit-reverse-lookup/", + "time": "2017-03-04T06:30:41+00:00" + }, + { + "name": "sebastian/comparator", + "version": "3.0.2", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/comparator.git", + "reference": "5de4fc177adf9bce8df98d8d141a7559d7ccf6da" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/5de4fc177adf9bce8df98d8d141a7559d7ccf6da", + "reference": "5de4fc177adf9bce8df98d8d141a7559d7ccf6da", + "shasum": "" + }, + "require": { + "php": "^7.1", + "sebastian/diff": "^3.0", + "sebastian/exporter": "^3.1" + }, + "require-dev": { + "phpunit/phpunit": "^7.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Volker Dusch", + "email": "github@wallbash.com" + }, + { + "name": "Bernhard Schussek", + "email": "bschussek@2bepublished.at" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Provides the functionality to compare PHP values for equality", + "homepage": "https://github.com/sebastianbergmann/comparator", + "keywords": [ + "comparator", + "compare", + "equality" + ], + "time": "2018-07-12T15:12:46+00:00" + }, + { + "name": "sebastian/diff", + "version": "3.0.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/diff.git", + "reference": "366541b989927187c4ca70490a35615d3fef2dce" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/366541b989927187c4ca70490a35615d3fef2dce", + "reference": "366541b989927187c4ca70490a35615d3fef2dce", + "shasum": "" + }, + "require": { + "php": "^7.1" + }, + "require-dev": { + "phpunit/phpunit": "^7.0", + "symfony/process": "^2 || ^3.3 || ^4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Kore Nordmann", + "email": "mail@kore-nordmann.de" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Diff implementation", + "homepage": "https://github.com/sebastianbergmann/diff", + "keywords": [ + "diff", + "udiff", + "unidiff", + "unified diff" + ], + "time": "2018-06-10T07:54:39+00:00" + }, + { + "name": "sebastian/environment", + "version": "3.1.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/environment.git", + "reference": "cd0871b3975fb7fc44d11314fd1ee20925fce4f5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/cd0871b3975fb7fc44d11314fd1ee20925fce4f5", + "reference": "cd0871b3975fb7fc44d11314fd1ee20925fce4f5", + "shasum": "" + }, + "require": { + "php": "^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^6.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.1.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Provides functionality to handle HHVM/PHP environments", + "homepage": "http://www.github.com/sebastianbergmann/environment", + "keywords": [ + "Xdebug", + "environment", + "hhvm" + ], + "time": "2017-07-01T08:51:00+00:00" + }, + { + "name": "sebastian/exporter", + "version": "3.1.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/exporter.git", + "reference": "234199f4528de6d12aaa58b612e98f7d36adb937" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/234199f4528de6d12aaa58b612e98f7d36adb937", + "reference": "234199f4528de6d12aaa58b612e98f7d36adb937", + "shasum": "" + }, + "require": { + "php": "^7.0", + "sebastian/recursion-context": "^3.0" + }, + "require-dev": { + "ext-mbstring": "*", + "phpunit/phpunit": "^6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.1.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Volker Dusch", + "email": "github@wallbash.com" + }, + { + "name": "Bernhard Schussek", + "email": "bschussek@2bepublished.at" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + }, + { + "name": "Adam Harvey", + "email": "aharvey@php.net" + } + ], + "description": "Provides the functionality to export PHP variables for visualization", + "homepage": "http://www.github.com/sebastianbergmann/exporter", + "keywords": [ + "export", + "exporter" + ], + "time": "2017-04-03T13:19:02+00:00" + }, + { + "name": "sebastian/global-state", + "version": "2.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/global-state.git", + "reference": "e8ba02eed7bbbb9e59e43dedd3dddeff4a56b0c4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/e8ba02eed7bbbb9e59e43dedd3dddeff4a56b0c4", + "reference": "e8ba02eed7bbbb9e59e43dedd3dddeff4a56b0c4", + "shasum": "" + }, + "require": { + "php": "^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^6.0" + }, + "suggest": { + "ext-uopz": "*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Snapshotting of global state", + "homepage": "http://www.github.com/sebastianbergmann/global-state", + "keywords": [ + "global state" + ], + "time": "2017-04-27T15:39:26+00:00" + }, + { + "name": "sebastian/object-enumerator", + "version": "3.0.3", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/object-enumerator.git", + "reference": "7cfd9e65d11ffb5af41198476395774d4c8a84c5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/object-enumerator/zipball/7cfd9e65d11ffb5af41198476395774d4c8a84c5", + "reference": "7cfd9e65d11ffb5af41198476395774d4c8a84c5", + "shasum": "" + }, + "require": { + "php": "^7.0", + "sebastian/object-reflector": "^1.1.1", + "sebastian/recursion-context": "^3.0" + }, + "require-dev": { + "phpunit/phpunit": "^6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Traverses array structures and object graphs to enumerate all referenced objects", + "homepage": "https://github.com/sebastianbergmann/object-enumerator/", + "time": "2017-08-03T12:35:26+00:00" + }, + { + "name": "sebastian/object-reflector", + "version": "1.1.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/object-reflector.git", + "reference": "773f97c67f28de00d397be301821b06708fca0be" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/object-reflector/zipball/773f97c67f28de00d397be301821b06708fca0be", + "reference": "773f97c67f28de00d397be301821b06708fca0be", + "shasum": "" + }, + "require": { + "php": "^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.1-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Allows reflection of object attributes, including inherited and non-public ones", + "homepage": "https://github.com/sebastianbergmann/object-reflector/", + "time": "2017-03-29T09:07:27+00:00" + }, + { + "name": "sebastian/recursion-context", + "version": "3.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/recursion-context.git", + "reference": "5b0cd723502bac3b006cbf3dbf7a1e3fcefe4fa8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/5b0cd723502bac3b006cbf3dbf7a1e3fcefe4fa8", + "reference": "5b0cd723502bac3b006cbf3dbf7a1e3fcefe4fa8", + "shasum": "" + }, + "require": { + "php": "^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + }, + { + "name": "Adam Harvey", + "email": "aharvey@php.net" + } + ], + "description": "Provides functionality to recursively process PHP variables", + "homepage": "http://www.github.com/sebastianbergmann/recursion-context", + "time": "2017-03-03T06:23:57+00:00" + }, + { + "name": "sebastian/resource-operations", + "version": "2.0.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/resource-operations.git", + "reference": "4d7a795d35b889bf80a0cc04e08d77cedfa917a9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/resource-operations/zipball/4d7a795d35b889bf80a0cc04e08d77cedfa917a9", + "reference": "4d7a795d35b889bf80a0cc04e08d77cedfa917a9", + "shasum": "" + }, + "require": { + "php": "^7.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Provides a list of PHP built-in functions that operate on resources", + "homepage": "https://www.github.com/sebastianbergmann/resource-operations", + "time": "2018-10-04T04:07:39+00:00" + }, + { + "name": "sebastian/version", + "version": "2.0.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/version.git", + "reference": "99732be0ddb3361e16ad77b68ba41efc8e979019" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/version/zipball/99732be0ddb3361e16ad77b68ba41efc8e979019", + "reference": "99732be0ddb3361e16ad77b68ba41efc8e979019", + "shasum": "" + }, + "require": { + "php": ">=5.6" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Library that helps with managing the version number of Git-hosted PHP projects", + "homepage": "https://github.com/sebastianbergmann/version", + "time": "2016-10-03T07:35:21+00:00" + }, + { + "name": "theseer/tokenizer", + "version": "1.1.0", + "source": { + "type": "git", + "url": "https://github.com/theseer/tokenizer.git", + "reference": "cb2f008f3f05af2893a87208fe6a6c4985483f8b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/theseer/tokenizer/zipball/cb2f008f3f05af2893a87208fe6a6c4985483f8b", + "reference": "cb2f008f3f05af2893a87208fe6a6c4985483f8b", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "ext-tokenizer": "*", + "ext-xmlwriter": "*", + "php": "^7.0" + }, + "type": "library", + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Arne Blankerts", + "email": "arne@blankerts.de", + "role": "Developer" + } + ], + "description": "A small library for converting tokenized PHP source code into XML and potentially other formats", + "time": "2017-04-07T12:08:54+00:00" + }, + { + "name": "webmozart/assert", + "version": "1.3.0", + "source": { + "type": "git", + "url": "https://github.com/webmozart/assert.git", + "reference": "0df1908962e7a3071564e857d86874dad1ef204a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/webmozart/assert/zipball/0df1908962e7a3071564e857d86874dad1ef204a", + "reference": "0df1908962e7a3071564e857d86874dad1ef204a", + "shasum": "" + }, + "require": { + "php": "^5.3.3 || ^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.6", + "sebastian/version": "^1.0.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.3-dev" + } + }, + "autoload": { + "psr-4": { + "Webmozart\\Assert\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Bernhard Schussek", + "email": "bschussek@gmail.com" + } + ], + "description": "Assertions to validate method input/output with nice error messages.", + "keywords": [ + "assert", + "check", + "validate" + ], + "time": "2018-01-29T19:49:41+00:00" + } + ], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": true, + "prefer-lowest": false, + "platform": { + "php": ">=7.1.0" + }, + "platform-dev": [] +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..bd110cb --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,21 @@ + + + + + tests + + + + + + src + + + diff --git a/src/SoftCreatR/MimeDetector/MimeDetector.php b/src/SoftCreatR/MimeDetector/MimeDetector.php new file mode 100644 index 0000000..e68cfab --- /dev/null +++ b/src/SoftCreatR/MimeDetector/MimeDetector.php @@ -0,0 +1,1090 @@ +init(); + } + + /** + * Called within __construct(), override if necessary. + */ + protected function init() + { + } + + /** + * Object cloning is disallowed. + * + * @codeCoverageIgnore + */ + final protected function __clone() + { + } + + /** + * Object serializing is disallowed. + * + * @throws MimeDetectorException + * @codeCoverageIgnore + */ + final public function __sleep() + { + throw new MimeDetectorException('Serializing of Singletons is not allowed'); + } + + /** + * Returns an unique instance of the MimeDetector class. + */ + public static function getInstance() + { + if (empty(self::$instance)) { + self::$instance = new MimeDetector(); + } + + return self::$instance; + } + + /** + * Setter for the file to be checked. + * + * @param string $filePath + * @return void + * @throws MimeDetectorException + */ + public function setFile(string $filePath): void + { + if (!file_exists($filePath)) { + throw new MimeDetectorException("File '" . $filePath . "' does not exist."); + } + + $fileHash = hash_file('crc32b', $filePath); + + if ($this->fileHash !== $fileHash) { + $this->byteCache = []; + $this->byteCacheLen = 0; + $this->file = $filePath; + $this->fileHash = $fileHash; + + $this->createByteCache(); + } + } + + /** + * Tries to determine the correct mime type of the given file by using "magic numbers". + * + * @return array + */ + public function getFileType(): array + { + // Perform check + if ($this->checkForBytes([0xFF, 0xD8, 0xFF])) { + return [ + 'ext' => 'jpg', + 'mime' => 'image/jpeg' + ]; + } + + if ($this->checkForBytes([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A])) { + return [ + 'ext' => 'png', + 'mime' => 'image/png' + ]; + } + + if ($this->checkForBytes([0x47, 0x49, 0x46])) { + return [ + 'ext' => 'gif', + 'mime' => 'image/gif' + ]; + } + + if ($this->checkForBytes([0x57, 0x45, 0x42, 0x50], 8)) { + return [ + 'ext' => 'webp', + 'mime' => 'image/webp' + ]; + } + + if ($this->checkForBytes([0x46, 0x4C, 0x49, 0x46])) { + return [ + 'ext' => 'flif', + 'mime' => 'image/flif' + ]; + } + + // Needs to be before `tif` check + if (( + $this->checkForBytes([0x49, 0x49, 0x2A, 0x0]) || + $this->checkForBytes([0x4D, 0x4D, 0x0, 0x2A]) + ) && $this->checkForBytes([0x43, 0x52], 8) + ) { + return [ + 'ext' => 'cr2', + 'mime' => 'image/x-canon-cr2' + ]; + } + + if ($this->checkForBytes([0x49, 0x49, 0x2A, 0x0]) || + $this->checkForBytes([0x4D, 0x4D, 0x0, 0x2A]) + ) { + return [ + 'ext' => 'tif', + 'mime' => 'image/tiff' + ]; + } + + if ($this->checkForBytes([0x42, 0x4D])) { + return [ + 'ext' => 'bmp', + 'mime' => 'image/bmp' + ]; + } + + if ($this->checkForBytes([0x49, 0x49, 0xBC])) { + return [ + 'ext' => 'jxr', + 'mime' => 'image/vnd.ms-photo' + ]; + } + + if ($this->checkForBytes([0x38, 0x42, 0x50, 0x53])) { + return [ + 'ext' => 'psd', + 'mime' => 'image/vnd.adobe.photoshop' + ]; + } + + // Zip-based file formats + // Need to be before the `zip` check + if ($this->checkForBytes([0x50, 0x4B, 0x3, 0x4])) { + if ($this->checkForBytes([ + 0x6D, 0x69, 0x6D, 0x65, 0x74, 0x79, 0x70, + 0x65, 0x61, 0x70, 0x70, 0x6C, 0x69, 0x63, + 0x61, 0x74, 0x69, 0x6F, 0x6E, 0x2F, 0x65, + 0x70, 0x75, 0x62, 0x2B, 0x7A, 0x69, 0x70 + ], 30) + ) { + return [ + 'ext' => 'epub', + 'mime' => 'application/epub+zip' + ]; + } + + // Assumes signed `.xpi` from addons.mozilla.org + if ($this->checkString('META-INF/mozilla.rsa', 30)) { + return [ + 'ext' => 'xpi', + 'mime' => 'application/x-xpinstall' + ]; + } + + if ($this->checkString('mimetypeapplication/vnd.oasis.opendocument.text', 30)) { + return [ + 'ext' => 'odt', + 'mime' => 'application/vnd.oasis.opendocument.text' + ]; + } + + if ($this->checkString('mimetypeapplication/vnd.oasis.opendocument.spreadsheet', 30)) { + return [ + 'ext' => 'ods', + 'mime' => 'application/vnd.oasis.opendocument.spreadsheet' + ]; + } + + if ($this->checkString('mimetypeapplication/vnd.oasis.opendocument.presentation', 30)) { + return [ + 'ext' => 'odp', + 'mime' => 'application/vnd.oasis.opendocument.presentation' + ]; + } + + // The docx, xlsx and pptx file types extend the Office Open XML file format: + // https://en.wikipedia.org/wiki/Office_Open_XML_file_formats + $zipHeaderIndex = 0; // The first zip header was already found at index 0 + $oxmlFound = false; + $type = null; + $oxmlCTypes = $this->toBytes('[Content_Types].xml'); + $oxmlRels = $this->toBytes('_rels/.rels'); + + do { + $offset = $zipHeaderIndex + 30; + + if (!$oxmlFound) { + $oxmlFound = $this->checkForBytes($oxmlCTypes, $offset) || $this->checkForBytes($oxmlRels, $offset); + } + + if (!$type) { + if ($this->checkString('word/', $offset)) { + $type = [ + 'ext' => 'docx', + 'mime' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' + ]; + } elseif ($this->checkString('ppt/', $offset)) { + $type = [ + 'ext' => 'pptx', + 'mime' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation' + ]; + } elseif ($this->checkString('xl/', $offset)) { + $type = [ + 'ext' => 'xlsx', + 'mime' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' + ]; + } + } + + if ($oxmlFound && $type) { + return $type; + } + + $zipHeaderIndex = $this->searchForBytes([0x50, 0x4B, 0x03, 0x04], $offset); + } while ($zipHeaderIndex !== -1); + + // No more zip parts available in the buffer, but maybe we are almost certain about the type? + if ($type) { + return $type; + } + } + + if ($this->checkForBytes([0x50, 0x4B, 0x3, 0x4])) { + return [ + 'ext' => 'zip', + 'mime' => 'application/zip' + ]; + } + + if ($this->checkForBytes([0x75, 0x73, 0x74, 0x61, 0x72], 257)) { + return [ + 'ext' => 'tar', + 'mime' => 'application/x-tar' + ]; + } + + if ($this->checkForBytes([0x52, 0x61, 0x72, 0x21, 0x1A, 0x7]) && + ( + $this->byteCache[6] === 0x0 || + $this->byteCache[6] === 0x1 + ) + ) { + return [ + 'ext' => 'rar', + 'mime' => 'application/x-rar-compressed' + ]; + } + + if ($this->checkString('searchForBytes($this->toBytes('searchForBytes($this->toBytes(' 'svg', + 'mime' => 'image/svg+xml' + ]; + } + + return [ + 'ext' => 'xml', + 'mime' => 'application/xml' + ]; + } + + if ($this->checkForBytes([0x1F, 0x8B, 0x8])) { + return [ + 'ext' => 'gz', + 'mime' => 'application/gzip' + ]; + } + + if ($this->checkForBytes([0x42, 0x5A, 0x68])) { + return [ + 'ext' => 'bz2', + 'mime' => 'application/x-bzip2' + ]; + } + + if ($this->checkForBytes([0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C])) { + return [ + 'ext' => '7z', + 'mime' => 'application/x-7z-compressed' + ]; + } + + if ($this->checkForBytes([0x78, 0x01])) { + return [ + 'ext' => 'dmg', + 'mime' => 'application/x-apple-diskimage' + ]; + } + + if ($this->checkForBytes([0x33, 0x67, 0x70, 0x35]) || // 3gp5 + ( + $this->checkForBytes([0x0, 0x0, 0x0]) && + $this->checkForBytes([0x66, 0x74, 0x79, 0x70], 4) && + ( + $this->checkForBytes([0x6D, 0x70, 0x34, 0x31], 8) || // MP41 + $this->checkForBytes([0x6D, 0x70, 0x34, 0x32], 8) || // MP42 + $this->checkForBytes([0x69, 0x73, 0x6F, 0x6D], 8) || // ISOM + $this->checkForBytes([0x69, 0x73, 0x6F, 0x32], 8) || // ISO2 + $this->checkForBytes([0x6D, 0x6D, 0x70, 0x34], 8) || // MMP4 + $this->checkForBytes([0x4D, 0x34, 0x56], 8) || // M4V + $this->checkForBytes([0x64, 0x61, 0x73, 0x68], 8) // DASH + ) + ) + ) { + return [ + 'ext' => 'mp4', + 'mime' => 'video/mp4' + ]; + } + + if ($this->checkForBytes([0x4D, 0x54, 0x68, 0x64])) { + return [ + 'ext' => 'mid', + 'mime' => 'audio/midi' + ]; + } + + // https://github.com/threatstack/libmagic/blob/master/magic/Magdir/matroska + if ($this->checkForBytes([0x1A, 0x45, 0xDF, 0xA3])) { + $idPos = $this->searchForBytes([0x42, 0x82]); + + if ($idPos !== -1) { + if ($this->checkString('matroska', $idPos + 3)) { + return [ + 'ext' => 'mkv', + 'mime' => 'video/x-matroska' + ]; + } + + if ($this->checkString('webm', $idPos + 3)) { + return [ + 'ext' => 'webm', + 'mime' => 'video/webm' + ]; + } + } + } + + if ($this->checkForBytes([0x0, 0x0, 0x0, 0x14, 0x66, 0x74, 0x79, 0x70, 0x71, 0x74, 0x20, 0x20]) || + $this->checkForBytes([0x66, 0x72, 0x65, 0x65], 4) || + $this->checkForBytes([0x66, 0x74, 0x79, 0x70, 0x71, 0x74, 0x20, 0x20], 4) || + $this->checkForBytes([0x6D, 0x64, 0x61, 0x74], 4) || // MJPEG + $this->checkForBytes([0x77, 0x69, 0x64, 0x65], 4) + ) { + return [ + 'ext' => 'mov', + 'mime' => 'video/quicktime' + ]; + } + + // RIFF file format which might be AVI, WAV, QCP, etc + if ($this->checkForBytes([0x52, 0x49, 0x46, 0x46])) { + if ($this->checkForBytes([0x41, 0x56, 0x49], 8)) { + return [ + 'ext' => 'avi', + 'mime' => 'video/vnd.avi' + ]; + } + + if ($this->checkForBytes([0x57, 0x41, 0x56, 0x45], 8)) { + return [ + 'ext' => 'wav', + 'mime' => 'audio/vnd.wave' + ]; + } + + // QLCM, QCP file + if ($this->checkForBytes([0x51, 0x4C, 0x43, 0x4D], 8)) { + return [ + 'ext' => 'qcp', + 'mime' => 'audio/qcelp' + ]; + } + } + + if ($this->checkForBytes([0x30, 0x26, 0xB2, 0x75, 0x8E, 0x66, 0xCF, 0x11, 0xA6, 0xD9])) { + return [ + 'ext' => 'wmv', + 'mime' => 'video/x-ms-wmv' + ]; + } + + if ($this->checkForBytes([0x0, 0x0, 0x1, 0xBA]) || $this->checkForBytes([0x0, 0x0, 0x1, 0xB3])) { + return [ + 'ext' => 'mpg', + 'mime' => 'video/mpeg' + ]; + } + + if ($this->checkForBytes([0x66, 0x74, 0x79, 0x70, 0x33, 0x67], 4)) { + return [ + 'ext' => '3gp', + 'mime' => 'video/3gpp' + ]; + } + + // Check for MPEG header at different starting offsets + for ($offset = 0; ($offset < 2 && $offset < ($this->byteCacheLen - 16)); $offset++) { + if ($this->checkForBytes([0x49, 0x44, 0x33], $offset) || // ID3 header + $this->checkForBytes([0xFF, 0xE2], $offset, [0xFF, 0xE2]) // MPEG 1 or 2 Layer 3 header + ) { + return [ + 'ext' => 'mp3', + 'mime' => 'audio/mpeg' + ]; + } + + // MPEG 1 or 2 Layer 2 header + if ($this->checkForBytes([0xFF, 0xE4], $offset, [0xFF, 0xE4])) { + return [ + 'ext' => 'mp2', + 'mime' => 'audio/mpeg' + ]; + } + + // MPEG 2 layer 0 using ADTS + if ($this->checkForBytes([0xFF, 0xF8], $offset, [0xFF, 0xFC])) { + return [ + 'ext' => 'mp2', + 'mime' => 'audio/mpeg' + ]; + } + + // MPEG 4 layer 0 using ADTS + if ($this->checkForBytes([0xFF, 0xF0], $offset, [0xFF, 0xFC])) { + return [ + 'ext' => 'mp4', + 'mime' => 'audio/mpeg' + ]; + } + } + + if ($this->checkForBytes([0x66, 0x74, 0x79, 0x70, 0x4D, 0x34, 0x41], 4) || + $this->checkForBytes([0x4D, 0x34, 0x41, 0x20]) + ) { + return [ // MPEG-4 layer 3 (audio) + 'ext' => 'm4a', + 'mime' => 'audio/mp4' // RFC 4337 + ]; + } + + // Needs to be before `ogg` check + if ($this->checkForBytes([0x4F, 0x70, 0x75, 0x73, 0x48, 0x65, 0x61, 0x64], 28)) { + return [ + 'ext' => 'opus', + 'mime' => 'audio/opus' + ]; + } + + // If 'OggS' in first bytes, then OGG container + if ($this->checkForBytes([0x4F, 0x67, 0x67, 0x53])) { + // This is a OGG container + + // If ' theora' in header. + if ($this->checkForBytes([0x80, 0x74, 0x68, 0x65, 0x6F, 0x72, 0x61], 28)) { + return [ + 'ext' => 'ogv', + 'mime' => 'video/ogg' + ]; + } + + // If '\x01video' in header. + if ($this->checkForBytes([0x01, 0x76, 0x69, 0x64, 0x65, 0x6F, 0x00], 28)) { + return [ + 'ext' => 'ogm', + 'mime' => 'video/ogg' + ]; + } + + // If ' FLAC' in header https://xiph.org/flac/faq.html + if ($this->checkForBytes([0x7F, 0x46, 0x4C, 0x41, 0x43], 28)) { + return [ + 'ext' => 'oga', + 'mime' => 'audio/ogg' + ]; + } + + // 'Speex ' in header https://en.wikipedia.org/wiki/Speex + if ($this->checkForBytes([0x53, 0x70, 0x65, 0x65, 0x78, 0x20, 0x20], 28)) { + return [ + 'ext' => 'spx', + 'mime' => 'audio/ogg' + ]; + } + + // If '\x01vorbis' in header + if ($this->checkForBytes([0x01, 0x76, 0x6F, 0x72, 0x62, 0x69, 0x73], 28)) { + return [ + 'ext' => 'ogg', + 'mime' => 'audio/ogg' + ]; + } + + // Default OGG container https://www.iana.org/assignments/media-types/application/ogg + // @codeCoverageIgnoreStart + return [ + 'ext' => 'ogx', + 'mime' => 'application/ogg' + ]; + // @codeCoverageIgnoreEnd + } + + if ($this->checkForBytes([0x66, 0x4C, 0x61, 0x43])) { + return [ + 'ext' => 'flac', + 'mime' => 'audio/x-flac' + ]; + } + + // 'MAC ' + if ($this->checkForBytes([0x4D, 0x41, 0x43, 0x20])) { + return [ + 'ext' => 'ape', + 'mime' => 'audio/ape' + ]; + } + + // 'wvpk' + if ($this->checkForBytes([0x77, 0x76, 0x70, 0x6B])) { + return [ + 'ext' => 'wv', + 'mime' => 'audio/wavpack' + ]; + } + + if ($this->checkForBytes([0x23, 0x21, 0x41, 0x4D, 0x52, 0x0A])) { + return [ + 'ext' => 'amr', + 'mime' => 'audio/amr' + ]; + } + + if ($this->checkForBytes([0x25, 0x50, 0x44, 0x46])) { + return [ + 'ext' => 'pdf', + 'mime' => 'application/pdf' + ]; + } + + if ($this->checkForBytes([0x4D, 0x5A])) { + return [ + 'ext' => 'exe', + 'mime' => 'application/x-msdownload' + ]; + } + + if (( + $this->byteCache[0] === 0x43 || $this->byteCache[0] === 0x46 + ) && + $this->checkForBytes([0x57, 0x53], 1) + ) { + return [ + 'ext' => 'swf', + 'mime' => 'application/x-shockwave-flash' + ]; + } + + if ($this->checkForBytes([0x7B, 0x5C, 0x72, 0x74, 0x66])) { + return [ + 'ext' => 'rtf', + 'mime' => 'application/rtf' + ]; + } + + if ($this->checkForBytes([0x00, 0x61, 0x73, 0x6D])) { + return [ + 'ext' => 'wasm', + 'mime' => 'application/wasm' + ]; + } + + if ($this->checkForBytes([0x77, 0x4F, 0x46]) && + ( + $this->checkForBytes([0x00, 0x01, 0x00, 0x00], 4) || + $this->checkForBytes([0x4F, 0x54, 0x54, 0x4F], 4) + ) + ) { + if ($this->byteCache[3] === 0x46) { + return [ + 'ext' => 'woff', + 'mime' => 'font/woff' + ]; + } + + if ($this->byteCache[3] === 0x32) { + return [ + 'ext' => 'woff2', + 'mime' => 'font/woff2' + ]; + } + } + + if ($this->checkForBytes([0x4C, 0x50], 34) && + ( + $this->checkForBytes([0x00, 0x00, 0x01], 8) || + $this->checkForBytes([0x01, 0x00, 0x02], 8) || + $this->checkForBytes([0x02, 0x00, 0x02], 8) + ) + ) { + return [ + 'ext' => 'eot', + 'mime' => 'application/vnd.ms-fontobject' + ]; + } + + if ($this->checkForBytes([0x00, 0x01, 0x00, 0x00, 0x00])) { + return [ + 'ext' => 'ttf', + 'mime' => 'font/ttf' + ]; + } + + if ($this->checkForBytes([0x4F, 0x54, 0x54, 0x4F, 0x00])) { + return [ + 'ext' => 'otf', + 'mime' => 'font/otf' + ]; + } + + if ($this->checkForBytes([0x00, 0x00, 0x01, 0x00])) { + return [ + 'ext' => 'ico', + 'mime' => 'image/x-icon' + ]; + } + + if ($this->checkForBytes([0x00, 0x00, 0x02, 0x00])) { + return [ + 'ext' => 'cur', + 'mime' => 'image/x-icon' + ]; + } + + if ($this->checkForBytes([0x46, 0x4C, 0x56, 0x01])) { + return [ + 'ext' => 'flv', + 'mime' => 'video/x-flv' + ]; + } + + if ($this->checkForBytes([0x25, 0x21])) { + return [ + 'ext' => 'ps', + 'mime' => 'application/postscript' + ]; + } + + if ($this->checkForBytes([0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00])) { + return [ + 'ext' => 'xz', + 'mime' => 'application/x-xz' + ]; + } + + if ($this->checkForBytes([0x53, 0x51, 0x4C, 0x69])) { + return [ + 'ext' => 'sqlite', + 'mime' => 'application/x-sqlite3' + ]; + } + + if ($this->checkForBytes([0x4E, 0x45, 0x53, 0x1A])) { + return [ + 'ext' => 'nes', + 'mime' => 'application/x-nintendo-nes-rom' + ]; + } + + if ($this->checkForBytes([0x43, 0x72, 0x32, 0x34])) { + return [ + 'ext' => 'crx', + 'mime' => 'application/x-google-chrome-extension' + ]; + } + + if ($this->checkForBytes([0x4D, 0x53, 0x43, 0x46]) || + $this->checkForBytes([0x49, 0x53, 0x63, 0x28]) + ) { + return [ + 'ext' => 'cab', + 'mime' => 'application/vnd.ms-cab-compressed' + ]; + } + + // Needs to be before `ar` check + if ($this->checkForBytes([ + 0x21, 0x3C, 0x61, 0x72, 0x63, 0x68, 0x3E, + 0x0A, 0x64, 0x65, 0x62, 0x69, 0x61, 0x6E, + 0x2D, 0x62, 0x69, 0x6E, 0x61, 0x72, 0x79 + ])) { + return [ + 'ext' => 'deb', + 'mime' => 'application/x-deb' + ]; + } + + if ($this->checkForBytes([0x21, 0x3C, 0x61, 0x72, 0x63, 0x68, 0x3E])) { + return [ + 'ext' => 'ar', + 'mime' => 'application/x-unix-archive' + ]; + } + + if ($this->checkForBytes([0xED, 0xAB, 0xEE, 0xDB])) { + return [ + 'ext' => 'rpm', + 'mime' => 'application/x-rpm' + ]; + } + + if ($this->checkForBytes([0x1F, 0xA0]) || $this->checkForBytes([0x1F, 0x9D])) { + return [ + 'ext' => 'Z', + 'mime' => 'application/x-compress' + ]; + } + + if ($this->checkForBytes([0x4C, 0x5A, 0x49, 0x50])) { + return [ + 'ext' => 'lz', + 'mime' => 'application/x-lzip' + ]; + } + + if ($this->checkForBytes([0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1])) { + return [ + 'ext' => 'msi', + 'mime' => 'application/x-msi' + ]; + } + + if ($this->checkForBytes([ + 0x06, 0x0E, 0x2B, 0x34, 0x02, 0x05, 0x01, + 0x01, 0x0D, 0x01, 0x02, 0x01, 0x01, 0x02 + ])) { + return [ + 'ext' => 'mxf', + 'mime' => 'application/mxf' + ]; + } + + if ($this->checkForBytes([0x47], 4) && + ( + $this->checkForBytes([0x47], 192) || + $this->checkForBytes([0x47], 196) + ) + ) { + return [ + 'ext' => 'mts', + 'mime' => 'video/mp2t' + ]; + } + + if ($this->checkForBytes([0x42, 0x4C, 0x45, 0x4E, 0x44, 0x45, 0x52])) { + return [ + 'ext' => 'blend', + 'mime' => 'application/x-blender' + ]; + } + + if ($this->checkForBytes([0x42, 0x50, 0x47, 0xFB])) { + return [ + 'ext' => 'bpg', + 'mime' => 'image/bpg' + ]; + } + + if ($this->checkForBytes([0x00, 0x00, 0x00, 0x0C, 0x6A, 0x50, 0x20, 0x20, 0x0D, 0x0A, 0x87, 0x0A])) { + // JPEG-2000 family + if ($this->checkForBytes([0x6A, 0x70, 0x32, 0x20], 20)) { + return [ + 'ext' => 'jp2', + 'mime' => 'image/jp2' + ]; + } + + if ($this->checkForBytes([0x6A, 0x70, 0x78, 0x20], 20)) { + return [ + 'ext' => 'jpx', + 'mime' => 'image/jpx' + ]; + } + + if ($this->checkForBytes([0x6A, 0x70, 0x6D, 0x20], 20)) { + return [ + 'ext' => 'jpm', + 'mime' => 'image/jpm' + ]; + } + + if ($this->checkForBytes([0x6D, 0x6A, 0x70, 0x32], 20)) { + return [ + 'ext' => 'mj2', + 'mime' => 'image/mj2' + ]; + } + } + + if ($this->checkForBytes([0x46, 0x4F, 0x52, 0x4D, 0x00])) { + return [ + 'ext' => 'aif', + 'mime' => 'audio/aiff' + ]; + } + + if ($this->checkForBytes([0x42, 0x4F, 0x4F, 0x4B, 0x4D, 0x4F, 0x42, 0x49], 60)) { + return [ + 'ext' => 'mobi', + 'mime' => 'application/x-mobipocket-ebook' + ]; + } + + // File Type Box (https://en.wikipedia.org/wiki/ISO_base_media_file_format) + if ($this->checkForBytes([0x66, 0x74, 0x79, 0x70], 4)) { + if ($this->checkForBytes([0x6D, 0x69, 0x66, 0x31], 8)) { + return [ + 'ext' => 'heic', + 'mime' => 'image/heif' + ]; + } + + if ($this->checkForBytes([0x6D, 0x73, 0x66, 0x31], 8)) { + return [ + 'ext' => 'heic', + 'mime' => 'image/heif-sequence' + ]; + } + + if ($this->checkForBytes([0x68, 0x65, 0x69, 0x63], 8) || + $this->checkForBytes([0x68, 0x65, 0x69, 0x78], 8) + ) { + return [ + 'ext' => 'heic', + 'mime' => 'image/heic' + ]; + } + + // @codeCoverageIgnoreStart + if ($this->checkForBytes([0x68, 0x65, 0x76, 0x63], 8) || + $this->checkForBytes([0x68, 0x65, 0x76, 0x78], 8) + ) { + return [ + 'ext' => 'heic', + 'mime' => 'image/heic-sequence' + ]; + } + // @codeCoverageIgnoreEnd + } + + if ($this->checkForBytes([0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A])) { + return [ + 'ext' => 'ktx', + 'mime' => 'image/ktx' + ]; + } + + if ($this->checkForBytes([0x44, 0x49, 0x43, 0x4D], 128)) { + return [ + 'ext' => 'dcm', + 'mime' => 'application/dicom' + ]; + } + + if ($this->checkForBytes([0x1B, 0x4C, 0x75, 0x61])) { + return [ + 'ext' => 'luac', + 'mime' => 'application/x-lua-bytecode' + ]; + } + + return []; + } + + /** + * Returns the detected file extension. + * + * @return string + */ + public function getFileExtension(): string + { + $fileType = $this->getFileType(); + + if (!empty($fileType['ext'])) { + return $fileType['ext']; + } + + return ''; + } + + /** + * Returns the detected file mime type. + * + * @return string + */ + public function getMimeType(): string + { + $fileType = $this->getFileType(); + + if (!empty($fileType['mime'])) { + return $fileType['mime']; + } + + return ''; + } + + /** + * Returns the byte sequence of a given string. + * + * @param string $str + * @return array + */ + public function toBytes(string $str): array + { + return array_values(unpack('C*', $str)); + } + + /** + * Checks the byte sequence of a given string. + * + * @param string $str + * @param int $offset + * @return bool + */ + protected function checkString(string $str, int $offset = 0): bool + { + return $this->checkForBytes($this->toBytes($str), $offset); + } + + /** + * Returns the offset to the next position of the given byte sequence. + * Returns -1 if the sequence was not found. + * + * @param array $bytes + * @param int $offset + * @param array $mask + * @return int + */ + protected function searchForBytes(array $bytes, int $offset = 0, array $mask = []): int + { + $limit = $this->byteCacheLen - count($bytes); + + for ($i = $offset; $i < $limit; $i++) { + if ($this->checkForBytes($bytes, $i, $mask)) { + return $i; + } + } + + return -1; + } + + /** + * Returns true, if a given byte sequence is found at the given offset within the given file. + * + * @param array $bytes + * @param int $offset + * @param array $mask + * @return bool + */ + protected function checkForBytes(array $bytes, int $offset = 0, array $mask = []): bool + { + if (empty($bytes) || empty($this->byteCache)) { + return false; + } + + // make sure we have nummeric indices + $bytes = array_values($bytes); + + foreach ($bytes as $i => $byte) { + if (!empty($mask)) { + if (!isset($this->byteCache[$offset + $i]) || + !isset($mask[$i]) || + $byte !== ($mask[$i] & $this->byteCache[$offset + $i]) + ) { + return false; + } + } elseif (!isset($this->byteCache[$offset + $i]) || $this->byteCache[$offset + $i] != $byte) { + return false; + } + } + + return true; + } + + /** + * Caches the first 4096 bytes of the given file, so we don't have to read the whole file on every iteration. + * + * @return void + * @throws MimeDetectorException + */ + protected function createByteCache(): void + { + if (!empty($this->byteCache)) { + return; + } + + if (empty($this->file)) { + throw new MimeDetectorException('No file provided.'); + } + + $handle = fopen($this->file, 'rb'); + $data = fread($handle, 4096); + fclose($handle); + + foreach (str_split($data) as $i => $char) { + $this->byteCache[$i] = ord($char); + } + + $this->byteCacheLen = count($this->byteCache); + } +} diff --git a/src/SoftCreatR/MimeDetector/MimeDetectorException.php b/src/SoftCreatR/MimeDetector/MimeDetectorException.php new file mode 100644 index 0000000..daed4a0 --- /dev/null +++ b/src/SoftCreatR/MimeDetector/MimeDetectorException.php @@ -0,0 +1,17 @@ +getInstance(); + + self::assertInstanceOf(MimeDetector::class, $mimeDetector); + self::assertAttributeInternalType('array', 'byteCache', $mimeDetector); + self::assertAttributeInternalType('int', 'byteCacheLen', $mimeDetector); + self::assertAttributeInternalType('string', 'file', $mimeDetector); + self::assertAttributeInternalType('string', 'fileHash', $mimeDetector); + } + + /** + * Test, if `setFile` throws an exception, if the provided file does not exist. + * + * @return void + * @throws MimeDetectorException + */ + public function testSetFileThrowsException(): void + { + self::expectException(MimeDetectorException::class); + + $mimeDetector = MimeDetector::getInstance(); + $mimeDetector->setFile('nonexistant.file'); + } + + /** + * @dataProvider provideTestFiles + * @param array $testFiles + * @return void + * @throws MimeDetectorException + */ + public function testSetFile($testFiles): void + { + $mimeDetector = $this->getInstance(); + + foreach ($testFiles as $testFile) { + $mimeDetector->setFile($testFile['file']); + + self::assertAttributeNotEmpty('byteCache', $mimeDetector); + self::assertAttributeGreaterThanOrEqual(1, 'byteCacheLen', $mimeDetector); + self::assertAttributeEquals($testFile['file'], 'file', $mimeDetector); + self::assertAttributeEquals($testFile['hash'], 'fileHash', $mimeDetector); + } + } + + /** + * @dataProvider provideTestFiles + * @param array $testFiles + * @return void + * @throws MimeDetectorException + */ + public function testGetFileType(array $testFiles): void + { + $mimeDetector = $this->getInstance(); + + foreach ($testFiles as $testFile) { + $mimeDetector->setFile($testFile['file']); + $fileData = $mimeDetector->getFileType(); + + self::assertInternalType('array', $fileData); + self::assertEquals($testFile['ext'], $fileData['ext']); + } + } + + /** + * Test, if `getFileExtension` returns an empty string, if the file type of the provided file cannot be determined. + * + * @dataProvider provideTestFiles + * @return void + * @throws MimeDetectorException + */ + public function testGetFileExtensionEmpty(): void + { + $mimeDetector = $this->getInstance(); + $mimeDetector->setFile(__FILE__); + $detectedExtension = $mimeDetector->getFileExtension(); + + self::assertInternalType('string', $detectedExtension); + self::assertEmpty($detectedExtension); + } + + /** + * @dataProvider provideTestFiles + * @param array $testFiles + * @return void + * @throws MimeDetectorException + */ + public function testGetFileExtension(array $testFiles): void + { + $mimeDetector = $this->getInstance(); + + foreach ($testFiles as $testFile) { + $mimeDetector->setFile($testFile['file']); + $detectedExtension = $mimeDetector->getFileExtension(); + + self::assertInternalType('string', $detectedExtension); + self::assertEquals($testFile['ext'], $detectedExtension); + } + } + + /** + * Test, if `getMimeType` returns an empty string, if the file type of the provided file cannot be determined. + * + * @dataProvider provideTestFiles + * @return void + * @throws MimeDetectorException + */ + public function testGetMimeTypeEmpty(): void + { + $mimeDetector = $this->getInstance(); + $mimeDetector->setFile(__FILE__); + $detectedMimeType = $mimeDetector->getMimeType(); + + self::assertInternalType('string', $detectedMimeType); + self::assertEmpty($detectedMimeType); + } + + /** + * @dataProvider provideTestFiles + * @param array $testFiles + * @return void + * @throws MimeDetectorException + */ + public function testGetMimeType(array $testFiles): void + { + $mimeDetector = $this->getInstance(); + + foreach ($testFiles as $testFile) { + $mimeDetector->setFile($testFile['file']); + $detectedMimeType = $mimeDetector->getMimeType(); + + // we don't know the mime type of our test file, so we'll just check, if any mimetype has been detected + self::assertInternalType('string', $detectedMimeType); + self::assertNotEmpty($detectedMimeType); + } + } + + /** + * @return void + */ + public function testToBytes(): void + { + $mimeDetector = $this->getInstance(); + $result = $mimeDetector->toBytes('php'); + + self::assertInternalType('array', $result); + self::assertEquals([112, 104, 112], $result); + } + + /** + * @return void + * @throws ReflectionException + * @throws MimeDetectorException + */ + public function testCheckString(): void + { + $mimeDetector = $this->getInstance(); + $mimeDetector->setFile(__FILE__); + $method = MimeDetectorTestUtil::getProtectedMethod($mimeDetector, 'checkString'); + $result = $method->invoke($mimeDetector, 'php', 2); + + self::assertInternalType('bool', $result); + self::assertTrue($result); + } + + /** + * Test, if `searchForBytes` returns -1, if a byte array is provided, that isn't in the cached byte array. + * + * @return void + * @throws MimeDetectorException + * @throws ReflectionException + */ + public function testSearchForBytesNegative(): void + { + $mimeDetector = $this->getInstance(); + $mimeDetector->setFile(__FILE__); + $method = MimeDetectorTestUtil::getProtectedMethod($mimeDetector, 'searchForBytes'); + $result = $method->invoke($mimeDetector, [0x66, 0x6F, 0x6F]); // foo + + self::assertInternalType('int', $result); + self::assertEquals(-1, $result); + } + + /** + * @return void + * @throws MimeDetectorException + * @throws ReflectionException + */ + public function testSearchForBytes(): void + { + $mimeDetector = $this->getInstance(); + $mimeDetector->setFile(__FILE__); + $method = MimeDetectorTestUtil::getProtectedMethod($mimeDetector, 'searchForBytes'); + $result = $method->invoke($mimeDetector, [0x70, 0x68, 0x70]); // php + + self::assertInternalType('int', $result); + self::assertEquals(2, $result); + } + + /** + * Test, if `checkForBytes` returns false, if an empty byte array is provided. + * + * @return void + * @throws MimeDetectorException + * @throws ReflectionException + */ + public function testCheckForBytesFalse(): void + { + $mimeDetector = $this->getInstance(); + $mimeDetector->setFile(__FILE__); + $method = MimeDetectorTestUtil::getProtectedMethod($mimeDetector, 'checkForBytes'); + $result = $method->invoke($mimeDetector, []); + + self::assertInternalType('bool', $result); + self::assertFalse($result); + } + + /** + * @return void + * @throws MimeDetectorException + * @throws ReflectionException + */ + public function testCheckForBytes(): void + { + $mimeDetector = $this->getInstance(); + $mimeDetector->setFile(__FILE__); + $method = MimeDetectorTestUtil::getProtectedMethod($mimeDetector, 'checkForBytes'); + $result = $method->invoke($mimeDetector, [0x70, 0x68, 0x70], 2); // php + + self::assertInternalType('bool', $result); + self::assertTrue($result); + } + + /** + * Test, if `createByteCache` returns early. + * + * @return void + * @throws MimeDetectorException + * @throws ReflectionException + */ + public function testCreateByteCacheNull(): void + { + $mimeDetector = $this->getInstance(); + $mimeDetector->setFile(__FILE__); + $method = MimeDetectorTestUtil::getProtectedMethod($mimeDetector, 'createByteCache'); + $result = $method->invoke($mimeDetector); + + self::assertNull($result); + } + + /** + * Test, if `createByteCache` throws a MimeDetectorException. + * + * @return void + * @throws MimeDetectorException + * @throws ReflectionException + */ + public function testCreateByteCacheException(): void + { + self::expectException(MimeDetectorException::class); + + $mimeDetector = $this->getInstance(); + $mimeDetector->setFile(__FILE__); + + MimeDetectorTestUtil::setPrivateProperty($mimeDetector, 'byteCache', []); + MimeDetectorTestUtil::setPrivateProperty($mimeDetector, 'file', ''); + MimeDetectorTestUtil::setPrivateProperty($mimeDetector, 'fileHash', ''); + + $method = MimeDetectorTestUtil::getProtectedMethod($mimeDetector, 'createByteCache'); + $method->invoke($mimeDetector); + } + + /** + * Returns an array of all existing test files and their corresponding CRC32b hashes. + * + * @return array + */ + public function provideTestFiles(): array + { + $files = []; + + foreach (new DirectoryIterator(__DIR__ . '/fixtures') as $file) { + if ($file->isFile() && $file->getBasename() !== '.git') { + $files[$file->getBasename()] = [ + 'file' => $file->getPathname(), + 'hash' => hash_file('crc32b', $file->getPathname()), + 'ext' => $file->getExtension() + ]; + } + } + + return [[$files]]; + } +} diff --git a/tests/SoftCreatR/MimeDetector/MimeDetectorTestUtil.php b/tests/SoftCreatR/MimeDetector/MimeDetectorTestUtil.php new file mode 100644 index 0000000..f8f919b --- /dev/null +++ b/tests/SoftCreatR/MimeDetector/MimeDetectorTestUtil.php @@ -0,0 +1,125 @@ +hasMethod($methodName)) { + throw new ReflectionException('Method ' . $methodName . ' is not defined.'); + } + + $method = $class->getMethod($methodName); + + if (!$method->isPrivate()) { + throw new ReflectionException('Method ' . $methodName . ' is not private.'); + } + + $method->setAccessible(true); + + return $method; + } + + /** + * Returns a protected method for testing purposes. + * + * @param MimeDetector $obj + * @param string $methodName + * @return ReflectionMethod + * @throws ReflectionException + */ + public static function getProtectedMethod(MimeDetector $obj, string $methodName): ReflectionMethod + { + $class = new ReflectionClass($obj); + + if (!$class->hasMethod($methodName)) { + throw new ReflectionException('Method ' . $methodName . ' is not defined.'); + } + + $method = $class->getMethod($methodName); + + if (!$method->isProtected()) { + throw new ReflectionException('Method ' . $methodName . ' is not protected.'); + } + + $method->setAccessible(true); + + return $method; + } + + /** + * Updates a protected property for testing purposes. + * + * @param MimeDetector $obj + * @param string $propertyName + * @param mixed $value + * @return void + * @throws ReflectionException + */ + public static function setProtectedProperty(MimeDetector $obj, string $propertyName, $value = null): void + { + $class = new ReflectionClass($obj); + + if (!$class->hasProperty($propertyName)) { + throw new ReflectionException('Property ' . $propertyName . ' is not defined.'); + } + + $property = $class->getProperty($propertyName); + + if (!$property->isProtected()) { + throw new ReflectionException('Property ' . $propertyName . ' is not protected.'); + } + + $property->setAccessible(true); + $property->setValue($obj, $value); + $property->setAccessible(false); + } + + /** + * Updates a protected property for testing purposes. + * + * @param MimeDetector $obj + * @param string $propertyName + * @param mixed $value + * @return void + * @throws ReflectionException + */ + public static function setPrivateProperty(MimeDetector $obj, string $propertyName, $value = null): void + { + $class = new ReflectionClass($obj); + + if (!$class->hasProperty($propertyName)) { + throw new ReflectionException('Property ' . $propertyName . ' is not defined.'); + } + + $property = $class->getProperty($propertyName); + + if (!$property->isPrivate()) { + throw new ReflectionException('Property ' . $propertyName . ' is not private.'); + } + + $property->setAccessible(true); + $property->setValue($obj, $value); + $property->setAccessible(false); + } +} diff --git a/tests/SoftCreatR/MimeDetector/fixtures b/tests/SoftCreatR/MimeDetector/fixtures new file mode 160000 index 0000000..14379b2 --- /dev/null +++ b/tests/SoftCreatR/MimeDetector/fixtures @@ -0,0 +1 @@ +Subproject commit 14379b244c037b372285e12cdbea51074c51c05e