3 * @copyright Copyright (C) 2010-2023, 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\Module\Moderation\Blocklist;
25 use Friendica\Content\Pager;
26 use Friendica\Core\L10n;
27 use Friendica\Core\Renderer;
28 use Friendica\Core\Session\Capability\IHandleUserSessions;
29 use Friendica\Core\Worker;
30 use Friendica\Database\Database;
32 use Friendica\Module\BaseModeration;
33 use Friendica\Module\Response;
34 use Friendica\Navigation\SystemMessages;
35 use Friendica\Util\Network;
36 use Friendica\Util\Profiler;
37 use Psr\Log\LoggerInterface;
39 class Contact extends BaseModeration
44 public function __construct(Database $database, App\Page $page, App $app, SystemMessages $systemMessages, IHandleUserSessions $session, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
46 parent::__construct($page, $app, $systemMessages, $session, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
48 $this->database = $database;
51 protected function post(array $request = [])
53 $this->checkModerationAccess();
55 self::checkFormSecurityTokenRedirectOnError('/moderation/blocklist/contact', 'moderation_contactblock');
57 $contact_url = $request['contact_url'] ?? '';
58 $block_reason = $request['contact_block_reason'] ?? '';
59 $block_purge = $request['contact_block_purge'] ?? false;
60 $contacts = $request['contacts'] ?? [];
62 if (!empty($request['page_contactblock_block'])) {
63 $contact = Model\Contact::getByURL($contact_url, null, ['id', 'nurl']);
64 if (empty($contact)) {
65 $this->systemMessages->addNotice($this->t('Could not find any contact entry for this URL (%s)', $contact_url));
66 $this->baseUrl->redirect('moderation/blocklist/contact');
69 if (Network::isLocalLink($contact['nurl'])) {
70 $this->systemMessages->addNotice($this->t('You can\'t block a local contact, please block the user instead'));
71 $this->baseUrl->redirect('moderation/blocklist/contact');
74 Model\Contact::block($contact['id'], $block_reason);
77 foreach (Model\Contact::selectToArray(['id'], ['nurl' => $contact['nurl']]) as $contact) {
78 Worker::add(Worker::PRIORITY_LOW, 'Contact\RemoveContent', $contact['id']);
82 $this->systemMessages->addInfo($this->t('The contact has been blocked from the node'));
85 if (!empty($request['page_contactblock_unblock'])) {
86 foreach ($contacts as $uid) {
87 Model\Contact::unblock($uid);
89 $this->systemMessages->addInfo($this->tt('%s contact unblocked', '%s contacts unblocked', count($contacts)));
92 $this->baseUrl->redirect('moderation/blocklist/contact');
95 protected function content(array $request = []): string
99 $condition = ['uid' => 0, 'blocked' => true];
101 $total = $this->database->count('contact', $condition);
103 $pager = new Pager($this->l10n, $this->args->getQueryString(), 30);
105 $contacts = Model\Contact::selectToArray([], $condition, ['limit' => [$pager->getStart(), $pager->getItemsPerPage()]]);
107 $t = Renderer::getMarkupTemplate('moderation/blocklist/contact.tpl');
108 return Renderer::replaceMacros($t, [
110 '$title' => $this->t('Moderation'),
111 '$page' => $this->t('Remote Contact Blocklist'),
112 '$description' => $this->t('This page allows you to prevent any message from a remote contact to reach your node.'),
113 '$submit' => $this->t('Block Remote Contact'),
114 '$select_all' => $this->t('select all'),
115 '$select_none' => $this->t('select none'),
116 '$block' => $this->t('Block'),
117 '$unblock' => $this->t('Unblock'),
118 '$no_data' => $this->t('No remote contact is blocked from this node.'),
120 '$h_contacts' => $this->t('Blocked Remote Contacts'),
121 '$h_newblock' => $this->t('Block New Remote Contact'),
122 '$th_contacts' => [$this->t('Photo'), $this->t('Name'), $this->t('Reason')],
124 '$form_security_token' => self::getFormSecurityToken('moderation_contactblock'),
127 '$baseurl' => $this->baseUrl,
129 '$contacts' => $contacts,
130 '$total_contacts' => $this->tt('%s total blocked contact', '%s total blocked contacts', $total),
131 '$paginate' => $pager->renderFull($total),
133 '$contacturl' => ['contact_url', $this->t('Profile URL'), '', $this->t('URL of the remote contact to block.')],
134 '$contact_block_purge' => ['contact_block_purge', $this->t('Also purge contact'), false, $this->t('Removes all content related to this contact from the node. Keeps the contact record. This action cannot be undone.')],
135 '$contact_block_reason' => ['contact_block_reason', $this->t('Block Reason')],