PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/concrete/libraries/3rdparty/Zend/Validate/File/Extension.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 232 lines | 103 code | 28 blank | 101 comment | 17 complexity | 11201e4ce51b4fd90748ecdfcc9d834b 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. * @version $Id: Extension.php 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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. * @return void
  69. */
  70. public function __construct($options)
  71. {
  72. if ($options instanceof Zend_Config) {
  73. $options = $options->toArray();
  74. }
  75. if (1 < func_num_args()) {
  76. $case = func_get_arg(1);
  77. $this->setCase($case);
  78. }
  79. if (is_array($options) and isset($options['case'])) {
  80. $this->setCase($options['case']);
  81. unset($options['case']);
  82. }
  83. $this->setExtension($options);
  84. }
  85. /**
  86. * Returns the case option
  87. *
  88. * @return boolean
  89. */
  90. public function getCase()
  91. {
  92. return $this->_case;
  93. }
  94. /**
  95. * Sets the case to use
  96. *
  97. * @param boolean $case
  98. * @return Zend_Validate_File_Extension Provides a fluent interface
  99. */
  100. public function setCase($case)
  101. {
  102. $this->_case = (boolean) $case;
  103. return $this;
  104. }
  105. /**
  106. * Returns the set file extension
  107. *
  108. * @return array
  109. */
  110. public function getExtension()
  111. {
  112. $extension = explode(',', $this->_extension);
  113. return $extension;
  114. }
  115. /**
  116. * Sets the file extensions
  117. *
  118. * @param string|array $extension The extensions to validate
  119. * @return Zend_Validate_File_Extension Provides a fluent interface
  120. */
  121. public function setExtension($extension)
  122. {
  123. $this->_extension = null;
  124. $this->addExtension($extension);
  125. return $this;
  126. }
  127. /**
  128. * Adds the file extensions
  129. *
  130. * @param string|array $extension The extensions to add for validation
  131. * @return Zend_Validate_File_Extension Provides a fluent interface
  132. */
  133. public function addExtension($extension)
  134. {
  135. $extensions = $this->getExtension();
  136. if (is_string($extension)) {
  137. $extension = explode(',', $extension);
  138. }
  139. foreach ($extension as $content) {
  140. if (empty($content) || !is_string($content)) {
  141. continue;
  142. }
  143. $extensions[] = trim($content);
  144. }
  145. $extensions = array_unique($extensions);
  146. // Sanity check to ensure no empty values
  147. foreach ($extensions as $key => $ext) {
  148. if (empty($ext)) {
  149. unset($extensions[$key]);
  150. }
  151. }
  152. $this->_extension = implode(',', $extensions);
  153. return $this;
  154. }
  155. /**
  156. * Defined by Zend_Validate_Interface
  157. *
  158. * Returns true if and only if the fileextension of $value is included in the
  159. * set extension list
  160. *
  161. * @param string $value Real file to check for extension
  162. * @param array $file File data from Zend_File_Transfer
  163. * @return boolean
  164. */
  165. public function isValid($value, $file = null)
  166. {
  167. // Is file readable ?
  168. require_once 'Zend/Loader.php';
  169. if (!Zend_Loader::isReadable($value)) {
  170. return $this->_throw($file, self::NOT_FOUND);
  171. }
  172. if ($file !== null) {
  173. $info['extension'] = substr($file['name'], strrpos($file['name'], '.') + 1);
  174. } else {
  175. $info = pathinfo($value);
  176. }
  177. $extensions = $this->getExtension();
  178. if ($this->_case && (in_array($info['extension'], $extensions))) {
  179. return true;
  180. } else if (!$this->getCase()) {
  181. foreach ($extensions as $extension) {
  182. if (strtolower($extension) == strtolower($info['extension'])) {
  183. return true;
  184. }
  185. }
  186. }
  187. return $this->_throw($file, self::FALSE_EXTENSION);
  188. }
  189. /**
  190. * Throws an error of the given type
  191. *
  192. * @param string $file
  193. * @param string $errorType
  194. * @return false
  195. */
  196. protected function _throw($file, $errorType)
  197. {
  198. if (null !== $file) {
  199. $this->_value = $file['name'];
  200. }
  201. $this->_error($errorType);
  202. return false;
  203. }
  204. }