]> git.mxchange.org Git - friendica.git/blob - src/Module/Moderation/Blocklist/Server/Add.php
News "blocked" field on the gserver table
[friendica.git] / src / Module / Moderation / Blocklist / Server / Add.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\Server;
23
24 use Friendica\App;
25 use Friendica\Content\ContactSelector;
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\Model\Contact;
31 use Friendica\Model\GServer;
32 use Friendica\Moderation\DomainPatternBlocklist;
33 use Friendica\Module\BaseModeration;
34 use Friendica\Module\Response;
35 use Friendica\Navigation\SystemMessages;
36 use Friendica\Network\HTTPException;
37 use Friendica\Util\Profiler;
38 use GuzzleHttp\Psr7\Uri;
39 use Psr\Log\LoggerInterface;
40
41 class Add extends BaseModeration
42 {
43         /** @var DomainPatternBlocklist */
44         private $blocklist;
45
46         public function __construct(DomainPatternBlocklist $blocklist, 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 = [])
47         {
48                 parent::__construct($page, $app, $systemMessages, $session, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
49
50                 $this->blocklist = $blocklist;
51         }
52
53         /**
54          * @param array $request
55          * @return void
56          * @throws HTTPException\ForbiddenException
57          * @throws HTTPException\FoundException
58          * @throws HTTPException\InternalServerErrorException
59          * @throws HTTPException\MovedPermanentlyException
60          * @throws HTTPException\TemporaryRedirectException
61          * @throws \Exception
62          */
63         protected function post(array $request = [])
64         {
65                 $this->checkModerationAccess();
66
67                 if (empty($request['page_blocklist_add'])) {
68                         return;
69                 }
70
71                 self::checkFormSecurityTokenRedirectOnError('/moderation/blocklist/server/add', 'moderation_blocklist_add');
72
73                 $pattern = trim($request['pattern']);
74
75                 //  Add new item to blocklist
76                 $this->blocklist->addPattern($pattern, trim($request['reason']));
77
78                 Worker::add(Worker::PRIORITY_LOW, 'UpdateBlockedServers');
79
80                 $this->systemMessages->addInfo($this->t('Server domain pattern added to the blocklist.'));
81
82                 if (!empty($request['purge'])) {
83                         $gservers = GServer::listByDomainPattern($pattern);
84                         foreach (Contact::selectToArray(['id'], ['gsid' => array_column($gservers, 'id')]) as $contact) {
85                                 Worker::add(Worker::PRIORITY_LOW, 'Contact\RemoveContent', $contact['id']);
86                         }
87
88                         $this->systemMessages->addInfo($this->tt('%s server scheduled to be purged.', '%s servers scheduled to be purged.', count($gservers)));
89                 }
90
91                 $this->baseUrl->redirect('moderation/blocklist/server');
92         }
93
94         /**
95          * @param array $request
96          * @return string
97          * @throws HTTPException\InternalServerErrorException
98          * @throws HTTPException\ServiceUnavailableException
99          * @throws \Exception
100          */
101         protected function content(array $request = []): string
102         {
103                 parent::content();
104
105                 $gservers = [];
106
107                 if ($pattern = trim($request['pattern'] ?? '')) {
108                         $gservers = GServer::listByDomainPattern($pattern);
109                 }
110
111                 array_walk($gservers, function (array &$gserver) {
112                         $gserver['domain'] = (new Uri($gserver['url']))->getHost();
113                         $gserver['network_icon'] = ContactSelector::networkToIcon($gserver['network']);
114                         $gserver['network_name'] = ContactSelector::networkToName($gserver['network']);
115                 });
116
117                 $t = Renderer::getMarkupTemplate('moderation/blocklist/server/add.tpl');
118                 return Renderer::replaceMacros($t, [
119                         '$l10n' => [
120                                 'return_list' => $this->t('← Return to the list'),
121                                 'title'       => $this->t('Moderation'),
122                                 'page'        => $this->t('Block A New Server Domain Pattern'),
123                                 'syntax'      => $this->t('<p>The server domain pattern syntax is case-insensitive shell wildcard, comprising the following special characters:</p>
124 <ul>
125         <li><code>*</code>: Any number of characters</li>
126         <li><code>?</code>: Any single character</li>
127 </ul>'),
128                                 'submit'           => $this->t('Check pattern'),
129                                 'matching_servers' => $this->t('Matching known servers'),
130                                 'server_name'      => $this->t('Server Name'),
131                                 'server_domain'    => $this->t('Server Domain'),
132                                 'known_contacts'   => $this->t('Known Contacts'),
133                                 'server_count'     => $this->tt('%d known server', '%d known servers', count($gservers)),
134                                 'add_pattern'      => $this->t('Add pattern to the blocklist'),
135                         ],
136                         '$newdomain'           => ['pattern', $this->t('Server Domain Pattern'), $pattern, $this->t('The domain pattern of the new server to add to the blocklist. Do not include the protocol.'), $this->t('Required'), '', ''],
137                         '$newpurge'            => ['purge', $this->t('Purge server'), $request['purge'] ?? false, $this->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))],
138                         '$newreason'           => ['reason', $this->t('Block reason'), $request['reason'] ?? '', $this->t('The reason why you blocked this server domain pattern. This reason will be shown publicly in the server information page.'), $this->t('Required'), '', ''],
139                         '$pattern'             => $pattern,
140                         '$gservers'            => $gservers,
141                         '$baseurl'             => $this->baseUrl->get(true),
142                         '$form_security_token' => self::getFormSecurityToken('moderation_blocklist_add')
143                 ]);
144         }
145 }