3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Worker;
24 use Friendica\Core\Logger;
25 use Friendica\Core\Protocol;
26 use Friendica\Database\DBA;
28 use Friendica\Model\Contact;
29 use Friendica\Model\Item;
30 use Friendica\Model\User;
31 use Friendica\Protocol\Activity;
32 use Friendica\Protocol\ActivityPub;
33 use Friendica\Protocol\Email;
34 use Friendica\Protocol\Feed;
35 use Friendica\Protocol\PortableContact;
36 use Friendica\Util\DateTimeFormat;
37 use Friendica\Util\Strings;
38 use Friendica\Util\XML;
42 public static function execute($contact_id = 0, $command = '')
44 Logger::log('Start for contact ' . $contact_id);
48 if ($command == "force") {
53 Logger::log('no contact');
58 $contact = DBA::selectFirst('contact', [], ['id' => $contact_id]);
59 if (!DBA::isResult($contact)) {
60 Logger::log('Contact not found or cannot be used.');
64 if (($contact['network'] != Protocol::MAIL) || $force) {
65 Contact::updateFromProbe($contact_id, '', $force);
68 // Special treatment for wrongly detected local contacts
69 if (!$force && ($contact['network'] != Protocol::DFRN) && Contact::isLocalById($contact_id)) {
70 Contact::updateFromProbe($contact_id, Protocol::DFRN, true);
71 $contact = DBA::selectFirst('contact', [], ['id' => $contact_id]);
74 if (($contact['network'] == Protocol::DFRN) && !Contact::isLegacyDFRNContact($contact)) {
75 $protocol = Protocol::ACTIVITYPUB;
77 $protocol = $contact['network'];
80 $importer_uid = $contact['uid'];
82 $updated = DateTimeFormat::utcNow();
84 if ($importer_uid == 0) {
85 Logger::log('Ignore public contacts');
87 // set the last-update so we don't keep polling
88 DBA::update('contact', ['last-update' => $updated], ['id' => $contact['id']]);
92 // Possibly switch the remote contact to AP
93 if ($protocol === Protocol::OSTATUS) {
94 ActivityPub\Receiver::switchContact($contact['id'], $importer_uid, $contact['url']);
95 $contact = DBA::selectFirst('contact', [], ['id' => $contact_id]);
98 // load current friends if possible.
99 if (!empty($contact['poco']) && !$contact['failed']) {
100 if (!DBA::exists('glink', ["`cid` = ? AND updated > UTC_TIMESTAMP() - INTERVAL 1 DAY", $contact['id']])) {
101 PortableContact::loadWorker($contact['id'], $importer_uid, 0, $contact['poco']);
105 // Don't poll if polling is deactivated (But we poll feeds and mails anyway)
106 if (!in_array($protocol, [Protocol::FEED, Protocol::MAIL]) && DI::config()->get('system', 'disable_polling')) {
107 Logger::log('Polling is disabled');
109 // set the last-update so we don't keep polling
110 DBA::update('contact', ['last-update' => $updated], ['id' => $contact['id']]);
114 // We don't poll AP contacts by now
115 if ($protocol === Protocol::ACTIVITYPUB) {
116 Logger::log("Don't poll AP contact");
118 // set the last-update so we don't keep polling
119 DBA::update('contact', ['last-update' => $updated], ['id' => $contact['id']]);
123 $importer = User::getOwnerDataById($importer_uid);
125 if (empty($importer)) {
126 Logger::log('No self contact for user '.$importer_uid);
128 // set the last-update so we don't keep polling
129 DBA::update('contact', ['last-update' => $updated], ['id' => $contact['id']]);
136 if ($contact['subhub']) {
137 $poll_interval = DI::config()->get('system', 'pushpoll_frequency', 3);
138 $contact['priority'] = intval($poll_interval);
141 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($contact['last-update'] . " + 1 day")) {
148 Logger::log("poll: ({$protocol}-{$contact['id']}) IMPORTER: {$importer['name']}, CONTACT: {$contact['name']}");
152 if ($protocol === Protocol::DFRN) {
153 $xml = self::pollDFRN($contact, $updated);
154 } elseif (($protocol === Protocol::OSTATUS)
155 || ($protocol === Protocol::DIASPORA)
156 || ($protocol === Protocol::FEED)) {
157 $xml = self::pollFeed($contact, $protocol, $updated);
158 } elseif ($protocol === Protocol::MAIL) {
159 self::pollMail($contact, $importer_uid, $updated);
163 Logger::log('received xml : ' . $xml, Logger::DATA);
164 if (!strstr($xml, '<')) {
165 Logger::log('post_handshake: response from ' . $url . ' did not contain XML.');
167 $fields = ['failed' => true, 'last-update' => $updated, 'failure_update' => $updated];
168 self::updateContact($contact, $fields);
169 Contact::markForArchival($contact);
174 Logger::log("Consume feed of contact ".$contact['id']);
176 Feed::consume($xml, $importer, $contact, $hub);
178 // do it a second time for DFRN so that any children find their parents.
179 if ($protocol === Protocol::DFRN) {
180 Feed::consume($xml, $importer, $contact, $hub);
183 $hubmode = 'subscribe';
184 if ($protocol === Protocol::DFRN || $contact['blocked']) {
185 $hubmode = 'unsubscribe';
188 if (($protocol === Protocol::OSTATUS || $protocol == Protocol::FEED) && (! $contact['hub-verify'])) {
196 Logger::log("Contact ".$contact['id']." returned hub: ".$hub." Network: ".$protocol." Relation: ".$contact['rel']." Update: ".$hub_update);
198 if (strlen($hub) && $hub_update && (($contact['rel'] != Contact::FOLLOWER) || $protocol == Protocol::FEED)) {
199 Logger::log('hub ' . $hubmode . ' : ' . $hub . ' contact name : ' . $contact['name'] . ' local user : ' . $importer['name']);
200 $hubs = explode(',', $hub);
203 foreach ($hubs as $h) {
210 self::subscribeToHub($h, $importer, $contact, $hubmode);
215 self::updateContact($contact, ['failed' => false, 'last-update' => $updated, 'success_update' => $updated]);
216 Contact::unmarkForArchival($contact);
217 } elseif (in_array($contact["network"], [Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, Protocol::FEED])) {
218 self::updateContact($contact, ['failed' => true, 'last-update' => $updated, 'failure_update' => $updated]);
219 Contact::markForArchival($contact);
221 self::updateContact($contact, ['last-update' => $updated]);
228 private static function RemoveReply($subject)
230 while (in_array(strtolower(substr($subject, 0, 3)), ["re:", "aw:"])) {
231 $subject = trim(substr($subject, 4));
238 * Updates a personal contact entry and the public contact entry
240 * @param array $contact The personal contact entry
241 * @param array $fields The fields that are updated
244 private static function updateContact(array $contact, array $fields)
246 // Update the user's contact
247 DBA::update('contact', $fields, ['id' => $contact['id']]);
249 // Update the public contact
250 DBA::update('contact', $fields, ['uid' => 0, 'nurl' => $contact['nurl']]);
252 // Update the rest of the contacts that aren't polled
253 DBA::update('contact', $fields, ['rel' => Contact::FOLLOWER, 'nurl' => $contact['nurl']]);
259 * @param array $contact The personal contact entry
260 * @param string $updated The updated date
261 * @return string polled XML
264 private static function pollDFRN(array $contact, $updated)
266 $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
267 if (intval($contact['duplex']) && $contact['dfrn-id']) {
268 $idtosend = '0:' . $orig_id;
270 if (intval($contact['duplex']) && $contact['issued-id']) {
271 $idtosend = '1:' . $orig_id;
274 // they have permission to write to us. We already filtered this in the contact query.
277 // But this may be our first communication, so set the writable flag if it isn't set already.
278 if (!intval($contact['writable'])) {
279 $fields = ['writable' => true];
280 DBA::update('contact', $fields, ['id' => $contact['id']]);
283 $last_update = (($contact['last-update'] <= DBA::NULL_DATETIME)
284 ? DateTimeFormat::utc('now - 7 days', DateTimeFormat::ATOM)
285 : DateTimeFormat::utc($contact['last-update'], DateTimeFormat::ATOM)
288 $url = $contact['poll'] . '?dfrn_id=' . $idtosend
289 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
290 . '&type=data&last_update=' . $last_update
293 $curlResult = DI::httpRequest()->curl($url);
295 if (!$curlResult->isSuccess() && ($curlResult->getErrorNumber() == CURLE_OPERATION_TIMEDOUT)) {
296 // set the last-update so we don't keep polling
297 self::updateContact($contact, ['failed' => true, 'last-update' => $updated]);
298 Contact::markForArchival($contact);
299 Logger::log('Contact archived');
303 $handshake_xml = $curlResult->getBody();
304 $html_code = $curlResult->getReturnCode();
306 Logger::log('handshake with url ' . $url . ' returns xml: ' . $handshake_xml, Logger::DATA);
308 if (!strlen($handshake_xml) || ($html_code >= 400) || !$html_code) {
309 // dead connection - might be a transient event, or this might
310 // mean the software was uninstalled or the domain expired.
311 // Will keep trying for one month.
312 Logger::log("$url appears to be dead - marking for death ");
314 // set the last-update so we don't keep polling
315 $fields = ['failed' => true, 'last-update' => $updated, 'failure_update' => $updated];
316 self::updateContact($contact, $fields);
317 Contact::markForArchival($contact);
321 if (!strstr($handshake_xml, '<')) {
322 Logger::log('response from ' . $url . ' did not contain XML.');
324 $fields = ['failed' => true, 'last-update' => $updated, 'failure_update' => $updated];
325 self::updateContact($contact, $fields);
326 Contact::markForArchival($contact);
330 $res = XML::parseString($handshake_xml);
332 if (!is_object($res)) {
333 Logger::info('Unparseable response', ['url' => $url]);
335 $fields = ['failed' => true, 'last-update' => $updated, 'failure_update' => $updated];
336 self::updateContact($contact, $fields);
337 Contact::markForArchival($contact);
341 if (intval($res->status) == 1) {
342 // we may not be friends anymore. Will keep trying for one month.
343 Logger::log("$url replied status 1 - marking for death ");
345 // set the last-update so we don't keep polling
346 $fields = ['failed' => true, 'last-update' => $updated, 'failure_update' => $updated];
347 self::updateContact($contact, $fields);
348 Contact::markForArchival($contact);
349 } elseif ($contact['term-date'] > DBA::NULL_DATETIME) {
350 Contact::unmarkForArchival($contact);
353 if ((intval($res->status) != 0) || !strlen($res->challenge) || !strlen($res->dfrn_id)) {
354 // set the last-update so we don't keep polling
355 DBA::update('contact', ['last-update' => $updated], ['id' => $contact['id']]);
356 Logger::log('Contact status is ' . $res->status);
360 if (((float)$res->dfrn_version > 2.21) && ($contact['poco'] == '')) {
361 $fields = ['poco' => str_replace('/profile/', '/poco/', $contact['url'])];
362 DBA::update('contact', $fields, ['id' => $contact['id']]);
367 $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
368 $challenge = hex2bin((string) $res->challenge);
372 if ($contact['duplex'] && strlen($contact['prvkey'])) {
373 openssl_private_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['prvkey']);
374 openssl_private_decrypt($challenge, $postvars['challenge'], $contact['prvkey']);
376 openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
377 openssl_public_decrypt($challenge, $postvars['challenge'], $contact['pubkey']);
380 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
382 if (strpos($final_dfrn_id, ':') == 1) {
383 $final_dfrn_id = substr($final_dfrn_id, 2);
386 // There are issues with the legacy DFRN transport layer.
387 // Since we mostly don't use it anyway, we won't dig into it deeper, but simply ignore it.
388 if (empty($final_dfrn_id) || empty($orig_id)) {
389 Logger::log('Contact has got no ID - quitting');
393 if ($final_dfrn_id != $orig_id) {
394 // did not decode properly - cannot trust this site
395 Logger::log('ID did not decode: ' . $contact['id'] . ' orig: ' . $orig_id . ' final: ' . $final_dfrn_id);
397 // set the last-update so we don't keep polling
398 DBA::update('contact', ['last-update' => $updated], ['id' => $contact['id']]);
399 Contact::markForArchival($contact);
403 $postvars['dfrn_id'] = $idtosend;
404 $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
405 $postvars['perm'] = 'rw';
407 return DI::httpRequest()->post($contact['poll'], $postvars)->getBody();
411 * Poll Feed/OStatus contacts
413 * @param array $contact The personal contact entry
414 * @param string $protocol The used protocol of the contact
415 * @param string $updated The updated date
416 * @return string polled XML
419 private static function pollFeed(array $contact, $protocol, $updated)
421 // Upgrading DB fields from an older Friendica version
422 // Will only do this once per notify-enabled OStatus contact
423 // or if relationship changes
425 $stat_writeable = $contact['notify'] && ($contact['rel'] == Contact::FOLLOWER || $contact['rel'] == Contact::FRIEND);
427 // Contacts from OStatus are always writable
428 if ($protocol === Protocol::OSTATUS) {
432 if ($stat_writeable != $contact['writable']) {
433 $fields = ['writable' => $stat_writeable];
434 DBA::update('contact', $fields, ['id' => $contact['id']]);
437 // Are we allowed to import from this person?
438 if ($contact['rel'] == Contact::FOLLOWER || $contact['blocked']) {
439 // set the last-update so we don't keep polling
440 DBA::update('contact', ['last-update' => $updated], ['id' => $contact['id']]);
441 Logger::log('Contact is blocked or only a follower');
445 $cookiejar = tempnam(get_temppath(), 'cookiejar-onepoll-');
446 $curlResult = DI::httpRequest()->curl($contact['poll'], false, ['cookiejar' => $cookiejar]);
449 if ($curlResult->isTimeout()) {
450 // set the last-update so we don't keep polling
451 self::updateContact($contact, ['failed' => true, 'last-update' => $updated]);
452 Contact::markForArchival($contact);
453 Logger::log('Contact archived');
457 return $curlResult->getBody();
463 * @param array $contact The personal contact entry
464 * @param integer $importer_uid The UID of the importer
465 * @param string $updated The updated date
468 private static function pollMail(array $contact, $importer_uid, $updated)
470 Logger::log("Mail: Fetching for ".$contact['addr'], Logger::DEBUG);
472 $mail_disabled = ((function_exists('imap_open') && !DI::config()->get('system', 'imap_disabled')) ? 0 : 1);
473 if ($mail_disabled) {
474 // set the last-update so we don't keep polling
475 self::updateContact($contact, ['failed' => true, 'last-update' => $updated]);
476 Contact::markForArchival($contact);
477 Logger::log('Contact archived');
481 Logger::log("Mail: Enabled", Logger::DEBUG);
484 $user = DBA::selectFirst('user', ['prvkey'], ['uid' => $importer_uid]);
486 $condition = ["`server` != '' AND `uid` = ?", $importer_uid];
487 $mailconf = DBA::selectFirst('mailacct', [], $condition);
488 if (DBA::isResult($user) && DBA::isResult($mailconf)) {
489 $mailbox = Email::constructMailboxName($mailconf);
491 openssl_private_decrypt(hex2bin($mailconf['pass']), $password, $user['prvkey']);
492 $mbox = Email::connect($mailbox, $mailconf['user'], $password);
494 Logger::log("Mail: Connect to " . $mailconf['user']);
496 $fields = ['last_check' => $updated];
497 DBA::update('mailacct', $fields, ['id' => $mailconf['id']]);
498 Logger::log("Mail: Connected to " . $mailconf['user']);
500 Logger::log("Mail: Connection error ".$mailconf['user']." ".print_r(imap_errors(), true));
508 $msgs = Email::poll($mbox, $contact['addr']);
511 Logger::log("Mail: Parsing ".count($msgs)." mails from ".$contact['addr']." for ".$mailconf['user'], Logger::DEBUG);
513 $metas = Email::messageMeta($mbox, implode(',', $msgs));
515 if (count($metas) != count($msgs)) {
516 Logger::log("for " . $mailconf['user'] . " there are ". count($msgs) . " messages but received " . count($metas) . " metas", Logger::DEBUG);
518 $msgs = array_combine($msgs, $metas);
520 foreach ($msgs as $msg_uid => $meta) {
521 Logger::log("Mail: Parsing mail ".$msg_uid, Logger::DATA);
524 $datarray['uid'] = $importer_uid;
525 $datarray['contact-id'] = $contact['id'];
526 $datarray['verb'] = Activity::POST;
527 $datarray['object-type'] = Activity\ObjectType::NOTE;
528 $datarray['network'] = Protocol::MAIL;
529 // $meta = Email::messageMeta($mbox, $msg_uid);
531 $datarray['uri'] = Email::msgid2iri(trim($meta->message_id, '<>'));
533 // Have we seen it before?
534 $fields = ['deleted', 'id'];
535 $condition = ['uid' => $importer_uid, 'uri' => $datarray['uri']];
536 $item = Item::selectFirst($fields, $condition);
537 if (DBA::isResult($item)) {
538 Logger::log("Mail: Seen before ".$msg_uid." for ".$mailconf['user']." UID: ".$importer_uid." URI: ".$datarray['uri'],Logger::DEBUG);
540 // Only delete when mails aren't automatically moved or deleted
541 if (($mailconf['action'] != 1) && ($mailconf['action'] != 3))
542 if ($meta->deleted && ! $item['deleted']) {
543 $fields = ['deleted' => true, 'changed' => $updated];
544 Item::update($fields, ['id' => $item['id']]);
547 switch ($mailconf['action']) {
549 Logger::log("Mail: Seen before ".$msg_uid." for ".$mailconf['user'].". Doing nothing.", Logger::DEBUG);
552 Logger::log("Mail: Deleting ".$msg_uid." for ".$mailconf['user']);
553 imap_delete($mbox, $msg_uid, FT_UID);
556 Logger::log("Mail: Mark as seen ".$msg_uid." for ".$mailconf['user']);
557 imap_setflag_full($mbox, $msg_uid, "\\Seen", ST_UID);
560 Logger::log("Mail: Moving ".$msg_uid." to ".$mailconf['movetofolder']." for ".$mailconf['user']);
561 imap_setflag_full($mbox, $msg_uid, "\\Seen", ST_UID);
562 if ($mailconf['movetofolder'] != "") {
563 imap_mail_move($mbox, $msg_uid, $mailconf['movetofolder'], FT_UID);
570 // look for a 'references' or an 'in-reply-to' header and try to match with a parent item we have locally.
571 $raw_refs = (property_exists($meta, 'references') ? str_replace("\t", '', $meta->references) : '');
572 if (!trim($raw_refs)) {
573 $raw_refs = (property_exists($meta, 'in_reply_to') ? str_replace("\t", '', $meta->in_reply_to) : '');
575 $raw_refs = trim($raw_refs); // Don't allow a blank reference in $refs_arr
578 $refs_arr = explode(' ', $raw_refs);
579 if (count($refs_arr)) {
580 for ($x = 0; $x < count($refs_arr); $x ++) {
581 $refs_arr[$x] = Email::msgid2iri(str_replace(['<', '>', ' '],['', '', ''], $refs_arr[$x]));
584 $condition = ['uri' => $refs_arr, 'uid' => $importer_uid];
585 $parent = Item::selectFirst(['parent-uri'], $condition);
586 if (DBA::isResult($parent)) {
587 $datarray['parent-uri'] = $parent['parent-uri']; // Set the parent as the top-level item
591 // Decoding the header
592 $subject = imap_mime_header_decode($meta->subject ?? '');
593 $datarray['title'] = "";
594 foreach ($subject as $subpart) {
595 if ($subpart->charset != "default") {
596 $datarray['title'] .= iconv($subpart->charset, 'UTF-8//IGNORE', $subpart->text);
598 $datarray['title'] .= $subpart->text;
601 $datarray['title'] = Strings::escapeTags(trim($datarray['title']));
603 //$datarray['title'] = Strings::escapeTags(trim($meta->subject));
604 $datarray['created'] = DateTimeFormat::utc($meta->date);
607 $reply = ((substr(strtolower($datarray['title']), 0, 3) == "re:") ||
608 (substr(strtolower($datarray['title']), 0, 3) == "re-") ||
611 // Remove Reply-signs in the subject
612 $datarray['title'] = self::RemoveReply($datarray['title']);
614 // If it seems to be a reply but a header couldn't be found take the last message with matching subject
615 if (empty($datarray['parent-uri']) && $reply) {
616 $condition = ['title' => $datarray['title'], 'uid' => $importer_uid, 'network' => Protocol::MAIL];
617 $params = ['order' => ['created' => true]];
618 $parent = Item::selectFirst(['parent-uri'], $condition, $params);
619 if (DBA::isResult($parent)) {
620 $datarray['parent-uri'] = $parent['parent-uri'];
624 if (empty($datarray['parent-uri'])) {
625 $datarray['parent-uri'] = $datarray['uri'];
628 $headers = imap_headerinfo($mbox, $meta->msgno);
632 if (!empty($headers->from)) {
633 $object['from'] = $headers->from;
636 if (!empty($headers->to)) {
637 $object['to'] = $headers->to;
640 if (!empty($headers->reply_to)) {
641 $object['reply_to'] = $headers->reply_to;
644 if (!empty($headers->sender)) {
645 $object['sender'] = $headers->sender;
648 if (!empty($object)) {
649 $datarray['object'] = json_encode($object);
652 $fromname = $frommail = $headers->from[0]->mailbox . '@' . $headers->from[0]->host;
653 if (!empty($headers->from[0]->personal)) {
654 $fromname = $headers->from[0]->personal;
657 $datarray['author-name'] = $fromname;
658 $datarray['author-link'] = "mailto:".$frommail;
659 $datarray['author-avatar'] = $contact['photo'];
661 $datarray['owner-name'] = $contact['name'];
662 $datarray['owner-link'] = "mailto:".$contact['addr'];
663 $datarray['owner-avatar'] = $contact['photo'];
665 if ($datarray['parent-uri'] === $datarray['uri']) {
666 $datarray['private'] = Item::PRIVATE;
669 if (!DI::pConfig()->get($importer_uid, 'system', 'allow_public_email_replies')) {
670 $datarray['private'] = Item::PRIVATE;
671 $datarray['allow_cid'] = '<' . $contact['id'] . '>';
674 $datarray = Email::getMessage($mbox, $msg_uid, $reply, $datarray);
675 if (empty($datarray['body'])) {
676 Logger::log("Mail: can't fetch msg ".$msg_uid." for ".$mailconf['user']);
680 Logger::log("Mail: Importing ".$msg_uid." for ".$mailconf['user']);
682 Item::insert($datarray);
684 switch ($mailconf['action']) {
686 Logger::log("Mail: Seen before ".$msg_uid." for ".$mailconf['user'].". Doing nothing.", Logger::DEBUG);
689 Logger::log("Mail: Deleting ".$msg_uid." for ".$mailconf['user']);
690 imap_delete($mbox, $msg_uid, FT_UID);
693 Logger::log("Mail: Mark as seen ".$msg_uid." for ".$mailconf['user']);
694 imap_setflag_full($mbox, $msg_uid, "\\Seen", ST_UID);
697 Logger::log("Mail: Moving ".$msg_uid." to ".$mailconf['movetofolder']." for ".$mailconf['user']);
698 imap_setflag_full($mbox, $msg_uid, "\\Seen", ST_UID);
699 if ($mailconf['movetofolder'] != "") {
700 imap_mail_move($mbox, $msg_uid, $mailconf['movetofolder'], FT_UID);
707 Logger::log("Mail: no mails for ".$mailconf['user']);
710 Logger::log("Mail: closing connection for ".$mailconf['user']);
717 * @param array $importer
718 * @param array $contact
719 * @param string $hubmode
720 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
722 private static function subscribeToHub(string $url, array $importer, array $contact, $hubmode = 'subscribe')
725 * Diaspora has different message-ids in feeds than they do
726 * through the direct Diaspora protocol. If we try and use
727 * the feed, we'll get duplicates. So don't.
729 if ($contact['network'] === Protocol::DIASPORA) {
733 // Without an importer we don't have a user id - so we quit
734 if (empty($importer)) {
738 $user = DBA::selectFirst('user', ['nickname'], ['uid' => $importer['uid']]);
740 // No user, no nickname, we quit
741 if (!DBA::isResult($user)) {
745 $push_url = DI::baseUrl() . '/pubsub/' . $user['nickname'] . '/' . $contact['id'];
747 // Use a single verify token, even if multiple hubs
748 $verify_token = ((strlen($contact['hub-verify'])) ? $contact['hub-verify'] : Strings::getRandomHex());
750 $params = 'hub.mode=' . $hubmode . '&hub.callback=' . urlencode($push_url) . '&hub.topic=' . urlencode($contact['poll']) . '&hub.verify=async&hub.verify_token=' . $verify_token;
752 Logger::log('subscribe_to_hub: ' . $hubmode . ' ' . $contact['name'] . ' to hub ' . $url . ' endpoint: ' . $push_url . ' with verifier ' . $verify_token);
754 if (!strlen($contact['hub-verify']) || ($contact['hub-verify'] != $verify_token)) {
755 DBA::update('contact', ['hub-verify' => $verify_token], ['id' => $contact['id']]);
758 $postResult = DI::httpRequest()->post($url, $params);
760 Logger::log('subscribe_to_hub: returns: ' . $postResult->getReturnCode(), Logger::DEBUG);