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