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