From e11d3cfeb0c39b672318299ea9ae2e0bd3f3bfc3 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Wed, 27 Jul 2022 11:54:50 -0400 Subject: [PATCH] Add upload feature for domain pattern block list - Add new /admin/blocklist/server/import route - Add form on domain pattern blocklist index page --- src/Module/Admin/Blocklist/Server/Import.php | 163 ++++++++++++++++++ src/Module/Admin/Blocklist/Server/Index.php | 8 +- static/routes.config.php | 7 +- .../admin/blocklist/server/import.tpl | 52 ++++++ .../admin/blocklist/server/index.tpl | 15 +- 5 files changed, 238 insertions(+), 7 deletions(-) create mode 100644 src/Module/Admin/Blocklist/Server/Import.php create mode 100644 view/templates/admin/blocklist/server/import.tpl diff --git a/src/Module/Admin/Blocklist/Server/Import.php b/src/Module/Admin/Blocklist/Server/Import.php new file mode 100644 index 0000000000..0714be5be9 --- /dev/null +++ b/src/Module/Admin/Blocklist/Server/Import.php @@ -0,0 +1,163 @@ +. + * + */ + +namespace Friendica\Module\Admin\Blocklist\Server; + +use Friendica\App; +use Friendica\Core\Config\Capability\IManageConfigValues; +use Friendica\Core\L10n; +use Friendica\Core\Renderer; +use Friendica\Module\Response; +use Friendica\Navigation\SystemMessages; +use Friendica\Util\Profiler; +use Psr\Log\LoggerInterface; + +class Import extends \Friendica\Module\BaseAdmin +{ + /** @var IManageConfigValues */ + private $config; + + /** @var SystemMessages */ + private $sysmsg; + + /** @var array of blocked server domain patterns */ + private $blocklist = []; + + public function __construct(IManageConfigValues $config, SystemMessages $sysmsg, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = []) + { + parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); + + $this->config = $config; + $this->sysmsg = $sysmsg; + } + + /** + * @param array $request + * @return void + * @throws \Friendica\Network\HTTPException\ForbiddenException + * @throws \Friendica\Network\HTTPException\FoundException + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + * @throws \Friendica\Network\HTTPException\MovedPermanentlyException + * @throws \Friendica\Network\HTTPException\TemporaryRedirectException + */ + protected function post(array $request = []) + { + self::checkAdminAccess(); + + if (!isset($_POST['page_blocklist_upload']) && !isset($_POST['page_blocklist_import'])) { + return; + } + + self::checkFormSecurityTokenRedirectOnError('/admin/blocklist/server/import', 'admin_blocklist_import'); + + if (isset($_POST['page_blocklist_upload'])) { + if (($fp = fopen($_FILES['listfile']['tmp_name'], 'r')) !== false) { + $blocklist = []; + while (($data = fgetcsv($fp, 1000, ',')) !== false) { + $domain = $data[0]; + if (count($data) == 0) { + $reason = 'blocked'; + } else { + $reason = $data[1]; + } + + $blocklist[] = [ + 'domain' => $domain, + 'reason' => $reason + ]; + } + } else { + $this->sysmsg->addNotice($this->l10n->t('Error importing pattern file')); + return; + } + + $this->blocklist = $blocklist; + + return; + } + + if (isset($_POST['page_blocklist_import'])) { + $blocklist = json_decode($_POST['blocklist'], true); + if ($blocklist === null) { + $this->sysmsg->addNotice($this->l10n->t('Error importing pattern file')); + return; + } + + if (($_POST['mode'] ?? 'append') == 'replace') { + $this->config->set('system', 'blocklist', $blocklist); + $this->sysmsg->addNotice($this->l10n->t('Local blocklist replaced with the provided file.')); + } else { + $localBlocklist = $this->config->get('system', 'blocklist', []); + $localPatterns = array_column($localBlocklist, 'domain'); + + $importedPatterns = array_column($blocklist, 'domain'); + + $patternsToAppend = array_diff($importedPatterns, $localPatterns); + + if (count($patternsToAppend)) { + foreach (array_keys($patternsToAppend) as $key) { + $localBlocklist[] = $blocklist[$key]; + } + + $this->config->set('system', 'blocklist', $localBlocklist); + $this->sysmsg->addNotice($this->l10n->tt('%d pattern was added to the local blocklist.', '%d patterns were added to the local blocklist.', count($patternsToAppend))); + } else { + $this->sysmsg->addNotice($this->l10n->t('No pattern was added to the local blocklist.')); + } + } + + $this->baseUrl->redirect('/admin/blocklist/server'); + } + } + + /** + * @param array $request + * @return string + * @throws \Friendica\Network\HTTPException\ServiceUnavailableException + */ + protected function content(array $request = []): string + { + parent::content(); + + $t = Renderer::getMarkupTemplate('admin/blocklist/server/import.tpl'); + return Renderer::replaceMacros($t, [ + '$l10n' => [ + 'return_list' => $this->l10n->t('← Return to the list'), + 'title' => $this->l10n->t('Administration'), + 'page' => $this->l10n->t('Import a Server Domain Pattern Blocklist'), + 'download' => $this->l10n->t('

This file can be downloaded from the /friendica path of any Friendica server.

'), + 'upload' => $this->l10n->t('Upload file'), + 'patterns' => $this->l10n->t('Patterns to import'), + 'domain_pattern' => $this->l10n->t('Domain Pattern'), + 'block_reason' => $this->l10n->t('Block Reason'), + 'mode' => $this->l10n->t('Import Mode'), + 'import' => $this->l10n->t('Import Patterns'), + 'pattern_count' => $this->l10n->tt('%d total pattern', '%d total patterns', count($this->blocklist)), + ], + '$listfile' => ['listfile', $this->l10n->t('Server domain pattern blocklist CSV file'), '', '', $this->l10n->t('Required'), '', 'file'], + '$mode_append' => ['mode', $this->l10n->t('Append'), 'append', $this->l10n->t('Imports patterns from the file that weren\'t already existing in the current blocklist.'), 'checked="checked"'], + '$mode_replace' => ['mode', $this->l10n->t('Replace'), 'replace', $this->l10n->t('Replaces the current blocklist by the imported patterns.')], + '$blocklist' => $this->blocklist, + '$baseurl' => $this->baseUrl->get(true), + '$form_security_token' => self::getFormSecurityToken('admin_blocklist_import') + ]); + } +} diff --git a/src/Module/Admin/Blocklist/Server/Index.php b/src/Module/Admin/Blocklist/Server/Index.php index ed2b9a2c59..ee6f1d4e88 100644 --- a/src/Module/Admin/Blocklist/Server/Index.php +++ b/src/Module/Admin/Blocklist/Server/Index.php @@ -84,8 +84,10 @@ class Index extends BaseAdmin
  • *: Any number of characters
  • ?: Any single character
  • '), + 'importtitle' => DI::l10n()->t('Import server domain pattern blocklist'), 'addtitle' => DI::l10n()->t('Add new entry to the blocklist'), - 'submit' => DI::l10n()->t('Check pattern'), + 'importsubmit' => DI::l10n()->t('Upload file'), + 'addsubmit' => DI::l10n()->t('Check pattern'), 'savechanges' => DI::l10n()->t('Save changes to the blocklist'), 'currenttitle' => DI::l10n()->t('Current Entries in the Blocklist'), 'thurl' => DI::l10n()->t('Blocked server domain pattern'), @@ -93,10 +95,12 @@ class Index extends BaseAdmin 'delentry' => DI::l10n()->t('Delete entry from the blocklist'), 'confirm_delete' => DI::l10n()->t('Delete entry from the blocklist?'), ], + '$listfile' => ['listfile', DI::l10n()->t('Server domain pattern blocklist CSV file'), '', '', DI::l10n()->t('Required'), '', 'file'], '$newdomain' => ['pattern', DI::l10n()->t('Server Domain 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'), '', ''], '$entries' => $blocklistform, '$baseurl' => DI::baseUrl()->get(true), - '$form_security_token' => self::getFormSecurityToken('admin_blocklist') + '$form_security_token' => self::getFormSecurityToken('admin_blocklist'), + '$form_security_token_import' => self::getFormSecurityToken('admin_blocklist_import'), ]); } } diff --git a/static/routes.config.php b/static/routes.config.php index 8c7a08758f..ce41c23d3a 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -310,9 +310,10 @@ return [ '/addons/{addon}' => [Module\Admin\Addons\Details::class, [R::GET, R::POST]], - '/blocklist/contact' => [Module\Admin\Blocklist\Contact::class, [R::GET, R::POST]], - '/blocklist/server' => [Module\Admin\Blocklist\Server\Index::class, [R::GET, R::POST]], - '/blocklist/server/add' => [Module\Admin\Blocklist\Server\Add::class, [R::GET, R::POST]], + '/blocklist/contact' => [Module\Admin\Blocklist\Contact::class, [R::GET, R::POST]], + '/blocklist/server' => [Module\Admin\Blocklist\Server\Index::class, [R::GET, R::POST]], + '/blocklist/server/add' => [Module\Admin\Blocklist\Server\Add::class, [R::GET, R::POST]], + '/blocklist/server/import' => [Module\Admin\Blocklist\Server\Import::class, [R::GET, R::POST]], '/dbsync[/{action}[/{update:\d+}]]' => [Module\Admin\DBSync::class, [R::GET]], diff --git a/view/templates/admin/blocklist/server/import.tpl b/view/templates/admin/blocklist/server/import.tpl new file mode 100644 index 0000000000..cdce214635 --- /dev/null +++ b/view/templates/admin/blocklist/server/import.tpl @@ -0,0 +1,52 @@ +
    +

    {{$l10n.return_list}}

    +

    {{$l10n.title}} - {{$l10n.page}}

    +{{if !$blocklist}} + {{$l10n.download nofilter}} + +
    + + {{include file="field_input.tpl" field=$listfile}} +
    + +
    +
    +{{else}} +

    {{$l10n.patterns}}

    +
    + + + + + + + + + + + + + + + + {{foreach $blocklist as $block}} + + + + + {{/foreach}} + +
    {{$l10n.domain_pattern}}{{$l10n.block_reason}}
    {{$l10n.pattern_count}}
    {{$block.domain}}{{$block.reason}}
    + +
    + + {{include file="field_radio.tpl" field=$mode_append}} + {{include file="field_radio.tpl" field=$mode_replace}} +
    + +
    + +
    +
    +{{/if}} +
    diff --git a/view/templates/admin/blocklist/server/index.tpl b/view/templates/admin/blocklist/server/index.tpl index 00df4da9cb..bfb269ebbe 100644 --- a/view/templates/admin/blocklist/server/index.tpl +++ b/view/templates/admin/blocklist/server/index.tpl @@ -7,13 +7,24 @@

    {{$l10n.title}} - {{$l10n.page}}

    {{$l10n.intro}}

    {{$l10n.public nofilter}}

    - {{$l10n.syntax nofilter}} + +

    {{$l10n.importtitle}}

    + {{$l10n.download nofilter}} + +
    + + {{include file="field_input.tpl" field=$listfile}} +
    + +
    +

    {{$l10n.addtitle}}

    + {{$l10n.syntax nofilter}}
    {{include file="field_input.tpl" field=$newdomain}}
    - +
    -- 2.39.5