Extension.php 6.1 KB

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