3 * @copyright Copyright (C) 2010-2022, 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;
53 /** @var IManageConfigValues */
58 public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IHandleUserSessions $session, SystemMessages $sysMessages, App $app, IManageConfigValues $config, App\Page $page, array $server, array $parameters = [])
60 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
62 $this->session = $session;
63 $this->sysMessages = $sysMessages;
65 $this->config = $config;
69 protected function post(array $request = [])
71 if (!$this->session->getLocalUserId()) {
72 throw new ForbiddenException($this->t('Access denied.'));
75 if (!empty($request['url'])) {
76 $this->baseUrl->redirect($request['url']);
79 $url = Probe::cleanURI($this->session->get('url'));
84 protected function content(array $request = []): string
86 $returnPath = 'contact';
88 if (!$this->session->getLocalUserId()) {
89 $this->sysMessages->addNotice($this->t('Permission denied.'));
90 $this->baseUrl->redirect($returnPath);
93 $uid = $this->session->getLocalUserId();
94 $url = Probe::cleanURI(trim($request['url'] ?? ''));
96 // Issue 6874: Allow remote following from Peertube
97 if (strpos($url, 'acct:') === 0) {
98 $url = str_replace('acct:', '', $url);
102 $this->baseUrl->redirect($returnPath);
105 $submit = $this->t('Submit Request');
107 // Don't try to add a pending contact
108 $userContact = Contact::selectFirst(['pending'], [
109 "`uid` = ? AND ((`rel` != ?) OR (`network` = ?)) AND (`nurl` = ? OR `alias` = ? OR `alias` = ?) AND `network` != ?",
110 $uid, Contact::FOLLOWER, Protocol::DFRN,
111 Strings::normaliseLink($url),
112 Strings::normaliseLink($url), $url,
113 Protocol::STATUSNET]);
115 if (!empty($userContact['pending'])) {
116 $this->sysMessages->addNotice($this->t('You already added this contact.'));
120 $contact = Contact::getByURL($url, true);
122 // Possibly it is a mail contact
123 if (empty($contact)) {
124 $contact = Probe::uri($url, Protocol::MAIL, $uid);
127 if (empty($contact) || ($contact['network'] == Protocol::PHANTOM)) {
128 // Possibly it is a remote item and not an account
129 $this->followRemoteItem($url);
131 $this->sysMessages->addNotice($this->t('The network type couldn\'t be detected. Contact can\'t be added.'));
133 $contact = ['url' => $url, 'network' => Protocol::PHANTOM, 'name' => $url, 'keywords' => ''];
136 $protocol = Contact::getProtocol($contact['url'], $contact['network']);
138 if (($protocol == Protocol::DIASPORA) && !$this->config->get('system', 'diaspora_enabled')) {
139 $this->sysMessages->addNotice($this->t('Diaspora support isn\'t enabled. Contact can\'t be added.'));
143 if (($protocol == Protocol::OSTATUS) && $this->config->get('system', 'ostatus_disabled')) {
144 $this->sysMessages->addNotice($this->t("OStatus support is disabled. Contact can't be added."));
148 if ($protocol == Protocol::MAIL) {
149 $contact['url'] = $contact['addr'];
152 if (!empty($request['auto'])) {
153 $this->process($contact['url']);
156 $request = $this->baseUrl . '/follow';
157 $tpl = Renderer::getMarkupTemplate('auto_request.tpl');
159 $owner = User::getOwnerDataById($uid);
161 $this->sysMessages->addNotice($this->t('Permission denied.'));
162 $this->baseUrl->redirect($returnPath);
165 $myaddr = $owner['url'];
167 $output = Renderer::replaceMacros($tpl, [
168 '$header' => $this->t('Connect/Follow'),
169 '$pls_answer' => $this->t('Please answer the following:'),
170 '$your_address' => $this->t('Your Identity Address:'),
171 '$url_label' => $this->t('Profile URL'),
172 '$keywords_label' => $this->t('Tags:'),
173 '$submit' => $submit,
174 '$cancel' => $this->t('Cancel'),
176 '$request' => $request,
177 '$name' => $contact['name'],
178 '$url' => $contact['url'],
179 '$zrl' => Profile::zrl($contact['url']),
180 '$myaddr' => $myaddr,
181 '$keywords' => $contact['keywords'],
183 '$does_know_you' => ['knowyou', $this->t('%s knows you', $contact['name'])],
184 '$addnote_field' => ['dfrn-request-message', $this->t('Add a personal note:')],
189 if (!in_array($protocol, [Protocol::PHANTOM, Protocol::MAIL])) {
190 $this['aside'] = VCard::getHTML($contact);
192 $output .= Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'),
193 ['$title' => $this->t('Status Messages and Posts')]
196 // Show last public posts
197 $output .= Contact::getPostsFromUrl($contact['url']);
203 protected function process(string $url)
205 $returnPath = 'follow?rul=' . urlencode($url);
207 $result = Contact::createFromProbeForUser($this->app->getLoggedInUserId(), $url);
209 if (!$result['success']) {
210 // Possibly it is a remote item and not an account
211 $this->followRemoteItem($url);
213 if (!empty($result['message'])) {
214 $this->sysMessages->addNotice($result['message']);
217 $this->baseUrl->redirect($returnPath);
218 } elseif (!empty($result['cid'])) {
219 $this->baseUrl->redirect('contact/' . $result['cid']);
222 $this->sysMessages->addNotice($this->t('The contact could not be added.'));
223 $this->baseUrl->redirect($returnPath);
226 protected function followRemoteItem(string $url)
228 $itemId = Item::fetchByLink($url, $this->session->getLocalUserId());
230 // If the user-specific search failed, we search and probe a public post
231 $itemId = Item::fetchByLink($url);
234 if (!empty($itemId)) {
235 $item = Post::selectFirst(['guid'], ['id' => $itemId]);
236 if (!empty($item['guid'])) {
237 $this->baseUrl->redirect('display/' . $item['guid']);