]> git.mxchange.org Git - friendica.git/blob - include/notifier.php
local_deliver testing
[friendica.git] / include / notifier.php
1 <?php
2
3 require_once("boot.php");
4
5 /*
6  * This file was at one time responsible for doing all deliveries, but this caused
7  * big problems on shared hosting systems, where the process might get killed by the 
8  * hosting provider and nothing would get delivered. 
9  * It now only delivers one message under certain cases, and invokes a queued
10  * delivery mechanism (include/deliver.php) to deliver individual contacts at 
11  * controlled intervals.
12  * This has a much better chance of surviving random processes getting killed
13  * by the hosting provider. 
14  * A lot of this code is duplicated in include/deliver.php until we have time to go back
15  * and re-structure the delivery procedure based on the obstacles that have been thrown at 
16  * us by hosting providers. 
17  */
18
19 function notifier_run($argv, $argc){
20         global $a, $db;
21
22         if(is_null($a)){
23                 $a = new App;
24         }
25   
26         if(is_null($db)) {
27                 @include(".htconfig.php");
28                 require_once("dba.php");
29                 $db = new dba($db_host, $db_user, $db_pass, $db_data);
30                         unset($db_host, $db_user, $db_pass, $db_data);
31         }
32
33         require_once("session.php");
34         require_once("datetime.php");
35         require_once('include/items.php');
36         require_once('include/bbcode.php');
37
38         load_config('config');
39         load_config('system');
40
41         load_hooks();
42
43         if($argc < 3)
44                 return;
45
46         $a->set_baseurl(get_config('system','url'));
47
48         logger('notifier: invoked: ' . print_r($argv,true));
49
50         $cmd = $argv[1];
51
52         switch($cmd) {
53                 case 'mail':
54                 default:
55                         $item_id = intval($argv[2]);
56                         if(! $item_id){
57                                 return;
58                         }
59                         break;
60         }
61
62         $expire = false;
63         $mail = false;
64         $fsuggest = false;
65         $top_level = false;
66         $recipients = array();
67         $url_recipients = array();
68
69         $normal_mode = true;
70
71         if($cmd === 'mail') {
72                 $normal_mode = false;
73                 $mail = true;
74                 $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
75                                 intval($item_id)
76                 );
77                 if(! count($message)){
78                         return;
79                 }
80                 $uid = $message[0]['uid'];
81                 $recipients[] = $message[0]['contact-id'];
82                 $item = $message[0];
83
84         }
85         elseif($cmd === 'expire') {
86                 $normal_mode = false;
87                 $expire = true;
88                 $items = q("SELECT * FROM `item` WHERE `uid` = %d AND `wall` = 1 
89                         AND `deleted` = 1 AND `changed` > UTC_TIMESTAMP() - INTERVAL 10 MINUTE",
90                         intval($item_id)
91                 );
92                 $uid = $item_id;
93                 $item_id = 0;
94                 if(! count($items))
95                         return;
96         }
97         elseif($cmd === 'suggest') {
98                 $normal_mode = false;
99                 $fsuggest = true;
100
101                 $suggest = q("SELECT * FROM `fsuggest` WHERE `id` = %d LIMIT 1",
102                         intval($item_id)
103                 );
104                 if(! count($suggest))
105                         return;
106                 $uid = $suggest[0]['uid'];
107                 $recipients[] = $suggest[0]['cid'];
108                 $item = $suggest[0];
109         }
110         else {
111
112                 // find ancestors
113                 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
114                         intval($item_id)
115                 );
116
117                 if((! count($r)) || (! intval($r[0]['parent']))) {
118                         return;
119                 }
120
121                 $target_item = $r[0];
122                 $parent_id = intval($r[0]['parent']);
123                 $uid = $r[0]['uid'];
124                 $updated = $r[0]['edited'];
125
126                 if(! $parent_id)
127                         return;
128
129                 $items = q("SELECT `item`.*, `sign`.`signed_text`,`sign`.`signature`,`sign`.`signer` 
130                         FROM `item` LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id` WHERE `parent` = %d ORDER BY `id` ASC",
131                         intval($parent_id)
132                 );
133
134                 if(! count($items)) {
135                         return;
136                 }
137
138                 // avoid race condition with deleting entries
139
140                 if($items[0]['deleted']) {
141                         foreach($items as $item)
142                                 $item['deleted'] = 1;
143                 }
144
145                 if((count($items) == 1) && ($items[0]['uri'] === $items[0]['parent-uri'])) {
146                         logger('notifier: top level post');
147                         $top_level = true;
148                 }
149
150         }
151
152         $r = q("SELECT `contact`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`, 
153                 `user`.`timezone`, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`, 
154                 `user`.`page-flags`, `user`.`prvnets`
155                 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` 
156                 WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
157                 intval($uid)
158         );
159
160         if(! count($r))
161                 return;
162
163         $owner = $r[0];
164
165         $walltowall = ((($top_level) && ($owner['id'] != $items[0]['contact-id'])) ? true : false);
166
167         $hub = get_config('system','huburl');
168
169         // If this is a public conversation, notify the feed hub
170         $public_message = true;
171
172         // fill this in with a single salmon slap if applicable
173         $slap = '';
174
175         if(! ($mail || $fsuggest)) {
176
177                 require_once('include/group.php');
178
179                 $parent = $items[0];
180
181                 // This is IMPORTANT!!!!
182
183                 // We will only send a "notify owner to relay" or followup message if the referenced post
184                 // originated on our system by virtue of having our hostname somewhere
185                 // in the URI, AND it was a comment (not top_level) AND the parent originated elsewhere.
186
187                 // if $parent['wall'] == 1 we will already have the parent message in our array
188                 // and we will relay the whole lot.
189  
190                 // expire sends an entire group of expire messages and cannot be forwarded.
191                 // However the conversation owner will be a part of the conversation and will 
192                 // be notified during this run.
193                 // Other DFRN conversation members will be alerted during polled updates.
194
195                 // Diaspora members currently are not notified of expirations, and other networks have
196                 // either limited or no ability to process deletions. We should at least fix Diaspora 
197                 // by stringing togther an array of retractions and sending them onward.
198                  
199         
200                 $localhost = $a->get_hostname();
201                 if(strpos($localhost,':'))
202                         $localhost = substr($localhost,0,strpos($localhost,':'));
203
204                 /**
205                  *
206                  * Be VERY CAREFUL if you make any changes to the following line. Seemingly innocuous changes 
207                  * have been known to cause runaway conditions which affected several servers, along with 
208                  * permissions issues. 
209                  *
210                  */
211  
212                 if((! $top_level) && ($parent['wall'] == 0) && (! $expire) && (stristr($target_item['uri'],$localhost))) {
213                         // local followup to remote post
214                         $followup = true;
215                         $public_message = false; // not public
216                         $conversant_str = dbesc($parent['contact-id']);
217                 }
218                 else {
219                         $followup = false;
220
221                         if((strlen($parent['allow_cid'])) 
222                                 || (strlen($parent['allow_gid'])) 
223                                 || (strlen($parent['deny_cid'])) 
224                                 || (strlen($parent['deny_gid']))) {
225                                 $public_message = false; // private recipients, not public
226                         }
227
228                         $allow_people = expand_acl($parent['allow_cid']);
229                         $allow_groups = expand_groups(expand_acl($parent['allow_gid']));
230                         $deny_people  = expand_acl($parent['deny_cid']);
231                         $deny_groups  = expand_groups(expand_acl($parent['deny_gid']));
232
233                         $conversants = array();
234
235                         foreach($items as $item) {
236                                 $recipients[] = $item['contact-id'];
237                                 $conversants[] = $item['contact-id'];
238                                 // pull out additional tagged people to notify (if public message)
239                                 if($public_message && strlen($item['inform'])) {
240                                         $people = explode(',',$item['inform']);
241                                         foreach($people as $person) {
242                                                 if(substr($person,0,4) === 'cid:') {
243                                                         $recipients[] = intval(substr($person,4));
244                                                         $conversants[] = intval(substr($person,4));
245                                                 }
246                                                 else {
247                                                         $url_recipients[] = substr($person,4);
248                                                 }
249                                         }
250                                 }
251                         }
252
253                         logger('notifier: url_recipients' . print_r($url_recipients,true));
254
255                         $conversants = array_unique($conversants);
256
257
258                         $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups));
259                         $deny = array_unique(array_merge($deny_people,$deny_groups));
260                         $recipients = array_diff($recipients,$deny);
261
262                         $conversant_str = dbesc(implode(', ',$conversants));
263                 }
264
265                 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0");
266
267                 if(count($r))
268                         $contacts = $r;
269         }
270
271         $feed_template = get_markup_template('atom_feed.tpl');
272         $mail_template = get_markup_template('atom_mail.tpl');
273
274         $atom = '';
275         $slaps = array();
276
277         $hubxml = feed_hublinks();
278
279         $birthday = feed_birthday($owner['uid'],$owner['timezone']);
280
281         if(strlen($birthday))
282                 $birthday = '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>';
283
284         $atom .= replace_macros($feed_template, array(
285                         '$version'      => xmlify(FRIENDIKA_VERSION),
286                         '$feed_id'      => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
287                         '$feed_title'   => xmlify($owner['name']),
288                         '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
289                         '$hub'          => $hubxml,
290                         '$salmon'       => '',  // private feed, we don't use salmon here
291                         '$name'         => xmlify($owner['name']),
292                         '$profile_page' => xmlify($owner['url']),
293                         '$photo'        => xmlify($owner['photo']),
294                         '$thumb'        => xmlify($owner['thumb']),
295                         '$picdate'      => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
296                         '$uridate'      => xmlify(datetime_convert('UTC','UTC',$owner['uri-date']    . '+00:00' , ATOM_TIME)) ,
297                         '$namdate'      => xmlify(datetime_convert('UTC','UTC',$owner['name-date']   . '+00:00' , ATOM_TIME)) ,
298                         '$birthday'     => $birthday
299         ));
300
301         if($mail) {
302                 $public_message = false;  // mail is  not public
303
304                 $body = fix_private_photos($item['body'],$owner['uid']);
305
306                 $atom .= replace_macros($mail_template, array(
307                         '$name'         => xmlify($owner['name']),
308                         '$profile_page' => xmlify($owner['url']),
309                         '$thumb'        => xmlify($owner['thumb']),
310                         '$item_id'      => xmlify($item['uri']),
311                         '$subject'      => xmlify($item['title']),
312                         '$created'      => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
313                         '$content'      => xmlify($body),
314                         '$parent_id'    => xmlify($item['parent-uri'])
315                 ));
316         }
317         elseif($fsuggest) {
318                 $public_message = false;  // suggestions are not public
319
320                 $sugg_template = get_markup_template('atom_suggest.tpl');
321
322                 $atom .= replace_macros($sugg_template, array(
323                         '$name'         => xmlify($item['name']),
324                         '$url'          => xmlify($item['url']),
325                         '$photo'        => xmlify($item['photo']),
326                         '$request'      => xmlify($item['request']),
327                         '$note'         => xmlify($item['note'])
328                 ));
329
330                 // We don't need this any more
331
332                 q("DELETE FROM `fsuggest` WHERE `id` = %d LIMIT 1",
333                         intval($item['id'])
334                 );
335
336         }
337         else {
338                 if($followup) {
339                         foreach($items as $item) {  // there is only one item
340                                 if(! $item['parent'])
341                                         continue;
342                                 if($item['id'] == $item_id) {
343                                         logger('notifier: followup: item: ' . print_r($item,true), LOGGER_DATA);
344                                         $slap  = atom_entry($item,'html',$owner,$owner,false);
345                                         $atom .= atom_entry($item,'text',$owner,$owner,false);
346                                 }
347                         }
348                 }
349                 else {
350                         foreach($items as $item) {
351
352                                 if(! $item['parent'])
353                                         continue;
354
355                                 // private emails may be in included in public conversations. Filter them.
356
357                                 if(($public_message) && $item['private'])
358                                         continue;
359
360                                 $contact = get_item_contact($item,$contacts);
361
362                                 if(! $contact)
363                                         continue;
364
365                                 if($normal_mode) {
366
367                                         // we only need the current item, but include the parent because without it
368                                         // older sites without a corresponding dfrn_notify change may do the wrong thing.
369
370                                     if($item_id == $item['id'] || $item['id'] == $item['parent'])
371                                                 $atom .= atom_entry($item,'text',$contact,$owner,true);
372                                 }
373                                 else
374                                         $atom .= atom_entry($item,'text',$contact,$owner,true);
375
376                                 if(($top_level) && ($public_message) && ($item['author-link'] === $item['owner-link']) && (! $expire)) 
377                                         $slaps[] = atom_entry($item,'html',$contact,$owner,true);
378                         }
379                 }
380         }
381         $atom .= '</feed>' . "\r\n";
382
383         logger('notifier: ' . $atom, LOGGER_DATA);
384
385         logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
386
387         // If this is a public message and pubmail is set on the parent, include all your email contacts
388
389         $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
390
391         if(! $mail_disabled) {
392                 if((! strlen($target_item['allow_cid'])) && (! strlen($target_item['allow_gid'])) 
393                         && (! strlen($target_item['deny_cid'])) && (! strlen($target_item['deny_gid'])) 
394                         && (intval($target_item['pubmail']))) {
395                         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `network` = '%s'",
396                                 intval($uid),
397                                 dbesc(NETWORK_MAIL)
398                         );
399                         if(count($r)) {
400                                 foreach($r as $rr)
401                                         $recipients[] = $rr['id'];
402                         }
403                 }
404         }
405
406         if($followup)
407                 $recip_str = $parent['contact-id'];
408         else
409                 $recip_str = implode(', ', $recipients);
410
411         $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 AND `pending` = 0 ",
412                 dbesc($recip_str)
413         );
414
415
416         require_once('include/salmon.php');
417
418         $interval = ((get_config('system','delivery_interval') === false) ? 2 : intval(get_config('system','delivery_interval')));
419
420         // delivery loop
421
422         if(count($r)) {
423
424                 foreach($r as $contact) {
425                         if((! $mail) && (! $fsuggest) && (! $followup) && (! $contact['self'])) {
426                                 if(($contact['network'] === NETWORK_DIASPORA) && ($public_message))
427                                         continue;
428                                 q("insert into deliverq ( `cmd`,`item`,`contact` ) values ('%s', %d, %d )",
429                                         dbesc($cmd),
430                                         intval($item_id),
431                                         intval($contact['id'])
432                                 );
433                         }
434                 }
435
436                 foreach($r as $contact) {
437                         if($contact['self'])
438                                 continue;
439
440                         // potentially more than one recipient. Start a new process and space them out a bit.
441                         // we will deliver single recipient types of message and email receipients here. 
442
443                         if((! $mail) && (! $fsuggest) && (! $followup)) {
444                                 proc_run('php','include/delivery.php',$cmd,$item_id,$contact['id']);
445                                 if($interval)
446                                         @time_sleep_until(microtime(true) + (float) $interval);
447                                 continue;
448                         }
449
450                         $deliver_status = 0;
451
452                         logger("main delivery by notifier: followup=$followup mail=$mail fsuggest=$fsuggest");
453
454                         switch($contact['network']) {
455                                 case NETWORK_DFRN:
456
457                                         // perform local delivery if we are on the same site
458
459                                         $basepath =  implode('/', array_slice(explode('/',$contact['url']),0,3));
460
461                                         if(link_compare($basepath,$a->get_baseurl())) {
462
463                                                 $nickname = basename($contact['url']);
464                                                 if($contact['issued-id'])
465                                                         $sql_extra = sprintf(" AND `dfrn-id` = '%s' ", dbesc($contact['issued-id']));
466                                                 else
467                                                         $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($contact['dfrn-id']));
468
469                                                 $x = q("SELECT  `contact`.*, `contact`.`uid` AS `importer_uid`, 
470                                                         `contact`.`pubkey` AS `cpubkey`, 
471                                                         `contact`.`prvkey` AS `cprvkey`, 
472                                                         `contact`.`thumb` AS `thumb`, 
473                                                         `contact`.`url` as `url`,
474                                                         `contact`.`name` as `senderName`,
475                                                         `user`.* 
476                                                         FROM `contact` 
477                                                         LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` 
478                                                         WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
479                                                         AND `contact`.`network` = '%s' AND `user`.`nickname` = '%s'
480                                                         $sql_extra
481                                                         AND `user`.`account_expired` = 0 LIMIT 1",
482                                                         dbesc(NETWORK_DFRN),
483                                                         dbesc($nickname)
484                                                 );
485
486                                                 if(count($x)) {
487                                                         require_once('library/simplepie/simplepie.inc');
488                                                         logger('mod-delivery: local delivery');
489                                                         local_delivery($x[0],$atom);
490                                                         break;                                  
491                                                 }
492                                         }
493
494
495
496                                         logger('notifier: dfrndelivery: ' . $contact['name']);
497                                         $deliver_status = dfrn_deliver($owner,$contact,$atom);
498
499                                         logger('notifier: dfrn_delivery returns ' . $deliver_status);
500         
501                                         if($deliver_status == (-1)) {
502                                                 logger('notifier: delivery failed: queuing message');
503                                                 // queue message for redelivery
504                                                 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
505                                                         VALUES ( %d, '%s', '%s', '%s') ",
506                                                         intval($contact['id']),
507                                                         dbesc(datetime_convert()),
508                                                         dbesc(datetime_convert()),
509                                                         dbesc($atom)
510                                                 );
511                                         }
512                                         break;
513                                 case NETWORK_OSTATUS:
514
515                                         // Do not send to otatus if we are not configured to send to public networks
516                                         if($owner['prvnets'])
517                                                 break;
518                                         if(get_config('system','ostatus_disabled') || get_config('system','dfrn_only'))
519                                                 break;
520
521                                         if($followup && $contact['notify']) {
522                                                 logger('notifier: slapdelivery: ' . $contact['name']);
523                                                 $deliver_status = slapper($owner,$contact['notify'],$slap);
524
525                                                 if($deliver_status == (-1)) {
526                                                         // queue message for redelivery
527                                                         q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
528                                                                 VALUES ( %d, '%s', '%s', '%s') ",
529                                                                 intval($contact['id']),
530                                                                 dbesc(datetime_convert()),
531                                                                 dbesc(datetime_convert()),
532                                                                 dbesc($slap)
533                                                         );
534
535                                                 }
536                                         }
537                                         else {
538
539                                                 // only send salmon if public - e.g. if it's ok to notify
540                                                 // a public hub, it's ok to send a salmon
541
542                                                 if((count($slaps)) && ($public_message) && (! $expire)) {
543                                                         logger('notifier: slapdelivery: ' . $contact['name']);
544                                                         foreach($slaps as $slappy) {
545                                                                 if($contact['notify']) {
546                                                                         $deliver_status = slapper($owner,$contact['notify'],$slappy);
547                                                                         if($deliver_status == (-1)) {
548                                                                                 // queue message for redelivery
549                                                                                 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
550                                                                                         VALUES ( %d, '%s', '%s', '%s') ",
551                                                                                         intval($contact['id']),
552                                                                                         dbesc(datetime_convert()),
553                                                                                         dbesc(datetime_convert()),
554                                                                                         dbesc($slappy)
555                                                                                 );                                                              
556                                                                         }
557                                                                 }
558                                                         }
559                                                 }
560                                         }
561                                         break;
562
563                                 case NETWORK_MAIL:
564                                                 
565                                         if(get_config('system','dfrn_only'))
566                                                 break;
567
568                                         // WARNING: does not currently convert to RFC2047 header encodings, etc.
569
570                                         $addr = $contact['addr'];
571                                         if(! strlen($addr))
572                                                 break;
573
574                                         if($cmd === 'wall-new' || $cmd === 'comment-new') {
575
576                                                 $it = null;
577                                                 if($cmd === 'wall-new') 
578                                                         $it = $items[0];
579                                                 else {
580                                                         $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1", 
581                                                                 intval($argv[2]),
582                                                                 intval($uid)
583                                                         );
584                                                         if(count($r))
585                                                                 $it = $r[0];
586                                                 }
587                                                 if(! $it)
588                                                         break;
589                                                 
590
591
592                                                 $local_user = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
593                                                         intval($uid)
594                                                 );
595                                                 if(! count($local_user))
596                                                         break;
597                                                 
598                                                 $reply_to = '';
599                                                 $r1 = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
600                                                         intval($uid)
601                                                 );
602                                                 if($r1 && $r1[0]['reply_to'])
603                                                         $reply_to = $r1[0]['reply_to'];
604         
605                                                 $subject  = (($it['title']) ? $it['title'] : t("\x28no subject\x29")) ;
606                                                 $headers  = 'From: ' . $local_user[0]['username'] . ' <' . $local_user[0]['email'] . '>' . "\n";
607
608                                                 if($reply_to)
609                                                         $headers .= 'Reply-to: ' . $reply_to . "\n";
610
611                                                 $headers .= 'Message-id: <' . $it['uri'] . '>' . "\n";
612
613                                                 if($it['uri'] !== $it['parent-uri']) {
614                                                         $header .= 'References: <' . $it['parent-uri'] . '>' . "\n";
615                                                         if(! strlen($it['title'])) {
616                                                                 $r = q("SELECT `title` FROM `item` WHERE `parent-uri` = '%s' LIMIT 1",
617                                                                         dbesc($it['parent-uri'])
618                                                                 );
619                                                                 if(count($r)) {
620                                                                         $subtitle = $r[0]['title'];
621                                                                         if($subtitle) {
622                                                                                 if(strncasecmp($subtitle,'RE:',3))
623                                                                                         $subject = $subtitle;
624                                                                                 else
625                                                                                         $subject = 'Re: ' . $subtitle;
626                                                                         }
627                                                                 }
628                                                         }
629                                                 }
630
631                                                 $headers .= 'MIME-Version: 1.0' . "\n";
632                                                 $headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
633                                                 $headers .= 'Content-Transfer-Encoding: 8bit' . "\n\n";
634                                                 $html    = prepare_body($it);
635                                                 $message = '<html><body>' . $html . '</body></html>';
636                                                 logger('notifier: email delivery to ' . $addr);
637                                                 mail($addr, $subject, $message, $headers);
638                                         }
639                                         break;
640                                 case NETWORK_DIASPORA:
641                                         require_once('include/diaspora.php');
642
643                                         if(get_config('system','dfrn_only') || (! get_config('system','diaspora_enabled')) || (! $normal_mode))
644                                                 break;
645
646                                         // special handling for followup to public post
647                                         // all other public posts processed as public batches further below
648
649                                         if($public_message) {
650                                                 if($followup)
651                                                         diaspora_send_followup($target_item,$owner,$contact, true);
652                                                 break;
653                                         }
654
655                                         if(! $contact['pubkey'])
656                                                 break;
657                                         
658                                         if($target_item['verb'] === ACTIVITY_DISLIKE) {
659                                                 // unsupported
660                                                 break;
661                                         }
662                                         elseif(($target_item['deleted']) && ($target_item['verb'] !== ACTIVITY_LIKE)) {
663                                                 // diaspora delete, 
664                                                 diaspora_send_retraction($target_item,$owner,$contact);
665                                                 break;
666                                         }
667                                         elseif($followup) {
668                                                 // send comments, likes and retractions of likes to owner to relay
669                                                 diaspora_send_followup($target_item,$owner,$contact);
670                                                 break;
671                                         }
672                                         elseif($target_item['parent'] != $target_item['id']) {
673                                                 // we are the relay - send comments, likes and unlikes to our conversants
674                                                 diaspora_send_relay($target_item,$owner,$contact);
675                                                 break;
676                                         }               
677                                         elseif(($top_level) && (! $walltowall)) {
678                                                 // currently no workable solution for sending walltowall
679                                                 diaspora_send_status($target_item,$owner,$contact);
680                                                 break;
681                                         }
682
683                                         break;
684
685                                 case NETWORK_FEED:
686                                 case NETWORK_FACEBOOK:
687                                         if(get_config('system','dfrn_only'))
688                                                 break;
689                                 default:
690                                         break;
691                         }
692                 }
693         }
694                 
695         // send additional slaps to mentioned remote tags (@foo@example.com)
696
697         if($slap && count($url_recipients) && ($followup || $top_level) && $public_message && (! $expire)) {
698                 if(! get_config('system','dfrn_only')) {
699                         foreach($url_recipients as $url) {
700                                 if($url) {
701                                         logger('notifier: urldelivery: ' . $url);
702                                         $deliver_status = slapper($owner,$url,$slap);
703                                         // TODO: redeliver/queue these items on failure, though there is no contact record
704                                 }
705                         }
706                 }
707         }
708
709
710         if($public_message) {
711
712                 $r1 = q("SELECT DISTINCT(`batch`), `id`, `name`,`network` FROM `contact` WHERE `network` = '%s' 
713                         AND `uid` = %d AND `rel` != %d ORDER BY rand() ",
714                         dbesc(NETWORK_DIASPORA),
715                         intval($owner['uid']),
716                         intval(CONTACT_IS_SHARING)
717                 );
718                         
719                 $r2 = q("SELECT `id`, `name`,`network` FROM `contact` 
720                         WHERE `network` = '%s' AND `uid` = %d AND `blocked` = 0 AND `pending` = 0
721                         AND `rel` != %d order by rand() ",
722                         dbesc(NETWORK_DFRN),
723                         intval($owner['uid']),
724                         intval(CONTACT_IS_SHARING)
725                 );
726
727                 $r = array_merge($r2,$r1);
728
729                 if(count($r)) {
730                         logger('pubdeliver: ' . print_r($r,true), LOGGER_DEBUG);
731
732                         // throw everything into the queue in case we get killed
733
734                         foreach($r as $rr) {
735                                 if((! $mail) && (! $fsuggest) && (! $followup)) {
736                                         q("insert into deliverq ( `cmd`,`item`,`contact` ) values ('%s', %d, %d )",
737                                                 dbesc($cmd),
738                                                 intval($item_id),
739                                                 intval($rr['id'])
740                                         );
741                                 }
742                         }
743
744                         foreach($r as $rr) {
745
746                                 // except for Diaspora batch jobs
747                                 // Don't deliver to folks who have already been delivered to
748
749                                 if(($rr['network'] !== NETWORK_DIASPORA) && (in_array($rr['id'],$conversants))) {
750                                         logger('notifier: already delivered id=' . $rr['id']);
751                                         continue;
752                                 }
753
754                                 if((! $mail) && (! $fsuggest) && (! $followup)) {
755                                         logger('notifier: delivery agent: ' . $rr['name'] . ' ' . $rr['id']); 
756                                         proc_run('php','include/delivery.php',$cmd,$item_id,$rr['id']);
757                                         if($interval)
758                                                 @time_sleep_until(microtime(true) + (float) $interval);
759                                 }
760                         }
761                 }
762
763
764                 if(strlen($hub)) {
765                         $hubs = explode(',', $hub);
766                         if(count($hubs)) {
767                                 foreach($hubs as $h) {
768                                         $h = trim($h);
769                                         if(! strlen($h))
770                                                 continue;
771                                         $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
772                                         post_url($h,$params);
773                                         logger('pubsub: publish: ' . $h . ' ' . $params . ' returned ' . $a->get_curl_code());
774                                         if(count($hubs) > 1)
775                                                 sleep(7);                               // try and avoid multiple hubs responding at precisely the same time
776                                 }
777                         }
778                 }
779
780         }
781
782         return;
783 }
784
785 if (array_search(__file__,get_included_files())===0){
786   notifier_run($argv,$argc);
787   killme();
788 }