Move server domain pattern blocklist features to its own class

- Update tests
This commit is contained in:
Hypolite Petovan 2022-07-27 15:36:20 -04:00
parent e445975c20
commit 1810b32c26
7 changed files with 458 additions and 421 deletions

View File

@ -24,7 +24,7 @@ namespace Friendica\Console;
use Asika\SimpleConsole\CommandArgsException; use Asika\SimpleConsole\CommandArgsException;
use Asika\SimpleConsole\Console; use Asika\SimpleConsole\Console;
use Console_Table; use Console_Table;
use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Moderation\DomainPatternBlocklist;
/** /**
* Manage blocked servers * Manage blocked servers
@ -34,18 +34,14 @@ use Friendica\Core\Config\Capability\IManageConfigValues;
*/ */
class ServerBlock extends Console class ServerBlock extends Console
{ {
const DEFAULT_REASON = 'blocked';
protected $helpOptions = ['h', 'help', '?']; protected $helpOptions = ['h', 'help', '?'];
/** /** @var DomainPatternBlocklist */
* @var IManageConfigValues private $blocklist;
*/
private $config;
protected function getHelp() protected function getHelp(): string
{ {
$help = <<<HELP return <<<HELP
console serverblock - Manage blocked server domain patterns console serverblock - Manage blocked server domain patterns
Usage Usage
bin/console serverblock [-h|--help|-?] [-v] bin/console serverblock [-h|--help|-?] [-v]
@ -69,214 +65,141 @@ Options
-h|--help|-? Show help information -h|--help|-? Show help information
-v Show more debug information. -v Show more debug information.
HELP; HELP;
return $help;
} }
public function __construct(IManageConfigValues $config, $argv = null) public function __construct(DomainPatternBlocklist $blocklist, $argv = null)
{ {
parent::__construct($argv); parent::__construct($argv);
$this->config = $config; $this->blocklist = $blocklist;
} }
protected function doExecute(): int protected function doExecute(): int
{ {
if (count($this->args) == 0) { if (count($this->args) == 0) {
$this->printBlockedServers($this->config); $this->printBlockedServers();
return 0; return 0;
} }
switch ($this->getArgument(0)) { switch ($this->getArgument(0)) {
case 'add': case 'add':
return $this->addBlockedServer($this->config); return $this->addBlockedServer();
case 'remove': case 'remove':
return $this->removeBlockedServer($this->config); return $this->removeBlockedServer();
case 'export': case 'export':
return $this->exportBlockedServers($this->config); return $this->exportBlockedServers();
case 'import': case 'import':
return $this->importBlockedServers($this->config); return $this->importBlockedServers();
default: default:
throw new CommandArgsException('Unknown command.'); throw new CommandArgsException('Unknown command.');
break;
} }
} }
/** /**
* Exports the list of blocked domains including the reason for the * Exports the list of blocked domain patterns including the reason for the
* block to a CSV file. * block to a CSV file.
* *
* @param IManageConfigValues $config * @return int
* @throws \Exception
*/ */
private function exportBlockedServers(IManageConfigValues $config) private function exportBlockedServers(): int
{ {
$filename = $this->getArgument(1); $filename = $this->getArgument(1);
$blocklist = $config->get('system', 'blocklist', []);
$fp = fopen($filename, 'w'); $this->blocklist->exportToFile($filename);
if (!$fp) {
throw new Exception(sprintf('The file "%s" could not be created.', $filename));
}
foreach ($blocklist as $domain) {
fputcsv($fp, $domain);
}
// Success // Success
return 0; return 0;
} }
/** /**
* Imports a list of domains and a reason for the block from a CSV * Imports a list of domain patterns and a reason for the block from a CSV
* file, e.g. created with the export function. * file, e.g. created with the export function.
* *
* @param IManageConfigValues $config * @return int
* @throws \Exception
*/ */
private function importBlockedServers(IManageConfigValues $config) private function importBlockedServers(): int
{ {
$filename = $this->getArgument(1); $filename = $this->getArgument(1);
$currBlockList = $config->get('system', 'blocklist', []);
$newBlockList = [];
if (($fp = fopen($filename, 'r')) !== false) { $newBlockList = $this->blocklist::extractFromCSVFile($filename);
while (($data = fgetcsv($fp, 1000, ',')) !== false) {
$domain = $data[0];
if (count($data) == 0) {
$reason = self::DEFAULT_REASON;
} else {
$reason = $data[1];
}
$data = [
'domain' => $domain,
'reason' => $reason
];
if (!in_array($data, $newBlockList)) {
$newBlockList[] = $data;
}
}
foreach ($currBlockList as $blocked) { if ($this->blocklist->append($newBlockList)) {
if (!in_array($blocked, $newBlockList)) { $this->out(sprintf("Entries from %s that were not blocked before are now blocked", $filename));
$newBlockList[] = $blocked;
}
}
if ($config->set('system', 'blocklist', $newBlockList)) {
$this->out(sprintf("Entries from %s that were not blocked before are now blocked", $filename));
return 0;
} else {
$this->out(sprintf("Couldn't save '%s' as blocked server", $domain));
return 1;
}
} else {
throw new Exception(sprintf('The file "%s" could not be opened for importing', $filename));
}
}
/**
* Prints the whole list of blocked domains including the reason
*
* @param IManageConfigValues $config
*/
private function printBlockedServers(IManageConfigValues $config)
{
$table = new Console_Table();
$table->setHeaders(['Domain', 'Reason']);
$blocklist = $config->get('system', 'blocklist', []);
foreach ($blocklist as $domain) {
$table->addRow($domain);
}
$this->out($table->getTable());
// Success
return 0;
}
/**
* Adds a server to the blocked list
*
* @param IManageConfigValues $config
*
* @return int The return code (0 = success, 1 = failed)
*/
private function addBlockedServer(IManageConfigValues $config)
{
if (count($this->args) < 2 || count($this->args) > 3) {
throw new CommandArgsException('Add needs a domain and optional a reason.');
}
$domain = $this->getArgument(1);
$reason = (count($this->args) === 3) ? $this->getArgument(2) : self::DEFAULT_REASON;
$update = false;
$currBlockList = $config->get('system', 'blocklist', []);
$newBlockList = [];
foreach ($currBlockList as $blocked) {
if ($blocked['domain'] === $domain) {
$update = true;
$newBlockList[] = [
'domain' => $domain,
'reason' => $reason,
];
} else {
$newBlockList[] = $blocked;
}
}
if (!$update) {
$newBlockList[] = [
'domain' => $domain,
'reason' => $reason,
];
}
if ($config->set('system', 'blocklist', $newBlockList)) {
if ($update) {
$this->out(sprintf("The domain '%s' is now updated. (Reason: '%s')", $domain, $reason));
} else {
$this->out(sprintf("The domain '%s' is now blocked. (Reason: '%s')", $domain, $reason));
}
return 0; return 0;
} else { } else {
$this->out(sprintf("Couldn't save '%s' as blocked server", $domain)); $this->out("Couldn't save the block list");
return 1; return 1;
} }
} }
/** /**
* Removes a server from the blocked list * Prints the whole list of blocked domain patterns including the reason
* */
* @param IManageConfigValues $config private function printBlockedServers(): void
{
$table = new Console_Table();
$table->setHeaders(['Pattern', 'Reason']);
foreach ($this->blocklist->get() as $pattern) {
$table->addRow($pattern);
}
$this->out($table->getTable());
}
/**
* Adds a domain pattern to the block list
* *
* @return int The return code (0 = success, 1 = failed) * @return int The return code (0 = success, 1 = failed)
*/ */
private function removeBlockedServer(IManageConfigValues $config) private function addBlockedServer(): int
{
if (count($this->args) < 2 || count($this->args) > 3) {
throw new CommandArgsException('Add needs a domain pattern and optionally a reason.');
}
$pattern = $this->getArgument(1);
$reason = (count($this->args) === 3) ? $this->getArgument(2) : DomainPatternBlocklist::DEFAULT_REASON;
$result = $this->blocklist->addPattern($pattern, $reason);
if ($result) {
if ($result == 2) {
$this->out(sprintf("The domain pattern '%s' is now updated. (Reason: '%s')", $pattern, $reason));
} else {
$this->out(sprintf("The domain pattern '%s' is now blocked. (Reason: '%s')", $pattern, $reason));
}
return 0;
} else {
$this->out(sprintf("Couldn't save '%s' as blocked domain pattern", $pattern));
return 1;
}
}
/**
* Removes a domain pattern from the block list
*
* @return int The return code (0 = success, 1 = failed)
*/
private function removeBlockedServer(): int
{ {
if (count($this->args) !== 2) { if (count($this->args) !== 2) {
throw new CommandArgsException('Remove needs a second parameter.'); throw new CommandArgsException('Remove needs a second parameter.');
} }
$domain = $this->getArgument(1); $pattern = $this->getArgument(1);
$found = false; $result = $this->blocklist->removePattern($pattern);
if ($result) {
$currBlockList = $config->get('system', 'blocklist', []); if ($result == 2) {
$newBlockList = []; $this->out(sprintf("The domain pattern '%s' isn't blocked anymore", $pattern));
foreach ($currBlockList as $blocked) { return 0;
if ($blocked['domain'] === $domain) {
$found = true;
} else { } else {
$newBlockList[] = $blocked; $this->out(sprintf("The domain pattern '%s' wasn't blocked.", $pattern));
return 1;
} }
}
if (!$found) {
$this->out(sprintf("The domain '%s' is not blocked.", $domain));
return 1;
}
if ($config->set('system', 'blocklist', $newBlockList)) {
$this->out(sprintf("The domain '%s' is not more blocked", $domain));
return 0;
} else { } else {
$this->out(sprintf("Couldn't remove '%s' from blocked servers", $domain)); $this->out(sprintf("Couldn't remove '%s' from blocked domain patterns", $pattern));
return 1; return 1;
} }
} }

View File

@ -0,0 +1,180 @@
<?php
/**
* @copyright Copyright (C) 2010-2022, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Moderation;
use Exception;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\L10n;
use Friendica\Database\Database;
use Friendica\Network\HTTPException;
use Friendica\Util\Emailer;
class DomainPatternBlocklist
{
const DEFAULT_REASON = 'blocked';
/**
* @var IManageConfigValues
*/
private $config;
public function __construct(IManageConfigValues $config)
{
$this->config = $config;
}
public function get(): array
{
return $this->config->get('system', 'blocklist', []);
}
public function set(array $blocklist): bool
{
return $this->config->set('system', 'blocklist', $blocklist);
}
/**
* @param string $pattern
* @param string|null $reason
* @return int 0 if the block list couldn't be saved, 1 if the pattern was added, 2 if it was updated in place
*/
public function addPattern(string $pattern, string $reason = null): int
{
$update = false;
$blocklist = [];
foreach ($this->get() as $blocked) {
if ($blocked['domain'] === $pattern) {
$blocklist[] = [
'domain' => $pattern,
'reason' => $reason ?? self::DEFAULT_REASON,
];
$update = true;
} else {
$blocklist[] = $blocked;
}
}
if (!$update) {
$blocklist[] = [
'domain' => $pattern,
'reason' => $reason ?? self::DEFAULT_REASON,
];
}
return $this->set($blocklist) ? ($update ? 2 : 1) : 0;
}
/**
* @param string $pattern
* @return int 0 if the block list couldn't be saved, 1 if the pattern wasn't found, 2 if it was removed
*/
public function removePattern(string $pattern): int
{
$found = false;
$blocklist = [];
foreach ($this->get() as $blocked) {
if ($blocked['domain'] === $pattern) {
$found = true;
} else {
$blocklist[] = $blocked;
}
}
return $found ? ($this->set($blocklist) ? 2 : 0) : 1;
}
public function exportToFile(string $filename)
{
$fp = fopen($filename, 'w');
if (!$fp) {
throw new Exception(sprintf('The file "%s" could not be created.', $filename));
}
foreach ($this->get() as $domain) {
fputcsv($fp, $domain);
}
}
/**
* Appends to the local block list all the patterns from the provided list that weren't already present.
*
* @param array $blocklist
* @return int The number of patterns actually added to the block list
*/
public function append(array $blocklist): int
{
$localBlocklist = $this->get();
$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->set($localBlocklist);
}
return count($patternsToAppend);
}
/**
* Extracts a server domain pattern block list from the provided CSV file name. Deduplicates the list based on patterns.
*
* @param string $filename
* @return array
* @throws Exception
*/
public static function extractFromCSVFile(string $filename): array
{
$fp = fopen($filename, 'r');
if ($fp === false) {
throw new Exception(sprintf('The file "%s" could not be opened for importing', $filename));
}
$blocklist = [];
while (($data = fgetcsv($fp, 1000)) !== false) {
$domain = $data[0];
if (count($data) == 0) {
$reason = self::DEFAULT_REASON;
} else {
$reason = $data[1];
}
$data = [
'domain' => $domain,
'reason' => $reason
];
if (!in_array($data, $blocklist)) {
$blocklist[] = $data;
}
}
return $blocklist;
}
}

View File

@ -21,58 +21,90 @@
namespace Friendica\Module\Admin\Blocklist\Server; namespace Friendica\Module\Admin\Blocklist\Server;
use Friendica\App;
use Friendica\Content\ContactSelector; use Friendica\Content\ContactSelector;
use Friendica\Core\L10n;
use Friendica\Core\Renderer; use Friendica\Core\Renderer;
use Friendica\Core\Worker; use Friendica\Core\Worker;
use Friendica\DI;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Model\GServer; use Friendica\Model\GServer;
use Friendica\Moderation\DomainPatternBlocklist;
use Friendica\Module\BaseAdmin; use Friendica\Module\BaseAdmin;
use Friendica\Module\Response;
use Friendica\Navigation\SystemMessages;
use Friendica\Util\Profiler;
use GuzzleHttp\Psr7\Uri; use GuzzleHttp\Psr7\Uri;
use Psr\Log\LoggerInterface;
class Add extends BaseAdmin class Add extends BaseAdmin
{ {
/** @var SystemMessages */
private $sysmsg;
/** @var DomainPatternBlocklist */
private $blocklist;
public function __construct(SystemMessages $sysmsg, DomainPatternBlocklist $blocklist, 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->sysmsg = $sysmsg;
$this->blocklist = $blocklist;
}
/**
* @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
* @throws \Exception
*/
protected function post(array $request = []) protected function post(array $request = [])
{ {
self::checkAdminAccess(); self::checkAdminAccess();
if (empty($_POST['page_blocklist_add'])) { if (empty($request['page_blocklist_add'])) {
return; return;
} }
self::checkFormSecurityTokenRedirectOnError('/admin/blocklist/server/add', 'admin_blocklist_add'); self::checkFormSecurityTokenRedirectOnError('/admin/blocklist/server/add', 'admin_blocklist_add');
$pattern = trim($request['pattern']);
// Add new item to blocklist // Add new item to blocklist
$domain = trim($_POST['pattern']); $this->blocklist->addPattern($pattern, trim($request['reason']));
$blocklist = DI::config()->get('system', 'blocklist'); $this->sysmsg->addInfo($this->l10n->t('Server domain pattern added to the 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($request['purge'])) {
$gservers = GServer::listByDomainPattern($pattern);
if (!empty($_POST['purge'])) {
$gservers = GServer::listByDomainPattern($domain);
foreach (Contact::selectToArray(['id'], ['gsid' => array_column($gservers, 'id')]) as $contact) { foreach (Contact::selectToArray(['id'], ['gsid' => array_column($gservers, 'id')]) as $contact) {
Worker::add(PRIORITY_LOW, 'Contact\RemoveContent', $contact['id']); 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))); $this->sysmsg->addInfo($this->l10n->tt('%s server scheduled to be purged.', '%s servers scheduled to be purged.', count($gservers)));
} }
DI::baseUrl()->redirect('admin/blocklist/server'); $this->baseUrl->redirect('admin/blocklist/server');
} }
/**
* @param array $request
* @return string
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \Friendica\Network\HTTPException\ServiceUnavailableException
* @throws \Exception
*/
protected function content(array $request = []): string protected function content(array $request = []): string
{ {
parent::content(); parent::content();
$gservers = []; $gservers = [];
if ($pattern = trim($_REQUEST['pattern'] ?? '')) { if ($pattern = trim($request['pattern'] ?? '')) {
$gservers = GServer::listByDomainPattern($pattern); $gservers = GServer::listByDomainPattern($pattern);
} }
@ -85,28 +117,28 @@ class Add extends BaseAdmin
$t = Renderer::getMarkupTemplate('admin/blocklist/server/add.tpl'); $t = Renderer::getMarkupTemplate('admin/blocklist/server/add.tpl');
return Renderer::replaceMacros($t, [ return Renderer::replaceMacros($t, [
'$l10n' => [ '$l10n' => [
'return_list' => DI::l10n()->t('← Return to the list'), 'return_list' => $this->l10n->t('← Return to the list'),
'title' => DI::l10n()->t('Administration'), 'title' => $this->l10n->t('Administration'),
'page' => DI::l10n()->t('Block A New Server Domain Pattern'), 'page' => $this->l10n->t('Block A New Server Domain Pattern'),
'syntax' => DI::l10n()->t('<p>The server domain pattern syntax is case-insensitive shell wildcard, comprising the following special characters:</p> 'syntax' => $this->l10n->t('<p>The server domain pattern syntax is case-insensitive shell wildcard, comprising the following special characters:</p>
<ul> <ul>
<li><code>*</code>: Any number of characters</li> <li><code>*</code>: Any number of characters</li>
<li><code>?</code>: Any single character</li> <li><code>?</code>: Any single character</li>
</ul>'), </ul>'),
'submit' => DI::l10n()->t('Check pattern'), 'submit' => $this->l10n->t('Check pattern'),
'matching_servers' => DI::l10n()->t('Matching known servers'), 'matching_servers' => $this->l10n->t('Matching known servers'),
'server_name' => DI::l10n()->t('Server Name'), 'server_name' => $this->l10n->t('Server Name'),
'server_domain' => DI::l10n()->t('Server Domain'), 'server_domain' => $this->l10n->t('Server Domain'),
'known_contacts' => DI::l10n()->t('Known Contacts'), 'known_contacts' => $this->l10n->t('Known Contacts'),
'server_count' => DI::l10n()->tt('%d known server', '%d known servers', count($gservers)), 'server_count' => $this->l10n->tt('%d known server', '%d known servers', count($gservers)),
'add_pattern' => DI::l10n()->t('Add pattern to the blocklist'), 'add_pattern' => $this->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'), '', ''], '$newdomain' => ['pattern', $this->l10n->t('Server Domain Pattern'), $pattern, $this->l10n->t('The domain pattern of the new server to add to the blocklist. Do not include the protocol.'), $this->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))], '$newpurge' => ['purge', $this->l10n->t('Purge server'), $request['purge'] ?? false, $this->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'), '', ''], '$newreason' => ['reason', $this->l10n->t('Block reason'), $request['reason'] ?? '', $this->l10n->t('The reason why you blocked this server domain pattern. This reason will be shown publicly in the server information page.'), $this->l10n->t('Required'), '', ''],
'$pattern' => $pattern, '$pattern' => $pattern,
'$gservers' => $gservers, '$gservers' => $gservers,
'$baseurl' => DI::baseUrl()->get(true), '$baseurl' => $this->baseUrl->get(true),
'$form_security_token' => self::getFormSecurityToken('admin_blocklist_add') '$form_security_token' => self::getFormSecurityToken('admin_blocklist_add')
]); ]);
} }

View File

@ -22,9 +22,9 @@
namespace Friendica\Module\Admin\Blocklist\Server; namespace Friendica\Module\Admin\Blocklist\Server;
use Friendica\App; use Friendica\App;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Core\Renderer; use Friendica\Core\Renderer;
use Friendica\Moderation\DomainPatternBlocklist;
use Friendica\Module\Response; use Friendica\Module\Response;
use Friendica\Navigation\SystemMessages; use Friendica\Navigation\SystemMessages;
use Friendica\Util\Profiler; use Friendica\Util\Profiler;
@ -32,8 +32,8 @@ use Psr\Log\LoggerInterface;
class Import extends \Friendica\Module\BaseAdmin class Import extends \Friendica\Module\BaseAdmin
{ {
/** @var IManageConfigValues */ /** @var DomainPatternBlocklist */
private $config; private $localBlocklist;
/** @var SystemMessages */ /** @var SystemMessages */
private $sysmsg; private $sysmsg;
@ -41,11 +41,11 @@ class Import extends \Friendica\Module\BaseAdmin
/** @var array of blocked server domain patterns */ /** @var array of blocked server domain patterns */
private $blocklist = []; 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 = []) public function __construct(DomainPatternBlocklist $localBlocklist, 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); parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
$this->config = $config; $this->localBlocklist = $localBlocklist;
$this->sysmsg = $sysmsg; $this->sysmsg = $sysmsg;
} }
@ -62,63 +62,36 @@ class Import extends \Friendica\Module\BaseAdmin
{ {
self::checkAdminAccess(); self::checkAdminAccess();
if (!isset($_POST['page_blocklist_upload']) && !isset($_POST['page_blocklist_import'])) { if (!isset($request['page_blocklist_upload']) && !isset($request['page_blocklist_import'])) {
return; return;
} }
self::checkFormSecurityTokenRedirectOnError('/admin/blocklist/server/import', 'admin_blocklist_import'); self::checkFormSecurityTokenRedirectOnError('/admin/blocklist/server/import', 'admin_blocklist_import');
if (isset($_POST['page_blocklist_upload'])) { if (isset($request['page_blocklist_upload'])) {
if (($fp = fopen($_FILES['listfile']['tmp_name'], 'r')) !== false) { try {
$blocklist = []; $this->blocklist = $this->localBlocklist::extractFromCSVFile($_FILES['listfile']['tmp_name']);
while (($data = fgetcsv($fp, 1000, ',')) !== false) { } catch (\Throwable $e) {
$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')); $this->sysmsg->addNotice($this->l10n->t('Error importing pattern file'));
return;
} }
$this->blocklist = $blocklist;
return; return;
} }
if (isset($_POST['page_blocklist_import'])) { if (isset($request['page_blocklist_import'])) {
$blocklist = json_decode($_POST['blocklist'], true); $blocklist = json_decode($request['blocklist'], true);
if ($blocklist === null) { if ($blocklist === null) {
$this->sysmsg->addNotice($this->l10n->t('Error importing pattern file')); $this->sysmsg->addNotice($this->l10n->t('Error importing pattern file'));
return; return;
} }
if (($_POST['mode'] ?? 'append') == 'replace') { if (($request['mode'] ?? 'append') == 'replace') {
$this->config->set('system', 'blocklist', $blocklist); $this->localBlocklist->set($blocklist);
$this->sysmsg->addNotice($this->l10n->t('Local blocklist replaced with the provided file.')); $this->sysmsg->addNotice($this->l10n->t('Local blocklist replaced with the provided file.'));
} else { } else {
$localBlocklist = $this->config->get('system', 'blocklist', []); $count = $this->localBlocklist->append($blocklist);
$localPatterns = array_column($localBlocklist, 'domain'); if ($count) {
$this->sysmsg->addNotice($this->l10n->tt('%d pattern was added to the local blocklist.', '%d patterns were added to the local blocklist.', $count));
$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 { } else {
$this->sysmsg->addNotice($this->l10n->t('No pattern was added to the local blocklist.')); $this->sysmsg->addNotice($this->l10n->t('No pattern was added to the local blocklist.'));
} }

View File

@ -21,17 +21,33 @@
namespace Friendica\Module\Admin\Blocklist\Server; 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\Core\Renderer;
use Friendica\DI; use Friendica\Moderation\DomainPatternBlocklist;
use Friendica\Module\BaseAdmin; use Friendica\Module\BaseAdmin;
use Friendica\Module\Response;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;
class Index extends BaseAdmin class Index extends BaseAdmin
{ {
/** @var DomainPatternBlocklist */
private $blocklist;
public function __construct(DomainPatternBlocklist $blocklist, 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->blocklist = $blocklist;
}
protected function post(array $request = []) protected function post(array $request = [])
{ {
self::checkAdminAccess(); self::checkAdminAccess();
if (empty($_POST['page_blocklist_edit'])) { if (empty($request['page_blocklist_edit'])) {
return; return;
} }
@ -39,11 +55,11 @@ class Index extends BaseAdmin
// Edit the entries from blocklist // Edit the entries from blocklist
$blocklist = []; $blocklist = [];
foreach ($_POST['domain'] as $id => $domain) { foreach ($request['domain'] as $id => $domain) {
// Trimming whitespaces as well as any lingering slashes // Trimming whitespaces as well as any lingering slashes
$domain = trim($domain); $domain = trim($domain);
$reason = trim($_POST['reason'][$id]); $reason = trim($request['reason'][$id]);
if (empty($_POST['delete'][$id])) { if (empty($request['delete'][$id])) {
$blocklist[] = [ $blocklist[] = [
'domain' => $domain, 'domain' => $domain,
'reason' => $reason 'reason' => $reason
@ -51,54 +67,53 @@ class Index extends BaseAdmin
} }
} }
DI::config()->set('system', 'blocklist', $blocklist); $this->blocklist->set($blocklist);
DI::baseUrl()->redirect('admin/blocklist/server'); $
$this->baseUrl->redirect('admin/blocklist/server');
} }
protected function content(array $request = []): string protected function content(array $request = []): string
{ {
parent::content(); parent::content();
$blocklist = DI::config()->get('system', 'blocklist');
$blocklistform = []; $blocklistform = [];
if (is_array($blocklist)) { foreach ($this->blocklist->get() as $id => $b) {
foreach ($blocklist as $id => $b) { $blocklistform[] = [
$blocklistform[] = [ 'domain' => ["domain[$id]", $this->l10n->t('Blocked server domain pattern'), $b['domain'], '', $this->l10n->t('Required'), '', ''],
'domain' => ["domain[$id]", DI::l10n()->t('Blocked server domain pattern'), $b['domain'], '', DI::l10n()->t('Required'), '', ''], 'reason' => ["reason[$id]", $this->l10n->t("Reason for the block"), $b['reason'], '', $this->l10n->t('Required'), '', ''],
'reason' => ["reason[$id]", DI::l10n()->t("Reason for the block"), $b['reason'], '', DI::l10n()->t('Required'), '', ''], 'delete' => ["delete[$id]", $this->l10n->t("Delete server domain pattern") . ' (' . $b['domain'] . ')', false, $this->l10n->t("Check to delete this entry from the blocklist")]
'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'); $t = Renderer::getMarkupTemplate('admin/blocklist/server/index.tpl');
return Renderer::replaceMacros($t, [ return Renderer::replaceMacros($t, [
'$l10n' => [ '$l10n' => [
'title' => DI::l10n()->t('Administration'), 'title' => $this->l10n->t('Administration'),
'page' => DI::l10n()->t('Server Domain Pattern Blocklist'), 'page' => $this->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.'), 'intro' => $this->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 <a href="/friendica">/friendica</a> page so that your users and people investigating communication problems can find the reason easily.'), 'public' => $this->l10n->t('The list of blocked server domain patterns will be made publically available on the <a href="/friendica">/friendica</a> page so that your users and people investigating communication problems can find the reason easily.'),
'syntax' => DI::l10n()->t('<p>The server domain pattern syntax is case-insensitive shell wildcard, comprising the following special characters:</p> 'syntax' => $this->l10n->t('<p>The server domain pattern syntax is case-insensitive shell wildcard, comprising the following special characters:</p>
<ul> <ul>
<li><code>*</code>: Any number of characters</li> <li><code>*</code>: Any number of characters</li>
<li><code>?</code>: Any single character</li> <li><code>?</code>: Any single character</li>
</ul>'), </ul>'),
'importtitle' => DI::l10n()->t('Import server domain pattern blocklist'), 'importtitle' => $this->l10n->t('Import server domain pattern blocklist'),
'addtitle' => DI::l10n()->t('Add new entry to the blocklist'), 'addtitle' => $this->l10n->t('Add new entry to the blocklist'),
'importsubmit' => DI::l10n()->t('Upload file'), 'importsubmit' => $this->l10n->t('Upload file'),
'addsubmit' => DI::l10n()->t('Check pattern'), 'addsubmit' => $this->l10n->t('Check pattern'),
'savechanges' => DI::l10n()->t('Save changes to the blocklist'), 'savechanges' => $this->l10n->t('Save changes to the blocklist'),
'currenttitle' => DI::l10n()->t('Current Entries in the Blocklist'), 'currenttitle' => $this->l10n->t('Current Entries in the Blocklist'),
'thurl' => DI::l10n()->t('Blocked server domain pattern'), 'thurl' => $this->l10n->t('Blocked server domain pattern'),
'threason' => DI::l10n()->t('Reason for the block'), 'threason' => $this->l10n->t('Reason for the block'),
'delentry' => DI::l10n()->t('Delete entry from the blocklist'), 'delentry' => $this->l10n->t('Delete entry from the blocklist'),
'confirm_delete' => DI::l10n()->t('Delete entry from the blocklist?'), 'confirm_delete' => $this->l10n->t('Delete entry from the blocklist?'),
], ],
'$listfile' => ['listfile', DI::l10n()->t('Server domain pattern blocklist CSV file'), '', '', DI::l10n()->t('Required'), '', 'file'], '$listfile' => ['listfile', $this->l10n->t('Server domain pattern blocklist CSV file'), '', '', $this->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'), '', ''], '$newdomain' => ['pattern', $this->l10n->t('Server Domain Pattern'), '', $this->l10n->t('The domain pattern of the new server to add to the blocklist. Do not include the protocol.'), $this->l10n->t('Required'), '', ''],
'$entries' => $blocklistform, '$entries' => $blocklistform,
'$baseurl' => DI::baseUrl()->get(true), '$baseurl' => $this->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'), '$form_security_token_import' => self::getFormSecurityToken('admin_blocklist_import'),
]); ]);

View File

@ -22,37 +22,38 @@
namespace Friendica\Module\Blocklist\Domain; namespace Friendica\Module\Blocklist\Domain;
use Friendica\App; use Friendica\App;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Moderation\DomainPatternBlocklist;
use Friendica\Module\Response; use Friendica\Module\Response;
use Friendica\Util\Profiler; use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
class Download extends \Friendica\BaseModule class Download extends \Friendica\BaseModule
{ {
/** @var IManageConfigValues */ /** @var DomainPatternBlocklist */
private $config; private $blocklist;
public function __construct(IManageConfigValues $config, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = []) public function __construct(DomainPatternBlocklist $blocklist, 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); parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
$this->config = $config; $this->blocklist = $blocklist;
} }
/**
* @param array $request
* @return void
* @throws \Exception
*/
protected function rawContent(array $request = []) protected function rawContent(array $request = [])
{ {
$blocklist = $this->config->get('system', 'blocklist'); $hash = md5(json_encode($this->blocklist->get(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
$blocklistJson = json_encode($blocklist, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
$hash = md5($blocklistJson);
$etag = 'W/"' . $hash . '"'; $etag = 'W/"' . $hash . '"';
if (trim($_SERVER['HTTP_IF_NONE_MATCH'] ?? '') == $etag) { if (trim($_SERVER['HTTP_IF_NONE_MATCH'] ?? '') == $etag) {
header("HTTP/1.1 304 Not Modified"); header("HTTP/1.1 304 Not Modified");
System::exit();
} }
header('Content-Type: text/csv'); header('Content-Type: text/csv');
@ -60,11 +61,7 @@ class Download extends \Friendica\BaseModule
header('Content-disposition: attachment; filename="' . $this->baseUrl->getHostname() . '_domain_blocklist_' . substr($hash, 0, 6) . '.csv"'); header('Content-disposition: attachment; filename="' . $this->baseUrl->getHostname() . '_domain_blocklist_' . substr($hash, 0, 6) . '.csv"');
header("Etag: $etag"); header("Etag: $etag");
$fp = fopen('php://output', 'w'); $this->blocklist->exportToFile('php://output');
foreach ($blocklist as $domain) {
fputcsv($fp, $domain);
}
fclose($fp);
System::exit(); System::exit();
} }

View File

@ -23,6 +23,7 @@ namespace Friendica\Test\src\Console;
use Friendica\Console\ServerBlock; use Friendica\Console\ServerBlock;
use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Moderation\DomainPatternBlocklist;
use Mockery; use Mockery;
class ServerBlockConsoleTest extends ConsoleTest class ServerBlockConsoleTest extends ConsoleTest
@ -38,15 +39,15 @@ class ServerBlockConsoleTest extends ConsoleTest
] ]
]; ];
/** /**
* @var IManageConfigValues|Mockery\LegacyMockInterface|Mockery\MockInterface * @var DomainPatternBlocklist|Mockery\LegacyMockInterface|Mockery\MockInterface
*/ */
private $configMock; private $blocklistMock;
protected function setUp() : void protected function setUp() : void
{ {
parent::setUp(); parent::setUp();
$this->configMock = Mockery::mock(IManageConfigValues::class); $this->blocklistMock = Mockery::mock(DomainPatternBlocklist::class);
} }
/** /**
@ -54,23 +55,18 @@ class ServerBlockConsoleTest extends ConsoleTest
*/ */
public function testBlockedServersList() public function testBlockedServersList()
{ {
$this->configMock $this->blocklistMock
->shouldReceive('get') ->shouldReceive('get')
->with('system', 'blocklist', [])
->andReturn($this->defaultBlockList) ->andReturn($this->defaultBlockList)
->once(); ->once();
$console = new ServerBlock($this->configMock, $this->consoleArgv); $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
$txt = $this->dumpExecute($console); $txt = $this->dumpExecute($console);
$output = <<<CONS $php_eol = PHP_EOL;
+----------------------------+-----------------+
| Domain | Reason |
+----------------------------+-----------------+
| social.nobodyhasthe.biz | Illegal content |
| pod.ordoevangelistarum.com | Illegal content |
+----------------------------+-----------------+
$output = <<<CONS
+----------------------------+-----------------+$php_eol| Pattern | Reason |$php_eol+----------------------------+-----------------+$php_eol| social.nobodyhasthe.biz | Illegal content |$php_eol| pod.ordoevangelistarum.com | Illegal content |$php_eol+----------------------------+-----------------+$php_eol
CONS; CONS;
@ -82,31 +78,19 @@ CONS;
*/ */
public function testAddBlockedServer() public function testAddBlockedServer()
{ {
$this->configMock $this->blocklistMock
->shouldReceive('get') ->shouldReceive('addPattern')
->with('system', 'blocklist', []) ->with('testme.now', 'I like it!')
->andReturn($this->defaultBlockList) ->andReturn(1)
->once(); ->once();
$newBlockList = $this->defaultBlockList; $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
$newBlockList[] = [
'domain' => 'testme.now',
'reason' => 'I like it!',
];
$this->configMock
->shouldReceive('set')
->with('system', 'blocklist', $newBlockList)
->andReturn(true)
->once();
$console = new ServerBlock($this->configMock, $this->consoleArgv);
$console->setArgument(0, 'add'); $console->setArgument(0, 'add');
$console->setArgument(1, 'testme.now'); $console->setArgument(1, 'testme.now');
$console->setArgument(2, 'I like it!'); $console->setArgument(2, 'I like it!');
$txt = $this->dumpExecute($console); $txt = $this->dumpExecute($console);
self::assertEquals('The domain \'testme.now\' is now blocked. (Reason: \'I like it!\')' . PHP_EOL, $txt); self::assertEquals('The domain pattern \'testme.now\' is now blocked. (Reason: \'I like it!\')' . "\n", $txt);
} }
/** /**
@ -114,30 +98,18 @@ CONS;
*/ */
public function testAddBlockedServerWithDefaultReason() public function testAddBlockedServerWithDefaultReason()
{ {
$this->configMock $this->blocklistMock
->shouldReceive('get') ->shouldReceive('addPattern')
->with('system', 'blocklist', []) ->with('testme.now', DomainPatternBlocklist::DEFAULT_REASON)
->andReturn($this->defaultBlockList) ->andReturn(1)
->once(); ->once();
$newBlockList = $this->defaultBlockList; $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
$newBlockList[] = [
'domain' => 'testme.now',
'reason' => ServerBlock::DEFAULT_REASON,
];
$this->configMock
->shouldReceive('set')
->with('system', 'blocklist', $newBlockList)
->andReturn(true)
->once();
$console = new ServerBlock($this->configMock, $this->consoleArgv);
$console->setArgument(0, 'add'); $console->setArgument(0, 'add');
$console->setArgument(1, 'testme.now'); $console->setArgument(1, 'testme.now');
$txt = $this->dumpExecute($console); $txt = $this->dumpExecute($console);
self::assertEquals('The domain \'testme.now\' is now blocked. (Reason: \'' . ServerBlock::DEFAULT_REASON . '\')' . PHP_EOL, $txt); self::assertEquals('The domain pattern \'testme.now\' is now blocked. (Reason: \'' . DomainPatternBlocklist::DEFAULT_REASON . '\')' . "\n", $txt);
} }
/** /**
@ -145,36 +117,19 @@ CONS;
*/ */
public function testUpdateBlockedServer() public function testUpdateBlockedServer()
{ {
$this->configMock $this->blocklistMock
->shouldReceive('get') ->shouldReceive('addPattern')
->with('system', 'blocklist', []) ->with('pod.ordoevangelistarum.com', 'Other reason')
->andReturn($this->defaultBlockList) ->andReturn(2)
->once(); ->once();
$newBlockList = [ $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
[
'domain' => 'social.nobodyhasthe.biz',
'reason' => 'Illegal content',
],
[
'domain' => 'pod.ordoevangelistarum.com',
'reason' => 'Other reason',
]
];
$this->configMock
->shouldReceive('set')
->with('system', 'blocklist', $newBlockList)
->andReturn(true)
->once();
$console = new ServerBlock($this->configMock, $this->consoleArgv);
$console->setArgument(0, 'add'); $console->setArgument(0, 'add');
$console->setArgument(1, 'pod.ordoevangelistarum.com'); $console->setArgument(1, 'pod.ordoevangelistarum.com');
$console->setArgument(2, 'Other reason'); $console->setArgument(2, 'Other reason');
$txt = $this->dumpExecute($console); $txt = $this->dumpExecute($console);
self::assertEquals('The domain \'pod.ordoevangelistarum.com\' is now updated. (Reason: \'Other reason\')' . PHP_EOL, $txt); self::assertEquals('The domain pattern \'pod.ordoevangelistarum.com\' is now updated. (Reason: \'Other reason\')' . "\n", $txt);
} }
/** /**
@ -182,31 +137,18 @@ CONS;
*/ */
public function testRemoveBlockedServer() public function testRemoveBlockedServer()
{ {
$this->configMock $this->blocklistMock
->shouldReceive('get') ->shouldReceive('removePattern')
->with('system', 'blocklist', []) ->with('pod.ordoevangelistarum.com')
->andReturn($this->defaultBlockList) ->andReturn(2)
->once(); ->once();
$newBlockList = [ $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
[
'domain' => 'social.nobodyhasthe.biz',
'reason' => 'Illegal content',
],
];
$this->configMock
->shouldReceive('set')
->with('system', 'blocklist', $newBlockList)
->andReturn(true)
->once();
$console = new ServerBlock($this->configMock, $this->consoleArgv);
$console->setArgument(0, 'remove'); $console->setArgument(0, 'remove');
$console->setArgument(1, 'pod.ordoevangelistarum.com'); $console->setArgument(1, 'pod.ordoevangelistarum.com');
$txt = $this->dumpExecute($console); $txt = $this->dumpExecute($console);
self::assertEquals('The domain \'pod.ordoevangelistarum.com\' is not more blocked' . PHP_EOL, $txt); self::assertEquals('The domain pattern \'pod.ordoevangelistarum.com\' isn\'t blocked anymore' . "\n", $txt);
} }
/** /**
@ -214,7 +156,7 @@ CONS;
*/ */
public function testBlockedServersWrongCommand() public function testBlockedServersWrongCommand()
{ {
$console = new ServerBlock($this->configMock, $this->consoleArgv); $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
$console->setArgument(0, 'wrongcommand'); $console->setArgument(0, 'wrongcommand');
$txt = $this->dumpExecute($console); $txt = $this->dumpExecute($console);
@ -226,18 +168,18 @@ CONS;
*/ */
public function testRemoveBlockedServerNotExist() public function testRemoveBlockedServerNotExist()
{ {
$this->configMock $this->blocklistMock
->shouldReceive('get') ->shouldReceive('removePattern')
->with('system', 'blocklist', []) ->with('not.exiting')
->andReturn($this->defaultBlockList) ->andReturn(1)
->once(); ->once();
$console = new ServerBlock($this->configMock, $this->consoleArgv); $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
$console->setArgument(0, 'remove'); $console->setArgument(0, 'remove');
$console->setArgument(1, 'not.exiting'); $console->setArgument(1, 'not.exiting');
$txt = $this->dumpExecute($console); $txt = $this->dumpExecute($console);
self::assertEquals('The domain \'not.exiting\' is not blocked.' . PHP_EOL, $txt); self::assertEquals('The domain pattern \'not.exiting\' wasn\'t blocked.' . "\n", $txt);
} }
/** /**
@ -245,11 +187,11 @@ CONS;
*/ */
public function testAddBlockedServerMissingArgument() public function testAddBlockedServerMissingArgument()
{ {
$console = new ServerBlock($this->configMock, $this->consoleArgv); $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
$console->setArgument(0, 'add'); $console->setArgument(0, 'add');
$txt = $this->dumpExecute($console); $txt = $this->dumpExecute($console);
self::assertStringStartsWith('[Warning] Add needs a domain and optional a reason.', $txt); self::assertStringStartsWith('[Warning] Add needs a domain pattern and optionally a reason.', $txt);
} }
/** /**
@ -257,30 +199,18 @@ CONS;
*/ */
public function testAddBlockedServerNoSave() public function testAddBlockedServerNoSave()
{ {
$this->configMock $this->blocklistMock
->shouldReceive('get') ->shouldReceive('addPattern')
->with('system', 'blocklist', []) ->with('testme.now', DomainPatternBlocklist::DEFAULT_REASON)
->andReturn($this->defaultBlockList) ->andReturn(0)
->once(); ->once();
$newBlockList = $this->defaultBlockList; $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
$newBlockList[] = [
'domain' => 'testme.now',
'reason' => ServerBlock::DEFAULT_REASON,
];
$this->configMock
->shouldReceive('set')
->with('system', 'blocklist', $newBlockList)
->andReturn(false)
->once();
$console = new ServerBlock($this->configMock, $this->consoleArgv);
$console->setArgument(0, 'add'); $console->setArgument(0, 'add');
$console->setArgument(1, 'testme.now'); $console->setArgument(1, 'testme.now');
$txt = $this->dumpExecute($console); $txt = $this->dumpExecute($console);
self::assertEquals('Couldn\'t save \'testme.now\' as blocked server' . PHP_EOL, $txt); self::assertEquals('Couldn\'t save \'testme.now\' as blocked domain pattern' . "\n", $txt);
} }
/** /**
@ -288,31 +218,18 @@ CONS;
*/ */
public function testRemoveBlockedServerNoSave() public function testRemoveBlockedServerNoSave()
{ {
$this->configMock $this->blocklistMock
->shouldReceive('get') ->shouldReceive('removePattern')
->with('system', 'blocklist', []) ->with('pod.ordoevangelistarum.com')
->andReturn($this->defaultBlockList) ->andReturn(0)
->once(); ->once();
$newBlockList = [ $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
[
'domain' => 'social.nobodyhasthe.biz',
'reason' => 'Illegal content',
],
];
$this->configMock
->shouldReceive('set')
->with('system', 'blocklist', $newBlockList)
->andReturn(false)
->once();
$console = new ServerBlock($this->configMock, $this->consoleArgv);
$console->setArgument(0, 'remove'); $console->setArgument(0, 'remove');
$console->setArgument(1, 'pod.ordoevangelistarum.com'); $console->setArgument(1, 'pod.ordoevangelistarum.com');
$txt = $this->dumpExecute($console); $txt = $this->dumpExecute($console);
self::assertEquals('Couldn\'t remove \'pod.ordoevangelistarum.com\' from blocked servers' . PHP_EOL, $txt); self::assertEquals('Couldn\'t remove \'pod.ordoevangelistarum.com\' from blocked domain patterns' . "\n", $txt);
} }
/** /**
@ -320,7 +237,7 @@ CONS;
*/ */
public function testRemoveBlockedServerMissingArgument() public function testRemoveBlockedServerMissingArgument()
{ {
$console = new ServerBlock($this->configMock, $this->consoleArgv); $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
$console->setArgument(0, 'remove'); $console->setArgument(0, 'remove');
$txt = $this->dumpExecute($console); $txt = $this->dumpExecute($console);
@ -332,7 +249,7 @@ CONS;
*/ */
public function testBlockedServersHelp() public function testBlockedServersHelp()
{ {
$console = new ServerBlock($this->configMock, $this->consoleArgv); $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
$console->setOption('help', true); $console->setOption('help', true);
$txt = $this->dumpExecute($console); $txt = $this->dumpExecute($console);