From 7d7a5a65e9bd95c85fe00f64af46a5b444d62778 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Wed, 27 Jul 2022 11:54:50 -0400 Subject: [PATCH] Add download feature for domain block list - Add new /blocklist/domain/download route - Add link on /friendica page --- src/Module/Blocklist/Domain/Download.php | 71 ++++++++++++++++++++++++ src/Module/Friendica.php | 7 ++- static/routes.config.php | 3 + view/templates/friendica.tpl | 24 +++----- 4 files changed, 87 insertions(+), 18 deletions(-) create mode 100644 src/Module/Blocklist/Domain/Download.php diff --git a/src/Module/Blocklist/Domain/Download.php b/src/Module/Blocklist/Domain/Download.php new file mode 100644 index 000000000..db7437dc7 --- /dev/null +++ b/src/Module/Blocklist/Domain/Download.php @@ -0,0 +1,71 @@ +. + * + */ + +namespace Friendica\Module\Blocklist\Domain; + +use Friendica\App; +use Friendica\Core\Config\Capability\IManageConfigValues; +use Friendica\Core\L10n; +use Friendica\Core\System; +use Friendica\Module\Response; +use Friendica\Util\Profiler; +use Psr\Log\LoggerInterface; + +class Download extends \Friendica\BaseModule +{ + /** @var IManageConfigValues */ + private $config; + + public function __construct(IManageConfigValues $config, 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; + } + + protected function rawContent(array $request = []) + { + $blocklist = $this->config->get('system', 'blocklist'); + + $blocklistJson = json_encode($blocklist, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); + + $hash = md5($blocklistJson); + + $etag = 'W/"' . $hash . '"'; + + if (trim($_SERVER['HTTP_IF_NONE_MATCH'] ?? '') == $etag) { + header("HTTP/1.1 304 Not Modified"); + } + + header('Content-Type: text/csv'); + header('Content-Transfer-Encoding: Binary'); + header('Content-disposition: attachment; filename="' . $this->baseUrl->getHostname() . '_domain_blocklist_' . substr($hash, 0, 6) . '.csv"'); + header("Etag: $etag"); + + $fp = fopen('php://output', 'w'); + foreach ($blocklist as $domain) { + fputcsv($fp, $domain); + } + fclose($fp); + + System::exit(); + } +} diff --git a/src/Module/Friendica.php b/src/Module/Friendica.php index 6b5e05c8b..012f65ef6 100644 --- a/src/Module/Friendica.php +++ b/src/Module/Friendica.php @@ -76,12 +76,13 @@ class Friendica extends BaseModule if (!empty($blockList)) { $blocked = [ - 'title' => DI::l10n()->t('On this server the following remote servers are blocked.'), - 'header' => [ + 'title' => DI::l10n()->t('On this server the following remote servers are blocked.'), + 'header' => [ DI::l10n()->t('Blocked domain'), DI::l10n()->t('Reason for the block'), ], - 'list' => $blockList, + 'download' => DI::l10n()->t('Download this list in CSV format'), + 'list' => $blockList, ]; } else { $blocked = null; diff --git a/static/routes.config.php b/static/routes.config.php index dac56a125..8c7a08758 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -353,6 +353,9 @@ return [ '/attach/{item:\d+}' => [Module\Attach::class, [R::GET]], '/babel' => [Module\Debug\Babel::class, [R::GET, R::POST]], '/debug/ap' => [Module\Debug\ActivityPubConversion::class, [R::GET, R::POST]], + + '/blocklist/domain/download' => [Module\Blocklist\Domain\Download::class, [R::GET]], + '/bookmarklet' => [Module\Bookmarklet::class, [R::GET]], '/community[/{content}]' => [Module\Conversation\Community::class, [R::GET]], diff --git a/view/templates/friendica.tpl b/view/templates/friendica.tpl index 25f1d2b64..a5c765f96 100644 --- a/view/templates/friendica.tpl +++ b/view/templates/friendica.tpl @@ -1,28 +1,22 @@ -
+

Friendica

-

{{$about nofilter}}

-

{{$friendica nofilter}}

-

{{$bugs nofilter}}

-

{{$info nofilter}}

-

{{$visible_addons.title nofilter}}

- {{if $visible_addons.list}} +{{if $visible_addons.list}}
{{$visible_addons.list}}
- {{/if}} +{{/if}} - {{if $tos}} +{{if $tos}}

{{$tos nofilter}}

- {{/if}} +{{/if}} - {{if $block_list}} +{{if $block_list}}

{{$block_list.title}}

-
@@ -39,9 +33,9 @@ {{/foreach}}
+

{{$block_list.download}}

+{{/if}} - {{/if}} - -{{$hooked nofilter}} + {{$hooked nofilter}}