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