fix calendar css, take 2. (#213)
[openemr.git] / interface / modules / zend_modules / library / Zend / Validator / File / Extension.php
bloba779832fe9e1a3d018aef5d92bd7d5b5ab1e9b8d
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-2015 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;
17 /**
18 * Validator for the file extension of a file
20 class Extension extends AbstractValidator
22 /**
23 * @const string Error constants
25 const FALSE_EXTENSION = 'fileExtensionFalse';
26 const NOT_FOUND = 'fileExtensionNotFound';
28 /**
29 * @var array Error message templates
31 protected $messageTemplates = array(
32 self::FALSE_EXTENSION => "File has an incorrect extension",
33 self::NOT_FOUND => "File is not readable or does not exist",
36 /**
37 * Options for this validator
39 * @var array
41 protected $options = array(
42 'case' => false, // Validate case sensitive
43 'extension' => '', // List of extensions
46 /**
47 * @var array Error message template variables
49 protected $messageVariables = array(
50 'extension' => array('options' => 'extension'),
53 /**
54 * Sets validator options
56 * @param string|array|Traversable $options
58 public function __construct($options = null)
60 if ($options instanceof Traversable) {
61 $options = ArrayUtils::iteratorToArray($options);
64 $case = null;
65 if (1 < func_num_args()) {
66 $case = func_get_arg(1);
69 if (is_array($options)) {
70 if (isset($options['case'])) {
71 $case = $options['case'];
72 unset($options['case']);
75 if (!array_key_exists('extension', $options)) {
76 $options = array('extension' => $options);
78 } else {
79 $options = array('extension' => $options);
82 if ($case !== null) {
83 $options['case'] = $case;
86 parent::__construct($options);
89 /**
90 * Returns the case option
92 * @return bool
94 public function getCase()
96 return $this->options['case'];
99 /**
100 * Sets the case to use
102 * @param bool $case
103 * @return Extension Provides a fluent interface
105 public function setCase($case)
107 $this->options['case'] = (bool) $case;
108 return $this;
112 * Returns the set file extension
114 * @return array
116 public function getExtension()
118 $extension = explode(',', $this->options['extension']);
120 return $extension;
124 * Sets the file extensions
126 * @param string|array $extension The extensions to validate
127 * @return Extension Provides a fluent interface
129 public function setExtension($extension)
131 $this->options['extension'] = null;
132 $this->addExtension($extension);
133 return $this;
137 * Adds the file extensions
139 * @param string|array $extension The extensions to add for validation
140 * @return Extension Provides a fluent interface
142 public function addExtension($extension)
144 $extensions = $this->getExtension();
145 if (is_string($extension)) {
146 $extension = explode(',', $extension);
149 foreach ($extension as $content) {
150 if (empty($content) || !is_string($content)) {
151 continue;
154 $extensions[] = trim($content);
157 $extensions = array_unique($extensions);
159 // Sanity check to ensure no empty values
160 foreach ($extensions as $key => $ext) {
161 if (empty($ext)) {
162 unset($extensions[$key]);
166 $this->options['extension'] = implode(',', $extensions);
167 return $this;
171 * Returns true if and only if the file extension of $value is included in the
172 * set extension list
174 * @param string|array $value Real file to check for extension
175 * @param array $file File data from \Zend\File\Transfer\Transfer (optional)
176 * @return bool
178 public function isValid($value, $file = null)
180 if (is_string($value) && is_array($file)) {
181 // Legacy Zend\Transfer API support
182 $filename = $file['name'];
183 $file = $file['tmp_name'];
184 } elseif (is_array($value)) {
185 if (!isset($value['tmp_name']) || !isset($value['name'])) {
186 throw new Exception\InvalidArgumentException(
187 'Value array must be in $_FILES format'
190 $file = $value['tmp_name'];
191 $filename = $value['name'];
192 } else {
193 $file = $value;
194 $filename = basename($file);
196 $this->setValue($filename);
198 // Is file readable ?
199 if (empty($file) || false === stream_resolve_include_path($file)) {
200 $this->error(self::NOT_FOUND);
201 return false;
204 $extension = substr($filename, strrpos($filename, '.') + 1);
205 $extensions = $this->getExtension();
207 if ($this->getCase() && (in_array($extension, $extensions))) {
208 return true;
209 } elseif (!$this->getCase()) {
210 foreach ($extensions as $ext) {
211 if (strtolower($ext) == strtolower($extension)) {
212 return true;
217 $this->error(self::FALSE_EXTENSION);
218 return false;