]> git.mxchange.org Git - friendica.git/blob - src/Console/ServerBlock.php
Rename Repository\Notify->NotifyOnDesktop to shouldShowOnDesktop
[friendica.git] / src / Console / ServerBlock.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
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\Moderation\DomainPatternBlocklist;
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         protected $helpOptions = ['h', 'help', '?'];
38
39         /** @var DomainPatternBlocklist */
40         private $blocklist;
41
42         protected function getHelp(): string
43         {
44                 return <<<HELP
45 console serverblock - Manage blocked server domain patterns
46 Usage
47     bin/console serverblock [-h|--help|-?] [-v]
48     bin/console serverblock add <pattern> <reason> [-h|--help|-?] [-v]
49     bin/console serverblock remove <pattern> [-h|--help|-?] [-v]
50     bin/console serverblock export <filename>
51     bin/console serverblock import <filename>
52
53 Description
54     With this tool, you can list the current blocked server domain patterns
55     or you can add / remove a blocked server domain pattern from the list.
56     Using the export and import options you can share your server blocklist
57     with other node admins by CSV files.
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         }
69
70         public function __construct(DomainPatternBlocklist $blocklist, $argv = null)
71         {
72                 parent::__construct($argv);
73
74                 $this->blocklist = $blocklist;
75         }
76
77         protected function doExecute(): int
78         {
79                 if (count($this->args) == 0) {
80                         $this->printBlockedServers();
81                         return 0;
82                 }
83
84                 switch ($this->getArgument(0)) {
85                         case 'add':
86                                 return $this->addBlockedServer();
87                         case 'remove':
88                                 return $this->removeBlockedServer();
89                         case 'export':
90                                 return $this->exportBlockedServers();
91                         case 'import':
92                                 return $this->importBlockedServers();
93                         default:
94                                 throw new CommandArgsException('Unknown command.');
95                 }
96         }
97
98         /**
99          * Exports the list of blocked domain patterns including the reason for the
100          * block to a CSV file.
101          *
102          * @return int
103          * @throws \Exception
104          */
105         private function exportBlockedServers(): int
106         {
107                 $filename = $this->getArgument(1);
108
109                 $this->blocklist->exportToFile($filename);
110
111                 // Success
112                 return 0;
113         }
114
115         /**
116          * Imports a list of domain patterns and a reason for the block from a CSV
117          * file, e.g. created with the export function.
118          *
119          * @return int
120          * @throws \Exception
121          */
122         private function importBlockedServers(): int
123         {
124                 $filename = $this->getArgument(1);
125
126                 $newBlockList = $this->blocklist::extractFromCSVFile($filename);
127
128                 if ($this->blocklist->append($newBlockList)) {
129                         $this->out(sprintf("Entries from %s that were not blocked before are now blocked", $filename));
130                         return 0;
131                 } else {
132                         $this->out("Couldn't save the block list");
133                         return 1;
134                 }
135         }
136
137         /**
138          * Prints the whole list of blocked domain patterns including the reason
139          */
140         private function printBlockedServers(): void
141         {
142                 $table = new Console_Table();
143                 $table->setHeaders(['Pattern', 'Reason']);
144                 foreach ($this->blocklist->get() as $pattern) {
145                         $table->addRow($pattern);
146                 }
147
148                 $this->out($table->getTable());
149         }
150
151         /**
152          * Adds a domain pattern to the block list
153          *
154          * @return int The return code (0 = success, 1 = failed)
155          */
156         private function addBlockedServer(): int
157         {
158                 if (count($this->args) != 3) {
159                         throw new CommandArgsException('Add needs a domain pattern and a reason.');
160                 }
161
162                 $pattern = $this->getArgument(1);
163                 $reason  = $this->getArgument(2);
164
165                 $result = $this->blocklist->addPattern($pattern, $reason);
166                 if ($result) {
167                         if ($result == 2) {
168                                 $this->out(sprintf("The domain pattern '%s' is now updated. (Reason: '%s')", $pattern, $reason));
169                         } else {
170                                 $this->out(sprintf("The domain pattern '%s' is now blocked. (Reason: '%s')", $pattern, $reason));
171                         }
172                         return 0;
173                 } else {
174                         $this->out(sprintf("Couldn't save '%s' as blocked domain pattern", $pattern));
175                         return 1;
176                 }
177         }
178
179         /**
180          * Removes a domain pattern from the block list
181          *
182          * @return int The return code (0 = success, 1 = failed)
183          */
184         private function removeBlockedServer(): int
185         {
186                 if (count($this->args) !== 2) {
187                         throw new CommandArgsException('Remove needs a second parameter.');
188                 }
189
190                 $pattern = $this->getArgument(1);
191
192                 $result = $this->blocklist->removePattern($pattern);
193                 if ($result) {
194                         if ($result == 2) {
195                                 $this->out(sprintf("The domain pattern '%s' isn't blocked anymore", $pattern));
196                                 return 0;
197                         } else {
198                                 $this->out(sprintf("The domain pattern '%s' wasn't blocked.", $pattern));
199                                 return 1;
200                         }
201                 } else {
202                         $this->out(sprintf("Couldn't remove '%s' from blocked domain patterns", $pattern));
203                         return 1;
204                 }
205         }
206 }