]> git.mxchange.org Git - friendica.git/blob - src/Module/Admin/Blocklist/Contact.php
Merge pull request #10883 from MrPetovan/task/10864-server-purge
[friendica.git] / src / Module / Admin / Blocklist / Contact.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\Admin\Blocklist;
23
24 use Friendica\Content\Pager;
25 use Friendica\Core\Renderer;
26 use Friendica\Core\Worker;
27 use Friendica\Database\DBA;
28 use Friendica\DI;
29 use Friendica\Model;
30 use Friendica\Module\BaseAdmin;
31 use Friendica\Util\Network;
32
33 class Contact extends BaseAdmin
34 {
35         public static function post(array $parameters = [])
36         {
37                 self::checkAdminAccess();
38
39                 self::checkFormSecurityTokenRedirectOnError('/admin/blocklist/contact', 'admin_contactblock');
40
41                 $contact_url  = $_POST['contact_url'] ?? '';
42                 $block_reason = $_POST['contact_block_reason'] ?? '';
43                 $block_purge  = $_POST['contact_block_purge'] ?? false;
44                 $contacts     = $_POST['contacts'] ?? [];
45
46                 if (!empty($_POST['page_contactblock_block'])) {
47                         $contact = Model\Contact::getByURL($contact_url, null, ['id', 'nurl']);
48                         if (empty($contact)) {
49                                 notice(DI::l10n()->t('Could not find any contact entry for this URL (%s)', $contact_url));
50                                 DI::baseUrl()->redirect('admin/blocklist/contact');
51                         }
52
53                         if (Network::isLocalLink($contact['nurl'])) {
54                                 notice(DI::l10n()->t('You can\'t block a local contact, please block the user instead'));
55                                 DI::baseUrl()->redirect('admin/blocklist/contact');
56                         }
57
58                         Model\Contact::block($contact['id'], $block_reason);
59
60                         if ($block_purge) {
61                                 foreach (Model\Contact::selectToArray(['id'], ['nurl' => $contact['nurl']]) as $contact) {
62                                         Worker::add(PRIORITY_LOW, 'Contact\RemoveContent', $contact['id']);
63                                 }
64                         }
65
66                         info(DI::l10n()->t('The contact has been blocked from the node'));
67                 }
68
69                 if (!empty($_POST['page_contactblock_unblock'])) {
70                         foreach ($contacts as $uid) {
71                                 Model\Contact::unblock($uid);
72                         }
73                         info(DI::l10n()->tt('%s contact unblocked', '%s contacts unblocked', count($contacts)));
74                 }
75
76                 DI::baseUrl()->redirect('admin/blocklist/contact');
77         }
78
79         public static function content(array $parameters = [])
80         {
81                 parent::content($parameters);
82
83                 $condition = ['uid' => 0, 'blocked' => true];
84
85                 $total = DBA::count('contact', $condition);
86
87                 $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), 30);
88
89                 $contacts = Model\Contact::selectToArray([], $condition, ['limit' => [$pager->getStart(), $pager->getItemsPerPage()]]);
90
91                 $t = Renderer::getMarkupTemplate('admin/blocklist/contact.tpl');
92                 $o = Renderer::replaceMacros($t, [
93                         // strings //
94                         '$title'       => DI::l10n()->t('Administration'),
95                         '$page'        => DI::l10n()->t('Remote Contact Blocklist'),
96                         '$description' => DI::l10n()->t('This page allows you to prevent any message from a remote contact to reach your node.'),
97                         '$submit'      => DI::l10n()->t('Block Remote Contact'),
98                         '$select_all'  => DI::l10n()->t('select all'),
99                         '$select_none' => DI::l10n()->t('select none'),
100                         '$block'       => DI::l10n()->t('Block'),
101                         '$unblock'     => DI::l10n()->t('Unblock'),
102                         '$no_data'     => DI::l10n()->t('No remote contact is blocked from this node.'),
103
104                         '$h_contacts'  => DI::l10n()->t('Blocked Remote Contacts'),
105                         '$h_newblock'  => DI::l10n()->t('Block New Remote Contact'),
106                         '$th_contacts' => [DI::l10n()->t('Photo'), DI::l10n()->t('Name'), DI::l10n()->t('Reason')],
107
108                         '$form_security_token' => self::getFormSecurityToken('admin_contactblock'),
109
110                         // values //
111                         '$baseurl'    => DI::baseUrl()->get(true),
112
113                         '$contacts'   => $contacts,
114                         '$total_contacts' => DI::l10n()->tt('%s total blocked contact', '%s total blocked contacts', $total),
115                         '$paginate'   => $pager->renderFull($total),
116                         '$contacturl' => ['contact_url', DI::l10n()->t('Profile URL'), '', DI::l10n()->t('URL of the remote contact to block.')],
117                         '$contact_block_purge'  => ['contact_block_purge', DI::l10n()->t('Also purge contact'), false, DI::l10n()->t('Removes all content related to this contact from the node. Keeps the contact record. This action canoot be undone.')],
118                         '$contact_block_reason' => ['contact_block_reason', DI::l10n()->t('Block Reason')],
119                 ]);
120                 return $o;
121         }
122 }