2 require_once("boot.php");
3 require_once('include/queue_fn.php');
4 require_once('include/html2plain.php');
5 require_once("include/Scrape.php");
6 require_once('include/diaspora.php');
7 require_once("include/ostatus.php");
8 require_once("include/dfrn.php");
10 function delivery_run(&$argv, &$argc){
18 @include(".htconfig.php");
19 require_once("include/dba.php");
20 $db = new dba($db_host, $db_user, $db_pass, $db_data);
21 unset($db_host, $db_user, $db_pass, $db_data);
24 require_once("include/session.php");
25 require_once("include/datetime.php");
26 require_once('include/items.php');
27 require_once('include/bbcode.php');
28 require_once('include/email.php');
30 load_config('config');
31 load_config('system');
38 $a->set_baseurl(get_config('system','url'));
40 logger('delivery: invoked: '. print_r($argv,true), LOGGER_DEBUG);
43 $item_id = intval($argv[2]);
45 for($x = 3; $x < $argc; $x ++) {
47 $contact_id = intval($argv[$x]);
49 // Some other process may have delivered this item already.
51 $r = q("SELECT * FROM `deliverq` WHERE `cmd` = '%s' AND `item` = %d AND `contact` = %d LIMIT 1",
60 if (App::maxload_reached())
63 // It's ours to deliver. Remove it from the queue.
65 q("DELETE FROM `deliverq` WHERE `cmd` = '%s' AND `item` = %d AND `contact` = %d",
71 if (!$item_id || !$contact_id)
79 $recipients = array();
80 $url_recipients = array();
85 $recipients[] = $contact_id;
87 if ($cmd === 'mail') {
90 $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
93 if (!count($message)){
96 $uid = $message[0]['uid'];
97 $recipients[] = $message[0]['contact-id'];
100 elseif ($cmd === 'expire') {
101 $normal_mode = false;
103 $items = q("SELECT * FROM `item` WHERE `uid` = %d AND `wall` = 1
104 AND `deleted` = 1 AND `changed` > UTC_TIMESTAMP() - INTERVAL 30 MINUTE",
112 elseif ($cmd === 'suggest') {
113 $normal_mode = false;
116 $suggest = q("SELECT * FROM `fsuggest` WHERE `id` = %d LIMIT 1",
119 if (!count($suggest))
121 $uid = $suggest[0]['uid'];
122 $recipients[] = $suggest[0]['cid'];
124 } elseif ($cmd === 'relocate') {
125 $normal_mode = false;
130 $r = q("SELECT * FROM `item` WHERE `id` = %d and visible = 1 and moderated = 0 LIMIT 1",
134 if ((!count($r)) || (!intval($r[0]['parent']))) {
138 $target_item = $r[0];
139 $parent_id = intval($r[0]['parent']);
141 $updated = $r[0]['edited'];
143 $items = q("SELECT `item`.*, `sign`.`signed_text`,`sign`.`signature`,`sign`.`signer`
144 FROM `item` LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id` WHERE `parent` = %d and visible = 1 and moderated = 0 ORDER BY `id` ASC",
148 if (!count($items)) {
153 $contacts_arr = array();
154 foreach($items as $item)
155 if (!in_array($item['contact-id'],$contacts_arr))
156 $contacts_arr[] = intval($item['contact-id']);
157 if (count($contacts_arr)) {
158 $str_contacts = implode(',',$contacts_arr);
159 $icontacts = q("SELECT * FROM `contact`
160 WHERE `id` IN ( $str_contacts ) "
163 if ( !($icontacts && count($icontacts)))
166 // avoid race condition with deleting entries
168 if ($items[0]['deleted']) {
169 foreach($items as $item)
170 $item['deleted'] = 1;
173 if ((count($items) == 1) && ($items[0]['uri'] === $items[0]['parent-uri'])) {
174 logger('delivery: top level post');
179 $r = q("SELECT `contact`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`,
180 `user`.`timezone`, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`,
181 `user`.`page-flags`, `user`.`prvnets`
182 FROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
183 WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
192 $walltowall = ((($top_level) && ($owner['id'] != $items[0]['contact-id'])) ? true : false);
194 $public_message = true;
196 if (!($mail || $fsuggest || $relocate)) {
197 require_once('include/group.php');
201 // This is IMPORTANT!!!!
203 // We will only send a "notify owner to relay" or followup message if the referenced post
204 // originated on our system by virtue of having our hostname somewhere
205 // in the URI, AND it was a comment (not top_level) AND the parent originated elsewhere.
206 // if $parent['wall'] == 1 we will already have the parent message in our array
207 // and we will relay the whole lot.
209 // expire sends an entire group of expire messages and cannot be forwarded.
210 // However the conversation owner will be a part of the conversation and will
211 // be notified during this run.
212 // Other DFRN conversation members will be alerted during polled updates.
214 // Diaspora members currently are not notified of expirations, and other networks have
215 // either limited or no ability to process deletions. We should at least fix Diaspora
216 // by stringing togther an array of retractions and sending them onward.
219 $localhost = $a->get_hostname();
220 if (strpos($localhost,':'))
221 $localhost = substr($localhost,0,strpos($localhost,':'));
225 * Be VERY CAREFUL if you make any changes to the following line. Seemingly innocuous changes
226 * have been known to cause runaway conditions which affected several servers, along with
227 * permissions issues.
231 $relay_to_owner = false;
233 if (!$top_level && ($parent['wall'] == 0) && !$expire && stristr($target_item['uri'],$localhost)) {
234 $relay_to_owner = true;
237 if ($relay_to_owner) {
238 logger('followup '.$target_item["guid"], LOGGER_DEBUG);
239 // local followup to remote post
243 if ((strlen($parent['allow_cid']))
244 || (strlen($parent['allow_gid']))
245 || (strlen($parent['deny_cid']))
246 || (strlen($parent['deny_gid']))) {
247 $public_message = false; // private recipients, not public
252 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `blocked` = 0 AND `pending` = 0",
259 if ($contact['self'])
264 logger("main delivery by delivery: followup=$followup mail=$mail fsuggest=$fsuggest relocate=$relocate - network ".$contact['network']);
266 switch($contact['network']) {
269 logger('notifier: '.$target_item["guid"].' dfrndelivery: '.$contact['name']);
272 $item['body'] = fix_private_photos($item['body'],$owner['uid'],null,$message[0]['contact-id']);
273 $atom = dfrn::mail($item, $owner);
274 } elseif ($fsuggest) {
275 $atom = dfrn::fsuggest($item, $owner);
276 q("DELETE FROM `fsuggest` WHERE `id` = %d LIMIT 1", intval($item['id']));
278 $atom = dfrn::relocate($owner, $uid);
281 foreach($items as $item) { // there is only one item
282 if (!$item['parent'])
284 if ($item['id'] == $item_id) {
285 logger('followup: item: '. print_r($item,true), LOGGER_DATA);
289 $atom = dfrn::entries($msgitems,$owner);
292 foreach($items as $item) {
293 if (!$item['parent'])
296 // private emails may be in included in public conversations. Filter them.
297 if ($public_message && $item['private'])
300 $item_contact = get_item_contact($item,$icontacts);
305 if ($item_id == $item['id'] || $item['id'] == $item['parent']) {
306 $item["entry:comment-allow"] = true;
307 $item["entry:cid"] = (($top_level) ? $contact['id'] : 0);
311 $item["entry:comment-allow"] = true;
315 $atom = dfrn::entries($msgitems,$owner);
318 logger('notifier entry: '.$contact["url"].' '.$target_item["guid"].' entry: '.$atom, LOGGER_DEBUG);
320 logger('notifier: '.$atom, LOGGER_DATA);
321 $basepath = implode('/', array_slice(explode('/',$contact['url']),0,3));
323 // perform local delivery if we are on the same site
325 if (link_compare($basepath,$a->get_baseurl())) {
327 $nickname = basename($contact['url']);
328 if ($contact['issued-id'])
329 $sql_extra = sprintf(" AND `dfrn-id` = '%s' ", dbesc($contact['issued-id']));
331 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($contact['dfrn-id']));
333 $x = q("SELECT `contact`.*, `contact`.`uid` AS `importer_uid`,
334 `contact`.`pubkey` AS `cpubkey`,
335 `contact`.`prvkey` AS `cprvkey`,
336 `contact`.`thumb` AS `thumb`,
337 `contact`.`url` as `url`,
338 `contact`.`name` as `senderName`,
341 INNER JOIN `user` ON `contact`.`uid` = `user`.`uid`
342 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
343 AND `contact`.`network` = '%s' AND `user`.`nickname` = '%s'
345 AND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 LIMIT 1",
350 if ($x && count($x)) {
351 $write_flag = ((($x[0]['rel']) && ($x[0]['rel'] != CONTACT_IS_SHARING)) ? true : false);
352 if ((($owner['page-flags'] == PAGE_COMMUNITY) || $write_flag) && !$x[0]['writable']) {
353 q("UPDATE `contact` SET `writable` = 1 WHERE `id` = %d",
356 $x[0]['writable'] = 1;
359 $ssl_policy = get_config('system','ssl_policy');
360 fix_contact_ssl_policy($x[0],$ssl_policy);
362 // If we are setup as a soapbox we aren't accepting top level posts from this person
364 if (($x[0]['page-flags'] == PAGE_SOAPBOX) AND $top_level)
367 logger('mod-delivery: local delivery');
368 dfrn::import($atom, $x[0]);
373 if (!was_recently_delayed($contact['id']))
374 $deliver_status = dfrn::deliver($owner,$contact,$atom);
376 $deliver_status = (-1);
378 logger('notifier: dfrn_delivery to '.$contact["url"].' with guid '.$target_item["guid"].' returns '.$deliver_status);
380 if ($deliver_status == (-1)) {
381 logger('notifier: delivery failed: queuing message');
382 add_to_queue($contact['id'],NETWORK_DFRN,$atom);
386 case NETWORK_OSTATUS:
387 // Do not send to otatus if we are not configured to send to public networks
388 if ($owner['prvnets'])
390 if (get_config('system','ostatus_disabled') || get_config('system','dfrn_only'))
393 // There is currently no code here to distribute anything to OStatus.
394 // This is done in "notifier.php" (See "url_recipients" and "push_notify")
400 if (get_config('system','dfrn_only'))
402 // WARNING: does not currently convert to RFC2047 header encodings, etc.
404 $addr = $contact['addr'];
408 if ($cmd === 'wall-new' || $cmd === 'comment-new') {
411 if ($cmd === 'wall-new')
414 $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
425 $local_user = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
428 if (!count($local_user))
432 $r1 = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
435 if ($r1 && $r1[0]['reply_to'])
436 $reply_to = $r1[0]['reply_to'];
438 $subject = (($it['title']) ? email_header_encode($it['title'],'UTF-8') : t("\x28no subject\x29")) ;
440 // only expose our real email address to true friends
442 if (($contact['rel'] == CONTACT_IS_FRIEND) && !$contact['blocked']) {
444 $headers = 'From: '.email_header_encode($local_user[0]['username'],'UTF-8').' <'.$reply_to.'>'."\n";
445 $headers .= 'Sender: '.$local_user[0]['email']."\n";
447 $headers = 'From: '.email_header_encode($local_user[0]['username'],'UTF-8').' <'.$local_user[0]['email'].'>'."\n";
449 $headers = 'From: '. email_header_encode($local_user[0]['username'],'UTF-8') .' <'. t('noreply') .'@'.$a->get_hostname() .'>'. "\n";
452 // $headers .= 'Reply-to: '.$reply_to . "\n";
454 $headers .= 'Message-Id: <'. iri2msgid($it['uri']).'>'. "\n";
456 //logger("Mail: uri: ".$it['uri']." parent-uri ".$it['parent-uri'], LOGGER_DEBUG);
457 //logger("Mail: Data: ".print_r($it, true), LOGGER_DEBUG);
458 //logger("Mail: Data: ".print_r($it, true), LOGGER_DATA);
460 if ($it['uri'] !== $it['parent-uri']) {
461 $headers .= "References: <".iri2msgid($it["parent-uri"]).">";
463 // If Threading is enabled, write down the correct parent
464 if (($it["thr-parent"] != "") and ($it["thr-parent"] != $it["parent-uri"]))
465 $headers .= " <".iri2msgid($it["thr-parent"]).">";
469 $r = q("SELECT `title` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
470 dbesc($it['parent-uri']),
473 if (count($r) AND ($r[0]['title'] != ''))
474 $subject = $r[0]['title'];
476 $r = q("SELECT `title` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d LIMIT 1",
477 dbesc($it['parent-uri']),
480 if (count($r) AND ($r[0]['title'] != ''))
481 $subject = $r[0]['title'];
484 if (strncasecmp($subject,'RE:',3))
485 $subject = 'Re: '.$subject;
487 email_send($addr, $subject, $headers, $it);
491 case NETWORK_DIASPORA:
493 $loc = 'public batch '.$contact['batch'];
495 $loc = $contact['name'];
497 logger('delivery: diaspora batch deliver: '.$loc);
499 if (get_config('system','dfrn_only') || (!get_config('system','diaspora_enabled')))
503 diaspora::send_mail($item,$owner,$contact);
504 //diaspora_send_mail($item,$owner,$contact);
511 if (!$contact['pubkey'] && !$public_message)
514 $unsupported_activities = array(ACTIVITY_DISLIKE, ACTIVITY_ATTEND, ACTIVITY_ATTENDNO, ACTIVITY_ATTENDMAYBE);
516 //don't transmit activities which are not supported by diaspora
517 foreach($unsupported_activities as $act) {
518 if (activity_match($target_item['verb'],$act)) {
523 if (($target_item['deleted']) && (($target_item['uri'] === $target_item['parent-uri']) || $followup)) {
524 // top-level retraction
525 logger('delivery: diaspora retract: '.$loc);
527 diaspora::send_retraction($target_item,$owner,$contact,$public_message);
528 //diaspora_send_retraction($target_item,$owner,$contact,$public_message);
530 } elseif ($followup) {
531 // send comments and likes to owner to relay
532 diaspora::send_followup($target_item,$owner,$contact,$public_message);
533 //diaspora_send_followup($target_item,$owner,$contact,$public_message);
535 } elseif ($target_item['uri'] !== $target_item['parent-uri']) {
536 // we are the relay - send comments, likes and relayable_retractions to our conversants
537 logger('delivery: diaspora relay: '.$loc);
538 diaspora::send_relay($target_item,$owner,$contact,$public_message);
539 //diaspora_send_relay($target_item,$owner,$contact,$public_message);
541 } elseif ($top_level && !$walltowall) {
542 // currently no workable solution for sending walltowall
543 logger('delivery: diaspora status: '.$loc);
544 diaspora::send_status($target_item,$owner,$contact,$public_message);
545 //diaspora_send_status($target_item,$owner,$contact,$public_message);
549 logger('delivery: diaspora unknown mode: '.$contact['name']);
561 if (array_search(__file__,get_included_files())===0){
562 delivery_run($_SERVER["argv"],$_SERVER["argc"]);