]> git.mxchange.org Git - friendica.git/blob - include/notifier.php
Merge remote-tracking branch 'friendica/develop' into develop
[friendica.git] / include / notifier.php
1 <?php
2 require_once("boot.php");
3 require_once('include/queue_fn.php');
4 require_once('include/html2plain.php');
5 require_once("include/Scrape.php");
6 require_once('include/diaspora.php');
7 require_once("include/ostatus.php");
8 require_once('include/salmon.php');
9
10 /*
11  * This file was at one time responsible for doing all deliveries, but this caused
12  * big problems when the process was killed or stalled during the delivery process.
13  * It now invokes separate queues that are delivering via delivery.php and pubsubpublish.php.
14  */
15
16 /*
17  * The notifier is typically called with:
18  *
19  *              proc_run(PRIORITY_HIGH, "include/notifier.php", COMMAND, ITEM_ID);
20  *
21  * where COMMAND is one of the following:
22  *
23  *              activity                                (in diaspora.php, dfrn_confirm.php, profiles.php)
24  *              comment-import                  (in diaspora.php, items.php)
25  *              comment-new                             (in item.php)
26  *              drop                                    (in diaspora.php, items.php, photos.php)
27  *              edit_post                               (in item.php)
28  *              event                                   (in events.php)
29  *              expire                                  (in items.php)
30  *              like                                    (in like.php, poke.php)
31  *              mail                                    (in message.php)
32  *              suggest                                 (in fsuggest.php)
33  *              tag                                             (in photos.php, poke.php, tagger.php)
34  *              tgroup                                  (in items.php)
35  *              wall-new                                (in photos.php, item.php)
36  *              removeme                                (in Contact.php)
37  *              relocate                                (in uimport.php)
38  *
39  * and ITEM_ID is the id of the item in the database that needs to be sent to others.
40  */
41
42
43 function notifier_run(&$argv, &$argc){
44         global $a, $db;
45
46         if(is_null($a)){
47                 $a = new App;
48         }
49
50         if(is_null($db)) {
51                 @include(".htconfig.php");
52                 require_once("include/dba.php");
53                 $db = new dba($db_host, $db_user, $db_pass, $db_data);
54                         unset($db_host, $db_user, $db_pass, $db_data);
55         }
56
57         require_once("include/session.php");
58         require_once("include/datetime.php");
59         require_once('include/items.php');
60         require_once('include/bbcode.php');
61         require_once('include/email.php');
62         load_config('config');
63         load_config('system');
64
65         load_hooks();
66
67         if($argc < 3)
68                 return;
69
70         $a->set_baseurl(get_config('system','url'));
71
72         logger('notifier: invoked: ' . print_r($argv,true), LOGGER_DEBUG);
73
74         $cmd = $argv[1];
75
76         switch($cmd) {
77                 case 'mail':
78                 default:
79                         $item_id = intval($argv[2]);
80                         if(! $item_id){
81                                 return;
82                         }
83                         break;
84         }
85
86         $expire = false;
87         $mail = false;
88         $fsuggest = false;
89         $relocate = false;
90         $top_level = false;
91         $recipients = array();
92         $url_recipients = array();
93
94         $normal_mode = true;
95
96         if($cmd === 'mail') {
97                 $normal_mode = false;
98                 $mail = true;
99                 $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
100                                 intval($item_id)
101                 );
102                 if(! count($message)){
103                         return;
104                 }
105                 $uid = $message[0]['uid'];
106                 $recipients[] = $message[0]['contact-id'];
107                 $item = $message[0];
108
109         }
110         elseif($cmd === 'expire') {
111                 $normal_mode = false;
112                 $expire = true;
113                 $items = q("SELECT * FROM `item` WHERE `uid` = %d AND `wall` = 1
114                         AND `deleted` = 1 AND `changed` > UTC_TIMESTAMP() - INTERVAL 10 MINUTE",
115                         intval($item_id)
116                 );
117                 $uid = $item_id;
118                 $item_id = 0;
119                 if(! count($items))
120                         return;
121         }
122         elseif($cmd === 'suggest') {
123                 $normal_mode = false;
124                 $fsuggest = true;
125
126                 $suggest = q("SELECT * FROM `fsuggest` WHERE `id` = %d LIMIT 1",
127                         intval($item_id)
128                 );
129                 if(! count($suggest))
130                         return;
131                 $uid = $suggest[0]['uid'];
132                 $recipients[] = $suggest[0]['cid'];
133                 $item = $suggest[0];
134         } elseif($cmd === 'removeme') {
135                 $r = q("SELECT `contact`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`,
136                                 `user`.`timezone`, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`,
137                                 `user`.`page-flags`, `user`.`prvnets`, `user`.`account-type`, `user`.`guid`
138                         FROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
139                                 WHERE `contact`.`uid` = %d AND `contact`.`self` LIMIT 1",
140                                 intval($item_id));
141                 if (!$r)
142                         return;
143
144                 $user = $r[0];
145
146                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` LIMIT 1", intval($item_id));
147                 if (!$r)
148                         return;
149
150                 $self = $r[0];
151
152                 $r = q("SELECT * FROM `contact` WHERE NOT `self` AND `uid` = %d", intval($item_id));
153                 if(!$r)
154                         return;
155
156                 require_once('include/Contact.php');
157                 foreach($r as $contact) {
158                         terminate_friendship($user, $self, $contact);
159                 }
160                 return;
161         } elseif($cmd === 'relocate') {
162                 $normal_mode = false;
163                 $relocate = true;
164                 $uid = $item_id;
165
166                 $recipients_relocate = q("SELECT * FROM contact WHERE uid = %d  AND self = 0 AND network = '%s'" , intval($uid), NETWORK_DFRN);
167         } else {
168                 // find ancestors
169                 $r = q("SELECT * FROM `item` WHERE `id` = %d and visible = 1 and moderated = 0 LIMIT 1",
170                         intval($item_id)
171                 );
172
173                 if((! dbm::is_result($r)) || (! intval($r[0]['parent']))) {
174                         return;
175                 }
176
177                 $target_item = $r[0];
178                 $parent_id = intval($r[0]['parent']);
179                 $uid = $r[0]['uid'];
180                 $updated = $r[0]['edited'];
181
182                 $items = q("SELECT `item`.*, `sign`.`signed_text`,`sign`.`signature`,`sign`.`signer`
183                         FROM `item` LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id` WHERE `parent` = %d and visible = 1 and moderated = 0 ORDER BY `id` ASC",
184                         intval($parent_id)
185                 );
186
187                 if(! count($items)) {
188                         return;
189                 }
190
191                 // avoid race condition with deleting entries
192
193                 if($items[0]['deleted']) {
194                         foreach($items as $item)
195                                 $item['deleted'] = 1;
196                 }
197
198                 if((count($items) == 1) && ($items[0]['id'] === $target_item['id']) && ($items[0]['uri'] === $items[0]['parent-uri'])) {
199                         logger('notifier: top level post');
200                         $top_level = true;
201                 }
202
203         }
204
205         $r = q("SELECT `contact`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`,
206                 `user`.`timezone`, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`,
207                 `user`.`page-flags`, `user`.`prvnets`, `user`.`account-type`
208                 FROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
209                 WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
210                 intval($uid)
211         );
212
213         if (! dbm::is_result($r)) {
214                 return;
215         }
216
217         $owner = $r[0];
218
219         $walltowall = ((($top_level) && ($owner['id'] != $items[0]['contact-id'])) ? true : false);
220
221         $hub = get_config('system','huburl');
222
223         // If this is a public conversation, notify the feed hub
224         $public_message = true;
225
226         // Do a PuSH
227         $push_notify = false;
228
229         // fill this in with a single salmon slap if applicable
230         $slap = '';
231
232         if(! ($mail || $fsuggest || $relocate)) {
233
234                 $slap = ostatus::salmon($target_item,$owner);
235
236                 require_once('include/group.php');
237
238                 $parent = $items[0];
239
240                 $thr_parent = q("SELECT `network`, `author-link`, `owner-link` FROM `item` WHERE `uri` = '%s' AND `uid` = %d",
241                         dbesc($target_item["thr-parent"]), intval($target_item["uid"]));
242
243                 logger('Parent is '.$parent['network'].'. Thread parent is '.$thr_parent[0]['network'], LOGGER_DEBUG);
244
245                 // This is IMPORTANT!!!!
246
247                 // We will only send a "notify owner to relay" or followup message if the referenced post
248                 // originated on our system by virtue of having our hostname somewhere
249                 // in the URI, AND it was a comment (not top_level) AND the parent originated elsewhere.
250
251                 // if $parent['wall'] == 1 we will already have the parent message in our array
252                 // and we will relay the whole lot.
253
254                 // expire sends an entire group of expire messages and cannot be forwarded.
255                 // However the conversation owner will be a part of the conversation and will
256                 // be notified during this run.
257                 // Other DFRN conversation members will be alerted during polled updates.
258
259
260
261                 // Diaspora members currently are not notified of expirations, and other networks have
262                 // either limited or no ability to process deletions. We should at least fix Diaspora
263                 // by stringing togther an array of retractions and sending them onward.
264
265
266                 $localhost = str_replace('www.','',$a->get_hostname());
267                 if(strpos($localhost,':'))
268                         $localhost = substr($localhost,0,strpos($localhost,':'));
269
270                 /**
271                  *
272                  * Be VERY CAREFUL if you make any changes to the following several lines. Seemingly innocuous changes
273                  * have been known to cause runaway conditions which affected several servers, along with
274                  * permissions issues.
275                  *
276                  */
277
278                 $relay_to_owner = false;
279
280                 if(!$top_level && ($parent['wall'] == 0) && !$expire && (stristr($target_item['uri'],$localhost))) {
281                         $relay_to_owner = true;
282                 }
283
284
285                 if(($cmd === 'uplink') && (intval($parent['forum_mode']) == 1) && !$top_level) {
286                         $relay_to_owner = true;
287                 }
288
289                 // until the 'origin' flag has been in use for several months
290                 // we will just use it as a fallback test
291                 // later we will be able to use it as the primary test of whether or not to relay.
292
293                 if(! $target_item['origin'])
294                         $relay_to_owner = false;
295
296                 if($parent['origin'])
297                         $relay_to_owner = false;
298
299                 if($relay_to_owner) {
300                         logger('notifier: followup '.$target_item["guid"], LOGGER_DEBUG);
301                         // local followup to remote post
302                         $followup = true;
303                         $public_message = false; // not public
304                         $conversant_str = dbesc($parent['contact-id']);
305                         $recipients = array($parent['contact-id']);
306                         $recipients_followup  = array($parent['contact-id']);
307
308                         //if (!$target_item['private'] AND $target_item['wall'] AND
309                         if (!$target_item['private'] AND
310                                 (strlen($target_item['allow_cid'].$target_item['allow_gid'].
311                                         $target_item['deny_cid'].$target_item['deny_gid']) == 0))
312                                 $push_notify = true;
313
314                         if (($thr_parent AND ($thr_parent[0]['network'] == NETWORK_OSTATUS)) OR ($parent['network'] == NETWORK_OSTATUS)) {
315
316                                 $push_notify = true;
317
318                                 if ($parent["network"] == NETWORK_OSTATUS) {
319                                         // Distribute the message to the DFRN contacts as if this wasn't a followup since OStatus can't relay comments
320                                         // Currently it is work at progress
321                                         $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `network` = '%s' AND NOT `blocked` AND NOT `pending` AND NOT `archive`",
322                                                 intval($uid),
323                                                 dbesc(NETWORK_DFRN)
324                                         );
325                                         if (dbm::is_result($r))
326                                                 foreach($r as $rr)
327                                                         $recipients_followup[] = $rr['id'];
328                                 }
329                         }
330                         logger("Notify ".$target_item["guid"]." via PuSH: ".($push_notify?"Yes":"No"), LOGGER_DEBUG);
331                 } else {
332                         $followup = false;
333
334                         logger('Distributing directly '.$target_item["guid"], LOGGER_DEBUG);
335
336                         // don't send deletions onward for other people's stuff
337
338                         if($target_item['deleted'] && (! intval($target_item['wall']))) {
339                                 logger('notifier: ignoring delete notification for non-wall item');
340                                 return;
341                         }
342
343                         if((strlen($parent['allow_cid']))
344                                 || (strlen($parent['allow_gid']))
345                                 || (strlen($parent['deny_cid']))
346                                 || (strlen($parent['deny_gid']))) {
347                                 $public_message = false; // private recipients, not public
348                         }
349
350                         $allow_people = expand_acl($parent['allow_cid']);
351                         $allow_groups = expand_groups(expand_acl($parent['allow_gid']),true);
352                         $deny_people  = expand_acl($parent['deny_cid']);
353                         $deny_groups  = expand_groups(expand_acl($parent['deny_gid']));
354
355                         // if our parent is a public forum (forum_mode == 1), uplink to the origional author causing
356                         // a delivery fork. private groups (forum_mode == 2) do not uplink
357
358                         if((intval($parent['forum_mode']) == 1) && (! $top_level) && ($cmd !== 'uplink')) {
359                                 proc_run(PRIORITY_HIGH,'include/notifier.php','uplink',$item_id);
360                         }
361
362                         $conversants = array();
363
364                         foreach($items as $item) {
365                                 $recipients[] = $item['contact-id'];
366                                 $conversants[] = $item['contact-id'];
367                                 // pull out additional tagged people to notify (if public message)
368                                 if($public_message && strlen($item['inform'])) {
369                                         $people = explode(',',$item['inform']);
370                                         foreach($people as $person) {
371                                                 if(substr($person,0,4) === 'cid:') {
372                                                         $recipients[] = intval(substr($person,4));
373                                                         $conversants[] = intval(substr($person,4));
374                                                 }
375                                                 else {
376                                                         $url_recipients[] = substr($person,4);
377                                                 }
378                                         }
379                                 }
380                         }
381
382                         if (count($url_recipients))
383                                 logger('notifier: '.$target_item["guid"].' url_recipients ' . print_r($url_recipients,true));
384
385                         $conversants = array_unique($conversants);
386
387
388                         $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups));
389                         $deny = array_unique(array_merge($deny_people,$deny_groups));
390                         $recipients = array_diff($recipients,$deny);
391
392                         $conversant_str = dbesc(implode(', ',$conversants));
393                 }
394
395                 // If the thread parent is OStatus then do some magic to distribute the messages.
396                 // We have not only to look at the parent, since it could be a Friendica thread.
397                 if (($thr_parent AND ($thr_parent[0]['network'] == NETWORK_OSTATUS)) OR ($parent['network'] == NETWORK_OSTATUS)) {
398
399                         logger('Some parent is OStatus for '.$target_item["guid"]." - Author: ".$thr_parent[0]['author-link']." - Owner: ".$thr_parent[0]['owner-link'], LOGGER_DEBUG);
400
401                         // Send a salmon to the parent author
402                         $r = q("SELECT `notify` FROM `contact` WHERE `nurl`='%s' AND `uid` IN (0, %d) AND `notify` != ''",
403                                 dbesc(normalise_link($thr_parent[0]['author-link'])),
404                                 intval($uid));
405                         if ($r)
406                                 $probed_contact = $r[0];
407                         else
408                                 $probed_contact = probe_url($thr_parent[0]['author-link']);
409
410                         if ($probed_contact["notify"] != "") {
411                                 logger('Notify parent author '.$probed_contact["url"].': '.$probed_contact["notify"]);
412                                 $url_recipients[$probed_contact["notify"]] = $probed_contact["notify"];
413                         }
414
415                         // Send a salmon to the parent owner
416                         $r = q("SELECT `notify` FROM `contact` WHERE `nurl`='%s' AND `uid` IN (0, %d) AND `notify` != ''",
417                                 dbesc(normalise_link($thr_parent[0]['owner-link'])),
418                                 intval($uid));
419                         if ($r)
420                                 $probed_contact = $r[0];
421                         else
422                                 $probed_contact = probe_url($thr_parent[0]['owner-link']);
423                         if ($probed_contact["notify"] != "") {
424                                 logger('Notify parent owner '.$probed_contact["url"].': '.$probed_contact["notify"]);
425                                 $url_recipients[$probed_contact["notify"]] = $probed_contact["notify"];
426                         }
427
428                         // Send a salmon notification to every person we mentioned in the post
429                         $arr = explode(',',$target_item['tag']);
430                         foreach($arr as $x) {
431                                 //logger('Checking tag '.$x, LOGGER_DEBUG);
432                                 $matches = null;
433                                 if(preg_match('/@\[url=([^\]]*)\]/',$x,$matches)) {
434                                                 $probed_contact = probe_url($matches[1]);
435                                         if ($probed_contact["notify"] != "") {
436                                                 logger('Notify mentioned user '.$probed_contact["url"].': '.$probed_contact["notify"]);
437                                                 $url_recipients[$probed_contact["notify"]] = $probed_contact["notify"];
438                                         }
439                                 }
440                         }
441
442                         // It only makes sense to distribute answers to OStatus messages to Friendica and OStatus - but not Diaspora
443                         $sql_extra = " AND `network` IN ('".NETWORK_OSTATUS."', '".NETWORK_DFRN."')";
444                 } else
445                         $sql_extra = " AND `network` IN ('".NETWORK_OSTATUS."', '".NETWORK_DFRN."', '".NETWORK_DIASPORA."', '".NETWORK_MAIL."', '".NETWORK_MAIL2."')";
446
447                 $r = q("SELECT * FROM `contact` WHERE `id` IN ($conversant_str) AND NOT `blocked` AND NOT `pending` AND NOT `archive`".$sql_extra);
448
449                 if (dbm::is_result($r))
450                         $contacts = $r;
451
452         } else
453                 $public_message = false;
454
455         // If this is a public message and pubmail is set on the parent, include all your email contacts
456
457         $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
458
459         if(! $mail_disabled) {
460                 if((! strlen($target_item['allow_cid'])) && (! strlen($target_item['allow_gid']))
461                         && (! strlen($target_item['deny_cid'])) && (! strlen($target_item['deny_gid']))
462                         && (intval($target_item['pubmail']))) {
463                         $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `network` = '%s'",
464                                 intval($uid),
465                                 dbesc(NETWORK_MAIL)
466                         );
467                         if (dbm::is_result($r)) {
468                                 foreach($r as $rr)
469                                         $recipients[] = $rr['id'];
470                         }
471                 }
472         }
473
474         if($followup)
475                 $recip_str = implode(', ', $recipients_followup);
476         else
477                 $recip_str = implode(', ', $recipients);
478
479         if ($relocate)
480                 $r = $recipients_relocate;
481         else
482                 $r = q("SELECT * FROM `contact` WHERE `id` IN (%s) AND NOT `blocked` AND NOT `pending` AND NOT `archive`",
483                         dbesc($recip_str)
484                 );
485
486
487         $interval = ((get_config('system','delivery_interval') === false) ? 2 : intval(get_config('system','delivery_interval')));
488
489         // If we are using the worker we don't need a delivery interval
490         if (get_config("system", "worker"))
491                 $interval = false;
492
493         // delivery loop
494
495         if (dbm::is_result($r)) {
496
497                 foreach($r as $contact) {
498                         if(!$contact['self']) {
499                                 if(($contact['network'] === NETWORK_DIASPORA) && ($public_message))
500                                         continue;
501                                 q("INSERT INTO `deliverq` (`cmd`,`item`,`contact`) VALUES ('%s', %d, %d)",
502                                         dbesc($cmd),
503                                         intval($item_id),
504                                         intval($contact['id'])
505                                 );
506                         }
507                 }
508
509
510                 // This controls the number of deliveries to execute with each separate delivery process.
511                 // By default we'll perform one delivery per process. Assuming a hostile shared hosting
512                 // provider, this provides the greatest chance of deliveries if processes start getting
513                 // killed. We can also space them out with the delivery_interval to also help avoid them
514                 // getting whacked.
515
516                 // If $deliveries_per_process > 1, we will chain this number of multiple deliveries
517                 // together into a single process. This will reduce the overall number of processes
518                 // spawned for each delivery, but they will run longer.
519
520                 // When using the workerqueue, we don't need this functionality.
521
522                 $deliveries_per_process = intval(get_config('system','delivery_batch_count'));
523                 if (($deliveries_per_process <= 0) OR get_config("system", "worker"))
524                         $deliveries_per_process = 1;
525
526                 $this_batch = array();
527
528                 for($x = 0; $x < count($r); $x ++) {
529                         $contact = $r[$x];
530
531                         if($contact['self'])
532                                 continue;
533
534                         logger("Deliver ".$target_item["guid"]." to ".$contact['url']." via network ".$contact['network'], LOGGER_DEBUG);
535
536                         // potentially more than one recipient. Start a new process and space them out a bit.
537                         // we will deliver single recipient types of message and email recipients here.
538
539                         $this_batch[] = $contact['id'];
540
541                         if(count($this_batch) >= $deliveries_per_process) {
542                                 proc_run(PRIORITY_HIGH,'include/delivery.php',$cmd,$item_id,$this_batch);
543                                 $this_batch = array();
544                                 if($interval)
545                                         @time_sleep_until(microtime(true) + (float) $interval);
546                         }
547                         continue;
548                 }
549
550                 // be sure to pick up any stragglers
551                 if(count($this_batch))
552                         proc_run(PRIORITY_HIGH,'include/delivery.php',$cmd,$item_id,$this_batch);
553         }
554
555         // send salmon slaps to mentioned remote tags (@foo@example.com) in OStatus posts
556         // They are especially used for notifications to OStatus users that don't follow us.
557
558         if($slap && count($url_recipients) && ($public_message || $push_notify) && $normal_mode) {
559                 if(!get_config('system','dfrn_only')) {
560                         foreach($url_recipients as $url) {
561                                 if ($url) {
562                                         logger('notifier: urldelivery: ' . $url);
563                                         $deliver_status = slapper($owner,$url,$slap);
564                                         /// @TODO Redeliver/queue these items on failure, though there is no contact record
565                                 }
566                         }
567                 }
568         }
569
570
571         if($public_message) {
572
573                 if (!$followup)
574                         $r0 = Diaspora::relay_list();
575                 else
576                         $r0 = array();
577
578                 $r1 = q("SELECT DISTINCT(`batch`), `id`, `name`,`network` FROM `contact` WHERE `network` = '%s'
579                         AND `uid` = %d AND `rel` != %d AND NOT `blocked` AND NOT `pending` AND NOT `archive` GROUP BY `batch` ORDER BY rand()",
580                         dbesc(NETWORK_DIASPORA),
581                         intval($owner['uid']),
582                         intval(CONTACT_IS_SHARING)
583                 );
584
585                 $r2 = q("SELECT `id`, `name`,`network` FROM `contact`
586                         WHERE `network` in ( '%s', '%s')  AND `uid` = %d AND NOT `blocked` AND NOT `pending` AND NOT `archive`
587                         AND `rel` != %d order by rand() ",
588                         dbesc(NETWORK_DFRN),
589                         dbesc(NETWORK_MAIL2),
590                         intval($owner['uid']),
591                         intval(CONTACT_IS_SHARING)
592                 );
593
594                 $r = array_merge($r2,$r1,$r0);
595
596                 if (dbm::is_result($r)) {
597                         logger('pubdeliver '.$target_item["guid"].': '.print_r($r,true), LOGGER_DEBUG);
598
599                         // throw everything into the queue in case we get killed
600
601                         foreach ($r as $rr) {
602                                 if((! $mail) && (! $fsuggest) && (! $followup)) {
603                                         q("INSERT INTO `deliverq` (`cmd`,`item`,`contact`) VALUES ('%s', %d, %d)
604                                                 ON DUPLICATE KEY UPDATE `cmd` = '%s', `item` = %d, `contact` = %d",
605                                                 dbesc($cmd), intval($item_id), intval($rr['id']),
606                                                 dbesc($cmd), intval($item_id), intval($rr['id'])
607                                         );
608                                 }
609                         }
610
611                         foreach ($r as $rr) {
612
613                                 // except for Diaspora batch jobs
614                                 // Don't deliver to folks who have already been delivered to
615
616                                 if(($rr['network'] !== NETWORK_DIASPORA) && (in_array($rr['id'],$conversants))) {
617                                         logger('notifier: already delivered id=' . $rr['id']);
618                                         continue;
619                                 }
620
621                                 if((! $mail) && (! $fsuggest) && (! $followup)) {
622                                         logger('notifier: delivery agent: '.$rr['name'].' '.$rr['id'].' '.$rr['network'].' '.$target_item["guid"]);
623                                         proc_run(PRIORITY_HIGH,'include/delivery.php',$cmd,$item_id,$rr['id']);
624                                         if($interval)
625                                                 @time_sleep_until(microtime(true) + (float) $interval);
626                                 }
627                         }
628                 }
629
630                 $push_notify = true;
631
632         }
633
634         // Notify PuSH subscribers (Used for OStatus distribution of regular posts)
635         if($push_notify AND strlen($hub)) {
636                 $hubs = explode(',', $hub);
637                 if(count($hubs)) {
638                         foreach($hubs as $h) {
639                                 $h = trim($h);
640                                 if(! strlen($h))
641                                         continue;
642
643                                 if ($h === '[internal]') {
644                                         // Set push flag for PuSH subscribers to this topic,
645                                         // they will be notified in queue.php
646                                         q("UPDATE `push_subscriber` SET `push` = 1 ".
647                                           "WHERE `nickname` = '%s' AND `push` = 0", dbesc($owner['nickname']));
648
649                                         logger('Activating internal PuSH for item '.$item_id, LOGGER_DEBUG);
650
651                                 } else {
652
653                                         $params = 'hub.mode=publish&hub.url=' . urlencode( App::get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
654                                         post_url($h,$params);
655                                         logger('publish for item '.$item_id.' ' . $h . ' ' . $params . ' returned ' . $a->get_curl_code());
656                                 }
657                                 if(count($hubs) > 1)
658                                         sleep(7);                               // try and avoid multiple hubs responding at precisely the same time
659                         }
660                 }
661
662                 // Handling the pubsubhubbub requests
663                 proc_run(PRIORITY_HIGH,'include/pubsubpublish.php');
664         }
665
666         logger('notifier: calling hooks', LOGGER_DEBUG);
667
668         if($normal_mode)
669                 call_hooks('notifier_normal',$target_item);
670
671         call_hooks('notifier_end',$target_item);
672
673         return;
674 }
675
676
677 if (array_search(__file__,get_included_files())===0){
678         notifier_run($_SERVER["argv"],$_SERVER["argc"]);
679         killme();
680 }