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'];
103 $items = q("SELECT `item`.*, `sign`.`signed_text`,`sign`.`signature`,`sign`.`signer`
104 FROM `item` LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id` WHERE `parent` = %d ORDER BY `id` ASC",
108 if(! count($items)) {
113 $contacts_arr = array();
114 foreach($items as $item)
115 if(! in_array($item['contact-id'],$contacts_arr))
116 $contacts_arr[] = intval($item['contact-id']);
117 if(count($contacts_arr)) {
118 $str_contacts = implode(',',$contacts_arr);
119 $icontacts = q("SELECT * FROM `contact`
120 WHERE `id` IN ( $str_contacts ) "
123 if( ! ($icontacts && count($icontacts)))
127 // avoid race condition with deleting entries
129 if($items[0]['deleted']) {
130 foreach($items as $item)
131 $item['deleted'] = 1;
134 if((count($items) == 1) && ($items[0]['uri'] === $items[0]['parent-uri'])) {
135 logger('delivery: top level post');
140 $r = q("SELECT `contact`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`,
141 `user`.`timezone`, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`,
142 `user`.`page-flags`, `user`.`prvnets`
143 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
144 WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
153 $walltowall = ((($top_level) && ($owner['id'] != $items[0]['contact-id'])) ? true : false);
155 $public_message = true;
157 // fill this in with a single salmon slap if applicable
161 require_once('include/group.php');
165 // This is IMPORTANT!!!!
167 // We will only send a "notify owner to relay" or followup message if the referenced post
168 // originated on our system by virtue of having our hostname somewhere
169 // in the URI, AND it was a comment (not top_level) AND the parent originated elsewhere.
170 // if $parent['wall'] == 1 we will already have the parent message in our array
171 // and we will relay the whole lot.
173 // expire sends an entire group of expire messages and cannot be forwarded.
174 // However the conversation owner will be a part of the conversation and will
175 // be notified during this run.
176 // Other DFRN conversation members will be alerted during polled updates.
178 // Diaspora members currently are not notified of expirations, and other networks have
179 // either limited or no ability to process deletions. We should at least fix Diaspora
180 // by stringing togther an array of retractions and sending them onward.
183 $localhost = $a->get_hostname();
184 if(strpos($localhost,':'))
185 $localhost = substr($localhost,0,strpos($localhost,':'));
189 * Be VERY CAREFUL if you make any changes to the following line. Seemingly innocuous changes
190 * have been known to cause runaway conditions which affected several servers, along with
191 * permissions issues.
195 if((! $top_level) && ($parent['wall'] == 0) && (! $expire) && (stristr($target_item['uri'],$localhost))) {
196 logger('relay denied for delivery agent.');
198 /* no relay allowed for direct contact delivery */
202 if((strlen($parent['allow_cid']))
203 || (strlen($parent['allow_gid']))
204 || (strlen($parent['deny_cid']))
205 || (strlen($parent['deny_gid']))) {
206 $public_message = false; // private recipients, not public
209 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `blocked` = 0 AND `pending` = 0",
216 $hubxml = feed_hublinks();
218 logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
220 require_once('include/salmon.php');
227 switch($contact['network']) {
230 logger('notifier: dfrndelivery: ' . $contact['name']);
232 $feed_template = get_markup_template('atom_feed.tpl');
233 $mail_template = get_markup_template('atom_mail.tpl');
238 $birthday = feed_birthday($owner['uid'],$owner['timezone']);
240 if(strlen($birthday))
241 $birthday = '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>';
243 $atom .= replace_macros($feed_template, array(
244 '$version' => xmlify(FRIENDIKA_VERSION),
245 '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
246 '$feed_title' => xmlify($owner['name']),
247 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
249 '$salmon' => '', // private feed, we don't use salmon here
250 '$name' => xmlify($owner['name']),
251 '$profile_page' => xmlify($owner['url']),
252 '$photo' => xmlify($owner['photo']),
253 '$thumb' => xmlify($owner['thumb']),
254 '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
255 '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
256 '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME)) ,
257 '$birthday' => $birthday
260 foreach($items as $item) {
261 if(! $item['parent'])
264 // private emails may be in included in public conversations. Filter them.
265 if(($public_message) && $item['private'])
268 $item_contact = get_item_contact($item,$icontacts);
272 $atom .= atom_entry($item,'text',$item_contact,$owner,true);
276 $atom .= '</feed>' . "\r\n";
278 logger('notifier: ' . $atom, LOGGER_DATA);
280 $deliver_status = dfrn_deliver($owner,$contact,$atom);
282 logger('notifier: dfrn_delivery returns ' . $deliver_status);
284 if($deliver_status == (-1)) {
285 logger('notifier: delivery failed: queuing message');
286 // queue message for redelivery
287 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
288 VALUES ( %d, '%s', '%s', '%s') ",
289 intval($contact['id']),
290 dbesc(datetime_convert()),
291 dbesc(datetime_convert()),
297 case NETWORK_OSTATUS :
299 // Do not send to otatus if we are not configured to send to public networks
300 if($owner['prvnets'])
302 if(get_config('system','ostatus_disabled') || get_config('system','dfrn_only'))
305 // only send salmon if public - e.g. if it's ok to notify
306 // a public hub, it's ok to send a salmon
308 if(($public_message) && (! $expire)) {
311 foreach($items as $item) {
312 if(! $item['parent'])
315 // private emails may be in included in public conversations. Filter them.
316 if(($public_message) && $item['private'])
319 $item_contact = get_item_contact($item,$icontacts);
323 if(($top_level) && ($public_message) && ($item['author-link'] === $item['owner-link']) && (! $expire))
324 $slaps[] = atom_entry($item,'html',$item_contact,$owner,true);
327 logger('notifier: slapdelivery: ' . $contact['name']);
328 foreach($slaps as $slappy) {
329 if($contact['notify']) {
330 $deliver_status = slapper($owner,$contact['notify'],$slappy);
331 if($deliver_status == (-1)) {
332 // queue message for redelivery
333 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
334 VALUES ( %d, '%s', '%s', '%s') ",
335 intval($contact['id']),
336 dbesc(datetime_convert()),
337 dbesc(datetime_convert()),
349 if(get_config('system','dfrn_only'))
351 // WARNING: does not currently convert to RFC2047 header encodings, etc.
353 $addr = $contact['addr'];
357 if($cmd === 'wall-new' || $cmd === 'comment-new') {
360 if($cmd === 'wall-new')
363 $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
374 $local_user = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
377 if(! count($local_user))
381 $r1 = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
384 if($r1 && $r1[0]['reply_to'])
385 $reply_to = $r1[0]['reply_to'];
387 $subject = (($it['title']) ? $it['title'] : t("\x28no subject\x29")) ;
388 $headers = 'From: ' . $local_user[0]['username'] . ' <' . $local_user[0]['email'] . '>' . "\n";
390 $headers .= 'Reply-to: ' . $reply_to . "\n";
391 $headers .= 'Message-id: <' . $it['uri'] . '>' . "\n";
392 if($it['uri'] !== $it['parent-uri']) {
393 $header .= 'References: <' . $it['parent-uri'] . '>' . "\n";
394 if(! strlen($it['title'])) {
395 $r = q("SELECT `title` FROM `item` WHERE `parent-uri` = '%s' LIMIT 1",
396 dbesc($it['parent-uri'])
399 $subtitle = $r[0]['title'];
401 if(strncasecmp($subtitle,'RE:',3))
402 $subject = $subtitle;
404 $subject = 'Re: ' . $subtitle;
409 $headers .= 'MIME-Version: 1.0' . "\n";
410 $headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
411 $headers .= 'Content-Transfer-Encoding: 8bit' . "\n\n";
412 $html = prepare_body($it);
413 $message = '<html><body>' . $html . '</body></html>';
414 logger('notifier: email delivery to ' . $addr);
415 mail($addr, $subject, $message, $headers);
419 case NETWORK_DIASPORA :
421 $loc = 'public batch ' . $contact['batch'];
423 $loc = $contact['name'];
425 logger('delivery: diaspora batch deliver: ' . $loc);
427 if(get_config('system','dfrn_only') || (! get_config('system','diaspora_enabled')) || (! $normal_mode))
430 if((! $contact['pubkey']) && (! $public_message))
433 if($target_item['verb'] === ACTIVITY_DISLIKE) {
437 elseif(($target_item['deleted']) && ($target_item['verb'] !== ACTIVITY_LIKE)) {
438 logger('delivery: diaspora retract: ' . $loc);
440 diaspora_send_retraction($target_item,$owner,$contact,$public_message);
443 elseif($target_item['parent'] != $target_item['id']) {
445 logger('delivery: diaspora relay: ' . $loc);
447 // we are the relay - send comments, likes and unlikes to our conversants
448 diaspora_send_relay($target_item,$owner,$contact,$public_message);
451 elseif(($top_level) && (! $walltowall)) {
452 // currently no workable solution for sending walltowall
453 logger('delivery: diaspora status: ' . $loc);
454 diaspora_send_status($target_item,$owner,$contact,$public_message);
458 logger('delivery: diaspora unknown mode: ' . $contact['name']);
463 case NETWORK_FACEBOOK :
464 if(get_config('system','dfrn_only'))
473 if (array_search(__file__,get_included_files())===0){
474 delivery_run($argv,$argc);