Blacklist   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 52
c 0
b 0
f 0
dl 0
loc 110
rs 10
wmc 23

5 Methods

Rating   Name   Duplication   Size   Complexity  
A delete() 0 10 5
A getAllKeywords() 0 18 4
A addkeywords() 0 14 5
A remove_blacklisted() 0 18 5
A store() 0 16 4
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\News;
4
5
//  ------------------------------------------------------------------------ //
6
//                  Copyright (c) 2005-2006 Hervé Thouzard                     //
7
//                     <https://www.herve-thouzard.com>                      //
8
// ------------------------------------------------------------------------- //
9
//  This program is free software; you can redistribute it and/or modify     //
10
//  it under the terms of the GNU General Public License as published by     //
11
//  the Free Software Foundation; either version 2 of the License, or        //
12
//  (at your option) any later version.                                      //
13
//                                                                           //
14
//  You may not change or alter any portion of this comment or credits       //
15
//  of supporting developers from this source code or any supporting         //
16
//  source code which is considered copyrighted (c) material of the          //
17
//  original comment or credit authors.                                      //
18
//                                                                           //
19
//  This program is distributed in the hope that it will be useful,          //
20
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
21
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
22
//  GNU General Public License for more details.                             //
23
//                                                                           //
24
//  You should have received a copy of the GNU General Public License        //
25
//  along with this program; if not, write to the Free Software              //
26
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
27
//  ------------------------------------------------------------------------ //
28
29
/**
30
 * Class Blacklist
31
 */
32
class Blacklist
33
{
34
    public $keywords; // Holds keywords
35
36
    /**
37
     * Get all the keywords
38
     */
39
    public function getAllKeywords()
40
    {
41
        $ret      = $tbl_black_list = [];
42
        $myts     = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
43
        $filename = XOOPS_UPLOAD_PATH . '/news_black_list.php';
44
        if (\is_file($filename)) {
45
            require_once $filename;
46
            foreach ($tbl_black_list as $onekeyword) {
47
                if ('' !== \xoops_trim($onekeyword)) {
48
                    $onekeyword       = \htmlspecialchars($onekeyword, \ENT_QUOTES | \ENT_HTML5);
49
                    $ret[$onekeyword] = $onekeyword;
50
                }
51
            }
52
        }
53
        \asort($ret);
54
        $this->keywords = $ret;
55
56
        return $this->keywords;
57
    }
58
59
    /**
60
     * Remove one or many keywords from the list
61
     * @param $keyword
62
     */
63
    public function delete($keyword): void
64
    {
65
        if (\is_array($keyword)) {
66
            foreach ($keyword as $onekeyword) {
67
                if (isset($this->keywords[$onekeyword])) {
68
                    unset($this->keywords[$onekeyword]);
69
                }
70
            }
71
        } elseif (isset($this->keywords[$keyword])) {
72
            unset($this->keywords[$keyword]);
73
        }
74
    }
75
76
    /**
77
     * Add one or many keywords
78
     * @param $keyword
79
     */
80
    public function addkeywords($keyword): void
81
    {
82
        $myts = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
83
        if (\is_array($keyword)) {
84
            foreach ($keyword as $onekeyword) {
85
                $onekeyword = \xoops_trim(\htmlspecialchars($onekeyword, \ENT_QUOTES | \ENT_HTML5));
86
                if ('' !== $onekeyword) {
87
                    $this->keywords[$onekeyword] = $onekeyword;
88
                }
89
            }
90
        } else {
91
            $keyword = \xoops_trim(\htmlspecialchars($keyword, \ENT_QUOTES | \ENT_HTML5));
92
            if ('' !== $keyword) {
93
                $this->keywords[$keyword] = $keyword;
94
            }
95
        }
96
    }
97
98
    /**
99
     * Remove, from a list, all the blacklisted words
100
     * @param $keywords
101
     * @return array
102
     */
103
    public function remove_blacklisted($keywords)
104
    {
105
        $ret       = [];
106
        $tmp_array = \array_values($this->keywords);
107
        foreach ($keywords as $onekeyword) {
108
            $add = true;
109
            foreach ($tmp_array as $onebanned) {
110
                if (\preg_match('/' . $onebanned . '/i', $onekeyword)) {
111
                    $add = false;
112
                    break;
113
                }
114
            }
115
            if ($add) {
116
                $ret[] = $onekeyword;
117
            }
118
        }
119
120
        return $ret;
121
    }
122
123
    /**
124
     * Save keywords
125
     */
126
    public function store(): void
127
    {
128
        $filename = XOOPS_UPLOAD_PATH . '/news_black_list.php';
129
        if (\is_file($filename)) {
130
            \unlink($filename);
131
        }
132
        /** @var resource $fd */
133
        $fd = \fopen($filename, 'wb') || exit('Error unable to create the blacklist file');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
134
        \fwrite($fd, "<?php\n");
135
        \fwrite($fd, '$tbl_black_list=array(' . "\n");
136
        foreach ($this->keywords as $onekeyword) {
137
            \fwrite($fd, '"' . $onekeyword . "\",\n");
138
        }
139
        \fwrite($fd, "'');\n");
140
        \fwrite($fd, "?>\n");
141
        \fclose($fd);
142
    }
143
}
144