3 require_once("boot.php");
7 @include(".htconfig.php");
8 require_once("dba.php");
9 $db = new dba($db_host, $db_user, $db_pass, $db_data);
10 unset($db_host, $db_user, $db_pass, $db_data);
13 require_once("session.php");
14 require_once("datetime.php");
15 require_once('include/items.php');
16 require_once('include/bbcode.php');
21 $a->set_baseurl(get_config('system','url'));
29 $item_id = intval($argv[2]);
35 $recipients = array();
36 $url_recipients = array();
40 $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
45 $uid = $message[0]['uid'];
46 $recipients[] = $message[0]['contact-id'];
53 $r = q("SELECT `parent`, `uid`, `edited` FROM `item` WHERE `id` = %d LIMIT 1",
59 $parent_id = $r[0]['parent'];
61 $updated = $r[0]['edited'];
63 $items = q("SELECT * FROM `item` WHERE `parent` = %d ORDER BY `id` ASC",
71 $r = q("SELECT `contact`.*, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`, `user`.`page-flags`
72 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
73 WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
82 $hub = get_config('system','huburl');
84 // If this is a public conversation, notify the feed hub
87 // fill this in with a single salmon slap if applicable
92 require_once('include/group.php');
96 if($parent['type'] === 'remote') {
97 // local followup to remote post
99 $notify_hub = false; // not public
100 $conversant_str = dbesc($parent['contact-id']);
105 if((strlen($parent['allow_cid']))
106 || (strlen($parent['allow_gid']))
107 || (strlen($parent['deny_cid']))
108 || (strlen($parent['deny_gid']))) {
109 $notify_hub = false; // private recipients, not public
112 $allow_people = expand_acl($parent['allow_cid']);
113 $allow_groups = expand_groups(expand_acl($parent['allow_gid']));
114 $deny_people = expand_acl($parent['deny_cid']);
115 $deny_groups = expand_groups(expand_acl($parent['deny_gid']));
117 $conversants = array();
119 foreach($items as $item) {
120 $recipients[] = $item['contact-id'];
121 $conversants[] = $item['contact-id'];
122 // pull out additional tagged people to notify (if public message)
123 if($notify_hub && strlen($item['inform'])) {
124 $people = explode(',',$item['inform']);
125 foreach($people as $person) {
126 if(substr($person,0,4) === 'cid:') {
127 $recipients[] = intval(substr($person,4));
128 $conversants[] = intval(substr($person,4));
131 $url_recipients[] = substr($person,4);
137 logger('notifier: url_recipients' . print_r($url_recipients,true));
139 $conversants = array_unique($conversants,SORT_NUMERIC);
142 $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups),SORT_NUMERIC);
143 $deny = array_unique(array_merge($deny_people,$deny_groups),SORT_NUMERIC);
144 $recipients = array_diff($recipients,$deny);
146 $conversant_str = dbesc(implode(', ',$conversants));
149 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0");
157 $feed_template = load_view_file('view/atom_feed.tpl');
158 $mail_template = load_view_file('view/atom_mail.tpl');
165 $hubs = explode(',', $hub);
167 foreach($hubs as $h) {
171 $hubxml .= '<link rel="hub" href="' . xmlify($h) . '" />' . "\n" ;
176 $atom .= replace_macros($feed_template, array(
177 '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
178 '$feed_title' => xmlify($owner['name']),
179 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
181 '$salmon' => '', // private feed, we don't use salmon here
182 '$name' => xmlify($owner['name']),
183 '$profile_page' => xmlify($owner['url']),
184 '$photo' => xmlify($owner['photo']),
185 '$thumb' => xmlify($owner['thumb']),
186 '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
187 '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
188 '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME))
191 if($cmd === 'mail') {
192 $notify_hub = false; // mail is not public
194 $atom .= replace_macros($mail_template, array(
195 '$name' => xmlify($owner['name']),
196 '$profile_page' => xmlify($owner['url']),
197 '$thumb' => xmlify($owner['thumb']),
198 '$item_id' => xmlify($item['uri']),
199 '$subject' => xmlify($item['title']),
200 '$created' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
201 '$content' => xmlify($item['body']),
202 '$parent_id' => xmlify($item['parent-uri'])
208 foreach($items as $item) { // there is only one item
209 if($item['id'] == $item_id) {
210 $slap = atom_entry($item,'html',$owner,$owner,false);
211 $atom .= atom_entry($item,'text',$owner,$owner,false);
216 foreach($items as $item) {
217 $contact = get_item_contact($item,$contacts);
221 $atom .= atom_entry($item,'text',$contact,$owner,true);
222 $slaps[] = atom_entry($item,'html',$contact,$owner,true);
226 $atom .= '</feed>' . "\r\n";
228 logger('notifier: ' . $atom, LOGGER_DATA);
230 logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
233 $recip_str = $parent['contact-id'];
235 $recip_str = implode(', ', $recipients);
238 $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 ",
246 require_once('include/salmon.php');
248 foreach($r as $contact) {
254 switch($contact['network']) {
256 logger('notifier: dfrndelivery: ' . $contact['name']);
257 $deliver_status = dfrn_deliver($owner,$contact,$atom);
259 if($deliver_status == (-1)) {
260 // queue message for redelivery
261 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
262 VALUES ( %d, '%s', '%s', '%s') ",
263 intval($contact['id']),
264 dbesc(datetime_convert()),
265 dbesc(datetime_convert()),
271 if($followup && $contact['notify']) {
272 logger('notifier: slapdelivery: ' . $contact['name']);
273 $deliver_status = slapper($owner,$contact['notify'],$slap);
275 if($deliver_status == (-1)) {
276 // queue message for redelivery
277 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
278 VALUES ( %d, '%s', '%s', '%s') ",
279 intval($contact['id']),
280 dbesc(datetime_convert()),
281 dbesc(datetime_convert()),
291 // only send salmon if public - e.g. if it's ok to notify
292 // a public hub, it's ok to send a salmon
294 if(count($slaps) && $notify_hub) {
295 logger('notifier: slapdelivery: ' . $contact['name']);
296 foreach($slaps as $slappy) {
297 if($contact['notify']) {
298 $deliver_status = slapper($owner,$contact['notify'],$slappy);
299 if($deliver_status == (-1)) {
300 // queue message for redelivery
301 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
302 VALUES ( %d, '%s', '%s', '%s') ",
303 intval($contact['id']),
304 dbesc(datetime_convert()),
305 dbesc(datetime_convert()),
317 // send additional slaps to mentioned remote tags (@foo@example.com)
319 if(count($slaps) && count($url_recipients) && $notify_hub) {
320 foreach($url_recipients as $url) {
321 logger('notifier: urldelivery: ' . $url);
322 foreach($slaps as $slappy) {
324 $deliver_status = slapper($owner,$url,$slappy);
325 // TODO: redeliver/queue these items on failure, though there is no contact record
331 if((strlen($hub)) && ($notify_hub)) {
332 $hubs = explode(',', $hub);
334 foreach($hubs as $h) {
338 $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
339 post_url($h,$params);
340 logger('pubsub: publish: ' . $h . ' returned ' . $a->get_curl_code());
342 sleep(7); // try and avoid multiple hubs responding at precisely the same time