]> git.mxchange.org Git - friendica.git/blob - src/Core/Console/GlobalCommunityBlock.php
Improve Console\GlobalCommunityBlock
[friendica.git] / src / Core / Console / GlobalCommunityBlock.php
1 <?php\r
2 \r
3 namespace Friendica\Core\Console;\r
4 \r
5 use Friendica\Core\L10n;\r
6 use Friendica\Model\Contact;\r
7 \r
8 /**\r
9  * @brief tool to block an account from the node\r
10  *\r
11  * With this tool, you can block an account in such a way, that no postings\r
12  * or comments this account writes are accepted to the node.\r
13  *\r
14  * License: AGPLv3 or later, same as Friendica\r
15  *\r
16  * @author Tobias Diekershoff <mrpetovan@gmail.com>\r
17  * @author Hypolite Petovan <mrpetovan@gmail.com>\r
18  */\r
19 class GlobalCommunityBlock extends \Asika\SimpleConsole\Console\r
20 {\r
21         protected $helpOptions = ['h', 'help', '?'];\r
22 \r
23         protected function getHelp()\r
24         {\r
25                 $help = <<<HELP\r
26 console globalcommunityblock - Block remote profile from interacting with this node\r
27 Usage\r
28         bin/console globalcommunityblock <profile_url> [-h|--help|-?] [-v]\r
29 \r
30 Description\r
31         Blocks an account in such a way that no postings or comments this account writes are accepted to this node.\r
32 \r
33 Options\r
34     -h|--help|-? Show help information\r
35     -v           Show more debug information.\r
36 HELP;\r
37                 return $help;\r
38         }\r
39 \r
40         protected function doExecute()\r
41         {\r
42                 if ($this->getOption('v')) {\r
43                         $this->out('Class: ' . __CLASS__);\r
44                         $this->out('Arguments: ' . var_export($this->args, true));\r
45                         $this->out('Options: ' . var_export($this->options, true));\r
46                 }\r
47 \r
48                 if (count($this->args) == 0) {\r
49                         $this->out($this->getHelp());\r
50                         return 0;\r
51                 }\r
52 \r
53                 if (count($this->args) > 1) {\r
54                         throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');\r
55                 }\r
56 \r
57                 require_once '.htconfig.php';\r
58                 $result = \dba::connect($db_host, $db_user, $db_pass, $db_data);\r
59                 unset($db_host, $db_user, $db_pass, $db_data);\r
60 \r
61                 if (!$result) {\r
62                         throw new \RuntimeException('Unable to connect to database');\r
63                 }\r
64 \r
65                 $contact_id = Contact::getIdForURL($this->getArgument(0));\r
66                 if (!$contact_id) {\r
67                         throw new \RuntimeException(L10n::t('Could not find any contact entry for this URL (%s)', $nurl));\r
68                 }\r
69                 if(Contact::block($contact_id)) {\r
70                         $this->out(L10n::t('The contact has been blocked from the node'));\r
71                 } else {\r
72                         throw new \RuntimeException('The contact block failed.');\r
73                 }\r
74 \r
75                 return 0;\r
76         }\r
77 }\r