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