]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_request.php
Merge remote-tracking branch 'upstream/develop' into item-notification
[friendica.git] / mod / dfrn_request.php
1 <?php
2
3 /**
4  * @file mod/dfrn_request.php
5  * @brief Module: dfrn_request
6  *
7  * Purpose: Handles communication associated with the issuance of
8  * friend requests.
9  *
10  * @see PDF with dfrn specs: https://github.com/friendica/friendica/blob/master/spec/dfrn2.pdf
11  *    You also find a graphic which describes the confirmation process at
12  *    https://github.com/friendica/friendica/blob/master/spec/dfrn2_contact_request.png
13  */
14
15 use Friendica\App;
16 use Friendica\Core\Config;
17 use Friendica\Core\L10n;
18 use Friendica\Core\Logger;
19 use Friendica\Core\Protocol;
20 use Friendica\Core\Renderer;
21 use Friendica\Core\Search;
22 use Friendica\Core\System;
23 use Friendica\Core\Session;
24 use Friendica\Database\DBA;
25 use Friendica\DI;
26 use Friendica\Model\Contact;
27 use Friendica\Model\Group;
28 use Friendica\Model\Profile;
29 use Friendica\Model\User;
30 use Friendica\Module\Security\Login;
31 use Friendica\Network\Probe;
32 use Friendica\Protocol\Activity;
33 use Friendica\Util\DateTimeFormat;
34 use Friendica\Util\Network;
35 use Friendica\Util\Strings;
36
37 function dfrn_request_init(App $a)
38 {
39         if ($a->argc > 1) {
40                 $which = $a->argv[1];
41                 Profile::load($a, $which);
42         }
43
44         return;
45 }
46
47 /**
48  * Function: dfrn_request_post
49  *
50  * Purpose:
51  * Handles multiple scenarios.
52  *
53  * Scenario 1:
54  * Clicking 'submit' on a friend request page.
55  *
56  * Scenario 2:
57  * Following Scenario 1, we are brought back to our home site
58  * in order to link our friend request with our own server cell.
59  * After logging in, we click 'submit' to approve the linkage.
60  *
61  * @param App $a
62  * @throws ImagickException
63  * @throws \Friendica\Network\HTTPException\InternalServerErrorException
64  */
65 function dfrn_request_post(App $a)
66 {
67         if (($a->argc != 2) || (!count($a->profile))) {
68                 Logger::log('Wrong count of argc or profiles: argc=' . $a->argc . ',profile()=' . count($a->profile));
69                 return;
70         }
71
72         if (!empty($_POST['cancel'])) {
73                 DI::baseUrl()->redirect();
74         }
75
76         /*
77          * Scenario 2: We've introduced ourself to another cell, then have been returned to our own cell
78          * to confirm the request, and then we've clicked submit (perhaps after logging in).
79          * That brings us here:
80          */
81         if (!empty($_POST['localconfirm']) && ($_POST['localconfirm'] == 1)) {
82                 // Ensure this is a valid request
83                 if (local_user() && ($a->user['nickname'] == $a->argv[1]) && !empty($_POST['dfrn_url'])) {
84                         $dfrn_url    = Strings::escapeTags(trim($_POST['dfrn_url']));
85                         $aes_allow   = !empty($_POST['aes_allow']);
86                         $confirm_key = $_POST['confirm_key'] ?? '';
87                         $hidden      = (!empty($_POST['hidden-contact']) ? intval($_POST['hidden-contact']) : 0);
88                         $contact_record = null;
89                         $blocked     = 1;
90                         $pending     = 1;
91
92                         if (!empty($dfrn_url)) {
93                                 // Lookup the contact based on their URL (which is the only unique thing we have at the moment)
94                                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND NOT `self` LIMIT 1",
95                                         intval(local_user()),
96                                         DBA::escape(Strings::normaliseLink($dfrn_url))
97                                 );
98
99                                 if (DBA::isResult($r)) {
100                                         if (strlen($r[0]['dfrn-id'])) {
101                                                 // We don't need to be here. It has already happened.
102                                                 notice(L10n::t("This introduction has already been accepted.") . EOL);
103                                                 return;
104                                         } else {
105                                                 $contact_record = $r[0];
106                                         }
107                                 }
108
109                                 if (is_array($contact_record)) {
110                                         $r = q("UPDATE `contact` SET `ret-aes` = %d, hidden = %d WHERE `id` = %d",
111                                                 intval($aes_allow),
112                                                 intval($hidden),
113                                                 intval($contact_record['id'])
114                                         );
115                                 } else {
116                                         // Scrape the other site's profile page to pick up the dfrn links, key, fn, and photo
117                                         $parms = Probe::profile($dfrn_url);
118
119                                         if (!count($parms)) {
120                                                 notice(L10n::t('Profile location is not valid or does not contain profile information.') . EOL);
121                                                 return;
122                                         } else {
123                                                 if (empty($parms['fn'])) {
124                                                         notice(L10n::t('Warning: profile location has no identifiable owner name.') . EOL);
125                                                 }
126                                                 if (empty($parms['photo'])) {
127                                                         notice(L10n::t('Warning: profile location has no profile photo.') . EOL);
128                                                 }
129                                                 $invalid = Probe::validDfrn($parms);
130                                                 if ($invalid) {
131                                                         notice(L10n::tt("%d required parameter was not found at the given location", "%d required parameters were not found at the given location", $invalid) . EOL);
132                                                         return;
133                                                 }
134                                         }
135
136                                         $dfrn_request = $parms['dfrn-request'];
137
138                                         $photo = $parms["photo"];
139
140                                         // Escape the entire array
141                                         DBA::escapeArray($parms);
142
143                                         // Create a contact record on our site for the other person
144                                         $r = q("INSERT INTO `contact` ( `uid`, `created`,`url`, `nurl`, `addr`, `name`, `nick`, `photo`, `site-pubkey`,
145                                                 `request`, `confirm`, `notify`, `poll`, `network`, `aes_allow`, `hidden`, `blocked`, `pending`)
146                                                 VALUES ( %d, '%s', '%s', '%s', '%s', '%s' , '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d)",
147                                                 intval(local_user()),
148                                                 DateTimeFormat::utcNow(),
149                                                 DBA::escape($dfrn_url),
150                                                 DBA::escape(Strings::normaliseLink($dfrn_url)),
151                                                 $parms['addr'],
152                                                 $parms['fn'],
153                                                 $parms['nick'],
154                                                 $parms['photo'],
155                                                 $parms['key'],
156                                                 $parms['dfrn-request'],
157                                                 $parms['dfrn-confirm'],
158                                                 $parms['dfrn-notify'],
159                                                 $parms['dfrn-poll'],
160                                                 DBA::escape(Protocol::DFRN),
161                                                 intval($aes_allow),
162                                                 intval($hidden),
163                                                 intval($blocked),
164                                                 intval($pending)
165                                         );
166                                 }
167
168                                 if ($r) {
169                                         info(L10n::t("Introduction complete.") . EOL);
170                                 }
171
172                                 $r = q("SELECT `id`, `network` FROM `contact` WHERE `uid` = %d AND `url` = '%s' AND `site-pubkey` = '%s' LIMIT 1",
173                                         intval(local_user()),
174                                         DBA::escape($dfrn_url),
175                                         $parms['key'] ?? '' // Potentially missing
176                                 );
177                                 if (DBA::isResult($r)) {
178                                         Group::addMember(User::getDefaultGroup(local_user(), $r[0]["network"]), $r[0]['id']);
179
180                                         if (isset($photo)) {
181                                                 Contact::updateAvatar($photo, local_user(), $r[0]["id"], true);
182                                         }
183
184                                         $forward_path = "contact/" . $r[0]['id'];
185                                 } else {
186                                         $forward_path = "contact";
187                                 }
188
189                                 // Allow the blocked remote notification to complete
190                                 if (is_array($contact_record)) {
191                                         $dfrn_request = $contact_record['request'];
192                                 }
193
194                                 if (!empty($dfrn_request) && strlen($confirm_key)) {
195                                         Network::fetchUrl($dfrn_request . '?confirm_key=' . $confirm_key);
196                                 }
197
198                                 // (ignore reply, nothing we can do it failed)
199                                 DI::baseUrl()->redirect($forward_path);
200                                 return; // NOTREACHED
201                         }
202                 }
203
204                 // invalid/bogus request
205                 notice(L10n::t('Unrecoverable protocol error.') . EOL);
206                 DI::baseUrl()->redirect();
207                 return; // NOTREACHED
208         }
209
210         /*
211          * Otherwise:
212          *
213          * Scenario 1:
214          * We are the requestee. A person from a remote cell has made an introduction
215          * on our profile web page and clicked submit. We will use their DFRN-URL to
216          * figure out how to contact their cell.
217          *
218          * Scrape the originating DFRN-URL for everything we need. Create a contact record
219          * and an introduction to show our user next time he/she logs in.
220          * Finally redirect back to the requestor so that their site can record the request.
221          * If our user (the requestee) later confirms this request, a record of it will need
222          * to exist on the requestor's cell in order for the confirmation process to complete..
223          *
224          * It's possible that neither the requestor or the requestee are logged in at the moment,
225          * and the requestor does not yet have any credentials to the requestee profile.
226          *
227          * Who is the requestee? We've already loaded their profile which means their nickname should be
228          * in $a->argv[1] and we should have their complete info in $a->profile.
229          *
230          */
231         if (!(is_array($a->profile) && count($a->profile))) {
232                 notice(L10n::t('Profile unavailable.') . EOL);
233                 return;
234         }
235
236         $nickname       = $a->profile['nickname'];
237         $uid            = $a->profile['uid'];
238         $maxreq         = intval($a->profile['maxreq']);
239         $contact_record = null;
240         $failed         = false;
241         $parms          = null;
242         $blocked = 1;
243         $pending = 1;
244
245         if (!empty($_POST['dfrn_url'])) {
246                 // Block friend request spam
247                 if ($maxreq) {
248                         $r = q("SELECT * FROM `intro` WHERE `datetime` > '%s' AND `uid` = %d",
249                                 DBA::escape(DateTimeFormat::utc('now - 24 hours')),
250                                 intval($uid)
251                         );
252                         if (DBA::isResult($r) && count($r) > $maxreq) {
253                                 notice(L10n::t('%s has received too many connection requests today.', $a->profile['name']) . EOL);
254                                 notice(L10n::t('Spam protection measures have been invoked.') . EOL);
255                                 notice(L10n::t('Friends are advised to please try again in 24 hours.') . EOL);
256                                 return;
257                         }
258                 }
259
260                 /* Cleanup old introductions that remain blocked.
261                  * Also remove the contact record, but only if there is no existing relationship
262                  */
263                 $r = q("SELECT `intro`.*, `intro`.`id` AS `iid`, `contact`.`id` AS `cid`, `contact`.`rel`
264                         FROM `intro` LEFT JOIN `contact` on `intro`.`contact-id` = `contact`.`id`
265                         WHERE `intro`.`blocked` = 1 AND `contact`.`self` = 0
266                         AND `intro`.`datetime` < UTC_TIMESTAMP() - INTERVAL 30 MINUTE "
267                 );
268                 if (DBA::isResult($r)) {
269                         foreach ($r as $rr) {
270                                 if (!$rr['rel']) {
271                                         DBA::delete('contact', ['id' => $rr['cid'], 'self' => false]);
272                                 }
273                                 DBA::delete('intro', ['id' => $rr['iid']]);
274                         }
275                 }
276
277                 $url = trim($_POST['dfrn_url']);
278                 if (!strlen($url)) {
279                         notice(L10n::t("Invalid locator") . EOL);
280                         return;
281                 }
282
283                 $hcard = '';
284
285                 // Detect the network
286                 $data = Probe::uri($url);
287                 $network = $data["network"];
288
289                 // Canonicalise email-style profile locator
290                 $url = Probe::webfingerDfrn($url, $hcard);
291
292                 if (substr($url, 0, 5) === 'stat:') {
293                         // Every time we detect the remote subscription we define this as OStatus.
294                         // We do this even if it is not OStatus.
295                         // we only need to pass this through another section of the code.
296                         if ($network != Protocol::DIASPORA) {
297                                 $network = Protocol::OSTATUS;
298                         }
299
300                         $url = substr($url, 5);
301                 } else {
302                         $network = Protocol::DFRN;
303                 }
304
305                 Logger::log('dfrn_request: url: ' . $url . ',network=' . $network, Logger::DEBUG);
306
307                 if ($network === Protocol::DFRN) {
308                         $ret = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' AND `self` = 0 LIMIT 1",
309                                 intval($uid),
310                                 DBA::escape($url)
311                         );
312
313                         if (DBA::isResult($ret)) {
314                                 if (strlen($ret[0]['issued-id'])) {
315                                         notice(L10n::t('You have already introduced yourself here.') . EOL);
316                                         return;
317                                 } elseif ($ret[0]['rel'] == Contact::FRIEND) {
318                                         notice(L10n::t('Apparently you are already friends with %s.', $a->profile['name']) . EOL);
319                                         return;
320                                 } else {
321                                         $contact_record = $ret[0];
322                                         $parms = ['dfrn-request' => $ret[0]['request']];
323                                 }
324                         }
325
326                         $issued_id = Strings::getRandomHex();
327
328                         if (is_array($contact_record)) {
329                                 // There is a contact record but no issued-id, so this
330                                 // is a reciprocal introduction from a known contact
331                                 $r = q("UPDATE `contact` SET `issued-id` = '%s' WHERE `id` = %d",
332                                         DBA::escape($issued_id),
333                                         intval($contact_record['id'])
334                                 );
335                         } else {
336                                 $url = Network::isUrlValid($url);
337                                 if (!$url) {
338                                         notice(L10n::t('Invalid profile URL.') . EOL);
339                                         DI::baseUrl()->redirect(DI::args()->getCommand());
340                                         return; // NOTREACHED
341                                 }
342
343                                 if (!Network::isUrlAllowed($url)) {
344                                         notice(L10n::t('Disallowed profile URL.') . EOL);
345                                         DI::baseUrl()->redirect(DI::args()->getCommand());
346                                         return; // NOTREACHED
347                                 }
348
349                                 if (Network::isUrlBlocked($url)) {
350                                         notice(L10n::t('Blocked domain') . EOL);
351                                         DI::baseUrl()->redirect(DI::args()->getCommand());
352                                         return; // NOTREACHED
353                                 }
354
355                                 $parms = Probe::profile(($hcard) ? $hcard : $url);
356
357                                 if (!count($parms)) {
358                                         notice(L10n::t('Profile location is not valid or does not contain profile information.') . EOL);
359                                         DI::baseUrl()->redirect(DI::args()->getCommand());
360                                 } else {
361                                         if (empty($parms['fn'])) {
362                                                 notice(L10n::t('Warning: profile location has no identifiable owner name.') . EOL);
363                                         }
364                                         if (empty($parms['photo'])) {
365                                                 notice(L10n::t('Warning: profile location has no profile photo.') . EOL);
366                                         }
367                                         $invalid = Probe::validDfrn($parms);
368                                         if ($invalid) {
369                                                 notice(L10n::tt("%d required parameter was not found at the given location", "%d required parameters were not found at the given location", $invalid) . EOL);
370
371                                                 return;
372                                         }
373                                 }
374
375                                 $parms['url'] = $url;
376                                 $parms['issued-id'] = $issued_id;
377                                 $photo = $parms["photo"];
378
379                                 DBA::escapeArray($parms);
380                                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `name`, `nick`, `issued-id`, `photo`, `site-pubkey`,
381                                         `request`, `confirm`, `notify`, `poll`, `network`, `blocked`, `pending` )
382                                         VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d )",
383                                         intval($uid),
384                                         DBA::escape(DateTimeFormat::utcNow()),
385                                         $parms['url'],
386                                         DBA::escape(Strings::normaliseLink($url)),
387                                         $parms['addr'],
388                                         $parms['fn'],
389                                         $parms['nick'],
390                                         $parms['issued-id'],
391                                         $parms['photo'],
392                                         $parms['key'],
393                                         $parms['dfrn-request'],
394                                         $parms['dfrn-confirm'],
395                                         $parms['dfrn-notify'],
396                                         $parms['dfrn-poll'],
397                                         DBA::escape(Protocol::DFRN),
398                                         intval($blocked),
399                                         intval($pending)
400                                 );
401
402                                 // find the contact record we just created
403                                 if ($r) {
404                                         $r = q("SELECT `id` FROM `contact`
405                                                 WHERE `uid` = %d AND `url` = '%s' AND `issued-id` = '%s' LIMIT 1",
406                                                 intval($uid),
407                                                 $parms['url'],
408                                                 $parms['issued-id']
409                                         );
410                                         if (DBA::isResult($r)) {
411                                                 $contact_record = $r[0];
412                                                 Contact::updateAvatar($photo, $uid, $contact_record["id"], true);
413                                         }
414                                 }
415                         }
416                         if ($r === false) {
417                                 notice(L10n::t('Failed to update contact record.') . EOL);
418                                 return;
419                         }
420
421                         $hash = Strings::getRandomHex() . (string) time();   // Generate a confirm_key
422
423                         if (is_array($contact_record)) {
424                                 q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `note`, `hash`, `datetime`)
425                                         VALUES ( %d, %d, 1, %d, '%s', '%s', '%s' )",
426                                         intval($uid),
427                                         intval($contact_record['id']),
428                                         intval(!empty($_POST['knowyou'])),
429                                         DBA::escape(Strings::escapeTags(trim($_POST['dfrn-request-message'] ?? ''))),
430                                         DBA::escape($hash),
431                                         DBA::escape(DateTimeFormat::utcNow())
432                                 );
433                         }
434
435                         // This notice will only be seen by the requestor if the requestor and requestee are on the same server.
436                         if (!$failed) {
437                                 info(L10n::t('Your introduction has been sent.') . EOL);
438                         }
439
440                         // "Homecoming" - send the requestor back to their site to record the introduction.
441                         $dfrn_url = bin2hex(DI::baseUrl()->get() . '/profile/' . $nickname);
442                         $aes_allow = ((function_exists('openssl_encrypt')) ? 1 : 0);
443
444                         System::externalRedirect($parms['dfrn-request'] . "?dfrn_url=$dfrn_url"
445                                 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
446                                 . '&confirm_key=' . $hash
447                                 . (($aes_allow) ? "&aes_allow=1" : "")
448                         );
449                         // NOTREACHED
450                         // END $network === Protocol::DFRN
451                 } elseif (($network != Protocol::PHANTOM) && ($url != "")) {
452
453                         /* Substitute our user's feed URL into $url template
454                          * Send the subscriber home to subscribe
455                          */
456                         // Diaspora needs the uri in the format user@domain.tld
457                         // Diaspora will support the remote subscription in a future version
458                         if ($network == Protocol::DIASPORA) {
459                                 $uri = $nickname . '@' . DI::baseUrl()->getHostname();
460
461                                 if (DI::baseUrl()->getUrlPath()) {
462                                         $uri .= '/' . DI::baseUrl()->getUrlPath();
463                                 }
464
465                                 $uri = urlencode($uri);
466                         } else {
467                                 $uri = 'profile/' . $nickname;
468                         }
469
470                         $url = str_replace('{uri}', $uri, $url);
471                         System::externalRedirect($url);
472                         // NOTREACHED
473                         // END $network != Protocol::PHANTOM
474                 } else {
475                         notice(L10n::t("Remote subscription can't be done for your network. Please subscribe directly on your system.") . EOL);
476                         return;
477                 }
478         } return;
479 }
480
481 function dfrn_request_content(App $a)
482 {
483         if ($a->argc != 2 || empty($a->profile)) {
484                 return "";
485         }
486
487         // "Homecoming". Make sure we're logged in to this site as the correct user. Then offer a confirm button
488         // to send us to the post section to record the introduction.
489         if (!empty($_GET['dfrn_url'])) {
490                 if (!local_user()) {
491                         info(L10n::t("Please login to confirm introduction.") . EOL);
492                         /* setup the return URL to come back to this page if they use openid */
493                         return Login::form();
494                 }
495
496                 // Edge case, but can easily happen in the wild. This person is authenticated,
497                 // but not as the person who needs to deal with this request.
498                 if ($a->user['nickname'] != $a->argv[1]) {
499                         notice(L10n::t("Incorrect identity currently logged in. Please login to <strong>this</strong> profile.") . EOL);
500                         return Login::form();
501                 }
502
503                 $dfrn_url = Strings::escapeTags(trim(hex2bin($_GET['dfrn_url'])));
504                 $aes_allow = !empty($_GET['aes_allow']);
505                 $confirm_key = $_GET['confirm_key'] ?? '';
506
507                 // Checking fastlane for validity
508                 if (!empty($_SESSION['fastlane']) && (Strings::normaliseLink($_SESSION["fastlane"]) == Strings::normaliseLink($dfrn_url))) {
509                         $_POST["dfrn_url"] = $dfrn_url;
510                         $_POST["confirm_key"] = $confirm_key;
511                         $_POST["localconfirm"] = 1;
512                         $_POST["hidden-contact"] = 0;
513                         $_POST["submit"] = L10n::t('Confirm');
514
515                         dfrn_request_post($a);
516
517                         exit();
518                 }
519
520                 $tpl = Renderer::getMarkupTemplate("dfrn_req_confirm.tpl");
521                 $o = Renderer::replaceMacros($tpl, [
522                         '$dfrn_url' => $dfrn_url,
523                         '$aes_allow' => (($aes_allow) ? '<input type="hidden" name="aes_allow" value="1" />' : "" ),
524                         '$hidethem' => L10n::t('Hide this contact'),
525                         '$confirm_key' => $confirm_key,
526                         '$welcome' => L10n::t('Welcome home %s.', $a->user['username']),
527                         '$please' => L10n::t('Please confirm your introduction/connection request to %s.', $dfrn_url),
528                         '$submit' => L10n::t('Confirm'),
529                         '$uid' => $_SESSION['uid'],
530                         '$nickname' => $a->user['nickname'],
531                         'dfrn_rawurl' => $_GET['dfrn_url']
532                 ]);
533                 return $o;
534         } elseif (!empty($_GET['confirm_key'])) {
535                 // we are the requestee and it is now safe to send our user their introduction,
536                 // We could just unblock it, but first we have to jump through a few hoops to
537                 // send an email, or even to find out if we need to send an email.
538                 $intro = q("SELECT * FROM `intro` WHERE `hash` = '%s' LIMIT 1",
539                         DBA::escape($_GET['confirm_key'])
540                 );
541
542                 if (DBA::isResult($intro)) {
543                         $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
544                                 WHERE `contact`.`id` = %d LIMIT 1",
545                                 intval($intro[0]['contact-id'])
546                         );
547
548                         $auto_confirm = false;
549
550                         if (DBA::isResult($r)) {
551                                 if ($r[0]['page-flags'] != User::PAGE_FLAGS_NORMAL && $r[0]['page-flags'] != User::PAGE_FLAGS_PRVGROUP) {
552                                         $auto_confirm = true;
553                                 }
554
555                                 if (!$auto_confirm) {
556                                         notification([
557                                                 'type'         => NOTIFY_INTRO,
558                                                 'notify_flags' => $r[0]['notify-flags'],
559                                                 'language'     => $r[0]['language'],
560                                                 'to_name'      => $r[0]['username'],
561                                                 'to_email'     => $r[0]['email'],
562                                                 'uid'          => $r[0]['uid'],
563                                                 'link'         => DI::baseUrl() . '/notifications/intros',
564                                                 'source_name'  => ((strlen(stripslashes($r[0]['name']))) ? stripslashes($r[0]['name']) : L10n::t('[Name Withheld]')),
565                                                 'source_link'  => $r[0]['url'],
566                                                 'source_photo' => $r[0]['photo'],
567                                                 'verb'         => Activity::REQ_FRIEND,
568                                                 'otype'        => 'intro'
569                                         ]);
570                                 }
571
572                                 if ($auto_confirm) {
573                                         require_once 'mod/dfrn_confirm.php';
574                                         $handsfree = [
575                                                 'uid'      => $r[0]['uid'],
576                                                 'node'     => $r[0]['nickname'],
577                                                 'dfrn_id'  => $r[0]['issued-id'],
578                                                 'intro_id' => $intro[0]['id'],
579                                                 'duplex'   => (($r[0]['page-flags'] == User::PAGE_FLAGS_FREELOVE) ? 1 : 0),
580                                         ];
581                                         dfrn_confirm_post($a, $handsfree);
582                                 }
583                         }
584
585                         if (!$auto_confirm) {
586
587                                 // If we are auto_confirming, this record will have already been nuked
588                                 // in dfrn_confirm_post()
589
590                                 q("UPDATE `intro` SET `blocked` = 0 WHERE `hash` = '%s'",
591                                         DBA::escape($_GET['confirm_key'])
592                                 );
593                         }
594                 }
595
596                 exit();
597         } else {
598                 // Normal web request. Display our user's introduction form.
599                 if (Config::get('system', 'block_public') && !Session::isAuthenticated()) {
600                         if (!Config::get('system', 'local_block')) {
601                                 notice(L10n::t('Public access denied.') . EOL);
602                                 return;
603                         }
604                 }
605
606                 // Try to auto-fill the profile address
607                 // At first look if an address was provided
608                 // Otherwise take the local address
609                 if (!empty($_GET['addr'])) {
610                         $myaddr = hex2bin($_GET['addr']);
611                 } elseif (!empty($_GET['address'])) {
612                         $myaddr = $_GET['address'];
613                 } elseif (local_user()) {
614                         if (strlen(DI::baseUrl()->getUrlPath())) {
615                                 $myaddr = DI::baseUrl() . '/profile/' . $a->user['nickname'];
616                         } else {
617                                 $myaddr = $a->user['nickname'] . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3);
618                         }
619                 } else {
620                         // last, try a zrl
621                         $myaddr = Profile::getMyURL();
622                 }
623
624                 $target_addr = $a->profile['nickname'] . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3);
625
626                 /* The auto_request form only has the profile address
627                  * because nobody is going to read the comments and
628                  * it doesn't matter if they know you or not.
629                  */
630                 if ($a->profile['page-flags'] == User::PAGE_FLAGS_NORMAL) {
631                         $tpl = Renderer::getMarkupTemplate('dfrn_request.tpl');
632                 } else {
633                         $tpl = Renderer::getMarkupTemplate('auto_request.tpl');
634                 }
635
636                 $page_desc = L10n::t("Please enter your 'Identity Address' from one of the following supported communications networks:");
637
638                 $invite_desc = L10n::t('If you are not yet a member of the free social web, <a href="%s">follow this link to find a public Friendica site and join us today</a>.', Search::getGlobalDirectory() . '/servers');
639
640                 $o = Renderer::replaceMacros($tpl, [
641                         '$header' => L10n::t('Friend/Connection Request'),
642                         '$desc' => L10n::t('Examples: jojo@demo.friendica.com, http://demo.friendica.com/profile/jojo, testuser@gnusocial.de'),
643                         '$pls_answer' => L10n::t('Please answer the following:'),
644                         '$does_know_you' => ['knowyou', L10n::t('Does %s know you?', $a->profile['name']), false, '', [L10n::t('No'), L10n::t('Yes')]],
645                         '$add_note' => L10n::t('Add a personal note:'),
646                         '$page_desc' => $page_desc,
647                         '$friendica' => L10n::t('Friendica'),
648                         '$statusnet' => L10n::t("GNU Social \x28Pleroma, Mastodon\x29"),
649                         '$diaspora' => L10n::t("Diaspora \x28Socialhome, Hubzilla\x29"),
650                         '$diasnote' => L10n::t(' - please do not use this form.  Instead, enter %s into your Diaspora search bar.', $target_addr),
651                         '$your_address' => L10n::t('Your Identity Address:'),
652                         '$invite_desc' => $invite_desc,
653                         '$submit' => L10n::t('Submit Request'),
654                         '$cancel' => L10n::t('Cancel'),
655                         '$nickname' => $a->argv[1],
656                         '$name' => $a->profile['name'],
657                         '$myaddr' => $myaddr
658                 ]);
659                 return $o;
660         }
661 }