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