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\App\BaseURL;
26 use Friendica\Core\Config\Capability\IManageConfigValues;
27 use Friendica\Core\L10n;
28 use Friendica\Database\Database;
29 use Friendica\Network\HTTPException;
30 use Friendica\Util\Emailer;
32 class DomainPatternBlocklist
34 const DEFAULT_REASON = 'blocked';
36 /** @var IManageConfigValues */
51 public function __construct(IManageConfigValues $config, Database $db, Emailer $emailer, L10n $l10n, BaseURL $baseUrl)
53 $this->config = $config;
55 $this->emailer = $emailer;
57 $this->baseUrl = $baseUrl;
60 public function get(): array
62 return $this->config->get('system', 'blocklist', []);
65 public function set(array $blocklist): bool
67 $result = $this->config->set('system', 'blocklist', $blocklist);
76 * @param string $pattern
77 * @param string|null $reason
78 * @return int 0 if the block list couldn't be saved, 1 if the pattern was added, 2 if it was updated in place
80 public function addPattern(string $pattern, string $reason = null): int
85 foreach ($this->get() as $blocked) {
86 if ($blocked['domain'] === $pattern) {
89 'reason' => $reason ?? self::DEFAULT_REASON,
94 $blocklist[] = $blocked;
100 'domain' => $pattern,
101 'reason' => $reason ?? self::DEFAULT_REASON,
105 return $this->set($blocklist) ? ($update ? 2 : 1) : 0;
109 * @param string $pattern
110 * @return int 0 if the block list couldn't be saved, 1 if the pattern wasn't found, 2 if it was removed
112 public function removePattern(string $pattern): int
117 foreach ($this->get() as $blocked) {
118 if ($blocked['domain'] === $pattern) {
121 $blocklist[] = $blocked;
125 return $found ? ($this->set($blocklist) ? 2 : 0) : 1;
128 public function exportToFile(string $filename)
130 $fp = fopen($filename, 'w');
132 throw new Exception(sprintf('The file "%s" could not be created.', $filename));
135 foreach ($this->get() as $domain) {
136 fputcsv($fp, $domain);
141 * Appends to the local block list all the patterns from the provided list that weren't already present.
143 * @param array $blocklist
144 * @return int The number of patterns actually added to the block list
146 public function append(array $blocklist): int
148 $localBlocklist = $this->get();
149 $localPatterns = array_column($localBlocklist, 'domain');
151 $importedPatterns = array_column($blocklist, 'domain');
153 $patternsToAppend = array_diff($importedPatterns, $localPatterns);
155 if (count($patternsToAppend)) {
156 foreach (array_keys($patternsToAppend) as $key) {
157 $localBlocklist[] = $blocklist[$key];
160 $this->set($localBlocklist);
163 return count($patternsToAppend);
167 * Extracts a server domain pattern block list from the provided CSV file name. Deduplicates the list based on patterns.
169 * @param string $filename
173 public static function extractFromCSVFile(string $filename): array
175 $fp = fopen($filename, 'r');
177 throw new Exception(sprintf('The file "%s" could not be opened for importing', $filename));
181 while (($data = fgetcsv($fp, 1000)) !== false) {
183 if (count($data) == 0) {
184 $reason = self::DEFAULT_REASON;
193 if (!in_array($data, $blocklist)) {
194 $blocklist[] = $data;
202 * Sends a system email to all the node users about a change in the block list. Sends a single email to each unique
203 * email address among the valid users.
205 * @return int The number of recipients that were sent an email
206 * @throws HTTPException\InternalServerErrorException
207 * @throws HTTPException\UnprocessableEntityException
209 public function notifyAll(): int
211 // Gathering all non-system parent users who verified their email address and aren't blocked or about to be deleted
212 // We sort on language to minimize the number of actual language switches during the email build loop
213 $recipients = $this->db->selectToArray(
215 ['username', 'email', 'language'],
216 ['`uid` > 0 AND `parent-uid` = 0 AND `verified` AND NOT `account_removed` AND NOT `account_expired` AND NOT `blocked`'],
217 ['group_by' => ['email'], 'order' => ['language']]
223 foreach ($recipients as $recipient) {
224 $this->l10n->withLang($recipient['language']);
225 $email = $this->emailer->newSystemMail()
227 $this->l10n->t('[%s] Notice of remote server domain pattern block list update', $this->emailer->getSiteEmailName()),
231 You are receiving this email because the Friendica node at %s where you are registered as a user updated their remote server domain pattern block list.
233 Please review the updated list at %s at your earliest convenience.',
234 $recipient['username'],
235 $this->baseUrl->get(),
236 $this->baseUrl . '/friendica'
239 ->withRecipient($recipient['email'])
241 $this->emailer->send($email);
244 return count($recipients);