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 if((! strlen($parent_item['allow_cid'])) && (! strlen($parent_item['allow_gid'])) && (! strlen($parent_item['deny_cid'])) && (! strlen($parent_item['deny_gid']))
274 && (intval($parent_item['pubmail']))) {
275 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `network` = '%s'",
281 $recipients[] = $rr['id'];
286 $recip_str = $parent['contact-id'];
288 $recip_str = implode(', ', $recipients);
290 $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 AND `pending` = 0 ",
296 require_once('include/salmon.php');
299 foreach($r as $contact) {
305 switch($contact['network']) {
307 logger('notifier: dfrndelivery: ' . $contact['name']);
308 $deliver_status = dfrn_deliver($owner,$contact,$atom);
310 logger('notifier: dfrn_delivery returns ' . $deliver_status);
312 if($deliver_status == (-1)) {
313 logger('notifier: delivery failed: queuing message');
314 // queue message for redelivery
315 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
316 VALUES ( %d, '%s', '%s', '%s') ",
317 intval($contact['id']),
318 dbesc(datetime_convert()),
319 dbesc(datetime_convert()),
325 if($followup && $contact['notify']) {
326 logger('notifier: slapdelivery: ' . $contact['name']);
327 $deliver_status = slapper($owner,$contact['notify'],$slap);
329 if($deliver_status == (-1)) {
330 // queue message for redelivery
331 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
332 VALUES ( %d, '%s', '%s', '%s') ",
333 intval($contact['id']),
334 dbesc(datetime_convert()),
335 dbesc(datetime_convert()),
345 // only send salmon if public - e.g. if it's ok to notify
346 // a public hub, it's ok to send a salmon
348 if((count($slaps)) && ($notify_hub) && (! $expire)) {
349 logger('notifier: slapdelivery: ' . $contact['name']);
350 foreach($slaps as $slappy) {
351 if($contact['notify']) {
352 $deliver_status = slapper($owner,$contact['notify'],$slappy);
353 if($deliver_status == (-1)) {
354 // queue message for redelivery
355 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
356 VALUES ( %d, '%s', '%s', '%s') ",
357 intval($contact['id']),
358 dbesc(datetime_convert()),
359 dbesc(datetime_convert()),
370 // WARNING: does not currently convert to RFC2047 header encodings, etc.
372 $addr = $contact['addr'];
376 if($cmd === 'wall-new' || $cmd === 'comment-new') {
379 if($cmd === 'wall-new')
382 $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
394 $local_user = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
397 if(! count($local_user))
401 $r1 = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
404 if($r1 && $r1[0]['reply_to'])
405 $reply_to = $r1[0]['reply_to'];
407 $subject = (($it['title']) ? $it['title'] : t("\x28no subject\x29")) ;
408 $headers = 'From: ' . $local_user[0]['username'] . ' <' . $local_user[0]['email'] . '>' . "\n";
411 $headers .= 'Reply-to: ' . $reply_to . "\n";
413 $headers .= 'Message-id: <' . $it['uri'] . '>' . "\n";
415 if($it['uri'] !== $it['parent-uri']) {
416 $header .= 'References: <' . $it['parent-uri'] . '>' . "\n";
417 if(! strlen($it['title'])) {
418 $r = q("SELECT `title` FROM `item` WHERE `parent-uri` = '%s' LIMIT 1",
419 dbesc($it['parent-uri'])
422 $subtitle = $r[0]['title'];
424 if(strncasecmp($subtitle,'RE:',3))
425 $subject = $subtitle;
427 $subject = 'Re: ' . $subtitle;
433 $headers .= 'MIME-Version: 1.0' . "\n";
434 $headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
435 $headers .= 'Content-Transfer-Encoding: 8bit' . "\n\n";
436 $html = prepare_body($it);
437 $message = '<html><body>' . $html . '</body></html>';
438 logger('notifier: email delivery to ' . $addr);
439 mail($addr, $subject, $message, $headers);
451 // send additional slaps to mentioned remote tags (@foo@example.com)
453 if($slap && count($url_recipients) && $followup && $notify_hub && (! $expire)) {
454 foreach($url_recipients as $url) {
456 logger('notifier: urldelivery: ' . $url);
457 $deliver_status = slapper($owner,$url,$slap);
458 // TODO: redeliver/queue these items on failure, though there is no contact record
463 if((strlen($hub)) && ($notify_hub)) {
464 $hubs = explode(',', $hub);
466 foreach($hubs as $h) {
470 $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
471 post_url($h,$params);
472 logger('pubsub: publish: ' . $h . ' ' . $params . ' returned ' . $a->get_curl_code());
474 sleep(7); // try and avoid multiple hubs responding at precisely the same time
483 * If you have less than 150 dfrn friends and it's a public message,
484 * we'll just go ahead and push them out securely with dfrn/rino.
485 * If you've got more than that, you'll have to rely on PuSH delivery.
489 $max_allowed = ((get_config('system','maxpubdeliver') === false) ? 150 : intval(get_config('system','maxpubdeliver')));
493 * Only get the bare essentials and go back for the full record.
494 * If you've got a lot of friends and we grab all the details at once it could exhaust memory.
498 $r = q("SELECT `id`, `name` FROM `contact`
499 WHERE `network` = 'dfrn' AND `uid` = %d AND `blocked` = 0 AND `pending` = 0
501 intval($owner['uid']),
505 if((count($r)) && (($max_allowed == 0) || (count($r) < $max_allowed))) {
509 /* Don't deliver to folks who have already been delivered to */
511 if(! in_array($rr['id'], $conversants)) {
512 $n = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
518 logger('notifier: dfrnpubdelivery: ' . $n[0]['name']);
519 $deliver_status = dfrn_deliver($owner,$n[0],$atom);
523 logger('notifier: dfrnpubdelivery: ignoring ' . $rr['name']);
531 if (array_search(__file__,get_included_files())===0){
533 notifier_run($argv,$argc);