Main::hookRouteList()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
/**
4
 * @package XSS filter
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2018, Iurii Makukh <[email protected]>
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0-or-later
8
 */
9
10
namespace gplcart\modules\xss;
11
12
use gplcart\core\Container;
13
use gplcart\core\Module;
14
15
/**
16
 * Main class for XSS filter module
17
 */
18
class Main
19
{
20
    /**
21
     * Module class instance
22
     * @var Module
23
     */
24
    protected $module;
25
26
    /**
27
     * Main constructor.
28
     * @param Module $module
29
     */
30
    public function __construct(Module $module)
31
    {
32
        $this->module = $module;
33
    }
34
35
    /**
36
     * Implements hook "route.list"
37
     * @param array $routes
38
     */
39
    public function hookRouteList(array &$routes)
40
    {
41
        $routes['admin/module/settings/xss'] = array(
42
            'access' => 'module_xss_edit',
43
            'handlers' => array(
44
                'controller' => array('gplcart\\modules\\xss\\controllers\\Settings', 'editSettings')
45
            )
46
        );
47
    }
48
49
    /**
50
     * Implements hook "user.role.permissions"
51
     * @param array $permissions
52
     */
53
    public function hookUserRolePermissions(array &$permissions)
54
    {
55
        $permissions['module_xss_edit'] = 'XSS filter: edit settings'; // @text
56
    }
57
58
    /**
59
     * Implements hook "filter"
60
     * @param string $text
61
     * @param array $filter
62
     * @param null|string $filtered
63
     */
64
    public function hookFilter($text, $filter, &$filtered)
65
    {
66
        if (!isset($filtered) && (!isset($filter['filter_id']) || $filter['filter_id'] === 'xss')) {
67
            $filtered = $this->filter($text);
68
        }
69
    }
70
71
    /**
72
     * Implements hook "filter.handlers"
73
     * @param array $filters
74
     */
75
    public function hookFilterHandlers(array &$filters)
76
    {
77
        $filters['xss'] = array(
78
            'name' => 'XSS', // @text
79
            'description' => 'Simple XSS filter', // @text
80
            'status' => true,
81
            'module' => 'xss'
82
        );
83
    }
84
85
    /**
86
     * Filter a string
87
     * @param string $text
88
     * @param null|array $allowed_tags
89
     * @param null|array $allowed_protocols
90
     * @return string
91
     */
92
    public function filter($text, $allowed_tags = null, $allowed_protocols = null)
93
    {
94
        $settings = $this->module->getSettings('xss');
95
96
        if (!isset($allowed_tags)) {
97
            $allowed_tags = $settings['tags'];
98
        }
99
100
        if (!isset($allowed_protocols)) {
101
            $allowed_protocols = $settings['protocols'];
102
        }
103
104
        return $this->getFilter()
105
            ->setTags($allowed_tags)
106
            ->setProtocols($allowed_protocols)
107
            ->filter($text);
108
    }
109
110
    /**
111
     * Returns Filter library class instance
112
     * @return helpers\Filter
113
     */
114
    public function getFilter()
115
    {
116
        /** @var \gplcart\modules\xss\helpers\Filter $instance */
117
        $instance = Container::get('gplcart\\modules\\xss\\helpers\\Filter');
118
        return $instance;
119
    }
120
}
121