]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/DFRN.php
Fix Undefined index: photo in ActivityPub/Receiver
[friendica.git] / src / Protocol / DFRN.php
index 3ba84ac44cc8ffcd9e9d06c08a77321f9bdf3600..f92d1401dfe49f7c5365217f35a1fa1cabfd507d 100644 (file)
@@ -81,7 +81,8 @@ class DFRN
                                return [];
                        }
 
-                       $user['importer_uid']  = $user['uid'];
+                       $user['importer_uid'] = $user['uid'];
+                       $user['uprvkey'] = $user['prvkey'];
                } else {
                        $user = ['importer_uid' => 0, 'uprvkey' => '', 'timezone' => 'UTC',
                                'nickname' => '', 'sprvkey' => '', 'spubkey' => '',
@@ -1163,15 +1164,17 @@ class DFRN
         * @return int Deliver status. Negative values mean an error.
         * @todo Add array type-hint for $owner, $contact
         */
-       public static function deliver($owner, $contact, $atom, $dissolve = false)
+       public static function deliver($owner, $contact, $atom, $dissolve = false, $legacy_transport = false)
        {
                $a = get_app();
 
                // At first try the Diaspora transport layer
-               $ret = self::transmit($owner, $contact, $atom);
-               if ($ret >= 200) {
-                       logger('Delivery via Diaspora transport layer was successful with status ' . $ret);
-                       return $ret;
+               if (!$dissolve && !$legacy_transport) {
+                       $curlResult = self::transmit($owner, $contact, $atom);
+                       if ($curlResult >= 200) {
+                               logger('Delivery via Diaspora transport layer was successful with status ' . $curlResult);
+                               return $curlResult;
+                       }
                }
 
                $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
@@ -1208,16 +1211,16 @@ class DFRN
 
                logger('dfrn_deliver: ' . $url);
 
-               $ret = Network::curl($url);
+               $curlResult = Network::curl($url);
 
-               if (!empty($ret["errno"]) && ($ret['errno'] == CURLE_OPERATION_TIMEDOUT)) {
+               if ($curlResult->isTimeout()) {
                        Contact::markForArchival($contact);
                        return -2; // timed out
                }
 
-               $xml = $ret['body'];
+               $xml = $curlResult->getBody();
 
-               $curl_stat = $a->get_curl_code();
+               $curl_stat = $curlResult->getReturnCode();
                if (empty($curl_stat)) {
                        Contact::markForArchival($contact);
                        return -3; // timed out
@@ -1330,6 +1333,7 @@ class DFRN
                                        $key = openssl_random_pseudo_bytes(16);
                                        $data = self::aesEncrypt($postvars['data'], $key);
                                        break;
+
                                default:
                                        logger("rino: invalid requested version '$rino_remote_version'");
                                        Contact::markForArchival($contact);
@@ -1364,17 +1368,19 @@ class DFRN
 
                logger('dfrn_deliver: ' . "SENDING: " . print_r($postvars, true), LOGGER_DATA);
 
-               $xml = Network::post($contact['notify'], $postvars);
+               $postResult = Network::post($contact['notify'], $postvars);
+
+               $xml = $postResult->getBody();
 
                logger('dfrn_deliver: ' . "RECEIVED: " . $xml, LOGGER_DATA);
 
-               $curl_stat = $a->get_curl_code();
+               $curl_stat = $postResult->getReturnCode();
                if (empty($curl_stat) || empty($xml)) {
                        Contact::markForArchival($contact);
                        return -9; // timed out
                }
 
-               if (($curl_stat == 503) && stristr($a->get_curl_headers(), 'retry-after')) {
+               if (($curl_stat == 503) && stristr($postResult->getHeader(), 'retry-after')) {
                        Contact::markForArchival($contact);
                        return -10;
                }
@@ -1463,16 +1469,17 @@ class DFRN
 
                $content_type = ($public_batch ? "application/magic-envelope+xml" : "application/json");
 
-               $xml = Network::post($dest_url, $envelope, ["Content-Type: ".$content_type]);
+               $postResult = Network::post($dest_url, $envelope, ["Content-Type: ".$content_type]);
+               $xml = $postResult->getBody();
 
-               $curl_stat = $a->get_curl_code();
+               $curl_stat = $postResult->getReturnCode();
                if (empty($curl_stat) || empty($xml)) {
                        logger('Empty answer from ' . $contact['id'] . ' - ' . $dest_url);
                        Contact::markForArchival($contact);
                        return -9; // timed out
                }
 
-               if (($curl_stat == 503) && (stristr($a->get_curl_headers(), 'retry-after'))) {
+               if (($curl_stat == 503) && (stristr($postResult->getHeader(), 'retry-after'))) {
                        Contact::markForArchival($contact);
                        return -10;
                }
@@ -1617,6 +1624,10 @@ class DFRN
                        }
                }
 
+               if (empty($author['avatar'])) {
+                       logger('Empty author: ' . $xml);
+               }
+
                if (DBA::isResult($contact_old) && !$onlyfetch) {
                        logger("Check if contact details for contact " . $contact_old["id"] . " (" . $contact_old["nick"] . ") have to be updated.", LOGGER_DEBUG);
 
@@ -1746,6 +1757,10 @@ class DFRN
                        // Update check for this field has to be done differently
                        $datefields = ["name-date", "uri-date"];
                        foreach ($datefields as $field) {
+                               // The date fields arrives as '2018-07-17T10:44:45Z' - the database return '2018-07-17 10:44:45'
+                               // The fields have to be in the same format to be comparable, since strtotime does add timezones.
+                               $contact[$field] = DateTimeFormat::utc($contact[$field]);
+
                                if (strtotime($contact[$field]) > strtotime($contact_old[$field])) {
                                        logger("Difference for contact " . $contact["id"] . " in field '" . $field . "'. New value: '" . $contact[$field] . "', old value '" . $contact_old[$field] . "'", LOGGER_DEBUG);
                                        $update = true;
@@ -2243,6 +2258,11 @@ class DFRN
                        if ($Blink && link_compare($Blink, System::baseUrl() . "/profile/" . $importer["nickname"])) {
                                $author = DBA::selectFirst('contact', ['name', 'thumb', 'url'], ['id' => $item['author-id']]);
 
+                               $item['id'] = $posted_id;
+
+                               $parent = Item::selectFirst(['id'], ['uri' => $item['parent-uri'], 'uid' => $importer["importer_uid"]]);
+                               $item["parent"] = $parent['id'];
+
                                // send a notification
                                notification(
                                        [
@@ -2332,6 +2352,12 @@ class DFRN
                                if (Item::exists($condition)) {
                                        return false;
                                }
+
+                               // The owner of an activity must be the author
+                               $item["owner-name"] = $item["author-name"];
+                               $item["owner-link"] = $item["author-link"];
+                               $item["owner-avatar"] = $item["author-avatar"];
+                               $item["owner-id"] = $item["author-id"];
                        } else {
                                $is_like = false;
                        }
@@ -2393,8 +2419,11 @@ class DFRN
                                                break;
                                        case "enclosure":
                                                $enclosure = $href;
-                                               if (strlen($item["attach"])) {
+
+                                               if (!empty($item["attach"])) {
                                                        $item["attach"] .= ",";
+                                               } else {
+                                                       $item["attach"] = "";
                                                }
 
                                                $item["attach"] .= '[attach]href="' . $href . '" length="' . $length . '" type="' . $type . '" title="' . $title . '"[/attach]';
@@ -2440,7 +2469,7 @@ class DFRN
                }
 
                // Fetch the owner
-               $owner = self::fetchauthor($xpath, $entry, $importer, "dfrn:owner", true);
+               $owner = self::fetchauthor($xpath, $entry, $importer, "dfrn:owner", true, $xml);
 
                $owner_unknown = (isset($owner["contact-unknown"]) && $owner["contact-unknown"]);
 
@@ -2450,7 +2479,7 @@ class DFRN
                $item["owner-id"] = Contact::getIdForURL($owner["link"], 0);
 
                // fetch the author
-               $author = self::fetchauthor($xpath, $entry, $importer, "atom:author", true);
+               $author = self::fetchauthor($xpath, $entry, $importer, "atom:author", true, $xml);
 
                $item["author-name"] = $author["name"];
                $item["author-link"] = $author["link"];
@@ -2470,7 +2499,7 @@ class DFRN
 
                /// @todo Do we really need this check for HTML elements? (It was copied from the old function)
                if ((strpos($item['body'], '<') !== false) && (strpos($item['body'], '>') !== false)) {
-                       $base_url = get_app()->get_baseurl();
+                       $base_url = get_app()->getBaseURL();
                        $item['body'] = reltoabs($item['body'], $base_url);
 
                        $item['body'] = html2bb_video($item['body']);