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