]> git.mxchange.org Git - friendica.git/blob - mod/unfollow.php
9f9ebdf5930307bba830dc7604052b07607ce8b8
[friendica.git] / mod / unfollow.php
1 <?php
2 /**
3  * @file mod/unfollow.php
4  */
5
6 use Friendica\App;
7 use Friendica\Core\L10n;
8 use Friendica\Core\Protocol;
9 use Friendica\Core\Renderer;
10 use Friendica\Core\System;
11 use Friendica\Database\DBA;
12 use Friendica\DI;
13 use Friendica\Model\Contact;
14 use Friendica\Model\Profile;
15 use Friendica\Model\User;
16 use Friendica\Util\Strings;
17
18 function unfollow_post(App $a)
19 {
20         $base_return_path = 'contact';
21
22         if (!local_user()) {
23                 notice(L10n::t('Permission denied.'));
24                 DI::baseUrl()->redirect('login');
25                 // NOTREACHED
26         }
27
28         $uid = local_user();
29         $url = Strings::escapeTags(trim($_REQUEST['url'] ?? ''));
30
31         $condition = ["`uid` = ? AND (`rel` = ? OR `rel` = ?) AND (`nurl` = ? OR `alias` = ? OR `alias` = ?)",
32                 $uid, Contact::SHARING, Contact::FRIEND, Strings::normaliseLink($url),
33                 Strings::normaliseLink($url), $url];
34         $contact = DBA::selectFirst('contact', [], $condition);
35
36         if (!DBA::isResult($contact)) {
37                 notice(L10n::t("You aren't following this contact."));
38                 DI::baseUrl()->redirect($base_return_path);
39                 // NOTREACHED
40         }
41
42         if (!empty($_REQUEST['cancel'])) {
43                 DI::baseUrl()->redirect($base_return_path . '/' . $contact['id']);
44         }
45
46         if (!in_array($contact['network'], Protocol::NATIVE_SUPPORT)) {
47                 notice(L10n::t('Unfollowing is currently not supported by your network.'));
48                 DI::baseUrl()->redirect($base_return_path . '/' . $contact['id']);
49                 // NOTREACHED
50         }
51
52         $dissolve = ($contact['rel'] == Contact::SHARING);
53
54         $owner = User::getOwnerDataById($uid);
55         if ($owner) {
56                 Contact::terminateFriendship($owner, $contact, $dissolve);
57         }
58
59         // Sharing-only contacts get deleted as there no relationship any more
60         if ($dissolve) {
61                 Contact::remove($contact['id']);
62                 $return_path = $base_return_path;
63         } else {
64                 DBA::update('contact', ['rel' => Contact::FOLLOWER], ['id' => $contact['id']]);
65                 $return_path = $base_return_path . '/' . $contact['id'];
66         }
67
68         info(L10n::t('Contact unfollowed'));
69         DI::baseUrl()->redirect($return_path);
70         // NOTREACHED
71 }
72
73 function unfollow_content(App $a)
74 {
75         $base_return_path = 'contact';
76
77         if (!local_user()) {
78                 notice(L10n::t('Permission denied.'));
79                 DI::baseUrl()->redirect('login');
80                 // NOTREACHED
81         }
82
83         $uid = local_user();
84         $url = Strings::escapeTags(trim($_REQUEST['url']));
85
86         $condition = ["`uid` = ? AND (`rel` = ? OR `rel` = ?) AND (`nurl` = ? OR `alias` = ? OR `alias` = ?)",
87                 local_user(), Contact::SHARING, Contact::FRIEND, Strings::normaliseLink($url),
88                 Strings::normaliseLink($url), $url];
89
90         $contact = DBA::selectFirst('contact', ['url', 'network', 'addr', 'name'], $condition);
91
92         if (!DBA::isResult($contact)) {
93                 notice(L10n::t("You aren't following this contact."));
94                 DI::baseUrl()->redirect($base_return_path);
95                 // NOTREACHED
96         }
97
98         if (!in_array($contact['network'], Protocol::NATIVE_SUPPORT)) {
99                 notice(L10n::t('Unfollowing is currently not supported by your network.'));
100                 DI::baseUrl()->redirect($base_return_path . '/' . $contact['id']);
101                 // NOTREACHED
102         }
103
104         $request = System::baseUrl() . '/unfollow';
105         $tpl = Renderer::getMarkupTemplate('auto_request.tpl');
106
107         $self = DBA::selectFirst('contact', ['url'], ['uid' => $uid, 'self' => true]);
108
109         if (!DBA::isResult($self)) {
110                 notice(L10n::t('Permission denied.'));
111                 DI::baseUrl()->redirect($base_return_path);
112                 // NOTREACHED
113         }
114
115         // Makes the connection request for friendica contacts easier
116         $_SESSION['fastlane'] = $contact['url'];
117
118         $o = Renderer::replaceMacros($tpl, [
119                 '$header'        => L10n::t('Disconnect/Unfollow'),
120                 '$desc'          => '',
121                 '$pls_answer'    => '',
122                 '$does_know_you' => '',
123                 '$add_note'      => '',
124                 '$page_desc'     => '',
125                 '$friendica'     => '',
126                 '$statusnet'     => '',
127                 '$diaspora'      => '',
128                 '$diasnote'      => '',
129                 '$your_address'  => L10n::t('Your Identity Address:'),
130                 '$invite_desc'   => '',
131                 '$emailnet'      => '',
132                 '$submit'        => L10n::t('Submit Request'),
133                 '$cancel'        => L10n::t('Cancel'),
134                 '$nickname'      => '',
135                 '$name'          => $contact['name'],
136                 '$url'           => $contact['url'],
137                 '$zrl'           => Contact::magicLink($contact['url']),
138                 '$url_label'     => L10n::t('Profile URL'),
139                 '$myaddr'        => $self['url'],
140                 '$request'       => $request,
141                 '$keywords'      => '',
142                 '$keywords_label'=> ''
143         ]);
144
145         DI::page()['aside'] = '';
146         Profile::load($a, '', 0, Contact::getDetailsByURL($contact['url']));
147
148         $o .= Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), ['$title' => L10n::t('Status Messages and Posts')]);
149
150         // Show last public posts
151         $o .= Contact::getPostsFromUrl($contact['url']);
152
153         return $o;
154 }