]> git.mxchange.org Git - friendica.git/blob - src/Module/Admin/Blocklist/Server/Add.php
Merge pull request #11141 from urbalazs/language-names
[friendica.git] / src / Module / Admin / Blocklist / Server / Add.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\Admin\Blocklist\Server;
23
24 use Friendica\Content\ContactSelector;
25 use Friendica\Core\Renderer;
26 use Friendica\Core\Worker;
27 use Friendica\DI;
28 use Friendica\Model\Contact;
29 use Friendica\Model\GServer;
30 use Friendica\Module\BaseAdmin;
31 use GuzzleHttp\Psr7\Uri;
32
33 class Add extends BaseAdmin
34 {
35         protected function post(array $request = [])
36         {
37                 self::checkAdminAccess();
38
39                 if (empty($_POST['page_blocklist_add'])) {
40                         return;
41                 }
42
43                 self::checkFormSecurityTokenRedirectOnError('/admin/blocklist/server/add', 'admin_blocklist_add');
44
45                 //  Add new item to blocklist
46                 $domain = trim($_POST['pattern']);
47
48                 $blocklist   = DI::config()->get('system', 'blocklist');
49                 $blocklist[] = [
50                         'domain' => $domain,
51                         'reason' => trim($_POST['reason']),
52                 ];
53                 DI::config()->set('system', 'blocklist', $blocklist);
54
55                 info(DI::l10n()->t('Server domain pattern added to the blocklist.'));
56
57                 if (!empty($_POST['purge'])) {
58                         $gservers = GServer::listByDomainPattern($domain);
59                         foreach (Contact::selectToArray(['id'], ['gsid' => array_column($gservers, 'id')]) as $contact) {
60                                 Worker::add(PRIORITY_LOW, 'Contact\RemoveContent', $contact['id']);
61                         }
62
63                         info(DI::l10n()->tt('%s server scheduled to be purged.', '%s servers scheduled to be purged.', count($gservers)));
64                 }
65
66                 DI::baseUrl()->redirect('admin/blocklist/server');
67         }
68
69         protected function content(array $request = []): string
70         {
71                 parent::content();
72
73                 $gservers = [];
74
75                 if ($pattern = trim($_REQUEST['pattern'] ?? '')) {
76                         $gservers = GServer::listByDomainPattern($pattern);
77                 }
78
79                 array_walk($gservers, function (array &$gserver) {
80                         $gserver['domain'] = (new Uri($gserver['url']))->getHost();
81                         $gserver['network_icon'] = ContactSelector::networkToIcon($gserver['network']);
82                         $gserver['network_name'] = ContactSelector::networkToName($gserver['network']);
83                 });
84
85                 $t = Renderer::getMarkupTemplate('admin/blocklist/server/add.tpl');
86                 return Renderer::replaceMacros($t, [
87                         '$l10n' => [
88                                 'return_list' => DI::l10n()->t('← Return to the list'),
89                                 'title'       => DI::l10n()->t('Administration'),
90                                 'page'        => DI::l10n()->t('Block A New Server Domain Pattern'),
91                                 'syntax'      => DI::l10n()->t('<p>The server domain pattern syntax is case-insensitive shell wildcard, comprising the following special characters:</p>
92 <ul>
93         <li><code>*</code>: Any number of characters</li>
94         <li><code>?</code>: Any single character</li>
95 </ul>'),
96                                 'submit'           => DI::l10n()->t('Check pattern'),
97                                 'matching_servers' => DI::l10n()->t('Matching known servers'),
98                                 'server_name'      => DI::l10n()->t('Server Name'),
99                                 'server_domain'    => DI::l10n()->t('Server Domain'),
100                                 'known_contacts'   => DI::l10n()->t('Known Contacts'),
101                                 'server_count'     => DI::l10n()->tt('%d known server', '%d known servers', count($gservers)),
102                                 'add_pattern'      => DI::l10n()->t('Add pattern to the blocklist'),
103                         ],
104                         '$newdomain'           => ['pattern', DI::l10n()->t('Server Domain Pattern'), $pattern, DI::l10n()->t('The domain pattern of the new server to add to the blocklist. Do not include the protocol.'), DI::l10n()->t('Required'), '', ''],
105                         '$newpurge'            => ['purge', DI::l10n()->t('Purge server'), $_REQUEST['purge'] ?? false, DI::l10n()->tt('Also purges all the locally stored content authored by the known contacts registered on that server. Keeps the contacts and the server records. This action cannot be undone.', 'Also purges all the locally stored content authored by the known contacts registered on these servers. Keeps the contacts and the servers records. This action cannot be undone.', count($gservers))],
106                         '$newreason'           => ['reason', DI::l10n()->t('Block reason'), $_REQUEST['reason'] ?? '', DI::l10n()->t('The reason why you blocked this server domain pattern. This reason will be shown publicly in the server information page.'), DI::l10n()->t('Required'), '', ''],
107                         '$pattern'             => $pattern,
108                         '$gservers'            => $gservers,
109                         '$baseurl'             => DI::baseUrl()->get(true),
110                         '$form_security_token' => self::getFormSecurityToken('admin_blocklist_add')
111                 ]);
112         }
113 }