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_Adminhtml
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 custom layout update
30: *
31: * Validator checked XML validation and protected expressions
32: *
33: * @category Mage
34: * @package Mage_Adminhtml
35: * @author Magento Core Team <core@magentocommerce.com>
36: */
37: class Mage_Adminhtml_Model_LayoutUpdate_Validator extends Zend_Validate_Abstract
38: {
39: const XML_INVALID = 'invalidXml';
40: const PROTECTED_ATTR_HELPER_IN_TAG_ACTION_VAR = 'protectedAttrHelperInActionVar';
41:
42: /**
43: * The Varien SimpleXml object
44: *
45: * @var Varien_Simplexml_Element
46: */
47: protected $_value;
48:
49: /**
50: * Protected expressions
51: *
52: * @var array
53: */
54: protected $_protectedExpressions = array(
55: self::PROTECTED_ATTR_HELPER_IN_TAG_ACTION_VAR => '//action/*[@helper]',
56: );
57:
58: /**
59: * Construct
60: */
61: public function __construct()
62: {
63: $this->_initMessageTemplates();
64: }
65:
66: /**
67: * Initialize messages templates with translating
68: *
69: * @return Mage_Adminhtml_Model_LayoutUpdate_Validator
70: */
71: protected function _initMessageTemplates()
72: {
73: if (!$this->_messageTemplates) {
74: $this->_messageTemplates = array(
75: self::PROTECTED_ATTR_HELPER_IN_TAG_ACTION_VAR =>
76: Mage::helper('adminhtml')->__('Helper attributes should not be used in custom layout updates.'),
77: self::XML_INVALID => Mage::helper('adminhtml')->__('XML data is invalid.'),
78: );
79: }
80: return $this;
81: }
82:
83: /**
84: * Returns true if and only if $value meets the validation requirements
85: *
86: * If $value fails validation, then this method returns false, and
87: * getMessages() will return an array of messages that explain why the
88: * validation failed.
89: *
90: * @throws Exception Throw exception when xml object is not
91: * instance of Varien_Simplexml_Element
92: * @param Varien_Simplexml_Element|string $value
93: * @return bool
94: */
95: public function isValid($value)
96: {
97: if (is_string($value)) {
98: $value = trim($value);
99: try {
100: //wrap XML value in the "config" tag because config cannot
101: //contain multiple root tags
102: $value = new Varien_Simplexml_Element('<config>' . $value . '</config>');
103: } catch (Exception $e) {
104: $this->_error(self::XML_INVALID);
105: return false;
106: }
107: } elseif (!($value instanceof Varien_Simplexml_Element)) {
108: throw new Exception(
109: Mage::helper('adminhtml')->__('XML object is not instance of "Varien_Simplexml_Element".'));
110: }
111:
112: $this->_setValue($value);
113:
114: foreach ($this->_protectedExpressions as $key => $xpr) {
115: if ($this->_value->xpath($xpr)) {
116: $this->_error($key);
117: return false;
118: }
119: }
120: return true;
121: }
122: }
123: