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');
28 $a->set_baseurl(get_config('system','url'));
30 logger('notifier: invoked: ' . print_r($argv,true));
38 $item_id = intval($argv[2]);
47 $recipients = array();
48 $url_recipients = array();
52 $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
55 if(! count($message)){
58 $uid = $message[0]['uid'];
59 $recipients[] = $message[0]['contact-id'];
63 elseif($cmd === 'expire') {
65 $items = q("SELECT * FROM `item` WHERE `uid` = %d AND `wall` = 1
66 AND `deleted` = 1 AND `changed` > UTC_TIMESTAMP - INTERVAL 10 MINUTE",
77 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
81 if((! count($r)) || (! intval($r[0]['parent']))) {
86 $parent_id = intval($r[0]['parent']);
88 $updated = $r[0]['edited'];
90 $items = q("SELECT * FROM `item` WHERE `parent` = %d ORDER BY `id` ASC",
98 // avoid race condition with deleting entries
100 if($items[0]['deleted']) {
101 foreach($items as $item)
102 $item['deleted'] = 1;
105 if(count($items) == 1 && $items[0]['uri'] === $items[0]['parent-uri'])
109 $r = q("SELECT `contact`.*, `user`.`timezone`, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`,
110 `user`.`page-flags`, `user`.`prvnets`
111 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
112 WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
121 $hub = get_config('system','huburl');
123 // If this is a public conversation, notify the feed hub
126 // fill this in with a single salmon slap if applicable
131 require_once('include/group.php');
135 if($parent['type'] === 'remote' && (! $expire)) {
136 // local followup to remote post
138 $notify_hub = false; // not public
139 $conversant_str = dbesc($parent['contact-id']);
144 if((strlen($parent['allow_cid']))
145 || (strlen($parent['allow_gid']))
146 || (strlen($parent['deny_cid']))
147 || (strlen($parent['deny_gid']))) {
148 $notify_hub = false; // private recipients, not public
151 $allow_people = expand_acl($parent['allow_cid']);
152 $allow_groups = expand_groups(expand_acl($parent['allow_gid']));
153 $deny_people = expand_acl($parent['deny_cid']);
154 $deny_groups = expand_groups(expand_acl($parent['deny_gid']));
156 $conversants = array();
158 foreach($items as $item) {
159 $recipients[] = $item['contact-id'];
160 $conversants[] = $item['contact-id'];
161 // pull out additional tagged people to notify (if public message)
162 if($notify_hub && strlen($item['inform'])) {
163 $people = explode(',',$item['inform']);
164 foreach($people as $person) {
165 if(substr($person,0,4) === 'cid:') {
166 $recipients[] = intval(substr($person,4));
167 $conversants[] = intval(substr($person,4));
170 $url_recipients[] = substr($person,4);
176 logger('notifier: url_recipients' . print_r($url_recipients,true));
178 $conversants = array_unique($conversants);
181 $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups));
182 $deny = array_unique(array_merge($deny_people,$deny_groups));
183 $recipients = array_diff($recipients,$deny);
185 $conversant_str = dbesc(implode(', ',$conversants));
188 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0");
195 $feed_template = get_markup_template('atom_feed.tpl');
196 $mail_template = get_markup_template('atom_mail.tpl');
201 $hubxml = feed_hublinks();
203 $birthday = feed_birthday($owner['uid'],$owner['timezone']);
205 if(strlen($birthday))
206 $birthday = '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>';
208 $atom .= replace_macros($feed_template, array(
209 '$version' => xmlify(FRIENDIKA_VERSION),
210 '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
211 '$feed_title' => xmlify($owner['name']),
212 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
214 '$salmon' => '', // private feed, we don't use salmon here
215 '$name' => xmlify($owner['name']),
216 '$profile_page' => xmlify($owner['url']),
217 '$photo' => xmlify($owner['photo']),
218 '$thumb' => xmlify($owner['thumb']),
219 '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
220 '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
221 '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME)) ,
222 '$birthday' => $birthday
225 if($cmd === 'mail') {
226 $notify_hub = false; // mail is not public
228 $atom .= replace_macros($mail_template, array(
229 '$name' => xmlify($owner['name']),
230 '$profile_page' => xmlify($owner['url']),
231 '$thumb' => xmlify($owner['thumb']),
232 '$item_id' => xmlify($item['uri']),
233 '$subject' => xmlify($item['title']),
234 '$created' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
235 '$content' => xmlify($item['body']),
236 '$parent_id' => xmlify($item['parent-uri'])
241 foreach($items as $item) { // there is only one item
242 if(! $item['parent'])
244 if($item['id'] == $item_id) {
245 logger('notifier: followup: item: ' . print_r($item,true), LOGGER_DATA);
246 $slap = atom_entry($item,'html',$owner,$owner,false);
247 $atom .= atom_entry($item,'text',$owner,$owner,false);
252 foreach($items as $item) {
254 if(! $item['parent'])
257 $contact = get_item_contact($item,$contacts);
261 $atom .= atom_entry($item,'text',$contact,$owner,true);
263 if(($top_level) && ($notify_hub) && ($item['author-link'] === $item['owner-link']) && (! $expire))
264 $slaps[] = atom_entry($item,'html',$contact,$owner,true);
268 $atom .= '</feed>' . "\r\n";
270 logger('notifier: ' . $atom, LOGGER_DATA);
272 logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
274 // If this is a public message and pubmail is set on the parent, include all your email contacts
276 $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
278 if(! $mail_disabled) {
279 if((! strlen($parent_item['allow_cid'])) && (! strlen($parent_item['allow_gid']))
280 && (! strlen($parent_item['deny_cid'])) && (! strlen($parent_item['deny_gid']))
281 && (intval($parent_item['pubmail']))) {
282 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `network` = '%s'",
288 $recipients[] = $rr['id'];
294 $recip_str = $parent['contact-id'];
296 $recip_str = implode(', ', $recipients);
298 $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 AND `pending` = 0 ",
304 require_once('include/salmon.php');
307 foreach($r as $contact) {
313 switch($contact['network']) {
315 logger('notifier: dfrndelivery: ' . $contact['name']);
316 $deliver_status = dfrn_deliver($owner,$contact,$atom);
318 logger('notifier: dfrn_delivery returns ' . $deliver_status);
320 if($deliver_status == (-1)) {
321 logger('notifier: delivery failed: queuing message');
322 // queue message for redelivery
323 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
324 VALUES ( %d, '%s', '%s', '%s') ",
325 intval($contact['id']),
326 dbesc(datetime_convert()),
327 dbesc(datetime_convert()),
333 if($owner['prvnets'])
335 if($followup && $contact['notify']) {
336 logger('notifier: slapdelivery: ' . $contact['name']);
337 $deliver_status = slapper($owner,$contact['notify'],$slap);
339 if($deliver_status == (-1)) {
340 // queue message for redelivery
341 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
342 VALUES ( %d, '%s', '%s', '%s') ",
343 intval($contact['id']),
344 dbesc(datetime_convert()),
345 dbesc(datetime_convert()),
355 // only send salmon if public - e.g. if it's ok to notify
356 // a public hub, it's ok to send a salmon
358 if((count($slaps)) && ($notify_hub) && (! $expire)) {
359 logger('notifier: slapdelivery: ' . $contact['name']);
360 foreach($slaps as $slappy) {
361 if($contact['notify']) {
362 $deliver_status = slapper($owner,$contact['notify'],$slappy);
363 if($deliver_status == (-1)) {
364 // queue message for redelivery
365 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
366 VALUES ( %d, '%s', '%s', '%s') ",
367 intval($contact['id']),
368 dbesc(datetime_convert()),
369 dbesc(datetime_convert()),
381 // WARNING: does not currently convert to RFC2047 header encodings, etc.
383 $addr = $contact['addr'];
387 if($cmd === 'wall-new' || $cmd === 'comment-new') {
390 if($cmd === 'wall-new')
393 $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
405 $local_user = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
408 if(! count($local_user))
412 $r1 = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
415 if($r1 && $r1[0]['reply_to'])
416 $reply_to = $r1[0]['reply_to'];
418 $subject = (($it['title']) ? $it['title'] : t("\x28no subject\x29")) ;
419 $headers = 'From: ' . $local_user[0]['username'] . ' <' . $local_user[0]['email'] . '>' . "\n";
422 $headers .= 'Reply-to: ' . $reply_to . "\n";
424 $headers .= 'Message-id: <' . $it['uri'] . '>' . "\n";
426 if($it['uri'] !== $it['parent-uri']) {
427 $header .= 'References: <' . $it['parent-uri'] . '>' . "\n";
428 if(! strlen($it['title'])) {
429 $r = q("SELECT `title` FROM `item` WHERE `parent-uri` = '%s' LIMIT 1",
430 dbesc($it['parent-uri'])
433 $subtitle = $r[0]['title'];
435 if(strncasecmp($subtitle,'RE:',3))
436 $subject = $subtitle;
438 $subject = 'Re: ' . $subtitle;
444 $headers .= 'MIME-Version: 1.0' . "\n";
445 $headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
446 $headers .= 'Content-Transfer-Encoding: 8bit' . "\n\n";
447 $html = prepare_body($it);
448 $message = '<html><body>' . $html . '</body></html>';
449 logger('notifier: email delivery to ' . $addr);
450 mail($addr, $subject, $message, $headers);
462 // send additional slaps to mentioned remote tags (@foo@example.com)
464 if($slap && count($url_recipients) && $followup && $notify_hub && (! $expire)) {
465 foreach($url_recipients as $url) {
467 logger('notifier: urldelivery: ' . $url);
468 $deliver_status = slapper($owner,$url,$slap);
469 // TODO: redeliver/queue these items on failure, though there is no contact record
474 if((strlen($hub)) && ($notify_hub)) {
475 $hubs = explode(',', $hub);
477 foreach($hubs as $h) {
481 $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
482 post_url($h,$params);
483 logger('pubsub: publish: ' . $h . ' ' . $params . ' returned ' . $a->get_curl_code());
485 sleep(7); // try and avoid multiple hubs responding at precisely the same time
494 * If you have less than 150 dfrn friends and it's a public message,
495 * we'll just go ahead and push them out securely with dfrn/rino.
496 * If you've got more than that, you'll have to rely on PuSH delivery.
500 $max_allowed = ((get_config('system','maxpubdeliver') === false) ? 150 : intval(get_config('system','maxpubdeliver')));
504 * Only get the bare essentials and go back for the full record.
505 * If you've got a lot of friends and we grab all the details at once it could exhaust memory.
509 $r = q("SELECT `id`, `name` FROM `contact`
510 WHERE `network` = 'dfrn' AND `uid` = %d AND `blocked` = 0 AND `pending` = 0
512 intval($owner['uid']),
516 if((count($r)) && (($max_allowed == 0) || (count($r) < $max_allowed))) {
518 logger('pubdeliver: ' . print_r($r,true));
522 /* Don't deliver to folks who have already been delivered to */
524 if(! in_array($rr['id'], $conversants)) {
525 $n = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
531 logger('notifier: dfrnpubdelivery: ' . $n[0]['name']);
532 $deliver_status = dfrn_deliver($owner,$n[0],$atom);
536 logger('notifier: dfrnpubdelivery: ignoring ' . $rr['name']);
544 if (array_search(__file__,get_included_files())===0){
546 notifier_run($argv,$argc);