]> git.mxchange.org Git - friendica.git/blob - src/Worker/Delivery.php
0d01d82848714023e76a3bb24cc153d37831907e
[friendica.git] / src / Worker / Delivery.php
1 <?php
2 /**
3  * @file src/Worker/Delivery.php
4  */
5 namespace Friendica\Worker;
6
7 use Friendica\BaseObject;
8 use Friendica\Core\Config;
9 use Friendica\Core\L10n;
10 use Friendica\Core\Logger;
11 use Friendica\Core\Protocol;
12 use Friendica\Core\System;
13 use Friendica\Database\DBA;
14 use Friendica\Model;
15 use Friendica\Protocol\DFRN;
16 use Friendica\Protocol\Diaspora;
17 use Friendica\Protocol\Email;
18 use Friendica\Protocol\Activity;
19 use Friendica\Protocol\ActivityPub;
20 use Friendica\Util\Strings;
21 use Friendica\Util\Network;
22 use Friendica\Core\Worker;
23
24 class Delivery extends BaseObject
25 {
26         const MAIL          = 'mail';
27         const SUGGESTION    = 'suggest';
28         const RELOCATION    = 'relocate';
29         const DELETION      = 'drop';
30         const POST          = 'wall-new';
31         const POKE          = 'poke';
32         const UPLINK        = 'uplink';
33         const REMOVAL       = 'removeme';
34         const PROFILEUPDATE = 'profileupdate';
35
36         public static function execute($cmd, $target_id, $contact_id)
37         {
38                 Logger::info('Invoked', ['cmd' => $cmd, 'target' => $target_id, 'contact' => $contact_id]);
39
40                 $top_level = false;
41                 $followup = false;
42                 $public_message = false;
43
44                 $items = [];
45                 if ($cmd == self::MAIL) {
46                         $target_item = DBA::selectFirst('mail', [], ['id' => $target_id]);
47                         if (!DBA::isResult($target_item)) {
48                                 self::setFailedQueue($cmd, $target_id);
49                                 return;
50                         }
51                         $uid = $target_item['uid'];
52                 } elseif ($cmd == self::SUGGESTION) {
53                         $target_item = DBA::selectFirst('fsuggest', [], ['id' => $target_id]);
54                         if (!DBA::isResult($target_item)) {
55                                 self::setFailedQueue($cmd, $target_id);
56                                 return;
57                         }
58                         $uid = $target_item['uid'];
59                 } elseif ($cmd == self::RELOCATION) {
60                         $uid = $target_id;
61                         $target_item = [];
62                 } else {
63                         $item = Model\Item::selectFirst(['parent'], ['id' => $target_id]);
64                         if (!DBA::isResult($item) || empty($item['parent'])) {
65                                 self::setFailedQueue($cmd, $target_id);
66                                 return;
67                         }
68                         $parent_id = intval($item['parent']);
69
70                         $condition = ['id' => [$target_id, $parent_id], 'visible' => true, 'moderated' => false];
71                         $params = ['order' => ['id']];
72                         $itemdata = Model\Item::select([], $condition, $params);
73
74                         while ($item = Model\Item::fetch($itemdata)) {
75                                 if ($item['id'] == $parent_id) {
76                                         $parent = $item;
77                                 }
78                                 if ($item['id'] == $target_id) {
79                                         $target_item = $item;
80                                 }
81                                 $items[] = $item;
82                         }
83                         DBA::close($itemdata);
84
85                         if (empty($target_item)) {
86                                 Logger::log('Item ' . $target_id . "wasn't found. Quitting here.");
87                                 self::setFailedQueue($cmd, $target_id);
88                                 return;
89                         }
90
91                         if (empty($parent)) {
92                                 Logger::log('Parent ' . $parent_id . ' for item ' . $target_id . "wasn't found. Quitting here.");
93                                 self::setFailedQueue($cmd, $target_id);
94                                 return;
95                         }
96
97                         if (!empty($target_item['contact-uid'])) {
98                                 $uid = $target_item['contact-uid'];
99                         } elseif (!empty($target_item['uid'])) {
100                                 $uid = $target_item['uid'];
101                         } else {
102                                 Logger::log('Only public users for item ' . $target_id, Logger::DEBUG);
103                                 self::setFailedQueue($cmd, $target_id);
104                                 return;
105                         }
106
107                         $condition = ['uri' => $target_item['thr-parent'], 'uid' => $target_item['uid']];
108                         $thr_parent = Model\Item::selectFirst(['network', 'object'], $condition);
109                         if (!DBA::isResult($thr_parent)) {
110                                 // Shouldn't happen. But when this does, we just take the parent as thread parent.
111                                 // That's totally okay for what we use this variable here.
112                                 $thr_parent = $parent;
113                         }
114
115                         if (!empty($contact_id) && Model\Contact::isArchived($contact_id)) {
116                                 Logger::info('Contact is archived', ['id' => $contact_id, 'cmd' => $cmd, 'item' => $target_item['id']]);
117                                 self::setFailedQueue($cmd, $target_id);
118                                 return;
119                         }
120
121                         // avoid race condition with deleting entries
122                         if ($items[0]['deleted']) {
123                                 foreach ($items as $item) {
124                                         $item['deleted'] = 1;
125                                 }
126                         }
127
128                         // When commenting too fast after delivery, a post wasn't recognized as top level post.
129                         // The count then showed more than one entry. The additional check should help.
130                         // The check for the "count" should be superfluous, but I'm not totally sure by now, so we keep it.
131                         if ((($parent['id'] == $target_id) || (count($items) == 1)) && ($parent['uri'] === $parent['parent-uri'])) {
132                                 Logger::log('Top level post');
133                                 $top_level = true;
134                         }
135
136                         // This is IMPORTANT!!!!
137
138                         // We will only send a "notify owner to relay" or followup message if the referenced post
139                         // originated on our system by virtue of having our hostname somewhere
140                         // in the URI, AND it was a comment (not top_level) AND the parent originated elsewhere.
141                         // if $parent['wall'] == 1 we will already have the parent message in our array
142                         // and we will relay the whole lot.
143
144                         $localhost = self::getApp()->getHostName();
145                         if (strpos($localhost, ':')) {
146                                 $localhost = substr($localhost, 0, strpos($localhost, ':'));
147                         }
148                         /**
149                          *
150                          * Be VERY CAREFUL if you make any changes to the following line. Seemingly innocuous changes
151                          * have been known to cause runaway conditions which affected several servers, along with
152                          * permissions issues.
153                          *
154                          */
155
156                         if (!$top_level && ($parent['wall'] == 0) && stristr($target_item['uri'], $localhost)) {
157                                 Logger::log('Followup ' . $target_item["guid"], Logger::DEBUG);
158                                 // local followup to remote post
159                                 $followup = true;
160                         }
161
162                         if (empty($parent['allow_cid'])
163                                 && empty($parent['allow_gid'])
164                                 && empty($parent['deny_cid'])
165                                 && empty($parent['deny_gid'])
166                                 && !$parent["private"]) {
167                                 $public_message = true;
168                         }
169                 }
170
171                 if (empty($items)) {
172                         Logger::log('No delivery data for  ' . $cmd . ' - Item ID: ' .$target_id . ' - Contact ID: ' . $contact_id);
173                 }
174
175                 $owner = Model\User::getOwnerDataById($uid);
176                 if (!DBA::isResult($owner)) {
177                         self::setFailedQueue($cmd, $target_id);
178                         return;
179                 }
180
181                 // We don't deliver our items to blocked or pending contacts, and not to ourselves either
182                 $contact = DBA::selectFirst('contact', [],
183                         ['id' => $contact_id, 'blocked' => false, 'pending' => false, 'self' => false]
184                 );
185                 if (!DBA::isResult($contact)) {
186                         self::setFailedQueue($cmd, $target_id);
187                         return;
188                 }
189
190                 if (Network::isUrlBlocked($contact['url'])) {
191                         self::setFailedQueue($cmd, $target_id);
192                         return;
193                 }
194
195                 // Transmit via Diaspora if the thread had started as Diaspora post.
196                 // Also transmit via Diaspora if this is a direct answer to a Diaspora comment.
197                 // This is done since the uri wouldn't match (Diaspora doesn't transmit it)
198                 if (!empty($parent) && !empty($thr_parent) && in_array(Protocol::DIASPORA, [$parent['network'], $thr_parent['network']])) {
199                         $contact['network'] = Protocol::DIASPORA;
200                 }
201
202                 Logger::notice('Delivering', ['cmd' => $cmd, 'target' => $target_id, 'followup' => $followup, 'network' => $contact['network']]);
203
204                 switch ($contact['network']) {
205                         case Protocol::DFRN:
206                                 self::deliverDFRN($cmd, $contact, $owner, $items, $target_item, $public_message, $top_level, $followup);
207                                 break;
208
209                         case Protocol::DIASPORA:
210                                 self::deliverDiaspora($cmd, $contact, $owner, $items, $target_item, $public_message, $top_level, $followup);
211                                 break;
212
213                         case Protocol::MAIL:
214                                 self::deliverMail($cmd, $contact, $owner, $target_item, $thr_parent);
215                                 break;
216
217                         default:
218                                 break;
219                 }
220
221                 return;
222         }
223
224         /**
225          * Increased the "failed" counter in the item delivery data
226          *
227          * @param string  $cmd Command
228          * @param integer $id  Item id
229          */
230         private static function setFailedQueue(string $cmd, int $id)
231         {
232                 if (!in_array($cmd, [Delivery::POST, Delivery::POKE])) {
233                         return;
234                 }
235
236                 Model\ItemDeliveryData::incrementQueueFailed($id);
237         }
238
239         /**
240          * @brief Deliver content via DFRN
241          *
242          * @param string  $cmd            Command
243          * @param array   $contact        Contact record of the receiver
244          * @param array   $owner          Owner record of the sender
245          * @param array   $items          Item record of the content and the parent
246          * @param array   $target_item    Item record of the content
247          * @param boolean $public_message Is the content public?
248          * @param boolean $top_level      Is it a thread starter?
249          * @param boolean $followup       Is it an answer to a remote post?
250          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
251          * @throws \ImagickException
252          */
253         private static function deliverDFRN($cmd, $contact, $owner, $items, $target_item, $public_message, $top_level, $followup)
254         {
255                 // Skip transmitting the announce via DFRN when it had already been done via AP
256                 if (ActivityPub\Transmitter::isAnnounce($target_item) && !empty(Model\APContact::getByURL($contact['url'], false))) {
257                         Logger::info('Announce had already been transmitted', ['url' => $contact['url'], 'guid' => ($target_item['guid'] ?? '') ?: $target_item['id']]);
258                         Model\ItemDeliveryData::incrementQueueDone($target_item['id']);
259                         return;
260                 }
261
262                 // Transmit Diaspora reshares via Diaspora if the Friendica contact support Diaspora
263                 if (Diaspora::isReshare($target_item['body']) && !empty(Diaspora::personByHandle(contact['addr'], false))) {
264                         Logger::info('Reshare will be transmitted via Diaspora', ['url' => $contact['url'], 'guid' => ($target_item['guid'] ?? '') ?: $target_item['id']]);
265                         self::deliverDiaspora($cmd, $contact, $owner, $items, $target_item, $public_message, $top_level, $followup);
266                         return;
267                 }
268
269                 Logger::info('Deliver ' . (($target_item['guid'] ?? '') ?: $target_item['id']) . ' via DFRN to ' . (($contact['addr'] ?? '') ?: $contact['url']));
270
271                 if ($cmd == self::MAIL) {
272                         $item = $target_item;
273                         $item['body'] = Model\Item::fixPrivatePhotos($item['body'], $owner['uid'], null, $item['contact-id']);
274                         $atom = DFRN::mail($item, $owner);
275                 } elseif ($cmd == self::SUGGESTION) {
276                         $item = $target_item;
277                         $atom = DFRN::fsuggest($item, $owner);
278                         DBA::delete('fsuggest', ['id' => $item['id']]);
279                 } elseif ($cmd == self::RELOCATION) {
280                         $atom = DFRN::relocate($owner, $owner['uid']);
281                 } elseif ($followup) {
282                         $msgitems = [$target_item];
283                         $atom = DFRN::entries($msgitems, $owner);
284                 } else {
285                         $msgitems = [];
286                         foreach ($items as $item) {
287                                 // Only add the parent when we don't delete other items.
288                                 if (($target_item['id'] == $item['id']) || ($cmd != self::DELETION)) {
289                                         $item["entry:comment-allow"] = true;
290                                         $item["entry:cid"] = ($top_level ? $contact['id'] : 0);
291                                         $msgitems[] = $item;
292                                 }
293                         }
294                         $atom = DFRN::entries($msgitems, $owner);
295                 }
296
297                 Logger::debug('Notifier entry: ' . $contact["url"] . ' ' . (($target_item['guid'] ?? '') ?: $target_item['id']) . ' entry: ' . $atom);
298
299                 $basepath =  implode('/', array_slice(explode('/', $contact['url']), 0, 3));
300
301                 // perform local delivery if we are on the same site
302
303                 if (Strings::compareLink($basepath, System::baseUrl())) {
304                         $condition = ['nurl' => Strings::normaliseLink($contact['url']), 'self' => true];
305                         $target_self = DBA::selectFirst('contact', ['uid'], $condition);
306                         if (!DBA::isResult($target_self)) {
307                                 return;
308                         }
309                         $target_uid = $target_self['uid'];
310
311                         // Check if the user has got this contact
312                         $cid = Model\Contact::getIdForURL($owner['url'], $target_uid);
313                         if (!$cid) {
314                                 // Otherwise there should be a public contact
315                                 $cid = Model\Contact::getIdForURL($owner['url']);
316                                 if (!$cid) {
317                                         return;
318                                 }
319                         }
320
321                         $target_importer = DFRN::getImporter($cid, $target_uid);
322                         if (empty($target_importer)) {
323                                 // This should never happen
324                                 return;
325                         }
326
327                         DFRN::import($atom, $target_importer);
328
329                         if (in_array($cmd, [Delivery::POST, Delivery::POKE])) {
330                                 Model\ItemDeliveryData::incrementQueueDone($target_item['id'], Model\ItemDeliveryData::DFRN);
331                         }
332
333                         return;
334                 }
335
336                 $protocol = Model\ItemDeliveryData::DFRN;
337
338                 // We don't have a relationship with contacts on a public post.
339                 // Se we transmit with the new method and via Diaspora as a fallback
340                 if (!empty($items) && (($items[0]['uid'] == 0) || ($contact['uid'] == 0))) {
341                         // Transmit in public if it's a relay post
342                         $public_dfrn = ($contact['contact-type'] == Model\Contact::TYPE_RELAY);
343
344                         $deliver_status = DFRN::transmit($owner, $contact, $atom, $public_dfrn);
345
346                         // We never spool failed relay deliveries
347                         if ($public_dfrn) {
348                                 Logger::info('Relay delivery to ' . $contact["url"] . ' with guid ' . $target_item["guid"] . ' returns ' . $deliver_status);
349
350                                 if (in_array($cmd, [Delivery::POST, Delivery::POKE])) {
351                                         if (($deliver_status >= 200) && ($deliver_status <= 299)) {
352                                                 Model\ItemDeliveryData::incrementQueueDone($target_item['id'], $protocol);
353                                         } else {
354                                                 Model\ItemDeliveryData::incrementQueueFailed($target_item['id']);
355                                         }
356                                 }
357                                 return;
358                         }
359
360                         if (($deliver_status < 200) || ($deliver_status > 299)) {
361                                 // Transmit via Diaspora if not possible via Friendica
362                                 self::deliverDiaspora($cmd, $contact, $owner, $items, $target_item, $public_message, $top_level, $followup);
363                                 return;
364                         }
365                 } elseif ($cmd != self::RELOCATION) {
366                         // DFRN payload over Diaspora transport layer
367                         $deliver_status = DFRN::transmit($owner, $contact, $atom);
368                         if ($deliver_status < 200) {
369                                 // Legacy DFRN
370                                 $deliver_status = DFRN::deliver($owner, $contact, $atom);
371                                 $protocol = Model\ItemDeliveryData::LEGACY_DFRN;
372                         }
373                 } else {
374                         $deliver_status = DFRN::deliver($owner, $contact, $atom);
375                         $protocol = Model\ItemDeliveryData::LEGACY_DFRN;
376                 }
377
378                 Logger::info('DFRN Delivery', ['cmd' => $cmd, 'url' => $contact['url'], 'guid' => ($target_item['guid'] ?? '') ?: $target_item['id'], 'return' => $deliver_status]);
379
380                 if (($deliver_status >= 200) && ($deliver_status <= 299)) {
381                         // We successfully delivered a message, the contact is alive
382                         Model\Contact::unmarkForArchival($contact);
383
384                         if (in_array($cmd, [Delivery::POST, Delivery::POKE])) {
385                                 Model\ItemDeliveryData::incrementQueueDone($target_item['id'], $protocol);
386                         }
387                 } else {
388                         // The message could not be delivered. We mark the contact as "dead"
389                         Model\Contact::markForArchival($contact);
390
391                         Logger::info('Delivery failed: defer message', ['id' => ($target_item['guid'] ?? '') ?: $target_item['id']]);
392                         if (!Worker::defer() && in_array($cmd, [Delivery::POST, Delivery::POKE])) {
393                                 Model\ItemDeliveryData::incrementQueueFailed($target_item['id']);
394                         }
395                 }
396         }
397
398         /**
399          * @brief Deliver content via Diaspora
400          *
401          * @param string  $cmd            Command
402          * @param array   $contact        Contact record of the receiver
403          * @param array   $owner          Owner record of the sender
404          * @param array   $items          Item record of the content and the parent
405          * @param array   $target_item    Item record of the content
406          * @param boolean $public_message Is the content public?
407          * @param boolean $top_level      Is it a thread starter?
408          * @param boolean $followup       Is it an answer to a remote post?
409          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
410          * @throws \ImagickException
411          */
412         private static function deliverDiaspora($cmd, $contact, $owner, $items, $target_item, $public_message, $top_level, $followup)
413         {
414                 // We don't treat Forum posts as "wall-to-wall" to be able to post them via Diaspora
415                 $walltowall = $top_level && ($owner['id'] != $items[0]['contact-id']) & ($owner['account-type'] != Model\User::ACCOUNT_TYPE_COMMUNITY);
416
417                 if ($public_message) {
418                         $loc = 'public batch ' . $contact['batch'];
419                 } else {
420                         $loc = $contact['addr'];
421                 }
422
423                 Logger::notice('Deliver via Diaspora', ['target' => $target_item['id'], 'guid' => $target_item['guid'], 'to' => $loc]);
424
425                 if (Config::get('system', 'dfrn_only') || !Config::get('system', 'diaspora_enabled')) {
426                         return;
427                 }
428
429                 if ($cmd == self::MAIL) {
430                         Diaspora::sendMail($target_item, $owner, $contact);
431                         return;
432                 }
433
434                 if ($cmd == self::SUGGESTION) {
435                         return;
436                 }
437
438                 if (!$contact['pubkey'] && !$public_message) {
439                         return;
440                 }
441
442                 if ($cmd == self::RELOCATION) {
443                         $deliver_status = Diaspora::sendAccountMigration($owner, $contact, $owner['uid']);
444                 } elseif ($target_item['deleted'] && (($target_item['uri'] === $target_item['parent-uri']) || $followup)) {
445                         // top-level retraction
446                         Logger::log('diaspora retract: ' . $loc);
447                         $deliver_status = Diaspora::sendRetraction($target_item, $owner, $contact, $public_message);
448                 } elseif ($followup) {
449                         // send comments and likes to owner to relay
450                         Logger::log('diaspora followup: ' . $loc);
451                         $deliver_status = Diaspora::sendFollowup($target_item, $owner, $contact, $public_message);
452                 } elseif ($target_item['uri'] !== $target_item['parent-uri']) {
453                         // we are the relay - send comments, likes and relayable_retractions to our conversants
454                         Logger::log('diaspora relay: ' . $loc);
455                         $deliver_status = Diaspora::sendRelay($target_item, $owner, $contact, $public_message);
456                 } elseif ($top_level && !$walltowall) {
457                         // currently no workable solution for sending walltowall
458                         Logger::log('diaspora status: ' . $loc);
459                         $deliver_status = Diaspora::sendStatus($target_item, $owner, $contact, $public_message);
460                 } else {
461                         Logger::log('Unknown mode ' . $cmd . ' for ' . $loc);
462                         return;
463                 }
464
465                 if (($deliver_status >= 200) && ($deliver_status <= 299)) {
466                         // We successfully delivered a message, the contact is alive
467                         Model\Contact::unmarkForArchival($contact);
468
469                         if (in_array($cmd, [Delivery::POST, Delivery::POKE])) {
470                                 Model\ItemDeliveryData::incrementQueueDone($target_item['id'], Model\ItemDeliveryData::DIASPORA);
471                         }
472                 } else {
473                         // The message could not be delivered. We mark the contact as "dead"
474                         Model\Contact::markForArchival($contact);
475
476                         // When it is delivered to the public endpoint, we do mark the relay contact for archival as well
477                         if ($public_message) {
478                                 Diaspora::markRelayForArchival($contact);
479                         }
480
481                         if (empty($contact['contact-type']) || ($contact['contact-type'] != Model\Contact::TYPE_RELAY)) {
482                                 Logger::info('Delivery failed: defer message', ['id' => ($target_item['guid'] ?? '') ?: $target_item['id']]);
483                                 // defer message for redelivery
484                                 if (!Worker::defer() && in_array($cmd, [Delivery::POST, Delivery::POKE])) {
485                                         Model\ItemDeliveryData::incrementQueueFailed($target_item['id']);
486                                 }
487                         } elseif (in_array($cmd, [Delivery::POST, Delivery::POKE])) {
488                                 Model\ItemDeliveryData::incrementQueueFailed($target_item['id']);
489                         }
490                 }
491         }
492
493         /**
494          * @brief Deliver content via mail
495          *
496          * @param string $cmd         Command
497          * @param array  $contact     Contact record of the receiver
498          * @param array  $owner       Owner record of the sender
499          * @param array  $target_item Item record of the content
500          * @param array  $thr_parent  Item record of the direct parent in the thread
501          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
502          * @throws \ImagickException
503          */
504         private static function deliverMail($cmd, $contact, $owner, $target_item, $thr_parent)
505         {
506                 if (Config::get('system','dfrn_only')) {
507                         return;
508                 }
509
510                 $addr = $contact['addr'];
511                 if (!strlen($addr)) {
512                         return;
513                 }
514
515                 if (!in_array($cmd, [self::POST, self::POKE])) {
516                         return;
517                 }
518
519                 if ($target_item['verb'] != Activity::POST) {
520                         return;
521                 }
522
523                 if (!empty($thr_parent['object'])) {
524                         $data = json_decode($thr_parent['object'], true);
525                         if (!empty($data['reply_to'])) {
526                                 $addr = $data['reply_to'][0]['mailbox'] . '@' . $data['reply_to'][0]['host'];
527                                 Logger::info('Use "reply-to" address of the thread parent', ['addr' => $addr]);
528                         } elseif (!empty($data['from'])) {
529                                 $addr = $data['from'][0]['mailbox'] . '@' . $data['from'][0]['host'];
530                                 Logger::info('Use "from" address of the thread parent', ['addr' => $addr]);
531                         }
532                 }
533
534                 $local_user = DBA::selectFirst('user', [], ['uid' => $owner['uid']]);
535                 if (!DBA::isResult($local_user)) {
536                         return;
537                 }
538
539                 Logger::info('About to deliver via mail', ['guid' => $target_item['guid'], 'to' => $addr]);
540
541                 $reply_to = '';
542                 $mailacct = DBA::selectFirst('mailacct', ['reply_to'], ['uid' => $owner['uid']]);
543                 if (DBA::isResult($mailacct) && !empty($mailacct['reply_to'])) {
544                         $reply_to = $mailacct['reply_to'];
545                 }
546
547                 $subject  = ($target_item['title'] ? Email::encodeHeader($target_item['title'], 'UTF-8') : L10n::t("\x28no subject\x29"));
548
549                 // only expose our real email address to true friends
550
551                 if (($contact['rel'] == Model\Contact::FRIEND) && !$contact['blocked']) {
552                         if ($reply_to) {
553                                 $headers  = 'From: ' . Email::encodeHeader($local_user['username'],'UTF-8') . ' <' . $reply_to . '>' . "\n";
554                                 $headers .= 'Sender: ' . $local_user['email'] . "\n";
555                         } else {
556                                 $headers  = 'From: ' . Email::encodeHeader($local_user['username'],'UTF-8') . ' <' . $local_user['email'] . '>' . "\n";
557                         }
558                 } else {
559                         $headers  = 'From: '. Email::encodeHeader($local_user['username'], 'UTF-8') . ' <noreply@' . self::getApp()->getHostName() . '>' . "\n";
560                 }
561
562                 $headers .= 'Message-Id: <' . Email::iri2msgid($target_item['uri']) . '>' . "\n";
563
564                 if ($target_item['uri'] !== $target_item['parent-uri']) {
565                         $headers .= 'References: <' . Email::iri2msgid($target_item['parent-uri']) . '>';
566
567                         // Export more references on deeper nested threads
568                         if (($target_item['thr-parent'] != '') && ($target_item['thr-parent'] != $target_item['parent-uri'])) {
569                                 $headers .= ' <' . Email::iri2msgid($target_item['thr-parent']) . '>';
570                         }
571
572                         $headers .= "\n";
573
574                         if (empty($target_item['title'])) {
575                                 $condition = ['uri' => $target_item['parent-uri'], 'uid' => $owner['uid']];
576                                 $title = Model\Item::selectFirst(['title'], $condition);
577
578                                 if (DBA::isResult($title) && ($title['title'] != '')) {
579                                         $subject = $title['title'];
580                                 } else {
581                                         $condition = ['parent-uri' => $target_item['parent-uri'], 'uid' => $owner['uid']];
582                                         $title = Model\Item::selectFirst(['title'], $condition);
583
584                                         if (DBA::isResult($title) && ($title['title'] != '')) {
585                                                 $subject = $title['title'];
586                                         }
587                                 }
588                         }
589
590                         if (strncasecmp($subject, 'RE:', 3)) {
591                                 $subject = 'Re: ' . $subject;
592                         }
593                 }
594
595                 Email::send($addr, $subject, $headers, $target_item);
596
597                 Model\ItemDeliveryData::incrementQueueDone($target_item['id'], Model\ItemDeliveryData::MAIL);
598
599                 Logger::info('Delivered via mail', ['guid' => $target_item['guid'], 'to' => $addr, 'subject' => $subject]);
600         }
601 }