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