2 require_once("boot.php");
3 require_once('include/queue_fn.php');
4 require_once('include/html2plain.php');
5 require_once("include/Scrape.php");
8 * This file was at one time responsible for doing all deliveries, but this caused
9 * big problems on shared hosting systems, where the process might get killed by the
10 * hosting provider and nothing would get delivered.
11 * It now only delivers one message under certain cases, and invokes a queued
12 * delivery mechanism (include/deliver.php) to deliver individual contacts at
13 * controlled intervals.
14 * This has a much better chance of surviving random processes getting killed
15 * by the hosting provider.
16 * A lot of this code is duplicated in include/deliver.php until we have time to go back
17 * and re-structure the delivery procedure based on the obstacles that have been thrown at
18 * us by hosting providers.
22 * The notifier is typically called with:
24 * proc_run('php', "include/notifier.php", COMMAND, ITEM_ID);
26 * where COMMAND is one of the following:
28 * activity (in diaspora.php, dfrn_confirm.php, profiles.php)
29 * comment-import (in diaspora.php, items.php)
30 * comment-new (in item.php)
31 * drop (in diaspora.php, items.php, photos.php)
32 * edit_post (in item.php)
33 * event (in events.php)
34 * expire (in items.php)
35 * like (in like.php, poke.php)
36 * mail (in message.php)
37 * suggest (in fsuggest.php)
38 * tag (in photos.php, poke.php, tagger.php)
39 * tgroup (in items.php)
40 * wall-new (in photos.php, item.php)
41 * removeme (in Contact.php)
42 * relocate (in uimport.php)
44 * and ITEM_ID is the id of the item in the database that needs to be sent to others.
48 function notifier_run(&$argv, &$argc){
56 @include(".htconfig.php");
57 require_once("include/dba.php");
58 $db = new dba($db_host, $db_user, $db_pass, $db_data);
59 unset($db_host, $db_user, $db_pass, $db_data);
62 require_once("include/session.php");
63 require_once("include/datetime.php");
64 require_once('include/items.php');
65 require_once('include/bbcode.php');
66 require_once('include/email.php');
67 load_config('config');
68 load_config('system');
75 $a->set_baseurl(get_config('system','url'));
77 logger('notifier: invoked: ' . print_r($argv,true), LOGGER_DEBUG);
84 $item_id = intval($argv[2]);
96 $recipients = array();
97 $url_recipients = array();
101 if($cmd === 'mail') {
102 $normal_mode = false;
104 $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
107 if(! count($message)){
110 $uid = $message[0]['uid'];
111 $recipients[] = $message[0]['contact-id'];
115 elseif($cmd === 'expire') {
116 $normal_mode = false;
118 $items = q("SELECT * FROM `item` WHERE `uid` = %d AND `wall` = 1
119 AND `deleted` = 1 AND `changed` > UTC_TIMESTAMP() - INTERVAL 10 MINUTE",
127 elseif($cmd === 'suggest') {
128 $normal_mode = false;
131 $suggest = q("SELECT * FROM `fsuggest` WHERE `id` = %d LIMIT 1",
134 if(! count($suggest))
136 $uid = $suggest[0]['uid'];
137 $recipients[] = $suggest[0]['cid'];
139 } elseif($cmd === 'removeme') {
140 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval($item_id));
145 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1", intval($item_id));
150 $r = q("SELECT * FROM `contact` WHERE `self` = 0 AND `uid` = %d", intval($item_id));
154 require_once('include/Contact.php');
155 foreach($r as $contact) {
156 terminate_friendship($user, $self, $contact);
159 } elseif($cmd === 'relocate') {
160 $normal_mode = false;
165 $r = q("SELECT * FROM `item` WHERE `id` = %d and visible = 1 and moderated = 0 LIMIT 1",
169 if((! count($r)) || (! intval($r[0]['parent']))) {
173 $target_item = $r[0];
174 $parent_id = intval($r[0]['parent']);
176 $updated = $r[0]['edited'];
178 // POSSIBLE CLEANUP --> The following seems superfluous. We've already checked for "if (! intval($r[0]['parent']))" a few lines up
182 $items = q("SELECT `item`.*, `sign`.`signed_text`,`sign`.`signature`,`sign`.`signer`
183 FROM `item` LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id` WHERE `parent` = %d and visible = 1 and moderated = 0 ORDER BY `id` ASC",
187 if(! count($items)) {
191 // avoid race condition with deleting entries
193 if($items[0]['deleted']) {
194 foreach($items as $item)
195 $item['deleted'] = 1;
198 if((count($items) == 1) && ($items[0]['id'] === $target_item['id']) && ($items[0]['uri'] === $items[0]['parent-uri'])) {
199 logger('notifier: top level post');
205 $r = q("SELECT `contact`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`,
206 `user`.`timezone`, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`,
207 `user`.`page-flags`, `user`.`prvnets`
208 FROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
209 WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
218 $walltowall = ((($top_level) && ($owner['id'] != $items[0]['contact-id'])) ? true : false);
220 $hub = get_config('system','huburl');
222 // If this is a public conversation, notify the feed hub
223 $public_message = true;
226 $push_notify = false;
228 // fill this in with a single salmon slap if applicable
231 if(! ($mail || $fsuggest || $relocate)) {
233 require_once('include/group.php');
237 // This is IMPORTANT!!!!
239 // We will only send a "notify owner to relay" or followup message if the referenced post
240 // originated on our system by virtue of having our hostname somewhere
241 // in the URI, AND it was a comment (not top_level) AND the parent originated elsewhere.
243 // if $parent['wall'] == 1 we will already have the parent message in our array
244 // and we will relay the whole lot.
246 // expire sends an entire group of expire messages and cannot be forwarded.
247 // However the conversation owner will be a part of the conversation and will
248 // be notified during this run.
249 // Other DFRN conversation members will be alerted during polled updates.
253 // Diaspora members currently are not notified of expirations, and other networks have
254 // either limited or no ability to process deletions. We should at least fix Diaspora
255 // by stringing togther an array of retractions and sending them onward.
258 $localhost = str_replace('www.','',$a->get_hostname());
259 if(strpos($localhost,':'))
260 $localhost = substr($localhost,0,strpos($localhost,':'));
264 * Be VERY CAREFUL if you make any changes to the following several lines. Seemingly innocuous changes
265 * have been known to cause runaway conditions which affected several servers, along with
266 * permissions issues.
270 $relay_to_owner = false;
272 if((! $top_level) && ($parent['wall'] == 0) && (! $expire) && (stristr($target_item['uri'],$localhost))) {
273 $relay_to_owner = true;
277 if(($cmd === 'uplink') && (intval($parent['forum_mode']) == 1) && (! $top_level)) {
278 $relay_to_owner = true;
281 // until the 'origin' flag has been in use for several months
282 // we will just use it as a fallback test
283 // later we will be able to use it as the primary test of whether or not to relay.
285 if(! $target_item['origin'])
286 $relay_to_owner = false;
288 if($parent['origin'])
289 $relay_to_owner = false;
291 if($relay_to_owner) {
292 logger('notifier: followup', LOGGER_DEBUG);
293 // local followup to remote post
295 $public_message = false; // not public
296 $conversant_str = dbesc($parent['contact-id']);
297 $recipients = array($parent['contact-id']);
299 if (!$target_item['private'] AND $target_item['wall'] AND
300 (strlen($target_item['allow_cid'].$target_item['allow_gid'].
301 $target_item['deny_cid'].$target_item['deny_gid']) == 0))
304 if ($parent['network'] == NETWORK_OSTATUS) {
305 logger('Parent is OStatus', LOGGER_DEBUG);
309 // Send a salmon notification to every person we mentioned in the post
310 // To-Do: Send a Salmon to every Friendica user in that thread
311 $arr = explode(',',$target_item['tag']);
312 foreach($arr as $x) {
313 logger('Checking tag '.$x, LOGGER_DEBUG);
315 if(preg_match('/@\[url=([^\]]*)\]/',$x,$matches)) {
316 $probed_contact = probe_url($matches[1]);
317 if ($probed_contact["notify"] != "") {
318 logger('scrape data for slapper: '.print_r($probed_contact, true));
319 $url_recipients[$probed_contact["notify"]] = $probed_contact["notify"];
325 // Check if the recipient isn't in your contact list, try to slap it
326 // Not sure if it is working or not.
327 $r = q("SELECT `url` FROM `contact` WHERE `id` = %d", $parent['contact-id']);
330 $thrparent = q("SELECT `author-link` FROM `item` WHERE `uri` = '%s'", dbesc($target_item["thr-parent"]));
331 if (count($thrparent) AND (normalise_link($r[0]["url"]) != normalise_link($thrparent[0]["author-link"]))) {
332 $probed_contact = probe_url($thrparent[0]["author-link"]);
333 if ($probed_contact["notify"] != "") {
334 logger('scrape data for slapper: '.print_r($probed_contact, true));
335 $url_recipients[$probed_contact["notify"]] = $probed_contact["notify"];
340 if (count($url_recipients))
341 logger("url_recipients ".print_r($url_recipients,true));
346 // don't send deletions onward for other people's stuff
348 if($target_item['deleted'] && (! intval($target_item['wall']))) {
349 logger('notifier: ignoring delete notification for non-wall item');
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
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']));
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
368 if((intval($parent['forum_mode']) == 1) && (! $top_level) && ($cmd !== 'uplink')) {
369 proc_run('php','include/notifier.php','uplink',$item_id);
372 $conversants = array();
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));
386 $url_recipients[] = substr($person,4);
392 if (count($url_recipients))
393 logger('notifier: url_recipients ' . print_r($url_recipients,true));
395 $conversants = array_unique($conversants);
398 $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups));
399 $deny = array_unique(array_merge($deny_people,$deny_groups));
400 $recipients = array_diff($recipients,$deny);
402 $conversant_str = dbesc(implode(', ',$conversants));
405 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0");
411 $feed_template = get_markup_template('atom_feed.tpl');
412 $mail_template = get_markup_template('atom_mail.tpl');
417 $hubxml = feed_hublinks();
419 $birthday = feed_birthday($owner['uid'],$owner['timezone']);
421 if(strlen($birthday))
422 $birthday = '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>';
424 $atom .= replace_macros($feed_template, array(
425 '$version' => xmlify(FRIENDICA_VERSION),
426 '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
427 '$feed_title' => xmlify($owner['name']),
428 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
430 '$salmon' => '', // private feed, we don't use salmon here
431 '$name' => xmlify($owner['name']),
432 '$profile_page' => xmlify($owner['url']),
433 '$photo' => xmlify($owner['photo']),
434 '$thumb' => xmlify($owner['thumb']),
435 '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
436 '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
437 '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME)) ,
438 '$birthday' => $birthday,
439 '$community' => (($owner['page-flags'] == PAGE_COMMUNITY) ? '<dfrn:community>1</dfrn:community>' : '')
444 $public_message = false; // mail is not public
446 $body = fix_private_photos($item['body'],$owner['uid'],null,$message[0]['contact-id']);
448 $atom .= replace_macros($mail_template, array(
449 '$name' => xmlify($owner['name']),
450 '$profile_page' => xmlify($owner['url']),
451 '$thumb' => xmlify($owner['thumb']),
452 '$item_id' => xmlify($item['uri']),
453 '$subject' => xmlify($item['title']),
454 '$created' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
455 '$content' => xmlify($body),
456 '$parent_id' => xmlify($item['parent-uri'])
458 } elseif($fsuggest) {
459 $public_message = false; // suggestions are not public
461 $sugg_template = get_markup_template('atom_suggest.tpl');
463 $atom .= replace_macros($sugg_template, array(
464 '$name' => xmlify($item['name']),
465 '$url' => xmlify($item['url']),
466 '$photo' => xmlify($item['photo']),
467 '$request' => xmlify($item['request']),
468 '$note' => xmlify($item['note'])
471 // We don't need this any more
473 q("DELETE FROM `fsuggest` WHERE `id` = %d LIMIT 1",
477 } elseif($relocate) {
478 $public_message = false; // suggestions are not public
480 $sugg_template = get_markup_template('atom_relocate.tpl');
482 /* get site pubkey. this could be a new installation with no site keys*/
483 $pubkey = get_config('system','site_pubkey');
485 $res = new_keypair(1024);
486 set_config('system','site_prvkey', $res['prvkey']);
487 set_config('system','site_pubkey', $res['pubkey']);
490 $rp = q("SELECT `resource-id` , `scale`, type FROM `photo`
491 WHERE `profile` = 1 AND `uid` = %d ORDER BY scale;", $uid);
493 $ext = Photo::supportedTypes();
495 $photos[$p['scale']] = $a->get_baseurl().'/photo/'.$p['resource-id'].'-'.$p['scale'].'.'.$ext[$p['type']];
499 $atom .= replace_macros($sugg_template, array(
500 '$name' => xmlify($owner['name']),
501 '$photo' => xmlify($photos[4]),
502 '$thumb' => xmlify($photos[5]),
503 '$micro' => xmlify($photos[6]),
504 '$url' => xmlify($owner['url']),
505 '$request' => xmlify($owner['request']),
506 '$confirm' => xmlify($owner['confirm']),
507 '$notify' => xmlify($owner['notify']),
508 '$poll' => xmlify($owner['poll']),
509 '$sitepubkey' => xmlify(get_config('system','site_pubkey')),
510 //'$pubkey' => xmlify($owner['pubkey']),
511 //'$prvkey' => xmlify($owner['prvkey']),
513 $recipients_relocate = q("SELECT * FROM contact WHERE uid = %d AND self = 0 AND network = '%s'" , intval($uid), NETWORK_DFRN);
517 foreach($items as $item) { // there is only one item
518 if(! $item['parent'])
520 if($item['id'] == $item_id) {
521 logger('notifier: followup: item: ' . print_r($item,true), LOGGER_DATA);
522 $slap = atom_entry($item,'html',null,$owner,false);
523 $atom .= atom_entry($item,'text',null,$owner,false);
527 foreach($items as $item) {
529 if(! $item['parent'])
532 // private emails may be in included in public conversations. Filter them.
534 if(($public_message) && $item['private'] == 1)
538 $contact = get_item_contact($item,$contacts);
545 // we only need the current item, but include the parent because without it
546 // older sites without a corresponding dfrn_notify change may do the wrong thing.
548 if($item_id == $item['id'] || $item['id'] == $item['parent'])
549 $atom .= atom_entry($item,'text',null,$owner,true);
551 $atom .= atom_entry($item,'text',null,$owner,true);
553 if(($top_level) && ($public_message) && ($item['author-link'] === $item['owner-link']) && (! $expire))
554 $slaps[] = atom_entry($item,'html',null,$owner,true);
558 $atom .= '</feed>' . "\r\n";
560 logger('notifier: ' . $atom, LOGGER_DATA);
562 logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
564 // If this is a public message and pubmail is set on the parent, include all your email contacts
566 $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
568 if(! $mail_disabled) {
569 if((! strlen($target_item['allow_cid'])) && (! strlen($target_item['allow_gid']))
570 && (! strlen($target_item['deny_cid'])) && (! strlen($target_item['deny_gid']))
571 && (intval($target_item['pubmail']))) {
572 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `network` = '%s'",
578 $recipients[] = $rr['id'];
584 $recip_str = $parent['contact-id'];
586 $recip_str = implode(', ', $recipients);
589 $r = $recipients_relocate;
591 $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 AND `pending` = 0 ",
596 require_once('include/salmon.php');
598 $interval = ((get_config('system','delivery_interval') === false) ? 2 : intval(get_config('system','delivery_interval')));
604 foreach($r as $contact) {
605 if((! $mail) && (! $fsuggest) && (! $followup) && (!$relocate) && (! $contact['self'])) {
606 if(($contact['network'] === NETWORK_DIASPORA) && ($public_message))
608 q("insert into deliverq ( `cmd`,`item`,`contact` ) values ('%s', %d, %d )",
611 intval($contact['id'])
617 // This controls the number of deliveries to execute with each separate delivery process.
618 // By default we'll perform one delivery per process. Assuming a hostile shared hosting
619 // provider, this provides the greatest chance of deliveries if processes start getting
620 // killed. We can also space them out with the delivery_interval to also help avoid them
623 // If $deliveries_per_process > 1, we will chain this number of multiple deliveries
624 // together into a single process. This will reduce the overall number of processes
625 // spawned for each delivery, but they will run longer.
627 $deliveries_per_process = intval(get_config('system','delivery_batch_count'));
628 if($deliveries_per_process <= 0)
629 $deliveries_per_process = 1;
631 $this_batch = array();
633 for($x = 0; $x < count($r); $x ++) {
639 logger("Deliver to ".$contact['url'], LOGGER_DEBUG);
641 // potentially more than one recipient. Start a new process and space them out a bit.
642 // we will deliver single recipient types of message and email recipients here.
644 if((! $mail) && (! $fsuggest) && (!$relocate) && (! $followup)) {
646 $this_batch[] = $contact['id'];
648 if(count($this_batch) == $deliveries_per_process) {
649 proc_run('php','include/delivery.php',$cmd,$item_id,$this_batch);
650 $this_batch = array();
652 @time_sleep_until(microtime(true) + (float) $interval);
656 // be sure to pick up any stragglers
657 if(count($this_batch))
658 proc_run('php','include/delivery.php',$cmd,$item_id,$this_batch);
663 logger("main delivery by notifier: followup=$followup mail=$mail fsuggest=$fsuggest relocate=$relocate");
665 switch($contact['network']) {
668 // perform local delivery if we are on the same site
670 $basepath = implode('/', array_slice(explode('/',$contact['url']),0,3));
672 if(link_compare($basepath,$a->get_baseurl())) {
674 $nickname = basename($contact['url']);
675 if($contact['issued-id'])
676 $sql_extra = sprintf(" AND `dfrn-id` = '%s' ", dbesc($contact['issued-id']));
678 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($contact['dfrn-id']));
680 $x = q("SELECT `contact`.*, `contact`.`uid` AS `importer_uid`,
681 `contact`.`pubkey` AS `cpubkey`,
682 `contact`.`prvkey` AS `cprvkey`,
683 `contact`.`thumb` AS `thumb`,
684 `contact`.`url` as `url`,
685 `contact`.`name` as `senderName`,
688 INNER JOIN `user` ON `contact`.`uid` = `user`.`uid`
689 WHERE `contact`.`blocked` = 0 AND `contact`.`archive` = 0
690 AND `contact`.`pending` = 0
691 AND `contact`.`network` = '%s' AND `user`.`nickname` = '%s'
693 AND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 LIMIT 1",
698 if($x && count($x)) {
699 $write_flag = ((($x[0]['rel']) && ($x[0]['rel'] != CONTACT_IS_SHARING)) ? true : false);
700 if((($owner['page-flags'] == PAGE_COMMUNITY) || ($write_flag)) && (! $x[0]['writable'])) {
701 q("update contact set writable = 1 where id = %d",
704 $x[0]['writable'] = 1;
707 // if contact's ssl policy changed, which we just determined
708 // is on our own server, update our contact links
710 $ssl_policy = get_config('system','ssl_policy');
711 fix_contact_ssl_policy($x[0],$ssl_policy);
713 // If we are setup as a soapbox we aren't accepting input from this person
715 if($x[0]['page-flags'] == PAGE_SOAPBOX)
718 require_once('library/simplepie/simplepie.inc');
719 logger('mod-delivery: local delivery');
720 local_delivery($x[0],$atom);
725 logger('notifier: dfrndelivery: ' . $contact['name']);
726 $deliver_status = dfrn_deliver($owner,$contact,$atom);
728 logger('notifier: dfrn_delivery returns ' . $deliver_status);
730 if($deliver_status == (-1)) {
731 logger('notifier: delivery failed: queuing message');
732 // queue message for redelivery
733 add_to_queue($contact['id'],NETWORK_DFRN,$atom);
736 case NETWORK_OSTATUS:
738 // Do not send to ostatus if we are not configured to send to public networks
739 if($owner['prvnets'])
742 if(get_config('system','ostatus_disabled') || get_config('system','dfrn_only'))
745 if($followup && $contact['notify']) {
746 logger('slapdelivery followup item '.$item_id.' to ' . $contact['name']);
747 $deliver_status = slapper($owner,$contact['notify'],$slap);
749 if($deliver_status == (-1)) {
750 // queue message for redelivery
751 add_to_queue($contact['id'],NETWORK_OSTATUS,$slap);
755 // only send salmon if public - e.g. if it's ok to notify
756 // a public hub, it's ok to send a salmon
758 if((count($slaps)) && ($public_message) && (! $expire)) {
759 logger('slapdelivery item '.$item_id.' to ' . $contact['name']);
760 foreach($slaps as $slappy) {
761 if($contact['notify']) {
762 $deliver_status = slapper($owner,$contact['notify'],$slappy);
763 if($deliver_status == (-1)) {
764 // queue message for redelivery
765 add_to_queue($contact['id'],NETWORK_OSTATUS,$slappy);
776 if(get_config('system','dfrn_only'))
779 // WARNING: does not currently convert to RFC2047 header encodings, etc.
781 $addr = $contact['addr'];
785 if($cmd === 'wall-new' || $cmd === 'comment-new') {
788 if($cmd === 'wall-new')
791 $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
803 $local_user = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
806 if(! count($local_user))
810 $r1 = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
813 if($r1 && $r1[0]['reply_to'])
814 $reply_to = $r1[0]['reply_to'];
816 $subject = (($it['title']) ? email_header_encode($it['title'],'UTF-8') : t("\x28no subject\x29")) ;
818 // only expose our real email address to true friends
819 if(($contact['rel'] == CONTACT_IS_FRIEND) && (! $contact['blocked']))
821 $headers = 'From: ' . email_header_encode($local_user[0]['username'],'UTF-8') . ' <' . $reply_to . '>' . "\n";
822 $headers .= 'Sender: '.$local_user[0]['email']."\n";
824 $headers = 'From: ' . email_header_encode($local_user[0]['username'],'UTF-8') . ' <' . $local_user[0]['email'] . '>' . "\n";
826 $headers = 'From: ' . email_header_encode($local_user[0]['username'],'UTF-8') . ' <' . t('noreply') . '@' . $a->get_hostname() . '>' . "\n";
829 // $headers .= 'Reply-to: ' . $reply_to . "\n";
831 $headers .= 'Message-Id: <' . iri2msgid($it['uri']) . '>' . "\n";
833 if($it['uri'] !== $it['parent-uri']) {
834 $headers .= "References: <".iri2msgid($it["parent-uri"]).">";
836 // If Threading is enabled, write down the correct parent
837 if (($it["thr-parent"] != "") and ($it["thr-parent"] != $it["parent-uri"]))
838 $headers .= " <".iri2msgid($it["thr-parent"]).">";
842 $r = q("SELECT `title` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
843 dbesc($it['parent-uri']),
846 if(count($r) AND ($r[0]['title'] != ''))
847 $subject = $r[0]['title'];
849 $r = q("SELECT `title` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d LIMIT 1",
850 dbesc($it['parent-uri']),
853 if(count($r) AND ($r[0]['title'] != ''))
854 $subject = $r[0]['title'];
857 if(strncasecmp($subject,'RE:',3))
858 $subject = 'Re: '.$subject;
860 email_send($addr, $subject, $headers, $it);
863 case NETWORK_DIASPORA:
864 require_once('include/diaspora.php');
866 if(get_config('system','dfrn_only') || (! get_config('system','diaspora_enabled')))
870 diaspora_send_mail($item,$owner,$contact);
877 // special handling for followup to public post
878 // all other public posts processed as public batches further below
880 if($public_message) {
882 diaspora_send_followup($target_item,$owner,$contact, true);
886 if(! $contact['pubkey'])
889 if($target_item['verb'] === ACTIVITY_DISLIKE) {
893 elseif(($target_item['deleted']) && (($target_item['uri'] === $target_item['parent-uri']) || $followup)) {
894 // send both top-level retractions and relayable retractions for owner to relay
895 diaspora_send_retraction($target_item,$owner,$contact);
899 // send comments and likes to owner to relay
900 diaspora_send_followup($target_item,$owner,$contact);
903 elseif($target_item['uri'] !== $target_item['parent-uri']) {
904 // we are the relay - send comments, likes and relayable_retractions
905 // (of comments and likes) to our conversants
906 diaspora_send_relay($target_item,$owner,$contact);
909 elseif(($top_level) && (! $walltowall)) {
910 // currently no workable solution for sending walltowall
911 diaspora_send_status($target_item,$owner,$contact);
918 case NETWORK_FACEBOOK:
919 if(get_config('system','dfrn_only'))
922 if(get_config('system','dfrn_only'))
930 // send additional slaps to mentioned remote tags (@foo@example.com)
932 if($slap && count($url_recipients) && ($followup || $top_level) && ($public_message || $push_notify) && (! $expire)) {
933 if(! get_config('system','dfrn_only')) {
934 foreach($url_recipients as $url) {
936 logger('notifier: urldelivery: ' . $url);
937 $deliver_status = slapper($owner,$url,$slap);
938 // TODO: redeliver/queue these items on failure, though there is no contact record
945 if($public_message) {
947 $r1 = q("SELECT DISTINCT(`batch`), `id`, `name`,`network` FROM `contact` WHERE `network` = '%s'
948 AND `uid` = %d AND `rel` != %d group by `batch` ORDER BY rand() ",
949 dbesc(NETWORK_DIASPORA),
950 intval($owner['uid']),
951 intval(CONTACT_IS_SHARING)
954 $r2 = q("SELECT `id`, `name`,`network` FROM `contact`
955 WHERE `network` in ( '%s', '%s') AND `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
956 AND `rel` != %d order by rand() ",
958 dbesc(NETWORK_MAIL2),
959 intval($owner['uid']),
960 intval(CONTACT_IS_SHARING)
963 $r = array_merge($r2,$r1);
966 logger('pubdeliver: ' . print_r($r,true), LOGGER_DEBUG);
968 // throw everything into the queue in case we get killed
971 if((! $mail) && (! $fsuggest) && (! $followup)) {
972 q("insert into deliverq ( `cmd`,`item`,`contact` ) values ('%s', %d, %d )",
982 // except for Diaspora batch jobs
983 // Don't deliver to folks who have already been delivered to
985 if(($rr['network'] !== NETWORK_DIASPORA) && (in_array($rr['id'],$conversants))) {
986 logger('notifier: already delivered id=' . $rr['id']);
990 if((! $mail) && (! $fsuggest) && (! $followup)) {
991 logger('notifier: delivery agent: ' . $rr['name'] . ' ' . $rr['id']);
992 proc_run('php','include/delivery.php',$cmd,$item_id,$rr['id']);
994 @time_sleep_until(microtime(true) + (float) $interval);
1004 if($push_notify AND strlen($hub)) {
1005 $hubs = explode(',', $hub);
1007 foreach($hubs as $h) {
1012 if ($h === '[internal]') {
1013 // Set push flag for PuSH subscribers to this topic,
1014 // they will be notified in queue.php
1015 q("UPDATE `push_subscriber` SET `push` = 1 " .
1016 "WHERE `nickname` = '%s'", dbesc($owner['nickname']));
1018 logger('Activating internal PuSH for item '.$item_id, LOGGER_DEBUG);
1022 $params = 'hub.mode=publish&hub.url=' . urlencode( $a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
1023 post_url($h,$params);
1024 logger('publish for item '.$item_id.' ' . $h . ' ' . $params . ' returned ' . $a->get_curl_code());
1026 if(count($hubs) > 1)
1027 sleep(7); // try and avoid multiple hubs responding at precisely the same time
1031 // Handling the pubsubhubbub requests
1032 proc_run('php','include/pubsubpublish.php');
1035 // If the item was deleted, clean up the `sign` table
1036 if($target_item['deleted']) {
1037 $r = q("DELETE FROM sign where `retract_iid` = %d",
1038 intval($target_item['id'])
1042 logger('notifier: calling hooks', LOGGER_DEBUG);
1045 call_hooks('notifier_normal',$target_item);
1047 call_hooks('notifier_end',$target_item);
1053 if (array_search(__file__,get_included_files())===0){
1054 notifier_run($_SERVER["argv"],$_SERVER["argc"]);