00001 <?php
00025 require_once 'Zend/Validate/File/Hash.php';
00026
00035 class Zend_Validate_File_Md5 extends Zend_Validate_File_Hash
00036 {
00040 const DOES_NOT_MATCH = 'fileMd5DoesNotMatch';
00041 const NOT_DETECTED = 'fileMd5NotDetected';
00042 const NOT_FOUND = 'fileMd5NotFound';
00043
00047 protected $_messageTemplates = array(
00048 self::DOES_NOT_MATCH => "The file '%value%' does not match the given md5 hashes",
00049 self::NOT_DETECTED => "There was no md5 hash detected for the given file",
00050 self::NOT_FOUND => "The file '%value%' could not be found"
00051 );
00052
00058 protected $_hash;
00059
00068 public function __construct($options)
00069 {
00070 if ($options instanceof Zend_Config) {
00071 $options = $options->toArray();
00072 } elseif (is_scalar($options)) {
00073 $options = array('hash1' => $options);
00074 } elseif (!is_array($options)) {
00075 require_once 'Zend/Validate/Exception.php';
00076 throw new Zend_Validate_Exception('Invalid options to validator provided');
00077 }
00078
00079 $this->setMd5($options);
00080 }
00081
00087 public function getMd5()
00088 {
00089 return $this->getHash();
00090 }
00091
00099 public function setHash($options)
00100 {
00101 if (!is_array($options)) {
00102 $options = (array) $options;
00103 }
00104
00105 $options['algorithm'] = 'md5';
00106 parent::setHash($options);
00107 return $this;
00108 }
00109
00116 public function setMd5($options)
00117 {
00118 $this->setHash($options);
00119 return $this;
00120 }
00121
00129 public function addHash($options)
00130 {
00131 if (!is_array($options)) {
00132 $options = (array) $options;
00133 }
00134
00135 $options['algorithm'] = 'md5';
00136 parent::addHash($options);
00137 return $this;
00138 }
00139
00146 public function addMd5($options)
00147 {
00148 $this->addHash($options);
00149 return $this;
00150 }
00151
00161 public function isValid($value, $file = null)
00162 {
00163
00164 require_once 'Zend/Loader.php';
00165 if (!Zend_Loader::isReadable($value)) {
00166 return $this->_throw($file, self::NOT_FOUND);
00167 }
00168
00169 $hashes = array_unique(array_keys($this->_hash));
00170 $filehash = hash_file('md5', $value);
00171 if ($filehash === false) {
00172 return $this->_throw($file, self::NOT_DETECTED);
00173 }
00174
00175 foreach($hashes as $hash) {
00176 if ($filehash === $hash) {
00177 return true;
00178 }
00179 }
00180
00181 return $this->_throw($file, self::DOES_NOT_MATCH);
00182 }
00183 }