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