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

/DevApp/library/ServerLibraries/ZendFramework/1.7/library/Zend/Validate/File/Extension.php

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