]> git.mxchange.org Git - friendica.git/blobdiff - src/Worker/OnePoll.php
wrapping up 2019.12
[friendica.git] / src / Worker / OnePoll.php
index 3c14100ab25706ff17e1f395ac89c833a8c20bea..f2ba89adf7bc99bfde1a93d0f89b2880072804b6 100644 (file)
@@ -14,6 +14,7 @@ use Friendica\Database\DBA;
 use Friendica\Model\Contact;
 use Friendica\Model\Item;
 use Friendica\Model\User;
+use Friendica\Protocol\Activity;
 use Friendica\Protocol\ActivityPub;
 use Friendica\Protocol\Email;
 use Friendica\Protocol\PortableContact;
@@ -26,8 +27,6 @@ class OnePoll
 {
        public static function execute($contact_id = 0, $command = '')
        {
-               $a = BaseObject::getApp();
-
                Logger::log('Start for contact ' . $contact_id);
 
                $force = false;
@@ -42,7 +41,7 @@ class OnePoll
                }
 
                if ($force) {
-                       Contact::updateFromProbe($contact_id, true);
+                       Contact::updateFromProbe($contact_id, '', true);
                }
 
                $contact = DBA::selectFirst('contact', [], ['id' => $contact_id]);
@@ -193,11 +192,9 @@ class OnePoll
                        }
 
                        self::updateContact($contact, ['last-update' => $updated, 'success_update' => $updated]);
-                       DBA::update('gcontact', ['last_contact' => $updated], ['nurl' => $contact['nurl']]);
                        Contact::unmarkForArchival($contact);
                } elseif (in_array($contact["network"], [Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, Protocol::FEED])) {
                        self::updateContact($contact, ['last-update' => $updated, 'failure_update' => $updated]);
-                       DBA::update('gcontact', ['last_failure' => $updated], ['nurl' => $contact['nurl']]);
                        Contact::markForArchival($contact);
                } else {
                        self::updateContact($contact, ['last-update' => $updated]);
@@ -305,6 +302,15 @@ class OnePoll
 
                $res = XML::parseString($handshake_xml);
 
+               if (!is_object($res)) {
+                       Logger::info('Unparseable response', ['url' => $url]);
+
+                       $fields = ['last-update' => $updated, 'failure_update' => $updated];
+                       self::updateContact($contact, $fields);
+                       Contact::markForArchival($contact);
+                       return false;
+               }
+
                if (intval($res->status) == 1) {
                        // we may not be friends anymore. Will keep trying for one month.
                        Logger::log("$url replied status 1 - marking for death ");
@@ -488,8 +494,10 @@ class OnePoll
                                        Logger::log("Mail: Parsing mail ".$msg_uid, Logger::DATA);
 
                                        $datarray = [];
-                                       $datarray['verb'] = ACTIVITY_POST;
-                                       $datarray['object-type'] = ACTIVITY_OBJ_NOTE;
+                                       $datarray['uid'] = $importer_uid;
+                                       $datarray['contact-id'] = $contact['id'];
+                                       $datarray['verb'] = Activity::POST;
+                                       $datarray['object-type'] = Activity\ObjectType::NOTE;
                                        $datarray['network'] = Protocol::MAIL;
                                        // $meta = Email::messageMeta($mbox, $msg_uid);
 
@@ -533,9 +541,9 @@ class OnePoll
                                        }
 
                                        // look for a 'references' or an 'in-reply-to' header and try to match with a parent item we have locally.
-                                       $raw_refs = ((property_exists($meta, 'references')) ? str_replace("\t", '', $meta->references) : '');
+                                       $raw_refs = (property_exists($meta, 'references') ? str_replace("\t", '', $meta->references) : '');
                                        if (!trim($raw_refs)) {
-                                               $raw_refs = ((property_exists($meta, 'in_reply_to')) ? str_replace("\t", '', $meta->in_reply_to) : '');
+                                               $raw_refs = (property_exists($meta, 'in_reply_to') ? str_replace("\t", '', $meta->in_reply_to) : '');
                                        }
                                        $raw_refs = trim($raw_refs);  // Don't allow a blank reference in $refs_arr
 
@@ -554,7 +562,7 @@ class OnePoll
                                        }
 
                                        // Decoding the header
-                                       $subject = imap_mime_header_decode($meta->subject);
+                                       $subject = imap_mime_header_decode($meta->subject ?? '');
                                        $datarray['title'] = "";
                                        foreach ($subject as $subpart) {
                                                if ($subpart->charset != "default") {
@@ -590,36 +598,33 @@ class OnePoll
                                                $datarray['parent-uri'] = $datarray['uri'];
                                        }
 
-                                       $r = Email::getMessage($mbox, $msg_uid, $reply);
-                                       if (!$r) {
-                                               Logger::log("Mail: can't fetch msg ".$msg_uid." for ".$mailconf['user']);
-                                               continue;
-                                       }
-                                       $datarray['body'] = Strings::escapeHtml($r['body']);
-                                       $datarray['body'] = BBCode::limitBodySize($datarray['body']);
+                                       $headers = imap_headerinfo($mbox, $meta->msgno);
 
-                                       Logger::log("Mail: Importing ".$msg_uid." for ".$mailconf['user']);
+                                       $object = [];
+
+                                       if (!empty($headers->from)) {
+                                               $object['from'] = $headers->from;
+                                       }
 
-                                       /// @TODO Adding a gravatar for the original author would be cool
+                                       if (!empty($headers->to)) {
+                                               $object['to'] = $headers->to;
+                                       }
 
-                                       $from = imap_mime_header_decode($meta->from);
-                                       $fromdecoded = "";
-                                       foreach ($from as $frompart) {
-                                               if ($frompart->charset != "default") {
-                                                       $fromdecoded .= iconv($frompart->charset, 'UTF-8//IGNORE', $frompart->text);
-                                               } else {
-                                                       $fromdecoded .= $frompart->text;
-                                               }
+                                       if (!empty($headers->reply_to)) {
+                                               $object['reply_to'] = $headers->reply_to;
                                        }
 
-                                       $fromarr = imap_rfc822_parse_adrlist($fromdecoded, $a->getHostName());
+                                       if (!empty($headers->sender)) {
+                                               $object['sender'] = $headers->sender;
+                                       }
 
-                                       $frommail = $fromarr[0]->mailbox."@".$fromarr[0]->host;
+                                       if (!empty($object)) {
+                                               $datarray['object'] = json_encode($object);
+                                       }
 
-                                       if (isset($fromarr[0]->personal)) {
-                                               $fromname = $fromarr[0]->personal;
-                                       } else {
-                                               $fromname = $frommail;
+                                       $fromname = $frommail = $headers->from[0]->mailbox . '@' . $headers->from[0]->host;
+                                       if (!empty($headers->from[0]->personal)) {
+                                               $fromname = $headers->from[0]->personal;
                                        }
 
                                        $datarray['author-name'] = $fromname;
@@ -630,16 +635,23 @@ class OnePoll
                                        $datarray['owner-link'] = "mailto:".$contact['addr'];
                                        $datarray['owner-avatar'] = $contact['photo'];
 
-                                       $datarray['uid'] = $importer_uid;
-                                       $datarray['contact-id'] = $contact['id'];
                                        if ($datarray['parent-uri'] === $datarray['uri']) {
                                                $datarray['private'] = 1;
                                        }
+
                                        if (!PConfig::get($importer_uid, 'system', 'allow_public_email_replies')) {
                                                $datarray['private'] = 1;
                                                $datarray['allow_cid'] = '<' . $contact['id'] . '>';
                                        }
 
+                                       $datarray = Email::getMessage($mbox, $msg_uid, $reply, $datarray);
+                                       if (empty($datarray['body'])) {
+                                               Logger::log("Mail: can't fetch msg ".$msg_uid." for ".$mailconf['user']);
+                                               continue;
+                                       }
+
+                                       Logger::log("Mail: Importing ".$msg_uid." for ".$mailconf['user']);
+
                                        Item::insert($datarray);
 
                                        switch ($mailconf['action']) {