Drupal investigation

File.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Constraints;
  11. use Symfony\Component\Validator\Constraint;
  12. use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
  13. /**
  14. * @Annotation
  15. * @Target({"PROPERTY", "METHOD", "ANNOTATION"})
  16. *
  17. * @author Bernhard Schussek <bschussek@gmail.com>
  18. */
  19. class File extends Constraint
  20. {
  21. // Check the Image constraint for clashes if adding new constants here
  22. const NOT_FOUND_ERROR = 'd2a3fb6e-7ddc-4210-8fbf-2ab345ce1998';
  23. const NOT_READABLE_ERROR = 'c20c92a4-5bfa-4202-9477-28e800e0f6ff';
  24. const EMPTY_ERROR = '5d743385-9775-4aa5-8ff5-495fb1e60137';
  25. const TOO_LARGE_ERROR = 'df8637af-d466-48c6-a59d-e7126250a654';
  26. const INVALID_MIME_TYPE_ERROR = '744f00bc-4389-4c74-92de-9a43cde55534';
  27. protected static $errorNames = array(
  28. self::NOT_FOUND_ERROR => 'NOT_FOUND_ERROR',
  29. self::NOT_READABLE_ERROR => 'NOT_READABLE_ERROR',
  30. self::EMPTY_ERROR => 'EMPTY_ERROR',
  31. self::TOO_LARGE_ERROR => 'TOO_LARGE_ERROR',
  32. self::INVALID_MIME_TYPE_ERROR => 'INVALID_MIME_TYPE_ERROR',
  33. );
  34. public $binaryFormat;
  35. public $mimeTypes = array();
  36. public $notFoundMessage = 'The file could not be found.';
  37. public $notReadableMessage = 'The file is not readable.';
  38. public $maxSizeMessage = 'The file is too large ({{ size }} {{ suffix }}). Allowed maximum size is {{ limit }} {{ suffix }}.';
  39. public $mimeTypesMessage = 'The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}.';
  40. public $disallowEmptyMessage = 'An empty file is not allowed.';
  41. public $uploadIniSizeErrorMessage = 'The file is too large. Allowed maximum size is {{ limit }} {{ suffix }}.';
  42. public $uploadFormSizeErrorMessage = 'The file is too large.';
  43. public $uploadPartialErrorMessage = 'The file was only partially uploaded.';
  44. public $uploadNoFileErrorMessage = 'No file was uploaded.';
  45. public $uploadNoTmpDirErrorMessage = 'No temporary folder was configured in php.ini.';
  46. public $uploadCantWriteErrorMessage = 'Cannot write temporary file to disk.';
  47. public $uploadExtensionErrorMessage = 'A PHP extension caused the upload to fail.';
  48. public $uploadErrorMessage = 'The file could not be uploaded.';
  49. protected $maxSize;
  50. public function __construct($options = null)
  51. {
  52. parent::__construct($options);
  53. if (null !== $this->maxSize) {
  54. $this->normalizeBinaryFormat($this->maxSize);
  55. }
  56. }
  57. public function __set($option, $value)
  58. {
  59. if ('maxSize' === $option) {
  60. $this->normalizeBinaryFormat($value);
  61. return;
  62. }
  63. parent::__set($option, $value);
  64. }
  65. public function __get($option)
  66. {
  67. if ('maxSize' === $option) {
  68. return $this->maxSize;
  69. }
  70. return parent::__get($option);
  71. }
  72. private function normalizeBinaryFormat($maxSize)
  73. {
  74. $sizeInt = (int) $maxSize;
  75. if (ctype_digit((string) $maxSize)) {
  76. $this->maxSize = $sizeInt;
  77. $this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
  78. } elseif (preg_match('/^\d++k$/i', $maxSize)) {
  79. $this->maxSize = $sizeInt * 1000;
  80. $this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
  81. } elseif (preg_match('/^\d++M$/i', $maxSize)) {
  82. $this->maxSize = $sizeInt * 1000000;
  83. $this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
  84. } elseif (preg_match('/^\d++Ki$/i', $maxSize)) {
  85. $this->maxSize = $sizeInt << 10;
  86. $this->binaryFormat = null === $this->binaryFormat ? true : $this->binaryFormat;
  87. } elseif (preg_match('/^\d++Mi$/i', $maxSize)) {
  88. $this->maxSize = $sizeInt << 20;
  89. $this->binaryFormat = null === $this->binaryFormat ? true : $this->binaryFormat;
  90. } else {
  91. throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $this->maxSize));
  92. }
  93. }
  94. }