]> git.mxchange.org Git - friendica.git/blob - src/Module/Contact/Revoke.php
Add Dice logging for Module creation
[friendica.git] / src / Module / Contact / Revoke.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\Module\Contact;
23
24 use Friendica\App;
25 use Friendica\BaseModule;
26 use Friendica\Content\Nav;
27 use Friendica\Core\L10n;
28 use Friendica\Core\Protocol;
29 use Friendica\Core\Renderer;
30 use Friendica\Database\Database;
31 use Friendica\Model;
32 use Friendica\Module\Contact;
33 use Friendica\Module\Response;
34 use Friendica\Module\Security\Login;
35 use Friendica\Network\HTTPException;
36 use Friendica\Util\Profiler;
37 use Psr\Log\LoggerInterface;
38
39 class Revoke extends BaseModule
40 {
41         /** @var array */
42         protected $contact;
43         
44         /** @var Database */
45         protected $dba;
46
47         public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Database $dba, Response $response, array $server, array $parameters = [])
48         {
49                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
50
51                 $this->dba     = $dba;
52
53                 if (!local_user()) {
54                         return;
55                 }
56
57                 $data = Model\Contact::getPublicAndUserContactID($this->parameters['id'], local_user());
58                 if (!$this->dba->isResult($data)) {
59                         throw new HTTPException\NotFoundException($this->t('Unknown contact.'));
60                 }
61
62                 if (empty($data['user'])) {
63                         throw new HTTPException\ForbiddenException();
64                 }
65
66                 $this->contact = Model\Contact::getById($data['user']);
67
68                 if ($this->contact['deleted']) {
69                         throw new HTTPException\NotFoundException($this->t('Contact is deleted.'));
70                 }
71
72                 if (!empty($this->contact['network']) && $this->contact['network'] == Protocol::PHANTOM) {
73                         throw new HTTPException\NotFoundException($this->t('Contact is being deleted.'));
74                 }
75         }
76
77         protected function post(array $request = [])
78         {
79                 if (!local_user()) {
80                         throw new HTTPException\UnauthorizedException();
81                 }
82
83                 self::checkFormSecurityTokenRedirectOnError('contact/' . $this->parameters['id'], 'contact_revoke');
84
85                 $result = Model\Contact::revokeFollow($this->contact);
86                 if ($result === true) {
87                         notice($this->t('Follow was successfully revoked.'));
88                 } elseif ($result === null) {
89                         notice($this->t('Follow was successfully revoked, however the remote contact won\'t be aware of this revokation.'));
90                 } else {
91                         notice($this->t('Unable to revoke follow, please try again later or contact the administrator.'));
92                 }
93
94                 $this->baseUrl->redirect('contact/' . $this->parameters['id']);
95         }
96
97         protected function content(array $request = []): string
98         {
99                 if (!local_user()) {
100                         return Login::form($_SERVER['REQUEST_URI']);
101                 }
102
103                 Nav::setSelected('contact');
104
105                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('contact_drop_confirm.tpl'), [
106                         '$l10n' => [
107                                 'header'  => $this->t('Revoke Follow'),
108                                 'message' => $this->t('Do you really want to revoke this contact\'s follow? This cannot be undone and they will have to manually follow you back again.'),
109                                 'confirm' => $this->t('Yes'),
110                                 'cancel'  => $this->t('Cancel'),
111                         ],
112                         '$contact'       => Contact::getContactTemplateVars($this->contact),
113                         '$method'        => 'post',
114                         '$confirm_url'   => $this->args->getCommand(),
115                         '$confirm_name'  => 'form_security_token',
116                         '$confirm_value' => BaseModule::getFormSecurityToken('contact_revoke'),
117                 ]);
118         }
119 }