3 * @copyright Copyright (C) 2010-2023, 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/>.
22 namespace Friendica\Module\Contact;
25 use Friendica\BaseModule;
26 use Friendica\Content\Widget\VCard;
27 use Friendica\Core\Config\Capability\IManageConfigValues;
28 use Friendica\Core\L10n;
29 use Friendica\Core\Protocol;
30 use Friendica\Core\Renderer;
31 use Friendica\Core\Session\Capability\IHandleUserSessions;
32 use Friendica\Model\Contact;
33 use Friendica\Model\Item;
34 use Friendica\Model\Post;
35 use Friendica\Model\Profile;
36 use Friendica\Model\User;
37 use Friendica\Module\Response;
38 use Friendica\Navigation\SystemMessages;
39 use Friendica\Network\HTTPException\ForbiddenException;
40 use Friendica\Network\Probe;
41 use Friendica\Util\Profiler;
42 use Friendica\Util\Strings;
43 use Psr\Log\LoggerInterface;
45 class Follow extends BaseModule
47 /** @var IHandleUserSessions */
49 /** @var SystemMessages */
50 protected $sysMessages;
51 /** @var IManageConfigValues */
56 public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IHandleUserSessions $session, SystemMessages $sysMessages, IManageConfigValues $config, App\Page $page, array $server, array $parameters = [])
58 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
60 $this->session = $session;
61 $this->sysMessages = $sysMessages;
62 $this->config = $config;
66 protected function post(array $request = [])
68 if (!$this->session->getLocalUserId()) {
69 throw new ForbiddenException($this->t('Access denied.'));
72 if (isset($request['cancel']) || empty($request['url'])) {
73 $this->baseUrl->redirect('contact');
76 $url = Probe::cleanURI($request['url']);
81 protected function content(array $request = []): string
83 $returnPath = 'contact';
85 if (!$this->session->getLocalUserId()) {
86 $this->sysMessages->addNotice($this->t('Permission denied.'));
87 $this->baseUrl->redirect($returnPath);
90 $uid = $this->session->getLocalUserId();
92 // uri is used by the /authorize_interaction Mastodon route
93 $url = Probe::cleanURI(trim($request['uri'] ?? $request['url'] ?? ''));
95 // Issue 6874: Allow remote following from Peertube
96 if (strpos($url, 'acct:') === 0) {
97 $url = str_replace('acct:', '', $url);
101 $this->baseUrl->redirect($returnPath);
104 $submit = $this->t('Submit Request');
106 // Don't try to add a pending contact
107 $userContact = Contact::selectFirst(['pending'], [
108 "`uid` = ? AND ((`rel` != ?) OR (`network` = ?)) AND (`nurl` = ? OR `alias` = ? OR `alias` = ?)",
109 $uid, Contact::FOLLOWER, Protocol::DFRN,
110 Strings::normaliseLink($url),
111 Strings::normaliseLink($url), $url]);
113 if (!empty($userContact['pending'])) {
114 $this->sysMessages->addNotice($this->t('You already added this contact.'));
118 $contact = Contact::getByURL($url, true);
120 // Possibly it is a mail contact
121 if (empty($contact)) {
122 $contact = Probe::uri($url, Protocol::MAIL, $uid);
125 if (empty($contact) || ($contact['network'] == Protocol::PHANTOM)) {
126 // Possibly it is a remote item and not an account
127 $this->followRemoteItem($url);
129 $this->sysMessages->addNotice($this->t('The network type couldn\'t be detected. Contact can\'t be added.'));
131 $contact = ['url' => $url, 'network' => Protocol::PHANTOM, 'name' => $url, 'keywords' => ''];
134 $protocol = Contact::getProtocol($contact['url'], $contact['network']);
136 if (($protocol == Protocol::DIASPORA) && !$this->config->get('system', 'diaspora_enabled')) {
137 $this->sysMessages->addNotice($this->t('Diaspora support isn\'t enabled. Contact can\'t be added.'));
141 if (($protocol == Protocol::OSTATUS) && $this->config->get('system', 'ostatus_disabled')) {
142 $this->sysMessages->addNotice($this->t("OStatus support is disabled. Contact can't be added."));
146 if ($protocol == Protocol::MAIL) {
147 $contact['url'] = $contact['addr'];
150 if (!empty($request['auto'])) {
151 $this->process($contact['url']);
154 $requestUrl = $this->baseUrl . '/contact/follow';
155 $tpl = Renderer::getMarkupTemplate('auto_request.tpl');
157 $owner = User::getOwnerDataById($uid);
159 $this->sysMessages->addNotice($this->t('Permission denied.'));
160 $this->baseUrl->redirect($returnPath);
163 $myaddr = $owner['url'];
165 $output = Renderer::replaceMacros($tpl, [
166 '$header' => $this->t('Connect/Follow'),
167 '$pls_answer' => $this->t('Please answer the following:'),
168 '$your_address' => $this->t('Your Identity Address:'),
169 '$url_label' => $this->t('Profile URL'),
170 '$keywords_label' => $this->t('Tags:'),
171 '$submit' => $submit,
172 '$cancel' => $this->t('Cancel'),
174 '$action' => $requestUrl,
175 '$name' => $contact['name'],
176 '$url' => $contact['url'],
177 '$zrl' => Profile::zrl($contact['url']),
178 '$myaddr' => $myaddr,
179 '$keywords' => $contact['keywords'],
181 '$does_know_you' => ['knowyou', $this->t('%s knows you', $contact['name'])],
182 '$addnote_field' => ['dfrn-request-message', $this->t('Add a personal note:')],
185 $this->page['aside'] = '';
187 if (!in_array($protocol, [Protocol::PHANTOM, Protocol::MAIL])) {
188 $this->page['aside'] = VCard::getHTML($contact);
190 $output .= Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'),
191 ['$title' => $this->t('Posts and Replies')]
194 // Show last public posts
195 $output .= Contact::getPostsFromUrl($contact['url']);
201 protected function process(string $url)
203 $returnPath = 'contact/follow?url=' . urlencode($url);
205 $result = Contact::createFromProbeForUser($this->session->getLocalUserId(), $url);
207 if (!$result['success']) {
208 // Possibly it is a remote item and not an account
209 $this->followRemoteItem($url);
211 if (!empty($result['message'])) {
212 $this->sysMessages->addNotice($result['message']);
215 $this->baseUrl->redirect($returnPath);
216 } elseif (!empty($result['cid'])) {
217 $this->baseUrl->redirect('contact/' . $result['cid']);
220 $this->sysMessages->addNotice($this->t('The contact could not be added.'));
221 $this->baseUrl->redirect($returnPath);
224 protected function followRemoteItem(string $url)
226 $itemId = Item::fetchByLink($url, $this->session->getLocalUserId());
228 // If the user-specific search failed, we search and probe a public post
229 $itemId = Item::fetchByLink($url);
232 if (!empty($itemId)) {
233 $item = Post::selectFirst(['guid'], ['id' => $itemId]);
234 if (!empty($item['guid'])) {
235 $this->baseUrl->redirect('display/' . $item['guid']);