From 41062eb7e4036946711afefd529de5c9a89b7b9d Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 16 Oct 2021 19:18:11 -0400 Subject: [PATCH] Add new purge contacts option to admin server blocklist - Move adding a server domain pattern to the blocklist in a separate module to allow reviewing the list of known servers that would be affected --- src/Model/GServer.php | 27 ++++ src/Module/Admin/Blocklist/Server.php | 116 ------------------ src/Module/Admin/Blocklist/Server/Add.php | 113 +++++++++++++++++ src/Module/Admin/Blocklist/Server/Index.php | 102 +++++++++++++++ static/routes.config.php | 5 +- view/templates/admin/blocklist/server/add.tpl | 56 +++++++++ .../{server.tpl => server/index.tpl} | 23 ++-- 7 files changed, 311 insertions(+), 131 deletions(-) delete mode 100644 src/Module/Admin/Blocklist/Server.php create mode 100644 src/Module/Admin/Blocklist/Server/Add.php create mode 100644 src/Module/Admin/Blocklist/Server/Index.php create mode 100644 view/templates/admin/blocklist/server/add.tpl rename view/templates/admin/blocklist/{server.tpl => server/index.tpl} (51%) diff --git a/src/Model/GServer.php b/src/Model/GServer.php index c56e7701e0..af03041498 100644 --- a/src/Model/GServer.php +++ b/src/Model/GServer.php @@ -117,6 +117,33 @@ class GServer return self::getID($url, true); } + /** + * Retrieves all the servers which base domain are matching the provided domain pattern + * + * The pattern is a simple fnmatch() pattern with ? for single wildcard and * for multiple wildcard + * + * @param string $pattern + * @return array + * @throws Exception + */ + public static function listByDomainPattern(string $pattern): array + { + $likePattern = 'http://' . strtr($pattern, ['_' => '\_', '%' => '\%', '?' => '_', '*' => '%']); + + // The SUBSTRING_INDEX returns everything before the eventual third /, which effectively trims an + // eventual server path and keep only the server domain which we're matching against the pattern. + $sql = "SELECT `gserver`.*, COUNT(*) AS `contacts` + FROM `gserver` + LEFT JOIN `contact` ON `gserver`.`id` = `contact`.`gsid` + WHERE SUBSTRING_INDEX(`gserver`.`nurl`, '/', 3) LIKE ? + AND NOT `gserver`.`failed` + GROUP BY `gserver`.`id`"; + + $stmt = DI::dba()->p($sql, $likePattern); + + return DI::dba()->toArray($stmt); + } + /** * Checks if the given server is reachable * diff --git a/src/Module/Admin/Blocklist/Server.php b/src/Module/Admin/Blocklist/Server.php deleted file mode 100644 index 0bd195317c..0000000000 --- a/src/Module/Admin/Blocklist/Server.php +++ /dev/null @@ -1,116 +0,0 @@ -. - * - */ - -namespace Friendica\Module\Admin\Blocklist; - -use Friendica\Core\Renderer; -use Friendica\DI; -use Friendica\Module\BaseAdmin; -use Friendica\Util\Strings; - -class Server extends BaseAdmin -{ - public static function post(array $parameters = []) - { - self::checkAdminAccess(); - - if (empty($_POST['page_blocklist_save']) && empty($_POST['page_blocklist_edit'])) { - return; - } - - self::checkFormSecurityTokenRedirectOnError('/admin/blocklist/server', 'admin_blocklist'); - - if (!empty($_POST['page_blocklist_save'])) { - // Add new item to blocklist - $domain = trim($_POST['newentry_domain']); - - $blocklist = DI::config()->get('system', 'blocklist'); - $blocklist[] = [ - 'domain' => $domain, - 'reason' => trim($_POST['newentry_reason']), - ]; - DI::config()->set('system', 'blocklist', $blocklist); - - info(DI::l10n()->t('Server domain pattern added to blocklist.')); - } else { - // Edit the entries from blocklist - $blocklist = []; - foreach ($_POST['domain'] as $id => $domain) { - // Trimming whitespaces as well as any lingering slashes - $domain = trim($domain); - $reason = trim($_POST['reason'][$id]); - if (empty($_POST['delete'][$id])) { - $blocklist[] = [ - 'domain' => $domain, - 'reason' => $reason - ]; - } - } - DI::config()->set('system', 'blocklist', $blocklist); - } - - DI::baseUrl()->redirect('admin/blocklist/server'); - } - - public static function content(array $parameters = []) - { - parent::content($parameters); - - $blocklist = DI::config()->get('system', 'blocklist'); - $blocklistform = []; - if (is_array($blocklist)) { - foreach ($blocklist as $id => $b) { - $blocklistform[] = [ - 'domain' => ["domain[$id]", DI::l10n()->t('Blocked server domain pattern'), $b['domain'], '', DI::l10n()->t('Required'), '', ''], - 'reason' => ["reason[$id]", DI::l10n()->t("Reason for the block"), $b['reason'], '', DI::l10n()->t('Required'), '', ''], - 'delete' => ["delete[$id]", DI::l10n()->t("Delete server domain pattern") . ' (' . $b['domain'] . ')', false, DI::l10n()->t("Check to delete this entry from the blocklist")] - ]; - } - } - - $t = Renderer::getMarkupTemplate('admin/blocklist/server.tpl'); - return Renderer::replaceMacros($t, [ - '$title' => DI::l10n()->t('Administration'), - '$page' => DI::l10n()->t('Server Domain Pattern Blocklist'), - '$intro' => DI::l10n()->t('This page can be used to define a blocklist of server domain patterns from the federated network that are not allowed to interact with your node. For each domain pattern you should also provide the reason why you block it.'), - '$public' => DI::l10n()->t('The list of blocked server domain patterns will be made publically available on the /friendica page so that your users and people investigating communication problems can find the reason easily.'), - '$syntax' => DI::l10n()->t('

The server domain pattern syntax is case-insensitive shell wildcard, comprising the following special characters:

-'), - '$addtitle' => DI::l10n()->t('Add new entry to block list'), - '$newdomain' => ['newentry_domain', DI::l10n()->t('Server Domain Pattern'), '', DI::l10n()->t('The domain pattern of the new server to add to the block list. Do not include the protocol.'), DI::l10n()->t('Required'), '', ''], - '$newreason' => ['newentry_reason', DI::l10n()->t('Block reason'), '', DI::l10n()->t('The reason why you blocked this server domain pattern. This reason will be shown publicly in the server information page.'), DI::l10n()->t('Required'), '', ''], - '$submit' => DI::l10n()->t('Add Entry'), - '$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'), - '$threason' => DI::l10n()->t('Reason for the block'), - '$delentry' => DI::l10n()->t('Delete entry from blocklist'), - '$entries' => $blocklistform, - '$baseurl' => DI::baseUrl()->get(true), - '$confirm_delete' => DI::l10n()->t('Delete entry from blocklist?'), - '$form_security_token' => self::getFormSecurityToken("admin_blocklist") - ]); - } -} diff --git a/src/Module/Admin/Blocklist/Server/Add.php b/src/Module/Admin/Blocklist/Server/Add.php new file mode 100644 index 0000000000..03f1026401 --- /dev/null +++ b/src/Module/Admin/Blocklist/Server/Add.php @@ -0,0 +1,113 @@ +. + * + */ + +namespace Friendica\Module\Admin\Blocklist\Server; + +use Friendica\Content\ContactSelector; +use Friendica\Core\Renderer; +use Friendica\Core\Worker; +use Friendica\DI; +use Friendica\Model\Contact; +use Friendica\Model\GServer; +use Friendica\Module\BaseAdmin; +use GuzzleHttp\Psr7\Uri; + +class Add extends BaseAdmin +{ + public static function post(array $parameters = []) + { + self::checkAdminAccess(); + + if (empty($_POST['page_blocklist_add'])) { + return; + } + + self::checkFormSecurityTokenRedirectOnError('/admin/blocklist/server/add', 'admin_blocklist_add'); + + // Add new item to blocklist + $domain = trim($_POST['pattern']); + + $blocklist = DI::config()->get('system', 'blocklist'); + $blocklist[] = [ + 'domain' => $domain, + 'reason' => trim($_POST['reason']), + ]; + DI::config()->set('system', 'blocklist', $blocklist); + + info(DI::l10n()->t('Server domain pattern added to the blocklist.')); + + if (!empty($_POST['purge'])) { + $gservers = GServer::listByDomainPattern($domain); + foreach (Contact::selectToArray(['id'], ['gsid' => array_column($gservers, 'id')]) as $contact) { + Worker::add(PRIORITY_LOW, 'Contact\RemoveContent', $contact['id']); + } + + info(DI::l10n()->tt('%s server scheduled to be purged.', '%s servers scheduled to be purged.', count($gservers))); + } + + DI::baseUrl()->redirect('admin/blocklist/server'); + } + + public static function content(array $parameters = []) + { + parent::content($parameters); + + $gservers = []; + + if ($pattern = trim($_REQUEST['pattern'] ?? '')) { + $gservers = GServer::listByDomainPattern($pattern); + } + + array_walk($gservers, function (array &$gserver) { + $gserver['domain'] = (new Uri($gserver['url']))->getHost(); + $gserver['network_icon'] = ContactSelector::networkToIcon($gserver['network']); + $gserver['network_name'] = ContactSelector::networkToName($gserver['network']); + }); + + $t = Renderer::getMarkupTemplate('admin/blocklist/server/add.tpl'); + return Renderer::replaceMacros($t, [ + '$l10n' => [ + 'return_list' => DI::l10n()->t('← Return to the list'), + 'title' => DI::l10n()->t('Administration'), + 'page' => DI::l10n()->t('Block A New Server Domain Pattern'), + 'syntax' => DI::l10n()->t('

The server domain pattern syntax is case-insensitive shell wildcard, comprising the following special characters:

+'), + 'submit' => DI::l10n()->t('Check pattern'), + 'matching_servers' => DI::l10n()->t('Matching known servers'), + 'server_name' => DI::l10n()->t('Server Name'), + 'server_domain' => DI::l10n()->t('Server Domain'), + 'known_contacts' => DI::l10n()->t('Known Contacts'), + 'server_count' => DI::l10n()->tt('%d known server', '%d known servers', count($gservers)), + 'add_pattern' => DI::l10n()->t('Add pattern to the blocklist'), + ], + '$newdomain' => ['pattern', DI::l10n()->t('Server Domain Pattern'), $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'), '', ''], + '$newpurge' => ['purge', DI::l10n()->t('Purge server'), $_REQUEST['purge'] ?? false, DI::l10n()->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))], + '$newreason' => ['reason', DI::l10n()->t('Block reason'), $_REQUEST['reason'] ?? '', DI::l10n()->t('The reason why you blocked this server domain pattern. This reason will be shown publicly in the server information page.'), DI::l10n()->t('Required'), '', ''], + '$pattern' => $pattern, + '$gservers' => $gservers, + '$baseurl' => DI::baseUrl()->get(true), + '$form_security_token' => self::getFormSecurityToken('admin_blocklist_add') + ]); + } +} diff --git a/src/Module/Admin/Blocklist/Server/Index.php b/src/Module/Admin/Blocklist/Server/Index.php new file mode 100644 index 0000000000..7dd59678a4 --- /dev/null +++ b/src/Module/Admin/Blocklist/Server/Index.php @@ -0,0 +1,102 @@ +. + * + */ + +namespace Friendica\Module\Admin\Blocklist\Server; + +use Friendica\Core\Renderer; +use Friendica\DI; +use Friendica\Module\BaseAdmin; + +class Index extends BaseAdmin +{ + public static function post(array $parameters = []) + { + self::checkAdminAccess(); + + if (empty($_POST['page_blocklist_edit'])) { + return; + } + + self::checkFormSecurityTokenRedirectOnError('/admin/blocklist/server', 'admin_blocklist'); + + // Edit the entries from blocklist + $blocklist = []; + foreach ($_POST['domain'] as $id => $domain) { + // Trimming whitespaces as well as any lingering slashes + $domain = trim($domain); + $reason = trim($_POST['reason'][$id]); + if (empty($_POST['delete'][$id])) { + $blocklist[] = [ + 'domain' => $domain, + 'reason' => $reason + ]; + } + } + + DI::config()->set('system', 'blocklist', $blocklist); + + DI::baseUrl()->redirect('admin/blocklist/server'); + } + + public static function content(array $parameters = []) + { + parent::content($parameters); + + $blocklist = DI::config()->get('system', 'blocklist'); + $blocklistform = []; + if (is_array($blocklist)) { + foreach ($blocklist as $id => $b) { + $blocklistform[] = [ + 'domain' => ["domain[$id]", DI::l10n()->t('Blocked server domain pattern'), $b['domain'], '', DI::l10n()->t('Required'), '', ''], + 'reason' => ["reason[$id]", DI::l10n()->t("Reason for the block"), $b['reason'], '', DI::l10n()->t('Required'), '', ''], + 'delete' => ["delete[$id]", DI::l10n()->t("Delete server domain pattern") . ' (' . $b['domain'] . ')', false, DI::l10n()->t("Check to delete this entry from the blocklist")] + ]; + } + } + + $t = Renderer::getMarkupTemplate('admin/blocklist/server/index.tpl'); + return Renderer::replaceMacros($t, [ + '$l10n' => [ + 'title' => DI::l10n()->t('Administration'), + 'page' => DI::l10n()->t('Server Domain Pattern Blocklist'), + 'intro' => DI::l10n()->t('This page can be used to define a blocklist of server domain patterns from the federated network that are not allowed to interact with your node. For each domain pattern you should also provide the reason why you block it.'), + 'public' => DI::l10n()->t('The list of blocked server domain patterns will be made publically available on the /friendica page so that your users and people investigating communication problems can find the reason easily.'), + 'syntax' => DI::l10n()->t('

The server domain pattern syntax is case-insensitive shell wildcard, comprising the following special characters:

+'), + 'addtitle' => DI::l10n()->t('Add new entry to the blocklist'), + 'submit' => 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'), + 'threason' => DI::l10n()->t('Reason for the block'), + 'delentry' => DI::l10n()->t('Delete entry from the blocklist'), + 'confirm_delete' => DI::l10n()->t('Delete entry from the blocklist?'), + ], + '$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') + ]); + } +} diff --git a/static/routes.config.php b/static/routes.config.php index 24fa016179..d1f11567a2 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -180,8 +180,9 @@ 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::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]], '/dbsync[/{action}[/{update:\d+}]]' => [Module\Admin\DBSync::class, [R::GET]], diff --git a/view/templates/admin/blocklist/server/add.tpl b/view/templates/admin/blocklist/server/add.tpl new file mode 100644 index 0000000000..999965d55c --- /dev/null +++ b/view/templates/admin/blocklist/server/add.tpl @@ -0,0 +1,56 @@ +
+

{{$l10n.return_list}}

+

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

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

{{$l10n.matching_servers}}

+
+ + + + + + + + + + + + + + + + + + {{foreach $gservers as $gserver}} + + + + + + + {{/foreach}} + +
{{$l10n.server_name}}{{$l10n.server_domain}}{{$l10n.known_contacts}}
{{$l10n.server_count}}
+ + + + {{$gserver.site_name|default:$gserver.domain}} + {{$gserver.domain}} + {{$gserver.contacts}}
+ + {{include file="field_checkbox.tpl" field=$newpurge}} + {{include file="field_input.tpl" field=$newreason}} +
+ +
+
+{{/if}} +
diff --git a/view/templates/admin/blocklist/server.tpl b/view/templates/admin/blocklist/server/index.tpl similarity index 51% rename from view/templates/admin/blocklist/server.tpl rename to view/templates/admin/blocklist/server/index.tpl index 43d006e090..00df4da9cb 100644 --- a/view/templates/admin/blocklist/server.tpl +++ b/view/templates/admin/blocklist/server/index.tpl @@ -1,27 +1,24 @@
-

{{$title}} - {{$page}}

-

{{$intro}}

-

{{$public nofilter}}

- {{$syntax nofilter}} +

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

+

{{$l10n.intro}}

+

{{$l10n.public nofilter}}

+ {{$l10n.syntax nofilter}} -

{{$addtitle}}

-
- +

{{$l10n.addtitle}}

+ {{include file="field_input.tpl" field=$newdomain}} - {{include file="field_input.tpl" field=$newreason}}
- +
{{if $entries}} -

{{$currenttitle}}

-

{{$currentintro}}

+

{{$l10n.currenttitle}}

{{foreach $entries as $e}} @@ -30,7 +27,7 @@ {{include file="field_checkbox.tpl" field=$e.delete}} {{/foreach}}
- +
{{/if}}