2 require_once("boot.php");
4 function notifier_run($argv, $argc){
12 @include(".htconfig.php");
13 require_once("dba.php");
14 $db = new dba($db_host, $db_user, $db_pass, $db_data);
15 unset($db_host, $db_user, $db_pass, $db_data);
18 require_once("session.php");
19 require_once("datetime.php");
20 require_once('include/items.php');
21 require_once('include/bbcode.php');
23 load_config('config');
24 load_config('system');
31 $a->set_baseurl(get_config('system','url'));
33 logger('notifier: invoked: ' . print_r($argv,true));
41 $item_id = intval($argv[2]);
50 $recipients = array();
51 $url_recipients = array();
57 $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
60 if(! count($message)){
63 $uid = $message[0]['uid'];
64 $recipients[] = $message[0]['contact-id'];
68 elseif($cmd === 'expire') {
71 $items = q("SELECT * FROM `item` WHERE `uid` = %d AND `wall` = 1
72 AND `deleted` = 1 AND `changed` > UTC_TIMESTAMP - INTERVAL 10 MINUTE",
80 elseif($cmd === 'suggest') {
82 $suggest = q("SELECT * FROM `fsuggest` WHERE `id` = %d LIMIT 1",
87 $uid = $suggest[0]['uid'];
88 $recipients[] = $suggest[0]['cid'];
94 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
98 if((! count($r)) || (! intval($r[0]['parent']))) {
102 $target_item = $r[0];
103 $parent_id = intval($r[0]['parent']);
105 $updated = $r[0]['edited'];
107 $items = q("SELECT * FROM `item` WHERE `parent` = %d ORDER BY `id` ASC",
111 if(! count($items)) {
115 // avoid race condition with deleting entries
117 if($items[0]['deleted']) {
118 foreach($items as $item)
119 $item['deleted'] = 1;
122 if(count($items) == 1 && $items[0]['uri'] === $items[0]['parent-uri'])
126 $r = q("SELECT `contact`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`,
127 `user`.`timezone`, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`,
128 `user`.`page-flags`, `user`.`prvnets`
129 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
130 WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
139 $hub = get_config('system','huburl');
141 // If this is a public conversation, notify the feed hub
142 $public_message = true;
144 // fill this in with a single salmon slap if applicable
147 if($cmd != 'mail' && $cmd != 'suggest') {
149 require_once('include/group.php');
153 // This is IMPORTANT!!!!
155 // We will only send a "notify owner to relay" or followup message if the referenced post
156 // originated on our system by virtue of having our hostname somewhere
157 // in the URI, AND it was a comment (not top_level) AND the parent originated elsewhere.
158 // if $parent['wall'] == 1 we will already have the parent message in our array
159 // and we will relay the whole lot.
161 // expire sends an entire group of expire messages and cannot be forwarded.
162 // However the conversation owner will be a part of the conversation and will
163 // be notified during this run.
164 // Other DFRN conversation members will be alerted during polled updates.
166 // Diaspora members currently are not notified of expirations, and other networks have
167 // either limited or no ability to process deletions. We should at least fix Diaspora
168 // by stringing togther an array of retractions and sending them onward.
171 $localhost = $a->get_hostname();
172 if(strpos($localhost,':'))
173 $localhost = substr($localhost,0,strpos($localhost,':'));
177 * Be VERY CAREFUL if you make any changes to the following line. Seemingly innocuous changes
178 * have been known to cause runaway conditions which affected several servers, along with
179 * permissions issues.
183 if((! $top_level) && ($parent['wall'] == 0) && (! $expire) && (stristr($target_item['uri'],$localhost))) {
184 // local followup to remote post
186 $public_message = false; // not public
187 $conversant_str = dbesc($parent['contact-id']);
192 if((strlen($parent['allow_cid']))
193 || (strlen($parent['allow_gid']))
194 || (strlen($parent['deny_cid']))
195 || (strlen($parent['deny_gid']))) {
196 $public_message = false; // private recipients, not public
199 $allow_people = expand_acl($parent['allow_cid']);
200 $allow_groups = expand_groups(expand_acl($parent['allow_gid']));
201 $deny_people = expand_acl($parent['deny_cid']);
202 $deny_groups = expand_groups(expand_acl($parent['deny_gid']));
204 $conversants = array();
206 foreach($items as $item) {
207 $recipients[] = $item['contact-id'];
208 $conversants[] = $item['contact-id'];
209 // pull out additional tagged people to notify (if public message)
210 if($public_message && strlen($item['inform'])) {
211 $people = explode(',',$item['inform']);
212 foreach($people as $person) {
213 if(substr($person,0,4) === 'cid:') {
214 $recipients[] = intval(substr($person,4));
215 $conversants[] = intval(substr($person,4));
218 $url_recipients[] = substr($person,4);
224 logger('notifier: url_recipients' . print_r($url_recipients,true));
226 $conversants = array_unique($conversants);
229 $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups));
230 $deny = array_unique(array_merge($deny_people,$deny_groups));
231 $recipients = array_diff($recipients,$deny);
233 $conversant_str = dbesc(implode(', ',$conversants));
236 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0");
243 $feed_template = get_markup_template('atom_feed.tpl');
244 $mail_template = get_markup_template('atom_mail.tpl');
249 $hubxml = feed_hublinks();
251 $birthday = feed_birthday($owner['uid'],$owner['timezone']);
253 if(strlen($birthday))
254 $birthday = '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>';
256 $atom .= replace_macros($feed_template, array(
257 '$version' => xmlify(FRIENDIKA_VERSION),
258 '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
259 '$feed_title' => xmlify($owner['name']),
260 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
262 '$salmon' => '', // private feed, we don't use salmon here
263 '$name' => xmlify($owner['name']),
264 '$profile_page' => xmlify($owner['url']),
265 '$photo' => xmlify($owner['photo']),
266 '$thumb' => xmlify($owner['thumb']),
267 '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
268 '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
269 '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME)) ,
270 '$birthday' => $birthday
273 if($cmd === 'mail') {
274 $public_message = false; // mail is not public
276 $body = fix_private_photos($item['body'],$owner['uid']);
278 $atom .= replace_macros($mail_template, array(
279 '$name' => xmlify($owner['name']),
280 '$profile_page' => xmlify($owner['url']),
281 '$thumb' => xmlify($owner['thumb']),
282 '$item_id' => xmlify($item['uri']),
283 '$subject' => xmlify($item['title']),
284 '$created' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
285 '$content' => xmlify($body),
286 '$parent_id' => xmlify($item['parent-uri'])
289 elseif($cmd === 'suggest') {
290 $public_message = false; // suggestions are not public
292 $sugg_template = get_markup_template('atom_suggest.tpl');
294 $atom .= replace_macros($sugg_template, array(
295 '$name' => xmlify($item['name']),
296 '$url' => xmlify($item['url']),
297 '$photo' => xmlify($item['photo']),
298 '$request' => xmlify($item['request']),
299 '$note' => xmlify($item['note'])
302 // We don't need this any more
304 q("DELETE FROM `fsuggest` WHERE `id` = %d LIMIT 1",
311 foreach($items as $item) { // there is only one item
312 if(! $item['parent'])
314 if($item['id'] == $item_id) {
315 logger('notifier: followup: item: ' . print_r($item,true), LOGGER_DATA);
316 $slap = atom_entry($item,'html',$owner,$owner,false);
317 $atom .= atom_entry($item,'text',$owner,$owner,false);
322 foreach($items as $item) {
324 if(! $item['parent'])
327 // private emails may be in included in public conversations. Filter them.
329 if(($public_message) && $item['private'])
332 $contact = get_item_contact($item,$contacts);
336 $atom .= atom_entry($item,'text',$contact,$owner,true);
338 if(($top_level) && ($public_message) && ($item['author-link'] === $item['owner-link']) && (! $expire))
339 $slaps[] = atom_entry($item,'html',$contact,$owner,true);
343 $atom .= '</feed>' . "\r\n";
345 logger('notifier: ' . $atom, LOGGER_DATA);
347 logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
349 // If this is a public message and pubmail is set on the parent, include all your email contacts
351 $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
353 if(! $mail_disabled) {
354 if((! strlen($target_item['allow_cid'])) && (! strlen($target_item['allow_gid']))
355 && (! strlen($target_item['deny_cid'])) && (! strlen($target_item['deny_gid']))
356 && (intval($target_item['pubmail']))) {
357 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `network` = '%s'",
363 $recipients[] = $rr['id'];
369 $recip_str = $parent['contact-id'];
371 $recip_str = implode(', ', $recipients);
373 $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 AND `pending` = 0 ",
379 require_once('include/salmon.php');
382 foreach($r as $contact) {
388 switch($contact['network']) {
390 logger('notifier: dfrndelivery: ' . $contact['name']);
391 $deliver_status = dfrn_deliver($owner,$contact,$atom);
393 logger('notifier: dfrn_delivery returns ' . $deliver_status);
395 if($deliver_status == (-1)) {
396 logger('notifier: delivery failed: queuing message');
397 // queue message for redelivery
398 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
399 VALUES ( %d, '%s', '%s', '%s') ",
400 intval($contact['id']),
401 dbesc(datetime_convert()),
402 dbesc(datetime_convert()),
407 case NETWORK_OSTATUS:
409 // Do not send to otatus if we are not configured to send to public networks
410 if($owner['prvnets'])
412 if(get_config('system','ostatus_disabled') || get_config('system','dfrn_only'))
415 if($followup && $contact['notify']) {
416 logger('notifier: slapdelivery: ' . $contact['name']);
417 $deliver_status = slapper($owner,$contact['notify'],$slap);
419 if($deliver_status == (-1)) {
420 // queue message for redelivery
421 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
422 VALUES ( %d, '%s', '%s', '%s') ",
423 intval($contact['id']),
424 dbesc(datetime_convert()),
425 dbesc(datetime_convert()),
433 // only send salmon if public - e.g. if it's ok to notify
434 // a public hub, it's ok to send a salmon
436 if((count($slaps)) && ($public_message) && (! $expire)) {
437 logger('notifier: slapdelivery: ' . $contact['name']);
438 foreach($slaps as $slappy) {
439 if($contact['notify']) {
440 $deliver_status = slapper($owner,$contact['notify'],$slappy);
441 if($deliver_status == (-1)) {
442 // queue message for redelivery
443 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
444 VALUES ( %d, '%s', '%s', '%s') ",
445 intval($contact['id']),
446 dbesc(datetime_convert()),
447 dbesc(datetime_convert()),
459 if(get_config('system','dfrn_only'))
462 // WARNING: does not currently convert to RFC2047 header encodings, etc.
464 $addr = $contact['addr'];
468 if($cmd === 'wall-new' || $cmd === 'comment-new') {
471 if($cmd === 'wall-new')
474 $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
486 $local_user = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
489 if(! count($local_user))
493 $r1 = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
496 if($r1 && $r1[0]['reply_to'])
497 $reply_to = $r1[0]['reply_to'];
499 $subject = (($it['title']) ? $it['title'] : t("\x28no subject\x29")) ;
500 $headers = 'From: ' . $local_user[0]['username'] . ' <' . $local_user[0]['email'] . '>' . "\n";
503 $headers .= 'Reply-to: ' . $reply_to . "\n";
505 $headers .= 'Message-id: <' . $it['uri'] . '>' . "\n";
507 if($it['uri'] !== $it['parent-uri']) {
508 $header .= 'References: <' . $it['parent-uri'] . '>' . "\n";
509 if(! strlen($it['title'])) {
510 $r = q("SELECT `title` FROM `item` WHERE `parent-uri` = '%s' LIMIT 1",
511 dbesc($it['parent-uri'])
514 $subtitle = $r[0]['title'];
516 if(strncasecmp($subtitle,'RE:',3))
517 $subject = $subtitle;
519 $subject = 'Re: ' . $subtitle;
525 $headers .= 'MIME-Version: 1.0' . "\n";
526 $headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
527 $headers .= 'Content-Transfer-Encoding: 8bit' . "\n\n";
528 $html = prepare_body($it);
529 $message = '<html><body>' . $html . '</body></html>';
530 logger('notifier: email delivery to ' . $addr);
531 mail($addr, $subject, $message, $headers);
534 case NETWORK_DIASPORA:
535 require_once('include/diaspora.php');
536 if(get_config('system','dfrn_only') || (! get_config('system','diaspora_enabled')) || (! $normal_mode))
539 if(! $contact['pubkey'])
542 if($target_item['verb'] === ACTIVITY_DISLIKE) {
546 elseif(($target_item['deleted']) && ($target_item['verb'] !== ACTIVITY_LIKE)) {
548 diaspora_send_retraction($target_item,$owner,$contact);
552 // send comments, likes and retractions of likes to owner to relay
553 diaspora_send_followup($target_item,$owner,$contact);
556 elseif($target_item['parent'] != $target_item['id']) {
557 // we are the relay - send comments, likes and unlikes to our conversants
558 diaspora_send_relay($target_item,$owner,$contact);
562 diaspora_send_status($target_item,$owner,$contact);
569 case NETWORK_FACEBOOK:
570 if(get_config('system','dfrn_only'))
578 // send additional slaps to mentioned remote tags (@foo@example.com)
580 if($slap && count($url_recipients) && ($followup || $top_level) && $public_message && (! $expire)) {
581 if(! get_config('system','dfrn_only')) {
582 foreach($url_recipients as $url) {
584 logger('notifier: urldelivery: ' . $url);
585 $deliver_status = slapper($owner,$url,$slap);
586 // TODO: redeliver/queue these items on failure, though there is no contact record
592 if((strlen($hub)) && ($public_message)) {
593 $hubs = explode(',', $hub);
595 foreach($hubs as $h) {
599 $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
600 post_url($h,$params);
601 logger('pubsub: publish: ' . $h . ' ' . $params . ' returned ' . $a->get_curl_code());
603 sleep(7); // try and avoid multiple hubs responding at precisely the same time
608 if($public_message) {
612 * If you have less than 999 dfrn friends and it's a public message,
613 * we'll just go ahead and push them out securely with dfrn/rino or Diaspora.
614 * If you've got more than that, you'll have to rely on PuSH delivery.
618 $max_allowed = ((get_config('system','maxpubdeliver') === false) ? 999 : intval(get_config('system','maxpubdeliver')));
622 * Only get the bare essentials and go back for the full record.
623 * If you've got a lot of friends and we grab all the details at once it could exhaust memory.
627 $r = q("SELECT `id`, `name` FROM `contact`
628 WHERE `network` in ('%s','%s') AND `uid` = %d AND `blocked` = 0 AND `pending` = 0
631 dbesc(NETWORK_DIASPORA),
632 intval($owner['uid']),
633 intval(CONTACT_IS_SHARING)
636 if((count($r)) && (($max_allowed == 0) || (count($r) < $max_allowed))) {
638 logger('pubdeliver: ' . print_r($r,true));
642 /* Don't deliver to folks who have already been delivered to */
644 if(in_array($rr['id'],$conversants)) {
645 logger('notifier: already delivered id=' . $rr['id']);
649 $n = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
655 logger('pubdeliver: network: ' . $contact['network']);
657 switch($contact['network']) {
659 logger('notifier: dfrnpubdelivery: ' . $contact['name']);
660 $deliver_status = dfrn_deliver($owner,$contact,$atom);
662 case NETWORK_DIASPORA :
663 require_once('include/diaspora.php');
665 logger('notifier: diaspora pubdelivery: ' . $contact['name']);
667 if(get_config('system','dfrn_only') || (! get_config('system','diaspora_enabled')) || (! $normal_mode)) {
668 logger('notifier: diaspora pubdelivery not allowed at this time');
672 if(! $contact['pubkey']) {
673 logger('notifier: diaspora pubdelivery: no pubkey');
677 if($target_item['verb'] === ACTIVITY_DISLIKE) {
681 elseif(($target_item['deleted']) && ($target_item['verb'] !== ACTIVITY_LIKE)) {
683 diaspora_send_retraction($target_item,$owner,$contact);
687 // send comments, likes and retractions of likes to owner to relay
688 diaspora_send_followup($target_item,$owner,$contact);
691 elseif($target_item['parent'] != $target_item['id']) {
692 // we are the relay - send comments, likes and unlikes to our conversants
693 diaspora_send_relay($target_item,$owner,$contact);
697 diaspora_send_status($target_item,$owner,$contact);
711 if (array_search(__file__,get_included_files())===0){
712 notifier_run($argv,$argc);