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 Smarty3TemplateChecks |
18
|
|
|
* |
19
|
|
|
* Scanner process to look for BC issues in existing templates when used with Smarty3 |
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 Smarty3TemplateChecks extends ScannerProcess |
29
|
|
|
{ |
30
|
|
|
protected $patterns = array( |
31
|
|
|
'varname' => '/<{foreach[[:space:]]+item=([a-zA-Z0-9\-_.]+)[[:space:]]from=\$([a-zA-Z0-9\-_.]+) *}>/', |
32
|
|
|
'noquotes' => '/(<{[a-zA-Z0-1_\-]+[[:space:]]+[a-zA-Z.]+}>)/', |
33
|
|
|
'includeq' => '/(<{includeq[[:space:]]+[ -=\.\/_\'\"\$a-zA-Z0-9]+}>)/', |
34
|
|
|
'foreachq' => '/(<{foreachq[[:space:]]+[ -=\.\/_\'\"\$a-zA-Z0-9]+}>)/', |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var ScannerOutput |
39
|
|
|
*/ |
40
|
|
|
private $output; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param ScannerOutput $output |
44
|
|
|
*/ |
45
|
|
|
public function __construct(ScannerOutput $output) |
46
|
|
|
{ |
47
|
|
|
$this->output = $output; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param SplFileInfo $fileInfo |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
|
|
public function inspectFile(SplFileInfo $fileInfo) |
55
|
|
|
{ |
56
|
|
|
$output = $this->output; |
57
|
|
|
$writable = $fileInfo->isWritable(); |
58
|
|
|
$length = $fileInfo->getSize(); |
59
|
|
|
$file = $fileInfo->openFile(); |
60
|
|
|
$contents = $file->fread($length); |
61
|
|
|
|
62
|
|
|
// variable names in Smarty 3 foreach item and from must be unique |
63
|
|
|
$rule = 'varname'; |
64
|
|
|
$pattern = $this->patterns[$rule]; |
65
|
|
|
$results = preg_match_all($pattern, $contents, $matches, PREG_PATTERN_ORDER, 0); |
66
|
|
|
if ((false !== $results) && @is_string($matches[0][0])) { |
67
|
|
|
if ($matches[1][0] == $matches[2][0]) { |
68
|
|
|
$file = str_replace(XOOPS_ROOT_PATH, '', $fileInfo->getPathname()); |
69
|
|
|
$match = $matches[0][0]; |
70
|
|
|
$output->outputIssue($output->makeOutputIssue($rule, $file, $match, $writable)); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
unset($matches); |
74
|
|
|
|
75
|
|
|
// plugin function arguments must be quoted |
76
|
|
|
$rule = 'noquotes'; |
77
|
|
|
$pattern = $this->patterns[$rule]; |
78
|
|
|
$results = preg_match_all($pattern, $contents, $matches, PREG_PATTERN_ORDER, 0); |
79
|
|
|
if (false !== $results) { |
80
|
|
|
@$match = $matches[0][0]; |
81
|
|
|
if (!is_null($match) |
82
|
|
|
&& '<{if false}>' != $match // oddball case |
83
|
|
|
) { |
84
|
|
|
$file = str_replace(XOOPS_ROOT_PATH, '', $fileInfo->getPathname()); |
85
|
|
|
$output->outputIssue($output->makeOutputIssue($rule, $file, $match, $writable)); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
unset($matches); |
89
|
|
|
|
90
|
|
|
// includeq was removed, use include instead |
91
|
|
|
$rule = 'includeq'; |
92
|
|
|
$pattern = $this->patterns[$rule]; |
93
|
|
|
$results = preg_match_all($pattern, $contents, $matches, PREG_PATTERN_ORDER, 0); |
94
|
|
|
if (false !== $results) { |
95
|
|
|
@$match = $matches[0][0]; |
96
|
|
|
if (!is_null($match)) { |
97
|
|
|
$file = str_replace(XOOPS_ROOT_PATH, '', $fileInfo->getPathname()); |
98
|
|
|
$output->outputIssue($output->makeOutputIssue($rule, $file, $match, $writable)); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
unset($matches); |
102
|
|
|
|
103
|
|
|
// foreachq was removed, use foreach instead |
104
|
|
|
$rule = 'foreachq'; |
105
|
|
|
$pattern = $this->patterns[$rule]; |
106
|
|
|
$results = preg_match_all($pattern, $contents, $matches, PREG_PATTERN_ORDER, 0); |
107
|
|
|
if (false !== $results) { |
108
|
|
|
@$match = $matches[0][0]; |
109
|
|
|
if (!is_null($match)) { |
110
|
|
|
$file = str_replace(XOOPS_ROOT_PATH, '', $fileInfo->getPathname()); |
111
|
|
|
$output->outputIssue($output->makeOutputIssue($rule, $file, $match, $writable)); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
unset($matches); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|