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`.`timezone`, `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');
184 $hubxml = feed_hublinks();
186 $birthday = feed_birthday($owner['uid'],$owner['timezone']);
188 if(strlen($birthday))
189 $birthday = '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>';
191 $atom .= replace_macros($feed_template, array(
192 '$version' => xmlify(FRIENDIKA_VERSION),
193 '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
194 '$feed_title' => xmlify($owner['name']),
195 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
197 '$salmon' => '', // private feed, we don't use salmon here
198 '$name' => xmlify($owner['name']),
199 '$profile_page' => xmlify($owner['url']),
200 '$photo' => xmlify($owner['photo']),
201 '$thumb' => xmlify($owner['thumb']),
202 '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
203 '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
204 '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME)) ,
205 '$birthday' => $birthday
208 if($cmd === 'mail') {
209 $notify_hub = false; // mail is not public
211 $atom .= replace_macros($mail_template, array(
212 '$name' => xmlify($owner['name']),
213 '$profile_page' => xmlify($owner['url']),
214 '$thumb' => xmlify($owner['thumb']),
215 '$item_id' => xmlify($item['uri']),
216 '$subject' => xmlify($item['title']),
217 '$created' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
218 '$content' => xmlify($item['body']),
219 '$parent_id' => xmlify($item['parent-uri'])
224 foreach($items as $item) { // there is only one item
225 if(! $item['parent'])
227 if($item['id'] == $item_id) {
228 logger('notifier: followup: item: ' . print_r($item,true), LOGGER_DATA);
229 $slap = atom_entry($item,'html',$owner,$owner,false);
230 $atom .= atom_entry($item,'text',$owner,$owner,false);
235 foreach($items as $item) {
236 if(! $item['parent'])
239 $contact = get_item_contact($item,$contacts);
243 $atom .= atom_entry($item,'text',$contact,$owner,true);
245 // There's a problem here - we *were* going to use salmon to provide semi-authenticated
246 // communication to OStatus, but unless we're the item author they won't verify.
247 // commented out for now, though we'll still send local replies (and any mentions
248 // that they contain) upstream. Rethinking the problem space.
250 // $slaps[] = atom_entry($item,'html',$contact,$owner,true);
254 $atom .= '</feed>' . "\r\n";
256 logger('notifier: ' . $atom, LOGGER_DATA);
258 // logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
261 $recip_str = $parent['contact-id'];
263 $recip_str = implode(', ', $recipients);
266 $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 AND `pending` = 0 ",
272 require_once('include/salmon.php');
275 foreach($r as $contact) {
281 switch($contact['network']) {
283 logger('notifier: dfrndelivery: ' . $contact['name']);
284 $deliver_status = dfrn_deliver($owner,$contact,$atom);
286 logger('notifier: dfrn_delivery returns ' . $deliver_status);
288 if($deliver_status == (-1)) {
289 logger('notifier: delivery failed: queuing message');
290 // queue message for redelivery
291 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
292 VALUES ( %d, '%s', '%s', '%s') ",
293 intval($contact['id']),
294 dbesc(datetime_convert()),
295 dbesc(datetime_convert()),
301 if($followup && $contact['notify']) {
302 logger('notifier: slapdelivery: ' . $contact['name']);
303 $deliver_status = slapper($owner,$contact['notify'],$slap);
305 if($deliver_status == (-1)) {
306 // queue message for redelivery
307 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
308 VALUES ( %d, '%s', '%s', '%s') ",
309 intval($contact['id']),
310 dbesc(datetime_convert()),
311 dbesc(datetime_convert()),
321 // only send salmon if public - e.g. if it's ok to notify
322 // a public hub, it's ok to send a salmon
324 if(count($slaps) && $notify_hub) {
325 logger('notifier: slapdelivery: ' . $contact['name']);
326 foreach($slaps as $slappy) {
327 if($contact['notify']) {
328 $deliver_status = slapper($owner,$contact['notify'],$slappy);
329 if($deliver_status == (-1)) {
330 // queue message for redelivery
331 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
332 VALUES ( %d, '%s', '%s', '%s') ",
333 intval($contact['id']),
334 dbesc(datetime_convert()),
335 dbesc(datetime_convert()),
353 // send additional slaps to mentioned remote tags (@foo@example.com)
355 if($slap && count($url_recipients) && $followup && $notify_hub) {
356 foreach($url_recipients as $url) {
358 logger('notifier: urldelivery: ' . $url);
359 $deliver_status = slapper($owner,$url,$slap);
360 // TODO: redeliver/queue these items on failure, though there is no contact record
365 if((strlen($hub)) && ($notify_hub)) {
366 $hubs = explode(',', $hub);
368 foreach($hubs as $h) {
372 $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
373 post_url($h,$params);
374 logger('pubsub: publish: ' . $h . ' ' . $params . ' returned ' . $a->get_curl_code());
376 sleep(7); // try and avoid multiple hubs responding at precisely the same time
385 * If you have less than 150 dfrn friends and it's a public message,
386 * we'll just go ahead and push them out securely with dfrn/rino.
387 * If you've got more than that, you'll have to rely on PuSH delivery.
391 $max_allowed = ((get_config('system','maxpubdeliver') === false) ? 150 : intval(get_config('system','maxpubdeliver')));
395 * Only get the bare essentials and go back for the full record.
396 * If you've got a lot of friends and we grab all the details at once it could exhaust memory.
400 $r = q("SELECT `id`, `name` FROM `contact`
401 WHERE `network` = 'dfrn' AND `uid` = %d AND `blocked` = 0 AND `pending` = 0
403 intval($owner['uid']),
407 if((count($r)) && (($max_allowed == 0) || (count($r) < $max_allowed))) {
411 /* Don't deliver to folks who have already been delivered to */
413 if(! in_array($rr['id'], $conversants)) {
414 $n = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
420 logger('notifier: dfrnpubdelivery: ' . $n[0]['name']);
421 $deliver_status = dfrn_deliver($owner,$n[0],$atom);
425 logger('notifier: dfrnpubdelivery: ignoring ' . $rr['name']);
433 if (array_search(__file__,get_included_files())===0){
435 notifier_run($argv,$argc);