3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module\Moderation\Blocklist\Server;
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;
35 class Import extends \Friendica\Module\BaseModeration
37 /** @var DomainPatternBlocklist */
38 private $localBlocklist;
40 /** @var array of blocked server domain patterns */
41 private $blocklist = [];
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 = [])
45 parent::__construct($page, $app, $systemMessages, $session, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
47 $this->localBlocklist = $localBlocklist;
51 * @param array $request
53 * @throws HTTPException\ForbiddenException
54 * @throws HTTPException\FoundException
55 * @throws HTTPException\InternalServerErrorException
56 * @throws HTTPException\MovedPermanentlyException
57 * @throws HTTPException\TemporaryRedirectException
59 protected function post(array $request = [])
61 $this->checkModerationAccess();
63 if (!isset($request['page_blocklist_upload']) && !isset($request['page_blocklist_import'])) {
67 self::checkFormSecurityTokenRedirectOnError('/moderation/blocklist/server/import', 'moderation_blocklist_import');
69 if (isset($request['page_blocklist_upload'])) {
71 $this->blocklist = $this->localBlocklist::extractFromCSVFile($_FILES['listfile']['tmp_name']);
72 } catch (\Throwable $e) {
73 $this->systemMessages->addNotice($this->t('Error importing pattern file'));
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'));
86 if (($request['mode'] ?? 'append') == 'replace') {
87 $this->localBlocklist->set($blocklist);
88 $this->systemMessages->addNotice($this->t('Local blocklist replaced with the provided file.'));
90 $count = $this->localBlocklist->append($blocklist);
92 $this->systemMessages->addNotice($this->tt('%d pattern was added to the local blocklist.', '%d patterns were added to the local blocklist.', $count));
94 $this->systemMessages->addNotice($this->t('No pattern was added to the local blocklist.'));
98 $this->baseUrl->redirect('/moderation/blocklist/server');
103 * @param array $request
105 * @throws HTTPException\ServiceUnavailableException
107 protected function content(array $request = []): string
111 $t = Renderer::getMarkupTemplate('moderation/blocklist/server/import.tpl');
112 return Renderer::replaceMacros($t, [
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)),
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')