Completed
Push — master ( a8cb5f...9c0d7d )
by Iurii
02:17
created

Filter::info()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 0
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
    gplcart\core\Library;
14
use gplcart\core\models\Language as LanguageModel;
15
16
/**
17
 * Main class for Filter module
18
 */
19
class Filter extends Module
20
{
21
22
    /**
23
     * An array of HTML Purifier instances keyed by filter config hash
24
     * @var array
25
     */
26
    protected $htmlpurifiers = array();
27
28
    /**
29
     * Library class instance
30
     * @var \gplcart\core\Library $library
31
     */
32
    protected $library;
33
34
    /**
35
     * Language model instance
36
     * @var \gplcart\core\models\Language $language
37
     */
38
    protected $language;
39
40
    /**
41
     * @param Library $library
42
     * @param LanguageModel $language
43
     */
44
    public function __construct(Library $library, LanguageModel $language)
45
    {
46
        parent::__construct();
47
48
        $this->library = $library;
49
        $this->language = $language;
50
    }
51
52
    /**
53
     * Implements hook "library.list"
54
     * @param array $libraries
55
     */
56
    public function hookLibraryList(array &$libraries)
57
    {
58
        $libraries['htmlpurifier'] = array(
59
            'name' => 'HTML Purifier',
60
            'description' => 'Standards compliant HTML filter written in PHP',
61
            'type' => 'php',
62
            'module' => 'filter',
63
            'url' => 'https://github.com/ezyang/htmlpurifier',
64
            'download' => 'https://github.com/ezyang/htmlpurifier/archive/v4.9.2.zip',
65
            'version_source' => array(
66
                'lines' => 100,
67
                'file' => 'vendor/ezyang/htmlpurifier/library/HTMLPurifier.php',
68
                'pattern' => '/.*VERSION.*(\\d+\\.+\\d+\\.+\\d+)/'
69
            ),
70
            'files' => array(
71
                'vendor/ezyang/htmlpurifier/library/HTMLPurifier.auto.php'
72
            ),
73
        );
74
    }
75
76
    /**
77
     * Implements hook "route.list"
78
     * @param array $routes
79
     */
80
    public function hookRouteList(array &$routes)
81
    {
82
        $routes['admin/module/settings/filter'] = array(
83
            'access' => 'module_edit',
84
            'handlers' => array(
85
                'controller' => array('gplcart\\modules\\filter\\controllers\\Settings', 'editSettings')
86
            )
87
        );
88
    }
89
90
    /**
91
     * Implements hook "user.role.permissions"
92
     * @param array $permissions
93
     */
94
    public function hookUserRolePermissions(array &$permissions)
95
    {
96
        $permissions['module_filter_edit'] = 'HTML Filter: edit';
97
    }
98
99
    /**
100
     * Implements hook "filter"
101
     * @param mixed $data
0 ignored issues
show
Bug introduced by
There is no parameter named $data. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
102
     */
103
    public function hookFilter($text, $filter, &$filtered)
104
    {
105
        $filtered = $this->filter($text, $filter);
106
    }
107
108
    /**
109
     * Filter a string
110
     * @param string $text
111
     * @param array $filter
112
     * @return string
113
     */
114
    public function filter($text, $filter)
115
    {
116
        if (empty($filter['config']) || empty($filter['status'])) {
117
            $filter['config'] = array(); // Empty config enables most safest default filter
118
        }
119
120
        return $this->getHtmlpurifierInstance($filter)->purify($text);
121
    }
122
123
    /**
124
     * Returns HTML Purifier class instance depending on the filter config
125
     * @param array $filter
126
     * @return \HTMLPurifier
127
     */
128
    public function getHtmlpurifierInstance(array $filter)
129
    {
130
        ksort($filter['config']);
131
        $key = md5(json_encode($filter['config']));
132
133
        if (isset($this->htmlpurifiers[$key])) {
134
            return $this->htmlpurifiers[$key];
135
        }
136
137
        $this->library->load('htmlpurifier');
138
139
        if (empty($filter['config'])) {
140
            $config = \HTMLPurifier_Config::createDefault();
141
        } else {
142
            $config = \HTMLPurifier_Config::create($filter['config']);
143
        }
144
145
        return $this->htmlpurifiers[$key] = new \HTMLPurifier($config);
146
    }
147
148
    /**
149
     * Implements hook "filter.list"
150
     * @param mixed $filters
151
     */
152
    public function hookFilterList(array &$filters)
153
    {
154
        $settings = $this->config->module('filter');
155
156
        $filters['minimal'] = array(
157
            'name' => $this->language->text('Minimal'),
158
            'description' => $this->language->text('Minimal configuration for untrusted users'),
159
            'status' => $settings['status']['minimal'],
160
            'role_id' => $settings['role_id']['minimal'],
161
            'config' => array(
162
                'AutoFormat.DisplayLinkURI' => true,
163
                'AutoFormat.RemoveEmpty' => true,
164
                'HTML.Allowed' => 'strong,em,p,b,s,i,a[href|title],img[src|alt],'
165
                . 'blockquote,code,pre,del,ul,ol,li'
166
            )
167
        );
168
169
        $filters['advanced'] = array(
170
            'name' => $this->language->text('Advanced'),
171
            'description' => $this->language->text('Advanced configuration for trusted users, e.g content managers'),
172
            'status' => $settings['status']['advanced'],
173
            'role_id' => $settings['role_id']['advanced'],
174
            'config' => array(
175
                'AutoFormat.Linkify' => true,
176
                'AutoFormat.RemoveEmpty.RemoveNbsp' => true,
177
                'AutoFormat.RemoveEmpty' => true,
178
                'HTML.Nofollow' => true,
179
                'HTML.Allowed' => 'div,table,tr,td,tbody,tfoot,thead,th,strong,'
180
                . 'em,p[style],b,s,i,h2,h3,h4,h5,hr,br,span[style],a[href|title],'
181
                . 'img[width|height|alt|src],blockquote,code,pre,del,kbd,'
182
                . 'cite,dt,dl,dd,sup,sub,ul,ol,li',
183
                'CSS.AllowedProperties' => 'font,font-size,font-weight,font-style,'
184
                . 'font-family,text-decoration,padding-left,color,'
185
                . 'background-color,text-align',
186
                'HTML.FlashAllowFullScreen' => true,
187
                'HTML.SafeObject' => true,
188
                'HTML.SafeEmbed' => true,
189
                'HTML.Trusted' => true,
190
                'Output.FlashCompat' => true
191
            )
192
        );
193
194
        $filters['maximal'] = array(
195
            'name' => $this->language->text('Maximal'),
196
            'description' => $this->language->text('Maximal configuration for experienced and trusted users, e.g superadmin'),
197
            'status' => $settings['status']['maximal'],
198
            'role_id' => $settings['role_id']['maximal'],
199
            'config' => array(
200
                'AutoFormat.Linkify' => true,
201
                'AutoFormat.RemoveEmpty.RemoveNbsp' => false,
202
                'AutoFormat.RemoveEmpty' => true,
203
                'HTML.Allowed' => 'div,table,tr,td,tbody,tfoot,thead,th,strong,'
204
                . 'em,p[style],b,s,i,h2,h3,h4,h5,hr,br,span[style],a[href|title],'
205
                . 'img[width|height|alt|src],blockquote,code,pre,del,kbd,'
206
                . 'cite,dt,dl,dd,sup,sub,ul,ol,li',
207
                'CSS.AllowedProperties' => 'font,font-size,font-weight,font-style,'
208
                . 'font-family,text-decoration,padding-left,color,'
209
                . 'background-color,text-align',
210
                'HTML.FlashAllowFullScreen' => true,
211
                'HTML.SafeObject' => true,
212
                'HTML.SafeEmbed' => true,
213
                'HTML.Trusted' => true,
214
                'Output.FlashCompat' => true,
215
                'Attr.AllowedFrameTargets' => array('_blank', '_self', '_parent', '_top')
216
            )
217
        );
218
    }
219
220
    /**
221
     * Implements hook "module.enable.after"
222
     */
223
    public function hookModuleEnableAfter()
224
    {
225
        $this->library->clearCache();
226
    }
227
228
    /**
229
     * Implements hook "module.disable.after"
230
     */
231
    public function hookModuleDisableAfter()
232
    {
233
        $this->library->clearCache();
234
    }
235
236
    /**
237
     * Implements hook "module.install.after"
238
     */
239
    public function hookModuleInstallAfter()
240
    {
241
        $this->library->clearCache();
242
    }
243
244
    /**
245
     * Implements hook "module.uninstall.after"
246
     */
247
    public function hookModuleUninstallAfter()
248
    {
249
        $this->library->clearCache();
250
    }
251
252
}
253