PageRenderTime 56ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/libraries/3rdparty/Zend/Validate/File/ExcludeMimeType.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 109 lines | 56 code | 11 blank | 42 comment | 12 complexity | 4124579b874e562d4daf342883b46fa8 MD5 | raw file
  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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: ExcludeMimeType.php 23775 2011-03-01 17:25:24Z ralph $
  20. */
  21. /**
  22. * @see Zend_Validate_File_MimeType
  23. */
  24. require_once 'Zend/Validate/File/MimeType.php';
  25. /**
  26. * Validator for the mime type of a file
  27. *
  28. * @category Zend
  29. * @package Zend_Validate
  30. * @copyright Copyright (c) 2005-2011 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_ExcludeMimeType extends Zend_Validate_File_MimeType
  34. {
  35. const FALSE_TYPE = 'fileExcludeMimeTypeFalse';
  36. const NOT_DETECTED = 'fileExcludeMimeTypeNotDetected';
  37. const NOT_READABLE = 'fileExcludeMimeTypeNotReadable';
  38. /**
  39. * Defined by Zend_Validate_Interface
  40. *
  41. * Returns true if the mimetype of the file does not matche the given ones. Also parts
  42. * of mimetypes can be checked. If you give for example "image" all image
  43. * mime types will not be accepted like "image/gif", "image/jpeg" and so on.
  44. *
  45. * @param string $value Real file to check for mimetype
  46. * @param array $file File data from Zend_File_Transfer
  47. * @return boolean
  48. */
  49. public function isValid($value, $file = null)
  50. {
  51. if ($file === null) {
  52. $file = array(
  53. 'type' => null,
  54. 'name' => $value
  55. );
  56. }
  57. // Is file readable ?
  58. require_once 'Zend/Loader.php';
  59. if (!Zend_Loader::isReadable($value)) {
  60. return $this->_throw($file, self::NOT_READABLE);
  61. }
  62. $mimefile = $this->getMagicFile();
  63. if (class_exists('finfo', false)) {
  64. $const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME;
  65. if (!empty($mimefile)) {
  66. $mime = new finfo($const, $mimefile);
  67. } else {
  68. $mime = new finfo($const);
  69. }
  70. if (!empty($mime)) {
  71. $this->_type = $mime->file($value);
  72. }
  73. unset($mime);
  74. }
  75. if (empty($this->_type)) {
  76. if (function_exists('mime_content_type') && ini_get('mime_magic.magicfile')) {
  77. $this->_type = mime_content_type($value);
  78. } elseif ($this->_headerCheck) {
  79. $this->_type = $file['type'];
  80. }
  81. }
  82. if (empty($this->_type)) {
  83. return $this->_throw($file, self::NOT_DETECTED);
  84. }
  85. $mimetype = $this->getMimeType(true);
  86. if (in_array($this->_type, $mimetype)) {
  87. return $this->_throw($file, self::FALSE_TYPE);
  88. }
  89. $types = explode('/', $this->_type);
  90. $types = array_merge($types, explode('-', $this->_type));
  91. foreach($mimetype as $mime) {
  92. if (in_array($mime, $types)) {
  93. return $this->_throw($file, self::FALSE_TYPE);
  94. }
  95. }
  96. return true;
  97. }
  98. }