File indexing completed on 2024-12-22 05:37:11

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category  Zend
0016  * @package   Zend_Validate
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license   http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version   $Id$
0020  */
0021 
0022 /**
0023  * @see Zend_Validate_File_Hash
0024  */
0025 // require_once 'Zend/Validate/File/Hash.php';
0026 
0027 /**
0028  * Validator for the crc32 hash of given files
0029  *
0030  * @category  Zend
0031  * @package   Zend_Validate
0032  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0033  * @license   http://framework.zend.com/license/new-bsd     New BSD License
0034  */
0035 class Zend_Validate_File_Crc32 extends Zend_Validate_File_Hash
0036 {
0037     /**
0038      * @const string Error constants
0039      */
0040     const DOES_NOT_MATCH = 'fileCrc32DoesNotMatch';
0041     const NOT_DETECTED   = 'fileCrc32NotDetected';
0042     const NOT_FOUND      = 'fileCrc32NotFound';
0043 
0044     /**
0045      * @var array Error message templates
0046      */
0047     protected $_messageTemplates = array(
0048         self::DOES_NOT_MATCH => "File '%value%' does not match the given crc32 hashes",
0049         self::NOT_DETECTED   => "A crc32 hash could not be evaluated for the given file",
0050         self::NOT_FOUND      => "File '%value%' is not readable or does not exist",
0051     );
0052 
0053     /**
0054      * Hash of the file
0055      *
0056      * @var string
0057      */
0058     protected $_hash;
0059 
0060     /**
0061      * Sets validator options
0062      *
0063      * @param  string|array|Zend_Config $options
0064      * @throws Zend_Validate_Exception
0065      * @return Zend_Validate_File_Crc32
0066      */
0067     public function __construct($options)
0068     {
0069         if ($options instanceof Zend_Config) {
0070             $options = $options->toArray();
0071         } elseif (is_scalar($options)) {
0072             $options = array('hash1' => $options);
0073         } elseif (!is_array($options)) {
0074             // require_once 'Zend/Validate/Exception.php';
0075             throw new Zend_Validate_Exception('Invalid options to validator provided');
0076         }
0077 
0078         $this->setCrc32($options);
0079     }
0080 
0081     /**
0082      * Returns all set crc32 hashes
0083      *
0084      * @return array
0085      */
0086     public function getCrc32()
0087     {
0088         return $this->getHash();
0089     }
0090 
0091     /**
0092      * Sets the crc32 hash for one or multiple files
0093      *
0094      * @param  string|array $options
0095      * @return Zend_Validate_File_Hash Provides a fluent interface
0096      */
0097     public function setHash($options)
0098     {
0099         if (!is_array($options)) {
0100             $options = array($options);
0101         }
0102 
0103         $options['algorithm'] = 'crc32';
0104         parent::setHash($options);
0105         return $this;
0106     }
0107 
0108     /**
0109      * Sets the crc32 hash for one or multiple files
0110      *
0111      * @param  string|array $options
0112      * @return Zend_Validate_File_Hash Provides a fluent interface
0113      */
0114     public function setCrc32($options)
0115     {
0116         $this->setHash($options);
0117         return $this;
0118     }
0119 
0120     /**
0121      * Adds the crc32 hash for one or multiple files
0122      *
0123      * @param  string|array $options
0124      * @return Zend_Validate_File_Hash Provides a fluent interface
0125      */
0126     public function addHash($options)
0127     {
0128         if (!is_array($options)) {
0129             $options = array($options);
0130         }
0131 
0132         $options['algorithm'] = 'crc32';
0133         parent::addHash($options);
0134         return $this;
0135     }
0136 
0137     /**
0138      * Adds the crc32 hash for one or multiple files
0139      *
0140      * @param  string|array $options
0141      * @return Zend_Validate_File_Hash Provides a fluent interface
0142      */
0143     public function addCrc32($options)
0144     {
0145         $this->addHash($options);
0146         return $this;
0147     }
0148 
0149     /**
0150      * Defined by Zend_Validate_Interface
0151      *
0152      * Returns true if and only if the given file confirms the set hash
0153      *
0154      * @param  string $value Filename to check for hash
0155      * @param  array  $file  File data from Zend_File_Transfer
0156      * @return boolean
0157      */
0158     public function isValid($value, $file = null)
0159     {
0160         // Is file readable ?
0161         // require_once 'Zend/Loader.php';
0162         if (!Zend_Loader::isReadable($value)) {
0163             return $this->_throw($file, self::NOT_FOUND);
0164         }
0165 
0166         $hashes = array_unique(array_keys($this->_hash));
0167         $filehash = hash_file('crc32', $value);
0168         if ($filehash === false) {
0169             return $this->_throw($file, self::NOT_DETECTED);
0170         }
0171 
0172         foreach($hashes as $hash) {
0173             if ($filehash === $hash) {
0174                 return true;
0175             }
0176         }
0177 
0178         return $this->_throw($file, self::DOES_NOT_MATCH);
0179     }
0180 }