1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package Filter |
5
|
|
|
* @author Iurii Makukh |
6
|
|
|
* @copyright Copyright (c) 2017, Iurii Makukh |
7
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+ |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace gplcart\modules\filter; |
11
|
|
|
|
12
|
|
|
use Exception; |
13
|
|
|
use gplcart\core\Config; |
14
|
|
|
use gplcart\core\Library; |
15
|
|
|
use HTMLPurifier; |
16
|
|
|
use HTMLPurifier_Config; |
17
|
|
|
use LogicException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Main class for Filter module |
21
|
|
|
*/ |
22
|
|
|
class Main |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* An array of HTML Purifier instances keyed by filter configuration hash |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $htmlpurifiers = array(); |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Config class instance |
33
|
|
|
* @var \gplcart\core\Config $config |
34
|
|
|
*/ |
35
|
|
|
protected $config; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Library class instance |
39
|
|
|
* @var \gplcart\core\Library $library |
40
|
|
|
*/ |
41
|
|
|
protected $library; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param Config $config |
45
|
|
|
* @param Library $library |
46
|
|
|
*/ |
47
|
|
|
public function __construct(Config $config, Library $library) |
48
|
|
|
{ |
49
|
|
|
$this->config = $config; |
50
|
|
|
$this->library = $library; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Implements hook "library.list" |
55
|
|
|
* @param array $libraries |
56
|
|
|
*/ |
57
|
|
|
public function hookLibraryList(array &$libraries) |
58
|
|
|
{ |
59
|
|
|
$libraries['htmlpurifier'] = array( |
60
|
|
|
'name' => 'HTML Purifier', // @text |
61
|
|
|
'description' => 'Standards compliant HTML filter written in PHP', // @text |
62
|
|
|
'type' => 'php', |
63
|
|
|
'module' => 'filter', |
64
|
|
|
'url' => 'https://github.com/ezyang/htmlpurifier', |
65
|
|
|
'download' => 'https://github.com/ezyang/htmlpurifier/archive/v4.9.2.zip', |
66
|
|
|
'version' => '4.9.2', |
67
|
|
|
'vendor' => 'ezyang/htmlpurifier', |
68
|
|
|
'files' => array( |
69
|
|
|
'library/HTMLPurifier.auto.php' |
70
|
|
|
), |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Implements hook "route.list" |
76
|
|
|
* @param array $routes |
77
|
|
|
*/ |
78
|
|
|
public function hookRouteList(array &$routes) |
79
|
|
|
{ |
80
|
|
|
$routes['admin/module/settings/filter'] = array( |
81
|
|
|
'access' => 'module_edit', |
82
|
|
|
'handlers' => array( |
83
|
|
|
'controller' => array('gplcart\\modules\\filter\\controllers\\Filter', 'listFilter') |
84
|
|
|
) |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
$routes['admin/module/settings/filter/edit/(\w+)'] = array( |
88
|
|
|
'access' => 'module_filter_edit', |
89
|
|
|
'handlers' => array( |
90
|
|
|
'controller' => array('gplcart\\modules\\filter\\controllers\\Filter', 'editFilter') |
91
|
|
|
) |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Implements hook "user.role.permissions" |
97
|
|
|
* @param array $permissions |
98
|
|
|
*/ |
99
|
|
|
public function hookUserRolePermissions(array &$permissions) |
100
|
|
|
{ |
101
|
|
|
$permissions['module_filter_edit'] = 'HTML Filter: edit'; // @text |
102
|
|
|
$permissions['module_filter_delete'] = 'HTML Filter: delete'; // @text |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Implements hook "filter" |
107
|
|
|
* @param string $text |
108
|
|
|
* @param array $filter |
109
|
|
|
* @param null|string $filtered |
110
|
|
|
*/ |
111
|
|
|
public function hookFilter($text, $filter, &$filtered) |
112
|
|
|
{ |
113
|
|
|
if (!isset($filtered) && isset($filter['module']) && $filter['module'] === 'filter' && !empty($filter['status'])) { |
114
|
|
|
try { |
115
|
|
|
$filtered = $this->filter($text, $filter); |
116
|
|
|
} catch (Exception $ex) { |
117
|
|
|
$filtered = '** filter error **'; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Implements hook "filter.handlers" |
124
|
|
|
* @param mixed $filters |
125
|
|
|
*/ |
126
|
|
|
public function hookFilterHandlers(array &$filters) |
127
|
|
|
{ |
128
|
|
|
$filters = array_merge($filters, $this->getHandlers()); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Implements hook "module.uninstall.after" |
133
|
|
|
*/ |
134
|
|
|
public function hookModuleUninstallAfter() |
135
|
|
|
{ |
136
|
|
|
foreach (array_keys($this->config->select()) as $key) { |
137
|
|
|
if (strpos($key, 'module_filter_') === 0) { |
138
|
|
|
$this->config->reset($key); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Filter a string |
145
|
|
|
* @param string $text |
146
|
|
|
* @param array $filter |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
public function filter($text, $filter) |
150
|
|
|
{ |
151
|
|
|
return $this->getPurifier($filter)->purify($text); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Returns HTML Purifier class instance depending on the filter configuration |
156
|
|
|
* @param array $filter |
157
|
|
|
* @return HTMLPurifier |
158
|
|
|
* @throws LogicException |
159
|
|
|
*/ |
160
|
|
|
public function getPurifier(array $filter) |
161
|
|
|
{ |
162
|
|
|
ksort($filter['data']); |
163
|
|
|
$key = md5(json_encode($filter['data'])); |
164
|
|
|
|
165
|
|
|
if (isset($this->htmlpurifiers[$key])) { |
166
|
|
|
return $this->htmlpurifiers[$key]; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$this->library->load('htmlpurifier'); |
170
|
|
|
|
171
|
|
|
if (!class_exists('HTMLPurifier')) { |
172
|
|
|
throw new LogicException('Class HTMLPurifier not found'); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
if (empty($filter['data'])) { |
176
|
|
|
$config = HTMLPurifier_Config::createDefault(); |
177
|
|
|
} else { |
178
|
|
|
$config = HTMLPurifier_Config::create($filter['data']); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $this->htmlpurifiers[$key] = new HTMLPurifier($config); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Returns an array of filter handlers |
186
|
|
|
* @return array |
187
|
|
|
*/ |
188
|
|
|
public function getHandlers() |
189
|
|
|
{ |
190
|
|
|
$filters = gplcart_config_get(__DIR__ . '/config/filters.php'); |
191
|
|
|
$saved = $this->config->get('module_filter_filters', array()); |
192
|
|
|
|
193
|
|
|
return array_replace_recursive($filters, $saved); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
} |
197
|
|
|
|