]> git.mxchange.org Git - friendica.git/blob - src/Moderation/DomainPatternBlocklist.php
Fix db error: missing column name 'line-text'
[friendica.git] / src / Moderation / DomainPatternBlocklist.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Moderation;
23
24 use Exception;
25 use Friendica\Core\Config\Capability\IManageConfigValues;
26 use Friendica\Core\L10n;
27 use Friendica\Database\Database;
28 use Friendica\Network\HTTPException;
29 use Friendica\Util\Emailer;
30
31 class DomainPatternBlocklist
32 {
33         /** @var IManageConfigValues */
34         private $config;
35
36         public function __construct(IManageConfigValues $config)
37         {
38                 $this->config = $config;
39         }
40
41         public function get(): array
42         {
43                 return $this->config->get('system', 'blocklist', []);
44         }
45
46         public function set(array $blocklist): bool
47         {
48                 return $this->config->set('system', 'blocklist', $blocklist);
49         }
50
51         /**
52          * @param string $pattern
53          * @param string $reason
54          *
55          * @return int 0 if the block list couldn't be saved, 1 if the pattern was added, 2 if it was updated in place
56          */
57         public function addPattern(string $pattern, string $reason): int
58         {
59                 $update = false;
60
61                 $blocklist = [];
62                 foreach ($this->get() as $blocked) {
63                         if ($blocked['domain'] === $pattern) {
64                                 $blocklist[] = [
65                                         'domain' => $pattern,
66                                         'reason' => $reason,
67                                 ];
68
69                                 $update = true;
70                         } else {
71                                 $blocklist[] = $blocked;
72                         }
73                 }
74
75                 if (!$update) {
76                         $blocklist[] = [
77                                 'domain' => $pattern,
78                                 'reason' => $reason,
79                         ];
80                 }
81
82                 return $this->set($blocklist) ? ($update ? 2 : 1) : 0;
83         }
84
85         /**
86          * @param string $pattern
87          *
88          * @return int 0 if the block list couldn't be saved, 1 if the pattern wasn't found, 2 if it was removed
89          */
90         public function removePattern(string $pattern): int
91         {
92                 $found = false;
93
94                 $blocklist = [];
95                 foreach ($this->get() as $blocked) {
96                         if ($blocked['domain'] === $pattern) {
97                                 $found = true;
98                         } else {
99                                 $blocklist[] = $blocked;
100                         }
101                 }
102
103                 return $found ? ($this->set($blocklist) ? 2 : 0) : 1;
104         }
105
106         /**
107          * @param string $filename
108          *
109          * @return void
110          * @throws Exception
111          */
112         public function exportToFile(string $filename)
113         {
114                 $fp = fopen($filename, 'w');
115                 if (!$fp) {
116                         throw new Exception(sprintf('The file "%s" could not be created.', $filename));
117                 }
118
119                 foreach ($this->get() as $domain) {
120                         fputcsv($fp, $domain);
121                 }
122         }
123
124         /**
125          * Appends to the local block list all the patterns from the provided list that weren't already present.
126          *
127          * @param array $blocklist
128          *
129          * @return int The number of patterns actually added to the block list
130          */
131         public function append(array $blocklist): int
132         {
133                 $localBlocklist = $this->get();
134                 $localPatterns  = array_column($localBlocklist, 'domain');
135
136                 $importedPatterns = array_column($blocklist, 'domain');
137
138                 $patternsToAppend = array_diff($importedPatterns, $localPatterns);
139
140                 if (count($patternsToAppend)) {
141                         foreach (array_keys($patternsToAppend) as $key) {
142                                 $localBlocklist[] = $blocklist[$key];
143                         }
144
145                         $this->set($localBlocklist);
146                 }
147
148                 return count($patternsToAppend);
149         }
150
151         /**
152          * Extracts a server domain pattern block list from the provided CSV file name. Deduplicates the list based on patterns.
153          *
154          * @param string $filename
155          *
156          * @return array
157          * @throws Exception
158          */
159         public static function extractFromCSVFile(string $filename): array
160         {
161                 $fp = fopen($filename, 'r');
162                 if ($fp === false) {
163                         throw new Exception(sprintf('The file "%s" could not be opened for importing', $filename));
164                 }
165
166                 $blocklist = [];
167                 while (($data = fgetcsv($fp, 1000)) !== false) {
168                         $item = [
169                                 'domain' => $data[0],
170                                 'reason' => $data[1] ?? '',
171                         ];
172                         if (!in_array($item, $blocklist)) {
173                                 $blocklist[] = $item;
174                         }
175                 }
176
177                 return $blocklist;
178         }
179 }