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