3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
23 use Friendica\Core\Protocol;
24 use Friendica\Core\Renderer;
26 use Friendica\Model\Contact;
27 use Friendica\Model\Profile;
28 use Friendica\Model\Item;
29 use Friendica\Network\Probe;
30 use Friendica\Database\DBA;
31 use Friendica\Model\Post;
32 use Friendica\Model\User;
33 use Friendica\Util\Strings;
35 function follow_post(App $a)
38 throw new \Friendica\Network\HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
41 if (isset($_REQUEST['cancel'])) {
42 DI::baseUrl()->redirect('contact');
45 $url = Probe::cleanURI($_REQUEST['url']);
47 follow_process($a, $url);
50 function follow_content(App $a)
52 $return_path = 'contact';
55 notice(DI::l10n()->t('Permission denied.'));
56 DI::baseUrl()->redirect($return_path);
62 // Issue 4815: Silently removing a prefixing @
63 $url = ltrim(Strings::escapeTags(trim($_REQUEST['url'] ?? '')), '@!');
65 // Issue 6874: Allow remote following from Peertube
66 if (strpos($url, 'acct:') === 0) {
67 $url = str_replace('acct:', '', $url);
71 DI::baseUrl()->redirect($return_path);
74 $submit = DI::l10n()->t('Submit Request');
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]);
82 if (DBA::isResult($user_contact)) {
83 if ($user_contact['pending']) {
84 notice(DI::l10n()->t('You already added this contact.'));
89 $contact = Contact::getByURL($url, true);
91 // Possibly it is a mail contact
92 if (empty($contact)) {
93 $contact = Probe::uri($url, Protocol::MAIL, $uid);
96 if (empty($contact) || ($contact['network'] == Protocol::PHANTOM)) {
97 // Possibly it is a remote item and not an account
98 follow_remote_item($url);
100 notice(DI::l10n()->t("The network type couldn't be detected. Contact can't be added."));
102 $contact = ['url' => $url, 'network' => Protocol::PHANTOM, 'name' => $url, 'keywords' => ''];
105 $protocol = Contact::getProtocol($contact['url'], $contact['network']);
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."));
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."));
117 if ($protocol == Protocol::MAIL) {
118 $contact['url'] = $contact['addr'];
121 if (($protocol === Protocol::DFRN) && !DBA::isResult($contact)) {
122 $request = $contact['request'];
123 $tpl = Renderer::getMarkupTemplate('dfrn_request.tpl');
125 if (!empty($_REQUEST['auto'])) {
126 follow_process($a, $contact['url']);
129 $request = DI::baseUrl() . '/follow';
130 $tpl = Renderer::getMarkupTemplate('auto_request.tpl');
133 $owner = User::getOwnerDataById($uid);
135 notice(DI::l10n()->t('Permission denied.'));
136 DI::baseUrl()->redirect($return_path);
140 $myaddr = $owner['url'];
142 // Makes the connection request for friendica contacts easier
143 $_SESSION['fastlane'] = $contact['url'];
145 $o = Renderer::replaceMacros($tpl, [
146 '$header' => DI::l10n()->t('Connect/Follow'),
147 '$pls_answer' => DI::l10n()->t('Please answer the following:'),
148 '$your_address' => DI::l10n()->t('Your Identity Address:'),
149 '$url_label' => DI::l10n()->t('Profile URL'),
150 '$keywords_label'=> DI::l10n()->t('Tags:'),
151 '$submit' => $submit,
152 '$cancel' => DI::l10n()->t('Cancel'),
154 '$request' => $request,
155 '$name' => $contact['name'],
156 '$url' => $contact['url'],
157 '$zrl' => Profile::zrl($contact['url']),
158 '$myaddr' => $myaddr,
159 '$keywords' => $contact['keywords'],
161 '$does_know_you' => ['knowyou', DI::l10n()->t('%s knows you', $contact['name'])],
162 '$addnote_field' => ['dfrn-request-message', DI::l10n()->t('Add a personal note:')],
165 DI::page()['aside'] = '';
167 if ($protocol != Protocol::PHANTOM) {
168 Profile::load($a, '', $contact, false);
170 $o .= Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'),
171 ['$title' => DI::l10n()->t('Status Messages and Posts')]
174 // Show last public posts
175 $o .= Contact::getPostsFromUrl($contact['url']);
181 function follow_process(App $a, string $url)
183 $return_path = 'follow?url=' . urlencode($url);
185 // Makes the connection request for friendica contacts easier
186 // This is just a precaution if maybe this page is called somewhere directly via POST
187 $_SESSION['fastlane'] = $url;
189 $result = Contact::createFromProbe($a->user, $url, true);
191 if ($result['success'] == false) {
192 // Possibly it is a remote item and not an account
193 follow_remote_item($url);
195 if ($result['message']) {
196 notice($result['message']);
198 DI::baseUrl()->redirect($return_path);
199 } elseif ($result['cid']) {
200 DI::baseUrl()->redirect('contact/' . $result['cid']);
203 notice(DI::l10n()->t('The contact could not be added.'));
205 DI::baseUrl()->redirect($return_path);
208 function follow_remote_item($url)
210 $item_id = Item::fetchByLink($url, local_user());
212 // If the user-specific search failed, we search and probe a public post
213 $item_id = Item::fetchByLink($url);
216 if (!empty($item_id)) {
217 $item = Post::selectFirst(['guid'], ['id' => $item_id]);
218 if (DBA::isResult($item)) {
219 DI::baseUrl()->redirect('display/' . $item['guid']);