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'));
23 logger('notifier: invoked: ' . print_r($argv,true));
31 $item_id = intval($argv[2]);
37 $recipients = array();
38 $url_recipients = array();
42 $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
47 $uid = $message[0]['uid'];
48 $recipients[] = $message[0]['contact-id'];
55 $r = q("SELECT `parent`, `uid`, `edited` FROM `item` WHERE `id` = %d LIMIT 1",
61 $parent_id = $r[0]['parent'];
63 $updated = $r[0]['edited'];
65 $items = q("SELECT * FROM `item` WHERE `parent` = %d ORDER BY `id` ASC",
73 $r = q("SELECT `contact`.*, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`, `user`.`page-flags`
74 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
75 WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
84 $hub = get_config('system','huburl');
86 // If this is a public conversation, notify the feed hub
89 // fill this in with a single salmon slap if applicable
94 require_once('include/group.php');
98 if($parent['type'] === 'remote') {
99 // local followup to remote post
101 $notify_hub = false; // not public
102 $conversant_str = dbesc($parent['contact-id']);
107 if((strlen($parent['allow_cid']))
108 || (strlen($parent['allow_gid']))
109 || (strlen($parent['deny_cid']))
110 || (strlen($parent['deny_gid']))) {
111 $notify_hub = false; // private recipients, not public
114 $allow_people = expand_acl($parent['allow_cid']);
115 $allow_groups = expand_groups(expand_acl($parent['allow_gid']));
116 $deny_people = expand_acl($parent['deny_cid']);
117 $deny_groups = expand_groups(expand_acl($parent['deny_gid']));
119 $conversants = array();
121 foreach($items as $item) {
122 $recipients[] = $item['contact-id'];
123 $conversants[] = $item['contact-id'];
124 // pull out additional tagged people to notify (if public message)
125 if($notify_hub && strlen($item['inform'])) {
126 $people = explode(',',$item['inform']);
127 foreach($people as $person) {
128 if(substr($person,0,4) === 'cid:') {
129 $recipients[] = intval(substr($person,4));
130 $conversants[] = intval(substr($person,4));
133 $url_recipients[] = substr($person,4);
139 logger('notifier: url_recipients' . print_r($url_recipients,true));
141 $conversants = array_unique($conversants);
144 $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups));
145 $deny = array_unique(array_merge($deny_people,$deny_groups));
146 $recipients = array_diff($recipients,$deny);
148 $conversant_str = dbesc(implode(', ',$conversants));
151 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0");
159 $feed_template = load_view_file('view/atom_feed.tpl');
160 $mail_template = load_view_file('view/atom_mail.tpl');
167 $hubs = explode(',', $hub);
169 foreach($hubs as $h) {
173 $hubxml .= '<link rel="hub" href="' . xmlify($h) . '" />' . "\n" ;
178 $atom .= replace_macros($feed_template, array(
179 '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
180 '$feed_title' => xmlify($owner['name']),
181 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
183 '$salmon' => '', // private feed, we don't use salmon here
184 '$name' => xmlify($owner['name']),
185 '$profile_page' => xmlify($owner['url']),
186 '$photo' => xmlify($owner['photo']),
187 '$thumb' => xmlify($owner['thumb']),
188 '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
189 '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
190 '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME)) ,
194 if($cmd === 'mail') {
195 $notify_hub = false; // mail is not public
197 $atom .= replace_macros($mail_template, array(
198 '$name' => xmlify($owner['name']),
199 '$profile_page' => xmlify($owner['url']),
200 '$thumb' => xmlify($owner['thumb']),
201 '$item_id' => xmlify($item['uri']),
202 '$subject' => xmlify($item['title']),
203 '$created' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
204 '$content' => xmlify($item['body']),
205 '$parent_id' => xmlify($item['parent-uri'])
211 foreach($items as $item) { // there is only one item
212 if($item['id'] == $item_id) {
213 logger('notifier: followup: item: ' . print_r($item,true), LOGGER_DATA);
214 $slap = atom_entry($item,'html',$owner,$owner,false);
215 $atom .= atom_entry($item,'text',$owner,$owner,false);
220 foreach($items as $item) {
221 $contact = get_item_contact($item,$contacts);
225 $atom .= atom_entry($item,'text',$contact,$owner,true);
226 $slaps[] = atom_entry($item,'html',$contact,$owner,true);
230 $atom .= '</feed>' . "\r\n";
232 logger('notifier: ' . $atom, LOGGER_DATA);
234 logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
237 $recip_str = $parent['contact-id'];
239 $recip_str = implode(', ', $recipients);
242 $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 ",
250 require_once('include/salmon.php');
252 foreach($r as $contact) {
258 switch($contact['network']) {
260 logger('notifier: dfrndelivery: ' . $contact['name']);
261 $deliver_status = dfrn_deliver($owner,$contact,$atom);
263 logger('notifier: dfrn_delivery returns ' . $deliver_status);
265 if($deliver_status == (-1)) {
266 logger('notifier: delivery failed: queuing message');
267 // queue message for redelivery
268 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
269 VALUES ( %d, '%s', '%s', '%s') ",
270 intval($contact['id']),
271 dbesc(datetime_convert()),
272 dbesc(datetime_convert()),
278 if($followup && $contact['notify']) {
279 logger('notifier: slapdelivery: ' . $contact['name']);
280 $deliver_status = slapper($owner,$contact['notify'],$slap);
282 if($deliver_status == (-1)) {
283 // queue message for redelivery
284 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
285 VALUES ( %d, '%s', '%s', '%s') ",
286 intval($contact['id']),
287 dbesc(datetime_convert()),
288 dbesc(datetime_convert()),
298 // only send salmon if public - e.g. if it's ok to notify
299 // a public hub, it's ok to send a salmon
301 if(count($slaps) && $notify_hub) {
302 logger('notifier: slapdelivery: ' . $contact['name']);
303 foreach($slaps as $slappy) {
304 if($contact['notify']) {
305 $deliver_status = slapper($owner,$contact['notify'],$slappy);
306 if($deliver_status == (-1)) {
307 // queue message for redelivery
308 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
309 VALUES ( %d, '%s', '%s', '%s') ",
310 intval($contact['id']),
311 dbesc(datetime_convert()),
312 dbesc(datetime_convert()),
324 // send additional slaps to mentioned remote tags (@foo@example.com)
326 if(count($slaps) && count($url_recipients) && $notify_hub) {
327 foreach($url_recipients as $url) {
328 logger('notifier: urldelivery: ' . $url);
329 foreach($slaps as $slappy) {
331 $deliver_status = slapper($owner,$url,$slappy);
332 // TODO: redeliver/queue these items on failure, though there is no contact record
338 if((strlen($hub)) && ($notify_hub)) {
339 $hubs = explode(',', $hub);
341 foreach($hubs as $h) {
345 $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
346 post_url($h,$params);
347 logger('pubsub: publish: ' . $h . ' returned ' . $a->get_curl_code());
349 sleep(7); // try and avoid multiple hubs responding at precisely the same time