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');
23 load_config('config');
24 load_config('system');
31 $a->set_baseurl(get_config('system','url'));
33 logger('notifier: invoked: ' . print_r($argv,true));
41 $item_id = intval($argv[2]);
50 $recipients = array();
51 $url_recipients = array();
55 $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
58 if(! count($message)){
61 $uid = $message[0]['uid'];
62 $recipients[] = $message[0]['contact-id'];
66 elseif($cmd === 'expire') {
68 $items = q("SELECT * FROM `item` WHERE `uid` = %d AND `wall` = 1
69 AND `deleted` = 1 AND `changed` > UTC_TIMESTAMP - INTERVAL 10 MINUTE",
77 elseif($cmd === 'suggest') {
78 $suggest = q("SELECT * FROM `fsuggest` WHERE `id` = %d LIMIT 1",
83 $uid = $suggest[0]['uid'];
84 $recipients[] = $suggest[0]['cid'];
90 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
94 if((! count($r)) || (! intval($r[0]['parent']))) {
99 $parent_id = intval($r[0]['parent']);
101 $updated = $r[0]['edited'];
103 $items = q("SELECT * FROM `item` WHERE `parent` = %d ORDER BY `id` ASC",
107 if(! count($items)) {
111 // avoid race condition with deleting entries
113 if($items[0]['deleted']) {
114 foreach($items as $item)
115 $item['deleted'] = 1;
118 if(count($items) == 1 && $items[0]['uri'] === $items[0]['parent-uri'])
122 $r = q("SELECT `contact`.*, `user`.`timezone`, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`,
123 `user`.`page-flags`, `user`.`prvnets`
124 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
125 WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
134 $hub = get_config('system','huburl');
136 // If this is a public conversation, notify the feed hub
139 // fill this in with a single salmon slap if applicable
142 if($cmd != 'mail' && $cmd != 'suggest') {
144 require_once('include/group.php');
148 if($parent['type'] === 'remote' && (! $expire)) {
149 // local followup to remote post
151 $notify_hub = false; // not public
152 $conversant_str = dbesc($parent['contact-id']);
157 if((strlen($parent['allow_cid']))
158 || (strlen($parent['allow_gid']))
159 || (strlen($parent['deny_cid']))
160 || (strlen($parent['deny_gid']))) {
161 $notify_hub = false; // private recipients, not public
164 $allow_people = expand_acl($parent['allow_cid']);
165 $allow_groups = expand_groups(expand_acl($parent['allow_gid']));
166 $deny_people = expand_acl($parent['deny_cid']);
167 $deny_groups = expand_groups(expand_acl($parent['deny_gid']));
169 $conversants = array();
171 foreach($items as $item) {
172 $recipients[] = $item['contact-id'];
173 $conversants[] = $item['contact-id'];
174 // pull out additional tagged people to notify (if public message)
175 if($notify_hub && strlen($item['inform'])) {
176 $people = explode(',',$item['inform']);
177 foreach($people as $person) {
178 if(substr($person,0,4) === 'cid:') {
179 $recipients[] = intval(substr($person,4));
180 $conversants[] = intval(substr($person,4));
183 $url_recipients[] = substr($person,4);
189 logger('notifier: url_recipients' . print_r($url_recipients,true));
191 $conversants = array_unique($conversants);
194 $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups));
195 $deny = array_unique(array_merge($deny_people,$deny_groups));
196 $recipients = array_diff($recipients,$deny);
198 $conversant_str = dbesc(implode(', ',$conversants));
201 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0");
208 $feed_template = get_markup_template('atom_feed.tpl');
209 $mail_template = get_markup_template('atom_mail.tpl');
214 $hubxml = feed_hublinks();
216 $birthday = feed_birthday($owner['uid'],$owner['timezone']);
218 if(strlen($birthday))
219 $birthday = '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>';
221 $atom .= replace_macros($feed_template, array(
222 '$version' => xmlify(FRIENDIKA_VERSION),
223 '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
224 '$feed_title' => xmlify($owner['name']),
225 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
227 '$salmon' => '', // private feed, we don't use salmon here
228 '$name' => xmlify($owner['name']),
229 '$profile_page' => xmlify($owner['url']),
230 '$photo' => xmlify($owner['photo']),
231 '$thumb' => xmlify($owner['thumb']),
232 '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
233 '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
234 '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME)) ,
235 '$birthday' => $birthday
238 if($cmd === 'mail') {
239 $notify_hub = false; // mail is not public
241 $body = fix_private_photos($item['body'],$owner['uid']);
243 $atom .= replace_macros($mail_template, array(
244 '$name' => xmlify($owner['name']),
245 '$profile_page' => xmlify($owner['url']),
246 '$thumb' => xmlify($owner['thumb']),
247 '$item_id' => xmlify($item['uri']),
248 '$subject' => xmlify($item['title']),
249 '$created' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
250 '$content' => xmlify($body),
251 '$parent_id' => xmlify($item['parent-uri'])
254 elseif($cmd === 'suggest') {
255 $notify_hub = false; // suggestions are not public
257 $sugg_template = get_markup_template('atom_suggest.tpl');
259 $atom .= replace_macros($sugg_template, array(
260 '$name' => xmlify($item['name']),
261 '$url' => xmlify($item['url']),
262 '$photo' => xmlify($item['photo']),
263 '$request' => xmlify($item['request']),
264 '$note' => xmlify($item['note'])
267 // We don't need this any more
269 q("DELETE FROM `fsuggest` WHERE `id` = %d LIMIT 1",
276 foreach($items as $item) { // there is only one item
277 if(! $item['parent'])
279 if($item['id'] == $item_id) {
280 logger('notifier: followup: item: ' . print_r($item,true), LOGGER_DATA);
281 $slap = atom_entry($item,'html',$owner,$owner,false);
282 $atom .= atom_entry($item,'text',$owner,$owner,false);
287 foreach($items as $item) {
289 if(! $item['parent'])
292 $contact = get_item_contact($item,$contacts);
296 $atom .= atom_entry($item,'text',$contact,$owner,true);
298 if(($top_level) && ($notify_hub) && ($item['author-link'] === $item['owner-link']) && (! $expire))
299 $slaps[] = atom_entry($item,'html',$contact,$owner,true);
303 $atom .= '</feed>' . "\r\n";
305 logger('notifier: ' . $atom, LOGGER_DATA);
307 logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
309 // If this is a public message and pubmail is set on the parent, include all your email contacts
311 $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
313 if(! $mail_disabled) {
314 if((! strlen($parent_item['allow_cid'])) && (! strlen($parent_item['allow_gid']))
315 && (! strlen($parent_item['deny_cid'])) && (! strlen($parent_item['deny_gid']))
316 && (intval($parent_item['pubmail']))) {
317 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `network` = '%s'",
323 $recipients[] = $rr['id'];
329 $recip_str = $parent['contact-id'];
331 $recip_str = implode(', ', $recipients);
333 $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 AND `pending` = 0 ",
339 require_once('include/salmon.php');
342 foreach($r as $contact) {
348 switch($contact['network']) {
350 logger('notifier: dfrndelivery: ' . $contact['name']);
351 $deliver_status = dfrn_deliver($owner,$contact,$atom);
353 logger('notifier: dfrn_delivery returns ' . $deliver_status);
355 if($deliver_status == (-1)) {
356 logger('notifier: delivery failed: queuing message');
357 // queue message for redelivery
358 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
359 VALUES ( %d, '%s', '%s', '%s') ",
360 intval($contact['id']),
361 dbesc(datetime_convert()),
362 dbesc(datetime_convert()),
368 if($owner['prvnets'])
370 if($followup && $contact['notify']) {
371 logger('notifier: slapdelivery: ' . $contact['name']);
372 $deliver_status = slapper($owner,$contact['notify'],$slap);
374 if($deliver_status == (-1)) {
375 // queue message for redelivery
376 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
377 VALUES ( %d, '%s', '%s', '%s') ",
378 intval($contact['id']),
379 dbesc(datetime_convert()),
380 dbesc(datetime_convert()),
390 // only send salmon if public - e.g. if it's ok to notify
391 // a public hub, it's ok to send a salmon
393 if((count($slaps)) && ($notify_hub) && (! $expire)) {
394 logger('notifier: slapdelivery: ' . $contact['name']);
395 foreach($slaps as $slappy) {
396 if($contact['notify']) {
397 $deliver_status = slapper($owner,$contact['notify'],$slappy);
398 if($deliver_status == (-1)) {
399 // queue message for redelivery
400 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
401 VALUES ( %d, '%s', '%s', '%s') ",
402 intval($contact['id']),
403 dbesc(datetime_convert()),
404 dbesc(datetime_convert()),
416 // WARNING: does not currently convert to RFC2047 header encodings, etc.
418 $addr = $contact['addr'];
422 if($cmd === 'wall-new' || $cmd === 'comment-new') {
425 if($cmd === 'wall-new')
428 $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
440 $local_user = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
443 if(! count($local_user))
447 $r1 = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
450 if($r1 && $r1[0]['reply_to'])
451 $reply_to = $r1[0]['reply_to'];
453 $subject = (($it['title']) ? $it['title'] : t("\x28no subject\x29")) ;
454 $headers = 'From: ' . $local_user[0]['username'] . ' <' . $local_user[0]['email'] . '>' . "\n";
457 $headers .= 'Reply-to: ' . $reply_to . "\n";
459 $headers .= 'Message-id: <' . $it['uri'] . '>' . "\n";
461 if($it['uri'] !== $it['parent-uri']) {
462 $header .= 'References: <' . $it['parent-uri'] . '>' . "\n";
463 if(! strlen($it['title'])) {
464 $r = q("SELECT `title` FROM `item` WHERE `parent-uri` = '%s' LIMIT 1",
465 dbesc($it['parent-uri'])
468 $subtitle = $r[0]['title'];
470 if(strncasecmp($subtitle,'RE:',3))
471 $subject = $subtitle;
473 $subject = 'Re: ' . $subtitle;
479 $headers .= 'MIME-Version: 1.0' . "\n";
480 $headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
481 $headers .= 'Content-Transfer-Encoding: 8bit' . "\n\n";
482 $html = prepare_body($it);
483 $message = '<html><body>' . $html . '</body></html>';
484 logger('notifier: email delivery to ' . $addr);
485 mail($addr, $subject, $message, $headers);
497 // send additional slaps to mentioned remote tags (@foo@example.com)
499 if($slap && count($url_recipients) && $followup && $notify_hub && (! $expire)) {
500 foreach($url_recipients as $url) {
502 logger('notifier: urldelivery: ' . $url);
503 $deliver_status = slapper($owner,$url,$slap);
504 // TODO: redeliver/queue these items on failure, though there is no contact record
509 if((strlen($hub)) && ($notify_hub)) {
510 $hubs = explode(',', $hub);
512 foreach($hubs as $h) {
516 $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
517 post_url($h,$params);
518 logger('pubsub: publish: ' . $h . ' ' . $params . ' returned ' . $a->get_curl_code());
520 sleep(7); // try and avoid multiple hubs responding at precisely the same time
529 * If you have less than 150 dfrn friends and it's a public message,
530 * we'll just go ahead and push them out securely with dfrn/rino.
531 * If you've got more than that, you'll have to rely on PuSH delivery.
535 $max_allowed = ((get_config('system','maxpubdeliver') === false) ? 150 : intval(get_config('system','maxpubdeliver')));
539 * Only get the bare essentials and go back for the full record.
540 * If you've got a lot of friends and we grab all the details at once it could exhaust memory.
544 $r = q("SELECT `id`, `name` FROM `contact`
545 WHERE `network` = 'dfrn' AND `uid` = %d AND `blocked` = 0 AND `pending` = 0
547 intval($owner['uid']),
551 if((count($r)) && (($max_allowed == 0) || (count($r) < $max_allowed))) {
553 logger('pubdeliver: ' . print_r($r,true));
557 /* Don't deliver to folks who have already been delivered to */
559 if(! in_array($rr['id'], $conversants)) {
560 $n = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
566 logger('notifier: dfrnpubdelivery: ' . $n[0]['name']);
567 $deliver_status = dfrn_deliver($owner,$n[0],$atom);
571 logger('notifier: dfrnpubdelivery: ignoring ' . $rr['name']);
579 if (array_search(__file__,get_included_files())===0){
581 notifier_run($argv,$argc);