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