IsImage.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Validate
  17. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Validate_File_MimeType
  23. */
  24. #require_once 'Zend/Validate/File/MimeType.php';
  25. /**
  26. * Validator which checks if the file already exists in the directory
  27. *
  28. * @category Zend
  29. * @package Zend_Validate
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Validate_File_IsImage extends Zend_Validate_File_MimeType
  34. {
  35. /**
  36. * @const string Error constants
  37. */
  38. const FALSE_TYPE = 'fileIsImageFalseType';
  39. const NOT_DETECTED = 'fileIsImageNotDetected';
  40. const NOT_READABLE = 'fileIsImageNotReadable';
  41. /**
  42. * @var array Error message templates
  43. */
  44. protected $_messageTemplates = array(
  45. self::FALSE_TYPE => "File '%value%' is no image, '%type%' detected",
  46. self::NOT_DETECTED => "The mimetype of file '%value%' could not be detected",
  47. self::NOT_READABLE => "File '%value%' is not readable or does not exist",
  48. );
  49. /**
  50. * Sets validator options
  51. *
  52. * @param string|array|Zend_Config $mimetype
  53. */
  54. public function __construct($mimetype = array())
  55. {
  56. if ($mimetype instanceof Zend_Config) {
  57. $mimetype = $mimetype->toArray();
  58. }
  59. $temp = array();
  60. // http://de.wikipedia.org/wiki/Liste_von_Dateiendungen
  61. // http://www.iana.org/assignments/media-types/image/
  62. $default = array(
  63. 'application/cdf',
  64. 'application/dicom',
  65. 'application/fractals',
  66. 'application/postscript',
  67. 'application/vnd.hp-hpgl',
  68. 'application/vnd.oasis.opendocument.graphics',
  69. 'application/x-cdf',
  70. 'application/x-cmu-raster',
  71. 'application/x-ima',
  72. 'application/x-inventor',
  73. 'application/x-koan',
  74. 'application/x-portable-anymap',
  75. 'application/x-world-x-3dmf',
  76. 'image/bmp',
  77. 'image/c',
  78. 'image/cgm',
  79. 'image/fif',
  80. 'image/gif',
  81. 'image/jpeg',
  82. 'image/jpm',
  83. 'image/jpx',
  84. 'image/jp2',
  85. 'image/naplps',
  86. 'image/pjpeg',
  87. 'image/png',
  88. 'image/svg',
  89. 'image/svg+xml',
  90. 'image/tiff',
  91. 'image/vnd.adobe.photoshop',
  92. 'image/vnd.djvu',
  93. 'image/vnd.fpx',
  94. 'image/vnd.net-fpx',
  95. 'image/x-cmu-raster',
  96. 'image/x-cmx',
  97. 'image/x-coreldraw',
  98. 'image/x-cpi',
  99. 'image/x-emf',
  100. 'image/x-ico',
  101. 'image/x-icon',
  102. 'image/x-jg',
  103. 'image/x-ms-bmp',
  104. 'image/x-niff',
  105. 'image/x-pict',
  106. 'image/x-pcx',
  107. 'image/x-portable-anymap',
  108. 'image/x-portable-bitmap',
  109. 'image/x-portable-greymap',
  110. 'image/x-portable-pixmap',
  111. 'image/x-quicktime',
  112. 'image/x-rgb',
  113. 'image/x-tiff',
  114. 'image/x-unknown',
  115. 'image/x-windows-bmp',
  116. 'image/x-xpmi',
  117. );
  118. if (is_array($mimetype)) {
  119. $temp = $mimetype;
  120. if (array_key_exists('magicfile', $temp)) {
  121. unset($temp['magicfile']);
  122. }
  123. if (array_key_exists('headerCheck', $temp)) {
  124. unset($temp['headerCheck']);
  125. }
  126. if (empty($temp)) {
  127. $mimetype += $default;
  128. }
  129. }
  130. if (empty($mimetype)) {
  131. $mimetype = $default;
  132. }
  133. parent::__construct($mimetype);
  134. }
  135. /**
  136. * Throws an error of the given type
  137. * Duplicates parent method due to OOP Problem with late static binding in PHP 5.2
  138. *
  139. * @param string $file
  140. * @param string $errorType
  141. * @return false
  142. */
  143. protected function _throw($file, $errorType)
  144. {
  145. $this->_value = $file['name'];
  146. switch($errorType) {
  147. case Zend_Validate_File_MimeType::FALSE_TYPE :
  148. $errorType = self::FALSE_TYPE;
  149. break;
  150. case Zend_Validate_File_MimeType::NOT_DETECTED :
  151. $errorType = self::NOT_DETECTED;
  152. break;
  153. case Zend_Validate_File_MimeType::NOT_READABLE :
  154. $errorType = self::NOT_READABLE;
  155. break;
  156. }
  157. $this->_error($errorType);
  158. return false;
  159. }
  160. }