Merge pull request #9766 from annando/conversation-direction
[friendica.git/.git] / mod / follow.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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 use Friendica\App;
23 use Friendica\Core\Protocol;
24 use Friendica\Core\Renderer;
25 use Friendica\DI;
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\Model\User;
32 use Friendica\Util\Strings;
33
34 function follow_post(App $a)
35 {
36         if (!local_user()) {
37                 throw new \Friendica\Network\HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
38         }
39
40         if (isset($_REQUEST['cancel'])) {
41                 DI::baseUrl()->redirect('contact');
42         }
43
44         $url = Probe::cleanURI($_REQUEST['url']);
45
46         follow_process($a, $url);
47 }
48
49 function follow_content(App $a)
50 {
51         $return_path = 'contact';
52
53         if (!local_user()) {
54                 notice(DI::l10n()->t('Permission denied.'));
55                 DI::baseUrl()->redirect($return_path);
56                 // NOTREACHED
57         }
58
59         $uid = local_user();
60
61         // Issue 4815: Silently removing a prefixing @
62         $url = ltrim(Strings::escapeTags(trim($_REQUEST['url'] ?? '')), '@!');
63
64         // Issue 6874: Allow remote following from Peertube
65         if (strpos($url, 'acct:') === 0) {
66                 $url = str_replace('acct:', '', $url);
67         }
68
69         if (!$url) {
70                 DI::baseUrl()->redirect($return_path);
71         }
72
73         $submit = DI::l10n()->t('Submit Request');
74
75         // Don't try to add a pending contact
76         $user_contact = DBA::selectFirst('contact', ['pending'], ["`uid` = ? AND ((`rel` != ?) OR (`network` = ?)) AND
77                 (`nurl` = ? OR `alias` = ? OR `alias` = ?) AND `network` != ?", 
78                 $uid, Contact::FOLLOWER, Protocol::DFRN, Strings::normaliseLink($url),
79                 Strings::normaliseLink($url), $url, Protocol::STATUSNET]);
80
81         if (DBA::isResult($user_contact)) {
82                 if ($user_contact['pending']) {
83                         notice(DI::l10n()->t('You already added this contact.'));
84                         $submit = '';
85                 }
86         }
87
88         $contact = Contact::getByURL($url, true);
89
90         // Possibly it is a mail contact
91         if (empty($contact)) {
92                 $contact = Probe::uri($url, Protocol::MAIL, $uid);
93         }
94
95         if (empty($contact) || ($contact['network'] == Protocol::PHANTOM)) {
96                 // Possibly it is a remote item and not an account
97                 follow_remote_item($url);
98
99                 notice(DI::l10n()->t("The network type couldn't be detected. Contact can't be added."));
100                 $submit = '';
101                 $contact = ['url' => $url, 'network' => Protocol::PHANTOM, 'name' => $url, 'keywords' => ''];
102         }
103
104         $protocol = Contact::getProtocol($contact['url'], $contact['network']);
105
106         if (($protocol == Protocol::DIASPORA) && !DI::config()->get('system', 'diaspora_enabled')) {
107                 notice(DI::l10n()->t("Diaspora support isn't enabled. Contact can't be added."));
108                 $submit = '';
109         }
110
111         if (($protocol == Protocol::OSTATUS) && DI::config()->get('system', 'ostatus_disabled')) {
112                 notice(DI::l10n()->t("OStatus support is disabled. Contact can't be added."));
113                 $submit = '';
114         }
115
116         if ($protocol == Protocol::MAIL) {
117                 $contact['url'] = $contact['addr'];
118         }
119
120         if (($protocol === Protocol::DFRN) && !DBA::isResult($contact)) {
121                 $request = $contact['request'];
122                 $tpl = Renderer::getMarkupTemplate('dfrn_request.tpl');
123         } else {
124                 if (!empty($_REQUEST['auto'])) {
125                         follow_process($a, $contact['url']);
126                 }
127         
128                 $request = DI::baseUrl() . '/follow';
129                 $tpl = Renderer::getMarkupTemplate('auto_request.tpl');
130         }
131
132         $owner = User::getOwnerDataById($uid);
133         if (empty($owner)) {
134                 notice(DI::l10n()->t('Permission denied.'));
135                 DI::baseUrl()->redirect($return_path);
136                 // NOTREACHED
137         }
138
139         $myaddr = $owner['url'];
140
141         // Makes the connection request for friendica contacts easier
142         $_SESSION['fastlane'] = $contact['url'];
143
144         $o = Renderer::replaceMacros($tpl, [
145                 '$header'        => DI::l10n()->t('Connect/Follow'),
146                 '$pls_answer'    => DI::l10n()->t('Please answer the following:'),
147                 '$your_address'  => DI::l10n()->t('Your Identity Address:'),
148                 '$url_label'     => DI::l10n()->t('Profile URL'),
149                 '$keywords_label'=> DI::l10n()->t('Tags:'),
150                 '$submit'        => $submit,
151                 '$cancel'        => DI::l10n()->t('Cancel'),
152
153                 '$request'       => $request,
154                 '$name'          => $contact['name'],
155                 '$url'           => $contact['url'],
156                 '$zrl'           => Profile::zrl($contact['url']),
157                 '$myaddr'        => $myaddr,
158                 '$keywords'      => $contact['keywords'],
159
160                 '$does_know_you' => ['knowyou', DI::l10n()->t('%s knows you', $contact['name'])],
161                 '$addnote_field' => ['dfrn-request-message', DI::l10n()->t('Add a personal note:')],
162         ]);
163
164         DI::page()['aside'] = '';
165
166         if ($protocol != Protocol::PHANTOM) {
167                 Profile::load($a, '', $contact, false);
168
169                 $o .= Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'),
170                         ['$title' => DI::l10n()->t('Status Messages and Posts')]
171                 );
172
173                 // Show last public posts
174                 $o .= Contact::getPostsFromUrl($contact['url']);
175         }
176
177         return $o;
178 }
179
180 function follow_process(App $a, string $url)
181 {
182         $return_path = 'follow?url=' . urlencode($url);
183
184         // Makes the connection request for friendica contacts easier
185         // This is just a precaution if maybe this page is called somewhere directly via POST
186         $_SESSION['fastlane'] = $url;
187
188         $result = Contact::createFromProbe($a->user, $url, true);
189
190         if ($result['success'] == false) {
191                 // Possibly it is a remote item and not an account
192                 follow_remote_item($url);
193
194                 if ($result['message']) {
195                         notice($result['message']);
196                 }
197                 DI::baseUrl()->redirect($return_path);
198         } elseif ($result['cid']) {
199                 DI::baseUrl()->redirect('contact/' . $result['cid']);
200         }
201
202         notice(DI::l10n()->t('The contact could not be added.'));
203
204         DI::baseUrl()->redirect($return_path);
205 }
206
207 function follow_remote_item($url)
208 {
209         $item_id = Item::fetchByLink($url, local_user());
210         if (!$item_id) {
211                 // If the user-specific search failed, we search and probe a public post
212                 $item_id = Item::fetchByLink($url);
213         }
214
215         if (!empty($item_id)) {
216                 $item = Item::selectFirst(['guid'], ['id' => $item_id]);
217                 if (DBA::isResult($item)) {
218                         DI::baseUrl()->redirect('display/' . $item['guid']);
219                 }
220         }
221 }