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 md5 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_Md5 extends Zend_Validate_File_Hash 0036 { 0037 /** 0038 * @const string Error constants 0039 */ 0040 const DOES_NOT_MATCH = 'fileMd5DoesNotMatch'; 0041 const NOT_DETECTED = 'fileMd5NotDetected'; 0042 const NOT_FOUND = 'fileMd5NotFound'; 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 md5 hashes", 0049 self::NOT_DETECTED => "A md5 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 * $hash is the hash we accept for the file $file 0064 * 0065 * @param string|array $options 0066 * @throws Zend_Validate_Exception 0067 * @return Zend_Validate_File_Md5 0068 */ 0069 public function __construct($options) 0070 { 0071 if ($options instanceof Zend_Config) { 0072 $options = $options->toArray(); 0073 } elseif (is_scalar($options)) { 0074 $options = array('hash1' => $options); 0075 } elseif (!is_array($options)) { 0076 // require_once 'Zend/Validate/Exception.php'; 0077 throw new Zend_Validate_Exception('Invalid options to validator provided'); 0078 } 0079 0080 $this->setMd5($options); 0081 } 0082 0083 /** 0084 * Returns all set md5 hashes 0085 * 0086 * @return array 0087 */ 0088 public function getMd5() 0089 { 0090 return $this->getHash(); 0091 } 0092 0093 /** 0094 * Sets the md5 hash for one or multiple files 0095 * 0096 * @param string|array $options 0097 * @return Zend_Validate_File_Hash Provides a fluent interface 0098 */ 0099 public function setHash($options) 0100 { 0101 if (!is_array($options)) { 0102 $options = (array) $options; 0103 } 0104 0105 $options['algorithm'] = 'md5'; 0106 parent::setHash($options); 0107 return $this; 0108 } 0109 0110 /** 0111 * Sets the md5 hash for one or multiple files 0112 * 0113 * @param string|array $options 0114 * @return Zend_Validate_File_Hash Provides a fluent interface 0115 */ 0116 public function setMd5($options) 0117 { 0118 $this->setHash($options); 0119 return $this; 0120 } 0121 0122 /** 0123 * Adds the md5 hash for one or multiple files 0124 * 0125 * @param string|array $options 0126 * @return Zend_Validate_File_Hash Provides a fluent interface 0127 */ 0128 public function addHash($options) 0129 { 0130 if (!is_array($options)) { 0131 $options = (array) $options; 0132 } 0133 0134 $options['algorithm'] = 'md5'; 0135 parent::addHash($options); 0136 return $this; 0137 } 0138 0139 /** 0140 * Adds the md5 hash for one or multiple files 0141 * 0142 * @param string|array $options 0143 * @return Zend_Validate_File_Hash Provides a fluent interface 0144 */ 0145 public function addMd5($options) 0146 { 0147 $this->addHash($options); 0148 return $this; 0149 } 0150 0151 /** 0152 * Defined by Zend_Validate_Interface 0153 * 0154 * Returns true if and only if the given file confirms the set hash 0155 * 0156 * @param string $value Filename to check for hash 0157 * @param array $file File data from Zend_File_Transfer 0158 * @return boolean 0159 */ 0160 public function isValid($value, $file = null) 0161 { 0162 // Is file readable ? 0163 // require_once 'Zend/Loader.php'; 0164 if (!Zend_Loader::isReadable($value)) { 0165 return $this->_throw($file, self::NOT_FOUND); 0166 } 0167 0168 $hashes = array_unique(array_keys($this->_hash)); 0169 $filehash = hash_file('md5', $value); 0170 if ($filehash === false) { 0171 return $this->_throw($file, self::NOT_DETECTED); 0172 } 0173 0174 foreach($hashes as $hash) { 0175 if ($filehash === $hash) { 0176 return true; 0177 } 0178 } 0179 0180 return $this->_throw($file, self::DOES_NOT_MATCH); 0181 } 0182 }