File indexing completed on 2024-12-22 05:37:11
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Validate 0017 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0018 * @license http://framework.zend.com/license/new-bsd New BSD License 0019 * @version $Id$ 0020 */ 0021 0022 /** 0023 * @see Zend_Validate_Abstract 0024 */ 0025 // require_once 'Zend/Validate/Abstract.php'; 0026 0027 /** 0028 * Validator which checks if the file already exists in the directory 0029 * 0030 * @category Zend 0031 * @package Zend_Validate 0032 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0033 * @license http://framework.zend.com/license/new-bsd New BSD License 0034 */ 0035 class Zend_Validate_File_Exists extends Zend_Validate_Abstract 0036 { 0037 /** 0038 * @const string Error constants 0039 */ 0040 const DOES_NOT_EXIST = 'fileExistsDoesNotExist'; 0041 0042 /** 0043 * @var array Error message templates 0044 */ 0045 protected $_messageTemplates = array( 0046 self::DOES_NOT_EXIST => "File '%value%' does not exist", 0047 ); 0048 0049 /** 0050 * Internal list of directories 0051 * @var string 0052 */ 0053 protected $_directory = ''; 0054 0055 /** 0056 * @var array Error message template variables 0057 */ 0058 protected $_messageVariables = array( 0059 'directory' => '_directory' 0060 ); 0061 0062 /** 0063 * Sets validator options 0064 * 0065 * @param string|array|Zend_Config $directory 0066 * @throws Zend_Validate_Exception 0067 */ 0068 public function __construct($directory = array()) 0069 { 0070 if ($directory instanceof Zend_Config) { 0071 $directory = $directory->toArray(); 0072 } else if (is_string($directory)) { 0073 $directory = explode(',', $directory); 0074 } else if (!is_array($directory)) { 0075 // require_once 'Zend/Validate/Exception.php'; 0076 throw new Zend_Validate_Exception ('Invalid options to validator provided'); 0077 } 0078 0079 $this->setDirectory($directory); 0080 } 0081 0082 /** 0083 * Returns the set file directories which are checked 0084 * 0085 * @param boolean $asArray Returns the values as array, when false an concated string is returned 0086 * @return string 0087 */ 0088 public function getDirectory($asArray = false) 0089 { 0090 $asArray = (bool) $asArray; 0091 $directory = (string) $this->_directory; 0092 if ($asArray) { 0093 $directory = explode(',', $directory); 0094 } 0095 0096 return $directory; 0097 } 0098 0099 /** 0100 * Sets the file directory which will be checked 0101 * 0102 * @param string|array $directory The directories to validate 0103 * @return Zend_Validate_File_Extension Provides a fluent interface 0104 */ 0105 public function setDirectory($directory) 0106 { 0107 $this->_directory = null; 0108 $this->addDirectory($directory); 0109 return $this; 0110 } 0111 0112 /** 0113 * Adds the file directory which will be checked 0114 * 0115 * @param string|array $directory The directory to add for validation 0116 * @throws Zend_Validate_Exception 0117 * @return Zend_Validate_File_Extension Provides a fluent interface 0118 */ 0119 public function addDirectory($directory) 0120 { 0121 $directories = $this->getDirectory(true); 0122 0123 if (is_string($directory)) { 0124 $directory = explode(',', $directory); 0125 } else if (!is_array($directory)) { 0126 // require_once 'Zend/Validate/Exception.php'; 0127 throw new Zend_Validate_Exception ('Invalid options to validator provided'); 0128 } 0129 0130 foreach ($directory as $content) { 0131 if (empty($content) || !is_string($content)) { 0132 continue; 0133 } 0134 0135 $directories[] = trim($content); 0136 } 0137 $directories = array_unique($directories); 0138 0139 // Sanity check to ensure no empty values 0140 foreach ($directories as $key => $dir) { 0141 if (empty($dir)) { 0142 unset($directories[$key]); 0143 } 0144 } 0145 0146 $this->_directory = implode(',', $directories); 0147 0148 return $this; 0149 } 0150 0151 /** 0152 * Defined by Zend_Validate_Interface 0153 * 0154 * Returns true if and only if the file already exists in the set directories 0155 * 0156 * @param string $value Real file to check for existance 0157 * @param array $file File data from Zend_File_Transfer 0158 * @return boolean 0159 */ 0160 public function isValid($value, $file = null) 0161 { 0162 $directories = $this->getDirectory(true); 0163 if (($file !== null) and (!empty($file['destination']))) { 0164 $directories[] = $file['destination']; 0165 } else if (!isset($file['name'])) { 0166 $file['name'] = $value; 0167 } 0168 0169 $check = false; 0170 foreach ($directories as $directory) { 0171 if (empty($directory)) { 0172 continue; 0173 } 0174 0175 $check = true; 0176 if (!file_exists($directory . DIRECTORY_SEPARATOR . $file['name'])) { 0177 return $this->_throw($file, self::DOES_NOT_EXIST); 0178 } 0179 } 0180 0181 if (!$check) { 0182 return $this->_throw($file, self::DOES_NOT_EXIST); 0183 } 0184 0185 return true; 0186 } 0187 0188 /** 0189 * Throws an error of the given type 0190 * 0191 * @param string $file 0192 * @param string $errorType 0193 * @return false 0194 */ 0195 protected function _throw($file, $errorType) 0196 { 0197 if ($file !== null) { 0198 $this->_value = $file['name']; 0199 } 0200 0201 $this->_error($errorType); 0202 return false; 0203 } 0204 }