3 * @copyright Copyright (C) 2020, Friendica
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\Util\Strings;
33 function follow_post(App $a)
36 throw new \Friendica\Network\HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
39 if (isset($_REQUEST['cancel'])) {
40 DI::baseUrl()->redirect('contact');
43 $url = Probe::cleanURI($_REQUEST['url']);
44 $return_path = 'follow?url=' . urlencode($url);
46 // Makes the connection request for friendica contacts easier
47 // This is just a precaution if maybe this page is called somewhere directly via POST
48 $_SESSION['fastlane'] = $url;
50 $result = Contact::createFromProbe($a->user, $url, true);
52 if ($result['success'] == false) {
53 // Possibly it is a remote item and not an account
54 follow_remote_item($url);
56 if ($result['message']) {
57 notice($result['message']);
59 DI::baseUrl()->redirect($return_path);
60 } elseif ($result['cid']) {
61 DI::baseUrl()->redirect('contact/' . $result['cid']);
64 info(DI::l10n()->t('The contact could not be added.'));
66 DI::baseUrl()->redirect($return_path);
70 function follow_content(App $a)
72 $return_path = 'contact';
75 notice(DI::l10n()->t('Permission denied.'));
76 DI::baseUrl()->redirect($return_path);
82 // Issue 4815: Silently removing a prefixing @
83 $url = ltrim(Strings::escapeTags(trim($_REQUEST['url'] ?? '')), '@!');
85 // Issue 6874: Allow remote following from Peertube
86 if (strpos($url, 'acct:') === 0) {
87 $url = str_replace('acct:', '', $url);
91 DI::baseUrl()->redirect($return_path);
94 $submit = DI::l10n()->t('Submit Request');
96 // Don't try to add a pending contact
97 $r = q("SELECT `pending` FROM `contact` WHERE `uid` = %d AND ((`rel` != %d) OR (`network` = '%s')) AND
98 (`nurl` = '%s' OR `alias` = '%s' OR `alias` = '%s') AND
99 `network` != '%s' LIMIT 1",
100 intval(local_user()), DBA::escape(Contact::FOLLOWER), DBA::escape(Protocol::DFRN), DBA::escape(Strings::normaliseLink($url)),
101 DBA::escape(Strings::normaliseLink($url)), DBA::escape($url), DBA::escape(Protocol::STATUSNET));
104 if ($r[0]['pending']) {
105 notice(DI::l10n()->t('You already added this contact.'));
107 //$a->internalRedirect($_SESSION['return_path']);
112 $ret = Probe::uri($url);
114 $protocol = Contact::getProtocol($ret['url'], $ret['network']);
116 if (($protocol == Protocol::DIASPORA) && !DI::config()->get('system', 'diaspora_enabled')) {
117 notice(DI::l10n()->t("Diaspora support isn't enabled. Contact can't be added."));
119 //$a->internalRedirect($_SESSION['return_path']);
123 if (($protocol == Protocol::OSTATUS) && DI::config()->get('system', 'ostatus_disabled')) {
124 notice(DI::l10n()->t("OStatus support is disabled. Contact can't be added."));
126 //$a->internalRedirect($_SESSION['return_path']);
130 if ($protocol == Protocol::PHANTOM) {
131 // Possibly it is a remote item and not an account
132 follow_remote_item($url);
134 notice(DI::l10n()->t("The network type couldn't be detected. Contact can't be added."));
136 //$a->internalRedirect($_SESSION['return_path']);
140 if ($protocol == Protocol::MAIL) {
141 $ret['url'] = $ret['addr'];
144 if (($protocol === Protocol::DFRN) && !DBA::isResult($r)) {
145 $request = $ret['request'];
146 $tpl = Renderer::getMarkupTemplate('dfrn_request.tpl');
148 $request = DI::baseUrl() . '/follow';
149 $tpl = Renderer::getMarkupTemplate('auto_request.tpl');
152 $r = q("SELECT `url` FROM `contact` WHERE `uid` = %d AND `self` LIMIT 1", intval($uid));
155 notice(DI::l10n()->t('Permission denied.'));
156 DI::baseUrl()->redirect($return_path);
160 $myaddr = $r[0]['url'];
163 // Makes the connection request for friendica contacts easier
164 $_SESSION['fastlane'] = $ret['url'];
166 $r = q("SELECT `id`, `location`, `about`, `keywords` FROM `gcontact` WHERE `nurl` = '%s'",
167 Strings::normaliseLink($ret['url']));
170 $r = [['location' => '', 'about' => '', 'keywords' => '']];
172 $gcontact_id = $r[0]['id'];
175 if ($protocol === Protocol::DIASPORA) {
176 $r[0]['location'] = '';
180 $o = Renderer::replaceMacros($tpl, [
181 '$header' => DI::l10n()->t('Connect/Follow'),
182 '$pls_answer' => DI::l10n()->t('Please answer the following:'),
183 '$your_address' => DI::l10n()->t('Your Identity Address:'),
184 '$url_label' => DI::l10n()->t('Profile URL'),
185 '$keywords_label'=> DI::l10n()->t('Tags:'),
186 '$submit' => $submit,
187 '$cancel' => DI::l10n()->t('Cancel'),
189 '$request' => $request,
190 '$name' => $ret['name'],
191 '$url' => $ret['url'],
192 '$zrl' => Profile::zrl($ret['url']),
193 '$myaddr' => $myaddr,
194 '$keywords' => $r[0]['keywords'],
196 '$does_know_you' => ['knowyou', DI::l10n()->t('%s knows you', $ret['name'])],
197 '$addnote_field' => ['dfrn-request-message', DI::l10n()->t('Add a personal note:')],
200 DI::page()['aside'] = '';
202 $profiledata = Contact::getDetailsByURL($ret['url']);
204 Profile::load($a, '', $profiledata, false);
207 if ($gcontact_id <> 0) {
208 $o .= Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'),
209 ['$title' => DI::l10n()->t('Status Messages and Posts')]
212 // Show last public posts
213 $o .= Contact::getPostsFromUrl($ret['url']);
219 function follow_remote_item($url)
221 $item_id = Item::fetchByLink($url, local_user());
223 // If the user-specific search failed, we search and probe a public post
224 $item_id = Item::fetchByLink($url);
227 if (!empty($item_id)) {
228 $item = Item::selectFirst(['guid'], ['id' => $item_id]);
229 if (DBA::isResult($item)) {
230 DI::baseUrl()->redirect('display/' . $item['guid']);