Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/zend/Zend/Validate/File/Extension.php
Go to the documentation of this file.
00001 <?php
00025 require_once 'Zend/Validate/Abstract.php';
00026 
00035 class Zend_Validate_File_Extension extends Zend_Validate_Abstract
00036 {
00040     const FALSE_EXTENSION = 'fileExtensionFalse';
00041     const NOT_FOUND       = 'fileExtensionNotFound';
00042 
00046     protected $_messageTemplates = array(
00047         self::FALSE_EXTENSION => "File '%value%' has a false extension",
00048         self::NOT_FOUND       => "File '%value%' could not be found",
00049     );
00050 
00055     protected $_extension = '';
00056 
00062     protected $_case = false;
00063 
00067     protected $_messageVariables = array(
00068         'extension' => '_extension'
00069     );
00070 
00077     public function __construct($options)
00078     {
00079         if ($options instanceof Zend_Config) {
00080             $options = $options->toArray();
00081         }
00082 
00083         if (1 < func_num_args()) {
00084             $case = func_get_arg(1);
00085             $this->setCase($case);
00086         }
00087 
00088         if (is_array($options) and isset($options['case'])) {
00089             $this->setCase($options['case']);
00090             unset($options['case']);
00091         }
00092 
00093         $this->setExtension($options);
00094     }
00095 
00101     public function getCase()
00102     {
00103         return $this->_case;
00104     }
00105 
00112     public function setCase($case)
00113     {
00114         $this->_case = (boolean) $case;
00115         return $this;
00116     }
00117 
00123     public function getExtension()
00124     {
00125         $extension = explode(',', $this->_extension);
00126 
00127         return $extension;
00128     }
00129 
00136     public function setExtension($extension)
00137     {
00138         $this->_extension = null;
00139         $this->addExtension($extension);
00140         return $this;
00141     }
00142 
00149     public function addExtension($extension)
00150     {
00151         $extensions = $this->getExtension();
00152         if (is_string($extension)) {
00153             $extension = explode(',', $extension);
00154         }
00155 
00156         foreach ($extension as $content) {
00157             if (empty($content) || !is_string($content)) {
00158                 continue;
00159             }
00160 
00161             $extensions[] = trim($content);
00162         }
00163         $extensions = array_unique($extensions);
00164 
00165         // Sanity check to ensure no empty values
00166         foreach ($extensions as $key => $ext) {
00167             if (empty($ext)) {
00168                 unset($extensions[$key]);
00169             }
00170         }
00171 
00172         $this->_extension = implode(',', $extensions);
00173 
00174         return $this;
00175     }
00176 
00187     public function isValid($value, $file = null)
00188     {
00189         // Is file readable ?
00190         require_once 'Zend/Loader.php';
00191         if (!Zend_Loader::isReadable($value)) {
00192             return $this->_throw($file, self::NOT_FOUND);
00193         }
00194 
00195         if ($file !== null) {
00196             $info['extension'] = substr($file['name'], strrpos($file['name'], '.') + 1);
00197         } else {
00198             $info = pathinfo($value);
00199         }
00200 
00201         $extensions = $this->getExtension();
00202 
00203         if ($this->_case && (in_array($info['extension'], $extensions))) {
00204             return true;
00205         } else if (!$this->getCase()) {
00206             foreach ($extensions as $extension) {
00207                 if (strtolower($extension) == strtolower($info['extension'])) {
00208                     return true;
00209                 }
00210             }
00211         }
00212 
00213         return $this->_throw($file, self::FALSE_EXTENSION);
00214     }
00215 
00223     protected function _throw($file, $errorType)
00224     {
00225         if (null !== $file) {
00226             $this->_value = $file['name'];
00227         }
00228 
00229         $this->_error($errorType);
00230         return false;
00231     }
00232 }
 All Data Structures Namespaces Files Functions Variables Enumerations