]> git.mxchange.org Git - friendica.git/blob - src/Module/Moderation/Blocklist/Server/Import.php
New area "moderation"
[friendica.git] / src / Module / Moderation / Blocklist / Server / Import.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\Server;
23
24 use Friendica\App;
25 use Friendica\Core\L10n;
26 use Friendica\Core\Renderer;
27 use Friendica\Core\Session\Capability\IHandleUserSessions;
28 use Friendica\Moderation\DomainPatternBlocklist;
29 use Friendica\Module\Response;
30 use Friendica\Navigation\SystemMessages;
31 use Friendica\Network\HTTPException;
32 use Friendica\Util\Profiler;
33 use Psr\Log\LoggerInterface;
34
35 class Import extends \Friendica\Module\BaseModeration
36 {
37         /** @var DomainPatternBlocklist */
38         private $localBlocklist;
39
40         /** @var array of blocked server domain patterns */
41         private $blocklist = [];
42
43         public function __construct(DomainPatternBlocklist $localBlocklist, 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 = [])
44         {
45                 parent::__construct($page, $app, $systemMessages, $session, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
46
47                 $this->localBlocklist = $localBlocklist;
48         }
49
50         /**
51          * @param array $request
52          * @return void
53          * @throws HTTPException\ForbiddenException
54          * @throws HTTPException\FoundException
55          * @throws HTTPException\InternalServerErrorException
56          * @throws HTTPException\MovedPermanentlyException
57          * @throws HTTPException\TemporaryRedirectException
58          */
59         protected function post(array $request = [])
60         {
61                 $this->checkModerationAccess();
62
63                 if (!isset($request['page_blocklist_upload']) && !isset($request['page_blocklist_import'])) {
64                         return;
65                 }
66
67                 self::checkFormSecurityTokenRedirectOnError('/moderation/blocklist/server/import', 'moderation_blocklist_import');
68
69                 if (isset($request['page_blocklist_upload'])) {
70                         try {
71                                 $this->blocklist = $this->localBlocklist::extractFromCSVFile($_FILES['listfile']['tmp_name']);
72                         } catch (\Throwable $e) {
73                                 $this->systemMessages->addNotice($this->t('Error importing pattern file'));
74                         }
75
76                         return;
77                 }
78
79                 if (isset($request['page_blocklist_import'])) {
80                         $blocklist = json_decode($request['blocklist'], true);
81                         if ($blocklist === null) {
82                                 $this->systemMessages->addNotice($this->t('Error importing pattern file'));
83                                 return;
84                         }
85
86                         if (($request['mode'] ?? 'append') == 'replace') {
87                                 $this->localBlocklist->set($blocklist);
88                                 $this->systemMessages->addNotice($this->t('Local blocklist replaced with the provided file.'));
89                         } else {
90                                 $count = $this->localBlocklist->append($blocklist);
91                                 if ($count) {
92                                         $this->systemMessages->addNotice($this->tt('%d pattern was added to the local blocklist.', '%d patterns were added to the local blocklist.', $count));
93                                 } else {
94                                         $this->systemMessages->addNotice($this->t('No pattern was added to the local blocklist.'));
95                                 }
96                         }
97
98                         $this->baseUrl->redirect('/moderation/blocklist/server');
99                 }
100         }
101
102         /**
103          * @param array $request
104          * @return string
105          * @throws HTTPException\ServiceUnavailableException
106          */
107         protected function content(array $request = []): string
108         {
109                 parent::content();
110
111                 $t = Renderer::getMarkupTemplate('moderation/blocklist/server/import.tpl');
112                 return Renderer::replaceMacros($t, [
113                         '$l10n' => [
114                                 'return_list'    => $this->t('← Return to the list'),
115                                 'title'          => $this->t('Moderation'),
116                                 'page'           => $this->t('Import a Server Domain Pattern Blocklist'),
117                                 'download'       => $this->t('<p>This file can be downloaded from the <code>/friendica</code> path of any Friendica server.</p>'),
118                                 'upload'         => $this->t('Upload file'),
119                                 'patterns'       => $this->t('Patterns to import'),
120                                 'domain_pattern' => $this->t('Domain Pattern'),
121                                 'block_reason'   => $this->t('Block Reason'),
122                                 'mode'           => $this->t('Import Mode'),
123                                 'import'         => $this->t('Import Patterns'),
124                                 'pattern_count'  => $this->tt('%d total pattern', '%d total patterns', count($this->blocklist)),
125                         ],
126                         '$listfile'            => ['listfile', $this->t('Server domain pattern blocklist CSV file'), '', '', $this->t('Required'), '', 'file'],
127                         '$mode_append'         => ['mode', $this->t('Append'), 'append', $this->t('Imports patterns from the file that weren\'t already existing in the current blocklist.'), 'checked="checked"'],
128                         '$mode_replace'        => ['mode', $this->t('Replace'), 'replace', $this->t('Replaces the current blocklist by the imported patterns.')],
129                         '$blocklist'           => $this->blocklist,
130                         '$baseurl'             => $this->baseUrl->get(true),
131                         '$form_security_token' => self::getFormSecurityToken('moderation_blocklist_import')
132                 ]);
133         }
134 }