3 namespace Friendica\Console;
5 use Asika\SimpleConsole\CommandArgsException;
6 use Asika\SimpleConsole\Console;
8 use Friendica\BaseObject;
9 use Friendica\Core\Config\Configuration;
12 * @brief Manage blocked servers
14 * With this tool, you can list the current blocked servers
15 * or you can add / remove a blocked server from the list
17 class ServerBlock extends Console
19 const DEFAULT_REASON = 'blocked';
21 protected $helpOptions = ['h', 'help', '?'];
23 protected function getHelp()
26 console serverblock - Manage blocked server domain patterns
28 bin/console serverblock [-h|--help|-?] [-v]
29 bin/console serverblock add <pattern> <reason> [-h|--help|-?] [-v]
30 bin/console serverblock remove <pattern> [-h|--help|-?] [-v]
33 With this tool, you can list the current blocked server domain patterns
34 or you can add / remove a blocked server domain pattern from the list.
36 Patterns are case-insensitive shell wildcard comprising the following special characters:
37 - * : Any number of characters
38 - ? : Any single character
39 - [<char1><char2>...] : char1 or char2 or...
42 -h|--help|-? Show help information
43 -v Show more debug information.
48 protected function doExecute()
50 $a = BaseObject::getApp();
52 if (count($this->args) == 0) {
53 $this->printBlockedServers($a->getConfig());
57 switch ($this->getArgument(0)) {
59 return $this->addBlockedServer($a->getConfig());
61 return $this->removeBlockedServer($a->getConfig());
63 throw new CommandArgsException('Unknown command.');
69 * Prints the whole list of blocked domains including the reason
71 * @param Configuration $config
73 private function printBlockedServers(Configuration $config)
75 $table = new Console_Table();
76 $table->setHeaders(['Domain', 'Reason']);
77 $blocklist = $config->get('system', 'blocklist');
78 foreach ($blocklist as $domain) {
79 $table->addRow($domain);
81 $this->out($table->getTable());
85 * Adds a server to the blocked list
87 * @param Configuration $config
89 * @return int The return code (0 = success, 1 = failed)
91 private function addBlockedServer(Configuration $config)
93 if (count($this->args) < 2 || count($this->args) > 3) {
94 throw new CommandArgsException('Add needs a domain and optional a reason.');
97 $domain = $this->getArgument(1);
98 $reason = (count($this->args) === 3) ? $this->getArgument(2) : self::DEFAULT_REASON;
102 $currBlocklist = $config->get('system', 'blocklist');
104 foreach ($currBlocklist as $blocked) {
105 if ($blocked['domain'] === $domain) {
112 $newBlockList[] = $blocked;
123 if ($config->set('system', 'blocklist', $newBlockList)) {
125 $this->out(sprintf("The domain '%s' is now updated. (Reason: '%s')", $domain, $reason));
127 $this->out(sprintf("The domain '%s' is now blocked. (Reason: '%s')", $domain, $reason));
131 $this->out(sprintf("Couldn't save '%s' as blocked server", $domain));
137 * Removes a server from the blocked list
139 * @param Configuration $config
141 * @return int The return code (0 = success, 1 = failed)
143 private function removeBlockedServer(Configuration $config)
145 if (count($this->args) !== 2) {
146 throw new CommandArgsException('Remove needs a second parameter.');
149 $domain = $this->getArgument(1);
153 $currBlocklist = $config->get('system', 'blocklist');
155 foreach ($currBlocklist as $blocked) {
156 if ($blocked['domain'] === $domain) {
159 $newBlockList[] = $blocked;
164 $this->out(sprintf("The domain '%s' is not blocked.", $domain));
168 if ($config->set('system', 'blocklist', $newBlockList)) {
169 $this->out(sprintf("The domain '%s' is not more blocked", $domain));
172 $this->out(sprintf("Couldn't remove '%s' from blocked servers", $domain));