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');
26 $a->set_baseurl(get_config('system','url'));
28 logger('notifier: invoked: ' . print_r($argv,true));
36 $item_id = intval($argv[2]);
45 $recipients = array();
46 $url_recipients = array();
50 $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
53 if(! count($message)){
56 $uid = $message[0]['uid'];
57 $recipients[] = $message[0]['contact-id'];
61 elseif($cmd === 'expire') {
63 $items = q("SELECT * FROM `item` WHERE `uid` = %d AND `wall` = 1
64 AND `deleted` = 1 AND `changed` > UTC_TIMESTAMP - INTERVAL 10 MINUTE",
75 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
79 if((! count($r)) || (! intval($r[0]['parent']))) {
84 $parent_id = intval($r[0]['parent']);
86 $updated = $r[0]['edited'];
88 $items = q("SELECT * FROM `item` WHERE `parent` = %d ORDER BY `id` ASC",
96 // avoid race condition with deleting entries
98 if($items[0]['deleted']) {
99 foreach($items as $item)
100 $item['deleted'] = 1;
103 if(count($items) == 1 && $items[0]['uri'] === $items[0]['parent-uri'])
107 $r = q("SELECT `contact`.*, `user`.`timezone`, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`, `user`.`page-flags`
108 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
109 WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
118 $hub = get_config('system','huburl');
120 // If this is a public conversation, notify the feed hub
123 // fill this in with a single salmon slap if applicable
128 require_once('include/group.php');
132 if($parent['type'] === 'remote' && (! $expire)) {
133 // local followup to remote post
135 $notify_hub = false; // not public
136 $conversant_str = dbesc($parent['contact-id']);
141 if((strlen($parent['allow_cid']))
142 || (strlen($parent['allow_gid']))
143 || (strlen($parent['deny_cid']))
144 || (strlen($parent['deny_gid']))) {
145 $notify_hub = false; // private recipients, not public
148 $allow_people = expand_acl($parent['allow_cid']);
149 $allow_groups = expand_groups(expand_acl($parent['allow_gid']));
150 $deny_people = expand_acl($parent['deny_cid']);
151 $deny_groups = expand_groups(expand_acl($parent['deny_gid']));
153 $conversants = array();
155 foreach($items as $item) {
156 $recipients[] = $item['contact-id'];
157 $conversants[] = $item['contact-id'];
158 // pull out additional tagged people to notify (if public message)
159 if($notify_hub && strlen($item['inform'])) {
160 $people = explode(',',$item['inform']);
161 foreach($people as $person) {
162 if(substr($person,0,4) === 'cid:') {
163 $recipients[] = intval(substr($person,4));
164 $conversants[] = intval(substr($person,4));
167 $url_recipients[] = substr($person,4);
173 logger('notifier: url_recipients' . print_r($url_recipients,true));
175 $conversants = array_unique($conversants);
178 $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups));
179 $deny = array_unique(array_merge($deny_people,$deny_groups));
180 $recipients = array_diff($recipients,$deny);
182 $conversant_str = dbesc(implode(', ',$conversants));
185 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0");
192 $feed_template = load_view_file('view/atom_feed.tpl');
193 $mail_template = load_view_file('view/atom_mail.tpl');
198 $hubxml = feed_hublinks();
200 $birthday = feed_birthday($owner['uid'],$owner['timezone']);
202 if(strlen($birthday))
203 $birthday = '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>';
205 $atom .= replace_macros($feed_template, array(
206 '$version' => xmlify(FRIENDIKA_VERSION),
207 '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
208 '$feed_title' => xmlify($owner['name']),
209 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
211 '$salmon' => '', // private feed, we don't use salmon here
212 '$name' => xmlify($owner['name']),
213 '$profile_page' => xmlify($owner['url']),
214 '$photo' => xmlify($owner['photo']),
215 '$thumb' => xmlify($owner['thumb']),
216 '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
217 '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
218 '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME)) ,
219 '$birthday' => $birthday
222 if($cmd === 'mail') {
223 $notify_hub = false; // mail is not public
225 $atom .= replace_macros($mail_template, array(
226 '$name' => xmlify($owner['name']),
227 '$profile_page' => xmlify($owner['url']),
228 '$thumb' => xmlify($owner['thumb']),
229 '$item_id' => xmlify($item['uri']),
230 '$subject' => xmlify($item['title']),
231 '$created' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
232 '$content' => xmlify($item['body']),
233 '$parent_id' => xmlify($item['parent-uri'])
238 foreach($items as $item) { // there is only one item
239 if(! $item['parent'])
241 if($item['id'] == $item_id) {
242 logger('notifier: followup: item: ' . print_r($item,true), LOGGER_DATA);
243 $slap = atom_entry($item,'html',$owner,$owner,false);
244 $atom .= atom_entry($item,'text',$owner,$owner,false);
249 foreach($items as $item) {
251 if(! $item['parent'])
254 $contact = get_item_contact($item,$contacts);
258 $atom .= atom_entry($item,'text',$contact,$owner,true);
260 if(($top_level) && ($notify_hub) && ($item['author-link'] === $item['owner-link']) && (! $expire))
261 $slaps[] = atom_entry($item,'html',$contact,$owner,true);
265 $atom .= '</feed>' . "\r\n";
267 logger('notifier: ' . $atom, LOGGER_DATA);
269 logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
271 // If this is a public message and pubmail is set on the parent, include all your email contacts
273 $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
275 if(! $mail_disabled) {
276 if((! strlen($parent_item['allow_cid'])) && (! strlen($parent_item['allow_gid']))
277 && (! strlen($parent_item['deny_cid'])) && (! strlen($parent_item['deny_gid']))
278 && (intval($parent_item['pubmail']))) {
279 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `network` = '%s'",
285 $recipients[] = $rr['id'];
291 $recip_str = $parent['contact-id'];
293 $recip_str = implode(', ', $recipients);
295 $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 AND `pending` = 0 ",
301 require_once('include/salmon.php');
304 foreach($r as $contact) {
310 switch($contact['network']) {
312 logger('notifier: dfrndelivery: ' . $contact['name']);
313 $deliver_status = dfrn_deliver($owner,$contact,$atom);
315 logger('notifier: dfrn_delivery returns ' . $deliver_status);
317 if($deliver_status == (-1)) {
318 logger('notifier: delivery failed: queuing message');
319 // queue message for redelivery
320 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
321 VALUES ( %d, '%s', '%s', '%s') ",
322 intval($contact['id']),
323 dbesc(datetime_convert()),
324 dbesc(datetime_convert()),
330 if($followup && $contact['notify']) {
331 logger('notifier: slapdelivery: ' . $contact['name']);
332 $deliver_status = slapper($owner,$contact['notify'],$slap);
334 if($deliver_status == (-1)) {
335 // queue message for redelivery
336 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
337 VALUES ( %d, '%s', '%s', '%s') ",
338 intval($contact['id']),
339 dbesc(datetime_convert()),
340 dbesc(datetime_convert()),
350 // only send salmon if public - e.g. if it's ok to notify
351 // a public hub, it's ok to send a salmon
353 if((count($slaps)) && ($notify_hub) && (! $expire)) {
354 logger('notifier: slapdelivery: ' . $contact['name']);
355 foreach($slaps as $slappy) {
356 if($contact['notify']) {
357 $deliver_status = slapper($owner,$contact['notify'],$slappy);
358 if($deliver_status == (-1)) {
359 // queue message for redelivery
360 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
361 VALUES ( %d, '%s', '%s', '%s') ",
362 intval($contact['id']),
363 dbesc(datetime_convert()),
364 dbesc(datetime_convert()),
375 // WARNING: does not currently convert to RFC2047 header encodings, etc.
377 $addr = $contact['addr'];
381 if($cmd === 'wall-new' || $cmd === 'comment-new') {
384 if($cmd === 'wall-new')
387 $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
399 $local_user = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
402 if(! count($local_user))
406 $r1 = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
409 if($r1 && $r1[0]['reply_to'])
410 $reply_to = $r1[0]['reply_to'];
412 $subject = (($it['title']) ? $it['title'] : t("\x28no subject\x29")) ;
413 $headers = 'From: ' . $local_user[0]['username'] . ' <' . $local_user[0]['email'] . '>' . "\n";
416 $headers .= 'Reply-to: ' . $reply_to . "\n";
418 $headers .= 'Message-id: <' . $it['uri'] . '>' . "\n";
420 if($it['uri'] !== $it['parent-uri']) {
421 $header .= 'References: <' . $it['parent-uri'] . '>' . "\n";
422 if(! strlen($it['title'])) {
423 $r = q("SELECT `title` FROM `item` WHERE `parent-uri` = '%s' LIMIT 1",
424 dbesc($it['parent-uri'])
427 $subtitle = $r[0]['title'];
429 if(strncasecmp($subtitle,'RE:',3))
430 $subject = $subtitle;
432 $subject = 'Re: ' . $subtitle;
438 $headers .= 'MIME-Version: 1.0' . "\n";
439 $headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
440 $headers .= 'Content-Transfer-Encoding: 8bit' . "\n\n";
441 $html = prepare_body($it);
442 $message = '<html><body>' . $html . '</body></html>';
443 logger('notifier: email delivery to ' . $addr);
444 mail($addr, $subject, $message, $headers);
456 // send additional slaps to mentioned remote tags (@foo@example.com)
458 if($slap && count($url_recipients) && $followup && $notify_hub && (! $expire)) {
459 foreach($url_recipients as $url) {
461 logger('notifier: urldelivery: ' . $url);
462 $deliver_status = slapper($owner,$url,$slap);
463 // TODO: redeliver/queue these items on failure, though there is no contact record
468 if((strlen($hub)) && ($notify_hub)) {
469 $hubs = explode(',', $hub);
471 foreach($hubs as $h) {
475 $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
476 post_url($h,$params);
477 logger('pubsub: publish: ' . $h . ' ' . $params . ' returned ' . $a->get_curl_code());
479 sleep(7); // try and avoid multiple hubs responding at precisely the same time
488 * If you have less than 150 dfrn friends and it's a public message,
489 * we'll just go ahead and push them out securely with dfrn/rino.
490 * If you've got more than that, you'll have to rely on PuSH delivery.
494 $max_allowed = ((get_config('system','maxpubdeliver') === false) ? 150 : intval(get_config('system','maxpubdeliver')));
498 * Only get the bare essentials and go back for the full record.
499 * If you've got a lot of friends and we grab all the details at once it could exhaust memory.
503 $r = q("SELECT `id`, `name` FROM `contact`
504 WHERE `network` = 'dfrn' AND `uid` = %d AND `blocked` = 0 AND `pending` = 0
506 intval($owner['uid']),
510 if((count($r)) && (($max_allowed == 0) || (count($r) < $max_allowed))) {
514 /* Don't deliver to folks who have already been delivered to */
516 if(! in_array($rr['id'], $conversants)) {
517 $n = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
523 logger('notifier: dfrnpubdelivery: ' . $n[0]['name']);
524 $deliver_status = dfrn_deliver($owner,$n[0],$atom);
528 logger('notifier: dfrnpubdelivery: ignoring ' . $rr['name']);
536 if (array_search(__file__,get_included_files())===0){
538 notifier_run($argv,$argc);