]> git.mxchange.org Git - friendica.git/blob - src/Module/Contact/Follow.php
f8b88c05fc7ab35686ede286500d988a34748736
[friendica.git] / src / Module / Contact / Follow.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Module\Contact;
23
24 use Friendica\App;
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;
44
45 class Follow extends BaseModule
46 {
47         /** @var IHandleUserSessions */
48         protected $session;
49         /** @var SystemMessages */
50         protected $sysMessages;
51         /** @var IManageConfigValues */
52         protected $config;
53         /** @var App\Page */
54         protected $page;
55
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 = [])
57         {
58                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
59
60                 $this->session     = $session;
61                 $this->sysMessages = $sysMessages;
62                 $this->config      = $config;
63                 $this->page        = $page;
64         }
65
66         protected function post(array $request = [])
67         {
68                 if (!$this->session->getLocalUserId()) {
69                         throw new ForbiddenException($this->t('Access denied.'));
70                 }
71
72                 if (isset($request['cancel']) || empty($request['url'])) {
73                         $this->baseUrl->redirect('contact');
74                 }
75
76                 $url = Probe::cleanURI($request['url']);
77
78                 $this->process($url);
79         }
80
81         protected function content(array $request = []): string
82         {
83                 $returnPath = 'contact';
84
85                 if (!$this->session->getLocalUserId()) {
86                         $this->sysMessages->addNotice($this->t('Permission denied.'));
87                         $this->baseUrl->redirect($returnPath);
88                 }
89
90                 $uid = $this->session->getLocalUserId();
91
92                 // uri is used by the /authorize_interaction Mastodon route
93                 $url = Probe::cleanURI(trim($request['uri'] ?? $request['url'] ?? ''));
94
95                 // Issue 6874: Allow remote following from Peertube
96                 if (strpos($url, 'acct:') === 0) {
97                         $url = str_replace('acct:', '', $url);
98                 }
99
100                 if (empty($url)) {
101                         $this->baseUrl->redirect($returnPath);
102                 }
103
104                 $submit = $this->t('Submit Request');
105
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` = ?) AND `network` != ?",
109                         $uid, Contact::FOLLOWER, Protocol::DFRN,
110                         Strings::normaliseLink($url),
111                         Strings::normaliseLink($url), $url,
112                         Protocol::STATUSNET]);
113
114                 if (!empty($userContact['pending'])) {
115                         $this->sysMessages->addNotice($this->t('You already added this contact.'));
116                         $submit = '';
117                 }
118
119                 $contact = Contact::getByURL($url, true);
120
121                 // Possibly it is a mail contact
122                 if (empty($contact)) {
123                         $contact = Probe::uri($url, Protocol::MAIL, $uid);
124                 }
125
126                 if (empty($contact) || ($contact['network'] == Protocol::PHANTOM)) {
127                         // Possibly it is a remote item and not an account
128                         $this->followRemoteItem($url);
129
130                         $this->sysMessages->addNotice($this->t('The network type couldn\'t be detected. Contact can\'t be added.'));
131                         $submit  = '';
132                         $contact = ['url' => $url, 'network' => Protocol::PHANTOM, 'name' => $url, 'keywords' => ''];
133                 }
134
135                 $protocol = Contact::getProtocol($contact['url'], $contact['network']);
136
137                 if (($protocol == Protocol::DIASPORA) && !$this->config->get('system', 'diaspora_enabled')) {
138                         $this->sysMessages->addNotice($this->t('Diaspora support isn\'t enabled. Contact can\'t be added.'));
139                         $submit = '';
140                 }
141
142                 if (($protocol == Protocol::OSTATUS) && $this->config->get('system', 'ostatus_disabled')) {
143                         $this->sysMessages->addNotice($this->t("OStatus support is disabled. Contact can't be added."));
144                         $submit = '';
145                 }
146
147                 if ($protocol == Protocol::MAIL) {
148                         $contact['url'] = $contact['addr'];
149                 }
150
151                 if (!empty($request['auto'])) {
152                         $this->process($contact['url']);
153                 }
154
155                 $requestUrl = $this->baseUrl . '/contact/follow';
156                 $tpl        = Renderer::getMarkupTemplate('auto_request.tpl');
157
158                 $owner = User::getOwnerDataById($uid);
159                 if (empty($owner)) {
160                         $this->sysMessages->addNotice($this->t('Permission denied.'));
161                         $this->baseUrl->redirect($returnPath);
162                 }
163
164                 $myaddr = $owner['url'];
165
166                 $output = Renderer::replaceMacros($tpl, [
167                         '$header'         => $this->t('Connect/Follow'),
168                         '$pls_answer'     => $this->t('Please answer the following:'),
169                         '$your_address'   => $this->t('Your Identity Address:'),
170                         '$url_label'      => $this->t('Profile URL'),
171                         '$keywords_label' => $this->t('Tags:'),
172                         '$submit'         => $submit,
173                         '$cancel'         => $this->t('Cancel'),
174
175                         '$action'   => $requestUrl,
176                         '$name'     => $contact['name'],
177                         '$url'      => $contact['url'],
178                         '$zrl'      => Profile::zrl($contact['url']),
179                         '$myaddr'   => $myaddr,
180                         '$keywords' => $contact['keywords'],
181
182                         '$does_know_you' => ['knowyou', $this->t('%s knows you', $contact['name'])],
183                         '$addnote_field' => ['dfrn-request-message', $this->t('Add a personal note:')],
184                 ]);
185
186                 $this->page['aside'] = '';
187
188                 if (!in_array($protocol, [Protocol::PHANTOM, Protocol::MAIL])) {
189                         $this->page['aside'] = VCard::getHTML($contact);
190
191                         $output .= Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'),
192                                 ['$title' => $this->t('Status Messages and Posts')]
193                         );
194
195                         // Show last public posts
196                         $output .= Contact::getPostsFromUrl($contact['url']);
197                 }
198
199                 return $output;
200         }
201
202         protected function process(string $url)
203         {
204                 $returnPath = 'contact/follow?url=' . urlencode($url);
205
206                 $result = Contact::createFromProbeForUser($this->session->getLocalUserId(), $url);
207
208                 if (!$result['success']) {
209                         // Possibly it is a remote item and not an account
210                         $this->followRemoteItem($url);
211
212                         if (!empty($result['message'])) {
213                                 $this->sysMessages->addNotice($result['message']);
214                         }
215
216                         $this->baseUrl->redirect($returnPath);
217                 } elseif (!empty($result['cid'])) {
218                         $this->baseUrl->redirect('contact/' . $result['cid']);
219                 }
220
221                 $this->sysMessages->addNotice($this->t('The contact could not be added.'));
222                 $this->baseUrl->redirect($returnPath);
223         }
224
225         protected function followRemoteItem(string $url)
226         {
227                 $itemId = Item::fetchByLink($url, $this->session->getLocalUserId());
228                 if (!$itemId) {
229                         // If the user-specific search failed, we search and probe a public post
230                         $itemId = Item::fetchByLink($url);
231                 }
232
233                 if (!empty($itemId)) {
234                         $item = Post::selectFirst(['guid'], ['id' => $itemId]);
235                         if (!empty($item['guid'])) {
236                                 $this->baseUrl->redirect('display/' . $item['guid']);
237                         }
238                 }
239         }
240 }