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: Extension.php 22668 2010-07-25 14:50:46Z thomas $ |
---|
20 | */ |
---|
21 | |
---|
22 | /** |
---|
23 | * @see Zend_Validate_Abstract |
---|
24 | */ |
---|
25 | require_once 'Zend/Validate/Abstract.php'; |
---|
26 | |
---|
27 | /** |
---|
28 | * Validator for the file extension 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_Extension extends Zend_Validate_Abstract |
---|
36 | { |
---|
37 | /** |
---|
38 | * @const string Error constants |
---|
39 | */ |
---|
40 | const FALSE_EXTENSION = 'fileExtensionFalse'; |
---|
41 | const NOT_FOUND = 'fileExtensionNotFound'; |
---|
42 | |
---|
43 | /** |
---|
44 | * @var array Error message templates |
---|
45 | */ |
---|
46 | protected $_messageTemplates = array( |
---|
47 | self::FALSE_EXTENSION => "File '%value%' has a false extension", |
---|
48 | self::NOT_FOUND => "File '%value%' is not readable or does not exist", |
---|
49 | ); |
---|
50 | |
---|
51 | /** |
---|
52 | * Internal list of extensions |
---|
53 | * @var string |
---|
54 | */ |
---|
55 | protected $_extension = ''; |
---|
56 | |
---|
57 | /** |
---|
58 | * Validate case sensitive |
---|
59 | * |
---|
60 | * @var boolean |
---|
61 | */ |
---|
62 | protected $_case = false; |
---|
63 | |
---|
64 | /** |
---|
65 | * @var array Error message template variables |
---|
66 | */ |
---|
67 | protected $_messageVariables = array( |
---|
68 | 'extension' => '_extension' |
---|
69 | ); |
---|
70 | |
---|
71 | /** |
---|
72 | * Sets validator options |
---|
73 | * |
---|
74 | * @param string|array|Zend_Config $options |
---|
75 | * @return void |
---|
76 | */ |
---|
77 | public function __construct($options) |
---|
78 | { |
---|
79 | if ($options instanceof Zend_Config) { |
---|
80 | $options = $options->toArray(); |
---|
81 | } |
---|
82 | |
---|
83 | if (1 < func_num_args()) { |
---|
84 | $case = func_get_arg(1); |
---|
85 | $this->setCase($case); |
---|
86 | } |
---|
87 | |
---|
88 | if (is_array($options) and isset($options['case'])) { |
---|
89 | $this->setCase($options['case']); |
---|
90 | unset($options['case']); |
---|
91 | } |
---|
92 | |
---|
93 | $this->setExtension($options); |
---|
94 | } |
---|
95 | |
---|
96 | /** |
---|
97 | * Returns the case option |
---|
98 | * |
---|
99 | * @return boolean |
---|
100 | */ |
---|
101 | public function getCase() |
---|
102 | { |
---|
103 | return $this->_case; |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | * Sets the case to use |
---|
108 | * |
---|
109 | * @param boolean $case |
---|
110 | * @return Zend_Validate_File_Extension Provides a fluent interface |
---|
111 | */ |
---|
112 | public function setCase($case) |
---|
113 | { |
---|
114 | $this->_case = (boolean) $case; |
---|
115 | return $this; |
---|
116 | } |
---|
117 | |
---|
118 | /** |
---|
119 | * Returns the set file extension |
---|
120 | * |
---|
121 | * @return array |
---|
122 | */ |
---|
123 | public function getExtension() |
---|
124 | { |
---|
125 | $extension = explode(',', $this->_extension); |
---|
126 | |
---|
127 | return $extension; |
---|
128 | } |
---|
129 | |
---|
130 | /** |
---|
131 | * Sets the file extensions |
---|
132 | * |
---|
133 | * @param string|array $extension The extensions to validate |
---|
134 | * @return Zend_Validate_File_Extension Provides a fluent interface |
---|
135 | */ |
---|
136 | public function setExtension($extension) |
---|
137 | { |
---|
138 | $this->_extension = null; |
---|
139 | $this->addExtension($extension); |
---|
140 | return $this; |
---|
141 | } |
---|
142 | |
---|
143 | /** |
---|
144 | * Adds the file extensions |
---|
145 | * |
---|
146 | * @param string|array $extension The extensions to add for validation |
---|
147 | * @return Zend_Validate_File_Extension Provides a fluent interface |
---|
148 | */ |
---|
149 | public function addExtension($extension) |
---|
150 | { |
---|
151 | $extensions = $this->getExtension(); |
---|
152 | if (is_string($extension)) { |
---|
153 | $extension = explode(',', $extension); |
---|
154 | } |
---|
155 | |
---|
156 | foreach ($extension as $content) { |
---|
157 | if (empty($content) || !is_string($content)) { |
---|
158 | continue; |
---|
159 | } |
---|
160 | |
---|
161 | $extensions[] = trim($content); |
---|
162 | } |
---|
163 | $extensions = array_unique($extensions); |
---|
164 | |
---|
165 | // Sanity check to ensure no empty values |
---|
166 | foreach ($extensions as $key => $ext) { |
---|
167 | if (empty($ext)) { |
---|
168 | unset($extensions[$key]); |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | $this->_extension = implode(',', $extensions); |
---|
173 | |
---|
174 | return $this; |
---|
175 | } |
---|
176 | |
---|
177 | /** |
---|
178 | * Defined by Zend_Validate_Interface |
---|
179 | * |
---|
180 | * Returns true if and only if the fileextension of $value is included in the |
---|
181 | * set extension list |
---|
182 | * |
---|
183 | * @param string $value Real file to check for extension |
---|
184 | * @param array $file File data from Zend_File_Transfer |
---|
185 | * @return boolean |
---|
186 | */ |
---|
187 | public function isValid($value, $file = null) |
---|
188 | { |
---|
189 | // Is file readable ? |
---|
190 | require_once 'Zend/Loader.php'; |
---|
191 | if (!Zend_Loader::isReadable($value)) { |
---|
192 | return $this->_throw($file, self::NOT_FOUND); |
---|
193 | } |
---|
194 | |
---|
195 | if ($file !== null) { |
---|
196 | $info['extension'] = substr($file['name'], strrpos($file['name'], '.') + 1); |
---|
197 | } else { |
---|
198 | $info = pathinfo($value); |
---|
199 | } |
---|
200 | |
---|
201 | $extensions = $this->getExtension(); |
---|
202 | |
---|
203 | if ($this->_case && (in_array($info['extension'], $extensions))) { |
---|
204 | return true; |
---|
205 | } else if (!$this->getCase()) { |
---|
206 | foreach ($extensions as $extension) { |
---|
207 | if (strtolower($extension) == strtolower($info['extension'])) { |
---|
208 | return true; |
---|
209 | } |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | return $this->_throw($file, self::FALSE_EXTENSION); |
---|
214 | } |
---|
215 | |
---|
216 | /** |
---|
217 | * Throws an error of the given type |
---|
218 | * |
---|
219 | * @param string $file |
---|
220 | * @param string $errorType |
---|
221 | * @return false |
---|
222 | */ |
---|
223 | protected function _throw($file, $errorType) |
---|
224 | { |
---|
225 | if (null !== $file) { |
---|
226 | $this->_value = $file['name']; |
---|
227 | } |
---|
228 | |
---|
229 | $this->_error($errorType); |
---|
230 | return false; |
---|
231 | } |
---|
232 | } |
---|