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();
55 $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
58 if(! count($message)){
61 $uid = $message[0]['uid'];
62 $recipients[] = $message[0]['contact-id'];
66 elseif($cmd === 'expire') {
68 $items = q("SELECT * FROM `item` WHERE `uid` = %d AND `wall` = 1
69 AND `deleted` = 1 AND `changed` > UTC_TIMESTAMP - INTERVAL 10 MINUTE",
77 elseif($cmd === 'suggest') {
78 $suggest = q("SELECT * FROM `fsuggest` WHERE `id` = %d LIMIT 1",
83 $uid = $suggest[0]['uid'];
84 $recipients[] = $suggest[0]['cid'];
90 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
94 if((! count($r)) || (! intval($r[0]['parent']))) {
99 $parent_id = intval($r[0]['parent']);
101 $updated = $r[0]['edited'];
103 $items = q("SELECT * FROM `item` WHERE `parent` = %d ORDER BY `id` ASC",
107 if(! count($items)) {
111 // avoid race condition with deleting entries
113 if($items[0]['deleted']) {
114 foreach($items as $item)
115 $item['deleted'] = 1;
118 if(count($items) == 1 && $items[0]['uri'] === $items[0]['parent-uri'])
122 $r = q("SELECT `contact`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`,
123 `user`.`timezone`, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`,
124 `user`.`page-flags`, `user`.`prvnets`
125 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
126 WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
135 $hub = get_config('system','huburl');
137 // If this is a public conversation, notify the feed hub
140 // fill this in with a single salmon slap if applicable
143 if($cmd != 'mail' && $cmd != 'suggest') {
145 require_once('include/group.php');
149 if($parent['wall'] == 0 && (! $expire)) {
150 // local followup to remote post
152 $notify_hub = false; // not public
153 $conversant_str = dbesc($parent['contact-id']);
158 if((strlen($parent['allow_cid']))
159 || (strlen($parent['allow_gid']))
160 || (strlen($parent['deny_cid']))
161 || (strlen($parent['deny_gid']))) {
162 $notify_hub = false; // private recipients, not public
165 $allow_people = expand_acl($parent['allow_cid']);
166 $allow_groups = expand_groups(expand_acl($parent['allow_gid']));
167 $deny_people = expand_acl($parent['deny_cid']);
168 $deny_groups = expand_groups(expand_acl($parent['deny_gid']));
170 $conversants = array();
172 foreach($items as $item) {
173 $recipients[] = $item['contact-id'];
174 $conversants[] = $item['contact-id'];
175 // pull out additional tagged people to notify (if public message)
176 if($notify_hub && strlen($item['inform'])) {
177 $people = explode(',',$item['inform']);
178 foreach($people as $person) {
179 if(substr($person,0,4) === 'cid:') {
180 $recipients[] = intval(substr($person,4));
181 $conversants[] = intval(substr($person,4));
184 $url_recipients[] = substr($person,4);
190 logger('notifier: url_recipients' . print_r($url_recipients,true));
192 $conversants = array_unique($conversants);
195 $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups));
196 $deny = array_unique(array_merge($deny_people,$deny_groups));
197 $recipients = array_diff($recipients,$deny);
199 $conversant_str = dbesc(implode(', ',$conversants));
202 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0");
209 $feed_template = get_markup_template('atom_feed.tpl');
210 $mail_template = get_markup_template('atom_mail.tpl');
215 $hubxml = feed_hublinks();
217 $birthday = feed_birthday($owner['uid'],$owner['timezone']);
219 if(strlen($birthday))
220 $birthday = '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>';
222 $atom .= replace_macros($feed_template, array(
223 '$version' => xmlify(FRIENDIKA_VERSION),
224 '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
225 '$feed_title' => xmlify($owner['name']),
226 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
228 '$salmon' => '', // private feed, we don't use salmon here
229 '$name' => xmlify($owner['name']),
230 '$profile_page' => xmlify($owner['url']),
231 '$photo' => xmlify($owner['photo']),
232 '$thumb' => xmlify($owner['thumb']),
233 '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
234 '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
235 '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME)) ,
236 '$birthday' => $birthday
239 if($cmd === 'mail') {
240 $notify_hub = false; // mail is not public
242 $body = fix_private_photos($item['body'],$owner['uid']);
244 $atom .= replace_macros($mail_template, array(
245 '$name' => xmlify($owner['name']),
246 '$profile_page' => xmlify($owner['url']),
247 '$thumb' => xmlify($owner['thumb']),
248 '$item_id' => xmlify($item['uri']),
249 '$subject' => xmlify($item['title']),
250 '$created' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
251 '$content' => xmlify($body),
252 '$parent_id' => xmlify($item['parent-uri'])
255 elseif($cmd === 'suggest') {
256 $notify_hub = false; // suggestions are not public
258 $sugg_template = get_markup_template('atom_suggest.tpl');
260 $atom .= replace_macros($sugg_template, array(
261 '$name' => xmlify($item['name']),
262 '$url' => xmlify($item['url']),
263 '$photo' => xmlify($item['photo']),
264 '$request' => xmlify($item['request']),
265 '$note' => xmlify($item['note'])
268 // We don't need this any more
270 q("DELETE FROM `fsuggest` WHERE `id` = %d LIMIT 1",
277 foreach($items as $item) { // there is only one item
278 if(! $item['parent'])
280 if($item['id'] == $item_id) {
281 logger('notifier: followup: item: ' . print_r($item,true), LOGGER_DATA);
282 $slap = atom_entry($item,'html',$owner,$owner,false);
283 $atom .= atom_entry($item,'text',$owner,$owner,false);
288 foreach($items as $item) {
290 if(! $item['parent'])
293 // private emails may be in included in public conversations. Filter them.
295 if(($notify_hub) && $item['private'])
298 $contact = get_item_contact($item,$contacts);
302 $atom .= atom_entry($item,'text',$contact,$owner,true);
304 if(($top_level) && ($notify_hub) && ($item['author-link'] === $item['owner-link']) && (! $expire))
305 $slaps[] = atom_entry($item,'html',$contact,$owner,true);
309 $atom .= '</feed>' . "\r\n";
311 logger('notifier: ' . $atom, LOGGER_DATA);
313 logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
315 // If this is a public message and pubmail is set on the parent, include all your email contacts
317 $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
319 if(! $mail_disabled) {
320 if((! strlen($parent_item['allow_cid'])) && (! strlen($parent_item['allow_gid']))
321 && (! strlen($parent_item['deny_cid'])) && (! strlen($parent_item['deny_gid']))
322 && (intval($parent_item['pubmail']))) {
323 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `network` = '%s'",
329 $recipients[] = $rr['id'];
335 $recip_str = $parent['contact-id'];
337 $recip_str = implode(', ', $recipients);
339 $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 AND `pending` = 0 ",
345 require_once('include/salmon.php');
348 foreach($r as $contact) {
354 switch($contact['network']) {
356 logger('notifier: dfrndelivery: ' . $contact['name']);
357 $deliver_status = dfrn_deliver($owner,$contact,$atom);
359 logger('notifier: dfrn_delivery returns ' . $deliver_status);
361 if($deliver_status == (-1)) {
362 logger('notifier: delivery failed: queuing message');
363 // queue message for redelivery
364 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
365 VALUES ( %d, '%s', '%s', '%s') ",
366 intval($contact['id']),
367 dbesc(datetime_convert()),
368 dbesc(datetime_convert()),
373 case NETWORK_OSTATUS:
375 // Do not send to otatus if we are not configured to send to public networks
376 if($owner['prvnets'])
378 if(get_config('system','ostatus_disabled') || get_config('system','dfrn_only'))
381 if($followup && $contact['notify']) {
382 logger('notifier: slapdelivery: ' . $contact['name']);
383 $deliver_status = slapper($owner,$contact['notify'],$slap);
385 if($deliver_status == (-1)) {
386 // queue message for redelivery
387 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
388 VALUES ( %d, '%s', '%s', '%s') ",
389 intval($contact['id']),
390 dbesc(datetime_convert()),
391 dbesc(datetime_convert()),
399 // only send salmon if public - e.g. if it's ok to notify
400 // a public hub, it's ok to send a salmon
402 if((count($slaps)) && ($notify_hub) && (! $expire)) {
403 logger('notifier: slapdelivery: ' . $contact['name']);
404 foreach($slaps as $slappy) {
405 if($contact['notify']) {
406 $deliver_status = slapper($owner,$contact['notify'],$slappy);
407 if($deliver_status == (-1)) {
408 // queue message for redelivery
409 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
410 VALUES ( %d, '%s', '%s', '%s') ",
411 intval($contact['id']),
412 dbesc(datetime_convert()),
413 dbesc(datetime_convert()),
425 if(get_config('system','dfrn_only'))
428 // WARNING: does not currently convert to RFC2047 header encodings, etc.
430 $addr = $contact['addr'];
434 if($cmd === 'wall-new' || $cmd === 'comment-new') {
437 if($cmd === 'wall-new')
440 $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
452 $local_user = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
455 if(! count($local_user))
459 $r1 = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
462 if($r1 && $r1[0]['reply_to'])
463 $reply_to = $r1[0]['reply_to'];
465 $subject = (($it['title']) ? $it['title'] : t("\x28no subject\x29")) ;
466 $headers = 'From: ' . $local_user[0]['username'] . ' <' . $local_user[0]['email'] . '>' . "\n";
469 $headers .= 'Reply-to: ' . $reply_to . "\n";
471 $headers .= 'Message-id: <' . $it['uri'] . '>' . "\n";
473 if($it['uri'] !== $it['parent-uri']) {
474 $header .= 'References: <' . $it['parent-uri'] . '>' . "\n";
475 if(! strlen($it['title'])) {
476 $r = q("SELECT `title` FROM `item` WHERE `parent-uri` = '%s' LIMIT 1",
477 dbesc($it['parent-uri'])
480 $subtitle = $r[0]['title'];
482 if(strncasecmp($subtitle,'RE:',3))
483 $subject = $subtitle;
485 $subject = 'Re: ' . $subtitle;
491 $headers .= 'MIME-Version: 1.0' . "\n";
492 $headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
493 $headers .= 'Content-Transfer-Encoding: 8bit' . "\n\n";
494 $html = prepare_body($it);
495 $message = '<html><body>' . $html . '</body></html>';
496 logger('notifier: email delivery to ' . $addr);
497 mail($addr, $subject, $message, $headers);
500 case NETWORK_DIASPORA:
501 if(get_config('system','dfrn_only') || (! get_config('diaspora_enabled')))
504 diaspora_send_status($parent_item,$owner,$contact);
511 case NETWORK_FACEBOOK:
512 if(get_config('system','dfrn_only'))
520 // send additional slaps to mentioned remote tags (@foo@example.com)
522 if($slap && count($url_recipients) && ($followup || $top_level) && $notify_hub && (! $expire)) {
523 if(! get_config('system','dfrn_only')) {
524 foreach($url_recipients as $url) {
526 logger('notifier: urldelivery: ' . $url);
527 $deliver_status = slapper($owner,$url,$slap);
528 // TODO: redeliver/queue these items on failure, though there is no contact record
534 if((strlen($hub)) && ($notify_hub)) {
535 $hubs = explode(',', $hub);
537 foreach($hubs as $h) {
541 $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
542 post_url($h,$params);
543 logger('pubsub: publish: ' . $h . ' ' . $params . ' returned ' . $a->get_curl_code());
545 sleep(7); // try and avoid multiple hubs responding at precisely the same time
554 * If you have less than 150 dfrn friends and it's a public message,
555 * we'll just go ahead and push them out securely with dfrn/rino.
556 * If you've got more than that, you'll have to rely on PuSH delivery.
560 $max_allowed = ((get_config('system','maxpubdeliver') === false) ? 150 : intval(get_config('system','maxpubdeliver')));
564 * Only get the bare essentials and go back for the full record.
565 * If you've got a lot of friends and we grab all the details at once it could exhaust memory.
569 $r = q("SELECT `id`, `name` FROM `contact`
570 WHERE `network` = 'dfrn' AND `uid` = %d AND `blocked` = 0 AND `pending` = 0
572 intval($owner['uid']),
573 intval(CONTACT_IS_SHARING)
576 if((count($r)) && (($max_allowed == 0) || (count($r) < $max_allowed))) {
578 logger('pubdeliver: ' . print_r($r,true));
582 /* Don't deliver to folks who have already been delivered to */
584 if(! in_array($rr['id'], $conversants)) {
585 $n = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
591 logger('notifier: dfrnpubdelivery: ' . $n[0]['name']);
592 $deliver_status = dfrn_deliver($owner,$n[0],$atom);
596 logger('notifier: dfrnpubdelivery: ignoring ' . $rr['name']);
604 if (array_search(__file__,get_included_files())===0){
605 notifier_run($argv,$argc);