]> git.mxchange.org Git - friendica.git/blob - src/Console/ServerBlock.php
Merge remote-tracking branch 'upstream/develop' into views
[friendica.git] / src / Console / ServerBlock.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Console;
23
24 use Asika\SimpleConsole\CommandArgsException;
25 use Asika\SimpleConsole\Console;
26 use Console_Table;
27 use Friendica\Core\Config\IConfig;
28
29 /**
30  * Manage blocked servers
31  *
32  * With this tool, you can list the current blocked servers
33  * or you can add / remove a blocked server from the list
34  */
35 class ServerBlock extends Console
36 {
37         const DEFAULT_REASON = 'blocked';
38
39         protected $helpOptions = ['h', 'help', '?'];
40
41         /**
42          * @var IConfig
43          */
44         private $config;
45
46         protected function getHelp()
47         {
48                 $help = <<<HELP
49 console serverblock - Manage blocked server domain patterns
50 Usage
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]
54
55 Description
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.
58     
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...
63
64 Options
65     -h|--help|-? Show help information
66     -v           Show more debug information.
67 HELP;
68                 return $help;
69         }
70
71         public function __construct(IConfig $config, $argv = null)
72         {
73                 parent::__construct($argv);
74
75                 $this->config = $config;
76         }
77
78         protected function doExecute()
79         {
80                 if (count($this->args) == 0) {
81                         $this->printBlockedServers($this->config);
82                         return 0;
83                 }
84
85                 switch ($this->getArgument(0)) {
86                         case 'add':
87                                 return $this->addBlockedServer($this->config);
88                         case 'remove':
89                                 return $this->removeBlockedServer($this->config);
90                         default:
91                                 throw new CommandArgsException('Unknown command.');
92                                 break;
93                 }
94         }
95
96         /**
97          * Prints the whole list of blocked domains including the reason
98          *
99          * @param IConfig $config
100          */
101         private function printBlockedServers(IConfig $config)
102         {
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);
108                 }
109                 $this->out($table->getTable());
110         }
111
112         /**
113          * Adds a server to the blocked list
114          *
115          * @param IConfig $config
116          *
117          * @return int The return code (0 = success, 1 = failed)
118          */
119         private function addBlockedServer(IConfig $config)
120         {
121                 if (count($this->args) < 2 || count($this->args) > 3) {
122                         throw new CommandArgsException('Add needs a domain and optional a reason.');
123                 }
124
125                 $domain = $this->getArgument(1);
126                 $reason = (count($this->args) === 3) ? $this->getArgument(2) : self::DEFAULT_REASON;
127
128                 $update = false;
129
130                 $currBlocklist = $config->get('system', 'blocklist', []);
131                 $newBlockList = [];
132                 foreach ($currBlocklist  as $blocked) {
133                         if ($blocked['domain'] === $domain) {
134                                 $update = true;
135                                 $newBlockList[] = [
136                                         'domain' => $domain,
137                                         'reason' => $reason,
138                                 ];
139                         } else {
140                                 $newBlockList[] = $blocked;
141                         }
142                 }
143
144                 if (!$update) {
145                         $newBlockList[] = [
146                                 'domain' => $domain,
147                                 'reason' => $reason,
148                         ];
149                 }
150
151                 if ($config->set('system', 'blocklist', $newBlockList)) {
152                         if ($update) {
153                                 $this->out(sprintf("The domain '%s' is now updated. (Reason: '%s')", $domain, $reason));
154                         } else {
155                                 $this->out(sprintf("The domain '%s' is now blocked. (Reason: '%s')", $domain, $reason));
156                         }
157                         return 0;
158                 } else {
159                         $this->out(sprintf("Couldn't save '%s' as blocked server", $domain));
160                         return 1;
161                 }
162         }
163
164         /**
165          * Removes a server from the blocked list
166          *
167          * @param IConfig $config
168          *
169          * @return int The return code (0 = success, 1 = failed)
170          */
171         private function removeBlockedServer(IConfig $config)
172         {
173                 if (count($this->args) !== 2) {
174                         throw new CommandArgsException('Remove needs a second parameter.');
175                 }
176
177                 $domain = $this->getArgument(1);
178
179                 $found = false;
180
181                 $currBlocklist = $config->get('system', 'blocklist', []);
182                 $newBlockList = [];
183                 foreach ($currBlocklist as $blocked) {
184                         if ($blocked['domain'] === $domain) {
185                                 $found = true;
186                         } else {
187                                 $newBlockList[] = $blocked;
188                         }
189                 }
190
191                 if (!$found) {
192                         $this->out(sprintf("The domain '%s' is not blocked.", $domain));
193                         return 1;
194                 }
195
196                 if ($config->set('system', 'blocklist', $newBlockList)) {
197                         $this->out(sprintf("The domain '%s' is not more blocked", $domain));
198                         return 0;
199                 } else {
200                         $this->out(sprintf("Couldn't remove '%s' from blocked servers", $domain));
201                         return 1;
202                 }
203         }
204 }