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