]> git.mxchange.org Git - friendica.git/blob - src/Module/Contact/Unfollow.php
Removed redundant maximagesize = INF statements
[friendica.git] / src / Module / Contact / Unfollow.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Content\Widget;
26 use Friendica\Core\L10n;
27 use Friendica\Core\Protocol;
28 use Friendica\Core\Renderer;
29 use Friendica\Core\Session\Capability\IHandleUserSessions;
30 use Friendica\Database\Database;
31 use Friendica\Model\Contact;
32 use Friendica\Model\User;
33 use Friendica\Module\Response;
34 use Friendica\Navigation\SystemMessages;
35 use Friendica\Util\Profiler;
36 use Friendica\Util\Strings;
37 use Psr\Log\LoggerInterface;
38
39 class Unfollow extends \Friendica\BaseModule
40 {
41         /** @var IHandleUserSessions */
42         private $userSession;
43
44         /** @var SystemMessages */
45         private $systemMessages;
46
47         /** @var Database */
48         private $database;
49
50         /** @var App\Page */
51         private $page;
52
53         public function __construct(App\Page $page, Database $database, SystemMessages $systemMessages, IHandleUserSessions $userSession, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
54         {
55                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
56
57                 $this->userSession    = $userSession;
58                 $this->systemMessages = $systemMessages;
59                 $this->database       = $database;
60                 $this->page           = $page;
61         }
62
63         protected function post(array $request = [])
64         {
65                 if (!$this->userSession->getLocalUserId()) {
66                         $this->systemMessages->addNotice($this->t('Permission denied.'));
67                         $this->baseUrl->redirect('login');
68                 }
69
70                 $url = trim($request['url'] ?? '');
71
72                 $this->process($url);
73         }
74
75         protected function content(array $request = []): string
76         {
77                 $base_return_path = 'contact';
78
79                 if (!$this->userSession->getLocalUserId()) {
80                         $this->systemMessages->addNotice($this->t('Permission denied.'));
81                         $this->baseUrl->redirect('login');
82                 }
83
84                 $uid = $this->userSession->getLocalUserId();
85                 $url = trim($request['url']);
86
87                 $condition = [
88                         "`uid` = ?
89                         AND (`rel` = ? OR `rel` = ?)
90                         AND (`nurl` = ? OR `alias` = ? OR `alias` = ?)",
91                         $this->userSession->getLocalUserId(),
92                         Contact::SHARING, Contact::FRIEND,
93                         Strings::normaliseLink($url), Strings::normaliseLink($url), $url,
94                 ];
95
96                 $contact = $this->database->selectFirst('contact', ['url', 'id', 'uid', 'network', 'addr', 'name'], $condition);
97                 if (!$this->database->isResult($contact)) {
98                         $this->systemMessages->addNotice($this->t("You aren't following this contact."));
99                         $this->baseUrl->redirect($base_return_path);
100                 }
101
102                 if (!Protocol::supportsFollow($contact['network'])) {
103                         $this->systemMessages->addNotice($this->t('Unfollowing is currently not supported by your network.'));
104                         $this->baseUrl->redirect($base_return_path . '/' . $contact['id']);
105                 }
106
107                 $tpl = Renderer::getMarkupTemplate('auto_request.tpl');
108
109                 $self = $this->database->selectFirst('contact', ['url'], ['uid' => $uid, 'self' => true]);
110
111                 if (!$this->database->isResult($self)) {
112                         $this->systemMessages->addNotice($this->t('Permission denied.'));
113                         $this->baseUrl->redirect($base_return_path);
114                 }
115
116                 if (!empty($request['auto'])) {
117                         $this->process($contact['url']);
118                 }
119
120                 $o = Renderer::replaceMacros($tpl, [
121                         '$header'         => $this->t('Disconnect/Unfollow'),
122                         '$page_desc'      => '',
123                         '$your_address'   => $this->t('Your Identity Address:'),
124                         '$invite_desc'    => '',
125                         '$submit'         => $this->t('Submit Request'),
126                         '$cancel'         => $this->t('Cancel'),
127                         '$url'            => $contact['url'],
128                         '$zrl'            => Contact::magicLinkByContact($contact),
129                         '$url_label'      => $this->t('Profile URL'),
130                         '$myaddr'         => $self['url'],
131                         '$action'         => $this->baseUrl . '/contact/unfollow',
132                         '$keywords'       => '',
133                         '$keywords_label' => ''
134                 ]);
135
136                 $this->page['aside'] = Widget\VCard::getHTML(Contact::getByURL($contact['url'], false));
137
138                 $o .= Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), ['$title' => $this->t('Status Messages and Posts')]);
139
140                 // Show last public posts
141                 $o .= Contact::getPostsFromUrl($contact['url']);
142
143                 return $o;
144         }
145
146         private function process(string $url): void
147         {
148                 $base_return_path = 'contact';
149
150                 $uid = $this->userSession->getLocalUserId();
151
152                 $owner = User::getOwnerDataById($uid);
153                 if (!$owner) {
154                         throw new \Friendica\Network\HTTPException\NotFoundException();
155                 }
156
157                 $condition = [
158                         "`uid` = ?
159                         AND (`rel` = ? OR `rel` = ?)
160                         AND (`nurl` = ? OR `alias` = ? OR `alias` = ?)",
161                         $uid, Contact::SHARING, Contact::FRIEND,
162                         Strings::normaliseLink($url), Strings::normaliseLink($url), $url,
163                 ];
164                 $contact = $this->database->selectFirst('contact', [], $condition);
165
166                 if (!$this->database->isResult($contact)) {
167                         $this->systemMessages->addNotice($this->t("You aren't following this contact."));
168                         $this->baseUrl->redirect($base_return_path);
169                 }
170
171                 $return_path = $base_return_path . '/' . $contact['id'];
172
173                 try {
174                         Contact::unfollow($contact);
175                         $notice_message = $this->t('Contact was successfully unfollowed');
176                 } catch (\Exception $e) {
177                         $this->logger->error($e->getMessage(), ['contact' => $contact]);
178                         $notice_message = $this->t('Unable to unfollow this contact, please contact your administrator');
179                 }
180
181                 $this->systemMessages->addNotice($notice_message);
182                 $this->baseUrl->redirect($return_path);
183         }
184 }