3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Moderation;
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;
31 class DomainPatternBlocklist
33 /** @var IManageConfigValues */
36 public function __construct(IManageConfigValues $config)
38 $this->config = $config;
41 public function get(): array
43 return $this->config->get('system', 'blocklist', []);
46 public function set(array $blocklist): bool
48 return $this->config->set('system', 'blocklist', $blocklist);
52 * @param string $pattern
53 * @param string $reason
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
57 public function addPattern(string $pattern, string $reason): int
62 foreach ($this->get() as $blocked) {
63 if ($blocked['domain'] === $pattern) {
71 $blocklist[] = $blocked;
82 return $this->set($blocklist) ? ($update ? 2 : 1) : 0;
86 * @param string $pattern
88 * @return int 0 if the block list couldn't be saved, 1 if the pattern wasn't found, 2 if it was removed
90 public function removePattern(string $pattern): int
95 foreach ($this->get() as $blocked) {
96 if ($blocked['domain'] === $pattern) {
99 $blocklist[] = $blocked;
103 return $found ? ($this->set($blocklist) ? 2 : 0) : 1;
107 * @param string $filename
112 public function exportToFile(string $filename)
114 $fp = fopen($filename, 'w');
116 throw new Exception(sprintf('The file "%s" could not be created.', $filename));
119 foreach ($this->get() as $domain) {
120 fputcsv($fp, $domain);
125 * Appends to the local block list all the patterns from the provided list that weren't already present.
127 * @param array $blocklist
129 * @return int The number of patterns actually added to the block list
131 public function append(array $blocklist): int
133 $localBlocklist = $this->get();
134 $localPatterns = array_column($localBlocklist, 'domain');
136 $importedPatterns = array_column($blocklist, 'domain');
138 $patternsToAppend = array_diff($importedPatterns, $localPatterns);
140 if (count($patternsToAppend)) {
141 foreach (array_keys($patternsToAppend) as $key) {
142 $localBlocklist[] = $blocklist[$key];
145 $this->set($localBlocklist);
148 return count($patternsToAppend);
152 * Extracts a server domain pattern block list from the provided CSV file name. Deduplicates the list based on patterns.
154 * @param string $filename
159 public static function extractFromCSVFile(string $filename): array
161 $fp = fopen($filename, 'r');
163 throw new Exception(sprintf('The file "%s" could not be opened for importing', $filename));
167 while (($data = fgetcsv($fp, 1000)) !== false) {
169 'domain' => $data[0],
170 'reason' => $data[1] ?? '',
172 if (!in_array($item, $blocklist)) {
173 $blocklist[] = $item;