]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_notify.php
add nicknames to contact records (going forward and retroactive)
[friendica.git] / mod / dfrn_notify.php
1 <?php
2
3 require_once('simplepie/simplepie.inc');
4 require_once('include/items.php');
5
6
7 function dfrn_notify_post(&$a) {
8
9         $dfrn_id = notags(trim($_POST['dfrn_id']));
10         $dfrn_version = (float) $_POST['dfrn_version'];
11         $challenge = notags(trim($_POST['challenge']));
12         $data = $_POST['data'];
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                 xml_status(3);
26
27         $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
28                 dbesc($dfrn_id),
29                 dbesc($challenge)
30         );
31
32         // find the local user who owns this relationship.
33
34         $sql_extra = '';
35         switch($direction) {
36                 case (-1):
37                         $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
38                         break;
39                 case 0:
40                         $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
41                         break;
42                 case 1:
43                         $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
44                         break;
45                 default:
46                         xml_status(3);
47                         break; // NOTREACHED
48         }
49                  
50
51         $r = q("SELECT `contact`.*, `contact`.`uid` AS `importer_uid`, `user`.* FROM `contact` 
52                 LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` 
53                 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 
54                 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
55                 dbesc($a->argv[1])
56         );
57
58         if(! count($r)) {
59                 xml_status(3);
60                 //NOTREACHED
61         }
62
63         $importer = $r[0];
64
65
66         if($importer['readonly']) {
67                 // We aren't receiving stuff from this person. But we will quietly ignore them
68                 // rather than a blatant "go away" message.
69                 xml_status(0);
70                 //NOTREACHED
71         }
72
73         // Consume notification feed. This may differ from consuming a public feed in several ways
74         // - might contain email
75         // - might contain remote followup to our message
76         //              - in which case we need to accept it and then notify other conversants
77         // - we may need to send various email notifications
78
79         $feed = new SimplePie();
80         $feed->set_raw_data($data);
81         $feed->enable_order_by_date(false);
82         $feed->init();
83
84         $ismail = false;
85
86         $rawmail = $feed->get_feed_tags( NAMESPACE_DFRN, 'mail' );
87         if(isset($rawmail[0]['child'][NAMESPACE_DFRN])) {
88
89                 $ismail = true;
90                 $base = $rawmail[0]['child'][NAMESPACE_DFRN];
91
92                 $msg = array();
93                 $msg['uid'] = $importer['importer_uid'];
94                 $msg['from-name'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['name'][0]['data']));
95                 $msg['from-photo'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']));
96                 $msg['from-url'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['uri'][0]['data']));
97                 $msg['contact-id'] = $importer['id'];
98                 $msg['title'] = notags(unxmlify($base['subject'][0]['data']));
99                 $msg['body'] = escape_tags(unxmlify($base['content'][0]['data']));
100                 $msg['delivered'] = 1;
101                 $msg['seen'] = 0;
102                 $msg['replied'] = 0;
103                 $msg['uri'] = notags(unxmlify($base['id'][0]['data']));
104                 $msg['parent-uri'] = notags(unxmlify($base['in-reply-to'][0]['data']));
105                 $msg['created'] = datetime_convert(notags(unxmlify('UTC','UTC',$base['sentdate'][0]['data'])));
106                 
107                 dbesc_array($msg);
108
109                 $r = q("INSERT INTO `mail` (`" . implode("`, `", array_keys($msg)) 
110                         . "`) VALUES ('" . implode("', '", array_values($msg)) . "')" );
111
112                 // send email notification if requested.
113
114                 require_once('bbcode.php');
115                 if($importer['notify-flags'] & NOTIFY_MAIL) {
116                         $tpl = load_view_file('view/mail_received_eml.tpl');                    
117                         $email_tpl = replace_macros($tpl, array(
118                                 '$sitename' => $a->config['sitename'],
119                                 '$siteurl' =>  $a->get_baseurl(),
120                                 '$username' => $importer['username'],
121                                 '$email' => $importer['email'],
122                                 '$from' => $msg['from-name'],
123                                 '$title' => stripslashes($msg['title']),
124                                 '$body' => strip_tags(bbcode(stripslashes($msg['body'])))
125                         ));
126
127                         $res = mail($importer['email'], t('New mail received at ') . $a->config['sitename'],
128                                 $email_tpl, 'From: ' . t('Administrator') . '@' . $a->get_hostname() );
129                 }
130                 xml_status(0);
131                 // NOTREACHED
132         }       
133
134         foreach($feed->get_items() as $item) {
135
136                 $deleted = false;
137
138                 $rawdelete = $item->get_item_tags( NAMESPACE_TOMB , 'deleted-entry');
139                 if(isset($rawdelete[0]['attribs']['']['ref'])) {
140                         $uri = $rawthread[0]['attribs']['']['ref'];
141                         $deleted = true;
142                         if(isset($rawdelete[0]['attribs']['']['when'])) {
143                                 $when = $rawthread[0]['attribs']['']['when'];
144                                 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
145                         }
146                         else
147                                 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
148                 }
149                 if($deleted) {
150                         $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
151                                 dbesc($uri),
152                                 intval($importer['importer_uid'])
153                         );
154                         if(count($r)) {
155                                 $item = $r[0];
156                                 if($item['uri'] == $item['parent-uri']) {
157                                         $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s'
158                                                 WHERE `parent-uri` = '%s' AND `uid` = %d",
159                                                 dbesc($when),
160                                                 dbesc(datetime_convert()),
161                                                 dbesc($item['uri']),
162                                                 intval($importer['importer_uid'])
163                                         );
164                                 }
165                                 else {
166                                         $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s' 
167                                                 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
168                                                 dbesc($when),
169                                                 dbesc(datetime_convert()),
170                                                 dbesc($uri),
171                                                 intval($importer['importer_uid'])
172                                         );
173                                         if($item['last-child']) {
174                                                 // ensure that last-child is set in case the comment that had it just got wiped.
175                                                 $q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
176                                                         dbesc(datetime_convert()),
177                                                         dbesc($item['parent-uri']),
178                                                         intval($item['uid'])
179                                                 );
180                                                 // who is the last child now? 
181                                                 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d
182                                                         ORDER BY `created` DESC LIMIT 1",
183                                                                 dbesc($item['parent-uri']),
184                                                                 intval($importer['importer_uid'])
185                                                 );
186                                                 if(count($r)) {
187                                                         q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
188                                                                 intval($r[0]['id'])
189                                                         );
190                                                 }       
191                                         }
192                                 }
193                         }       
194                         continue;
195                 }
196
197                 $is_reply = false;              
198                 $item_id = $item->get_id();
199                 $rawthread = $item->get_item_tags( NAMESPACE_THREAD, 'in-reply-to');
200                 if(isset($rawthread[0]['attribs']['']['ref'])) {
201                         $is_reply = true;
202                         $parent_uri = $rawthread[0]['attribs']['']['ref'];
203                 }
204
205                 if($is_reply) {
206                         if($feed->get_item_quantity() == 1) {
207                                 // remote reply to our post. Import and then notify everybody else.
208                                 $datarray = get_atom_elements($item);
209                                 $datarray['type'] = 'remote-comment';
210                                 $datarray['wall'] = 1;
211                                 $datarray['parent-uri'] = $parent_uri;
212                                 $datarray['uid'] = $importer['importer_uid'];
213                                 $datarray['contact-id'] = $importer['id'];
214                                 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
215                                         $datarray['type'] = 'activity';
216                                         $datarray['gravity'] = GRAVITY_LIKE;
217                                 }
218                                 $posted_id = item_store($datarray);
219
220                                 if($posted_id) {
221                                         $r = q("SELECT `parent` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
222                                                 intval($posted_id),
223                                                 intval($importer['importer_uid'])
224                                         );
225                                         if(count($r)) {
226                                                 $r1 = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `uid` = %d AND `parent` = %d",
227                                                         dbesc(datetime_convert()),
228                                                         intval($importer['importer_uid']),
229                                                         intval($r[0]['parent'])
230                                                 );
231                                         }
232                                         $r2 = q("UPDATE `item` SET `last-child` = 1, `changed` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
233                                                         dbesc(datetime_convert()),
234                                                         intval($importer['importer_uid']),
235                                                         intval($posted_id)
236                                         );
237
238                                         if($datarray['type'] == 'remote-comment') {
239                                                 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
240                                                 $proc_debug = get_config('system','proc_debug');
241
242
243                                                 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"comment-import\" \"$posted_id\" $proc_debug &", 
244                                                         array(),$foo));
245
246                                                 if(($importer['notify-flags'] & NOTIFY_COMMENT) && (! $importer['self'])) {
247                                                         require_once('bbcode.php');
248                                                         $from = stripslashes($datarray['author-name']);
249                                                         $tpl = load_view_file('view/cmnt_received_eml.tpl');                    
250                                                         $email_tpl = replace_macros($tpl, array(
251                                                                 '$sitename' => $a->config['sitename'],
252                                                                 '$siteurl' =>  $a->get_baseurl(),
253                                                                 '$username' => $importer['username'],
254                                                                 '$email' => $importer['email'],
255                                                                 '$from' => $from,
256                                                         '$body' => strip_tags(bbcode(stripslashes($datarray['body'])))
257                                                         ));
258         
259                                                         $res = mail($importer['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
260                                                                 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
261                                                 }
262                                         }
263                                 }
264                                 xml_status(0);
265                                 // NOTREACHED
266
267                         }
268                         else {
269                                 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
270
271                                 $item_id = $item->get_id();
272
273                                 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
274                                         dbesc($item_id),
275                                         intval($importer['importer_uid'])
276                                 );
277                                 // FIXME update content if 'updated' changes
278                                 if(count($r)) {
279                                         $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
280                                         if($allow && $allow[0]['data'] != $r[0]['last-child']) {
281                                                 $r = q("UPDATE `item` SET `last-child` = %d, `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
282                                                         intval($allow[0]['data']),
283                                                         dbesc(datetime_convert()),
284                                                         dbesc($item_id),
285                                                         intval($importer['importer_uid'])
286                                                 );
287                                         }
288                                         continue;
289                                 }
290                                 $datarray = get_atom_elements($item);
291                                 $datarray['parent-uri'] = $parent_uri;
292                                 $datarray['uid'] = $importer['importer_uid'];
293                                 $datarray['contact-id'] = $importer['id'];
294                                 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
295                                         $datarray['type'] = 'activity';
296                                         $datarray['gravity'] = GRAVITY_LIKE;
297                                 }
298
299                                 $r = item_store($datarray);
300
301                                 // find out if our user is involved in this conversation and wants to be notified.
302                         
303                                 if(($datarray['type'] != 'activity') && ($importer['notify-flags'] & NOTIFY_COMMENT)) {
304
305                                         $myconv = q("SELECT `author-link` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d",
306                                                 dbesc($parent_uri),
307                                                 intval($importer['importer_uid'])
308                                         );
309                                         if(count($myconv)) {
310                                                 foreach($myconv as $conv) {
311                                                         if($conv['author-link'] != $importer['url'])
312                                                                 continue;
313                                                         require_once('bbcode.php');
314                                                         $from = stripslashes($datarray['author-name']);
315                                                         $tpl = load_view_file('view/cmnt_received_eml.tpl');                    
316                                                         $email_tpl = replace_macros($tpl, array(
317                                                                 '$sitename' => $a->config['sitename'],
318                                                                 '$siteurl' =>  $a->get_baseurl(),
319                                                                 '$username' => $importer['username'],
320                                                                 '$email' => $importer['email'],
321                                                                 '$from' => $from,
322                                                                 '$body' => strip_tags(bbcode(stripslashes($datarray['body'])))
323                                                         ));
324
325                                                         $res = mail($importer['email'], $from . t(" commented on an item at ") 
326                                                                 . $a->config['sitename'],
327                                                                 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
328                                                         break;
329                                                 }
330                                         }
331                                 }
332                                 continue;
333                         }
334                 }
335                 else {
336                         // Head post of a conversation. Have we seen it? If not, import it.
337
338                         $item_id = $item->get_id();
339                         $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
340                                 dbesc($item_id),
341                                 intval($importer['importer_uid'])
342                         );
343                         if(count($r)) {
344                                 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
345                                 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
346                                         $r = q("UPDATE `item` SET `last-child` = %d, `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
347                                                 intval($allow[0]['data']),
348                                                 dbesc(datetime_convert()),
349                                                 dbesc($item_id),
350                                                 intval($importer['importer_uid'])
351                                         );
352                                 }
353                                 continue;
354                         }
355
356
357                         $datarray = get_atom_elements($item);
358                         $datarray['parent-uri'] = $item_id;
359                         $datarray['uid'] = $importer['importer_uid'];
360                         $datarray['contact-id'] = $importer['id'];
361                         $r = item_store($datarray);
362                         continue;
363                 }
364         }
365
366         xml_status(0);
367         // NOTREACHED
368
369 }
370
371
372 function dfrn_notify_content(&$a) {
373
374         if(x($_GET,'dfrn_id')) {
375
376                 // initial communication from external contact, $direction is their direction.
377                 // If this is a duplex communication, ours will be the opposite.
378
379                 $dfrn_id = notags(trim($_GET['dfrn_id']));
380                 $dfrn_version = (float) $_GET['dfrn_version'];
381
382
383                 $direction = (-1);
384                 if(strpos($dfrn_id,':') == 1) {
385                         $direction = intval(substr($dfrn_id,0,1));
386                         $dfrn_id = substr($dfrn_id,2);
387                 }
388
389                 $hash = random_string();
390
391                 $status = 0;
392
393                 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
394
395                 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
396                         VALUES( '%s', '%s', '%s') ",
397                         dbesc($hash),
398                         dbesc($dfrn_id),
399                         intval(time() + 60 )
400                 );
401
402
403                 $sql_extra = '';
404                 switch($direction) {
405                         case (-1):
406                                 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
407                                 $my_id = $dfrn_id;
408                                 break;
409                         case 0:
410                                 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
411                                 $my_id = '1:' . $dfrn_id;
412                                 break;
413                         case 1:
414                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
415                                 $my_id = '0:' . $dfrn_id;
416                                 break;
417                         default:
418                                 $status = 1;
419                                 break; // NOTREACHED
420                 }
421
422                 $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` 
423                                 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
424                                 dbesc($a->argv[1])
425                 );
426
427                 if(! count($r))
428                         $status = 1;
429
430                 $challenge = '';
431                 $encrypted_id = '';
432                 $id_str = $my_id . '.' . mt_rand(1000,9999);
433
434                 if((($r[0]['duplex']) && strlen($r[0]['pubkey'])) || (! strlen($r[0]['prvkey']))) {
435                         openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
436                         openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
437                 }
438                 else {
439                         openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
440                         openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
441                 }
442
443                 $challenge    = bin2hex($challenge);
444                 $encrypted_id = bin2hex($encrypted_id);
445
446                 header("Content-type: text/xml");
447
448                 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n" 
449                         . '<dfrn_notify>' . "\r\n"
450                         . "\t" . '<status>' . $status . '</status>' . "\r\n"
451                         . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
452                         . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n" 
453                         . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
454                         . '</dfrn_notify>' . "\r\n" ;
455
456                 killme();
457         }
458
459 }