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]);
45 $recipients = array();
46 $url_recipients = array();
50 $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
53 if(! count($message)){
56 $uid = $message[0]['uid'];
57 $recipients[] = $message[0]['contact-id'];
61 elseif($cmd === 'expire') {
63 $items = q("SELECT * FROM `item` WHERE `uid` = %d AND `wall` = 1
64 AND `deleted` = 1 AND `changed` > UTC_TIMESTAMP - INTERVAL 10 MINUTE",
75 $r = q("SELECT `parent`, `uid`, `edited` FROM `item` WHERE `id` = %d LIMIT 1",
79 if((! count($r)) || (! intval($r[0]['parent']))) {
83 $parent_id = intval($r[0]['parent']);
85 $updated = $r[0]['edited'];
87 $items = q("SELECT * FROM `item` WHERE `parent` = %d ORDER BY `id` ASC",
95 // avoid race condition with deleting entries
97 if($items[0]['deleted']) {
98 foreach($items as $item)
102 if(count($items) == 1 && $items[0]['uri'] === $items[0]['parent-uri'])
106 $r = q("SELECT `contact`.*, `user`.`timezone`, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`, `user`.`page-flags`
107 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
108 WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
117 $hub = get_config('system','huburl');
119 // If this is a public conversation, notify the feed hub
122 // fill this in with a single salmon slap if applicable
127 require_once('include/group.php');
131 if($parent['type'] === 'remote' && (! $expire)) {
132 // local followup to remote post
134 $notify_hub = false; // not public
135 $conversant_str = dbesc($parent['contact-id']);
140 if((strlen($parent['allow_cid']))
141 || (strlen($parent['allow_gid']))
142 || (strlen($parent['deny_cid']))
143 || (strlen($parent['deny_gid']))) {
144 $notify_hub = false; // private recipients, not public
147 $allow_people = expand_acl($parent['allow_cid']);
148 $allow_groups = expand_groups(expand_acl($parent['allow_gid']));
149 $deny_people = expand_acl($parent['deny_cid']);
150 $deny_groups = expand_groups(expand_acl($parent['deny_gid']));
152 $conversants = array();
154 foreach($items as $item) {
155 $recipients[] = $item['contact-id'];
156 $conversants[] = $item['contact-id'];
157 // pull out additional tagged people to notify (if public message)
158 if($notify_hub && strlen($item['inform'])) {
159 $people = explode(',',$item['inform']);
160 foreach($people as $person) {
161 if(substr($person,0,4) === 'cid:') {
162 $recipients[] = intval(substr($person,4));
163 $conversants[] = intval(substr($person,4));
166 $url_recipients[] = substr($person,4);
172 logger('notifier: url_recipients' . print_r($url_recipients,true));
174 $conversants = array_unique($conversants);
177 $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups));
178 $deny = array_unique(array_merge($deny_people,$deny_groups));
179 $recipients = array_diff($recipients,$deny);
181 $conversant_str = dbesc(implode(', ',$conversants));
184 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0");
191 $feed_template = load_view_file('view/atom_feed.tpl');
192 $mail_template = load_view_file('view/atom_mail.tpl');
197 $hubxml = feed_hublinks();
199 $birthday = feed_birthday($owner['uid'],$owner['timezone']);
201 if(strlen($birthday))
202 $birthday = '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>';
204 $atom .= replace_macros($feed_template, array(
205 '$version' => xmlify(FRIENDIKA_VERSION),
206 '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
207 '$feed_title' => xmlify($owner['name']),
208 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
210 '$salmon' => '', // private feed, we don't use salmon here
211 '$name' => xmlify($owner['name']),
212 '$profile_page' => xmlify($owner['url']),
213 '$photo' => xmlify($owner['photo']),
214 '$thumb' => xmlify($owner['thumb']),
215 '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
216 '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
217 '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME)) ,
218 '$birthday' => $birthday
221 if($cmd === 'mail') {
222 $notify_hub = false; // mail is not public
224 $atom .= replace_macros($mail_template, array(
225 '$name' => xmlify($owner['name']),
226 '$profile_page' => xmlify($owner['url']),
227 '$thumb' => xmlify($owner['thumb']),
228 '$item_id' => xmlify($item['uri']),
229 '$subject' => xmlify($item['title']),
230 '$created' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
231 '$content' => xmlify($item['body']),
232 '$parent_id' => xmlify($item['parent-uri'])
237 foreach($items as $item) { // there is only one item
238 if(! $item['parent'])
240 if($item['id'] == $item_id) {
241 logger('notifier: followup: item: ' . print_r($item,true), LOGGER_DATA);
242 $slap = atom_entry($item,'html',$owner,$owner,false);
243 $atom .= atom_entry($item,'text',$owner,$owner,false);
248 foreach($items as $item) {
250 if(! $item['parent'])
253 $contact = get_item_contact($item,$contacts);
257 $atom .= atom_entry($item,'text',$contact,$owner,true);
259 if(($top_level) && ($notify_hub) && ($item['author-link'] === $item['owner-link']) && (! $expire))
260 $slaps[] = atom_entry($item,'html',$contact,$owner,true);
264 $atom .= '</feed>' . "\r\n";
266 logger('notifier: ' . $atom, LOGGER_DATA);
268 logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
271 $recip_str = $parent['contact-id'];
273 $recip_str = implode(', ', $recipients);
276 $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 AND `pending` = 0 ",
282 require_once('include/salmon.php');
285 foreach($r as $contact) {
291 switch($contact['network']) {
293 logger('notifier: dfrndelivery: ' . $contact['name']);
294 $deliver_status = dfrn_deliver($owner,$contact,$atom);
296 logger('notifier: dfrn_delivery returns ' . $deliver_status);
298 if($deliver_status == (-1)) {
299 logger('notifier: delivery failed: queuing message');
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()),
311 if($followup && $contact['notify']) {
312 logger('notifier: slapdelivery: ' . $contact['name']);
313 $deliver_status = slapper($owner,$contact['notify'],$slap);
315 if($deliver_status == (-1)) {
316 // queue message for redelivery
317 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
318 VALUES ( %d, '%s', '%s', '%s') ",
319 intval($contact['id']),
320 dbesc(datetime_convert()),
321 dbesc(datetime_convert()),
331 // only send salmon if public - e.g. if it's ok to notify
332 // a public hub, it's ok to send a salmon
334 if((count($slaps)) && ($notify_hub) && (! $expire)) {
335 logger('notifier: slapdelivery: ' . $contact['name']);
336 foreach($slaps as $slappy) {
337 if($contact['notify']) {
338 $deliver_status = slapper($owner,$contact['notify'],$slappy);
339 if($deliver_status == (-1)) {
340 // queue message for redelivery
341 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
342 VALUES ( %d, '%s', '%s', '%s') ",
343 intval($contact['id']),
344 dbesc(datetime_convert()),
345 dbesc(datetime_convert()),
364 // send additional slaps to mentioned remote tags (@foo@example.com)
366 if($slap && count($url_recipients) && $followup && $notify_hub && (! $expire)) {
367 foreach($url_recipients as $url) {
369 logger('notifier: urldelivery: ' . $url);
370 $deliver_status = slapper($owner,$url,$slap);
371 // TODO: redeliver/queue these items on failure, though there is no contact record
376 if((strlen($hub)) && ($notify_hub)) {
377 $hubs = explode(',', $hub);
379 foreach($hubs as $h) {
383 $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
384 post_url($h,$params);
385 logger('pubsub: publish: ' . $h . ' ' . $params . ' returned ' . $a->get_curl_code());
387 sleep(7); // try and avoid multiple hubs responding at precisely the same time
396 * If you have less than 150 dfrn friends and it's a public message,
397 * we'll just go ahead and push them out securely with dfrn/rino.
398 * If you've got more than that, you'll have to rely on PuSH delivery.
402 $max_allowed = ((get_config('system','maxpubdeliver') === false) ? 150 : intval(get_config('system','maxpubdeliver')));
406 * Only get the bare essentials and go back for the full record.
407 * If you've got a lot of friends and we grab all the details at once it could exhaust memory.
411 $r = q("SELECT `id`, `name` FROM `contact`
412 WHERE `network` = 'dfrn' AND `uid` = %d AND `blocked` = 0 AND `pending` = 0
414 intval($owner['uid']),
418 if((count($r)) && (($max_allowed == 0) || (count($r) < $max_allowed))) {
422 /* Don't deliver to folks who have already been delivered to */
424 if(! in_array($rr['id'], $conversants)) {
425 $n = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
431 logger('notifier: dfrnpubdelivery: ' . $n[0]['name']);
432 $deliver_status = dfrn_deliver($owner,$n[0],$atom);
436 logger('notifier: dfrnpubdelivery: ignoring ' . $rr['name']);
444 if (array_search(__file__,get_included_files())===0){
446 notifier_run($argv,$argc);