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 /** @var IManageConfigValues */
49 public function __construct(IManageConfigValues $config, Database $db, Emailer $emailer, L10n $l10n, BaseURL $baseUrl)
51 $this->config = $config;
53 $this->emailer = $emailer;
55 $this->baseUrl = $baseUrl;
58 public function get(): array
60 return $this->config->get('system', 'blocklist', []);
63 public function set(array $blocklist): bool
65 $result = $this->config->set('system', 'blocklist', $blocklist);
74 * @param string $pattern
75 * @param string $reason
77 * @return int 0 if the block list couldn't be saved, 1 if the pattern was added, 2 if it was updated in place
79 public function addPattern(string $pattern, string $reason): int
84 foreach ($this->get() as $blocked) {
85 if ($blocked['domain'] === $pattern) {
93 $blocklist[] = $blocked;
104 return $this->set($blocklist) ? ($update ? 2 : 1) : 0;
108 * @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;
129 * @param string $filename
134 public function exportToFile(string $filename)
136 $fp = fopen($filename, 'w');
138 throw new Exception(sprintf('The file "%s" could not be created.', $filename));
141 foreach ($this->get() as $domain) {
142 fputcsv($fp, $domain);
147 * Appends to the local block list all the patterns from the provided list that weren't already present.
149 * @param array $blocklist
151 * @return int The number of patterns actually added to the block list
153 public function append(array $blocklist): int
155 $localBlocklist = $this->get();
156 $localPatterns = array_column($localBlocklist, 'domain');
158 $importedPatterns = array_column($blocklist, 'domain');
160 $patternsToAppend = array_diff($importedPatterns, $localPatterns);
162 if (count($patternsToAppend)) {
163 foreach (array_keys($patternsToAppend) as $key) {
164 $localBlocklist[] = $blocklist[$key];
167 $this->set($localBlocklist);
170 return count($patternsToAppend);
174 * Extracts a server domain pattern block list from the provided CSV file name. Deduplicates the list based on patterns.
176 * @param string $filename
181 public static function extractFromCSVFile(string $filename): array
183 $fp = fopen($filename, 'r');
185 throw new Exception(sprintf('The file "%s" could not be opened for importing', $filename));
189 while (($data = fgetcsv($fp, 1000)) !== false) {
191 'domain' => $data[0],
192 'reason' => $data[1] ?? '',
194 if (!in_array($item, $blocklist)) {
195 $blocklist[] = $data;
203 * Sends a system email to all the node users about a change in the block list. Sends a single email to each unique
204 * email address among the valid users.
206 * @return int The number of recipients that were sent an email
207 * @throws HTTPException\InternalServerErrorException
208 * @throws HTTPException\UnprocessableEntityException
210 public function notifyAll(): int
212 // Gathering all non-system parent users who verified their email address and aren't blocked or about to be deleted
213 // We sort on language to minimize the number of actual language switches during the email build loop
214 $recipients = $this->db->selectToArray(
216 ['username', 'email', 'language'],
217 ['`uid` > 0 AND `parent-uid` = 0 AND `verified` AND NOT `account_removed` AND NOT `account_expired` AND NOT `blocked`'],
218 ['group_by' => ['email'], 'order' => ['language']]
224 foreach ($recipients as $recipient) {
225 $l10n = $this->l10n->withLang($recipient['language']);
226 $email = $this->emailer->newSystemMail()
228 $l10n->t('[%s] Notice of remote server domain pattern block list update', $this->emailer->getSiteEmailName()),
232 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.
234 Please review the updated list at %s at your earliest convenience.',
235 $recipient['username'],
236 $this->baseUrl->get(),
237 $this->baseUrl . '/friendica'
240 ->withRecipient($recipient['email'])
242 $this->emailer->send($email);
245 return count($recipients);