Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Validator / File / Extension.php
blob6854762c310bac2bddadbd62f7c90ec6508cd60f
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Validator\File;
12 use Traversable;
13 use Zend\Stdlib\ArrayUtils;
14 use Zend\Validator\AbstractValidator;
15 use Zend\Validator\Exception;
16 /**
17 * Validator for the file extension of a file
19 class Extension extends AbstractValidator
21 /**
22 * @const string Error constants
24 const FALSE_EXTENSION = 'fileExtensionFalse';
25 const NOT_FOUND = 'fileExtensionNotFound';
27 /**
28 * @var array Error message templates
30 protected $messageTemplates = array(
31 self::FALSE_EXTENSION => "File has an incorrect extension",
32 self::NOT_FOUND => "File is not readable or does not exist",
35 /**
36 * Options for this validator
38 * @var array
40 protected $options = array(
41 'case' => false, // Validate case sensitive
42 'extension' => '', // List of extensions
45 /**
46 * @var array Error message template variables
48 protected $messageVariables = array(
49 'extension' => array('options' => 'extension'),
52 /**
53 * Sets validator options
55 * @param string|array|Traversable $options
57 public function __construct($options = null)
59 if ($options instanceof Traversable) {
60 $options = ArrayUtils::iteratorToArray($options);
63 $case = null;
64 if (1 < func_num_args()) {
65 $case = func_get_arg(1);
68 if (is_array($options)) {
69 if (isset($options['case'])) {
70 $case = $options['case'];
71 unset($options['case']);
74 if (!array_key_exists('extension', $options)) {
75 $options = array('extension' => $options);
77 } else {
78 $options = array('extension' => $options);
81 if ($case !== null) {
82 $options['case'] = $case;
85 parent::__construct($options);
88 /**
89 * Returns the case option
91 * @return bool
93 public function getCase()
95 return $this->options['case'];
98 /**
99 * Sets the case to use
101 * @param bool $case
102 * @return Extension Provides a fluent interface
104 public function setCase($case)
106 $this->options['case'] = (bool) $case;
107 return $this;
111 * Returns the set file extension
113 * @return array
115 public function getExtension()
117 $extension = explode(',', $this->options['extension']);
119 return $extension;
123 * Sets the file extensions
125 * @param string|array $extension The extensions to validate
126 * @return Extension Provides a fluent interface
128 public function setExtension($extension)
130 $this->options['extension'] = null;
131 $this->addExtension($extension);
132 return $this;
136 * Adds the file extensions
138 * @param string|array $extension The extensions to add for validation
139 * @return Extension Provides a fluent interface
141 public function addExtension($extension)
143 $extensions = $this->getExtension();
144 if (is_string($extension)) {
145 $extension = explode(',', $extension);
148 foreach ($extension as $content) {
149 if (empty($content) || !is_string($content)) {
150 continue;
153 $extensions[] = trim($content);
156 $extensions = array_unique($extensions);
158 // Sanity check to ensure no empty values
159 foreach ($extensions as $key => $ext) {
160 if (empty($ext)) {
161 unset($extensions[$key]);
165 $this->options['extension'] = implode(',', $extensions);
166 return $this;
170 * Returns true if and only if the file extension of $value is included in the
171 * set extension list
173 * @param string|array $value Real file to check for extension
174 * @param array $file File data from \Zend\File\Transfer\Transfer (optional)
175 * @return bool
177 public function isValid($value, $file = null)
179 if (is_string($value) && is_array($file)) {
180 // Legacy Zend\Transfer API support
181 $filename = $file['name'];
182 $file = $file['tmp_name'];
183 } elseif (is_array($value)) {
184 if (!isset($value['tmp_name']) || !isset($value['name'])) {
185 throw new Exception\InvalidArgumentException(
186 'Value array must be in $_FILES format'
189 $file = $value['tmp_name'];
190 $filename = $value['name'];
191 } else {
192 $file = $value;
193 $filename = basename($file);
195 $this->setValue($filename);
197 // Is file readable ?
198 if (false === stream_resolve_include_path($file)) {
199 $this->error(self::NOT_FOUND);
200 return false;
203 $extension = substr($filename, strrpos($filename, '.') + 1);
204 $extensions = $this->getExtension();
206 if ($this->getCase() && (in_array($extension, $extensions))) {
207 return true;
208 } elseif (!$this->getCase()) {
209 foreach ($extensions as $ext) {
210 if (strtolower($ext) == strtolower($extension)) {
211 return true;
216 $this->error(self::FALSE_EXTENSION);
217 return false;