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\Content\Widget;
24 use Friendica\Core\Protocol;
25 use Friendica\Core\Renderer;
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;
36 function follow_post(App $a)
39 throw new \Friendica\Network\HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
42 if (isset($_REQUEST['cancel'])) {
43 DI::baseUrl()->redirect('contact');
46 $url = Probe::cleanURI($_REQUEST['url']);
48 follow_process($a, $url);
51 function follow_content(App $a)
53 $return_path = 'contact';
56 notice(DI::l10n()->t('Permission denied.'));
57 DI::baseUrl()->redirect($return_path);
63 // Issue 4815: Silently removing a prefixing @
64 $url = ltrim(Strings::escapeTags(trim($_REQUEST['url'] ?? '')), '@!');
66 // Issue 6874: Allow remote following from Peertube
67 if (strpos($url, 'acct:') === 0) {
68 $url = str_replace('acct:', '', $url);
72 DI::baseUrl()->redirect($return_path);
75 $submit = DI::l10n()->t('Submit Request');
77 // Don't try to add a pending contact
78 $user_contact = DBA::selectFirst('contact', ['pending'], ["`uid` = ? AND ((`rel` != ?) OR (`network` = ?)) AND
79 (`nurl` = ? OR `alias` = ? OR `alias` = ?) AND `network` != ?",
80 $uid, Contact::FOLLOWER, Protocol::DFRN, Strings::normaliseLink($url),
81 Strings::normaliseLink($url), $url, Protocol::STATUSNET]);
83 if (DBA::isResult($user_contact)) {
84 if ($user_contact['pending']) {
85 notice(DI::l10n()->t('You already added this contact.'));
90 $contact = Contact::getByURL($url, true);
92 // Possibly it is a mail contact
93 if (empty($contact)) {
94 $contact = Probe::uri($url, Protocol::MAIL, $uid);
97 if (empty($contact) || ($contact['network'] == Protocol::PHANTOM)) {
98 // Possibly it is a remote item and not an account
99 follow_remote_item($url);
101 notice(DI::l10n()->t("The network type couldn't be detected. Contact can't be added."));
103 $contact = ['url' => $url, 'network' => Protocol::PHANTOM, 'name' => $url, 'keywords' => ''];
106 $protocol = Contact::getProtocol($contact['url'], $contact['network']);
108 if (($protocol == Protocol::DIASPORA) && !DI::config()->get('system', 'diaspora_enabled')) {
109 notice(DI::l10n()->t("Diaspora support isn't enabled. Contact can't be added."));
113 if (($protocol == Protocol::OSTATUS) && DI::config()->get('system', 'ostatus_disabled')) {
114 notice(DI::l10n()->t("OStatus support is disabled. Contact can't be added."));
118 if ($protocol == Protocol::MAIL) {
119 $contact['url'] = $contact['addr'];
122 if (!empty($_REQUEST['auto'])) {
123 follow_process($a, $contact['url']);
126 $request = DI::baseUrl() . '/follow';
127 $tpl = Renderer::getMarkupTemplate('auto_request.tpl');
129 $owner = User::getOwnerDataById($uid);
131 notice(DI::l10n()->t('Permission denied.'));
132 DI::baseUrl()->redirect($return_path);
136 $myaddr = $owner['url'];
138 $o = Renderer::replaceMacros($tpl, [
139 '$header' => DI::l10n()->t('Connect/Follow'),
140 '$pls_answer' => DI::l10n()->t('Please answer the following:'),
141 '$your_address' => DI::l10n()->t('Your Identity Address:'),
142 '$url_label' => DI::l10n()->t('Profile URL'),
143 '$keywords_label'=> DI::l10n()->t('Tags:'),
144 '$submit' => $submit,
145 '$cancel' => DI::l10n()->t('Cancel'),
147 '$request' => $request,
148 '$name' => $contact['name'],
149 '$url' => $contact['url'],
150 '$zrl' => Profile::zrl($contact['url']),
151 '$myaddr' => $myaddr,
152 '$keywords' => $contact['keywords'],
154 '$does_know_you' => ['knowyou', DI::l10n()->t('%s knows you', $contact['name'])],
155 '$addnote_field' => ['dfrn-request-message', DI::l10n()->t('Add a personal note:')],
158 DI::page()['aside'] = '';
160 if ($protocol != Protocol::PHANTOM) {
161 DI::page()['aside'] = Widget\VCard::getHTML(Contact::getByURL($contact['url'], false));
163 $o .= Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'),
164 ['$title' => DI::l10n()->t('Status Messages and Posts')]
167 // Show last public posts
168 $o .= Contact::getPostsFromUrl($contact['url']);
174 function follow_process(App $a, string $url)
176 $return_path = 'follow?url=' . urlencode($url);
178 $result = Contact::createFromProbe($a->user, $url, true);
180 if ($result['success'] == false) {
181 // Possibly it is a remote item and not an account
182 follow_remote_item($url);
184 if ($result['message']) {
185 notice($result['message']);
187 DI::baseUrl()->redirect($return_path);
188 } elseif ($result['cid']) {
189 DI::baseUrl()->redirect('contact/' . $result['cid']);
192 notice(DI::l10n()->t('The contact could not be added.'));
194 DI::baseUrl()->redirect($return_path);
197 function follow_remote_item($url)
199 $item_id = Item::fetchByLink($url, local_user());
201 // If the user-specific search failed, we search and probe a public post
202 $item_id = Item::fetchByLink($url);
205 if (!empty($item_id)) {
206 $item = Post::selectFirst(['guid'], ['id' => $item_id]);
207 if (DBA::isResult($item)) {
208 DI::baseUrl()->redirect('display/' . $item['guid']);