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",
69 $parent_id = $r[0]['parent'];
71 $updated = $r[0]['edited'];
73 $items = q("SELECT * FROM `item` WHERE `parent` = %d ORDER BY `id` ASC",
82 $r = q("SELECT `contact`.*, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`, `user`.`page-flags`
83 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
84 WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
93 $hub = get_config('system','huburl');
95 // If this is a public conversation, notify the feed hub
98 // fill this in with a single salmon slap if applicable
103 require_once('include/group.php');
107 if($parent['type'] === 'remote') {
108 // local followup to remote post
110 $notify_hub = false; // not public
111 $conversant_str = dbesc($parent['contact-id']);
116 if((strlen($parent['allow_cid']))
117 || (strlen($parent['allow_gid']))
118 || (strlen($parent['deny_cid']))
119 || (strlen($parent['deny_gid']))) {
120 $notify_hub = false; // private recipients, not public
123 $allow_people = expand_acl($parent['allow_cid']);
124 $allow_groups = expand_groups(expand_acl($parent['allow_gid']));
125 $deny_people = expand_acl($parent['deny_cid']);
126 $deny_groups = expand_groups(expand_acl($parent['deny_gid']));
128 $conversants = array();
130 foreach($items as $item) {
131 $recipients[] = $item['contact-id'];
132 $conversants[] = $item['contact-id'];
133 // pull out additional tagged people to notify (if public message)
134 if($notify_hub && strlen($item['inform'])) {
135 $people = explode(',',$item['inform']);
136 foreach($people as $person) {
137 if(substr($person,0,4) === 'cid:') {
138 $recipients[] = intval(substr($person,4));
139 $conversants[] = intval(substr($person,4));
142 $url_recipients[] = substr($person,4);
148 logger('notifier: url_recipients' . print_r($url_recipients,true));
150 $conversants = array_unique($conversants);
153 $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups));
154 $deny = array_unique(array_merge($deny_people,$deny_groups));
155 $recipients = array_diff($recipients,$deny);
157 $conversant_str = dbesc(implode(', ',$conversants));
160 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0");
170 $feed_template = load_view_file('view/atom_feed.tpl');
171 $mail_template = load_view_file('view/atom_mail.tpl');
178 $hubs = explode(',', $hub);
180 foreach($hubs as $h) {
184 $hubxml .= '<link rel="hub" href="' . xmlify($h) . '" />' . "\n" ;
189 $atom .= replace_macros($feed_template, array(
190 '$version' => xmlify(FRIENDIKA_VERSION),
191 '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
192 '$feed_title' => xmlify($owner['name']),
193 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
195 '$salmon' => '', // private feed, we don't use salmon here
196 '$name' => xmlify($owner['name']),
197 '$profile_page' => xmlify($owner['url']),
198 '$photo' => xmlify($owner['photo']),
199 '$thumb' => xmlify($owner['thumb']),
200 '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
201 '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
202 '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME)) ,
206 if($cmd === 'mail') {
207 $notify_hub = false; // mail is not public
209 $atom .= replace_macros($mail_template, array(
210 '$name' => xmlify($owner['name']),
211 '$profile_page' => xmlify($owner['url']),
212 '$thumb' => xmlify($owner['thumb']),
213 '$item_id' => xmlify($item['uri']),
214 '$subject' => xmlify($item['title']),
215 '$created' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
216 '$content' => xmlify($item['body']),
217 '$parent_id' => xmlify($item['parent-uri'])
222 foreach($items as $item) { // there is only one item
223 if($item['id'] == $item_id) {
224 logger('notifier: followup: item: ' . print_r($item,true), LOGGER_DATA);
225 $slap = atom_entry($item,'html',$owner,$owner,false);
226 $atom .= atom_entry($item,'text',$owner,$owner,false);
231 foreach($items as $item) {
232 $contact = get_item_contact($item,$contacts);
236 $atom .= atom_entry($item,'text',$contact,$owner,true);
238 // There's a problem here - we *were* going to use salmon to provide semi-authenticated
239 // communication to OStatus, but unless we're the item author they won't verify.
240 // commented out for now, though we'll still send local replies (and any mentions
241 // that they contain) upstream. Rethinking the problem space.
243 // $slaps[] = atom_entry($item,'html',$contact,$owner,true);
247 $atom .= '</feed>' . "\r\n";
249 logger('notifier: ' . $atom, LOGGER_DATA);
251 // logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
254 $recip_str = $parent['contact-id'];
256 $recip_str = implode(', ', $recipients);
259 $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 AND `pending` = 0 ",
265 require_once('include/salmon.php');
268 foreach($r as $contact) {
274 switch($contact['network']) {
276 logger('notifier: dfrndelivery: ' . $contact['name']);
277 $deliver_status = dfrn_deliver($owner,$contact,$atom);
279 logger('notifier: dfrn_delivery returns ' . $deliver_status);
281 if($deliver_status == (-1)) {
282 logger('notifier: delivery failed: queuing message');
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()),
294 if($followup && $contact['notify']) {
295 logger('notifier: slapdelivery: ' . $contact['name']);
296 $deliver_status = slapper($owner,$contact['notify'],$slap);
298 if($deliver_status == (-1)) {
299 // queue message for redelivery
300 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
301 VALUES ( %d, '%s', '%s', '%s') ",
302 intval($contact['id']),
303 dbesc(datetime_convert()),
304 dbesc(datetime_convert()),
314 // only send salmon if public - e.g. if it's ok to notify
315 // a public hub, it's ok to send a salmon
317 if(count($slaps) && $notify_hub) {
318 logger('notifier: slapdelivery: ' . $contact['name']);
319 foreach($slaps as $slappy) {
320 if($contact['notify']) {
321 $deliver_status = slapper($owner,$contact['notify'],$slappy);
322 if($deliver_status == (-1)) {
323 // queue message for redelivery
324 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
325 VALUES ( %d, '%s', '%s', '%s') ",
326 intval($contact['id']),
327 dbesc(datetime_convert()),
328 dbesc(datetime_convert()),
346 // send additional slaps to mentioned remote tags (@foo@example.com)
348 if($slap && count($url_recipients) && $followup && $notify_hub) {
349 foreach($url_recipients as $url) {
351 logger('notifier: urldelivery: ' . $url);
352 $deliver_status = slapper($owner,$url,$slap);
353 // TODO: redeliver/queue these items on failure, though there is no contact record
358 if((strlen($hub)) && ($notify_hub)) {
359 $hubs = explode(',', $hub);
361 foreach($hubs as $h) {
365 $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
366 post_url($h,$params);
367 logger('pubsub: publish: ' . $h . ' ' . $params . ' returned ' . $a->get_curl_code());
369 sleep(7); // try and avoid multiple hubs responding at precisely the same time
378 * If you have less than 150 dfrn friends and it's a public message,
379 * we'll just go ahead and push them out securely with dfrn/rino.
380 * If you've got more than that, you'll have to rely on PuSH delivery.
384 $max_allowed = ((get_config('system','maxpubdeliver') === false) ? 150 : intval(get_config('system','maxdeliver')));
388 * Only get the bare essentials and go back for the full record.
389 * If you've got a lot of friends and we grab all the details at once it could exhaust memory.
393 $r = q("SELECT `id`, `name` FROM `contact`
394 WHERE `network` = 'dfrn' AND `uid` = %d AND `blocked` = 0 AND `pending` = 0
396 intval($owner['uid']),
400 if((count($r)) && (($max_allowed == 0) || (count($r) < $max_allowed))) {
404 /* Don't deliver to folks who have already been delivered to */
406 if(! in_array($rr['id'], $conversants)) {
407 $n = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
413 logger('notifier: dfrnpubdelivery: ' . $n[0]['name']);
414 $deliver_status = dfrn_deliver($owner,$n[0],$atom);
418 logger('notifier: dfrnpubdelivery: ignoring ' . $rr['name']);
426 if (array_search(__file__,get_included_files())===0){
428 notifier_run($argv,$argc);