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]);
44 $recipients = array();
45 $url_recipients = array();
49 $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
52 if(! count($message)){
55 $uid = $message[0]['uid'];
56 $recipients[] = $message[0]['contact-id'];
63 $r = q("SELECT `parent`, `uid`, `edited` FROM `item` WHERE `id` = %d LIMIT 1",
67 if((! count($r)) || (! intval($r[0]['parent']))) {
71 $parent_id = intval($r[0]['parent']);
73 $updated = $r[0]['edited'];
75 $items = q("SELECT * FROM `item` WHERE `parent` = %d ORDER BY `id` ASC",
84 // avoid race condition with deleting entries
86 if($items[0]['deleted']) {
87 foreach($items as $item)
91 if(count($items) == 1 && $items[0]['uri'] === $items[0]['parent-uri'])
95 $r = q("SELECT `contact`.*, `user`.`timezone`, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`, `user`.`page-flags`
96 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
97 WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
106 $hub = get_config('system','huburl');
108 // If this is a public conversation, notify the feed hub
111 // fill this in with a single salmon slap if applicable
116 require_once('include/group.php');
120 if($parent['type'] === 'remote') {
121 // local followup to remote post
123 $notify_hub = false; // not public
124 $conversant_str = dbesc($parent['contact-id']);
129 if((strlen($parent['allow_cid']))
130 || (strlen($parent['allow_gid']))
131 || (strlen($parent['deny_cid']))
132 || (strlen($parent['deny_gid']))) {
133 $notify_hub = false; // private recipients, not public
136 $allow_people = expand_acl($parent['allow_cid']);
137 $allow_groups = expand_groups(expand_acl($parent['allow_gid']));
138 $deny_people = expand_acl($parent['deny_cid']);
139 $deny_groups = expand_groups(expand_acl($parent['deny_gid']));
141 $conversants = array();
143 foreach($items as $item) {
144 $recipients[] = $item['contact-id'];
145 $conversants[] = $item['contact-id'];
146 // pull out additional tagged people to notify (if public message)
147 if($notify_hub && strlen($item['inform'])) {
148 $people = explode(',',$item['inform']);
149 foreach($people as $person) {
150 if(substr($person,0,4) === 'cid:') {
151 $recipients[] = intval(substr($person,4));
152 $conversants[] = intval(substr($person,4));
155 $url_recipients[] = substr($person,4);
161 logger('notifier: url_recipients' . print_r($url_recipients,true));
163 $conversants = array_unique($conversants);
166 $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups));
167 $deny = array_unique(array_merge($deny_people,$deny_groups));
168 $recipients = array_diff($recipients,$deny);
170 $conversant_str = dbesc(implode(', ',$conversants));
173 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0");
180 $feed_template = load_view_file('view/atom_feed.tpl');
181 $mail_template = load_view_file('view/atom_mail.tpl');
186 $hubxml = feed_hublinks();
188 $birthday = feed_birthday($owner['uid'],$owner['timezone']);
190 if(strlen($birthday))
191 $birthday = '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>';
193 $atom .= replace_macros($feed_template, array(
194 '$version' => xmlify(FRIENDIKA_VERSION),
195 '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
196 '$feed_title' => xmlify($owner['name']),
197 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
199 '$salmon' => '', // private feed, we don't use salmon here
200 '$name' => xmlify($owner['name']),
201 '$profile_page' => xmlify($owner['url']),
202 '$photo' => xmlify($owner['photo']),
203 '$thumb' => xmlify($owner['thumb']),
204 '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
205 '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
206 '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME)) ,
207 '$birthday' => $birthday
210 if($cmd === 'mail') {
211 $notify_hub = false; // mail is not public
213 $atom .= replace_macros($mail_template, array(
214 '$name' => xmlify($owner['name']),
215 '$profile_page' => xmlify($owner['url']),
216 '$thumb' => xmlify($owner['thumb']),
217 '$item_id' => xmlify($item['uri']),
218 '$subject' => xmlify($item['title']),
219 '$created' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
220 '$content' => xmlify($item['body']),
221 '$parent_id' => xmlify($item['parent-uri'])
226 foreach($items as $item) { // there is only one item
227 if(! $item['parent'])
229 if($item['id'] == $item_id) {
230 logger('notifier: followup: item: ' . print_r($item,true), LOGGER_DATA);
231 $slap = atom_entry($item,'html',$owner,$owner,false);
232 $atom .= atom_entry($item,'text',$owner,$owner,false);
237 foreach($items as $item) {
238 if(! $item['parent'])
241 $contact = get_item_contact($item,$contacts);
245 $atom .= atom_entry($item,'text',$contact,$owner,true);
247 if(($top_level) && ($notify_hub) && ($item['author-link'] === $item['owner-link']))
248 $slaps[] = atom_entry($item,'html',$contact,$owner,true);
252 $atom .= '</feed>' . "\r\n";
254 logger('notifier: ' . $atom, LOGGER_DATA);
256 logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
259 $recip_str = $parent['contact-id'];
261 $recip_str = implode(', ', $recipients);
264 $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 AND `pending` = 0 ",
270 require_once('include/salmon.php');
273 foreach($r as $contact) {
279 switch($contact['network']) {
281 logger('notifier: dfrndelivery: ' . $contact['name']);
282 $deliver_status = dfrn_deliver($owner,$contact,$atom);
284 logger('notifier: dfrn_delivery returns ' . $deliver_status);
286 if($deliver_status == (-1)) {
287 logger('notifier: delivery failed: queuing message');
288 // queue message for redelivery
289 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
290 VALUES ( %d, '%s', '%s', '%s') ",
291 intval($contact['id']),
292 dbesc(datetime_convert()),
293 dbesc(datetime_convert()),
299 if($followup && $contact['notify']) {
300 logger('notifier: slapdelivery: ' . $contact['name']);
301 $deliver_status = slapper($owner,$contact['notify'],$slap);
303 if($deliver_status == (-1)) {
304 // queue message for redelivery
305 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
306 VALUES ( %d, '%s', '%s', '%s') ",
307 intval($contact['id']),
308 dbesc(datetime_convert()),
309 dbesc(datetime_convert()),
319 // only send salmon if public - e.g. if it's ok to notify
320 // a public hub, it's ok to send a salmon
322 if(count($slaps) && $notify_hub) {
323 logger('notifier: slapdelivery: ' . $contact['name']);
324 foreach($slaps as $slappy) {
325 if($contact['notify']) {
326 $deliver_status = slapper($owner,$contact['notify'],$slappy);
327 if($deliver_status == (-1)) {
328 // queue message for redelivery
329 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
330 VALUES ( %d, '%s', '%s', '%s') ",
331 intval($contact['id']),
332 dbesc(datetime_convert()),
333 dbesc(datetime_convert()),
351 // send additional slaps to mentioned remote tags (@foo@example.com)
353 if($slap && count($url_recipients) && $followup && $notify_hub) {
354 foreach($url_recipients as $url) {
356 logger('notifier: urldelivery: ' . $url);
357 $deliver_status = slapper($owner,$url,$slap);
358 // TODO: redeliver/queue these items on failure, though there is no contact record
363 if((strlen($hub)) && ($notify_hub)) {
364 $hubs = explode(',', $hub);
366 foreach($hubs as $h) {
370 $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
371 post_url($h,$params);
372 logger('pubsub: publish: ' . $h . ' ' . $params . ' returned ' . $a->get_curl_code());
374 sleep(7); // try and avoid multiple hubs responding at precisely the same time
383 * If you have less than 150 dfrn friends and it's a public message,
384 * we'll just go ahead and push them out securely with dfrn/rino.
385 * If you've got more than that, you'll have to rely on PuSH delivery.
389 $max_allowed = ((get_config('system','maxpubdeliver') === false) ? 150 : intval(get_config('system','maxpubdeliver')));
393 * Only get the bare essentials and go back for the full record.
394 * If you've got a lot of friends and we grab all the details at once it could exhaust memory.
398 $r = q("SELECT `id`, `name` FROM `contact`
399 WHERE `network` = 'dfrn' AND `uid` = %d AND `blocked` = 0 AND `pending` = 0
401 intval($owner['uid']),
405 if((count($r)) && (($max_allowed == 0) || (count($r) < $max_allowed))) {
409 /* Don't deliver to folks who have already been delivered to */
411 if(! in_array($rr['id'], $conversants)) {
412 $n = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
418 logger('notifier: dfrnpubdelivery: ' . $n[0]['name']);
419 $deliver_status = dfrn_deliver($owner,$n[0],$atom);
423 logger('notifier: dfrnpubdelivery: ignoring ' . $rr['name']);
431 if (array_search(__file__,get_included_files())===0){
433 notifier_run($argv,$argc);