]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_notify.php
fixed a bbcode issue with rich text html messages, fixed some issues with plain text.
[friendica.git] / mod / dfrn_notify.php
1 <?php
2
3 require_once('simplepie/simplepie.inc');
4 require_once('include/items.php');
5 function dfrn_notify_post(&$a) {
6
7         $dfrn_id      = ((x($_POST,'dfrn_id'))      ? notags(trim($_POST['dfrn_id']))   : '');
8         $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version']    : 2.0);
9         $challenge    = ((x($_POST,'challenge'))    ? notags(trim($_POST['challenge'])) : '');
10         $data         = ((x($_POST,'data'))         ? $_POST['data']                    : '');
11         $key          = ((x($_POST,'key'))          ? $_POST['key']                     : '');
12         $dissolve     = ((x($_POST,'dissolve'))     ? intval($_POST['dissolve'])        :  0);
13
14         $direction = (-1);
15         if(strpos($dfrn_id,':') == 1) {
16                 $direction = intval(substr($dfrn_id,0,1));
17                 $dfrn_id = substr($dfrn_id,2);
18         }
19
20         $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
21                 dbesc($dfrn_id),
22                 dbesc($challenge)
23         );
24         if(! count($r)) {
25                 logger('dfrn_notify: could not match challenge to dfrn_id ' . $dfrn_id . ' challenge=' . $challenge);
26                 xml_status(3);
27         }
28
29         $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
30                 dbesc($dfrn_id),
31                 dbesc($challenge)
32         );
33
34         // find the local user who owns this relationship.
35
36         $sql_extra = '';
37         switch($direction) {
38                 case (-1):
39                         $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
40                         break;
41                 case 0:
42                         $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
43                         break;
44                 case 1:
45                         $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
46                         break;
47                 default:
48                         xml_status(3);
49                         break; // NOTREACHED
50         }
51                  
52
53         $r = q("SELECT  `contact`.*, `contact`.`uid` AS `importer_uid`, 
54                                         `contact`.`pubkey` AS `cpubkey`, 
55                                         `contact`.`prvkey` AS `cprvkey`, 
56                                         `contact`.`thumb` AS `thumb`, 
57                                         `contact`.`url` as `url`,
58                                         `contact`.`name` as `senderName`,
59                                         `user`.* 
60                         FROM `contact` 
61                         LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` 
62                         WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 
63                                 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
64                 dbesc($a->argv[1])
65         );
66
67         if(! count($r)) {
68                 logger('dfrn_notify: contact not found for dfrn_id ' . $dfrn_id);
69                 xml_status(3);
70                 //NOTREACHED
71         }
72
73         // $importer in this case contains the contact record for the remote contact joined with the user record of our user. 
74
75         $importer = $r[0];
76
77         logger('dfrn_notify: received notify from ' . $importer['name'] . ' for ' . $importer['username']);
78         logger('dfrn_notify: data: ' . $data, LOGGER_DATA);
79
80         if($dissolve == 1) {
81
82                 /**
83                  * Relationship is dissolved permanently
84                  */
85
86                 require_once('include/Contact.php'); 
87                 contact_remove($importer['id']);
88                 logger('relationship dissolved : ' . $importer['name'] . ' dissolved ' . $importer['username']);
89                 xml_status(0);
90
91         }
92
93         if(strlen($key)) {
94                 $rawkey = hex2bin(trim($key));
95                 logger('rino: md5 raw key: ' . md5($rawkey));
96                 $final_key = '';
97
98                 if($dfrn_version >= 2.1) {
99                         if((($importer['duplex']) && strlen($importer['cprvkey'])) || (! strlen($importer['cpubkey']))) {
100                                 openssl_private_decrypt($rawkey,$final_key,$importer['cprvkey']);
101                         }
102                         else {
103                                 openssl_public_decrypt($rawkey,$final_key,$importer['cpubkey']);
104                         }
105                 }
106                 else {
107                         if((($importer['duplex']) && strlen($importer['cpubkey'])) || (! strlen($importer['cprvkey']))) {
108                                 openssl_public_decrypt($rawkey,$final_key,$importer['cpubkey']);
109                         }
110                         else {
111                                 openssl_private_decrypt($rawkey,$final_key,$importer['cprvkey']);
112                         }
113                 }
114
115                 logger('rino: received key : ' . $final_key);
116                 $data = aes_decrypt(hex2bin($data),$final_key);
117                 logger('rino: decrypted data: ' . $data, LOGGER_DATA);
118         }
119
120
121
122
123         if($importer['readonly']) {
124                 // We aren't receiving stuff from this person. But we will quietly ignore them
125                 // rather than a blatant "go away" message.
126                 logger('dfrn_notify: ignoring');
127                 xml_status(0);
128                 //NOTREACHED
129         }
130
131         // Consume notification feed. This may differ from consuming a public feed in several ways
132         // - might contain email
133         // - might contain remote followup to our message
134         //              - in which case we need to accept it and then notify other conversants
135         // - we may need to send various email notifications
136
137         $feed = new SimplePie();
138         $feed->set_raw_data($data);
139         $feed->enable_order_by_date(false);
140         $feed->init();
141
142         $ismail = false;
143
144         $rawmail = $feed->get_feed_tags( NAMESPACE_DFRN, 'mail' );
145         if(isset($rawmail[0]['child'][NAMESPACE_DFRN])) {
146
147                 logger('dfrn_notify: private message received');
148
149                 $ismail = true;
150                 $base = $rawmail[0]['child'][NAMESPACE_DFRN];
151
152                 $msg = array();
153                 $msg['uid'] = $importer['importer_uid'];
154                 $msg['from-name'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['name'][0]['data']));
155                 $msg['from-photo'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']));
156                 $msg['from-url'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['uri'][0]['data']));
157                 $msg['contact-id'] = $importer['id'];
158                 $msg['title'] = notags(unxmlify($base['subject'][0]['data']));
159                 $msg['body'] = escape_tags(unxmlify($base['content'][0]['data']));
160                 $msg['seen'] = 0;
161                 $msg['replied'] = 0;
162                 $msg['uri'] = notags(unxmlify($base['id'][0]['data']));
163                 $msg['parent-uri'] = notags(unxmlify($base['in-reply-to'][0]['data']));
164                 $msg['created'] = datetime_convert(notags(unxmlify('UTC','UTC',$base['sentdate'][0]['data'])));
165                 
166                 dbesc_array($msg);
167
168                 $r = dbq("INSERT INTO `mail` (`" . implode("`, `", array_keys($msg)) 
169                         . "`) VALUES ('" . implode("', '", array_values($msg)) . "')" );
170
171                 // send email notification if requested.
172
173                 require_once('bbcode.php');
174                 if($importer['notify-flags'] & NOTIFY_MAIL) {
175
176 //                      $body = html_entity_decode(strip_tags(bbcode(stripslashes(nl2br($msg['body'])))),ENT_QUOTES,'UTF-8');
177 //                      $body = strip_tags(bbcode(stripslashes(nl2br($msg['body']))));
178                         
179                         /*if(function_exists('quoted_printable_encode'))
180                                 $body = quoted_printable_encode($body);
181                         else
182                                 $body = qp($body);*/
183
184                         //$msg['body'] = str_replace(array("\\r\\n", "\\r", "\\n"), "<br />", $msg['body']);
185                         //$msg['body'] = html_entity_decode(strip_tags(bbcode($msg['body'])));  
186                         $msg['mimeboundary']   =rand(0,9)."-"
187                                 .rand(10000000000,9999999999)."-"
188                                 .rand(10000000000,9999999999)."=:"
189                                 .rand(10000,99999);
190
191                         $msg['notificationfromname']    = t('Administrator');
192                         $msg['notificationfromemail']   = t('noreply') . '@' . $a->get_hostname();                              
193                         $msg['headers'] =
194                                 "From: {$msg['notificationfromname']} <{$msg['notificationfromemail']}>\n" . 
195                                 "Reply-To: {$msg['notificationfromemail']}\n" .
196                                 "MIME-Version: 1.0\n" .
197                                 "Content-Type: multipart/alternative; boundary=\"{$msg['mimeboundary']}\"";
198
199
200                         $msg['textversion']
201                                 = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",$msg['body']))),ENT_QUOTES,'UTF-8'));
202                         //$TextMessage  = html_entity_decode(strip_tags(bbcode(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",$msg['body']))));        ;
203                         $msg['htmlversion']     
204                                 = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "<br />\n",$msg['body']))));
205                                                                 
206                         $tpl = load_view_file('view/mail_received_eml.tpl');                    
207                         $email_tpl = replace_macros($tpl, array(
208                                 '$siteName'             => $a->config['sitename'],
209                                 '$siteurl'              => $a->get_baseurl(),
210                                 '$username'             => $importer['username'],
211                                 '$thumb'                => $importer['thumb'],
212                                 '$email'                => $importer['email'],
213                                 '$url'                  => $importer['url'],
214                                 '$senderName'   => $importer['senderName'],
215                                 '$from'                 => $msg['from-name'],
216                                 '$title'                => stripslashes($msg['title']),
217                                 '$textversion'  => $msg['textversion'],
218                                 '$htmlversion'  => $msg['htmlversion'],
219                                 '$mimeboundary' => $msg['mimeboundary'],
220                                 '$hostname'             => $a->get_hostname()
221                         ));
222                         
223                         logger("message headers: " . $msg['headers']);
224                         logger("message body: " . $mail_tpl);
225                         
226                         
227                         $res = mail($importer['email'], t('New mail received at ') . $a->config['sitename'],
228                                 $email_tpl, $msg['headers']
229                         /*              'From: ' . t('Administrator') . '@' . $a->get_hostname() . "\r\n"
230                                         . 'MIME-Version: 1.0' . "\r\n"
231                                         . 'Content-type: text/html; charset=utf-8' . "\r\n" 
232                                         . 'Content-transfer-encoding: quoted-printable' . "\r\n" */
233                         );
234                 }
235                 xml_status(0);
236                 // NOTREACHED
237         }       
238         
239         logger('dfrn_notify: feed item count = ' . $feed->get_item_quantity());
240
241         // process any deleted entries
242
243         $del_entries = $feed->get_feed_tags(NAMESPACE_TOMB, 'deleted-entry');
244         if(is_array($del_entries) && count($del_entries)) {
245                 foreach($del_entries as $dentry) {
246                         $deleted = false;
247                         if(isset($dentry['attribs']['']['ref'])) {
248                                 $uri = $dentry['attribs']['']['ref'];
249                                 $deleted = true;
250                                 if(isset($dentry['attribs']['']['when'])) {
251                                         $when = $dentry['attribs']['']['when'];
252                                         $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
253                                 }
254                                 else
255                                         $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
256                         }
257                         if($deleted) {
258                                 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
259                                         dbesc($uri),
260                                         intval($importer['importer_uid'])
261                                 );
262                                 if(count($r)) {
263                                         $item = $r[0];
264
265                                         if(! $item['deleted'])
266                                                 logger('dfrn_notify: deleting item ' . $item['id'] . ' uri=' . $item['uri'], LOGGER_DEBUG);
267
268                                         if($item['uri'] == $item['parent-uri']) {
269                                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s'
270                                                         WHERE `parent-uri` = '%s' AND `uid` = %d",
271                                                         dbesc($when),
272                                                         dbesc(datetime_convert()),
273                                                         dbesc($item['uri']),
274                                                         intval($importer['importer_uid'])
275                                                 );
276                                         }
277                                         else {
278                                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s' 
279                                                         WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
280                                                         dbesc($when),
281                                                         dbesc(datetime_convert()),
282                                                         dbesc($uri),
283                                                         intval($importer['importer_uid'])
284                                                 );
285                                                 if($item['last-child']) {
286                                                         // ensure that last-child is set in case the comment that had it just got wiped.
287                                                         q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
288                                                                 dbesc(datetime_convert()),
289                                                                 dbesc($item['parent-uri']),
290                                                                 intval($item['uid'])
291                                                         );
292                                                         // who is the last child now? 
293                                                         $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d
294                                                                 ORDER BY `created` DESC LIMIT 1",
295                                                                         dbesc($item['parent-uri']),
296                                                                         intval($importer['importer_uid'])
297                                                         );
298                                                         if(count($r)) {
299                                                                 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
300                                                                         intval($r[0]['id'])
301                                                                 );
302                                                         }       
303                                                 }
304                                         }       
305                                 }
306                         }
307                 }
308         }
309
310
311         foreach($feed->get_items() as $item) {
312
313                 $is_reply = false;              
314                 $item_id = $item->get_id();
315                 $rawthread = $item->get_item_tags( NAMESPACE_THREAD, 'in-reply-to');
316                 if(isset($rawthread[0]['attribs']['']['ref'])) {
317                         $is_reply = true;
318                         $parent_uri = $rawthread[0]['attribs']['']['ref'];
319                 }
320
321                 if($is_reply) {
322                         if($feed->get_item_quantity() == 1) {
323                                 logger('dfrn_notify: received remote comment');
324                                 $is_like = false;
325                                 // remote reply to our post. Import and then notify everybody else.
326                                 $datarray = get_atom_elements($feed,$item);
327                                 $datarray['type'] = 'remote-comment';
328                                 $datarray['wall'] = 1;
329                                 $datarray['parent-uri'] = $parent_uri;
330                                 $datarray['uid'] = $importer['importer_uid'];
331                                 $datarray['contact-id'] = $importer['id'];
332                                 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
333                                         $is_like = true;
334                                         $datarray['type'] = 'activity';
335                                         $datarray['gravity'] = GRAVITY_LIKE;
336                                         $datarray['last-child'] = 0;
337                                 }
338                                 $posted_id = item_store($datarray);
339                                 $parent = 0;
340
341                                 if($posted_id) {
342                                         $r = q("SELECT `parent` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
343                                                 intval($posted_id),
344                                                 intval($importer['importer_uid'])
345                                         );
346                                         if(count($r))
347                                                 $parent = $r[0]['parent'];
348                         
349                                         if(! $is_like) {
350                                                 $r1 = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `uid` = %d AND `parent` = %d",
351                                                         dbesc(datetime_convert()),
352                                                         intval($importer['importer_uid']),
353                                                         intval($r[0]['parent'])
354                                                 );
355
356                                                 $r2 = q("UPDATE `item` SET `last-child` = 1, `changed` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
357                                                         dbesc(datetime_convert()),
358                                                         intval($importer['importer_uid']),
359                                                         intval($posted_id)
360                                                 );
361                                         }
362
363                                         if($posted_id && $parent) {
364                                 
365                                                 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
366
367                                                 proc_run($php_path,"include/notifier.php","comment-import","$posted_id");
368                                         
369                                                 if((! $is_like) && ($importer['notify-flags'] & NOTIFY_COMMENT) && (! $importer['self'])) {
370                                                         require_once('bbcode.php');
371                                                         $from = stripslashes($datarray['author-name']);
372                                                         $tpl = load_view_file('view/cmnt_received_eml.tpl');                    
373                                                         $email_tpl = replace_macros($tpl, array(
374                                                                 '$sitename' => $a->config['sitename'],
375                                                                 '$siteurl' =>  $a->get_baseurl(),
376                                                                 '$username' => $importer['username'],
377                                                                 '$email' => $importer['email'],
378                                                                 '$display' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id, 
379                                                                 '$from' => $from,
380                                                                 '$body' => strip_tags(bbcode(stripslashes($datarray['body'])))
381                                                         ));
382         
383                                                         $res = mail($importer['email'], $from . t(' commented on an item at ') . $a->config['sitename'],
384                                                                 $email_tpl, "From: " . t('Administrator') . '@' . $a->get_hostname() );
385                                                 }
386                                         }
387                                         xml_status(0);
388                                         // NOTREACHED
389                                 }
390                         }
391                         else {
392                                 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
393
394                                 $item_id = $item->get_id();
395
396                                 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
397                                         dbesc($item_id),
398                                         intval($importer['importer_uid'])
399                                 );
400                                 // FIXME update content if 'updated' changes
401                                 if(count($r)) {
402                                         $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
403                                         if($allow && $allow[0]['data'] != $r[0]['last-child']) {
404                                                 $r = q("UPDATE `item` SET `last-child` = %d, `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
405                                                         intval($allow[0]['data']),
406                                                         dbesc(datetime_convert()),
407                                                         dbesc($item_id),
408                                                         intval($importer['importer_uid'])
409                                                 );
410                                         }
411                                         continue;
412                                 }
413                                 $datarray = get_atom_elements($feed,$item);
414                                 $datarray['parent-uri'] = $parent_uri;
415                                 $datarray['uid'] = $importer['importer_uid'];
416                                 $datarray['contact-id'] = $importer['id'];
417                                 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
418                                         $datarray['type'] = 'activity';
419                                         $datarray['gravity'] = GRAVITY_LIKE;
420                                 }
421                                 $r = item_store($datarray);
422
423                                 // find out if our user is involved in this conversation and wants to be notified.
424                         
425                                 if(($datarray['type'] != 'activity') && ($importer['notify-flags'] & NOTIFY_COMMENT)) {
426
427                                         $myconv = q("SELECT `author-link` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 ",
428                                                 dbesc($parent_uri),
429                                                 intval($importer['importer_uid'])
430                                         );
431                                         if(count($myconv)) {
432                                                 $importer_url = $a->get_baseurl() . '/profile/' . $importer['nickname'];
433                                                 foreach($myconv as $conv) {
434                                                         if(! link_compare($conv['author-link'],$importer_url))
435                                                                 continue;
436                                                         require_once('bbcode.php');
437                                                         $from = stripslashes($datarray['author-name']);
438                                                         $tpl = load_view_file('view/cmnt_received_eml.tpl');    
439                                                         $email_tpl = replace_macros($tpl, array(
440                                                                 '$sitename' => $a->config['sitename'],
441                                                                 '$siteurl' =>  $a->get_baseurl(),
442                                                                 '$username' => $importer['username'],
443                                                                 '$email' => $importer['email'],
444                                                                 '$from' => $from,
445                                                                 '$display' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $r,
446                                                                 '$body' => strip_tags(bbcode(stripslashes($datarray['body'])))
447                                                         ));
448
449                                                         $res = mail($importer['email'], $from . t(" commented on an item at ") 
450                                                                 . $a->config['sitename'],
451                                                                 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
452                                                         break;
453                                                 }
454                                         }
455                                 }
456                                 continue;
457                         }
458                 }
459                 else {
460                         // Head post of a conversation. Have we seen it? If not, import it.
461
462                         $item_id = $item->get_id();
463                         $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
464                                 dbesc($item_id),
465                                 intval($importer['importer_uid'])
466                         );
467                         if(count($r)) {
468                                 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
469                                 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
470                                         $r = q("UPDATE `item` SET `last-child` = %d, `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
471                                                 intval($allow[0]['data']),
472                                                 dbesc(datetime_convert()),
473                                                 dbesc($item_id),
474                                                 intval($importer['importer_uid'])
475                                         );
476                                 }
477                                 continue;
478                         }
479
480
481                         $datarray = get_atom_elements($feed,$item);
482                         $datarray['parent-uri'] = $item_id;
483                         $datarray['uid'] = $importer['importer_uid'];
484                         $datarray['contact-id'] = $importer['id'];
485                         $r = item_store($datarray);
486                         continue;
487                 }
488         }
489
490         xml_status(0);
491         // NOTREACHED
492
493 }
494
495
496 function dfrn_notify_content(&$a) {
497
498         if(x($_GET,'dfrn_id')) {
499
500                 // initial communication from external contact, $direction is their direction.
501                 // If this is a duplex communication, ours will be the opposite.
502
503                 $dfrn_id = notags(trim($_GET['dfrn_id']));
504                 $dfrn_version = (float) $_GET['dfrn_version'];
505
506                 logger('dfrn_notify: new notification dfrn_id=' . $dfrn_id);
507
508                 $direction = (-1);
509                 if(strpos($dfrn_id,':') == 1) {
510                         $direction = intval(substr($dfrn_id,0,1));
511                         $dfrn_id = substr($dfrn_id,2);
512                 }
513
514                 $hash = random_string();
515
516                 $status = 0;
517
518                 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
519
520                 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
521                         VALUES( '%s', '%s', %d ) ",
522                         dbesc($hash),
523                         dbesc($dfrn_id),
524                         intval(time() + 90 )
525                 );
526
527                 logger('dfrn_notify: challenge=' . $hash );
528
529                 $sql_extra = '';
530                 switch($direction) {
531                         case (-1):
532                                 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
533                                 $my_id = $dfrn_id;
534                                 break;
535                         case 0:
536                                 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
537                                 $my_id = '1:' . $dfrn_id;
538                                 break;
539                         case 1:
540                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
541                                 $my_id = '0:' . $dfrn_id;
542                                 break;
543                         default:
544                                 $status = 1;
545                                 break; // NOTREACHED
546                 }
547
548                 $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` 
549                                 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
550                                 dbesc($a->argv[1])
551                 );
552
553                 if(! count($r))
554                         $status = 1;
555
556                 $challenge = '';
557                 $encrypted_id = '';
558                 $id_str = $my_id . '.' . mt_rand(1000,9999);
559
560                 if((($r[0]['duplex']) && strlen($r[0]['prvkey'])) || (! strlen($r[0]['pubkey']))) {
561                         openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
562                         openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
563                 }
564                 else {
565                         openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
566                         openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
567                 }
568
569                 $challenge    = bin2hex($challenge);
570                 $encrypted_id = bin2hex($encrypted_id);
571
572                 $rino = ((function_exists('mcrypt_encrypt')) ? 1 : 0);
573
574                 $rino_enable = get_config('system','rino_encrypt');
575
576                 if(! $rino_enable)
577                         $rino = 0;
578
579
580                 header("Content-type: text/xml");
581
582                 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n" 
583                         . '<dfrn_notify>' . "\r\n"
584                         . "\t" . '<status>' . $status . '</status>' . "\r\n"
585                         . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
586                         . "\t" . '<rino>' . $rino . '</rino>' . "\r\n" 
587                         . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n" 
588                         . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
589                         . '</dfrn_notify>' . "\r\n" ;
590
591                 killme();
592         }
593
594 }