]> git.mxchange.org Git - friendica.git/commitdiff
Changes:
authorRoland Häder <roland@mxchange.org>
Fri, 24 Jun 2022 01:14:22 +0000 (03:14 +0200)
committerRoland Häder <roland@mxchange.org>
Fri, 24 Jun 2022 01:17:57 +0000 (03:17 +0200)
- Made Item::guidFromUri()'s $host parameter optional (allowing null)
- added some type-hints
- added documentation
- this may fix reported issue in #11630

src/Model/Item.php
src/Protocol/Feed.php
src/Util/LDSignature.php

index c87dacd9b238bedbe0db0c3bb1d0c1c93e3fb014..bb8080e5fd4f234623d5e90af64b6cb0a9a6df6d 100644 (file)
@@ -1830,11 +1830,11 @@ class Item
         * Posts that are created on this system are using System::createUUID.
         * Received ActivityPub posts are using Processor::getGUIDByURL.
         *
-        * @param string $uri uri of an item entry
-        * @param string $host hostname for the GUID prefix
-        * @return string unique guid
+        * @param string      $uri uri of an item entry
+        * @param string|null $host hostname for the GUID prefix
+        * @return string Unique guid
         */
-       public static function guidFromUri(string $uri, string $host): string
+       public static function guidFromUri(string $uri, string $host = null): string
        {
                // Our regular guid routine is using this kind of prefix as well
                // We have to avoid that different routines could accidentally create the same value
index 6428fca66277ed70150b0a63c8abaefe551e532b..65b0696effb5a3fd9b20dd8dc817c64a9507f8c8 100644 (file)
@@ -313,7 +313,7 @@ class Feed
                                $item['uri'] = $guid;
 
                                // Don't use the GUID value directly but instead use it as a basis for the GUID
-                               $item['guid'] = Item::guidFromUri($guid, parse_url($guid, PHP_URL_HOST) ?? parse_url($item['plink'], PHP_URL_HOST) ?? '');
+                               $item['guid'] = Item::guidFromUri($guid, parse_url($guid, PHP_URL_HOST) ?? parse_url($item['plink'], PHP_URL_HOST));
                        }
 
                        if (empty($item['uri'])) {
index b5a55ea3596f8b43dcaebca876c45bf028a30fa3..ed6030145d8a411294b46f9ff070b48d4c6cc188 100644 (file)
@@ -31,12 +31,24 @@ use Friendica\Model\APContact;
  */
 class LDSignature
 {
-       public static function isSigned($data)
+       /**
+        * Checks if element 'signature' is found and not empty
+        *
+        * @param array $data
+        * @return bool
+        */
+       public static function isSigned(array $data): bool
        {
                return !empty($data['signature']);
        }
 
-       public static function getSigner($data)
+       /**
+        * Returns actor (signer) from given data
+        *
+        * @param array $data
+        * @return mixed Returns actor or false on error
+        */
+       public static function getSigner(array $data)
        {
                if (!self::isSigned($data)) {
                        return false;
@@ -66,13 +78,20 @@ class LDSignature
                }
        }
 
-       public static function sign($data, $owner)
+       /**
+        * Signs given data by owner's signature
+        *
+        * @param array $data Data to sign
+        * @param array $owner Owner information, like URL
+        * @return array Merged array of $data and signature
+        */
+       public static function sign(array $data, array $owner): array
        {
                $options = [
                        'type' => 'RsaSignature2017',
                        'nonce' => Strings::getRandomHex(64),
                        'creator' => $owner['url'] . '#main-key',
-                       'created' => DateTimeFormat::utcNow(DateTimeFormat::ATOM)
+                       'created' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
                ];
 
                $ohash = self::hash(self::signableOptions($options));
@@ -82,13 +101,25 @@ class LDSignature
                return array_merge($data, ['signature' => $options]);
        }
 
-       private static function signableData($data)
+       /**
+        * Removes element 'signature' from array
+        *
+        * @param array $data
+        * @return array With no element 'signature'
+        */
+       private static function signableData(array $data): array
        {
                unset($data['signature']);
                return $data;
        }
 
-       private static function signableOptions($options)
+       /**
+        * Removes some elements and adds '@context' to it
+        *
+        * @param array $options
+        * @return array With some removed elements and added '@context' element
+        */
+       private static function signableOptions(array $options): array
        {
                $newopts = ['@context' => 'https://w3id.org/identity/v1'];
 
@@ -99,7 +130,13 @@ class LDSignature
                return array_merge($newopts, $options);
        }
 
-       private static function hash($obj)
+       /**
+        * Hashes normalized object
+        *
+        * @param ??? $obj
+        * @return string SHA256 hash
+        */
+       private static function hash($obj): string
        {
                return hash('sha256', JsonLD::normalize($obj));
        }