2 require_once("boot.php");
4 function delivery_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');
22 require_once('include/diaspora.php');
24 load_config('config');
25 load_config('system');
32 $a->set_baseurl(get_config('system','url'));
34 logger('delivery: invoked: ' . print_r($argv,true), LOGGER_DEBUG);
37 $item_id = intval($argv[2]);
38 $contact_id = intval($argv[3]);
40 // Some other process may have delivered this item already.
42 $r = q("select * from deliverq where cmd = '%s' and item = %d and contact = %d limit 1",
51 // It's ours to deliver. Remove it from the queue.
53 q("delete from deliverq where cmd = '%s' and item = %d and contact = %d limit 1",
59 if((! $item_id) || (! $contact_id))
64 $recipients = array();
65 $url_recipients = array();
69 $recipients[] = $contact_id;
71 if($cmd === 'expire') {
74 $items = q("SELECT * FROM `item` WHERE `uid` = %d AND `wall` = 1
75 AND `deleted` = 1 AND `changed` > UTC_TIMESTAMP - INTERVAL 30 MINUTE",
86 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
90 if((! count($r)) || (! intval($r[0]['parent']))) {
95 $parent_id = intval($r[0]['parent']);
97 $updated = $r[0]['edited'];
101 $items = q("SELECT `item`.*, `sign`.`signed_text`,`sign`.`signature`,`sign`.`signer`
102 FROM `item` LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id` WHERE `parent` = %d ORDER BY `id` ASC",
106 if(! count($items)) {
110 $icontacts = q("SELECT * FROM `contact` WHERE `id` IN ( SELECT distinct(`contact-id`) FROM `item` where `parent` = %d ) ",
113 if(! count($icontacts))
117 // avoid race condition with deleting entries
119 if($items[0]['deleted']) {
120 foreach($items as $item)
121 $item['deleted'] = 1;
124 if((count($items) == 1) && ($items[0]['uri'] === $items[0]['parent-uri'])) {
125 logger('delivery: top level post');
130 $r = q("SELECT `contact`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`,
131 `user`.`timezone`, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`,
132 `user`.`page-flags`, `user`.`prvnets`
133 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
134 WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
143 $walltowall = ((($top_level) && ($owner['id'] != $items[0]['contact-id'])) ? true : false);
145 $public_message = true;
147 // fill this in with a single salmon slap if applicable
151 require_once('include/group.php');
155 // This is IMPORTANT!!!!
157 // We will only send a "notify owner to relay" or followup message if the referenced post
158 // originated on our system by virtue of having our hostname somewhere
159 // in the URI, AND it was a comment (not top_level) AND the parent originated elsewhere.
160 // if $parent['wall'] == 1 we will already have the parent message in our array
161 // and we will relay the whole lot.
163 // expire sends an entire group of expire messages and cannot be forwarded.
164 // However the conversation owner will be a part of the conversation and will
165 // be notified during this run.
166 // Other DFRN conversation members will be alerted during polled updates.
168 // Diaspora members currently are not notified of expirations, and other networks have
169 // either limited or no ability to process deletions. We should at least fix Diaspora
170 // by stringing togther an array of retractions and sending them onward.
173 $localhost = $a->get_hostname();
174 if(strpos($localhost,':'))
175 $localhost = substr($localhost,0,strpos($localhost,':'));
179 * Be VERY CAREFUL if you make any changes to the following line. Seemingly innocuous changes
180 * have been known to cause runaway conditions which affected several servers, along with
181 * permissions issues.
185 if((! $top_level) && ($parent['wall'] == 0) && (! $expire) && (stristr($target_item['uri'],$localhost))) {
186 logger('relay denied for delivery agent.');
188 /* no relay allowed for direct contact delivery */
192 if((strlen($parent['allow_cid']))
193 || (strlen($parent['allow_gid']))
194 || (strlen($parent['deny_cid']))
195 || (strlen($parent['deny_gid']))) {
196 $public_message = false; // private recipients, not public
199 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `blocked` = 0 AND `pending` = 0",
206 $hubxml = feed_hublinks();
208 logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
210 require_once('include/salmon.php');
217 switch($contact['network']) {
220 logger('notifier: dfrndelivery: ' . $contact['name']);
222 $feed_template = get_markup_template('atom_feed.tpl');
223 $mail_template = get_markup_template('atom_mail.tpl');
228 $birthday = feed_birthday($owner['uid'],$owner['timezone']);
230 if(strlen($birthday))
231 $birthday = '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>';
233 $atom .= replace_macros($feed_template, array(
234 '$version' => xmlify(FRIENDIKA_VERSION),
235 '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
236 '$feed_title' => xmlify($owner['name']),
237 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
239 '$salmon' => '', // private feed, we don't use salmon here
240 '$name' => xmlify($owner['name']),
241 '$profile_page' => xmlify($owner['url']),
242 '$photo' => xmlify($owner['photo']),
243 '$thumb' => xmlify($owner['thumb']),
244 '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
245 '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
246 '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME)) ,
247 '$birthday' => $birthday
250 foreach($items as $item) {
251 if(! $item['parent'])
254 // private emails may be in included in public conversations. Filter them.
255 if(($public_message) && $item['private'])
258 $item_contact = get_item_contact($item,$icontacts);
262 $atom .= atom_entry($item,'text',$item_contact,$owner,true);
266 $atom .= '</feed>' . "\r\n";
268 logger('notifier: ' . $atom, LOGGER_DATA);
270 $deliver_status = dfrn_deliver($owner,$contact,$atom);
272 logger('notifier: dfrn_delivery returns ' . $deliver_status);
274 if($deliver_status == (-1)) {
275 logger('notifier: delivery failed: queuing message');
276 // queue message for redelivery
277 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
278 VALUES ( %d, '%s', '%s', '%s') ",
279 intval($contact['id']),
280 dbesc(datetime_convert()),
281 dbesc(datetime_convert()),
287 case NETWORK_OSTATUS :
289 // Do not send to otatus if we are not configured to send to public networks
290 if($owner['prvnets'])
292 if(get_config('system','ostatus_disabled') || get_config('system','dfrn_only'))
295 // only send salmon if public - e.g. if it's ok to notify
296 // a public hub, it's ok to send a salmon
298 if(($public_message) && (! $expire)) {
301 foreach($items as $item) {
302 if(! $item['parent'])
305 // private emails may be in included in public conversations. Filter them.
306 if(($public_message) && $item['private'])
309 $item_contact = get_item_contact($item,$icontacts);
313 if(($top_level) && ($public_message) && ($item['author-link'] === $item['owner-link']) && (! $expire))
314 $slaps[] = atom_entry($item,'html',$item_contact,$owner,true);
317 logger('notifier: slapdelivery: ' . $contact['name']);
318 foreach($slaps as $slappy) {
319 if($contact['notify']) {
320 $deliver_status = slapper($owner,$contact['notify'],$slappy);
321 if($deliver_status == (-1)) {
322 // queue message for redelivery
323 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
324 VALUES ( %d, '%s', '%s', '%s') ",
325 intval($contact['id']),
326 dbesc(datetime_convert()),
327 dbesc(datetime_convert()),
339 if(get_config('system','dfrn_only'))
341 // WARNING: does not currently convert to RFC2047 header encodings, etc.
343 $addr = $contact['addr'];
347 if($cmd === 'wall-new' || $cmd === 'comment-new') {
350 if($cmd === 'wall-new')
353 $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
364 $local_user = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
367 if(! count($local_user))
371 $r1 = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
374 if($r1 && $r1[0]['reply_to'])
375 $reply_to = $r1[0]['reply_to'];
377 $subject = (($it['title']) ? $it['title'] : t("\x28no subject\x29")) ;
378 $headers = 'From: ' . $local_user[0]['username'] . ' <' . $local_user[0]['email'] . '>' . "\n";
380 $headers .= 'Reply-to: ' . $reply_to . "\n";
381 $headers .= 'Message-id: <' . $it['uri'] . '>' . "\n";
382 if($it['uri'] !== $it['parent-uri']) {
383 $header .= 'References: <' . $it['parent-uri'] . '>' . "\n";
384 if(! strlen($it['title'])) {
385 $r = q("SELECT `title` FROM `item` WHERE `parent-uri` = '%s' LIMIT 1",
386 dbesc($it['parent-uri'])
389 $subtitle = $r[0]['title'];
391 if(strncasecmp($subtitle,'RE:',3))
392 $subject = $subtitle;
394 $subject = 'Re: ' . $subtitle;
399 $headers .= 'MIME-Version: 1.0' . "\n";
400 $headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
401 $headers .= 'Content-Transfer-Encoding: 8bit' . "\n\n";
402 $html = prepare_body($it);
403 $message = '<html><body>' . $html . '</body></html>';
404 logger('notifier: email delivery to ' . $addr);
405 mail($addr, $subject, $message, $headers);
409 case NETWORK_DIASPORA :
411 $loc = 'public batch ' . $contact['batch'];
413 $loc = $contact['name'];
415 logger('delivery: diaspora batch deliver: ' . $loc);
417 if(get_config('system','dfrn_only') || (! get_config('system','diaspora_enabled')) || (! $normal_mode))
420 if((! $contact['pubkey']) && (! $public_message))
423 if($target_item['verb'] === ACTIVITY_DISLIKE) {
427 elseif(($target_item['deleted']) && ($target_item['verb'] !== ACTIVITY_LIKE)) {
428 logger('delivery: diaspora retract: ' . $loc);
430 diaspora_send_retraction($target_item,$owner,$contact,$public_message);
433 elseif($target_item['parent'] != $target_item['id']) {
435 logger('delivery: diaspora relay: ' . $loc);
437 // we are the relay - send comments, likes and unlikes to our conversants
438 diaspora_send_relay($target_item,$owner,$contact,$public_message);
441 elseif(($top_level) && (! $walltowall)) {
442 // currently no workable solution for sending walltowall
443 logger('delivery: diaspora status: ' . $loc);
444 diaspora_send_status($target_item,$owner,$contact,$public_message);
448 logger('delivery: diaspora unknown mode: ' . $contact['name']);
453 case NETWORK_FACEBOOK :
454 if(get_config('system','dfrn_only'))
463 if (array_search(__file__,get_included_files())===0){
464 delivery_run($argv,$argc);