Move .well-known, webfinger, xrd to src/Module/
[friendica.git/.git] / mod / dfrn_confirm.php
1 <?php
2 /**
3  * @file mod/dfrn_confirm.php
4  * @brief Module: dfrn_confirm
5  * Purpose: Friendship acceptance for DFRN contacts
6  *
7  * There are two possible entry points and three scenarios.
8  *
9  *   1. A form was submitted by our user approving a friendship that originated elsewhere.
10  *      This may also be called from dfrn_request to automatically approve a friendship.
11  *
12  *   2. We may be the target or other side of the conversation to scenario 1, and will
13  *      interact with that process on our own user's behalf.
14  *
15  *  @see PDF with dfrn specs: https://github.com/friendica/friendica/blob/master/spec/dfrn2.pdf
16  *    You also find a graphic which describes the confirmation process at
17  *    https://github.com/friendica/friendica/blob/master/spec/dfrn2_contact_confirmation.png
18  */
19
20 use Friendica\App;
21 use Friendica\Core\Config;
22 use Friendica\Core\L10n;
23 use Friendica\Core\Logger;
24 use Friendica\Core\Protocol;
25 use Friendica\Core\System;
26 use Friendica\Database\DBA;
27 use Friendica\Model\APContact;
28 use Friendica\Model\Contact;
29 use Friendica\Model\Group;
30 use Friendica\Model\User;
31 use Friendica\Network\Probe;
32 use Friendica\Protocol\Diaspora;
33 use Friendica\Protocol\ActivityPub;
34 use Friendica\Util\Crypto;
35 use Friendica\Util\DateTimeFormat;
36 use Friendica\Util\Network;
37 use Friendica\Util\Strings;
38 use Friendica\Util\XML;
39
40 function dfrn_confirm_post(App $a, $handsfree = null)
41 {
42         $node = null;
43         if (is_array($handsfree)) {
44                 /*
45                  * We were called directly from dfrn_request due to automatic friend acceptance.
46                  * Any $_POST parameters we may require are supplied in the $handsfree array.
47                  *
48                  */
49                 $node = $handsfree['node'];
50                 $a->interactive = false; // notice() becomes a no-op since nobody is there to see it
51         } elseif ($a->argc > 1) {
52                 $node = $a->argv[1];
53         }
54
55         /*
56          * Main entry point. Scenario 1. Our user received a friend request notification (perhaps
57          * from another site) and clicked 'Approve'.
58          * $POST['source_url'] is not set. If it is, it indicates Scenario 2.
59          *
60          * We may also have been called directly from dfrn_request ($handsfree != null) due to
61          * this being a page type which supports automatic friend acceptance. That is also Scenario 1
62          * since we are operating on behalf of our registered user to approve a friendship.
63          */
64         if (empty($_POST['source_url'])) {
65                 $uid = defaults($handsfree, 'uid', local_user());
66                 if (!$uid) {
67                         notice(L10n::t('Permission denied.') . EOL);
68                         return;
69                 }
70
71                 $user = DBA::selectFirst('user', [], ['uid' => $uid]);
72                 if (!DBA::isResult($user)) {
73                         notice(L10n::t('Profile not found.') . EOL);
74                         return;
75                 }
76
77                 // These data elements may come from either the friend request notification form or $handsfree array.
78                 if (is_array($handsfree)) {
79                         Logger::log('Confirm in handsfree mode');
80                         $dfrn_id  = $handsfree['dfrn_id'];
81                         $intro_id = $handsfree['intro_id'];
82                         $duplex   = $handsfree['duplex'];
83                         $cid      = 0;
84                         $hidden   = intval(defaults($handsfree, 'hidden'  , 0));
85                 } else {
86                         $dfrn_id  = Strings::escapeTags(trim(defaults($_POST, 'dfrn_id'   , '')));
87                         $intro_id =      intval(defaults($_POST, 'intro_id'  , 0));
88                         $duplex   =      intval(defaults($_POST, 'duplex'    , 0));
89                         $cid      =      intval(defaults($_POST, 'contact_id', 0));
90                         $hidden   =      intval(defaults($_POST, 'hidden'    , 0));
91                 }
92
93                 /*
94                  * Ensure that dfrn_id has precedence when we go to find the contact record.
95                  * We only want to search based on contact id if there is no dfrn_id,
96                  * e.g. for OStatus network followers.
97                  */
98                 if (strlen($dfrn_id)) {
99                         $cid = 0;
100                 }
101
102                 Logger::log('Confirming request for dfrn_id (issued) ' . $dfrn_id);
103                 if ($cid) {
104                         Logger::log('Confirming follower with contact_id: ' . $cid);
105                 }
106
107                 /*
108                  * The other person will have been issued an ID when they first requested friendship.
109                  * Locate their record. At this time, their record will have both pending and blocked set to 1.
110                  * There won't be any dfrn_id if this is a network follower, so use the contact_id instead.
111                  */
112                 $r = q("SELECT *
113                         FROM `contact`
114                         WHERE (
115                                 (`issued-id` != '' AND `issued-id` = '%s')
116                                 OR
117                                 (`id` = %d AND `id` != 0)
118                         )
119                         AND `uid` = %d
120                         AND `duplex` = 0
121                         LIMIT 1",
122                         DBA::escape($dfrn_id),
123                         intval($cid),
124                         intval($uid)
125                 );
126                 if (!DBA::isResult($r)) {
127                         Logger::log('Contact not found in DB.');
128                         notice(L10n::t('Contact not found.') . EOL);
129                         notice(L10n::t('This may occasionally happen if contact was requested by both persons and it has already been approved.') . EOL);
130                         return;
131                 }
132
133                 $contact = $r[0];
134
135                 $contact_id   = $contact['id'];
136                 $relation     = $contact['rel'];
137                 $site_pubkey  = $contact['site-pubkey'];
138                 $dfrn_confirm = $contact['confirm'];
139                 $aes_allow    = $contact['aes_allow'];
140
141                 $network = ((strlen($contact['issued-id'])) ? Protocol::DFRN : Protocol::OSTATUS);
142
143                 if ($contact['network']) {
144                         $network = $contact['network'];
145                 }
146
147                 // an empty DFRN-ID tells us that it had been a request via AP from a Friendica contact
148                 if (($network === Protocol::DFRN) && empty($dfrn_id) && !empty($contact['hub-verify'])) {
149                         $apcontact = APContact::getByURL($contact['url']);
150                         if (!empty($apcontact)) {
151                                 $network = Protocol::ACTIVITYPUB;
152                         }
153                 }
154
155                 if ($network === Protocol::DFRN) {
156                         /*
157                          * Generate a key pair for all further communications with this person.
158                          * We have a keypair for every contact, and a site key for unknown people.
159                          * This provides a means to carry on relationships with other people if
160                          * any single key is compromised. It is a robust key. We're much more
161                          * worried about key leakage than anybody cracking it.
162                          */
163                         $res = Crypto::newKeypair(4096);
164
165                         $private_key = $res['prvkey'];
166                         $public_key  = $res['pubkey'];
167
168                         // Save the private key. Send them the public key.
169                         q("UPDATE `contact` SET `prvkey` = '%s' WHERE `id` = %d AND `uid` = %d",
170                                 DBA::escape($private_key),
171                                 intval($contact_id),
172                                 intval($uid)
173                         );
174
175                         $params = [];
176
177                         /*
178                          * Per the DFRN protocol, we will verify both ends by encrypting the dfrn_id with our
179                          * site private key (person on the other end can decrypt it with our site public key).
180                          * Then encrypt our profile URL with the other person's site public key. They can decrypt
181                          * it with their site private key. If the decryption on the other end fails for either
182                          * item, it indicates tampering or key failure on at least one site and we will not be
183                          * able to provide a secure communication pathway.
184                          *
185                          * If other site is willing to accept full encryption, (aes_allow is 1 AND we have php5.3
186                          * or later) then we encrypt the personal public key we send them using AES-256-CBC and a
187                          * random key which is encrypted with their site public key.
188                          */
189
190                         $src_aes_key = openssl_random_pseudo_bytes(64);
191
192                         $result = '';
193                         openssl_private_encrypt($dfrn_id, $result, $user['prvkey']);
194
195                         $params['dfrn_id'] = bin2hex($result);
196                         $params['public_key'] = $public_key;
197
198                         $my_url = System::baseUrl() . '/profile/' . $user['nickname'];
199
200                         openssl_public_encrypt($my_url, $params['source_url'], $site_pubkey);
201                         $params['source_url'] = bin2hex($params['source_url']);
202
203                         if ($aes_allow && function_exists('openssl_encrypt')) {
204                                 openssl_public_encrypt($src_aes_key, $params['aes_key'], $site_pubkey);
205                                 $params['aes_key'] = bin2hex($params['aes_key']);
206                                 $params['public_key'] = bin2hex(openssl_encrypt($public_key, 'AES-256-CBC', $src_aes_key));
207                         }
208
209                         $params['dfrn_version'] = DFRN_PROTOCOL_VERSION;
210                         if ($duplex == 1) {
211                                 $params['duplex'] = 1;
212                         }
213
214                         if ($user['page-flags'] == User::PAGE_FLAGS_COMMUNITY) {
215                                 $params['page'] = 1;
216                         }
217
218                         if ($user['page-flags'] == User::PAGE_FLAGS_PRVGROUP) {
219                                 $params['page'] = 2;
220                         }
221
222                         Logger::log('Confirm: posting data to ' . $dfrn_confirm . ': ' . print_r($params, true), Logger::DATA);
223
224                         /*
225                          *
226                          * POST all this stuff to the other site.
227                          * Temporarily raise the network timeout to 120 seconds because the default 60
228                          * doesn't always give the other side quite enough time to decrypt everything.
229                          *
230                          */
231
232                         $res = Network::post($dfrn_confirm, $params, null, $redirects, 120)->getBody();
233
234                         Logger::log(' Confirm: received data: ' . $res, Logger::DATA);
235
236                         // Now figure out what they responded. Try to be robust if the remote site is
237                         // having difficulty and throwing up errors of some kind.
238
239                         $leading_junk = substr($res, 0, strpos($res, '<?xml'));
240
241                         $res = substr($res, strpos($res, '<?xml'));
242                         if (!strlen($res)) {
243                                 // No XML at all, this exchange is messed up really bad.
244                                 // We shouldn't proceed, because the xml parser might choke,
245                                 // and $status is going to be zero, which indicates success.
246                                 // We can hardly call this a success.
247                                 notice(L10n::t('Response from remote site was not understood.') . EOL);
248                                 return;
249                         }
250
251                         if (strlen($leading_junk) && Config::get('system', 'debugging')) {
252                                 // This might be more common. Mixed error text and some XML.
253                                 // If we're configured for debugging, show the text. Proceed in either case.
254                                 notice(L10n::t('Unexpected response from remote site: ') . EOL . $leading_junk . EOL);
255                         }
256
257                         if (stristr($res, "<status") === false) {
258                                 // wrong xml! stop here!
259                                 Logger::log('Unexpected response posting to ' . $dfrn_confirm);
260                                 notice(L10n::t('Unexpected response from remote site: ') . EOL . htmlspecialchars($res) . EOL);
261                                 return;
262                         }
263
264                         $xml = XML::parseString($res);
265                         $status = (int) $xml->status;
266                         $message = XML::unescape($xml->message);   // human readable text of what may have gone wrong.
267                         switch ($status) {
268                                 case 0:
269                                         info(L10n::t("Confirmation completed successfully.") . EOL);
270                                         break;
271                                 case 1:
272                                         // birthday paradox - generate new dfrn-id and fall through.
273                                         $new_dfrn_id = Strings::getRandomHex();
274                                         q("UPDATE contact SET `issued-id` = '%s' WHERE `id` = %d AND `uid` = %d",
275                                                 DBA::escape($new_dfrn_id),
276                                                 intval($contact_id),
277                                                 intval($uid)
278                                         );
279
280                                 case 2:
281                                         notice(L10n::t("Temporary failure. Please wait and try again.") . EOL);
282                                         break;
283                                 case 3:
284                                         notice(L10n::t("Introduction failed or was revoked.") . EOL);
285                                         break;
286                         }
287
288                         if (strlen($message)) {
289                                 notice(L10n::t('Remote site reported: ') . $message . EOL);
290                         }
291
292                         if (($status == 0) && $intro_id) {
293                                 $intro = DBA::selectFirst('intro', ['note'], ['id' => $intro_id]);
294                                 if (DBA::isResult($intro)) {
295                                         DBA::update('contact', ['reason' => $intro['note']], ['id' => $contact_id]);
296                                 }
297
298                                 // Success. Delete the notification.
299                                 DBA::delete('intro', ['id' => $intro_id]);
300                         }
301
302                         if ($status != 0) {
303                                 return;
304                         }
305                 }
306
307                 /*
308                  * We have now established a relationship with the other site.
309                  * Let's make our own personal copy of their profile photo so we don't have
310                  * to always load it from their site.
311                  *
312                  * We will also update the contact record with the nature and scope of the relationship.
313                  */
314                 Contact::updateAvatar($contact['photo'], $uid, $contact_id);
315
316                 Logger::log('dfrn_confirm: confirm - imported photos');
317
318                 if ($network === Protocol::DFRN) {
319                         $new_relation = Contact::FOLLOWER;
320
321                         if (($relation == Contact::SHARING) || ($duplex)) {
322                                 $new_relation = Contact::FRIEND;
323                         }
324
325                         if (($relation == Contact::SHARING) && ($duplex)) {
326                                 $duplex = 0;
327                         }
328
329                         $r = q("UPDATE `contact` SET `rel` = %d,
330                                 `name-date` = '%s',
331                                 `uri-date` = '%s',
332                                 `blocked` = 0,
333                                 `pending` = 0,
334                                 `duplex` = %d,
335                                 `hidden` = %d,
336                                 `network` = '%s' WHERE `id` = %d
337                         ",
338                                 intval($new_relation),
339                                 DBA::escape(DateTimeFormat::utcNow()),
340                                 DBA::escape(DateTimeFormat::utcNow()),
341                                 intval($duplex),
342                                 intval($hidden),
343                                 DBA::escape(Protocol::DFRN),
344                                 intval($contact_id)
345                         );
346                 } else {
347                         if ($network == Protocol::ACTIVITYPUB) {
348                                 ActivityPub\Transmitter::sendContactAccept($contact['url'], $contact['hub-verify'], $uid);
349                                 // Setting "pending" to true on a bidirectional contact request could create a problem when it isn't accepted on the other side
350                                 // Then we have got a situation where - although one direction is accepted - the contact still appears as pending.
351                                 // Possibly we need two different "pending" fields, one for incoming, one for outgoing?
352                                 // This has to be thought over, but for now this here is a better solution.
353                                 // $pending = $duplex;
354                                 $pending = false;
355                         } else {
356                                 $pending = false;
357                         }
358
359                         // $network !== Protocol::DFRN
360                         $network = defaults($contact, 'network', Protocol::OSTATUS);
361
362                         $arr = Probe::uri($contact['url'], $network);
363
364                         $notify  = defaults($contact, 'notify' , $arr['notify']);
365                         $poll    = defaults($contact, 'poll'   , $arr['poll']);
366
367                         $addr = $arr['addr'];
368
369                         $new_relation = $contact['rel'];
370                         $writable = $contact['writable'];
371
372                         if (in_array($network, [Protocol::DIASPORA, Protocol::ACTIVITYPUB])) {
373                                 if ($duplex) {
374                                         $new_relation = Contact::FRIEND;
375                                 } else {
376                                         $new_relation = Contact::FOLLOWER;
377                                 }
378
379                                 if ($new_relation != Contact::FOLLOWER) {
380                                         $writable = 1;
381                                 }
382                         }
383
384                         DBA::delete('intro', ['id' => $intro_id]);
385
386                         $fields = ['name-date' => DateTimeFormat::utcNow(),
387                                 'uri-date' => DateTimeFormat::utcNow(), 'addr' => $addr,
388                                 'notify' => $notify, 'poll' => $poll, 'blocked' => false,
389                                 'pending' => $pending, 'network' => $network,
390                                 'writable' => $writable, 'hidden' => $hidden, 'rel' => $new_relation];
391                         DBA::update('contact', $fields, ['id' => $contact_id]);
392                 }
393
394                 if (!DBA::isResult($r)) {
395                         notice(L10n::t('Unable to set contact photo.') . EOL);
396                 }
397
398                 // reload contact info
399                 $contact = DBA::selectFirst('contact', [], ['id' => $contact_id]);
400                 if ((isset($new_relation) && $new_relation == Contact::FRIEND)) {
401                         if (DBA::isResult($contact) && ($contact['network'] === Protocol::DIASPORA)) {
402                                 $ret = Diaspora::sendShare($user, $contact);
403                                 Logger::log('share returns: ' . $ret);
404                         }
405                 }
406
407                 Group::addMember(User::getDefaultGroup($uid, $contact["network"]), $contact['id']);
408
409                 if ($network == Protocol::ACTIVITYPUB && $duplex) {
410                         ActivityPub\Transmitter::sendActivity('Follow', $contact['url'], $uid);
411                 }
412
413                 // Let's send our user to the contact editor in case they want to
414                 // do anything special with this new friend.
415                 if ($handsfree === null) {
416                         $a->internalRedirect('contact/' . intval($contact_id));
417                 } else {
418                         return;
419                 }
420                 //NOTREACHED
421         }
422
423         /*
424          * End of Scenario 1. [Local confirmation of remote friend request].
425          *
426          * Begin Scenario 2. This is the remote response to the above scenario.
427          * This will take place on the site that originally initiated the friend request.
428          * In the section above where the confirming party makes a POST and
429          * retrieves xml status information, they are communicating with the following code.
430          */
431         if (!empty($_POST['source_url'])) {
432                 // We are processing an external confirmation to an introduction created by our user.
433                 $public_key =         defaults($_POST, 'public_key', '');
434                 $dfrn_id    = hex2bin(defaults($_POST, 'dfrn_id'   , ''));
435                 $source_url = hex2bin(defaults($_POST, 'source_url', ''));
436                 $aes_key    =         defaults($_POST, 'aes_key'   , '');
437                 $duplex     =  intval(defaults($_POST, 'duplex'    , 0));
438                 $page       =  intval(defaults($_POST, 'page'      , 0));
439
440                 $forum = (($page == 1) ? 1 : 0);
441                 $prv   = (($page == 2) ? 1 : 0);
442
443                 Logger::log('dfrn_confirm: requestee contacted: ' . $node);
444
445                 Logger::log('dfrn_confirm: request: POST=' . print_r($_POST, true), Logger::DATA);
446
447                 // If $aes_key is set, both of these items require unpacking from the hex transport encoding.
448
449                 if (!empty($aes_key)) {
450                         $aes_key = hex2bin($aes_key);
451                         $public_key = hex2bin($public_key);
452                 }
453
454                 // Find our user's account
455                 $user = DBA::selectFirst('user', [], ['nickname' => $node]);
456                 if (!DBA::isResult($user)) {
457                         $message = L10n::t('No user record found for \'%s\' ', $node);
458                         System::xmlExit(3, $message); // failure
459                         // NOTREACHED
460                 }
461
462                 $my_prvkey = $user['prvkey'];
463                 $local_uid = $user['uid'];
464
465
466                 if (!strstr($my_prvkey, 'PRIVATE KEY')) {
467                         $message = L10n::t('Our site encryption key is apparently messed up.');
468                         System::xmlExit(3, $message);
469                 }
470
471                 // verify everything
472
473                 $decrypted_source_url = "";
474                 openssl_private_decrypt($source_url, $decrypted_source_url, $my_prvkey);
475
476
477                 if (!strlen($decrypted_source_url)) {
478                         $message = L10n::t('Empty site URL was provided or URL could not be decrypted by us.');
479                         System::xmlExit(3, $message);
480                         // NOTREACHED
481                 }
482
483                 $contact = DBA::selectFirst('contact', [], ['url' => $decrypted_source_url, 'uid' => $local_uid]);
484                 if (!DBA::isResult($contact)) {
485                         if (strstr($decrypted_source_url, 'http:')) {
486                                 $newurl = str_replace('http:', 'https:', $decrypted_source_url);
487                         } else {
488                                 $newurl = str_replace('https:', 'http:', $decrypted_source_url);
489                         }
490
491                         $contact = DBA::selectFirst('contact', [], ['url' => $newurl, 'uid' => $local_uid]);
492                         if (!DBA::isResult($contact)) {
493                                 // this is either a bogus confirmation (?) or we deleted the original introduction.
494                                 $message = L10n::t('Contact record was not found for you on our site.');
495                                 System::xmlExit(3, $message);
496                                 return; // NOTREACHED
497                         }
498                 }
499
500                 $relation = $contact['rel'];
501
502                 // Decrypt all this stuff we just received
503
504                 $foreign_pubkey = $contact['site-pubkey'];
505                 $dfrn_record = $contact['id'];
506
507                 if (!$foreign_pubkey) {
508                         $message = L10n::t('Site public key not available in contact record for URL %s.', $decrypted_source_url);
509                         System::xmlExit(3, $message);
510                 }
511
512                 $decrypted_dfrn_id = "";
513                 openssl_public_decrypt($dfrn_id, $decrypted_dfrn_id, $foreign_pubkey);
514
515                 if (strlen($aes_key)) {
516                         $decrypted_aes_key = "";
517                         openssl_private_decrypt($aes_key, $decrypted_aes_key, $my_prvkey);
518                         $dfrn_pubkey = openssl_decrypt($public_key, 'AES-256-CBC', $decrypted_aes_key);
519                 } else {
520                         $dfrn_pubkey = $public_key;
521                 }
522
523                 if (DBA::exists('contact', ['dfrn-id' => $decrypted_dfrn_id])) {
524                         $message = L10n::t('The ID provided by your system is a duplicate on our system. It should work if you try again.');
525                         System::xmlExit(1, $message); // Birthday paradox - duplicate dfrn-id
526                         // NOTREACHED
527                 }
528
529                 $r = q("UPDATE `contact` SET `dfrn-id` = '%s', `pubkey` = '%s' WHERE `id` = %d",
530                         DBA::escape($decrypted_dfrn_id),
531                         DBA::escape($dfrn_pubkey),
532                         intval($dfrn_record)
533                 );
534                 if (!DBA::isResult($r)) {
535                         $message = L10n::t('Unable to set your contact credentials on our system.');
536                         System::xmlExit(3, $message);
537                 }
538
539                 // It's possible that the other person also requested friendship.
540                 // If it is a duplex relationship, ditch the issued-id if one exists.
541
542                 if ($duplex) {
543                         q("UPDATE `contact` SET `issued-id` = '' WHERE `id` = %d",
544                                 intval($dfrn_record)
545                         );
546                 }
547
548                 // We're good but now we have to scrape the profile photo and send notifications.
549                 $contact = DBA::selectFirst('contact', ['photo'], ['id' => $dfrn_record]);
550                 if (DBA::isResult($contact)) {
551                         $photo = $contact['photo'];
552                 } else {
553                         $photo = System::baseUrl() . '/images/person-300.jpg';
554                 }
555
556                 Contact::updateAvatar($photo, $local_uid, $dfrn_record);
557
558                 Logger::log('dfrn_confirm: request - photos imported');
559
560                 $new_relation = Contact::SHARING;
561
562                 if (($relation == Contact::FOLLOWER) || ($duplex)) {
563                         $new_relation = Contact::FRIEND;
564                 }
565
566                 if (($relation == Contact::FOLLOWER) && ($duplex)) {
567                         $duplex = 0;
568                 }
569
570                 $r = q("UPDATE `contact` SET
571                         `rel` = %d,
572                         `name-date` = '%s',
573                         `uri-date` = '%s',
574                         `blocked` = 0,
575                         `pending` = 0,
576                         `duplex` = %d,
577                         `forum` = %d,
578                         `prv` = %d,
579                         `network` = '%s' WHERE `id` = %d
580                 ",
581                         intval($new_relation),
582                         DBA::escape(DateTimeFormat::utcNow()),
583                         DBA::escape(DateTimeFormat::utcNow()),
584                         intval($duplex),
585                         intval($forum),
586                         intval($prv),
587                         DBA::escape(Protocol::DFRN),
588                         intval($dfrn_record)
589                 );
590                 if (!DBA::isResult($r)) {       // indicates schema is messed up or total db failure
591                         $message = L10n::t('Unable to update your contact profile details on our system');
592                         System::xmlExit(3, $message);
593                 }
594
595                 // Otherwise everything seems to have worked and we are almost done. Yay!
596                 // Send an email notification
597
598                 Logger::log('dfrn_confirm: request: info updated');
599
600                 $combined = null;
601                 $r = q("SELECT `contact`.*, `user`.*
602                         FROM `contact`
603                         LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
604                         WHERE `contact`.`id` = %d
605                         LIMIT 1",
606                         intval($dfrn_record)
607                 );
608                 if (DBA::isResult($r)) {
609                         $combined = $r[0];
610
611                         if ($combined['notify-flags'] & NOTIFY_CONFIRM) {
612                                 $mutual = ($new_relation == Contact::FRIEND);
613                                 notification([
614                                         'type'         => NOTIFY_CONFIRM,
615                                         'notify_flags' => $combined['notify-flags'],
616                                         'language'     => $combined['language'],
617                                         'to_name'      => $combined['username'],
618                                         'to_email'     => $combined['email'],
619                                         'uid'          => $combined['uid'],
620                                         'link'         => System::baseUrl() . '/contact/' . $dfrn_record,
621                                         'source_name'  => ((strlen(stripslashes($combined['name']))) ? stripslashes($combined['name']) : L10n::t('[Name Withheld]')),
622                                         'source_link'  => $combined['url'],
623                                         'source_photo' => $combined['photo'],
624                                         'verb'         => ($mutual?ACTIVITY_FRIEND:ACTIVITY_FOLLOW),
625                                         'otype'        => 'intro'
626                                 ]);
627                         }
628                 }
629
630                 System::xmlExit(0); // Success
631                 return; // NOTREACHED
632                 ////////////////////// End of this scenario ///////////////////////////////////////////////
633         }
634
635         // somebody arrived here by mistake or they are fishing. Send them to the homepage.
636         $a->internalRedirect();
637         // NOTREACHED
638 }