]> git.mxchange.org Git - friendica.git/blob - src/Module/Moderation/Blocklist/Contact.php
New area "moderation"
[friendica.git] / src / Module / Moderation / Blocklist / Contact.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Module\Moderation\Blocklist;
23
24 use Friendica\App;
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;
31 use Friendica\Model;
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;
38
39 class Contact extends BaseModeration
40 {
41         /** @var Database */
42         private $database;
43
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 = [])
45         {
46                 parent::__construct($page, $app, $systemMessages, $session, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
47
48                 $this->database = $database;
49         }
50
51         protected function post(array $request = [])
52         {
53                 $this->checkModerationAccess();
54
55                 self::checkFormSecurityTokenRedirectOnError('/moderation/blocklist/contact', 'moderation_contactblock');
56
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']             ?? [];
61
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');
67                         }
68
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');
72                         }
73
74                         Model\Contact::block($contact['id'], $block_reason);
75
76                         if ($block_purge) {
77                                 foreach (Model\Contact::selectToArray(['id'], ['nurl' => $contact['nurl']]) as $contact) {
78                                         Worker::add(Worker::PRIORITY_LOW, 'Contact\RemoveContent', $contact['id']);
79                                 }
80                         }
81
82                         $this->systemMessages->addInfo($this->t('The contact has been blocked from the node'));
83                 }
84
85                 if (!empty($request['page_contactblock_unblock'])) {
86                         foreach ($contacts as $uid) {
87                                 Model\Contact::unblock($uid);
88                         }
89                         $this->systemMessages->addInfo($this->tt('%s contact unblocked', '%s contacts unblocked', count($contacts)));
90                 }
91
92                 $this->baseUrl->redirect('moderation/blocklist/contact');
93         }
94
95         protected function content(array $request = []): string
96         {
97                 parent::content();
98
99                 $condition = ['uid' => 0, 'blocked' => true];
100
101                 $total = $this->database->count('contact', $condition);
102
103                 $pager = new Pager($this->l10n, $this->args->getQueryString(), 30);
104
105                 $contacts = Model\Contact::selectToArray([], $condition, ['limit' => [$pager->getStart(), $pager->getItemsPerPage()]]);
106
107                 $t = Renderer::getMarkupTemplate('moderation/blocklist/contact.tpl');
108                 return Renderer::replaceMacros($t, [
109                         // strings //
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.'),
119
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')],
123
124                         '$form_security_token' => self::getFormSecurityToken('moderation_contactblock'),
125
126                         // values //
127                         '$baseurl' => $this->baseUrl->get(true),
128
129                         '$contacts'       => $contacts,
130                         '$total_contacts' => $this->tt('%s total blocked contact', '%s total blocked contacts', $total),
131                         '$paginate'       => $pager->renderFull($total),
132
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')],
136                 ]);
137         }
138 }