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/File/Extension.php';
0026 
0027 /**
0028  * Validator for the excluding file extensions
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_ExcludeExtension extends Zend_Validate_File_Extension
0036 {
0037     /**
0038      * @const string Error constants
0039      */
0040     const FALSE_EXTENSION = 'fileExcludeExtensionFalse';
0041     const NOT_FOUND       = 'fileExcludeExtensionNotFound';
0042 
0043     /**
0044      * @var array Error message templates
0045      */
0046     protected $_messageTemplates = array(
0047         self::FALSE_EXTENSION => "File '%value%' has a false extension",
0048         self::NOT_FOUND       => "File '%value%' is not readable or does not exist",
0049     );
0050 
0051     /**
0052      * Defined by Zend_Validate_Interface
0053      *
0054      * Returns true if and only if the fileextension of $value is not included in the
0055      * set extension list
0056      *
0057      * @param  string  $value Real file to check for extension
0058      * @param  array   $file  File data from Zend_File_Transfer
0059      * @return boolean
0060      */
0061     public function isValid($value, $file = null)
0062     {
0063         // Is file readable ?
0064         // require_once 'Zend/Loader.php';
0065         if (!Zend_Loader::isReadable($value)) {
0066             return $this->_throw($file, self::NOT_FOUND);
0067         }
0068 
0069         if ($file !== null) {
0070             $info['extension'] = substr($file['name'], strrpos($file['name'], '.') + 1);
0071         } else {
0072             $info = pathinfo($value);
0073         }
0074 
0075         $extensions = $this->getExtension();
0076 
0077         if ($this->_case and (!in_array($info['extension'], $extensions))) {
0078             return true;
0079         } else if (!$this->_case) {
0080             $found = false;
0081             foreach ($extensions as $extension) {
0082                 if (strtolower($extension) == strtolower($info['extension'])) {
0083                     $found = true;
0084                 }
0085             }
0086 
0087             if (!$found) {
0088                 return true;
0089             }
0090         }
0091 
0092         return $this->_throw($file, self::FALSE_EXTENSION);
0093     }
0094 }