|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * @copyright For copyright and license information, read the COPYING.txt file. |
| 7 | + * @link /COPYING.txt |
| 8 | + * @license Open Software License (OSL 3.0) |
| 9 | + * @package Mage_Csp |
| 10 | + */ |
| 11 | + |
| 12 | +/** |
| 13 | + * Base class for CSP hosts field renderer |
| 14 | + */ |
| 15 | +class Mage_Adminhtml_Block_System_Config_Form_Field_Csp_Hosts extends Mage_Adminhtml_Block_System_Config_Form_Field_Array_Abstract |
| 16 | +{ |
| 17 | + protected Mage_Csp_Helper_Data $helper; |
| 18 | + |
| 19 | + /** |
| 20 | + * Constructor |
| 21 | + */ |
| 22 | + public function __construct() |
| 23 | + { |
| 24 | + /** @var Mage_Csp_Helper_Data $helper */ |
| 25 | + $helper = Mage::helper('csp'); |
| 26 | + $this->helper = $helper; |
| 27 | + $this->addColumn('host', [ |
| 28 | + 'label' => Mage::helper('csp')->__('Host'), |
| 29 | + ]); |
| 30 | + |
| 31 | + $this->_addAfter = false; |
| 32 | + $this->_addButtonLabel = Mage::helper('csp')->__('Add Host'); |
| 33 | + $this->setTemplate('system/config/form/field/csp.phtml'); |
| 34 | + |
| 35 | + parent::__construct(); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Obtain existing data from form element |
| 40 | + * |
| 41 | + * Each row will be instance of Varien_Object |
| 42 | + * @return array<string, Varien_Object> Array of rows |
| 43 | + * @throws Exception |
| 44 | + */ |
| 45 | + public function getArrayRows(): array |
| 46 | + { |
| 47 | + if ($this->_arrayRowsCache !== null) { |
| 48 | + return $this->_arrayRowsCache; |
| 49 | + } |
| 50 | + |
| 51 | + $result = []; |
| 52 | + |
| 53 | + [$area, $directiveName] = $this->_parseNodePath(); |
| 54 | + |
| 55 | + $globalPolicy = $this->helper->getGlobalPolicy($directiveName); |
| 56 | + if ($globalPolicy) { |
| 57 | + foreach ($globalPolicy as $key => $host) { |
| 58 | + $rowId = $directiveName . '_xml_' . $area . '_' . $key; |
| 59 | + $result[$rowId] = new Varien_Object([ |
| 60 | + 'host' => $host, |
| 61 | + 'readonly' => 'readonly="readonly"', |
| 62 | + '_id' => $rowId, |
| 63 | + 'area' => 'global', |
| 64 | + ]); |
| 65 | + $this->_prepareArrayRow($result[$rowId]); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + $areaPolicy = $this->helper->getAreaPolicy($area, $directiveName); |
| 70 | + if ($areaPolicy) { |
| 71 | + foreach ($areaPolicy as $key => $host) { |
| 72 | + $rowId = $directiveName . '_xml_' . $area . '_' . $key; |
| 73 | + $result[$rowId] = new Varien_Object([ |
| 74 | + 'host' => $host, |
| 75 | + 'readonly' => 'readonly="readonly"', |
| 76 | + '_id' => $rowId, |
| 77 | + 'area' => $area, |
| 78 | + ]); |
| 79 | + $this->_prepareArrayRow($result[$rowId]); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + $configPolicy = $this->helper->getStoreConfigPolicy($area, $directiveName); |
| 84 | + if ($configPolicy) { |
| 85 | + foreach ($configPolicy as $key => $value) { |
| 86 | + $rowId = $directiveName . '_' . $area . '_' . $key; |
| 87 | + $result[$rowId] = new Varien_Object([ |
| 88 | + 'host' => $this->escapeHtml($value), |
| 89 | + '_id' => $rowId, |
| 90 | + ]); |
| 91 | + |
| 92 | + $this->_prepareArrayRow($result[$rowId]); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + $this->_arrayRowsCache = $result; |
| 97 | + return $this->_arrayRowsCache; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Extract and validate area and directive name from the node path |
| 102 | + * |
| 103 | + * @return array{Mage_Core_Model_App_Area::AREA_FRONTEND|Mage_Core_Model_App_Area::AREA_ADMINHTML, value-of<Mage_Csp_Helper_Data::CSP_DIRECTIVES>} Array containing area and directiveName |
| 104 | + * @throws Exception If path format is invalid or contains disallowed values |
| 105 | + */ |
| 106 | + private function _parseNodePath(): array |
| 107 | + { |
| 108 | + /** @var Varien_Data_Form_Element_Abstract $element */ |
| 109 | + $element = $this->getElement(); |
| 110 | + $configPath = $element->getData('config_path'); |
| 111 | + |
| 112 | + $allowedDirectives = implode('|', Mage_Csp_Helper_Data::CSP_DIRECTIVES); |
| 113 | + $allowedAreas = Mage_Core_Model_App_Area::AREA_FRONTEND . '|' . Mage_Core_Model_App_Area::AREA_ADMINHTML; |
| 114 | + |
| 115 | + $pattern = "#csp/({$allowedAreas})/({$allowedDirectives})#"; |
| 116 | + |
| 117 | + if (!$configPath || !preg_match($pattern, $configPath, $matches)) { |
| 118 | + throw new Exception('Invalid node path format or disallowed area/directive'); |
| 119 | + } |
| 120 | + |
| 121 | + $area = $matches[1]; |
| 122 | + $directiveName = $matches[2]; |
| 123 | + |
| 124 | + return [$area, $directiveName]; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Render array cell for prototypeJS template |
| 129 | + * |
| 130 | + * @param string $columnName |
| 131 | + * @return string |
| 132 | + * @throws Exception |
| 133 | + */ |
| 134 | + protected function _renderCellTemplate($columnName) |
| 135 | + { |
| 136 | + if (empty($this->_columns[$columnName])) { |
| 137 | + throw new Exception('Wrong column name specified.'); |
| 138 | + } |
| 139 | + |
| 140 | + $column = $this->_columns[$columnName]; |
| 141 | + /** @var Varien_Data_Form_Element_Text $element */ |
| 142 | + $element = $this->getElement(); |
| 143 | + $elementName = $element->getName(); |
| 144 | + $inputName = $elementName . '[#{_id}][' . $columnName . ']'; |
| 145 | + |
| 146 | + return '<input type="text" name="' . $inputName . '" value="#{' . $columnName . '}" ' . |
| 147 | + '#{readonly}' . |
| 148 | + ($column['size'] ? 'size="' . $column['size'] . '"' : '') . ' class="' . |
| 149 | + ($column['class'] ?? 'input-text') . '"' . |
| 150 | + (isset($column['style']) ? ' style="' . $column['style'] . '"' : '') . '/>'; |
| 151 | + } |
| 152 | +} |
0 commit comments