/vendor/zendframework/zend-validator/src/File/Upload.php

https://github.com/tmccormi/openemr · PHP · 237 lines · 216 code · 4 blank · 17 comment · 1 complexity · 3b0482c70630d9a3710b0d0fb1829ff5 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Validator\File;
  10. use Countable;
  11. use Zend\Validator\AbstractValidator;
  12. use Zend\Validator\Exception;
  13. /**
  14. * Validator for the maximum size of a file up to a max of 2GB
  15. *
  16. */
  17. class Upload extends AbstractValidator
  18. {
  19. /**
  20. * @const string Error constants
  21. */
  22. const INI_SIZE = 'fileUploadErrorIniSize';
  23. const FORM_SIZE = 'fileUploadErrorFormSize';
  24. const PARTIAL = 'fileUploadErrorPartial';
  25. const NO_FILE = 'fileUploadErrorNoFile';
  26. const NO_TMP_DIR = 'fileUploadErrorNoTmpDir';
  27. const CANT_WRITE = 'fileUploadErrorCantWrite';
  28. const EXTENSION = 'fileUploadErrorExtension';
  29. const ATTACK = 'fileUploadErrorAttack';
  30. const FILE_NOT_FOUND = 'fileUploadErrorFileNotFound';
  31. const UNKNOWN = 'fileUploadErrorUnknown';
  32. /**
  33. * @var array Error message templates
  34. */
  35. protected $messageTemplates = [
  36. self::INI_SIZE => "File '%value%' exceeds the defined ini size",
  37. self::FORM_SIZE => "File '%value%' exceeds the defined form size",
  38. self::PARTIAL => "File '%value%' was only partially uploaded",
  39. self::NO_FILE => "File '%value%' was not uploaded",
  40. self::NO_TMP_DIR => "No temporary directory was found for file '%value%'",
  41. self::CANT_WRITE => "File '%value%' can't be written",
  42. self::EXTENSION => "A PHP extension returned an error while uploading the file '%value%'",
  43. self::ATTACK => "File '%value%' was illegally uploaded. This could be a possible attack",
  44. self::FILE_NOT_FOUND => "File '%value%' was not found",
  45. self::UNKNOWN => "Unknown error while uploading file '%value%'"
  46. ];
  47. protected $options = [
  48. 'files' => [],
  49. ];
  50. /**
  51. * Sets validator options
  52. *
  53. * The array $files must be given in syntax of Zend\File\Transfer\Transfer to be checked
  54. * If no files are given the $_FILES array will be used automatically.
  55. * NOTE: This validator will only work with HTTP POST uploads!
  56. *
  57. * @param array|\Traversable $options Array of files in syntax of \Zend\File\Transfer\Transfer
  58. */
  59. public function __construct($options = [])
  60. {
  61. if (is_array($options) && ! array_key_exists('files', $options)) {
  62. $options = ['files' => $options];
  63. }
  64. parent::__construct($options);
  65. }
  66. /**
  67. * Returns the array of set files
  68. *
  69. * @param string $file (Optional) The file to return in detail
  70. * @return array
  71. * @throws Exception\InvalidArgumentException If file is not found
  72. */
  73. public function getFiles($file = null)
  74. {
  75. if ($file !== null) {
  76. $return = [];
  77. foreach ($this->options['files'] as $name => $content) {
  78. if ($name === $file) {
  79. $return[$file] = $this->options['files'][$name];
  80. }
  81. if ($content['name'] === $file) {
  82. $return[$name] = $this->options['files'][$name];
  83. }
  84. }
  85. if (count($return) === 0) {
  86. throw new Exception\InvalidArgumentException("The file '$file' was not found");
  87. }
  88. return $return;
  89. }
  90. return $this->options['files'];
  91. }
  92. /**
  93. * Sets the files to be checked
  94. *
  95. * @param array $files The files to check in syntax of \Zend\File\Transfer\Transfer
  96. * @return Upload Provides a fluent interface
  97. */
  98. public function setFiles($files = [])
  99. {
  100. if (null === $files
  101. || ((is_array($files) || $files instanceof Countable)
  102. && count($files) === 0)
  103. ) {
  104. $this->options['files'] = $_FILES;
  105. } else {
  106. $this->options['files'] = $files;
  107. }
  108. if ($this->options['files'] === null) {
  109. $this->options['files'] = [];
  110. }
  111. foreach ($this->options['files'] as $file => $content) {
  112. if (! isset($content['error'])) {
  113. unset($this->options['files'][$file]);
  114. }
  115. }
  116. return $this;
  117. }
  118. /**
  119. * Returns true if and only if the file was uploaded without errors
  120. *
  121. * @param string $value Single file to check for upload errors, when giving null the $_FILES array
  122. * from initialization will be used
  123. * @param mixed $file
  124. * @return bool
  125. */
  126. public function isValid($value, $file = null)
  127. {
  128. $files = [];
  129. $this->setValue($value);
  130. if (array_key_exists($value, $this->getFiles())) {
  131. $files = array_merge($files, $this->getFiles($value));
  132. } else {
  133. foreach ($this->getFiles() as $file => $content) {
  134. if (isset($content['name']) && ($content['name'] === $value)) {
  135. $files = array_merge($files, $this->getFiles($file));
  136. }
  137. if (isset($content['tmp_name']) && ($content['tmp_name'] === $value)) {
  138. $files = array_merge($files, $this->getFiles($file));
  139. }
  140. }
  141. }
  142. if (empty($files)) {
  143. return $this->throwError($file, self::FILE_NOT_FOUND);
  144. }
  145. foreach ($files as $file => $content) {
  146. $this->value = $file;
  147. switch ($content['error']) {
  148. case 0:
  149. if (! is_uploaded_file($content['tmp_name'])) {
  150. $this->throwError($content, self::ATTACK);
  151. }
  152. break;
  153. case 1:
  154. $this->throwError($content, self::INI_SIZE);
  155. break;
  156. case 2:
  157. $this->throwError($content, self::FORM_SIZE);
  158. break;
  159. case 3:
  160. $this->throwError($content, self::PARTIAL);
  161. break;
  162. case 4:
  163. $this->throwError($content, self::NO_FILE);
  164. break;
  165. case 6:
  166. $this->throwError($content, self::NO_TMP_DIR);
  167. break;
  168. case 7:
  169. $this->throwError($content, self::CANT_WRITE);
  170. break;
  171. case 8:
  172. $this->throwError($content, self::EXTENSION);
  173. break;
  174. default:
  175. $this->throwError($content, self::UNKNOWN);
  176. break;
  177. }
  178. }
  179. if (count($this->getMessages()) > 0) {
  180. return false;
  181. }
  182. return true;
  183. }
  184. /**
  185. * Throws an error of the given type
  186. *
  187. * @param string $file
  188. * @param string $errorType
  189. * @return false
  190. */
  191. protected function throwError($file, $errorType)
  192. {
  193. if ($file !== null) {
  194. if (is_array($file)) {
  195. if (array_key_exists('name', $file)) {
  196. $this->value = $file['name'];
  197. }
  198. } elseif (is_string($file)) {
  199. $this->value = $file;
  200. }
  201. }
  202. $this->error($errorType);
  203. return false;
  204. }
  205. }