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