]> git.mxchange.org Git - friendica.git/blob - mod/unfollow.php
Merge pull request #12025 from annando/no-boot-src-module
[friendica.git] / mod / 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 use Friendica\App;
23 use Friendica\Content\Widget;
24 use Friendica\Core\Protocol;
25 use Friendica\Core\Renderer;
26 use Friendica\Core\Session;
27 use Friendica\Database\DBA;
28 use Friendica\DI;
29 use Friendica\Model\Contact;
30 use Friendica\Model\User;
31 use Friendica\Util\Strings;
32
33 function unfollow_post(App $a)
34 {
35         if (!Session::getLocalUser()) {
36                 DI::sysmsg()->addNotice(DI::l10n()->t('Permission denied.'));
37                 DI::baseUrl()->redirect('login');
38                 // NOTREACHED
39         }
40
41         $url = trim($_REQUEST['url'] ?? '');
42
43         unfollow_process($url);
44 }
45
46 function unfollow_content(App $a)
47 {
48         $base_return_path = 'contact';
49
50         if (!Session::getLocalUser()) {
51                 DI::sysmsg()->addNotice(DI::l10n()->t('Permission denied.'));
52                 DI::baseUrl()->redirect('login');
53                 // NOTREACHED
54         }
55
56         $uid = Session::getLocalUser();
57         $url = trim($_REQUEST['url']);
58
59         $condition = ["`uid` = ? AND (`rel` = ? OR `rel` = ?) AND (`nurl` = ? OR `alias` = ? OR `alias` = ?)",
60                 Session::getLocalUser(), Contact::SHARING, Contact::FRIEND, Strings::normaliseLink($url),
61                 Strings::normaliseLink($url), $url];
62
63         $contact = DBA::selectFirst('contact', ['url', 'id', 'uid', 'network', 'addr', 'name'], $condition);
64
65         if (!DBA::isResult($contact)) {
66                 DI::sysmsg()->addNotice(DI::l10n()->t("You aren't following this contact."));
67                 DI::baseUrl()->redirect($base_return_path);
68                 // NOTREACHED
69         }
70
71         if (!Protocol::supportsFollow($contact['network'])) {
72                 DI::sysmsg()->addNotice(DI::l10n()->t('Unfollowing is currently not supported by your network.'));
73                 DI::baseUrl()->redirect($base_return_path . '/' . $contact['id']);
74                 // NOTREACHED
75         }
76
77         $request = DI::baseUrl() . '/unfollow';
78         $tpl = Renderer::getMarkupTemplate('auto_request.tpl');
79
80         $self = DBA::selectFirst('contact', ['url'], ['uid' => $uid, 'self' => true]);
81
82         if (!DBA::isResult($self)) {
83                 DI::sysmsg()->addNotice(DI::l10n()->t('Permission denied.'));
84                 DI::baseUrl()->redirect($base_return_path);
85                 // NOTREACHED
86         }
87
88         if (!empty($_REQUEST['auto'])) {
89                 unfollow_process($contact['url']);
90         }
91
92         $o = Renderer::replaceMacros($tpl, [
93                 '$header'        => DI::l10n()->t('Disconnect/Unfollow'),
94                 '$page_desc'     => '',
95                 '$your_address'  => DI::l10n()->t('Your Identity Address:'),
96                 '$invite_desc'   => '',
97                 '$submit'        => DI::l10n()->t('Submit Request'),
98                 '$cancel'        => DI::l10n()->t('Cancel'),
99                 '$url'           => $contact['url'],
100                 '$zrl'           => Contact::magicLinkByContact($contact),
101                 '$url_label'     => DI::l10n()->t('Profile URL'),
102                 '$myaddr'        => $self['url'],
103                 '$request'       => $request,
104                 '$keywords'      => '',
105                 '$keywords_label'=> ''
106         ]);
107
108         DI::page()['aside'] = Widget\VCard::getHTML(Contact::getByURL($contact['url'], false));
109
110         $o .= Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), ['$title' => DI::l10n()->t('Status Messages and Posts')]);
111
112         // Show last public posts
113         $o .= Contact::getPostsFromUrl($contact['url']);
114
115         return $o;
116 }
117
118 function unfollow_process(string $url)
119 {
120         $base_return_path = 'contact';
121
122         $uid = Session::getLocalUser();
123
124         $owner = User::getOwnerDataById($uid);
125         if (!$owner) {
126                 throw new \Friendica\Network\HTTPException\NotFoundException();
127         }
128
129         $condition = ["`uid` = ? AND (`rel` = ? OR `rel` = ?) AND (`nurl` = ? OR `alias` = ? OR `alias` = ?)",
130                 $uid, Contact::SHARING, Contact::FRIEND, Strings::normaliseLink($url),
131                 Strings::normaliseLink($url), $url];
132         $contact = DBA::selectFirst('contact', [], $condition);
133
134         if (!DBA::isResult($contact)) {
135                 DI::sysmsg()->addNotice(DI::l10n()->t("You aren't following this contact."));
136                 DI::baseUrl()->redirect($base_return_path);
137                 // NOTREACHED
138         }
139
140         $return_path = $base_return_path . '/' . $contact['id'];
141
142         try {
143                 Contact::unfollow($contact);
144                 $notice_message = DI::l10n()->t('Contact was successfully unfollowed');
145         } catch (Exception $e) {
146                 DI::logger()->error($e->getMessage(), ['contact' => $contact]);
147                 $notice_message = DI::l10n()->t('Unable to unfollow this contact, please contact your administrator');
148         }
149
150         DI::sysmsg()->addNotice($notice_message);
151         DI::baseUrl()->redirect($return_path);
152 }