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 Smarty3TemplateRepair |
18
|
|
|
* |
19
|
|
|
* Scanner process to look for and repair 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 Smarty3TemplateRepair extends ScannerProcess |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var int count of changes made |
32
|
|
|
*/ |
33
|
|
|
protected $count = 0; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array regex patterns |
37
|
|
|
*/ |
38
|
|
|
protected $patterns = array(); |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array replacement patterns |
42
|
|
|
*/ |
43
|
|
|
protected $replacements = array(); |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var ScannerOutput |
47
|
|
|
*/ |
48
|
|
|
private $output; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param ScannerOutput $output |
52
|
|
|
*/ |
53
|
|
|
public function __construct(ScannerOutput $output) |
54
|
|
|
{ |
55
|
|
|
$this->output = $output; |
56
|
|
|
$this->loadPatterns(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function loadPatterns() |
60
|
|
|
{ |
61
|
|
|
$this->patterns[] = '/(<{includeq[[:space:]]+)/'; |
62
|
|
|
$this->replacements[] = '<{include '; |
63
|
|
|
|
64
|
|
|
$this->patterns[] = '/(<{foreachq[[:space:]]+)/'; |
65
|
|
|
$this->replacements[] = '<{foreach '; |
66
|
|
|
|
67
|
|
|
$this->patterns[] = '/("<{[a-zA-Z0-9_\-]+)\s+([a-zA-Z\/\.][\/\.a-zA-Z0-9]*)\s?}>/'; |
68
|
|
|
$this->replacements[] = "$1 '$2'}>"; |
69
|
|
|
|
70
|
|
|
$this->patterns[] = "/('<{[a-zA-Z0-9_\-]+)\s+([a-zA-Z\/\.][\/\.a-zA-Z0-9]*)\s?}>/"; |
71
|
|
|
$this->replacements[] = '$1 "$2"}>'; |
72
|
|
|
|
73
|
|
|
$this->patterns[] = '/(<{[a-zA-Z0-9_\-]+)\s+([a-zA-Z\/\.][\/\.a-zA-Z0-9]*)\s?}>/'; |
74
|
|
|
$this->replacements[] = "$1 '$2'}>"; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param SplFileInfo $fileInfo |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
|
|
public function inspectFile(SplFileInfo $fileInfo) |
82
|
|
|
{ |
83
|
|
|
$output = $this->output; |
84
|
|
|
if (false===$fileInfo->isWritable()) { |
85
|
|
|
return; |
86
|
|
|
} |
87
|
|
|
$length = $fileInfo->getSize(); |
88
|
|
|
$file = $fileInfo->openFile('r+'); |
89
|
|
|
$lines = $file->fread($length); |
90
|
|
|
|
91
|
|
|
$count = 0; |
92
|
|
|
$updatedLines = preg_replace( |
93
|
|
|
$this->patterns, |
94
|
|
|
$this->replacements, |
95
|
|
|
$lines, |
96
|
|
|
-1, |
97
|
|
|
$count |
98
|
|
|
); |
99
|
|
|
if ($updatedLines===null) { |
100
|
|
|
error; |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/* rewrite if changes were made */ |
104
|
|
|
if ($count !== 0) { |
105
|
|
|
$file->fseek(0); |
106
|
|
|
$file->ftruncate(0); |
107
|
|
|
$result = $file->fwrite($updatedLines); |
108
|
|
|
if ($result===false) { |
109
|
|
|
error; |
110
|
|
|
} |
111
|
|
|
$filename = str_replace(XOOPS_ROOT_PATH, '', $fileInfo->getPathname()); |
112
|
|
|
$output->outputIssue($output->makeOutputIssue($filename, $count)); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|