1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
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@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Core
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27:
28: /**
29: * Validator for check not protected file extensions
30: *
31: * @category Mage
32: * @package Mage_Core
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Core_Model_File_Validator_NotProtectedExtension extends Zend_Validate_Abstract
36: {
37: const PROTECTED_EXTENSION = 'protectedExtension';
38:
39: /**
40: * The file extension
41: *
42: * @var string
43: */
44: protected $_value;
45:
46: /**
47: * Protected file types
48: *
49: * @var array
50: */
51: protected $_protectedFileExtensions = array();
52:
53: /**
54: * Construct
55: */
56: public function __construct()
57: {
58: $this->_initMessageTemplates();
59: $this->_initProtectedFileExtensions();
60: }
61:
62: /**
63: * Initialize message templates with translating
64: *
65: * @return Mage_Core_Model_File_Validator_NotProtectedExtension
66: */
67: protected function _initMessageTemplates()
68: {
69: if (!$this->_messageTemplates) {
70: $this->_messageTemplates = array(
71: self::PROTECTED_EXTENSION => Mage::helper('core')->__('File with an extension "%value%" is protected and cannot be uploaded'),
72: );
73: }
74: return $this;
75: }
76:
77: /**
78: * Initialize protected file extensions
79: *
80: * @return Mage_Core_Model_File_Validator_NotProtectedExtension
81: */
82: protected function _initProtectedFileExtensions()
83: {
84: if (!$this->_protectedFileExtensions) {
85: /** @var $helper Mage_Core_Helper_Data */
86: $helper = Mage::helper('core');
87: $extensions = $helper->getProtectedFileExtensions();
88: if (is_string($extensions)) {
89: $extensions = explode(',', $extensions);
90: }
91: foreach ($extensions as &$ext) {
92: $ext = strtolower(trim($ext));
93: }
94: $this->_protectedFileExtensions = (array) $extensions;
95: }
96: return $this;
97: }
98:
99:
100: /**
101: * Returns true if and only if $value meets the validation requirements
102: *
103: * If $value fails validation, then this method returns false, and
104: * getMessages() will return an array of messages that explain why the
105: * validation failed.
106: *
107: * @param string $value Extension of file
108: * @return bool
109: */
110: public function isValid($value)
111: {
112: $value = strtolower(trim($value));
113: $this->_setValue($value);
114:
115: if (in_array($this->_value, $this->_protectedFileExtensions)) {
116: $this->_error(self::PROTECTED_EXTENSION, $this->_value);
117: return false;
118: }
119:
120: return true;
121: }
122: }
123: