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`, `user`.`page-flags`
110 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
111 WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
120 $hub = get_config('system','huburl');
122 // If this is a public conversation, notify the feed hub
125 // fill this in with a single salmon slap if applicable
130 require_once('include/group.php');
134 if($parent['type'] === 'remote' && (! $expire)) {
135 // local followup to remote post
137 $notify_hub = false; // not public
138 $conversant_str = dbesc($parent['contact-id']);
143 if((strlen($parent['allow_cid']))
144 || (strlen($parent['allow_gid']))
145 || (strlen($parent['deny_cid']))
146 || (strlen($parent['deny_gid']))) {
147 $notify_hub = false; // private recipients, not public
150 $allow_people = expand_acl($parent['allow_cid']);
151 $allow_groups = expand_groups(expand_acl($parent['allow_gid']));
152 $deny_people = expand_acl($parent['deny_cid']);
153 $deny_groups = expand_groups(expand_acl($parent['deny_gid']));
155 $conversants = array();
157 foreach($items as $item) {
158 $recipients[] = $item['contact-id'];
159 $conversants[] = $item['contact-id'];
160 // pull out additional tagged people to notify (if public message)
161 if($notify_hub && strlen($item['inform'])) {
162 $people = explode(',',$item['inform']);
163 foreach($people as $person) {
164 if(substr($person,0,4) === 'cid:') {
165 $recipients[] = intval(substr($person,4));
166 $conversants[] = intval(substr($person,4));
169 $url_recipients[] = substr($person,4);
175 logger('notifier: url_recipients' . print_r($url_recipients,true));
177 $conversants = array_unique($conversants);
180 $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups));
181 $deny = array_unique(array_merge($deny_people,$deny_groups));
182 $recipients = array_diff($recipients,$deny);
184 $conversant_str = dbesc(implode(', ',$conversants));
187 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0");
194 $feed_template = load_view_file('view/atom_feed.tpl');
195 $mail_template = load_view_file('view/atom_mail.tpl');
200 $hubxml = feed_hublinks();
202 $birthday = feed_birthday($owner['uid'],$owner['timezone']);
204 if(strlen($birthday))
205 $birthday = '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>';
207 $atom .= replace_macros($feed_template, array(
208 '$version' => xmlify(FRIENDIKA_VERSION),
209 '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
210 '$feed_title' => xmlify($owner['name']),
211 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
213 '$salmon' => '', // private feed, we don't use salmon here
214 '$name' => xmlify($owner['name']),
215 '$profile_page' => xmlify($owner['url']),
216 '$photo' => xmlify($owner['photo']),
217 '$thumb' => xmlify($owner['thumb']),
218 '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
219 '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
220 '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME)) ,
221 '$birthday' => $birthday
224 if($cmd === 'mail') {
225 $notify_hub = false; // mail is not public
227 $atom .= replace_macros($mail_template, array(
228 '$name' => xmlify($owner['name']),
229 '$profile_page' => xmlify($owner['url']),
230 '$thumb' => xmlify($owner['thumb']),
231 '$item_id' => xmlify($item['uri']),
232 '$subject' => xmlify($item['title']),
233 '$created' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
234 '$content' => xmlify($item['body']),
235 '$parent_id' => xmlify($item['parent-uri'])
240 foreach($items as $item) { // there is only one item
241 if(! $item['parent'])
243 if($item['id'] == $item_id) {
244 logger('notifier: followup: item: ' . print_r($item,true), LOGGER_DATA);
245 $slap = atom_entry($item,'html',$owner,$owner,false);
246 $atom .= atom_entry($item,'text',$owner,$owner,false);
251 foreach($items as $item) {
253 if(! $item['parent'])
256 $contact = get_item_contact($item,$contacts);
260 $atom .= atom_entry($item,'text',$contact,$owner,true);
262 if(($top_level) && ($notify_hub) && ($item['author-link'] === $item['owner-link']) && (! $expire))
263 $slaps[] = atom_entry($item,'html',$contact,$owner,true);
267 $atom .= '</feed>' . "\r\n";
269 logger('notifier: ' . $atom, LOGGER_DATA);
271 logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
273 // If this is a public message and pubmail is set on the parent, include all your email contacts
275 $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
277 if(! $mail_disabled) {
278 if((! strlen($parent_item['allow_cid'])) && (! strlen($parent_item['allow_gid']))
279 && (! strlen($parent_item['deny_cid'])) && (! strlen($parent_item['deny_gid']))
280 && (intval($parent_item['pubmail']))) {
281 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `network` = '%s'",
287 $recipients[] = $rr['id'];
293 $recip_str = $parent['contact-id'];
295 $recip_str = implode(', ', $recipients);
297 $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 AND `pending` = 0 ",
303 require_once('include/salmon.php');
306 foreach($r as $contact) {
312 switch($contact['network']) {
314 logger('notifier: dfrndelivery: ' . $contact['name']);
315 $deliver_status = dfrn_deliver($owner,$contact,$atom);
317 logger('notifier: dfrn_delivery returns ' . $deliver_status);
319 if($deliver_status == (-1)) {
320 logger('notifier: delivery failed: queuing message');
321 // queue message for redelivery
322 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
323 VALUES ( %d, '%s', '%s', '%s') ",
324 intval($contact['id']),
325 dbesc(datetime_convert()),
326 dbesc(datetime_convert()),
332 if($followup && $contact['notify']) {
333 logger('notifier: slapdelivery: ' . $contact['name']);
334 $deliver_status = slapper($owner,$contact['notify'],$slap);
336 if($deliver_status == (-1)) {
337 // queue message for redelivery
338 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
339 VALUES ( %d, '%s', '%s', '%s') ",
340 intval($contact['id']),
341 dbesc(datetime_convert()),
342 dbesc(datetime_convert()),
352 // only send salmon if public - e.g. if it's ok to notify
353 // a public hub, it's ok to send a salmon
355 if((count($slaps)) && ($notify_hub) && (! $expire)) {
356 logger('notifier: slapdelivery: ' . $contact['name']);
357 foreach($slaps as $slappy) {
358 if($contact['notify']) {
359 $deliver_status = slapper($owner,$contact['notify'],$slappy);
360 if($deliver_status == (-1)) {
361 // queue message for redelivery
362 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
363 VALUES ( %d, '%s', '%s', '%s') ",
364 intval($contact['id']),
365 dbesc(datetime_convert()),
366 dbesc(datetime_convert()),
377 // WARNING: does not currently convert to RFC2047 header encodings, etc.
379 $addr = $contact['addr'];
383 if($cmd === 'wall-new' || $cmd === 'comment-new') {
386 if($cmd === 'wall-new')
389 $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
401 $local_user = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
404 if(! count($local_user))
408 $r1 = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
411 if($r1 && $r1[0]['reply_to'])
412 $reply_to = $r1[0]['reply_to'];
414 $subject = (($it['title']) ? $it['title'] : t("\x28no subject\x29")) ;
415 $headers = 'From: ' . $local_user[0]['username'] . ' <' . $local_user[0]['email'] . '>' . "\n";
418 $headers .= 'Reply-to: ' . $reply_to . "\n";
420 $headers .= 'Message-id: <' . $it['uri'] . '>' . "\n";
422 if($it['uri'] !== $it['parent-uri']) {
423 $header .= 'References: <' . $it['parent-uri'] . '>' . "\n";
424 if(! strlen($it['title'])) {
425 $r = q("SELECT `title` FROM `item` WHERE `parent-uri` = '%s' LIMIT 1",
426 dbesc($it['parent-uri'])
429 $subtitle = $r[0]['title'];
431 if(strncasecmp($subtitle,'RE:',3))
432 $subject = $subtitle;
434 $subject = 'Re: ' . $subtitle;
440 $headers .= 'MIME-Version: 1.0' . "\n";
441 $headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
442 $headers .= 'Content-Transfer-Encoding: 8bit' . "\n\n";
443 $html = prepare_body($it);
444 $message = '<html><body>' . $html . '</body></html>';
445 logger('notifier: email delivery to ' . $addr);
446 mail($addr, $subject, $message, $headers);
458 // send additional slaps to mentioned remote tags (@foo@example.com)
460 if($slap && count($url_recipients) && $followup && $notify_hub && (! $expire)) {
461 foreach($url_recipients as $url) {
463 logger('notifier: urldelivery: ' . $url);
464 $deliver_status = slapper($owner,$url,$slap);
465 // TODO: redeliver/queue these items on failure, though there is no contact record
470 if((strlen($hub)) && ($notify_hub)) {
471 $hubs = explode(',', $hub);
473 foreach($hubs as $h) {
477 $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
478 post_url($h,$params);
479 logger('pubsub: publish: ' . $h . ' ' . $params . ' returned ' . $a->get_curl_code());
481 sleep(7); // try and avoid multiple hubs responding at precisely the same time
490 * If you have less than 150 dfrn friends and it's a public message,
491 * we'll just go ahead and push them out securely with dfrn/rino.
492 * If you've got more than that, you'll have to rely on PuSH delivery.
496 $max_allowed = ((get_config('system','maxpubdeliver') === false) ? 150 : intval(get_config('system','maxpubdeliver')));
500 * Only get the bare essentials and go back for the full record.
501 * If you've got a lot of friends and we grab all the details at once it could exhaust memory.
505 $r = q("SELECT `id`, `name` FROM `contact`
506 WHERE `network` = 'dfrn' AND `uid` = %d AND `blocked` = 0 AND `pending` = 0
508 intval($owner['uid']),
512 if((count($r)) && (($max_allowed == 0) || (count($r) < $max_allowed))) {
516 /* Don't deliver to folks who have already been delivered to */
518 if(! in_array($rr['id'], $conversants)) {
519 $n = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
525 logger('notifier: dfrnpubdelivery: ' . $n[0]['name']);
526 $deliver_status = dfrn_deliver($owner,$n[0],$atom);
530 logger('notifier: dfrnpubdelivery: ignoring ' . $rr['name']);
538 if (array_search(__file__,get_included_files())===0){
540 notifier_run($argv,$argc);