/library/Zend/Validator/File/Extension.php

https://github.com/leerbag/zf2 · PHP · 251 lines · 124 code · 31 blank · 96 comment · 26 complexity · 4df8cfbda0dcacf650005ad90776647a 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. */
  20. /**
  21. * @namespace
  22. */
  23. namespace Zend\Validator\File;
  24. use Zend\Loader;
  25. /**
  26. * Validator for the file extension of a file
  27. *
  28. * @uses \Zend\Loader
  29. * @uses \Zend\Validator\AbstractValidator
  30. * @category Zend
  31. * @package Zend_Validate
  32. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Extension extends \Zend\Validator\AbstractValidator
  36. {
  37. /**
  38. * @const string Error constants
  39. */
  40. const FALSE_EXTENSION = 'fileExtensionFalse';
  41. const NOT_FOUND = 'fileExtensionNotFound';
  42. /**
  43. * @var array Error message templates
  44. */
  45. protected $_messageTemplates = array(
  46. self::FALSE_EXTENSION => "File '%value%' has a false extension",
  47. self::NOT_FOUND => "File '%value%' is not readable or does not exist",
  48. );
  49. /**
  50. * Options for this valdiator
  51. *
  52. * @var array
  53. */
  54. protected $options = array(
  55. 'case' => false, // Validate case sensitive
  56. 'extension' => '', // List of extensions
  57. );
  58. /**
  59. * @var array Error message template variables
  60. */
  61. protected $_messageVariables = array(
  62. 'extension' => array('options' => 'extension'),
  63. );
  64. /**
  65. * Sets validator options
  66. *
  67. * @param string|array|\Zend\Config\Config $options
  68. * @return void
  69. */
  70. public function __construct($options = null)
  71. {
  72. if ($options instanceof \Zend\Config\Config) {
  73. $options = $options->toArray();
  74. }
  75. $case = null;
  76. if (1 < func_num_args()) {
  77. $case = func_get_arg(1);
  78. }
  79. if (is_array($options)) {
  80. if (isset($options['case'])) {
  81. $case = $options['case'];
  82. unset($options['case']);
  83. }
  84. if (!array_key_exists('extension', $options)) {
  85. $options = array('extension' => $options);
  86. }
  87. } else {
  88. $options = array('extension' => $options);
  89. }
  90. if ($case !== null) {
  91. $options['case'] = $case;
  92. }
  93. parent::__construct($options);
  94. }
  95. /**
  96. * Returns the case option
  97. *
  98. * @return boolean
  99. */
  100. public function getCase()
  101. {
  102. return $this->options['case'];
  103. }
  104. /**
  105. * Sets the case to use
  106. *
  107. * @param boolean $case
  108. * @return \Zend\Validator\File\Extension Provides a fluent interface
  109. */
  110. public function setCase($case)
  111. {
  112. $this->options['case'] = (boolean) $case;
  113. return $this;
  114. }
  115. /**
  116. * Returns the set file extension
  117. *
  118. * @return array
  119. */
  120. public function getExtension()
  121. {
  122. $extension = explode(',', $this->options['extension']);
  123. return $extension;
  124. }
  125. /**
  126. * Sets the file extensions
  127. *
  128. * @param string|array $extension The extensions to validate
  129. * @return \Zend\Validator\File\Extension Provides a fluent interface
  130. */
  131. public function setExtension($extension)
  132. {
  133. $this->options['extension'] = null;
  134. $this->addExtension($extension);
  135. return $this;
  136. }
  137. /**
  138. * Adds the file extensions
  139. *
  140. * @param string|array $extension The extensions to add for validation
  141. * @return \Zend\Validator\File\Extension Provides a fluent interface
  142. */
  143. public function addExtension($extension)
  144. {
  145. $extensions = $this->getExtension();
  146. if (is_string($extension)) {
  147. $extension = explode(',', $extension);
  148. }
  149. foreach ($extension as $content) {
  150. if (empty($content) || !is_string($content)) {
  151. continue;
  152. }
  153. $extensions[] = trim($content);
  154. }
  155. $extensions = array_unique($extensions);
  156. // Sanity check to ensure no empty values
  157. foreach ($extensions as $key => $ext) {
  158. if (empty($ext)) {
  159. unset($extensions[$key]);
  160. }
  161. }
  162. $this->options['extension'] = implode(',', $extensions);
  163. return $this;
  164. }
  165. /**
  166. * Returns true if and only if the fileextension of $value is included in the
  167. * set extension list
  168. *
  169. * @param string $value Real file to check for extension
  170. * @param array $file File data from \Zend\File\Transfer\Transfer
  171. * @return boolean
  172. */
  173. public function isValid($value, $file = null)
  174. {
  175. if ($file === null) {
  176. $file = array('name' => basename($value));
  177. }
  178. // Is file readable ?
  179. if (!Loader::isReadable($value)) {
  180. return $this->_throw($file, self::NOT_FOUND);
  181. }
  182. if ($file !== null) {
  183. $info['extension'] = substr($file['name'], strrpos($file['name'], '.') + 1);
  184. } else {
  185. $info = pathinfo($value);
  186. }
  187. $extensions = $this->getExtension();
  188. if ($this->getCase() && (in_array($info['extension'], $extensions))) {
  189. return true;
  190. } else if (!$this->getCase()) {
  191. foreach ($extensions as $extension) {
  192. if (strtolower($extension) == strtolower($info['extension'])) {
  193. return true;
  194. }
  195. }
  196. }
  197. return $this->_throw($file, self::FALSE_EXTENSION);
  198. }
  199. /**
  200. * Throws an error of the given type
  201. *
  202. * @param string $file
  203. * @param string $errorType
  204. * @return false
  205. */
  206. protected function _throw($file, $errorType)
  207. {
  208. if ($file !== null) {
  209. if (is_array($file)) {
  210. if(array_key_exists('name', $file)) {
  211. $this->value = $file['name'];
  212. }
  213. } else if (is_string($file)) {
  214. $this->value = $file;
  215. }
  216. }
  217. $this->error($errorType);
  218. return false;
  219. }
  220. }