2 require_once("boot.php");
3 require_once('include/queue_fn.php');
4 require_once('include/html2plain.php');
6 function delivery_run($argv, $argc){
14 @include(".htconfig.php");
15 require_once("dba.php");
16 $db = new dba($db_host, $db_user, $db_pass, $db_data);
17 unset($db_host, $db_user, $db_pass, $db_data);
20 require_once("session.php");
21 require_once("datetime.php");
22 require_once('include/items.php');
23 require_once('include/bbcode.php');
24 require_once('include/diaspora.php');
25 require_once('include/email.php');
27 load_config('config');
28 load_config('system');
35 $a->set_baseurl(get_config('system','url'));
37 logger('delivery: invoked: ' . print_r($argv,true), LOGGER_DEBUG);
40 $item_id = intval($argv[2]);
41 $contact_id = intval($argv[3]);
43 // Some other process may have delivered this item already.
45 $r = q("select * from deliverq where cmd = '%s' and item = %d and contact = %d limit 1",
54 // It's ours to deliver. Remove it from the queue.
56 q("delete from deliverq where cmd = '%s' and item = %d and contact = %d limit 1",
62 if((! $item_id) || (! $contact_id))
67 $recipients = array();
68 $url_recipients = array();
72 $recipients[] = $contact_id;
74 if($cmd === 'expire') {
77 $items = q("SELECT * FROM `item` WHERE `uid` = %d AND `wall` = 1
78 AND `deleted` = 1 AND `changed` > UTC_TIMESTAMP() - INTERVAL 30 MINUTE",
89 $r = q("SELECT * FROM `item` WHERE `id` = %d and visible = 1 and moderated = 0 LIMIT 1",
93 if((! count($r)) || (! intval($r[0]['parent']))) {
98 $parent_id = intval($r[0]['parent']);
100 $updated = $r[0]['edited'];
106 $items = q("SELECT `item`.*, `sign`.`signed_text`,`sign`.`signature`,`sign`.`signer`
107 FROM `item` LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id` WHERE `parent` = %d and visible = 1 and moderated = 0 ORDER BY `id` ASC",
111 if(! count($items)) {
116 $contacts_arr = array();
117 foreach($items as $item)
118 if(! in_array($item['contact-id'],$contacts_arr))
119 $contacts_arr[] = intval($item['contact-id']);
120 if(count($contacts_arr)) {
121 $str_contacts = implode(',',$contacts_arr);
122 $icontacts = q("SELECT * FROM `contact`
123 WHERE `id` IN ( $str_contacts ) "
126 if( ! ($icontacts && count($icontacts)))
129 // avoid race condition with deleting entries
131 if($items[0]['deleted']) {
132 foreach($items as $item)
133 $item['deleted'] = 1;
136 if((count($items) == 1) && ($items[0]['uri'] === $items[0]['parent-uri'])) {
137 logger('delivery: top level post');
142 $r = q("SELECT `contact`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`,
143 `user`.`timezone`, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`,
144 `user`.`page-flags`, `user`.`prvnets`
145 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
146 WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
155 $walltowall = ((($top_level) && ($owner['id'] != $items[0]['contact-id'])) ? true : false);
157 $public_message = true;
159 // fill this in with a single salmon slap if applicable
163 require_once('include/group.php');
167 // This is IMPORTANT!!!!
169 // We will only send a "notify owner to relay" or followup message if the referenced post
170 // originated on our system by virtue of having our hostname somewhere
171 // in the URI, AND it was a comment (not top_level) AND the parent originated elsewhere.
172 // if $parent['wall'] == 1 we will already have the parent message in our array
173 // and we will relay the whole lot.
175 // expire sends an entire group of expire messages and cannot be forwarded.
176 // However the conversation owner will be a part of the conversation and will
177 // be notified during this run.
178 // Other DFRN conversation members will be alerted during polled updates.
180 // Diaspora members currently are not notified of expirations, and other networks have
181 // either limited or no ability to process deletions. We should at least fix Diaspora
182 // by stringing togther an array of retractions and sending them onward.
185 $localhost = $a->get_hostname();
186 if(strpos($localhost,':'))
187 $localhost = substr($localhost,0,strpos($localhost,':'));
191 * Be VERY CAREFUL if you make any changes to the following line. Seemingly innocuous changes
192 * have been known to cause runaway conditions which affected several servers, along with
193 * permissions issues.
197 if((! $top_level) && ($parent['wall'] == 0) && (! $expire) && (stristr($target_item['uri'],$localhost))) {
198 logger('relay denied for delivery agent.');
200 /* no relay allowed for direct contact delivery */
204 if((strlen($parent['allow_cid']))
205 || (strlen($parent['allow_gid']))
206 || (strlen($parent['deny_cid']))
207 || (strlen($parent['deny_gid']))) {
208 $public_message = false; // private recipients, not public
211 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `blocked` = 0 AND `pending` = 0",
218 $hubxml = feed_hublinks();
220 logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
222 require_once('include/salmon.php');
229 switch($contact['network']) {
232 logger('notifier: dfrndelivery: ' . $contact['name']);
234 $feed_template = get_markup_template('atom_feed.tpl');
235 $mail_template = get_markup_template('atom_mail.tpl');
240 $birthday = feed_birthday($owner['uid'],$owner['timezone']);
242 if(strlen($birthday))
243 $birthday = '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>';
245 $atom .= replace_macros($feed_template, array(
246 '$version' => xmlify(FRIENDICA_VERSION),
247 '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
248 '$feed_title' => xmlify($owner['name']),
249 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
251 '$salmon' => '', // private feed, we don't use salmon here
252 '$name' => xmlify($owner['name']),
253 '$profile_page' => xmlify($owner['url']),
254 '$photo' => xmlify($owner['photo']),
255 '$thumb' => xmlify($owner['thumb']),
256 '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
257 '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
258 '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME)) ,
259 '$birthday' => $birthday,
260 '$community' => (($owner['page-flags'] == PAGE_COMMUNITY) ? '<dfrn:community>1</dfrn:community>' : '')
263 foreach($items as $item) {
264 if(! $item['parent'])
267 // private emails may be in included in public conversations. Filter them.
268 if(($public_message) && $item['private'])
271 $item_contact = get_item_contact($item,$icontacts);
276 if($item_id == $item['id'] || $item['id'] == $item['parent'])
277 $atom .= atom_entry($item,'text',null,$owner,true);
280 $atom .= atom_entry($item,'text',null,$owner,true);
284 $atom .= '</feed>' . "\r\n";
286 logger('notifier: ' . $atom, LOGGER_DATA);
287 $basepath = implode('/', array_slice(explode('/',$contact['url']),0,3));
289 // perform local delivery if we are on the same site
291 if(link_compare($basepath,$a->get_baseurl())) {
293 $nickname = basename($contact['url']);
294 if($contact['issued-id'])
295 $sql_extra = sprintf(" AND `dfrn-id` = '%s' ", dbesc($contact['issued-id']));
297 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($contact['dfrn-id']));
299 $x = q("SELECT `contact`.*, `contact`.`uid` AS `importer_uid`,
300 `contact`.`pubkey` AS `cpubkey`,
301 `contact`.`prvkey` AS `cprvkey`,
302 `contact`.`thumb` AS `thumb`,
303 `contact`.`url` as `url`,
304 `contact`.`name` as `senderName`,
307 LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
308 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
309 AND `contact`.`network` = '%s' AND `user`.`nickname` = '%s'
311 AND `user`.`account_expired` = 0 LIMIT 1",
317 if($owner['page-flags'] == PAGE_COMMUNITY && ! $x[0]['writable']) {
318 q("update contact set writable = 1 where id = %d limit 1",
321 $x[0]['writable'] = 1;
324 $ssl_policy = get_config('system','ssl_policy');
325 fix_contact_ssl_policy($x[0],$ssl_policy);
327 // If we are setup as a soapbox we aren't accepting input from this person
329 if($x[0]['page-flags'] == PAGE_SOAPBOX)
332 require_once('library/simplepie/simplepie.inc');
333 logger('mod-delivery: local delivery');
334 local_delivery($x[0],$atom);
339 $deliver_status = dfrn_deliver($owner,$contact,$atom);
341 logger('notifier: dfrn_delivery returns ' . $deliver_status);
343 if($deliver_status == (-1)) {
344 logger('notifier: delivery failed: queuing message');
345 add_to_queue($contact['id'],NETWORK_DFRN,$atom);
349 case NETWORK_OSTATUS :
351 // Do not send to otatus if we are not configured to send to public networks
352 if($owner['prvnets'])
354 if(get_config('system','ostatus_disabled') || get_config('system','dfrn_only'))
357 // only send salmon if public - e.g. if it's ok to notify
358 // a public hub, it's ok to send a salmon
360 if(($public_message) && (! $expire)) {
363 foreach($items as $item) {
364 if(! $item['parent'])
367 // private emails may be in included in public conversations. Filter them.
368 if(($public_message) && $item['private'])
371 $item_contact = get_item_contact($item,$icontacts);
375 if(($top_level) && ($public_message) && ($item['author-link'] === $item['owner-link']) && (! $expire))
376 $slaps[] = atom_entry($item,'html',null,$owner,true);
379 logger('notifier: slapdelivery: ' . $contact['name']);
380 foreach($slaps as $slappy) {
381 if($contact['notify']) {
382 $deliver_status = slapper($owner,$contact['notify'],$slappy);
383 if($deliver_status == (-1)) {
384 // queue message for redelivery
385 add_to_queue($contact['id'],NETWORK_OSTATUS,$slappy);
396 if(get_config('system','dfrn_only'))
398 // WARNING: does not currently convert to RFC2047 header encodings, etc.
400 $addr = $contact['addr'];
404 if($cmd === 'wall-new' || $cmd === 'comment-new') {
407 if($cmd === 'wall-new')
410 $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
421 $local_user = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
424 if(! count($local_user))
428 $r1 = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
431 if($r1 && $r1[0]['reply_to'])
432 $reply_to = $r1[0]['reply_to'];
434 $subject = (($it['title']) ? email_header_encode($it['title'],'UTF-8') : t("\x28no subject\x29")) ;
436 // only expose our real email address to true friends
438 if(($contact['rel'] == CONTACT_IS_FRIEND) && (! $contact['blocked']))
439 $headers = 'From: ' . email_header_encode($local_user[0]['username'],'UTF-8') . ' <' . $local_user[0]['email'] . '>' . "\n";
441 $headers = 'From: ' . email_header_encode($local_user[0]['username'],'UTF-8') . ' <' . t('noreply') . '@' . $a->get_hostname() . '>' . "\n";
444 $headers .= 'Reply-to: ' . $reply_to . "\n";
446 // for testing purposes: Collect exported mails
447 // $file = tempnam("/tmp/friendica/", "mail-out-");
448 // file_put_contents($file, json_encode($it));
450 $headers .= 'Message-Id: <' . iri2msgid($it['uri']). '>' . "\n";
452 //logger("Mail: uri: ".$it['uri']." parent-uri ".$it['parent-uri'], LOGGER_DEBUG);
453 //logger("Mail: Data: ".print_r($it, true), LOGGER_DEBUG);
454 //logger("Mail: Data: ".print_r($it, true), LOGGER_DATA);
456 if($it['uri'] !== $it['parent-uri']) {
457 $headers .= 'References: <' . iri2msgid($it['parent-uri']) . '>' . "\n";
458 if(!strlen($it['title'])) {
459 $r = q("SELECT `title` FROM `item` WHERE `parent-uri` = '%s' LIMIT 1",
460 dbesc($it['parent-uri']));
462 if(count($r) AND ($r[0]['title'] != ''))
463 $subject = $r[0]['title'];
465 if(strncasecmp($subject,'RE:',3))
466 $subject = 'Re: '.$subject;
468 email_send($addr, $subject, $headers, $it);
472 case NETWORK_DIASPORA :
474 $loc = 'public batch ' . $contact['batch'];
476 $loc = $contact['name'];
478 logger('delivery: diaspora batch deliver: ' . $loc);
480 if(get_config('system','dfrn_only') || (! get_config('system','diaspora_enabled')) || (! $normal_mode))
483 if((! $contact['pubkey']) && (! $public_message))
486 if($target_item['verb'] === ACTIVITY_DISLIKE) {
490 elseif(($target_item['deleted']) && ($target_item['verb'] !== ACTIVITY_LIKE)) {
491 logger('delivery: diaspora retract: ' . $loc);
493 diaspora_send_retraction($target_item,$owner,$contact,$public_message);
496 elseif($target_item['parent'] != $target_item['id']) {
498 logger('delivery: diaspora relay: ' . $loc);
500 // we are the relay - send comments, likes and unlikes to our conversants
501 diaspora_send_relay($target_item,$owner,$contact,$public_message);
504 elseif(($top_level) && (! $walltowall)) {
505 // currently no workable solution for sending walltowall
506 logger('delivery: diaspora status: ' . $loc);
507 diaspora_send_status($target_item,$owner,$contact,$public_message);
511 logger('delivery: diaspora unknown mode: ' . $contact['name']);
516 case NETWORK_FACEBOOK :
517 if(get_config('system','dfrn_only'))
526 if (array_search(__file__,get_included_files())===0){
527 delivery_run($argv,$argc);