Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Validator / File / Upload.php
blob24bc743dd37cb24cc5d39a2d507f2bb8583260f5
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Validator\File;
12 use Zend\Validator\AbstractValidator;
13 use Zend\Validator\Exception;
15 /**
16 * Validator for the maximum size of a file up to a max of 2GB
19 class Upload extends AbstractValidator
21 /**
22 * @const string Error constants
24 const INI_SIZE = 'fileUploadErrorIniSize';
25 const FORM_SIZE = 'fileUploadErrorFormSize';
26 const PARTIAL = 'fileUploadErrorPartial';
27 const NO_FILE = 'fileUploadErrorNoFile';
28 const NO_TMP_DIR = 'fileUploadErrorNoTmpDir';
29 const CANT_WRITE = 'fileUploadErrorCantWrite';
30 const EXTENSION = 'fileUploadErrorExtension';
31 const ATTACK = 'fileUploadErrorAttack';
32 const FILE_NOT_FOUND = 'fileUploadErrorFileNotFound';
33 const UNKNOWN = 'fileUploadErrorUnknown';
35 /**
36 * @var array Error message templates
38 protected $messageTemplates = array(
39 self::INI_SIZE => "File '%value%' exceeds the defined ini size",
40 self::FORM_SIZE => "File '%value%' exceeds the defined form size",
41 self::PARTIAL => "File '%value%' was only partially uploaded",
42 self::NO_FILE => "File '%value%' was not uploaded",
43 self::NO_TMP_DIR => "No temporary directory was found for file '%value%'",
44 self::CANT_WRITE => "File '%value%' can't be written",
45 self::EXTENSION => "A PHP extension returned an error while uploading the file '%value%'",
46 self::ATTACK => "File '%value%' was illegally uploaded. This could be a possible attack",
47 self::FILE_NOT_FOUND => "File '%value%' was not found",
48 self::UNKNOWN => "Unknown error while uploading file '%value%'"
51 protected $options = array(
52 'files' => array(),
55 /**
56 * Sets validator options
58 * The array $files must be given in syntax of Zend\File\Transfer\Transfer to be checked
59 * If no files are given the $_FILES array will be used automatically.
60 * NOTE: This validator will only work with HTTP POST uploads!
62 * @param array|\Traversable $options Array of files in syntax of \Zend\File\Transfer\Transfer
64 public function __construct($options = array())
66 if (is_array($options) && !array_key_exists('files', $options)) {
67 $options = array('files' => $options);
70 parent::__construct($options);
73 /**
74 * Returns the array of set files
76 * @param string $file (Optional) The file to return in detail
77 * @return array
78 * @throws Exception\InvalidArgumentException If file is not found
80 public function getFiles($file = null)
82 if ($file !== null) {
83 $return = array();
84 foreach ($this->options['files'] as $name => $content) {
85 if ($name === $file) {
86 $return[$file] = $this->options['files'][$name];
89 if ($content['name'] === $file) {
90 $return[$name] = $this->options['files'][$name];
94 if (count($return) === 0) {
95 throw new Exception\InvalidArgumentException("The file '$file' was not found");
98 return $return;
101 return $this->options['files'];
105 * Sets the files to be checked
107 * @param array $files The files to check in syntax of \Zend\File\Transfer\Transfer
108 * @return Upload Provides a fluent interface
110 public function setFiles($files = array())
112 if (count($files) === 0) {
113 $this->options['files'] = $_FILES;
114 } else {
115 $this->options['files'] = $files;
118 if ($this->options['files'] === NULL) {
119 $this->options['files'] = array();
122 foreach ($this->options['files'] as $file => $content) {
123 if (!isset($content['error'])) {
124 unset($this->options['files'][$file]);
128 return $this;
132 * Returns true if and only if the file was uploaded without errors
134 * @param string $value Single file to check for upload errors, when giving null the $_FILES array
135 * from initialization will be used
136 * @param mixed $file
137 * @return bool
139 public function isValid($value, $file = null)
141 $files = array();
142 $this->setValue($value);
143 if (array_key_exists($value, $this->getFiles())) {
144 $files = array_merge($files, $this->getFiles($value));
145 } else {
146 foreach ($this->getFiles() as $file => $content) {
147 if (isset($content['name']) && ($content['name'] === $value)) {
148 $files = array_merge($files, $this->getFiles($file));
151 if (isset($content['tmp_name']) && ($content['tmp_name'] === $value)) {
152 $files = array_merge($files, $this->getFiles($file));
157 if (empty($files)) {
158 return $this->throwError($file, self::FILE_NOT_FOUND);
161 foreach ($files as $file => $content) {
162 $this->value = $file;
163 switch ($content['error']) {
164 case 0:
165 if (!is_uploaded_file($content['tmp_name'])) {
166 $this->throwError($content, self::ATTACK);
168 break;
170 case 1:
171 $this->throwError($content, self::INI_SIZE);
172 break;
174 case 2:
175 $this->throwError($content, self::FORM_SIZE);
176 break;
178 case 3:
179 $this->throwError($content, self::PARTIAL);
180 break;
182 case 4:
183 $this->throwError($content, self::NO_FILE);
184 break;
186 case 6:
187 $this->throwError($content, self::NO_TMP_DIR);
188 break;
190 case 7:
191 $this->throwError($content, self::CANT_WRITE);
192 break;
194 case 8:
195 $this->throwError($content, self::EXTENSION);
196 break;
198 default:
199 $this->throwError($content, self::UNKNOWN);
200 break;
204 if (count($this->getMessages()) > 0) {
205 return false;
208 return true;
212 * Throws an error of the given type
214 * @param string $file
215 * @param string $errorType
216 * @return false
218 protected function throwError($file, $errorType)
220 if ($file !== null) {
221 if (is_array($file)) {
222 if (array_key_exists('name', $file)) {
223 $this->value = $file['name'];
225 } elseif (is_string($file)) {
226 $this->value = $file;
230 $this->error($errorType);
231 return false;