|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* You may not change or alter any portion of this comment or credits |
|
4
|
|
|
* of supporting developers from this source code or any supporting source code |
|
5
|
|
|
* which is considered copyrighted (c) material of the original comment or credit authors. |
|
6
|
|
|
* |
|
7
|
|
|
* This program is distributed in the hope that it will be useful, |
|
8
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
9
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Xoops\Upgrade; |
|
13
|
|
|
|
|
14
|
|
|
use SplFileInfo; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* XOOPS Upgrade Smarty4TemplateChecks |
|
18
|
|
|
* |
|
19
|
|
|
* Scanner process to look for BC issues in existing templates when used with Smarty4 |
|
20
|
|
|
* |
|
21
|
|
|
* @category Xoops\Upgrade |
|
22
|
|
|
* @package Xoops |
|
23
|
|
|
* @author Richard Griffith <[email protected]> |
|
24
|
|
|
* @copyright 2023 XOOPS Project (https://xoops.org) |
|
25
|
|
|
* @license GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html) |
|
26
|
|
|
* @link https://xoops.org |
|
27
|
|
|
*/ |
|
28
|
|
|
class Smarty4TemplateChecks extends ScannerProcess |
|
29
|
|
|
{ |
|
30
|
|
|
protected $patterns = [ |
|
31
|
|
|
'varname' => '/<{foreach[[:space:]]+item=([a-zA-Z0-9\-_.]+)[[:space:]]from=\$([a-zA-Z0-9\-_.]+) *}>/', |
|
32
|
|
|
// 'noquotes' =>'/(<{xo[a-zA-Z\d]*\b[^}>]*?)\s*([^\'"}]+)(}>)/', |
|
33
|
|
|
// Updated noquotes pattern to ignore cases with variables or assignments |
|
34
|
|
|
'noquotes' => '/(<{xo[a-zA-Z\d]*\b)(?=[^}>]*[^\'"\$}>=]+[}>])([^}>]*?)(\s+)([^\$\'\"=]+)(\s*}>)/', |
|
35
|
|
|
'includeq' => '/(<{includeq[[:space:]]+[ -=\.\/_\'\"\$a-zA-Z0-9]+}>)/', |
|
36
|
|
|
'foreachq' => '/(<{foreachq[[:space:]]+[ -=\.\/_\'\"\$a-zA-Z0-9]+}>)/', |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var ScannerOutput |
|
41
|
|
|
*/ |
|
42
|
|
|
private $output; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param ScannerOutput $output |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct(ScannerOutput $output) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->output = $output; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param SplFileInfo $fileInfo |
|
54
|
|
|
* @return void |
|
55
|
|
|
*/ |
|
56
|
|
|
public function inspectFile(SplFileInfo $fileInfo) |
|
57
|
|
|
{ |
|
58
|
|
|
$output = $this->output; |
|
59
|
|
|
$writable = $fileInfo->isWritable(); |
|
60
|
|
|
$length = $fileInfo->getSize(); |
|
61
|
|
|
$file = $fileInfo->openFile(); |
|
62
|
|
|
$contents = $file->fread($length); |
|
63
|
|
|
|
|
64
|
|
|
// variable names in Smarty 3 foreach item and from must be unique |
|
65
|
|
|
$rule = 'varname'; |
|
66
|
|
|
$pattern = $this->patterns[$rule]; |
|
67
|
|
|
$results = preg_match_all($pattern, $contents, $matches, PREG_PATTERN_ORDER, 0); |
|
68
|
|
|
if ((0 < (int)$results) && isset($matches[0][0]) && is_string($matches[0][0])) { |
|
69
|
|
|
for ($i = 0; $i < (int)$results; $i++) { |
|
70
|
|
|
if ($matches[1][$i] == $matches[2][$i]) { |
|
71
|
|
|
$file = str_replace(XOOPS_ROOT_PATH, '', $fileInfo->getPathname()); |
|
72
|
|
|
$match = $matches[0][$i]; |
|
73
|
|
|
$output->outputIssue($output->makeOutputIssue($rule, $file, $match, $writable)); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
unset($matches); |
|
78
|
|
|
|
|
79
|
|
|
// plugin function arguments must be quoted |
|
80
|
|
|
$rule = 'noquotes'; |
|
81
|
|
|
$pattern = $this->patterns[$rule]; |
|
82
|
|
|
$results = preg_match_all($pattern, $contents, $matches, PREG_PATTERN_ORDER, 0); |
|
83
|
|
|
if (0 < (int)$results) { |
|
84
|
|
|
for ($i = 0; $i < (int)$results; $i++) { |
|
85
|
|
|
$match = $matches[0][$i] ?? null; |
|
86
|
|
|
if (null !== $match && '<{if false}>' !== $match) { // oddball case |
|
87
|
|
|
$file = str_replace(XOOPS_ROOT_PATH, '', $fileInfo->getPathname()); |
|
88
|
|
|
$output->outputIssue($output->makeOutputIssue($rule, $file, $match, $writable)); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
unset($matches); |
|
93
|
|
|
|
|
94
|
|
|
// includeq was removed, use include instead |
|
95
|
|
|
$rule = 'includeq'; |
|
96
|
|
|
$pattern = $this->patterns[$rule]; |
|
97
|
|
|
$results = preg_match_all($pattern, $contents, $matches, PREG_PATTERN_ORDER, 0); |
|
98
|
|
|
if (0 < (int)$results) { |
|
99
|
|
|
$match = $matches[0][0] ?? null; |
|
100
|
|
|
if (null !== $match) { |
|
101
|
|
|
$file = str_replace(XOOPS_ROOT_PATH, '', $fileInfo->getPathname()); |
|
102
|
|
|
$output->outputIssue($output->makeOutputIssue($rule, $file, $match, $writable)); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
unset($matches); |
|
106
|
|
|
|
|
107
|
|
|
// foreachq was removed, use foreach instead |
|
108
|
|
|
$rule = 'foreachq'; |
|
109
|
|
|
$pattern = $this->patterns[$rule]; |
|
110
|
|
|
$results = preg_match_all($pattern, $contents, $matches, PREG_PATTERN_ORDER, 0); |
|
111
|
|
|
if (0 < (int)$results) { |
|
112
|
|
|
$match = $matches[0][0] ?? null; |
|
113
|
|
|
if (null !== $match) { |
|
114
|
|
|
$file = str_replace(XOOPS_ROOT_PATH, '', $fileInfo->getPathname()); |
|
115
|
|
|
$output->outputIssue($output->makeOutputIssue($rule, $file, $match, $writable)); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
unset($matches); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|