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