2 require_once("boot.php");
3 require_once('include/queue_fn.php');
4 require_once('include/html2plain.php');
7 * This file was at one time responsible for doing all deliveries, but this caused
8 * big problems on shared hosting systems, where the process might get killed by the
9 * hosting provider and nothing would get delivered.
10 * It now only delivers one message under certain cases, and invokes a queued
11 * delivery mechanism (include/deliver.php) to deliver individual contacts at
12 * controlled intervals.
13 * This has a much better chance of surviving random processes getting killed
14 * by the hosting provider.
15 * A lot of this code is duplicated in include/deliver.php until we have time to go back
16 * and re-structure the delivery procedure based on the obstacles that have been thrown at
17 * us by hosting providers.
21 * The notifier is typically called with:
23 * proc_run('php', "include/notifier.php", COMMAND, ITEM_ID);
25 * where COMMAND is one of the following:
27 * activity (in diaspora.php, dfrn_confirm.php, profiles.php)
28 * comment-import (in diaspora.php, items.php)
29 * comment-new (in item.php)
30 * drop (in diaspora.php, items.php, photos.php)
31 * edit_post (in item.php)
32 * event (in events.php)
33 * expire (in items.php)
34 * like (in like.php, poke.php)
35 * mail (in message.php)
36 * suggest (in fsuggest.php)
37 * tag (in photos.php, poke.php, tagger.php)
38 * tgroup (in items.php)
39 * wall-new (in photos.php, item.php)
40 * removeme (in Contact.php)
41 * relocate (in uimport.php)
43 * and ITEM_ID is the id of the item in the database that needs to be sent to others.
47 function notifier_run(&$argv, &$argc){
55 @include(".htconfig.php");
56 require_once("include/dba.php");
57 $db = new dba($db_host, $db_user, $db_pass, $db_data);
58 unset($db_host, $db_user, $db_pass, $db_data);
61 require_once("include/session.php");
62 require_once("include/datetime.php");
63 require_once('include/items.php');
64 require_once('include/bbcode.php');
65 require_once('include/email.php');
66 load_config('config');
67 load_config('system');
74 $a->set_baseurl(get_config('system','url'));
76 logger('notifier: invoked: ' . print_r($argv,true), LOGGER_DEBUG);
83 $item_id = intval($argv[2]);
95 $recipients = array();
96 $url_recipients = array();
100 if($cmd === 'mail') {
101 $normal_mode = false;
103 $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
106 if(! count($message)){
109 $uid = $message[0]['uid'];
110 $recipients[] = $message[0]['contact-id'];
114 elseif($cmd === 'expire') {
115 $normal_mode = false;
117 $items = q("SELECT * FROM `item` WHERE `uid` = %d AND `wall` = 1
118 AND `deleted` = 1 AND `changed` > UTC_TIMESTAMP() - INTERVAL 10 MINUTE",
126 elseif($cmd === 'suggest') {
127 $normal_mode = false;
130 $suggest = q("SELECT * FROM `fsuggest` WHERE `id` = %d LIMIT 1",
133 if(! count($suggest))
135 $uid = $suggest[0]['uid'];
136 $recipients[] = $suggest[0]['cid'];
138 } elseif($cmd === 'removeme') {
139 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval($item_id));
144 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1", intval($item_id));
149 $r = q("SELECT * FROM `contact` WHERE `self` = 0 AND `uid` = %d", intval($item_id));
153 require_once('include/Contact.php');
154 foreach($r as $contact) {
155 terminate_friendship($user, $self, $contact);
158 } elseif($cmd === 'relocate') {
159 $normal_mode = false;
164 $r = q("SELECT * FROM `item` WHERE `id` = %d and visible = 1 and moderated = 0 LIMIT 1",
168 if((! count($r)) || (! intval($r[0]['parent']))) {
172 $target_item = $r[0];
173 $parent_id = intval($r[0]['parent']);
175 $updated = $r[0]['edited'];
177 // POSSIBLE CLEANUP --> The following seems superfluous. We've already checked for "if (! intval($r[0]['parent']))" a few lines up
181 $items = q("SELECT `item`.*, `sign`.`signed_text`,`sign`.`signature`,`sign`.`signer`
182 FROM `item` LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id` WHERE `parent` = %d and visible = 1 and moderated = 0 ORDER BY `id` ASC",
186 if(! count($items)) {
190 // avoid race condition with deleting entries
192 if($items[0]['deleted']) {
193 foreach($items as $item)
194 $item['deleted'] = 1;
197 if((count($items) == 1) && ($items[0]['id'] === $target_item['id']) && ($items[0]['uri'] === $items[0]['parent-uri'])) {
198 logger('notifier: top level post');
204 $r = q("SELECT `contact`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`,
205 `user`.`timezone`, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`,
206 `user`.`page-flags`, `user`.`prvnets`
207 FROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
208 WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
217 $walltowall = ((($top_level) && ($owner['id'] != $items[0]['contact-id'])) ? true : false);
219 $hub = get_config('system','huburl');
221 // If this is a public conversation, notify the feed hub
222 $public_message = true;
224 // fill this in with a single salmon slap if applicable
227 if(! ($mail || $fsuggest || $relocate)) {
229 require_once('include/group.php');
233 // This is IMPORTANT!!!!
235 // We will only send a "notify owner to relay" or followup message if the referenced post
236 // originated on our system by virtue of having our hostname somewhere
237 // in the URI, AND it was a comment (not top_level) AND the parent originated elsewhere.
239 // if $parent['wall'] == 1 we will already have the parent message in our array
240 // and we will relay the whole lot.
242 // expire sends an entire group of expire messages and cannot be forwarded.
243 // However the conversation owner will be a part of the conversation and will
244 // be notified during this run.
245 // Other DFRN conversation members will be alerted during polled updates.
249 // Diaspora members currently are not notified of expirations, and other networks have
250 // either limited or no ability to process deletions. We should at least fix Diaspora
251 // by stringing togther an array of retractions and sending them onward.
254 $localhost = str_replace('www.','',$a->get_hostname());
255 if(strpos($localhost,':'))
256 $localhost = substr($localhost,0,strpos($localhost,':'));
260 * Be VERY CAREFUL if you make any changes to the following several lines. Seemingly innocuous changes
261 * have been known to cause runaway conditions which affected several servers, along with
262 * permissions issues.
266 $relay_to_owner = false;
268 if((! $top_level) && ($parent['wall'] == 0) && (! $expire) && (stristr($target_item['uri'],$localhost))) {
269 $relay_to_owner = true;
273 if(($cmd === 'uplink') && (intval($parent['forum_mode']) == 1) && (! $top_level)) {
274 $relay_to_owner = true;
277 // until the 'origin' flag has been in use for several months
278 // we will just use it as a fallback test
279 // later we will be able to use it as the primary test of whether or not to relay.
281 if(! $target_item['origin'])
282 $relay_to_owner = false;
284 if($parent['origin'])
285 $relay_to_owner = false;
289 if($relay_to_owner) {
290 logger('notifier: followup', LOGGER_DEBUG);
291 // local followup to remote post
293 $public_message = false; // not public
294 $conversant_str = dbesc($parent['contact-id']);
295 $recipients = array($parent['contact-id']);
297 if ($parent['network'] == NETWORK_OSTATUS) {
299 // Check if the recipient isn't in your contact list
300 $r = q("SELECT `url` FROM `contact` WHERE `id` = %d", $parent['contact-id']);
302 $url_recipients = array();
304 $thrparent = q("SELECT `author-link` FROM `item` WHERE `uri` = '%s'", dbesc($target_item["thr-parent"]));
305 if (count($thrparent) AND (normalise_link($r[0]["url"]) != normalise_link($thrparent[0]["author-link"]))) {
306 require_once("include/Scrape.php");
307 $probed_contact = probe_url($thrparent[0]["author-link"]);
308 if ($probed_contact["notify"] != "") {
309 logger('scrape data for slapper: '.print_r($probed_contact, true));
310 $url_recipients[$probed_contact["notify"]] = $probed_contact["notify"];
314 logger("url_recipients".print_r($url_recipients,true));
319 // don't send deletions onward for other people's stuff
321 if($target_item['deleted'] && (! intval($target_item['wall']))) {
322 logger('notifier: ignoring delete notification for non-wall item');
326 if((strlen($parent['allow_cid']))
327 || (strlen($parent['allow_gid']))
328 || (strlen($parent['deny_cid']))
329 || (strlen($parent['deny_gid']))) {
330 $public_message = false; // private recipients, not public
333 $allow_people = expand_acl($parent['allow_cid']);
334 $allow_groups = expand_groups(expand_acl($parent['allow_gid']),true);
335 $deny_people = expand_acl($parent['deny_cid']);
336 $deny_groups = expand_groups(expand_acl($parent['deny_gid']));
338 // if our parent is a public forum (forum_mode == 1), uplink to the origional author causing
339 // a delivery fork. private groups (forum_mode == 2) do not uplink
341 if((intval($parent['forum_mode']) == 1) && (! $top_level) && ($cmd !== 'uplink')) {
342 proc_run('php','include/notifier.php','uplink',$item_id);
345 $conversants = array();
347 foreach($items as $item) {
348 $recipients[] = $item['contact-id'];
349 $conversants[] = $item['contact-id'];
350 // pull out additional tagged people to notify (if public message)
351 if($public_message && strlen($item['inform'])) {
352 $people = explode(',',$item['inform']);
353 foreach($people as $person) {
354 if(substr($person,0,4) === 'cid:') {
355 $recipients[] = intval(substr($person,4));
356 $conversants[] = intval(substr($person,4));
359 $url_recipients[] = substr($person,4);
365 logger('notifier: url_recipients' . print_r($url_recipients,true));
367 $conversants = array_unique($conversants);
370 $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups));
371 $deny = array_unique(array_merge($deny_people,$deny_groups));
372 $recipients = array_diff($recipients,$deny);
374 $conversant_str = dbesc(implode(', ',$conversants));
377 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0");
383 $feed_template = get_markup_template('atom_feed.tpl');
384 $mail_template = get_markup_template('atom_mail.tpl');
389 $hubxml = feed_hublinks();
391 $birthday = feed_birthday($owner['uid'],$owner['timezone']);
393 if(strlen($birthday))
394 $birthday = '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>';
396 $atom .= replace_macros($feed_template, array(
397 '$version' => xmlify(FRIENDICA_VERSION),
398 '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
399 '$feed_title' => xmlify($owner['name']),
400 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
402 '$salmon' => '', // private feed, we don't use salmon here
403 '$name' => xmlify($owner['name']),
404 '$profile_page' => xmlify($owner['url']),
405 '$photo' => xmlify($owner['photo']),
406 '$thumb' => xmlify($owner['thumb']),
407 '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
408 '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
409 '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME)) ,
410 '$birthday' => $birthday,
411 '$community' => (($owner['page-flags'] == PAGE_COMMUNITY) ? '<dfrn:community>1</dfrn:community>' : '')
416 $public_message = false; // mail is not public
418 $body = fix_private_photos($item['body'],$owner['uid'],null,$message[0]['contact-id']);
420 $atom .= replace_macros($mail_template, array(
421 '$name' => xmlify($owner['name']),
422 '$profile_page' => xmlify($owner['url']),
423 '$thumb' => xmlify($owner['thumb']),
424 '$item_id' => xmlify($item['uri']),
425 '$subject' => xmlify($item['title']),
426 '$created' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
427 '$content' => xmlify($body),
428 '$parent_id' => xmlify($item['parent-uri'])
430 } elseif($fsuggest) {
431 $public_message = false; // suggestions are not public
433 $sugg_template = get_markup_template('atom_suggest.tpl');
435 $atom .= replace_macros($sugg_template, array(
436 '$name' => xmlify($item['name']),
437 '$url' => xmlify($item['url']),
438 '$photo' => xmlify($item['photo']),
439 '$request' => xmlify($item['request']),
440 '$note' => xmlify($item['note'])
443 // We don't need this any more
445 q("DELETE FROM `fsuggest` WHERE `id` = %d LIMIT 1",
449 } elseif($relocate) {
450 $public_message = false; // suggestions are not public
452 $sugg_template = get_markup_template('atom_relocate.tpl');
454 /* get site pubkey. this could be a new installation with no site keys*/
455 $pubkey = get_config('system','site_pubkey');
457 $res = new_keypair(1024);
458 set_config('system','site_prvkey', $res['prvkey']);
459 set_config('system','site_pubkey', $res['pubkey']);
462 $rp = q("SELECT `resource-id` , `scale`, type FROM `photo`
463 WHERE `profile` = 1 AND `uid` = %d ORDER BY scale;", $uid);
465 $ext = Photo::supportedTypes();
467 $photos[$p['scale']] = $a->get_baseurl().'/photo/'.$p['resource-id'].'-'.$p['scale'].'.'.$ext[$p['type']];
471 $atom .= replace_macros($sugg_template, array(
472 '$name' => xmlify($owner['name']),
473 '$photo' => xmlify($photos[4]),
474 '$thumb' => xmlify($photos[5]),
475 '$micro' => xmlify($photos[6]),
476 '$url' => xmlify($owner['url']),
477 '$request' => xmlify($owner['request']),
478 '$confirm' => xmlify($owner['confirm']),
479 '$notify' => xmlify($owner['notify']),
480 '$poll' => xmlify($owner['poll']),
481 '$sitepubkey' => xmlify(get_config('system','site_pubkey')),
482 //'$pubkey' => xmlify($owner['pubkey']),
483 //'$prvkey' => xmlify($owner['prvkey']),
485 $recipients_relocate = q("SELECT * FROM contact WHERE uid = %d AND self = 0 AND network = '%s'" , intval($uid), NETWORK_DFRN);
489 foreach($items as $item) { // there is only one item
490 if(! $item['parent'])
492 if($item['id'] == $item_id) {
493 logger('notifier: followup: item: ' . print_r($item,true), LOGGER_DATA);
494 $slap = atom_entry($item,'html',null,$owner,false);
495 $atom .= atom_entry($item,'text',null,$owner,false);
499 foreach($items as $item) {
501 if(! $item['parent'])
504 // private emails may be in included in public conversations. Filter them.
506 if(($public_message) && $item['private'] == 1)
510 $contact = get_item_contact($item,$contacts);
517 // we only need the current item, but include the parent because without it
518 // older sites without a corresponding dfrn_notify change may do the wrong thing.
520 if($item_id == $item['id'] || $item['id'] == $item['parent'])
521 $atom .= atom_entry($item,'text',null,$owner,true);
523 $atom .= atom_entry($item,'text',null,$owner,true);
525 if(($top_level) && ($public_message) && ($item['author-link'] === $item['owner-link']) && (! $expire))
526 $slaps[] = atom_entry($item,'html',null,$owner,true);
530 $atom .= '</feed>' . "\r\n";
532 logger('notifier: ' . $atom, LOGGER_DATA);
534 logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
536 // If this is a public message and pubmail is set on the parent, include all your email contacts
538 $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
540 if(! $mail_disabled) {
541 if((! strlen($target_item['allow_cid'])) && (! strlen($target_item['allow_gid']))
542 && (! strlen($target_item['deny_cid'])) && (! strlen($target_item['deny_gid']))
543 && (intval($target_item['pubmail']))) {
544 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `network` = '%s'",
550 $recipients[] = $rr['id'];
556 $recip_str = $parent['contact-id'];
558 $recip_str = implode(', ', $recipients);
561 $r = $recipients_relocate;
563 $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 AND `pending` = 0 ",
568 require_once('include/salmon.php');
570 $interval = ((get_config('system','delivery_interval') === false) ? 2 : intval(get_config('system','delivery_interval')));
576 foreach($r as $contact) {
577 if((! $mail) && (! $fsuggest) && (! $followup) && (!$relocate) && (! $contact['self'])) {
578 if(($contact['network'] === NETWORK_DIASPORA) && ($public_message))
580 q("insert into deliverq ( `cmd`,`item`,`contact` ) values ('%s', %d, %d )",
583 intval($contact['id'])
589 // This controls the number of deliveries to execute with each separate delivery process.
590 // By default we'll perform one delivery per process. Assuming a hostile shared hosting
591 // provider, this provides the greatest chance of deliveries if processes start getting
592 // killed. We can also space them out with the delivery_interval to also help avoid them
595 // If $deliveries_per_process > 1, we will chain this number of multiple deliveries
596 // together into a single process. This will reduce the overall number of processes
597 // spawned for each delivery, but they will run longer.
599 $deliveries_per_process = intval(get_config('system','delivery_batch_count'));
600 if($deliveries_per_process <= 0)
601 $deliveries_per_process = 1;
603 $this_batch = array();
605 for($x = 0; $x < count($r); $x ++) {
611 // potentially more than one recipient. Start a new process and space them out a bit.
612 // we will deliver single recipient types of message and email recipients here.
614 if((! $mail) && (! $fsuggest) && (!$relocate) && (! $followup)) {
616 $this_batch[] = $contact['id'];
618 if(count($this_batch) == $deliveries_per_process) {
619 proc_run('php','include/delivery.php',$cmd,$item_id,$this_batch);
620 $this_batch = array();
622 @time_sleep_until(microtime(true) + (float) $interval);
626 // be sure to pick up any stragglers
627 if(count($this_batch))
628 proc_run('php','include/delivery.php',$cmd,$item_id,$this_batch);
633 logger("main delivery by notifier: followup=$followup mail=$mail fsuggest=$fsuggest relocate=$relocate");
635 switch($contact['network']) {
638 // perform local delivery if we are on the same site
640 $basepath = implode('/', array_slice(explode('/',$contact['url']),0,3));
642 if(link_compare($basepath,$a->get_baseurl())) {
644 $nickname = basename($contact['url']);
645 if($contact['issued-id'])
646 $sql_extra = sprintf(" AND `dfrn-id` = '%s' ", dbesc($contact['issued-id']));
648 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($contact['dfrn-id']));
650 $x = q("SELECT `contact`.*, `contact`.`uid` AS `importer_uid`,
651 `contact`.`pubkey` AS `cpubkey`,
652 `contact`.`prvkey` AS `cprvkey`,
653 `contact`.`thumb` AS `thumb`,
654 `contact`.`url` as `url`,
655 `contact`.`name` as `senderName`,
658 INNER JOIN `user` ON `contact`.`uid` = `user`.`uid`
659 WHERE `contact`.`blocked` = 0 AND `contact`.`archive` = 0
660 AND `contact`.`pending` = 0
661 AND `contact`.`network` = '%s' AND `user`.`nickname` = '%s'
663 AND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 LIMIT 1",
668 if($x && count($x)) {
669 $write_flag = ((($x[0]['rel']) && ($x[0]['rel'] != CONTACT_IS_SHARING)) ? true : false);
670 if((($owner['page-flags'] == PAGE_COMMUNITY) || ($write_flag)) && (! $x[0]['writable'])) {
671 q("update contact set writable = 1 where id = %d",
674 $x[0]['writable'] = 1;
677 // if contact's ssl policy changed, which we just determined
678 // is on our own server, update our contact links
680 $ssl_policy = get_config('system','ssl_policy');
681 fix_contact_ssl_policy($x[0],$ssl_policy);
683 // If we are setup as a soapbox we aren't accepting input from this person
685 if($x[0]['page-flags'] == PAGE_SOAPBOX)
688 require_once('library/simplepie/simplepie.inc');
689 logger('mod-delivery: local delivery');
690 local_delivery($x[0],$atom);
695 logger('notifier: dfrndelivery: ' . $contact['name']);
696 $deliver_status = dfrn_deliver($owner,$contact,$atom);
698 logger('notifier: dfrn_delivery returns ' . $deliver_status);
700 if($deliver_status == (-1)) {
701 logger('notifier: delivery failed: queuing message');
702 // queue message for redelivery
703 add_to_queue($contact['id'],NETWORK_DFRN,$atom);
706 case NETWORK_OSTATUS:
708 // Do not send to ostatus if we are not configured to send to public networks
709 if($owner['prvnets'])
712 if(get_config('system','ostatus_disabled') || get_config('system','dfrn_only'))
715 if($followup && $contact['notify']) {
716 logger('notifier: slapdelivery: ' . $contact['name']);
717 $deliver_status = slapper($owner,$contact['notify'],$slap);
719 if($deliver_status == (-1)) {
720 // queue message for redelivery
721 add_to_queue($contact['id'],NETWORK_OSTATUS,$slap);
725 // only send salmon if public - e.g. if it's ok to notify
726 // a public hub, it's ok to send a salmon
728 if((count($slaps)) && ($public_message) && (! $expire)) {
729 logger('notifier: slapdelivery: ' . $contact['name']);
730 foreach($slaps as $slappy) {
731 if($contact['notify']) {
732 $deliver_status = slapper($owner,$contact['notify'],$slappy);
733 if($deliver_status == (-1)) {
734 // queue message for redelivery
735 add_to_queue($contact['id'],NETWORK_OSTATUS,$slappy);
746 if(get_config('system','dfrn_only'))
749 // WARNING: does not currently convert to RFC2047 header encodings, etc.
751 $addr = $contact['addr'];
755 if($cmd === 'wall-new' || $cmd === 'comment-new') {
758 if($cmd === 'wall-new')
761 $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
773 $local_user = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
776 if(! count($local_user))
780 $r1 = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
783 if($r1 && $r1[0]['reply_to'])
784 $reply_to = $r1[0]['reply_to'];
786 $subject = (($it['title']) ? email_header_encode($it['title'],'UTF-8') : t("\x28no subject\x29")) ;
788 // only expose our real email address to true friends
789 if(($contact['rel'] == CONTACT_IS_FRIEND) && (! $contact['blocked']))
791 $headers = 'From: ' . email_header_encode($local_user[0]['username'],'UTF-8') . ' <' . $reply_to . '>' . "\n";
792 $headers .= 'Sender: '.$local_user[0]['email']."\n";
794 $headers = 'From: ' . email_header_encode($local_user[0]['username'],'UTF-8') . ' <' . $local_user[0]['email'] . '>' . "\n";
796 $headers = 'From: ' . email_header_encode($local_user[0]['username'],'UTF-8') . ' <' . t('noreply') . '@' . $a->get_hostname() . '>' . "\n";
799 // $headers .= 'Reply-to: ' . $reply_to . "\n";
801 $headers .= 'Message-Id: <' . iri2msgid($it['uri']) . '>' . "\n";
803 if($it['uri'] !== $it['parent-uri']) {
804 $headers .= "References: <".iri2msgid($it["parent-uri"]).">";
806 // If Threading is enabled, write down the correct parent
807 if (($it["thr-parent"] != "") and ($it["thr-parent"] != $it["parent-uri"]))
808 $headers .= " <".iri2msgid($it["thr-parent"]).">";
812 $r = q("SELECT `title` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
813 dbesc($it['parent-uri']),
816 if(count($r) AND ($r[0]['title'] != ''))
817 $subject = $r[0]['title'];
819 $r = q("SELECT `title` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d LIMIT 1",
820 dbesc($it['parent-uri']),
823 if(count($r) AND ($r[0]['title'] != ''))
824 $subject = $r[0]['title'];
827 if(strncasecmp($subject,'RE:',3))
828 $subject = 'Re: '.$subject;
830 email_send($addr, $subject, $headers, $it);
833 case NETWORK_DIASPORA:
834 require_once('include/diaspora.php');
836 if(get_config('system','dfrn_only') || (! get_config('system','diaspora_enabled')))
840 diaspora_send_mail($item,$owner,$contact);
847 // special handling for followup to public post
848 // all other public posts processed as public batches further below
850 if($public_message) {
852 diaspora_send_followup($target_item,$owner,$contact, true);
856 if(! $contact['pubkey'])
859 if($target_item['verb'] === ACTIVITY_DISLIKE) {
863 elseif(($target_item['deleted']) && (($target_item['uri'] === $target_item['parent-uri']) || $followup)) {
864 // send both top-level retractions and relayable retractions for owner to relay
865 diaspora_send_retraction($target_item,$owner,$contact);
869 // send comments and likes to owner to relay
870 diaspora_send_followup($target_item,$owner,$contact);
873 elseif($target_item['uri'] !== $target_item['parent-uri']) {
874 // we are the relay - send comments, likes and relayable_retractions
875 // (of comments and likes) to our conversants
876 diaspora_send_relay($target_item,$owner,$contact);
879 elseif(($top_level) && (! $walltowall)) {
880 // currently no workable solution for sending walltowall
881 diaspora_send_status($target_item,$owner,$contact);
888 case NETWORK_FACEBOOK:
889 if(get_config('system','dfrn_only'))
892 if(get_config('system','dfrn_only'))
900 // send additional slaps to mentioned remote tags (@foo@example.com)
902 if($slap && count($url_recipients) && ($followup || $top_level) && $public_message && (! $expire)) {
903 if(! get_config('system','dfrn_only')) {
904 foreach($url_recipients as $url) {
906 logger('notifier: urldelivery: ' . $url);
907 $deliver_status = slapper($owner,$url,$slap);
908 // TODO: redeliver/queue these items on failure, though there is no contact record
915 if($public_message) {
917 $r1 = q("SELECT DISTINCT(`batch`), `id`, `name`,`network` FROM `contact` WHERE `network` = '%s'
918 AND `uid` = %d AND `rel` != %d group by `batch` ORDER BY rand() ",
919 dbesc(NETWORK_DIASPORA),
920 intval($owner['uid']),
921 intval(CONTACT_IS_SHARING)
924 $r2 = q("SELECT `id`, `name`,`network` FROM `contact`
925 WHERE `network` in ( '%s', '%s') AND `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
926 AND `rel` != %d order by rand() ",
928 dbesc(NETWORK_MAIL2),
929 intval($owner['uid']),
930 intval(CONTACT_IS_SHARING)
933 $r = array_merge($r2,$r1);
936 logger('pubdeliver: ' . print_r($r,true), LOGGER_DEBUG);
938 // throw everything into the queue in case we get killed
941 if((! $mail) && (! $fsuggest) && (! $followup)) {
942 q("insert into deliverq ( `cmd`,`item`,`contact` ) values ('%s', %d, %d )",
952 // except for Diaspora batch jobs
953 // Don't deliver to folks who have already been delivered to
955 if(($rr['network'] !== NETWORK_DIASPORA) && (in_array($rr['id'],$conversants))) {
956 logger('notifier: already delivered id=' . $rr['id']);
960 if((! $mail) && (! $fsuggest) && (! $followup)) {
961 logger('notifier: delivery agent: ' . $rr['name'] . ' ' . $rr['id']);
962 proc_run('php','include/delivery.php',$cmd,$item_id,$rr['id']);
964 @time_sleep_until(microtime(true) + (float) $interval);
971 $hubs = explode(',', $hub);
973 foreach($hubs as $h) {
978 if ($h === '[internal]') {
979 // Set push flag for PuSH subscribers to this topic,
980 // they will be notified in queue.php
981 q("UPDATE `push_subscriber` SET `push` = 1 " .
982 "WHERE `nickname` = '%s'", dbesc($owner['nickname']));
985 $params = 'hub.mode=publish&hub.url=' . urlencode( $a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
986 post_url($h,$params);
987 logger('pubsub: publish: ' . $h . ' ' . $params . ' returned ' . $a->get_curl_code());
990 sleep(7); // try and avoid multiple hubs responding at precisely the same time
997 // If the item was deleted, clean up the `sign` table
998 if($target_item['deleted']) {
999 $r = q("DELETE FROM sign where `retract_iid` = %d",
1000 intval($target_item['id'])
1004 logger('notifier: calling hooks', LOGGER_DEBUG);
1007 call_hooks('notifier_normal',$target_item);
1009 call_hooks('notifier_end',$target_item);
1015 if (array_search(__file__,get_included_files())===0){
1016 notifier_run($_SERVER["argv"],$_SERVER["argc"]);