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