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