|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Email\Model\Template; |
| 9 | + |
| 10 | +use Magento\Framework\Filter\Template\Tokenizer\Parameter; |
| 11 | +use Magento\Framework\Filter\Template\Tokenizer\Variable; |
| 12 | + |
| 13 | +/** |
| 14 | + * Scan an email template for compatibility with the strict resolver |
| 15 | + */ |
| 16 | +class CompatibilityChecker |
| 17 | +{ |
| 18 | + private const CONSTRUCTION_DEPEND_PATTERN = '/{{depend\s*(.*?)}}(.*?){{\\/depend\s*}}/si'; |
| 19 | + private const CONSTRUCTION_IF_PATTERN = '/{{if\s*(.*?)}}(.*?)({{else}}(.*?))?{{\\/if\s*}}/si'; |
| 20 | + private const LOOP_PATTERN = '/{{for(?P<loopItem>.*? )(in)(?P<loopData>.*?)}}(?P<loopBody>.*?){{\/for}}/si'; |
| 21 | + private const CONSTRUCTION_PATTERN = '/{{([a-z]{0,10})(.*?)}}(?:(.*?)(?:{{\/(?:\\1)}}))?/si'; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var array |
| 25 | + */ |
| 26 | + private array $errors = []; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var Variable |
| 30 | + */ |
| 31 | + private Variable $variableTokenizer; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var Parameter |
| 35 | + */ |
| 36 | + private Parameter $parameterTokenizer; |
| 37 | + |
| 38 | + public function __construct(Variable $variableTokenizer, Parameter $parameterTokenizer) |
| 39 | + { |
| 40 | + $this->variableTokenizer = $variableTokenizer; |
| 41 | + $this->parameterTokenizer = $parameterTokenizer; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Detect invalid usage of template filter directives |
| 46 | + * |
| 47 | + */ |
| 48 | + public function getCompatibilityIssues(string $template): array |
| 49 | + { |
| 50 | + $this->errors = []; |
| 51 | + |
| 52 | + if (empty($template)) { |
| 53 | + return []; |
| 54 | + } |
| 55 | + |
| 56 | + $template = $this->processIfDirectives($template); |
| 57 | + $template = $this->processDependDirectives($template); |
| 58 | + $template = $this->processForDirectives($template); |
| 59 | + $this->processVarDirectivesAndParams($template); |
| 60 | + |
| 61 | + return $this->errors; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Process the {{if}} directives in the file |
| 66 | + * |
| 67 | + * @param string $html |
| 68 | + * @return string The processed template |
| 69 | + */ |
| 70 | + private function processIfDirectives(string $html): string |
| 71 | + { |
| 72 | + if (preg_match_all(self::CONSTRUCTION_IF_PATTERN, $html, $constructions, PREG_SET_ORDER)) { |
| 73 | + foreach ($constructions as $construction) { |
| 74 | + // validate {{if <var>}} |
| 75 | + $this->validateVariableUsage($construction[1]); |
| 76 | + $html = str_replace($construction[0], $construction[2] . ($construction[4] ?? ''), $html); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return $html; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Process the {{depend}} directives in the file |
| 85 | + * |
| 86 | + * @param string $html |
| 87 | + * @return string The processed template |
| 88 | + */ |
| 89 | + private function processDependDirectives(string $html): string |
| 90 | + { |
| 91 | + if (preg_match_all(self::CONSTRUCTION_DEPEND_PATTERN, $html, $constructions, PREG_SET_ORDER)) { |
| 92 | + foreach ($constructions as $construction) { |
| 93 | + // validate {{depend <var>}} |
| 94 | + $this->validateVariableUsage($construction[1]); |
| 95 | + $html = str_replace($construction[0], $construction[2], $html); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return $html; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Process the {{for}} directives in the file |
| 104 | + * |
| 105 | + * @param string $html |
| 106 | + * @return string The processed template |
| 107 | + */ |
| 108 | + private function processForDirectives(string $html): string |
| 109 | + { |
| 110 | + if (preg_match_all(self::LOOP_PATTERN, $html, $constructions, PREG_SET_ORDER)) { |
| 111 | + foreach ($constructions as $construction) { |
| 112 | + // validate {{for in <var>}} |
| 113 | + $this->validateVariableUsage($construction['loopData']); |
| 114 | + $html = str_replace($construction[0], $construction['loopBody'], $html); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return $html; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Process the all var directives and var directive params in the file |
| 123 | + * |
| 124 | + * @param string $html |
| 125 | + * @return string The processed template |
| 126 | + */ |
| 127 | + private function processVarDirectivesAndParams(string $html): string |
| 128 | + { |
| 129 | + if (preg_match_all(self::CONSTRUCTION_PATTERN, $html, $constructions, PREG_SET_ORDER)) { |
| 130 | + foreach ($constructions as $construction) { |
| 131 | + if (empty($construction[2])) { |
| 132 | + continue; |
| 133 | + } |
| 134 | + |
| 135 | + if ($construction[1] === 'var') { |
| 136 | + $this->validateVariableUsage($construction[2]); |
| 137 | + } else { |
| 138 | + $this->validateDirectiveBody($construction[2]); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return $html; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Validate directive body is valid. e.g. {{somedir <directive body>}} |
| 148 | + * |
| 149 | + * @param string $body |
| 150 | + */ |
| 151 | + private function validateDirectiveBody(string $body): void |
| 152 | + { |
| 153 | + $this->parameterTokenizer->setString($body); |
| 154 | + $params = $this->parameterTokenizer->tokenize(); |
| 155 | + |
| 156 | + foreach ($params as $param) { |
| 157 | + if (substr($param, 0, 1) === '$') { |
| 158 | + $this->validateVariableUsage(substr($param, 1)); |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Validate directive variable usage is valid. e.g. {{var <variable body>}} or {{somedir some_param="$foo.bar()"}} |
| 165 | + * |
| 166 | + * @param string $body |
| 167 | + */ |
| 168 | + private function validateVariableUsage(string $body): void |
| 169 | + { |
| 170 | + $this->variableTokenizer->setString($body); |
| 171 | + $stack = $this->variableTokenizer->tokenize(); |
| 172 | + |
| 173 | + if (empty($stack)) { |
| 174 | + return; |
| 175 | + } |
| 176 | + |
| 177 | + foreach ($stack as $token) { |
| 178 | + // As a static analyzer there are no data types to know if this is a DataObject so allow all get* methods |
| 179 | + if ($token['type'] === 'method' && substr($token['name'], 0, 3) !== 'get') { |
| 180 | + $this->addError( |
| 181 | + 'Template directives may not invoke methods. Only scalar array access is allowed.' . PHP_EOL |
| 182 | + . 'Found "' . trim($body) . '"' |
| 183 | + ); |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Add an error to the current processing template |
| 190 | + * |
| 191 | + * @param string $error |
| 192 | + */ |
| 193 | + private function addError(string $error): void |
| 194 | + { |
| 195 | + $this->errors[] = $error; |
| 196 | + } |
| 197 | +} |
0 commit comments