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