]> git.mxchange.org Git - friendica.git/blob - mod/unfollow.php
Source cleaning mod/unfollow.php
[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 ($contact['network'] == Protocol::STATUSNET) {
43                 notice(L10n::t('Unfollowing is currently not supported by your network.'));
44                 goaway($return_url);
45                 // NOTREACHED
46         }
47
48         if (in_array($contact['network'], [Protocol::OSTATUS, Protocol::DIASPORA, Protocol::DFRN])) {
49                 $r = q("SELECT `contact`.*, `user`.* FROM `contact` INNER JOIN `user` ON `contact`.`uid` = `user`.`uid`
50                         WHERE `user`.`uid` = %d AND `contact`.`self` LIMIT 1",
51                         intval($uid)
52                 );
53                 if (DBA::isResult($r)) {
54                         Contact::terminateFriendship($r[0], $contact);
55                 }
56         }
57
58         // Sharing-only contacts get deleted as there no relationship any more
59         if ($contact['rel'] == Contact::SHARING) {
60                 Contact::remove($contact['id']);
61                 $return_path = 'contacts';
62         } else {
63                 DBA::update('contact', ['rel' => Contact::FOLLOWER], ['id' => $contact['id']]);
64                 $return_path = 'contacts/' . $contact['id'];
65         }
66
67         info(L10n::t('Contact unfollowed'));
68         goaway($return_path);
69         // NOTREACHED
70 }
71
72 function unfollow_content(App $a)
73 {
74         if (!local_user()) {
75                 notice(L10n::t('Permission denied.'));
76                 goaway($_SESSION['return_url']);
77                 // NOTREACHED
78         }
79
80         $uid = local_user();
81         $url = notags(trim($_REQUEST['url']));
82
83         $condition = ["`uid` = ? AND (`rel` = ? OR `rel` = ?) AND (`nurl` = ? OR `alias` = ? OR `alias` = ?)",
84                 local_user(), Contact::SHARING, Contact::FRIEND, normalise_link($url),
85                 normalise_link($url), $url];
86
87         $contact = DBA::selectFirst('contact', ['url', 'network', 'addr', 'name'], $condition);
88
89         if (!DBA::isResult($contact)) {
90                 notice(L10n::t("You aren't following this contact."));
91                 goaway('contacts');
92                 // NOTREACHED
93         }
94
95         if ($contact['network'] == Protocol::STATUSNET) {
96                 notice(L10n::t('Unfollowing is currently not supported by your network.'));
97                 goaway('contacts/' . $contact['id']);
98                 // NOTREACHED
99         }
100
101         $request = System::baseUrl() . '/unfollow';
102         $tpl = get_markup_template('auto_request.tpl');
103
104         $self = DBA::selectFirst('contact', ['url'], ['uid' => $uid, 'self' => true]);
105
106         if (!DBA::isResult($self)) {
107                 notice(L10n::t('Permission denied.'));
108                 goaway($_SESSION['return_url']);
109                 // NOTREACHED
110         }
111
112         // Makes the connection request for friendica contacts easier
113         $_SESSION['fastlane'] = $contact['url'];
114
115         $header = L10n::t('Disconnect/Unfollow');
116
117         $o = replace_macros($tpl, [
118                 '$header'        => htmlentities($header),
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 .= replace_macros(get_markup_template('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 }