]> git.mxchange.org Git - friendica.git/blob - mod/follow.php
Some more "exit" replaced
[friendica.git] / mod / follow.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 use Friendica\App;
23 use Friendica\Content\Widget;
24 use Friendica\Core\Protocol;
25 use Friendica\Core\Renderer;
26 use Friendica\DI;
27 use Friendica\Model\Contact;
28 use Friendica\Model\Profile;
29 use Friendica\Model\Item;
30 use Friendica\Network\Probe;
31 use Friendica\Database\DBA;
32 use Friendica\Model\Post;
33 use Friendica\Model\User;
34 use Friendica\Util\Strings;
35
36 function follow_post(App $a)
37 {
38         if (!local_user()) {
39                 throw new \Friendica\Network\HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
40         }
41
42         if (isset($_REQUEST['cancel'])) {
43                 DI::baseUrl()->redirect('contact');
44         }
45
46         $url = Probe::cleanURI($_REQUEST['url']);
47
48         follow_process($a, $url);
49 }
50
51 function follow_content(App $a)
52 {
53         $return_path = 'contact';
54
55         if (!local_user()) {
56                 notice(DI::l10n()->t('Permission denied.'));
57                 DI::baseUrl()->redirect($return_path);
58                 // NOTREACHED
59         }
60
61         $uid = local_user();
62
63         $url = Probe::cleanURI(trim($_REQUEST['url'] ?? ''));
64
65         // Issue 6874: Allow remote following from Peertube
66         if (strpos($url, 'acct:') === 0) {
67                 $url = str_replace('acct:', '', $url);
68         }
69
70         if (!$url) {
71                 DI::baseUrl()->redirect($return_path);
72         }
73
74         $submit = DI::l10n()->t('Submit Request');
75
76         // Don't try to add a pending contact
77         $user_contact = DBA::selectFirst('contact', ['pending'], ["`uid` = ? AND ((`rel` != ?) OR (`network` = ?)) AND
78                 (`nurl` = ? OR `alias` = ? OR `alias` = ?) AND `network` != ?",
79                 $uid, Contact::FOLLOWER, Protocol::DFRN, Strings::normaliseLink($url),
80                 Strings::normaliseLink($url), $url, Protocol::STATUSNET]);
81
82         if (DBA::isResult($user_contact)) {
83                 if ($user_contact['pending']) {
84                         notice(DI::l10n()->t('You already added this contact.'));
85                         $submit = '';
86                 }
87         }
88
89         $contact = Contact::getByURL($url, true);
90
91         // Possibly it is a mail contact
92         if (empty($contact)) {
93                 $contact = Probe::uri($url, Protocol::MAIL, $uid);
94         }
95
96         if (empty($contact) || ($contact['network'] == Protocol::PHANTOM)) {
97                 // Possibly it is a remote item and not an account
98                 follow_remote_item($url);
99
100                 notice(DI::l10n()->t("The network type couldn't be detected. Contact can't be added."));
101                 $submit = '';
102                 $contact = ['url' => $url, 'network' => Protocol::PHANTOM, 'name' => $url, 'keywords' => ''];
103         }
104
105         $protocol = Contact::getProtocol($contact['url'], $contact['network']);
106
107         if (($protocol == Protocol::DIASPORA) && !DI::config()->get('system', 'diaspora_enabled')) {
108                 notice(DI::l10n()->t("Diaspora support isn't enabled. Contact can't be added."));
109                 $submit = '';
110         }
111
112         if (($protocol == Protocol::OSTATUS) && DI::config()->get('system', 'ostatus_disabled')) {
113                 notice(DI::l10n()->t("OStatus support is disabled. Contact can't be added."));
114                 $submit = '';
115         }
116
117         if ($protocol == Protocol::MAIL) {
118                 $contact['url'] = $contact['addr'];
119         }
120
121         if (!empty($_REQUEST['auto'])) {
122                 follow_process($a, $contact['url']);
123         }
124
125         $request = DI::baseUrl() . '/follow';
126         $tpl = Renderer::getMarkupTemplate('auto_request.tpl');
127
128         $owner = User::getOwnerDataById($uid);
129         if (empty($owner)) {
130                 notice(DI::l10n()->t('Permission denied.'));
131                 DI::baseUrl()->redirect($return_path);
132                 // NOTREACHED
133         }
134
135         $myaddr = $owner['url'];
136
137         $o = Renderer::replaceMacros($tpl, [
138                 '$header'        => DI::l10n()->t('Connect/Follow'),
139                 '$pls_answer'    => DI::l10n()->t('Please answer the following:'),
140                 '$your_address'  => DI::l10n()->t('Your Identity Address:'),
141                 '$url_label'     => DI::l10n()->t('Profile URL'),
142                 '$keywords_label'=> DI::l10n()->t('Tags:'),
143                 '$submit'        => $submit,
144                 '$cancel'        => DI::l10n()->t('Cancel'),
145
146                 '$request'       => $request,
147                 '$name'          => $contact['name'],
148                 '$url'           => $contact['url'],
149                 '$zrl'           => Profile::zrl($contact['url']),
150                 '$myaddr'        => $myaddr,
151                 '$keywords'      => $contact['keywords'],
152
153                 '$does_know_you' => ['knowyou', DI::l10n()->t('%s knows you', $contact['name'])],
154                 '$addnote_field' => ['dfrn-request-message', DI::l10n()->t('Add a personal note:')],
155         ]);
156
157         DI::page()['aside'] = '';
158
159         if (!in_array($protocol, [Protocol::PHANTOM, Protocol::MAIL])) {
160                 DI::page()['aside'] = Widget\VCard::getHTML($contact);
161
162                 $o .= Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'),
163                         ['$title' => DI::l10n()->t('Status Messages and Posts')]
164                 );
165
166                 // Show last public posts
167                 $o .= Contact::getPostsFromUrl($contact['url']);
168         }
169
170         return $o;
171 }
172
173 function follow_process(App $a, string $url)
174 {
175         $return_path = 'follow?url=' . urlencode($url);
176
177         $result = Contact::createFromProbeForUser($a->getLoggedInUserId(), $url);
178
179         if ($result['success'] == false) {
180                 // Possibly it is a remote item and not an account
181                 follow_remote_item($url);
182
183                 if ($result['message']) {
184                         notice($result['message']);
185                 }
186                 DI::baseUrl()->redirect($return_path);
187         } elseif ($result['cid']) {
188                 DI::baseUrl()->redirect('contact/' . $result['cid']);
189         }
190
191         notice(DI::l10n()->t('The contact could not be added.'));
192
193         DI::baseUrl()->redirect($return_path);
194 }
195
196 function follow_remote_item($url)
197 {
198         $item_id = Item::fetchByLink($url, local_user());
199         if (!$item_id) {
200                 // If the user-specific search failed, we search and probe a public post
201                 $item_id = Item::fetchByLink($url);
202         }
203
204         if (!empty($item_id)) {
205                 $item = Post::selectFirst(['guid'], ['id' => $item_id]);
206                 if (DBA::isResult($item)) {
207                         DI::baseUrl()->redirect('display/' . $item['guid']);
208                 }
209         }
210 }