]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_confirm.php
fixes share from diaspora
[friendica.git] / mod / dfrn_confirm.php
1 <?php
2
3 /*
4  * 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  */
16
17 function dfrn_confirm_post(&$a,$handsfree = null) {
18
19         if(is_array($handsfree)) {
20
21                 /**
22                  * We were called directly from dfrn_request due to automatic friend acceptance.
23                  * Any $_POST parameters we may require are supplied in the $handsfree array.
24                  *
25                  */
26
27                 $node = $handsfree['node'];
28                 $a->interactive = false; // notice() becomes a no-op since nobody is there to see it
29
30         }
31         else {
32                 if($a->argc > 1)
33                         $node = $a->argv[1];
34         }
35
36                 /**
37                  *
38                  * Main entry point. Scenario 1. Our user received a friend request notification (perhaps 
39                  * from another site) and clicked 'Approve'. 
40                  * $POST['source_url'] is not set. If it is, it indicates Scenario 2.
41                  *
42                  * We may also have been called directly from dfrn_request ($handsfree != null) due to 
43                  * this being a page type which supports automatic friend acceptance. That is also Scenario 1
44                  * since we are operating on behalf of our registered user to approve a friendship.
45                  *
46                  */
47
48         if(! x($_POST,'source_url')) {
49
50                 $uid = ((is_array($handsfree)) ? $handsfree['uid'] : local_user());
51
52                 if(! $uid) {
53                         notice( t('Permission denied.') . EOL );
54                         return;
55                 }       
56
57                 $user = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
58                         intval($uid)
59                 );
60
61                 if(! $user) {
62                         notice( t('Profile not found.') . EOL );
63                         return;
64                 }       
65
66
67                 // These data elements may come from either the friend request notification form or $handsfree array.
68
69                 if(is_array($handsfree)) {
70                         logger('dfrn_confirm: Confirm in handsfree mode');
71                         $dfrn_id   = $handsfree['dfrn_id'];
72                         $intro_id  = $handsfree['intro_id'];
73                         $duplex    = $handsfree['duplex'];
74                 }
75                 else {
76                         $dfrn_id  = ((x($_POST,'dfrn_id'))    ? notags(trim($_POST['dfrn_id'])) : "");
77                         $intro_id = ((x($_POST,'intro_id'))   ? intval($_POST['intro_id'])      : 0 );
78                         $duplex   = ((x($_POST,'duplex'))     ? intval($_POST['duplex'])        : 0 );
79                         $cid      = ((x($_POST,'contact_id')) ? intval($_POST['contact_id'])    : 0 );
80                 }
81
82                 /**
83                  *
84                  * Ensure that dfrn_id has precedence when we go to find the contact record.
85                  * We only want to search based on contact id if there is no dfrn_id, 
86                  * e.g. for OStatus network followers.
87                  *
88                  */
89
90                 if(strlen($dfrn_id))
91                         $cid = 0;
92
93                 logger('dfrn_confirm: Confirming request for dfrn_id (issued) ' . $dfrn_id);
94                 if($cid)
95                         logger('dfrn_confirm: Confirming follower with contact_id: ' . $cid);
96
97
98                 /**
99                  *
100                  * The other person will have been issued an ID when they first requested friendship.
101                  * Locate their record. At this time, their record will have both pending and blocked set to 1. 
102                  * There won't be any dfrn_id if this is a network follower, so use the contact_id instead.
103                  *
104                  */
105
106                 $r = q("SELECT * FROM `contact` WHERE ( ( `issued-id` != '' AND `issued-id` = '%s' ) OR ( `id` = %d AND `id` != 0 ) ) AND `uid` = %d LIMIT 1",
107                         dbesc($dfrn_id),
108                         intval($cid),
109                         intval($uid)
110                 );
111
112                 if(! count($r)) {
113                         logger('dfrn_confirm: Contact not found in DB.'); 
114                         notice( t('Contact not found.') . EOL );
115                         return;
116                 }
117
118                 $contact = $r[0];
119
120                 $contact_id   = $contact['id'];
121                 $relation     = $contact['rel'];
122                 $site_pubkey  = $contact['site-pubkey'];
123                 $dfrn_confirm = $contact['confirm'];
124                 $aes_allow    = $contact['aes_allow'];
125
126                 $network = ((strlen($contact['issued-id'])) ? 'dfrn' : 'stat');
127                 if($contact['network'])
128                         $network = $contact['network'];
129
130                 if($network === 'dfrn') {
131
132                         /**
133                          *
134                          * Generate a key pair for all further communications with this person.
135                          * We have a keypair for every contact, and a site key for unknown people.
136                          * This provides a means to carry on relationships with other people if 
137                          * any single key is compromised. It is a robust key. We're much more 
138                          * worried about key leakage than anybody cracking it.  
139                          *
140                          */
141
142                         $res = openssl_pkey_new(array(
143                                 'digest_alg' => 'sha1',
144                                 'private_key_bits' => 4096,
145                                 'encrypt_key' => false )
146                         );
147
148                         $private_key = '';
149
150                         openssl_pkey_export($res, $private_key);
151
152                         $pubkey = openssl_pkey_get_details($res);
153                         $public_key = $pubkey["key"];
154
155                         // Save the private key. Send them the public key.
156
157                         $r = q("UPDATE `contact` SET `prvkey` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
158                                 dbesc($private_key),
159                                 intval($contact_id),
160                                 intval($uid) 
161                         );
162
163                         $params = array();
164
165                         /**
166                          *
167                          * Per the DFRN protocol, we will verify both ends by encrypting the dfrn_id with our 
168                          * site private key (person on the other end can decrypt it with our site public key).
169                          * Then encrypt our profile URL with the other person's site public key. They can decrypt
170                          * it with their site private key. If the decryption on the other end fails for either
171                          * item, it indicates tampering or key failure on at least one site and we will not be 
172                          * able to provide a secure communication pathway.
173                          *
174                          * If other site is willing to accept full encryption, (aes_allow is 1 AND we have php5.3 
175                          * or later) then we encrypt the personal public key we send them using AES-256-CBC and a 
176                          * random key which is encrypted with their site public key.  
177                          *
178                          */
179
180                         $src_aes_key = random_string();
181
182                         $result = '';
183                         openssl_private_encrypt($dfrn_id,$result,$user[0]['prvkey']);
184
185                         $params['dfrn_id'] = bin2hex($result);
186                         $params['public_key'] = $public_key;
187
188
189                         $my_url = $a->get_baseurl() . '/profile/' . $user[0]['nickname'];
190
191                         openssl_public_encrypt($my_url, $params['source_url'], $site_pubkey);
192                         $params['source_url'] = bin2hex($params['source_url']);
193
194                         if($aes_allow && function_exists('openssl_encrypt')) {
195                                 openssl_public_encrypt($src_aes_key, $params['aes_key'], $site_pubkey);
196                                 $params['aes_key'] = bin2hex($params['aes_key']);
197                                 $params['public_key'] = bin2hex(openssl_encrypt($public_key,'AES-256-CBC',$src_aes_key));
198                         }
199
200                         $params['dfrn_version'] = DFRN_PROTOCOL_VERSION ;
201                         if($duplex == 1)
202                                 $params['duplex'] = 1;
203
204                         logger('dfrn_confirm: Confirm: posting data to ' . $dfrn_confirm . ': ' . print_r($params,true), LOGGER_DATA);
205
206                         /**
207                          *
208                          * POST all this stuff to the other site.
209                          * Temporarily raise the network timeout to 120 seconds because the default 60
210                          * doesn't always give the other side quite enough time to decrypt everything.
211                          *
212                          */
213
214                         $a->config['system']['curl_timeout'] = 120;
215
216                         $res = post_url($dfrn_confirm,$params);
217
218                         logger('dfrn_confirm: Confirm: received data: ' . $res, LOGGER_DATA);
219
220                         // Now figure out what they responded. Try to be robust if the remote site is 
221                         // having difficulty and throwing up errors of some kind. 
222
223                         $leading_junk = substr($res,0,strpos($res,'<?xml'));
224
225                         $res = substr($res,strpos($res,'<?xml'));
226                         if(! strlen($res)) {
227
228                                         // No XML at all, this exchange is messed up really bad.
229                                         // We shouldn't proceed, because the xml parser might choke,
230                                         // and $status is going to be zero, which indicates success.
231                                         // We can hardly call this a success.  
232         
233                                 notice( t('Response from remote site was not understood.') . EOL);
234                                 return;
235                         }
236
237                         if(strlen($leading_junk) && get_config('system','debugging')) {
238         
239                                         // This might be more common. Mixed error text and some XML.
240                                         // If we're configured for debugging, show the text. Proceed in either case.
241
242                                 notice( t('Unexpected response from remote site: ') . EOL . $leading_junk . EOL );
243                         }
244
245                         $xml = parse_xml_string($res);
246                         $status = (int) $xml->status;
247                         $message = unxmlify($xml->message);   // human readable text of what may have gone wrong.
248                         switch($status) {
249                                 case 0:
250                                         notice( t("Confirmation completed successfully.") . EOL);
251                                         if(strlen($message))
252                                                 notice( t('Remote site reported: ') . $message . EOL);
253                                         break;
254                                 case 1:
255                                         // birthday paradox - generate new dfrn-id and fall through.
256                                         $new_dfrn_id = random_string();
257                                         $r = q("UPDATE contact SET `issued-id` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
258                                                 dbesc($new_dfrn_id),
259                                                 intval($contact_id),
260                                                 intval($uid) 
261                                         );
262
263                                 case 2:
264                                         notice( t("Temporary failure. Please wait and try again.") . EOL);
265                                         if(strlen($message))
266                                                 notice( t('Remote site reported: ') . $message . EOL);
267                                         break;
268
269
270                                 case 3:
271                                         notice( t("Introduction failed or was revoked.") . EOL);
272                                         if(strlen($message))
273                                                 notice( t('Remote site reported: ') . $message . EOL);
274                                         break;
275                                 }
276
277                         if(($status == 0) && ($intro_id)) {
278         
279                                 // Success. Delete the notification.
280         
281                                 $r = q("DELETE FROM `intro` WHERE `id` = %d AND `uid` = %d LIMIT 1",
282                                         intval($intro_id),
283                                         intval($uid)
284                                 );
285                                 
286                         }
287
288                         if($status != 0) 
289                                 return;
290                 }
291
292
293                 /*
294                  *
295                  * We have now established a relationship with the other site.
296                  * Let's make our own personal copy of their profile photo so we don't have
297                  * to always load it from their site.
298                  *
299                  * We will also update the contact record with the nature and scope of the relationship.
300                  *
301                  */
302
303                 require_once("Photo.php");
304
305                 $photos = import_profile_photo($contact['photo'],$uid,$contact_id);
306                 
307                 logger('dfrn_confirm: confirm - imported photos');
308
309                 if($network === 'dfrn') {
310
311                         $new_relation = CONTACT_IS_FOLLOWER;
312                         if(($relation == CONTACT_IS_SHARING) || ($duplex))
313                                 $new_relation = CONTACT_IS_FRIEND;
314
315                         if(($relation == CONTACT_IS_SHARING) && ($duplex))
316                                 $duplex = 0;
317
318                         $r = q("UPDATE `contact` SET `photo` = '%s', 
319                                 `thumb` = '%s',
320                                 `micro` = '%s', 
321                                 `rel` = %d, 
322                                 `name-date` = '%s', 
323                                 `uri-date` = '%s', 
324                                 `avatar-date` = '%s', 
325                                 `blocked` = 0, 
326                                 `pending` = 0,
327                                 `duplex` = %d,
328                                 `network` = 'dfrn' WHERE `id` = %d LIMIT 1
329                         ",
330                                 dbesc($photos[0]),
331                                 dbesc($photos[1]),
332                                 dbesc($photos[2]),
333                                 intval($new_relation),
334                                 dbesc(datetime_convert()),
335                                 dbesc(datetime_convert()),
336                                 dbesc(datetime_convert()),
337                                 intval($duplex),
338                                 intval($contact_id)
339                         );
340                 }
341                 else {  
342                         // $network !== 'dfrn'
343
344                         $network = (($contact['network']) ? $contact['network'] : 'stat');
345                         $notify = (($contact['notify']) ? $contact['notify'] : '');
346                         $poll   = (($contact['poll']) ? $contact['poll'] : '');
347
348                         if((! $contact['notify']) || (! $contact['poll'])) {
349                                 $arr = lrdd($contact['url']);
350                                 if(count($arr)) {
351                                         foreach($arr as $link) {
352                                                 if($link['@attributes']['rel'] === 'salmon')
353                                                         $notify = $link['@attributes']['href'];
354                                                 if($link['@attributes']['rel'] === NAMESPACE_FEED)
355                                                         $poll = $link['@attributes']['href'];
356                                         }
357                                 }
358                         }
359
360                         $r = q("DELETE FROM `intro` WHERE `id` = %d AND `uid` = %d LIMIT 1",
361                                 intval($intro_id),
362                                 intval($uid)
363                         );
364
365
366                         $r = q("UPDATE `contact` SET `photo` = '%s', 
367                                 `thumb` = '%s',
368                                 `micro` = '%s', 
369                                 `name-date` = '%s', 
370                                 `uri-date` = '%s', 
371                                 `avatar-date` = '%s', 
372                                 `notify` = '%s',
373                                 `poll` = '%s',
374                                 `blocked` = 0, 
375                                 `pending` = 0,
376                                 `network` = '%s'
377                                 WHERE `id` = %d LIMIT 1
378                         ",
379                                 dbesc($photos[0]),
380                                 dbesc($photos[1]),
381                                 dbesc($photos[2]),
382                                 dbesc(datetime_convert()),
383                                 dbesc(datetime_convert()),
384                                 dbesc(datetime_convert()),
385                                 dbesc($notify),
386                                 dbesc($poll),
387                                 dbesc($network),
388                                 intval($contact_id)
389                         );                      
390                 }
391
392                 if($r === false)
393                                 notice( t('Unable to set contact photo.') . EOL);
394
395                 // reload contact info
396
397                 $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
398                         intval($contact_id)
399                 );
400                 if(count($r))
401                         $contact = $r[0];
402                 else
403                         $contact = null;
404
405                 // Send a new friend post if we are allowed to...
406
407                 $r = q("SELECT `hide-friends` FROM `profile` WHERE `uid` = %d AND `is-default` = 1 LIMIT 1",
408                         intval($uid)
409                 );
410                 if((count($r)) && ($r[0]['hide-friends'] == 0) && (is_array($contact)) &&  isset($new_relation) && ($new_relation == CONTACT_IS_FRIEND)) {
411
412                         require_once('include/items.php');
413
414                         $self = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
415                                 intval($uid)
416                         );
417
418                         if(count($self)) {
419
420                                 $arr = array();
421                                 $arr['uri'] = $arr['parent-uri'] = item_new_uri($a->get_hostname(), $uid); 
422                                 $arr['uid'] = $uid;
423                                 $arr['contact-id'] = $self[0]['id'];
424                                 $arr['wall'] = 1;
425                                 $arr['type'] = 'wall';
426                                 $arr['gravity'] = 0;
427                                 $arr['author-name'] = $arr['owner-name'] = $self[0]['name'];
428                                 $arr['author-link'] = $arr['owner-link'] = $self[0]['url'];
429                                 $arr['author-avatar'] = $arr['owner-avatar'] = $self[0]['thumb'];
430                                 $arr['verb'] = ACTIVITY_FRIEND;
431                                 $arr['object-type'] = ACTIVITY_OBJ_PERSON;
432                                 
433                                 $A = '[url=' . $self[0]['url'] . ']' . $self[0]['name'] . '[/url]';
434                                 $B = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
435                                 $BPhoto = '[url=' . $contact['url'] . ']' . '[img]' . $contact['thumb'] . '[/img][/url]';
436                                 $arr['body'] =  sprintf( t('%1$s is now friends with %2$s'), $A, $B)."\n\n\n".$Bphoto;
437
438                                 $arr['object'] = '<object><type>' . ACTIVITY_OBJ_PERSON . '</type><title>' . $contact['name'] . '</title>'
439                                         . '<id>' . $contact['url'] . '/' . $contact['name'] . '</id>';
440                                 $arr['object'] .= '<link>' . xmlify('<link rel="alternate" type="text/html" href="' . $contact['url'] . '" />' . "\n");
441                                 $arr['object'] .= xmlify('<link rel="photo" type="image/jpeg" href="' . $contact['thumb'] . '" />' . "\n");
442                                 $arr['object'] .= '</link></object>' . "\n";
443                                 $arr['last-child'] = 1;
444
445                                 $arr['allow_cid'] = $user[0]['allow_cid'];
446                                 $arr['allow_gid'] = $user[0]['allow_gid'];
447                                 $arr['deny_cid']  = $user[0]['deny_cid'];
448                                 $arr['deny_gid']  = $user[0]['deny_gid'];
449
450                                 $i = item_store($arr);
451                                 if($i)
452                                 proc_run('php',"include/notifier.php","activity","$i");
453
454                         }
455
456                 }
457                 // Let's send our user to the contact editor in case they want to
458                 // do anything special with this new friend.
459
460                 if($handsfree === null)
461                         goaway($a->get_baseurl() . '/contacts/' . intval($contact_id));
462                 else
463                         return;  
464                 //NOTREACHED
465         }
466
467         /**
468          *
469          *
470          * End of Scenario 1. [Local confirmation of remote friend request].
471          *
472          * Begin Scenario 2. This is the remote response to the above scenario.
473          * This will take place on the site that originally initiated the friend request.
474          * In the section above where the confirming party makes a POST and 
475          * retrieves xml status information, they are communicating with the following code.
476          *
477          */
478
479         if(x($_POST,'source_url')) {
480
481                 // We are processing an external confirmation to an introduction created by our user.
482
483                 $public_key = ((x($_POST,'public_key'))   ? $_POST['public_key']           : '');
484                 $dfrn_id    = ((x($_POST,'dfrn_id'))      ? hex2bin($_POST['dfrn_id'])     : '');
485                 $source_url = ((x($_POST,'source_url'))   ? hex2bin($_POST['source_url'])  : '');
486                 $aes_key    = ((x($_POST,'aes_key'))      ? $_POST['aes_key']              : '');
487                 $duplex     = ((x($_POST,'duplex'))       ? intval($_POST['duplex'])       : 0 );
488                 $version_id = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 2.0);
489         
490                 logger('dfrn_confirm: requestee contacted: ' . $node);
491
492                 logger('dfrn_confirm: request: POST=' . print_r($_POST,true), LOGGER_DATA);
493
494                 // If $aes_key is set, both of these items require unpacking from the hex transport encoding.
495
496                 if(x($aes_key)) {
497                         $aes_key = hex2bin($aes_key);
498                         $public_key = hex2bin($public_key);
499                 }
500
501                 // Find our user's account
502
503                 $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' LIMIT 1",
504                         dbesc($node));
505
506                 if(! count($r)) {
507                         $message = sprintf(t('No user record found for \'%s\' '), $node);
508                         xml_status(3,$message); // failure
509                         // NOTREACHED
510                 }
511
512                 $my_prvkey = $r[0]['prvkey'];
513                 $local_uid = $r[0]['uid'];
514
515
516                 if(! strstr($my_prvkey,'PRIVATE KEY')) {
517                         $message = t('Our site encryption key is apparently messed up.');
518                         xml_status(3,$message);
519                 }
520
521                 // verify everything
522
523                 $decrypted_source_url = "";
524                 openssl_private_decrypt($source_url,$decrypted_source_url,$my_prvkey);
525
526
527                 if(! strlen($decrypted_source_url)) {
528                         $message = t('Empty site URL was provided or URL could not be decrypted by us.');
529                         xml_status(3,$message);
530                         // NOTREACHED
531                 }
532
533                 $ret = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
534                         dbesc($decrypted_source_url),
535                         intval($local_uid)
536                 );
537                 if(! count($ret)) {
538                         if(strstr($decrypted_source_url,'http:'))
539                                 $newurl = str_replace('http:','https:',$decrypted_source_url);
540                         else
541                                 $newurl = str_replace('https:','http:',$decrypted_source_url);
542
543                         $ret = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
544                                 dbesc($newurl),
545                                 intval($local_uid)
546                         );
547                         if(! count($r)) {
548                                 // this is either a bogus confirmation (?) or we deleted the original introduction.
549                                 $message = t('Contact record was not found for you on our site.');
550                                 xml_status(3,$message);
551                                 return; // NOTREACHED 
552                         }
553                 }
554
555                 $relation = $ret[0]['rel'];
556
557                 // Decrypt all this stuff we just received
558
559                 $foreign_pubkey = $ret[0]['site-pubkey'];
560                 $dfrn_record    = $ret[0]['id'];
561
562                 $decrypted_dfrn_id = "";
563                 openssl_public_decrypt($dfrn_id,$decrypted_dfrn_id,$foreign_pubkey);
564
565                 if(strlen($aes_key)) {
566                         $decrypted_aes_key = "";
567                         openssl_private_decrypt($aes_key,$decrypted_aes_key,$my_prvkey);
568                         $dfrn_pubkey = openssl_decrypt($public_key,'AES-256-CBC',$decrypted_aes_key);
569                 }
570                 else {
571                         $dfrn_pubkey = $public_key;
572                 }
573
574                 $r = q("SELECT * FROM `contact` WHERE `dfrn-id` = '%s' LIMIT 1",
575                         dbesc($decrypted_dfrn_id)
576                 );
577                 if(count($r)) {
578                         $message = t('The ID provided by your system is a duplicate on our system. It should work if you try again.');
579                         xml_status(1,$message); // Birthday paradox - duplicate dfrn-id
580                         // NOTREACHED
581                 }
582
583                 $r = q("UPDATE `contact` SET `dfrn-id` = '%s', `pubkey` = '%s' WHERE `id` = %d LIMIT 1",
584                         dbesc($decrypted_dfrn_id),
585                         dbesc($dfrn_pubkey),
586                         intval($dfrn_record)
587                 );
588                 if(! count($r)) {
589                         $message = t('Unable to set your contact credentials on our system.');
590                         xml_status(3,$message);
591                 }
592
593                 // We're good but now we have to scrape the profile photo and send notifications.
594
595
596
597                 $r = q("SELECT `photo` FROM `contact` WHERE `id` = %d LIMIT 1",
598                         intval($dfrn_record));
599
600                 if(count($r))
601                         $photo = $r[0]['photo'];
602                 else
603                         $photo = $a->get_baseurl() . '/images/default-profile.jpg';
604                                 
605                 require_once("Photo.php");
606
607                 $photos = import_profile_photo($photo,$local_uid,$dfrn_record);
608
609                 logger('dfrn_confirm: request - photos imported');
610
611                 $new_relation = CONTACT_IS_SHARING;
612                 if(($relation == CONTACT_IS_FOLLOWER) || ($duplex))
613                         $new_relation = CONTACT_IS_FRIEND;
614
615                 if(($relation == CONTACT_IS_FOLLOWER) && ($duplex))
616                         $duplex = 0;
617
618                 $r = q("UPDATE `contact` SET 
619                         `photo` = '%s', 
620                         `thumb` = '%s', 
621                         `micro` = '%s',
622                         `rel` = %d, 
623                         `name-date` = '%s', 
624                         `uri-date` = '%s', 
625                         `avatar-date` = '%s', 
626                         `blocked` = 0, 
627                         `pending` = 0,
628                         `duplex` = %d, 
629                         `network` = 'dfrn' WHERE `id` = %d LIMIT 1
630                 ",
631                         dbesc($photos[0]),
632                         dbesc($photos[1]),
633                         dbesc($photos[2]),
634                         intval($new_relation),
635                         dbesc(datetime_convert()),
636                         dbesc(datetime_convert()),
637                         dbesc(datetime_convert()),
638                         intval($duplex),
639                         intval($dfrn_record)
640                 );
641                 if($r === false) {    // indicates schema is messed up or total db failure
642                         $message = t('Unable to update your contact profile details on our system');
643                         xml_status(3,$message);
644                 }
645
646                 // Otherwise everything seems to have worked and we are almost done. Yay!
647                 // Send an email notification
648
649                 logger('dfrn_confirm: request: info updated');
650
651                 $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
652                         WHERE `contact`.`id` = %d LIMIT 1",
653                         intval($dfrn_record)
654                 );
655                 if((count($r)) && ($r[0]['notify-flags'] & NOTIFY_CONFIRM)) {
656
657                         push_lang($r[0]['language']);
658                         $tpl = (($new_relation == CONTACT_IS_FRIEND) 
659                                 ? get_intltext_template('friend_complete_eml.tpl')
660                                 : get_intltext_template('intro_complete_eml.tpl'));
661                 
662                         $email_tpl = replace_macros($tpl, array(
663                                 '$sitename' => $a->config['sitename'],
664                                 '$siteurl' =>  $a->get_baseurl(),
665                                 '$username' => $r[0]['username'],
666                                 '$email' => $r[0]['email'],
667                                 '$fn' => $r[0]['name'],
668                                 '$dfrn_url' => $r[0]['url'],
669                                 '$uid' => $newuid )
670                         );
671         
672                         $res = mail($r[0]['email'], sprintf( t("Connection accepted at %s") , $a->config['sitename']),
673                                 $email_tpl,
674                                 'From: ' . t('Administrator') . '@' . $_SERVER['SERVER_NAME'] . "\n"
675                                 . 'Content-type: text/plain; charset=UTF-8' . "\n"
676                                 . 'Content-transfer-encoding: 8bit' );
677
678                         if(!$res) {
679                                 // pointless throwing an error here and confusing the person at the other end of the wire.
680                         }
681                         pop_lang();
682                 }
683                 xml_status(0); // Success
684                 return; // NOTREACHED
685
686                         ////////////////////// End of this scenario ///////////////////////////////////////////////
687         }
688
689         // somebody arrived here by mistake or they are fishing. Send them to the homepage.
690
691         goaway(z_root());
692         // NOTREACHED
693
694 }