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]);
38 $recipients = array();
39 $url_recipients = array();
43 $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
46 if(! count($message)){
49 $uid = $message[0]['uid'];
50 $recipients[] = $message[0]['contact-id'];
57 $r = q("SELECT `parent`, `uid`, `edited` FROM `item` WHERE `id` = %d LIMIT 1",
64 $parent_id = $r[0]['parent'];
66 $updated = $r[0]['edited'];
68 $items = q("SELECT * FROM `item` WHERE `parent` = %d ORDER BY `id` ASC",
77 $r = q("SELECT `contact`.*, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`, `user`.`page-flags`
78 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
79 WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
88 $hub = get_config('system','huburl');
90 // If this is a public conversation, notify the feed hub
93 // fill this in with a single salmon slap if applicable
98 require_once('include/group.php');
102 if($parent['type'] === 'remote') {
103 // local followup to remote post
105 $notify_hub = false; // not public
106 $conversant_str = dbesc($parent['contact-id']);
111 if((strlen($parent['allow_cid']))
112 || (strlen($parent['allow_gid']))
113 || (strlen($parent['deny_cid']))
114 || (strlen($parent['deny_gid']))) {
115 $notify_hub = false; // private recipients, not public
118 $allow_people = expand_acl($parent['allow_cid']);
119 $allow_groups = expand_groups(expand_acl($parent['allow_gid']));
120 $deny_people = expand_acl($parent['deny_cid']);
121 $deny_groups = expand_groups(expand_acl($parent['deny_gid']));
123 $conversants = array();
125 foreach($items as $item) {
126 $recipients[] = $item['contact-id'];
127 $conversants[] = $item['contact-id'];
128 // pull out additional tagged people to notify (if public message)
129 if($notify_hub && strlen($item['inform'])) {
130 $people = explode(',',$item['inform']);
131 foreach($people as $person) {
132 if(substr($person,0,4) === 'cid:') {
133 $recipients[] = intval(substr($person,4));
134 $conversants[] = intval(substr($person,4));
137 $url_recipients[] = substr($person,4);
143 logger('notifier: url_recipients' . print_r($url_recipients,true));
145 $conversants = array_unique($conversants);
148 $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups));
149 $deny = array_unique(array_merge($deny_people,$deny_groups));
150 $recipients = array_diff($recipients,$deny);
152 $conversant_str = dbesc(implode(', ',$conversants));
155 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0");
164 $feed_template = load_view_file('view/atom_feed.tpl');
165 $mail_template = load_view_file('view/atom_mail.tpl');
172 $hubs = explode(',', $hub);
174 foreach($hubs as $h) {
178 $hubxml .= '<link rel="hub" href="' . xmlify($h) . '" />' . "\n" ;
183 $atom .= replace_macros($feed_template, array(
184 '$version' => xmlify(FRIENDIKA_VERSION),
185 '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
186 '$feed_title' => xmlify($owner['name']),
187 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
189 '$salmon' => '', // private feed, we don't use salmon here
190 '$name' => xmlify($owner['name']),
191 '$profile_page' => xmlify($owner['url']),
192 '$photo' => xmlify($owner['photo']),
193 '$thumb' => xmlify($owner['thumb']),
194 '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
195 '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
196 '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME)) ,
200 if($cmd === 'mail') {
201 $notify_hub = false; // mail is not public
203 $atom .= replace_macros($mail_template, array(
204 '$name' => xmlify($owner['name']),
205 '$profile_page' => xmlify($owner['url']),
206 '$thumb' => xmlify($owner['thumb']),
207 '$item_id' => xmlify($item['uri']),
208 '$subject' => xmlify($item['title']),
209 '$created' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
210 '$content' => xmlify($item['body']),
211 '$parent_id' => xmlify($item['parent-uri'])
216 foreach($items as $item) { // there is only one item
217 if($item['id'] == $item_id) {
218 logger('notifier: followup: item: ' . print_r($item,true), LOGGER_DATA);
219 $slap = atom_entry($item,'html',$owner,$owner,false);
220 $atom .= atom_entry($item,'text',$owner,$owner,false);
225 foreach($items as $item) {
226 $contact = get_item_contact($item,$contacts);
230 $atom .= atom_entry($item,'text',$contact,$owner,true);
232 // There's a problem here - we *were* going to use salmon to provide semi-authenticated
233 // communication to OStatus, but unless we're the item author they won't verify.
234 // commented out for now, though we'll still send local replies (and any mentions
235 // that they contain) upstream. Rethinking the problem space.
237 // $slaps[] = atom_entry($item,'html',$contact,$owner,true);
241 $atom .= '</feed>' . "\r\n";
243 logger('notifier: ' . $atom, LOGGER_DATA);
245 // logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
248 $recip_str = $parent['contact-id'];
250 $recip_str = implode(', ', $recipients);
253 $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 ",
261 require_once('include/salmon.php');
263 foreach($r as $contact) {
269 switch($contact['network']) {
271 logger('notifier: dfrndelivery: ' . $contact['name']);
272 $deliver_status = dfrn_deliver($owner,$contact,$atom);
274 logger('notifier: dfrn_delivery returns ' . $deliver_status);
276 if($deliver_status == (-1)) {
277 logger('notifier: delivery failed: queuing message');
278 // queue message for redelivery
279 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
280 VALUES ( %d, '%s', '%s', '%s') ",
281 intval($contact['id']),
282 dbesc(datetime_convert()),
283 dbesc(datetime_convert()),
289 if($followup && $contact['notify']) {
290 logger('notifier: slapdelivery: ' . $contact['name']);
291 $deliver_status = slapper($owner,$contact['notify'],$slap);
293 if($deliver_status == (-1)) {
294 // queue message for redelivery
295 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
296 VALUES ( %d, '%s', '%s', '%s') ",
297 intval($contact['id']),
298 dbesc(datetime_convert()),
299 dbesc(datetime_convert()),
309 // only send salmon if public - e.g. if it's ok to notify
310 // a public hub, it's ok to send a salmon
312 if(count($slaps) && $notify_hub) {
313 logger('notifier: slapdelivery: ' . $contact['name']);
314 foreach($slaps as $slappy) {
315 if($contact['notify']) {
316 $deliver_status = slapper($owner,$contact['notify'],$slappy);
317 if($deliver_status == (-1)) {
318 // queue message for redelivery
319 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
320 VALUES ( %d, '%s', '%s', '%s') ",
321 intval($contact['id']),
322 dbesc(datetime_convert()),
323 dbesc(datetime_convert()),
340 // send additional slaps to mentioned remote tags (@foo@example.com)
342 if($slap && count($url_recipients) && $followup && $notify_hub) {
343 foreach($url_recipients as $url) {
345 logger('notifier: urldelivery: ' . $url);
346 $deliver_status = slapper($owner,$url,$slap);
347 // TODO: redeliver/queue these items on failure, though there is no contact record
352 if((strlen($hub)) && ($notify_hub)) {
353 $hubs = explode(',', $hub);
355 foreach($hubs as $h) {
359 $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
360 post_url($h,$params);
361 logger('pubsub: publish: ' . $h . ' returned ' . $a->get_curl_code());
363 sleep(7); // try and avoid multiple hubs responding at precisely the same time