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