00001 <?php
00025 require_once 'Zend/Validate/File/Hash.php';
00026
00035 class Zend_Validate_File_Sha1 extends Zend_Validate_File_Hash
00036 {
00040 const DOES_NOT_MATCH = 'fileSha1DoesNotMatch';
00041 const NOT_DETECTED = 'fileSha1NotDetected';
00042 const NOT_FOUND = 'fileSha1NotFound';
00043
00047 protected $_messageTemplates = array(
00048 self::DOES_NOT_MATCH => "The file '%value%' does not match the given sha1 hashes",
00049 self::NOT_DETECTED => "There was no sha1 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->setHash($options);
00080 }
00081
00087 public function getSha1()
00088 {
00089 return $this->getHash();
00090 }
00091
00098 public function setHash($options)
00099 {
00100 if (!is_array($options)) {
00101 $options = (array) $options;
00102 }
00103
00104 $options['algorithm'] = 'sha1';
00105 parent::setHash($options);
00106 return $this;
00107 }
00108
00115 public function setSha1($options)
00116 {
00117 $this->setHash($options);
00118 return $this;
00119 }
00120
00127 public function addHash($options)
00128 {
00129 if (!is_array($options)) {
00130 $options = (array) $options;
00131 }
00132
00133 $options['algorithm'] = 'sha1';
00134 parent::addHash($options);
00135 return $this;
00136 }
00137
00144 public function addSha1($options)
00145 {
00146 $this->addHash($options);
00147 return $this;
00148 }
00149
00159 public function isValid($value, $file = null)
00160 {
00161
00162 require_once 'Zend/Loader.php';
00163 if (!Zend_Loader::isReadable($value)) {
00164 return $this->_throw($file, self::NOT_FOUND);
00165 }
00166
00167 $hashes = array_unique(array_keys($this->_hash));
00168 $filehash = hash_file('sha1', $value);
00169 if ($filehash === false) {
00170 return $this->_throw($file, self::NOT_DETECTED);
00171 }
00172
00173 foreach ($hashes as $hash) {
00174 if ($filehash === $hash) {
00175 return true;
00176 }
00177 }
00178
00179 return $this->_throw($file, self::DOES_NOT_MATCH);
00180 }
00181 }