3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Console;
24 use Asika\SimpleConsole\CommandArgsException;
25 use Asika\SimpleConsole\Console;
27 use Friendica\Core\Config\IConfig;
30 * Manage blocked servers
32 * With this tool, you can list the current blocked servers
33 * or you can add / remove a blocked server from the list
35 class ServerBlock extends Console
37 const DEFAULT_REASON = 'blocked';
39 protected $helpOptions = ['h', 'help', '?'];
46 protected function getHelp()
49 console serverblock - Manage blocked server domain patterns
51 bin/console serverblock [-h|--help|-?] [-v]
52 bin/console serverblock add <pattern> <reason> [-h|--help|-?] [-v]
53 bin/console serverblock remove <pattern> [-h|--help|-?] [-v]
56 With this tool, you can list the current blocked server domain patterns
57 or you can add / remove a blocked server domain pattern from the list.
59 Patterns are case-insensitive shell wildcard comprising the following special characters:
60 - * : Any number of characters
61 - ? : Any single character
62 - [<char1><char2>...] : char1 or char2 or...
65 -h|--help|-? Show help information
66 -v Show more debug information.
71 public function __construct(IConfig $config, $argv = null)
73 parent::__construct($argv);
75 $this->config = $config;
78 protected function doExecute()
80 if (count($this->args) == 0) {
81 $this->printBlockedServers($this->config);
85 switch ($this->getArgument(0)) {
87 return $this->addBlockedServer($this->config);
89 return $this->removeBlockedServer($this->config);
91 throw new CommandArgsException('Unknown command.');
97 * Prints the whole list of blocked domains including the reason
99 * @param IConfig $config
101 private function printBlockedServers(IConfig $config)
103 $table = new Console_Table();
104 $table->setHeaders(['Domain', 'Reason']);
105 $blocklist = $config->get('system', 'blocklist', []);
106 foreach ($blocklist as $domain) {
107 $table->addRow($domain);
109 $this->out($table->getTable());
113 * Adds a server to the blocked list
115 * @param IConfig $config
117 * @return int The return code (0 = success, 1 = failed)
119 private function addBlockedServer(IConfig $config)
121 if (count($this->args) < 2 || count($this->args) > 3) {
122 throw new CommandArgsException('Add needs a domain and optional a reason.');
125 $domain = $this->getArgument(1);
126 $reason = (count($this->args) === 3) ? $this->getArgument(2) : self::DEFAULT_REASON;
130 $currBlocklist = $config->get('system', 'blocklist', []);
132 foreach ($currBlocklist as $blocked) {
133 if ($blocked['domain'] === $domain) {
140 $newBlockList[] = $blocked;
151 if ($config->set('system', 'blocklist', $newBlockList)) {
153 $this->out(sprintf("The domain '%s' is now updated. (Reason: '%s')", $domain, $reason));
155 $this->out(sprintf("The domain '%s' is now blocked. (Reason: '%s')", $domain, $reason));
159 $this->out(sprintf("Couldn't save '%s' as blocked server", $domain));
165 * Removes a server from the blocked list
167 * @param IConfig $config
169 * @return int The return code (0 = success, 1 = failed)
171 private function removeBlockedServer(IConfig $config)
173 if (count($this->args) !== 2) {
174 throw new CommandArgsException('Remove needs a second parameter.');
177 $domain = $this->getArgument(1);
181 $currBlocklist = $config->get('system', 'blocklist', []);
183 foreach ($currBlocklist as $blocked) {
184 if ($blocked['domain'] === $domain) {
187 $newBlockList[] = $blocked;
192 $this->out(sprintf("The domain '%s' is not blocked.", $domain));
196 if ($config->set('system', 'blocklist', $newBlockList)) {
197 $this->out(sprintf("The domain '%s' is not more blocked", $domain));
200 $this->out(sprintf("Couldn't remove '%s' from blocked servers", $domain));