]> git.mxchange.org Git - friendica.git/blob - src/Moderation/DomainPatternBlocklist.php
33bd189d6d56fb6b595dc74d19c724abac153a14
[friendica.git] / src / Moderation / DomainPatternBlocklist.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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         const DEFAULT_REASON = 'blocked';
34
35         /**
36          * @var IManageConfigValues
37          */
38         private $config;
39
40         public function __construct(IManageConfigValues $config)
41         {
42                 $this->config = $config;
43         }
44
45         public function get(): array
46         {
47                 return $this->config->get('system', 'blocklist', []);
48         }
49
50         public function set(array $blocklist): bool
51         {
52                 return $this->config->set('system', 'blocklist', $blocklist);
53         }
54
55         /**
56          * @param string      $pattern
57          * @param string|null $reason
58          * @return int 0 if the block list couldn't be saved, 1 if the pattern was added, 2 if it was updated in place
59          */
60         public function addPattern(string $pattern, string $reason = null): int
61         {
62                 $update = false;
63
64                 $blocklist = [];
65                 foreach ($this->get() as $blocked) {
66                         if ($blocked['domain'] === $pattern) {
67                                 $blocklist[] = [
68                                         'domain' => $pattern,
69                                         'reason' => $reason ?? self::DEFAULT_REASON,
70                                 ];
71
72                                 $update = true;
73                         } else {
74                                 $blocklist[] = $blocked;
75                         }
76                 }
77
78                 if (!$update) {
79                         $blocklist[] = [
80                                 'domain' => $pattern,
81                                 'reason' => $reason ?? self::DEFAULT_REASON,
82                         ];
83                 }
84
85                 return $this->set($blocklist) ? ($update ? 2 : 1) : 0;
86         }
87
88         /**
89          * @param string $pattern
90          * @return int 0 if the block list couldn't be saved, 1 if the pattern wasn't found, 2 if it was removed
91          */
92         public function removePattern(string $pattern): int
93         {
94                 $found = false;
95
96                 $blocklist = [];
97                 foreach ($this->get() as $blocked) {
98                         if ($blocked['domain'] === $pattern) {
99                                 $found = true;
100                         } else {
101                                 $blocklist[] = $blocked;
102                         }
103                 }
104
105                 return $found ? ($this->set($blocklist) ? 2 : 0) : 1;
106         }
107
108         public function exportToFile(string $filename)
109         {
110                 $fp = fopen($filename, 'w');
111                 if (!$fp) {
112                         throw new Exception(sprintf('The file "%s" could not be created.', $filename));
113                 }
114
115                 foreach ($this->get() as $domain) {
116                         fputcsv($fp, $domain);
117                 }
118         }
119
120         /**
121          * Appends to the local block list all the patterns from the provided list that weren't already present.
122          *
123          * @param array $blocklist
124          * @return int The number of patterns actually added to the block list
125          */
126         public function append(array $blocklist): int
127         {
128                 $localBlocklist = $this->get();
129                 $localPatterns  = array_column($localBlocklist, 'domain');
130
131                 $importedPatterns = array_column($blocklist, 'domain');
132
133                 $patternsToAppend = array_diff($importedPatterns, $localPatterns);
134
135                 if (count($patternsToAppend)) {
136                         foreach (array_keys($patternsToAppend) as $key) {
137                                 $localBlocklist[] = $blocklist[$key];
138                         }
139
140                         $this->set($localBlocklist);
141                 }
142
143                 return count($patternsToAppend);
144         }
145
146         /**
147          * Extracts a server domain pattern block list from the provided CSV file name. Deduplicates the list based on patterns.
148          *
149          * @param string $filename
150          * @return array
151          * @throws Exception
152          */
153         public static function extractFromCSVFile(string $filename): array
154         {
155                 $fp = fopen($filename, 'r');
156                 if ($fp === false) {
157                         throw new Exception(sprintf('The file "%s" could not be opened for importing', $filename));
158                 }
159
160                 $blocklist = [];
161                 while (($data = fgetcsv($fp, 1000)) !== false) {
162                         $domain = $data[0];
163                         if (count($data) == 0) {
164                                 $reason = self::DEFAULT_REASON;
165                         } else {
166                                 $reason = $data[1];
167                         }
168
169                         $data = [
170                                 'domain' => $domain,
171                                 'reason' => $reason
172                         ];
173                         if (!in_array($data, $blocklist)) {
174                                 $blocklist[] = $data;
175                         }
176                 }
177
178                 return $blocklist;
179         }
180 }