3 namespace Friendica\Console;
5 use Asika\SimpleConsole\CommandArgsException;
6 use Asika\SimpleConsole\Console;
8 use Friendica\Core\Config\IConfiguration;
11 * @brief Manage blocked servers
13 * With this tool, you can list the current blocked servers
14 * or you can add / remove a blocked server from the list
16 class ServerBlock extends Console
18 const DEFAULT_REASON = 'blocked';
20 protected $helpOptions = ['h', 'help', '?'];
27 protected function getHelp()
30 console serverblock - Manage blocked server domain patterns
32 bin/console serverblock [-h|--help|-?] [-v]
33 bin/console serverblock add <pattern> <reason> [-h|--help|-?] [-v]
34 bin/console serverblock remove <pattern> [-h|--help|-?] [-v]
37 With this tool, you can list the current blocked server domain patterns
38 or you can add / remove a blocked server domain pattern from the list.
40 Patterns are case-insensitive shell wildcard comprising the following special characters:
41 - * : Any number of characters
42 - ? : Any single character
43 - [<char1><char2>...] : char1 or char2 or...
46 -h|--help|-? Show help information
47 -v Show more debug information.
52 public function __construct(IConfiguration $config, $argv = null)
54 parent::__construct($argv);
56 $this->config = $config;
59 protected function doExecute()
61 if (count($this->args) == 0) {
62 $this->printBlockedServers($this->config);
66 switch ($this->getArgument(0)) {
68 return $this->addBlockedServer($this->config);
70 return $this->removeBlockedServer($this->config);
72 throw new CommandArgsException('Unknown command.');
78 * Prints the whole list of blocked domains including the reason
80 * @param IConfiguration $config
82 private function printBlockedServers(IConfiguration $config)
84 $table = new Console_Table();
85 $table->setHeaders(['Domain', 'Reason']);
86 $blocklist = $config->get('system', 'blocklist', []);
87 foreach ($blocklist as $domain) {
88 $table->addRow($domain);
90 $this->out($table->getTable());
94 * Adds a server to the blocked list
96 * @param IConfiguration $config
98 * @return int The return code (0 = success, 1 = failed)
100 private function addBlockedServer(IConfiguration $config)
102 if (count($this->args) < 2 || count($this->args) > 3) {
103 throw new CommandArgsException('Add needs a domain and optional a reason.');
106 $domain = $this->getArgument(1);
107 $reason = (count($this->args) === 3) ? $this->getArgument(2) : self::DEFAULT_REASON;
111 $currBlocklist = $config->get('system', 'blocklist', []);
113 foreach ($currBlocklist as $blocked) {
114 if ($blocked['domain'] === $domain) {
121 $newBlockList[] = $blocked;
132 if ($config->set('system', 'blocklist', $newBlockList)) {
134 $this->out(sprintf("The domain '%s' is now updated. (Reason: '%s')", $domain, $reason));
136 $this->out(sprintf("The domain '%s' is now blocked. (Reason: '%s')", $domain, $reason));
140 $this->out(sprintf("Couldn't save '%s' as blocked server", $domain));
146 * Removes a server from the blocked list
148 * @param IConfiguration $config
150 * @return int The return code (0 = success, 1 = failed)
152 private function removeBlockedServer(IConfiguration $config)
154 if (count($this->args) !== 2) {
155 throw new CommandArgsException('Remove needs a second parameter.');
158 $domain = $this->getArgument(1);
162 $currBlocklist = $config->get('system', 'blocklist', []);
164 foreach ($currBlocklist as $blocked) {
165 if ($blocked['domain'] === $domain) {
168 $newBlockList[] = $blocked;
173 $this->out(sprintf("The domain '%s' is not blocked.", $domain));
177 if ($config->set('system', 'blocklist', $newBlockList)) {
178 $this->out(sprintf("The domain '%s' is not more blocked", $domain));
181 $this->out(sprintf("Couldn't remove '%s' from blocked servers", $domain));