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 const DEFAULT_REASON = 'blocked';
36 * @var IManageConfigValues
40 public function __construct(IManageConfigValues $config)
42 $this->config = $config;
45 public function get(): array
47 return $this->config->get('system', 'blocklist', []);
50 public function set(array $blocklist): bool
52 return $this->config->set('system', 'blocklist', $blocklist);
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
60 public function addPattern(string $pattern, string $reason = null): int
65 foreach ($this->get() as $blocked) {
66 if ($blocked['domain'] === $pattern) {
69 'reason' => $reason ?? self::DEFAULT_REASON,
74 $blocklist[] = $blocked;
81 'reason' => $reason ?? self::DEFAULT_REASON,
85 return $this->set($blocklist) ? ($update ? 2 : 1) : 0;
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
92 public function removePattern(string $pattern): int
97 foreach ($this->get() as $blocked) {
98 if ($blocked['domain'] === $pattern) {
101 $blocklist[] = $blocked;
105 return $found ? ($this->set($blocklist) ? 2 : 0) : 1;
108 public function exportToFile(string $filename)
110 $fp = fopen($filename, 'w');
112 throw new Exception(sprintf('The file "%s" could not be created.', $filename));
115 foreach ($this->get() as $domain) {
116 fputcsv($fp, $domain);
121 * Appends to the local block list all the patterns from the provided list that weren't already present.
123 * @param array $blocklist
124 * @return int The number of patterns actually added to the block list
126 public function append(array $blocklist): int
128 $localBlocklist = $this->get();
129 $localPatterns = array_column($localBlocklist, 'domain');
131 $importedPatterns = array_column($blocklist, 'domain');
133 $patternsToAppend = array_diff($importedPatterns, $localPatterns);
135 if (count($patternsToAppend)) {
136 foreach (array_keys($patternsToAppend) as $key) {
137 $localBlocklist[] = $blocklist[$key];
140 $this->set($localBlocklist);
143 return count($patternsToAppend);
147 * Extracts a server domain pattern block list from the provided CSV file name. Deduplicates the list based on patterns.
149 * @param string $filename
153 public static function extractFromCSVFile(string $filename): array
155 $fp = fopen($filename, 'r');
157 throw new Exception(sprintf('The file "%s" could not be opened for importing', $filename));
161 while (($data = fgetcsv($fp, 1000)) !== false) {
163 if (count($data) == 0) {
164 $reason = self::DEFAULT_REASON;
173 if (!in_array($data, $blocklist)) {
174 $blocklist[] = $data;