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]);
43 $recipients = array();
44 $url_recipients = array();
48 $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
51 if(! count($message)){
54 $uid = $message[0]['uid'];
55 $recipients[] = $message[0]['contact-id'];
62 $r = q("SELECT `parent`, `uid`, `edited` FROM `item` WHERE `id` = %d LIMIT 1",
66 if((! count($r)) || (! intval($r[0]['parent']))) {
70 $parent_id = intval($r[0]['parent']);
72 $updated = $r[0]['edited'];
74 $items = q("SELECT * FROM `item` WHERE `parent` = %d ORDER BY `id` ASC",
82 // avoid race condition with deleting entries
84 if($items[0]['deleted']) {
85 foreach($items as $item)
90 $r = q("SELECT `contact`.*, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`, `user`.`page-flags`
91 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
92 WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
101 $hub = get_config('system','huburl');
103 // If this is a public conversation, notify the feed hub
106 // fill this in with a single salmon slap if applicable
111 require_once('include/group.php');
115 if($parent['type'] === 'remote') {
116 // local followup to remote post
118 $notify_hub = false; // not public
119 $conversant_str = dbesc($parent['contact-id']);
124 if((strlen($parent['allow_cid']))
125 || (strlen($parent['allow_gid']))
126 || (strlen($parent['deny_cid']))
127 || (strlen($parent['deny_gid']))) {
128 $notify_hub = false; // private recipients, not public
131 $allow_people = expand_acl($parent['allow_cid']);
132 $allow_groups = expand_groups(expand_acl($parent['allow_gid']));
133 $deny_people = expand_acl($parent['deny_cid']);
134 $deny_groups = expand_groups(expand_acl($parent['deny_gid']));
136 $conversants = array();
138 foreach($items as $item) {
139 $recipients[] = $item['contact-id'];
140 $conversants[] = $item['contact-id'];
141 // pull out additional tagged people to notify (if public message)
142 if($notify_hub && strlen($item['inform'])) {
143 $people = explode(',',$item['inform']);
144 foreach($people as $person) {
145 if(substr($person,0,4) === 'cid:') {
146 $recipients[] = intval(substr($person,4));
147 $conversants[] = intval(substr($person,4));
150 $url_recipients[] = substr($person,4);
156 logger('notifier: url_recipients' . print_r($url_recipients,true));
158 $conversants = array_unique($conversants);
161 $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups));
162 $deny = array_unique(array_merge($deny_people,$deny_groups));
163 $recipients = array_diff($recipients,$deny);
165 $conversant_str = dbesc(implode(', ',$conversants));
168 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0");
178 $feed_template = load_view_file('view/atom_feed.tpl');
179 $mail_template = load_view_file('view/atom_mail.tpl');
186 $hubs = explode(',', $hub);
188 foreach($hubs as $h) {
192 $hubxml .= '<link rel="hub" href="' . xmlify($h) . '" />' . "\n" ;
197 $atom .= replace_macros($feed_template, array(
198 '$version' => xmlify(FRIENDIKA_VERSION),
199 '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
200 '$feed_title' => xmlify($owner['name']),
201 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
203 '$salmon' => '', // private feed, we don't use salmon here
204 '$name' => xmlify($owner['name']),
205 '$profile_page' => xmlify($owner['url']),
206 '$photo' => xmlify($owner['photo']),
207 '$thumb' => xmlify($owner['thumb']),
208 '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
209 '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
210 '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME)) ,
214 if($cmd === 'mail') {
215 $notify_hub = false; // mail is not public
217 $atom .= replace_macros($mail_template, array(
218 '$name' => xmlify($owner['name']),
219 '$profile_page' => xmlify($owner['url']),
220 '$thumb' => xmlify($owner['thumb']),
221 '$item_id' => xmlify($item['uri']),
222 '$subject' => xmlify($item['title']),
223 '$created' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
224 '$content' => xmlify($item['body']),
225 '$parent_id' => xmlify($item['parent-uri'])
230 foreach($items as $item) { // there is only one item
231 if(! $item['parent'])
233 if($item['id'] == $item_id) {
234 logger('notifier: followup: item: ' . print_r($item,true), LOGGER_DATA);
235 $slap = atom_entry($item,'html',$owner,$owner,false);
236 $atom .= atom_entry($item,'text',$owner,$owner,false);
241 foreach($items as $item) {
242 if(! $item['parent'])
245 $contact = get_item_contact($item,$contacts);
249 $atom .= atom_entry($item,'text',$contact,$owner,true);
251 // There's a problem here - we *were* going to use salmon to provide semi-authenticated
252 // communication to OStatus, but unless we're the item author they won't verify.
253 // commented out for now, though we'll still send local replies (and any mentions
254 // that they contain) upstream. Rethinking the problem space.
256 // $slaps[] = atom_entry($item,'html',$contact,$owner,true);
260 $atom .= '</feed>' . "\r\n";
262 logger('notifier: ' . $atom, LOGGER_DATA);
264 // logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
267 $recip_str = $parent['contact-id'];
269 $recip_str = implode(', ', $recipients);
272 $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 AND `pending` = 0 ",
278 require_once('include/salmon.php');
281 foreach($r as $contact) {
287 switch($contact['network']) {
289 logger('notifier: dfrndelivery: ' . $contact['name']);
290 $deliver_status = dfrn_deliver($owner,$contact,$atom);
292 logger('notifier: dfrn_delivery returns ' . $deliver_status);
294 if($deliver_status == (-1)) {
295 logger('notifier: delivery failed: queuing message');
296 // queue message for redelivery
297 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
298 VALUES ( %d, '%s', '%s', '%s') ",
299 intval($contact['id']),
300 dbesc(datetime_convert()),
301 dbesc(datetime_convert()),
307 if($followup && $contact['notify']) {
308 logger('notifier: slapdelivery: ' . $contact['name']);
309 $deliver_status = slapper($owner,$contact['notify'],$slap);
311 if($deliver_status == (-1)) {
312 // queue message for redelivery
313 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
314 VALUES ( %d, '%s', '%s', '%s') ",
315 intval($contact['id']),
316 dbesc(datetime_convert()),
317 dbesc(datetime_convert()),
327 // only send salmon if public - e.g. if it's ok to notify
328 // a public hub, it's ok to send a salmon
330 if(count($slaps) && $notify_hub) {
331 logger('notifier: slapdelivery: ' . $contact['name']);
332 foreach($slaps as $slappy) {
333 if($contact['notify']) {
334 $deliver_status = slapper($owner,$contact['notify'],$slappy);
335 if($deliver_status == (-1)) {
336 // queue message for redelivery
337 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
338 VALUES ( %d, '%s', '%s', '%s') ",
339 intval($contact['id']),
340 dbesc(datetime_convert()),
341 dbesc(datetime_convert()),
359 // send additional slaps to mentioned remote tags (@foo@example.com)
361 if($slap && count($url_recipients) && $followup && $notify_hub) {
362 foreach($url_recipients as $url) {
364 logger('notifier: urldelivery: ' . $url);
365 $deliver_status = slapper($owner,$url,$slap);
366 // TODO: redeliver/queue these items on failure, though there is no contact record
371 if((strlen($hub)) && ($notify_hub)) {
372 $hubs = explode(',', $hub);
374 foreach($hubs as $h) {
378 $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
379 post_url($h,$params);
380 logger('pubsub: publish: ' . $h . ' ' . $params . ' returned ' . $a->get_curl_code());
382 sleep(7); // try and avoid multiple hubs responding at precisely the same time
391 * If you have less than 150 dfrn friends and it's a public message,
392 * we'll just go ahead and push them out securely with dfrn/rino.
393 * If you've got more than that, you'll have to rely on PuSH delivery.
397 $max_allowed = ((get_config('system','maxpubdeliver') === false) ? 150 : intval(get_config('system','maxdeliver')));
401 * Only get the bare essentials and go back for the full record.
402 * If you've got a lot of friends and we grab all the details at once it could exhaust memory.
406 $r = q("SELECT `id`, `name` FROM `contact`
407 WHERE `network` = 'dfrn' AND `uid` = %d AND `blocked` = 0 AND `pending` = 0
409 intval($owner['uid']),
413 if((count($r)) && (($max_allowed == 0) || (count($r) < $max_allowed))) {
417 /* Don't deliver to folks who have already been delivered to */
419 if(! in_array($rr['id'], $conversants)) {
420 $n = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
426 logger('notifier: dfrnpubdelivery: ' . $n[0]['name']);
427 $deliver_status = dfrn_deliver($owner,$n[0],$atom);
431 logger('notifier: dfrnpubdelivery: ignoring ' . $rr['name']);
439 if (array_search(__file__,get_included_files())===0){
441 notifier_run($argv,$argc);