]> git.mxchange.org Git - friendica.git/blob - src/Console/ServerBlock.php
202f7bb75edf271fe43ad7d1c4d36442e22fb033
[friendica.git] / src / Console / ServerBlock.php
1 <?php
2
3 namespace Friendica\Console;
4
5 use Asika\SimpleConsole\CommandArgsException;
6 use Asika\SimpleConsole\Console;
7 use Console_Table;
8 use Friendica\BaseObject;
9 use Friendica\Core\Config\Configuration;
10
11 /**
12  * @brief Manage blocked servers
13  *
14  * With this tool, you can list the current blocked servers
15  * or you can add / remove a blocked server from the list
16  */
17 class ServerBlock extends Console
18 {
19         const DEFAULT_REASON = 'blocked';
20
21         protected $helpOptions = ['h', 'help', '?'];
22
23         protected function getHelp()
24         {
25                 $help = <<<HELP
26 console serverblock - Manage blocked server domain patterns
27 Usage
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]
31
32 Description
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.
35     
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...
40
41 Options
42     -h|--help|-? Show help information
43     -v           Show more debug information.
44 HELP;
45                 return $help;
46         }
47
48         protected function doExecute()
49         {
50                 $a = BaseObject::getApp();
51
52                 if (count($this->args) == 0) {
53                         $this->printBlockedServers($a->getConfig());
54                         return 0;
55                 }
56
57                 switch ($this->getArgument(0)) {
58                         case 'add':
59                                 return $this->addBlockedServer($a->getConfig());
60                         case 'remove':
61                                 return $this->removeBlockedServer($a->getConfig());
62                         default:
63                                 throw new CommandArgsException('Unknown command.');
64                                 break;
65                 }
66         }
67
68         /**
69          * Prints the whole list of blocked domains including the reason
70          *
71          * @param Configuration $config
72          */
73         private function printBlockedServers(Configuration $config)
74         {
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);
80                 }
81                 $this->out($table->getTable());
82         }
83
84         /**
85          * Adds a server to the blocked list
86          *
87          * @param Configuration $config
88          *
89          * @return int The return code (0 = success, 1 = failed)
90          */
91         private function addBlockedServer(Configuration $config)
92         {
93                 if (count($this->args) < 2 || count($this->args) > 3) {
94                         throw new CommandArgsException('Add needs a domain and optional a reason.');
95                 }
96
97                 $domain = $this->getArgument(1);
98                 $reason = (count($this->args) === 3) ? $this->getArgument(2) : self::DEFAULT_REASON;
99
100                 $update = false;
101
102                 $currBlocklist = $config->get('system', 'blocklist');
103                 $newBlockList = [];
104                 foreach ($currBlocklist  as $blocked) {
105                         if ($blocked['domain'] === $domain) {
106                                 $update = true;
107                                 $newBlockList[] = [
108                                         'domain' => $domain,
109                                         'reason' => $reason,
110                                 ];
111                         } else {
112                                 $newBlockList[] = $blocked;
113                         }
114                 }
115
116                 if (!$update) {
117                         $newBlockList[] = [
118                                 'domain' => $domain,
119                                 'reason' => $reason,
120                         ];
121                 }
122
123                 if ($config->set('system', 'blocklist', $newBlockList)) {
124                         if ($update) {
125                                 $this->out(sprintf("The domain '%s' is now updated. (Reason: '%s')", $domain, $reason));
126                         } else {
127                                 $this->out(sprintf("The domain '%s' is now blocked. (Reason: '%s')", $domain, $reason));
128                         }
129                         return 0;
130                 } else {
131                         $this->out(sprintf("Couldn't save '%s' as blocked server", $domain));
132                         return 1;
133                 }
134         }
135
136         /**
137          * Removes a server from the blocked list
138          *
139          * @param Configuration $config
140          *
141          * @return int The return code (0 = success, 1 = failed)
142          */
143         private function removeBlockedServer(Configuration $config)
144         {
145                 if (count($this->args) !== 2) {
146                         throw new CommandArgsException('Remove needs a second parameter.');
147                 }
148
149                 $domain = $this->getArgument(1);
150
151                 $found = false;
152
153                 $currBlocklist = $config->get('system', 'blocklist');
154                 $newBlockList = [];
155                 foreach ($currBlocklist as $blocked) {
156                         if ($blocked['domain'] === $domain) {
157                                 $found = true;
158                         } else {
159                                 $newBlockList[] = $blocked;
160                         }
161                 }
162
163                 if (!$found) {
164                         $this->out(sprintf("The domain '%s' is not blocked.", $domain));
165                         return 1;
166                 }
167
168                 if ($config->set('system', 'blocklist', $newBlockList)) {
169                         $this->out(sprintf("The domain '%s' is not more blocked", $domain));
170                         return 0;
171                 } else {
172                         $this->out(sprintf("Couldn't remove '%s' from blocked servers", $domain));
173                         return 1;
174                 }
175         }
176 }