00001 <?php 00025 require_once 'Zend/Validate/Abstract.php'; 00026 00035 class Zend_Validate_File_Upload extends Zend_Validate_Abstract 00036 { 00040 const INI_SIZE = 'fileUploadErrorIniSize'; 00041 const FORM_SIZE = 'fileUploadErrorFormSize'; 00042 const PARTIAL = 'fileUploadErrorPartial'; 00043 const NO_FILE = 'fileUploadErrorNoFile'; 00044 const NO_TMP_DIR = 'fileUploadErrorNoTmpDir'; 00045 const CANT_WRITE = 'fileUploadErrorCantWrite'; 00046 const EXTENSION = 'fileUploadErrorExtension'; 00047 const ATTACK = 'fileUploadErrorAttack'; 00048 const FILE_NOT_FOUND = 'fileUploadErrorFileNotFound'; 00049 const UNKNOWN = 'fileUploadErrorUnknown'; 00055 protected $_messageTemplates = array( 00056 self::INI_SIZE => "The file '%value%' exceeds the defined ini size", 00057 self::FORM_SIZE => "The file '%value%' exceeds the defined form size", 00058 self::PARTIAL => "The file '%value%' was only partially uploaded", 00059 self::NO_FILE => "The file '%value%' was not uploaded", 00060 self::NO_TMP_DIR => "No temporary directory was found for the file '%value%'", 00061 self::CANT_WRITE => "The file '%value%' can't be written", 00062 self::EXTENSION => "The extension returned an error while uploading the file '%value%'", 00063 self::ATTACK => "The file '%value%' was illegal uploaded, possible attack", 00064 self::FILE_NOT_FOUND => "The file '%value%' was not found", 00065 self::UNKNOWN => "Unknown error while uploading the file '%value%'" 00066 ); 00067 00072 protected $_files = array(); 00073 00084 public function __construct($files = array()) 00085 { 00086 if ($files instanceof Zend_Config) { 00087 $files = $files->toArray(); 00088 } 00089 00090 $this->setFiles($files); 00091 } 00092 00100 public function getFiles($file = null) 00101 { 00102 if ($file !== null) { 00103 $return = array(); 00104 foreach ($this->_files as $name => $content) { 00105 if ($name === $file) { 00106 $return[$file] = $this->_files[$name]; 00107 } 00108 00109 if ($content['name'] === $file) { 00110 $return[$name] = $this->_files[$name]; 00111 } 00112 } 00113 00114 if (count($return) === 0) { 00115 require_once 'Zend/Validate/Exception.php'; 00116 throw new Zend_Validate_Exception("The file '$file' was not found"); 00117 } 00118 00119 return $return; 00120 } 00121 00122 return $this->_files; 00123 } 00124 00131 public function setFiles($files = array()) 00132 { 00133 if (count($files) === 0) { 00134 $this->_files = $_FILES; 00135 } else { 00136 $this->_files = $files; 00137 } 00138 00139 foreach($this->_files as $file => $content) { 00140 if (!isset($content['error'])) { 00141 unset($this->_files[$file]); 00142 } 00143 } 00144 00145 return $this; 00146 } 00147 00157 public function isValid($value, $file = null) 00158 { 00159 if (array_key_exists($value, $this->_files)) { 00160 $files[$value] = $this->_files[$value]; 00161 } else { 00162 foreach ($this->_files as $file => $content) { 00163 if (isset($content['name']) && ($content['name'] === $value)) { 00164 $files[$file] = $this->_files[$file]; 00165 } 00166 00167 if (isset($content['tmp_name']) && ($content['tmp_name'] === $value)) { 00168 $files[$file] = $this->_files[$file]; 00169 } 00170 } 00171 } 00172 00173 if (empty($files)) { 00174 return $this->_throw($file, self::FILE_NOT_FOUND); 00175 } 00176 00177 foreach ($files as $file => $content) { 00178 $this->_value = $file; 00179 switch($content['error']) { 00180 case 0: 00181 if (!is_uploaded_file($content['tmp_name'])) { 00182 $this->_throw($file, self::ATTACK); 00183 } 00184 break; 00185 00186 case 1: 00187 $this->_throw($file, self::INI_SIZE); 00188 break; 00189 00190 case 2: 00191 $this->_throw($file, self::FORM_SIZE); 00192 break; 00193 00194 case 3: 00195 $this->_throw($file, self::PARTIAL); 00196 break; 00197 00198 case 4: 00199 $this->_throw($file, self::NO_FILE); 00200 break; 00201 00202 case 6: 00203 $this->_throw($file, self::NO_TMP_DIR); 00204 break; 00205 00206 case 7: 00207 $this->_throw($file, self::CANT_WRITE); 00208 break; 00209 00210 case 8: 00211 $this->_throw($file, self::EXTENSION); 00212 break; 00213 00214 default: 00215 $this->_throw($file, self::UNKNOWN); 00216 break; 00217 } 00218 } 00219 00220 if (count($this->_messages) > 0) { 00221 return false; 00222 } else { 00223 return true; 00224 } 00225 } 00226 00234 protected function _throw($file, $errorType) 00235 { 00236 if ($file !== null) { 00237 if (is_array($file) and !empty($file['name'])) { 00238 $this->_value = $file['name']; 00239 } 00240 } 00241 00242 $this->_error($errorType); 00243 return false; 00244 } 00245 }