]> git.mxchange.org Git - friendica.git/blob - mod/follow.php
Merge pull request #8272 from MrPetovan/bug/8254-regex-url-img
[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\Util\Strings;
32
33 function follow_post(App $a)
34 {
35         if (!local_user()) {
36                 throw new \Friendica\Network\HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
37         }
38
39         if (isset($_REQUEST['cancel'])) {
40                 DI::baseUrl()->redirect('contact');
41         }
42
43         $uid = local_user();
44         $url = Strings::escapeTags(trim($_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($uid, $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         info(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         $r = q("SELECT `pending` FROM `contact` WHERE `uid` = %d AND ((`rel` != %d) OR (`network` = '%s')) AND
99                 (`nurl` = '%s' OR `alias` = '%s' OR `alias` = '%s') AND
100                 `network` != '%s' LIMIT 1",
101                 intval(local_user()), DBA::escape(Contact::FOLLOWER), DBA::escape(Protocol::DFRN), DBA::escape(Strings::normaliseLink($url)),
102                 DBA::escape(Strings::normaliseLink($url)), DBA::escape($url), DBA::escape(Protocol::STATUSNET));
103
104         if ($r) {
105                 if ($r[0]['pending']) {
106                         notice(DI::l10n()->t('You already added this contact.'));
107                         $submit = '';
108                         //$a->internalRedirect($_SESSION['return_path']);
109                         // NOTREACHED
110                 }
111         }
112
113         $ret = Probe::uri($url);
114
115         $protocol = Contact::getProtocol($ret['url'], $ret['network']);
116
117         if (($protocol == Protocol::DIASPORA) && !DI::config()->get('system', 'diaspora_enabled')) {
118                 notice(DI::l10n()->t("Diaspora support isn't enabled. Contact can't be added."));
119                 $submit = '';
120                 //$a->internalRedirect($_SESSION['return_path']);
121                 // NOTREACHED
122         }
123
124         if (($protocol == Protocol::OSTATUS) && DI::config()->get('system', 'ostatus_disabled')) {
125                 notice(DI::l10n()->t("OStatus support is disabled. Contact can't be added."));
126                 $submit = '';
127                 //$a->internalRedirect($_SESSION['return_path']);
128                 // NOTREACHED
129         }
130
131         if ($protocol == Protocol::PHANTOM) {
132                 // Possibly it is a remote item and not an account
133                 follow_remote_item($url);
134
135                 notice(DI::l10n()->t("The network type couldn't be detected. Contact can't be added."));
136                 $submit = '';
137                 //$a->internalRedirect($_SESSION['return_path']);
138                 // NOTREACHED
139         }
140
141         if ($protocol == Protocol::MAIL) {
142                 $ret['url'] = $ret['addr'];
143         }
144
145         if (($protocol === Protocol::DFRN) && !DBA::isResult($r)) {
146                 $request = $ret['request'];
147                 $tpl = Renderer::getMarkupTemplate('dfrn_request.tpl');
148         } else {
149                 $request = DI::baseUrl() . '/follow';
150                 $tpl = Renderer::getMarkupTemplate('auto_request.tpl');
151         }
152
153         $r = q("SELECT `url` FROM `contact` WHERE `uid` = %d AND `self` LIMIT 1", intval($uid));
154
155         if (!$r) {
156                 notice(DI::l10n()->t('Permission denied.'));
157                 DI::baseUrl()->redirect($return_path);
158                 // NOTREACHED
159         }
160
161         $myaddr = $r[0]['url'];
162         $gcontact_id = 0;
163
164         // Makes the connection request for friendica contacts easier
165         $_SESSION['fastlane'] = $ret['url'];
166
167         $r = q("SELECT `id`, `location`, `about`, `keywords` FROM `gcontact` WHERE `nurl` = '%s'",
168                 Strings::normaliseLink($ret['url']));
169
170         if (!$r) {
171                 $r = [['location' => '', 'about' => '', 'keywords' => '']];
172         } else {
173                 $gcontact_id = $r[0]['id'];
174         }
175
176         if ($protocol === Protocol::DIASPORA) {
177                 $r[0]['location'] = '';
178                 $r[0]['about'] = '';
179         }
180
181         $o = Renderer::replaceMacros($tpl, [
182                 '$header'        => DI::l10n()->t('Connect/Follow'),
183                 '$pls_answer'    => DI::l10n()->t('Please answer the following:'),
184                 '$your_address'  => DI::l10n()->t('Your Identity Address:'),
185                 '$url_label'     => DI::l10n()->t('Profile URL'),
186                 '$keywords_label'=> DI::l10n()->t('Tags:'),
187                 '$submit'        => $submit,
188                 '$cancel'        => DI::l10n()->t('Cancel'),
189
190                 '$request'       => $request,
191                 '$name'          => $ret['name'],
192                 '$url'           => $ret['url'],
193                 '$zrl'           => Profile::zrl($ret['url']),
194                 '$myaddr'        => $myaddr,
195                 '$keywords'      => $r[0]['keywords'],
196
197                 '$does_know_you' => ['knowyou', DI::l10n()->t('%s knows you', $ret['name'])],
198                 '$addnote_field' => ['dfrn-request-message', DI::l10n()->t('Add a personal note:')],
199         ]);
200
201         DI::page()['aside'] = '';
202
203         $profiledata = Contact::getDetailsByURL($ret['url']);
204         if ($profiledata) {
205                 Profile::load($a, '', $profiledata, false);
206         }
207
208         if ($gcontact_id <> 0) {
209                 $o .= Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'),
210                         ['$title' => DI::l10n()->t('Status Messages and Posts')]
211                 );
212
213                 // Show last public posts
214                 $o .= Contact::getPostsFromUrl($ret['url']);
215         }
216
217         return $o;
218 }
219
220 function follow_remote_item($url)
221 {
222         $item_id = Item::fetchByLink($url, local_user());
223         if (!$item_id) {
224                 // If the user-specific search failed, we search and probe a public post
225                 $item_id = Item::fetchByLink($url);
226         }
227
228         if (!empty($item_id)) {
229                 $item = Item::selectFirst(['guid'], ['id' => $item_id]);
230                 if (DBA::isResult($item)) {
231                         DI::baseUrl()->redirect('display/' . $item['guid']);
232                 }
233         }
234 }