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