]> git.mxchange.org Git - friendica.git/blob - include/notifier.php
6ac882c1992cf96511901f3c6f54033444aaffc2
[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                 $items = q("SELECT `item`.*, `sign`.`signed_text`,`sign`.`signature`,`sign`.`signer` 
127                         FROM `item` LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id` WHERE `parent` = %d ORDER BY `id` ASC",
128                         intval($parent_id)
129                 );
130
131                 if(! count($items)) {
132                         return;
133                 }
134
135                 // avoid race condition with deleting entries
136
137                 if($items[0]['deleted']) {
138                         foreach($items as $item)
139                                 $item['deleted'] = 1;
140                 }
141
142                 if((count($items) == 1) && ($items[0]['uri'] === $items[0]['parent-uri'])) {
143                         logger('notifier: top level post');
144                         $top_level = true;
145                 }
146
147         }
148
149         $r = q("SELECT `contact`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`, 
150                 `user`.`timezone`, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`, 
151                 `user`.`page-flags`, `user`.`prvnets`
152                 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` 
153                 WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
154                 intval($uid)
155         );
156
157         if(! count($r))
158                 return;
159
160         $owner = $r[0];
161
162         $walltowall = ((($top_level) && ($owner['id'] != $items[0]['contact-id'])) ? true : false);
163
164         $hub = get_config('system','huburl');
165
166         // If this is a public conversation, notify the feed hub
167         $public_message = true;
168
169         // fill this in with a single salmon slap if applicable
170         $slap = '';
171
172         if(! ($mail || $fsuggest)) {
173
174                 require_once('include/group.php');
175
176                 $parent = $items[0];
177
178                 // This is IMPORTANT!!!!
179
180                 // We will only send a "notify owner to relay" or followup message if the referenced post
181                 // originated on our system by virtue of having our hostname somewhere
182                 // in the URI, AND it was a comment (not top_level) AND the parent originated elsewhere.
183                 // if $parent['wall'] == 1 we will already have the parent message in our array
184                 // and we will relay the whole lot.
185  
186                 // expire sends an entire group of expire messages and cannot be forwarded.
187                 // However the conversation owner will be a part of the conversation and will 
188                 // be notified during this run.
189                 // Other DFRN conversation members will be alerted during polled updates.
190
191                 // Diaspora members currently are not notified of expirations, and other networks have
192                 // either limited or no ability to process deletions. We should at least fix Diaspora 
193                 // by stringing togther an array of retractions and sending them onward.
194                  
195         
196                 $localhost = $a->get_hostname();
197                 if(strpos($localhost,':'))
198                         $localhost = substr($localhost,0,strpos($localhost,':'));
199
200                 /**
201                  *
202                  * Be VERY CAREFUL if you make any changes to the following line. Seemingly innocuous changes 
203                  * have been known to cause runaway conditions which affected several servers, along with 
204                  * permissions issues. 
205                  *
206                  */
207  
208                 if((! $top_level) && ($parent['wall'] == 0) && (! $expire) && (stristr($target_item['uri'],$localhost))) {
209                         // local followup to remote post
210                         $followup = true;
211                         $public_message = false; // not public
212                         $conversant_str = dbesc($parent['contact-id']);
213                 }
214                 else {
215                         $followup = false;
216
217                         if((strlen($parent['allow_cid'])) 
218                                 || (strlen($parent['allow_gid'])) 
219                                 || (strlen($parent['deny_cid'])) 
220                                 || (strlen($parent['deny_gid']))) {
221                                 $public_message = false; // private recipients, not public
222                         }
223
224                         $allow_people = expand_acl($parent['allow_cid']);
225                         $allow_groups = expand_groups(expand_acl($parent['allow_gid']));
226                         $deny_people  = expand_acl($parent['deny_cid']);
227                         $deny_groups  = expand_groups(expand_acl($parent['deny_gid']));
228
229                         $conversants = array();
230
231                         foreach($items as $item) {
232                                 $recipients[] = $item['contact-id'];
233                                 $conversants[] = $item['contact-id'];
234                                 // pull out additional tagged people to notify (if public message)
235                                 if($public_message && strlen($item['inform'])) {
236                                         $people = explode(',',$item['inform']);
237                                         foreach($people as $person) {
238                                                 if(substr($person,0,4) === 'cid:') {
239                                                         $recipients[] = intval(substr($person,4));
240                                                         $conversants[] = intval(substr($person,4));
241                                                 }
242                                                 else {
243                                                         $url_recipients[] = substr($person,4);
244                                                 }
245                                         }
246                                 }
247                         }
248
249                         logger('notifier: url_recipients' . print_r($url_recipients,true));
250
251                         $conversants = array_unique($conversants);
252
253
254                         $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups));
255                         $deny = array_unique(array_merge($deny_people,$deny_groups));
256                         $recipients = array_diff($recipients,$deny);
257
258                         $conversant_str = dbesc(implode(', ',$conversants));
259                 }
260
261                 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0");
262
263                 if(count($r))
264                         $contacts = $r;
265         }
266
267         $feed_template = get_markup_template('atom_feed.tpl');
268         $mail_template = get_markup_template('atom_mail.tpl');
269
270         $atom = '';
271         $slaps = array();
272
273         $hubxml = feed_hublinks();
274
275         $birthday = feed_birthday($owner['uid'],$owner['timezone']);
276
277         if(strlen($birthday))
278                 $birthday = '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>';
279
280         $atom .= replace_macros($feed_template, array(
281                         '$version'      => xmlify(FRIENDIKA_VERSION),
282                         '$feed_id'      => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
283                         '$feed_title'   => xmlify($owner['name']),
284                         '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
285                         '$hub'          => $hubxml,
286                         '$salmon'       => '',  // private feed, we don't use salmon here
287                         '$name'         => xmlify($owner['name']),
288                         '$profile_page' => xmlify($owner['url']),
289                         '$photo'        => xmlify($owner['photo']),
290                         '$thumb'        => xmlify($owner['thumb']),
291                         '$picdate'      => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
292                         '$uridate'      => xmlify(datetime_convert('UTC','UTC',$owner['uri-date']    . '+00:00' , ATOM_TIME)) ,
293                         '$namdate'      => xmlify(datetime_convert('UTC','UTC',$owner['name-date']   . '+00:00' , ATOM_TIME)) ,
294                         '$birthday'     => $birthday
295         ));
296
297         if($mail) {
298                 $public_message = false;  // mail is  not public
299
300                 $body = fix_private_photos($item['body'],$owner['uid']);
301
302                 $atom .= replace_macros($mail_template, array(
303                         '$name'         => xmlify($owner['name']),
304                         '$profile_page' => xmlify($owner['url']),
305                         '$thumb'        => xmlify($owner['thumb']),
306                         '$item_id'      => xmlify($item['uri']),
307                         '$subject'      => xmlify($item['title']),
308                         '$created'      => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
309                         '$content'      => xmlify($body),
310                         '$parent_id'    => xmlify($item['parent-uri'])
311                 ));
312         }
313         elseif($fsuggest) {
314                 $public_message = false;  // suggestions are not public
315
316                 $sugg_template = get_markup_template('atom_suggest.tpl');
317
318                 $atom .= replace_macros($sugg_template, array(
319                         '$name'         => xmlify($item['name']),
320                         '$url'          => xmlify($item['url']),
321                         '$photo'        => xmlify($item['photo']),
322                         '$request'      => xmlify($item['request']),
323                         '$note'         => xmlify($item['note'])
324                 ));
325
326                 // We don't need this any more
327
328                 q("DELETE FROM `fsuggest` WHERE `id` = %d LIMIT 1",
329                         intval($item['id'])
330                 );
331
332         }
333         else {
334                 if($followup) {
335                         foreach($items as $item) {  // there is only one item
336                                 if(! $item['parent'])
337                                         continue;
338                                 if($item['id'] == $item_id) {
339                                         logger('notifier: followup: item: ' . print_r($item,true), LOGGER_DATA);
340                                         $slap  = atom_entry($item,'html',$owner,$owner,false);
341                                         $atom .= atom_entry($item,'text',$owner,$owner,false);
342                                 }
343                         }
344                 }
345                 else {
346                         foreach($items as $item) {
347
348                                 if(! $item['parent'])
349                                         continue;
350
351                                 // private emails may be in included in public conversations. Filter them.
352
353                                 if(($public_message) && $item['private'])
354                                         continue;
355
356                                 $contact = get_item_contact($item,$contacts);
357                                 if(! $contact)
358                                         continue;
359
360                                 $atom .= atom_entry($item,'text',$contact,$owner,true);
361
362                                 if(($top_level) && ($public_message) && ($item['author-link'] === $item['owner-link']) && (! $expire)) 
363                                         $slaps[] = atom_entry($item,'html',$contact,$owner,true);
364                         }
365                 }
366         }
367         $atom .= '</feed>' . "\r\n";
368
369         logger('notifier: ' . $atom, LOGGER_DATA);
370
371         logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
372
373         // If this is a public message and pubmail is set on the parent, include all your email contacts
374
375         $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
376
377         if(! $mail_disabled) {
378                 if((! strlen($target_item['allow_cid'])) && (! strlen($target_item['allow_gid'])) 
379                         && (! strlen($target_item['deny_cid'])) && (! strlen($target_item['deny_gid'])) 
380                         && (intval($target_item['pubmail']))) {
381                         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `network` = '%s'",
382                                 intval($uid),
383                                 dbesc(NETWORK_MAIL)
384                         );
385                         if(count($r)) {
386                                 foreach($r as $rr)
387                                         $recipients[] = $rr['id'];
388                         }
389                 }
390         }
391
392         if($followup)
393                 $recip_str = $parent['contact-id'];
394         else
395                 $recip_str = implode(', ', $recipients);
396
397         $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 AND `pending` = 0 ",
398                 dbesc($recip_str)
399         );
400
401
402         require_once('include/salmon.php');
403
404         $interval = ((get_config('system','delivery_interval') === false) ? 2 : intval(get_config('system','delivery_interval')));
405
406         // delivery loop
407
408         if(count($r)) {
409
410                 foreach($r as $contact) {
411                         if((! $mail) && (! $fsuggest) && (! $followup) && (! $contact['self'])) {
412                                 q("insert into deliverq ( `cmd`,`item`,`contact` ) values ('%s', %d, %d )",
413                                         dbesc($cmd),
414                                         intval($item_id),
415                                         intval($contact['id'])
416                                 );
417                         }
418                 }
419
420                 foreach($r as $contact) {
421                         if($contact['self'])
422                                 continue;
423
424                         // potentially more than one recipient. Start a new process and space them out a bit.
425                         // we will deliver single recipient types of message and email receipients here. 
426
427                         if((! $mail) && (! $fsuggest) && (! $followup)) {
428                                 proc_run('php','include/delivery.php',$cmd,$item_id,$contact['id']);
429                                 if($interval)
430                                         @time_sleep_until(microtime(true) + (float) $interval);
431                                 continue;
432                         }
433
434                         $deliver_status = 0;
435
436                         logger("main delivery by notifier: followup=$followup mail=$mail fsuggest=$fsuggest");
437
438                         switch($contact['network']) {
439                                 case NETWORK_DFRN:
440                                         logger('notifier: dfrndelivery: ' . $contact['name']);
441                                         $deliver_status = dfrn_deliver($owner,$contact,$atom);
442
443                                         logger('notifier: dfrn_delivery returns ' . $deliver_status);
444         
445                                         if($deliver_status == (-1)) {
446                                                 logger('notifier: delivery failed: queuing message');
447                                                 // queue message for redelivery
448                                                 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
449                                                         VALUES ( %d, '%s', '%s', '%s') ",
450                                                         intval($contact['id']),
451                                                         dbesc(datetime_convert()),
452                                                         dbesc(datetime_convert()),
453                                                         dbesc($atom)
454                                                 );
455                                         }
456                                         break;
457                                 case NETWORK_OSTATUS:
458
459                                         // Do not send to otatus if we are not configured to send to public networks
460                                         if($owner['prvnets'])
461                                                 break;
462                                         if(get_config('system','ostatus_disabled') || get_config('system','dfrn_only'))
463                                                 break;
464
465                                         if($followup && $contact['notify']) {
466                                                 logger('notifier: slapdelivery: ' . $contact['name']);
467                                                 $deliver_status = slapper($owner,$contact['notify'],$slap);
468
469                                                 if($deliver_status == (-1)) {
470                                                         // queue message for redelivery
471                                                         q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
472                                                                 VALUES ( %d, '%s', '%s', '%s') ",
473                                                                 intval($contact['id']),
474                                                                 dbesc(datetime_convert()),
475                                                                 dbesc(datetime_convert()),
476                                                                 dbesc($slap)
477                                                         );
478
479                                                 }
480                                         }
481                                         else {
482
483                                                 // only send salmon if public - e.g. if it's ok to notify
484                                                 // a public hub, it's ok to send a salmon
485
486                                                 if((count($slaps)) && ($public_message) && (! $expire)) {
487                                                         logger('notifier: slapdelivery: ' . $contact['name']);
488                                                         foreach($slaps as $slappy) {
489                                                                 if($contact['notify']) {
490                                                                         $deliver_status = slapper($owner,$contact['notify'],$slappy);
491                                                                         if($deliver_status == (-1)) {
492                                                                                 // queue message for redelivery
493                                                                                 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
494                                                                                         VALUES ( %d, '%s', '%s', '%s') ",
495                                                                                         intval($contact['id']),
496                                                                                         dbesc(datetime_convert()),
497                                                                                         dbesc(datetime_convert()),
498                                                                                         dbesc($slappy)
499                                                                                 );                                                              
500                                                                         }
501                                                                 }
502                                                         }
503                                                 }
504                                         }
505                                         break;
506
507                                 case NETWORK_MAIL:
508                                                 
509                                         if(get_config('system','dfrn_only'))
510                                                 break;
511
512                                         // WARNING: does not currently convert to RFC2047 header encodings, etc.
513
514                                         $addr = $contact['addr'];
515                                         if(! strlen($addr))
516                                                 break;
517
518                                         if($cmd === 'wall-new' || $cmd === 'comment-new') {
519
520                                                 $it = null;
521                                                 if($cmd === 'wall-new') 
522                                                         $it = $items[0];
523                                                 else {
524                                                         $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1", 
525                                                                 intval($argv[2]),
526                                                                 intval($uid)
527                                                         );
528                                                         if(count($r))
529                                                                 $it = $r[0];
530                                                 }
531                                                 if(! $it)
532                                                         break;
533                                                 
534
535
536                                                 $local_user = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
537                                                         intval($uid)
538                                                 );
539                                                 if(! count($local_user))
540                                                         break;
541                                                 
542                                                 $reply_to = '';
543                                                 $r1 = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
544                                                         intval($uid)
545                                                 );
546                                                 if($r1 && $r1[0]['reply_to'])
547                                                         $reply_to = $r1[0]['reply_to'];
548         
549                                                 $subject  = (($it['title']) ? $it['title'] : t("\x28no subject\x29")) ;
550                                                 $headers  = 'From: ' . $local_user[0]['username'] . ' <' . $local_user[0]['email'] . '>' . "\n";
551
552                                                 if($reply_to)
553                                                         $headers .= 'Reply-to: ' . $reply_to . "\n";
554
555                                                 $headers .= 'Message-id: <' . $it['uri'] . '>' . "\n";
556
557                                                 if($it['uri'] !== $it['parent-uri']) {
558                                                         $header .= 'References: <' . $it['parent-uri'] . '>' . "\n";
559                                                         if(! strlen($it['title'])) {
560                                                                 $r = q("SELECT `title` FROM `item` WHERE `parent-uri` = '%s' LIMIT 1",
561                                                                         dbesc($it['parent-uri'])
562                                                                 );
563                                                                 if(count($r)) {
564                                                                         $subtitle = $r[0]['title'];
565                                                                         if($subtitle) {
566                                                                                 if(strncasecmp($subtitle,'RE:',3))
567                                                                                         $subject = $subtitle;
568                                                                                 else
569                                                                                         $subject = 'Re: ' . $subtitle;
570                                                                         }
571                                                                 }
572                                                         }
573                                                 }
574
575                                                 $headers .= 'MIME-Version: 1.0' . "\n";
576                                                 $headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
577                                                 $headers .= 'Content-Transfer-Encoding: 8bit' . "\n\n";
578                                                 $html    = prepare_body($it);
579                                                 $message = '<html><body>' . $html . '</body></html>';
580                                                 logger('notifier: email delivery to ' . $addr);
581                                                 mail($addr, $subject, $message, $headers);
582                                         }
583                                         break;
584                                 case NETWORK_DIASPORA:
585                                         require_once('include/diaspora.php');
586                                         if(get_config('system','dfrn_only') || (! get_config('system','diaspora_enabled')) || (! $normal_mode))
587                                                 break;
588
589                                         if(! $contact['pubkey'])
590                                                 break;
591                                         
592                                         if($target_item['verb'] === ACTIVITY_DISLIKE) {
593                                                 // unsupported
594                                                 break;
595                                         }
596                                         elseif(($target_item['deleted']) && ($target_item['verb'] !== ACTIVITY_LIKE)) {
597                                                 // diaspora delete, 
598                                                 diaspora_send_retraction($target_item,$owner,$contact);
599                                                 break;
600                                         }
601                                         elseif($followup) {
602                                                 // send comments, likes and retractions of likes to owner to relay
603                                                 diaspora_send_followup($target_item,$owner,$contact);
604                                                 break;
605                                         }
606                                         elseif($target_item['parent'] != $target_item['id']) {
607                                                 // we are the relay - send comments, likes and unlikes to our conversants
608                                                 diaspora_send_relay($target_item,$owner,$contact);
609                                                 break;
610                                         }               
611                                         elseif(($top_level) && (! $walltowall)) {
612                                                 // currently no workable solution for sending walltowall
613                                                 diaspora_send_status($target_item,$owner,$contact);
614                                                 break;
615                                         }
616
617                                         break;
618
619                                 case NETWORK_FEED:
620                                 case NETWORK_FACEBOOK:
621                                         if(get_config('system','dfrn_only'))
622                                                 break;
623                                 default:
624                                         break;
625                         }
626                 }
627         }
628                 
629         // send additional slaps to mentioned remote tags (@foo@example.com)
630
631         if($slap && count($url_recipients) && ($followup || $top_level) && $public_message && (! $expire)) {
632                 if(! get_config('system','dfrn_only')) {
633                         foreach($url_recipients as $url) {
634                                 if($url) {
635                                         logger('notifier: urldelivery: ' . $url);
636                                         $deliver_status = slapper($owner,$url,$slap);
637                                         // TODO: redeliver/queue these items on failure, though there is no contact record
638                                 }
639                         }
640                 }
641         }
642
643
644         if($public_message) {
645
646                 $r = q("SELECT `id`, `name` FROM `contact` 
647                         WHERE `network` in ('%s','%s') AND `uid` = %d AND `blocked` = 0 AND `pending` = 0
648                         AND `rel` != %d order by rand() ",
649                         dbesc(NETWORK_DFRN),
650                         dbesc(NETWORK_DIASPORA),
651                         intval($owner['uid']),
652                         intval(CONTACT_IS_SHARING)
653                 );
654
655                 if(count($r)) {
656                         logger('pubdeliver: ' . print_r($r,true));
657
658                         // throw everything into the queue in case we get killed
659
660                         foreach($r as $rr) {
661                                 if((! $mail) && (! $fsuggest) && (! $followup)) {
662                                         q("insert into deliverq ( `cmd`,`item`,`contact` ) values ('%s', %d, %d )",
663                                                 dbesc($cmd),
664                                                 intval($item_id),
665                                                 intval($rr['id'])
666                                         );
667                                 }
668                         }
669
670                         foreach($r as $rr) {
671
672                                 /* Don't deliver to folks who have already been delivered to */
673
674                                 if(in_array($rr['id'],$conversants)) {
675                                         logger('notifier: already delivered id=' . $rr['id']);
676                                         continue;
677                                 }
678
679                                 if((! $mail) && (! $fsuggest) && (! $followup)) {
680                                         logger('notifier: delivery agent: ' . $rr['name'] . ' ' . $rr['id']); 
681                                         proc_run('php','include/delivery.php',$cmd,$item_id,$rr['id']);
682                                         if($interval)
683                                                 @time_sleep_until(microtime(true) + (float) $interval);
684                                 }
685                         }
686                 }
687
688
689                 if(strlen($hub)) {
690                         $hubs = explode(',', $hub);
691                         if(count($hubs)) {
692                                 foreach($hubs as $h) {
693                                         $h = trim($h);
694                                         if(! strlen($h))
695                                                 continue;
696                                         $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
697                                         post_url($h,$params);
698                                         logger('pubsub: publish: ' . $h . ' ' . $params . ' returned ' . $a->get_curl_code());
699                                         if(count($hubs) > 1)
700                                                 sleep(7);                               // try and avoid multiple hubs responding at precisely the same time
701                                 }
702                         }
703                 }
704
705         }
706
707         return;
708 }
709
710 if (array_search(__file__,get_included_files())===0){
711   notifier_run($argv,$argc);
712   killme();
713 }