3 namespace Friendica\Core\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 servers
28 bin/console serverblock [-h|--help|-?] [-v]
29 bin/console serverblock add <server> <reason> [-h|--help|-?] [-v]
30 bin/console serverblock remove <server> [-h|--help|-?] [-v]
33 With this tool, you can list the current blocked servers
34 or you can add / remove a blocked server from the list
37 -h|--help|-? Show help information
38 -v Show more debug information.
43 protected function doExecute()
45 $a = BaseObject::getApp();
47 if (count($this->args) == 0) {
48 $this->printBlockedServers($a->getConfig());
52 switch ($this->getArgument(0)) {
54 return $this->addBlockedServer($a->getConfig());
56 return $this->removeBlockedServer($a->getConfig());
58 throw new CommandArgsException('Unknown command.');
64 * Prints the whole list of blocked domains including the reason
66 * @param Configuration $config
68 private function printBlockedServers(Configuration $config)
70 $table = new Console_Table();
71 $table->setHeaders(['Domain', 'Reason']);
72 $blocklist = $config->get('system', 'blocklist');
73 foreach ($blocklist as $domain) {
74 $table->addRow($domain);
76 $this->out($table->getTable());
80 * Adds a server to the blocked list
82 * @param Configuration $config
84 * @return int The return code (0 = success, 1 = failed)
86 private function addBlockedServer(Configuration $config)
88 if (count($this->args) < 2 || count($this->args) > 3) {
89 throw new CommandArgsException('Add needs a domain and optional a reason.');
92 $domain = $this->getArgument(1);
93 $reason = (count($this->args) === 3) ? $this->getArgument(2) : self::DEFAULT_REASON;
97 $currBlocklist = $config->get('system', 'blocklist');
99 foreach ($currBlocklist as $blocked) {
100 if ($blocked['domain'] === $domain) {
107 $newBlockList[] = $blocked;
118 if ($config->set('system', 'blocklist', $newBlockList)) {
120 $this->out(sprintf("The domain '%s' is now updated. (Reason: '%s')", $domain, $reason));
122 $this->out(sprintf("The domain '%s' is now blocked. (Reason: '%s')", $domain, $reason));
126 $this->out(sprintf("Couldn't save '%s' as blocked server", $domain));
132 * Removes a server from the blocked list
134 * @param Configuration $config
136 * @return int The return code (0 = success, 1 = failed)
138 private function removeBlockedServer(Configuration $config)
140 if (count($this->args) !== 2) {
141 throw new CommandArgsException('Remove needs a second parameter.');
144 $domain = $this->getArgument(1);
148 $currBlocklist = $config->get('system', 'blocklist');
150 foreach ($currBlocklist as $blocked) {
151 if ($blocked['domain'] === $domain) {
154 $newBlockList[] = $blocked;
159 $this->out(sprintf("The domain '%s' is not blocked.", $domain));
163 if ($config->set('system', 'blocklist', $newBlockList)) {
164 $this->out(sprintf("The domain '%s' is not more blocked", $domain));
167 $this->out(sprintf("Couldn't remove '%s' from blocked servers", $domain));