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_Abstract 0024 */ 0025 // require_once 'Zend/Validate/Abstract.php'; 0026 0027 /** 0028 * Validator for the 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_Hash extends Zend_Validate_Abstract 0036 { 0037 /** 0038 * @const string Error constants 0039 */ 0040 const DOES_NOT_MATCH = 'fileHashDoesNotMatch'; 0041 const NOT_DETECTED = 'fileHashHashNotDetected'; 0042 const NOT_FOUND = 'fileHashNotFound'; 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 hashes", 0049 self::NOT_DETECTED => "A 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 $options 0064 * @throws Zend_Validate_Exception 0065 */ 0066 public function __construct($options) 0067 { 0068 if ($options instanceof Zend_Config) { 0069 $options = $options->toArray(); 0070 } elseif (is_scalar($options)) { 0071 $options = array('hash1' => $options); 0072 } elseif (!is_array($options)) { 0073 // require_once 'Zend/Validate/Exception.php'; 0074 throw new Zend_Validate_Exception('Invalid options to validator provided'); 0075 } 0076 0077 if (1 < func_num_args()) { 0078 $options['algorithm'] = func_get_arg(1); 0079 } 0080 0081 $this->setHash($options); 0082 } 0083 0084 /** 0085 * Returns the set hash values as array, the hash as key and the algorithm the value 0086 * 0087 * @return array 0088 */ 0089 public function getHash() 0090 { 0091 return $this->_hash; 0092 } 0093 0094 /** 0095 * Sets the hash for one or multiple files 0096 * 0097 * @param string|array $options 0098 * @return Zend_Validate_File_Hash Provides a fluent interface 0099 */ 0100 public function setHash($options) 0101 { 0102 $this->_hash = null; 0103 $this->addHash($options); 0104 0105 return $this; 0106 } 0107 0108 /** 0109 * Adds the hash for one or multiple files 0110 * 0111 * @param string|array $options 0112 * @throws Zend_Validate_Exception 0113 * @return Zend_Validate_File_Hash Provides a fluent interface 0114 */ 0115 public function addHash($options) 0116 { 0117 if (is_string($options)) { 0118 $options = array($options); 0119 } else if (!is_array($options)) { 0120 // require_once 'Zend/Validate/Exception.php'; 0121 throw new Zend_Validate_Exception("False parameter given"); 0122 } 0123 0124 $known = hash_algos(); 0125 if (!isset($options['algorithm'])) { 0126 $algorithm = 'crc32'; 0127 } else { 0128 $algorithm = $options['algorithm']; 0129 unset($options['algorithm']); 0130 } 0131 0132 if (!in_array($algorithm, $known)) { 0133 // require_once 'Zend/Validate/Exception.php'; 0134 throw new Zend_Validate_Exception("Unknown algorithm '{$algorithm}'"); 0135 } 0136 0137 foreach ($options as $value) { 0138 $this->_hash[$value] = $algorithm; 0139 } 0140 0141 return $this; 0142 } 0143 0144 /** 0145 * Defined by Zend_Validate_Interface 0146 * 0147 * Returns true if and only if the given file confirms the set hash 0148 * 0149 * @param string $value Filename to check for hash 0150 * @param array $file File data from Zend_File_Transfer 0151 * @return boolean 0152 */ 0153 public function isValid($value, $file = null) 0154 { 0155 // Is file readable ? 0156 // require_once 'Zend/Loader.php'; 0157 if (!Zend_Loader::isReadable($value)) { 0158 return $this->_throw($file, self::NOT_FOUND); 0159 } 0160 0161 $algos = array_unique(array_values($this->_hash)); 0162 $hashes = array_unique(array_keys($this->_hash)); 0163 foreach ($algos as $algorithm) { 0164 $filehash = hash_file($algorithm, $value); 0165 if ($filehash === false) { 0166 return $this->_throw($file, self::NOT_DETECTED); 0167 } 0168 0169 foreach($hashes as $hash) { 0170 if ($filehash === $hash) { 0171 return true; 0172 } 0173 } 0174 } 0175 0176 return $this->_throw($file, self::DOES_NOT_MATCH); 0177 } 0178 0179 /** 0180 * Throws an error of the given type 0181 * 0182 * @param string $file 0183 * @param string $errorType 0184 * @return false 0185 */ 0186 protected function _throw($file, $errorType) 0187 { 0188 if ($file !== null) { 0189 $this->_value = $file['name']; 0190 } 0191 0192 $this->_error($errorType); 0193 return false; 0194 } 0195 }