]> git.mxchange.org Git - friendica.git/commitdiff
JSON-LD stuff is now in a separate file
authorMichael <heluecht@pirati.ca>
Thu, 20 Sep 2018 05:30:07 +0000 (05:30 +0000)
committerMichael <heluecht@pirati.ca>
Thu, 20 Sep 2018 05:30:07 +0000 (05:30 +0000)
src/Protocol/ActivityPub.php
src/Util/JsonLD.php [new file with mode: 0644]

index 4a2394a24b2ba5f1a2fbb9f2b8d1e9a82c17b788..36cc32d64b961b5c1b1f3101ef84be757e4520ad 100644 (file)
@@ -19,8 +19,6 @@ use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Crypto;
 use Friendica\Content\Text\BBCode;
 use Friendica\Content\Text\HTML;
-use Friendica\Core\Cache;
-use digitalbazaar\jsonld;
 
 /**
  * @brief ActivityPub Protocol class
@@ -58,51 +56,6 @@ class ActivityPub
 {
        const PUBLIC = 'https://www.w3.org/ns/activitystreams#Public';
 
-public static function jsonld_document_loader($url)
-{
-       $recursion = 0;
-
-       $x = debug_backtrace();
-       if ($x) {
-               foreach ($x as $n) {
-                       if ($n['function'] === __FUNCTION__)  {
-                               $recursion ++;
-                       }
-               }
-       }
-
-       if ($recursion > 5) {
-               logger('jsonld bomb detected at: ' . $url);
-               exit();
-       }
-
-       $result = Cache::get('jsonld_document_loader:' . $url);
-       if (!is_null($result)) {
-               return $result;
-       }
-
-       $data = jsonld_default_document_loader($url);
-       Cache::set('jsonld_document_loader:' . $url, $data, CACHE_DAY);
-       return $data;
-}
-
-       public static function compactJsonLD($json)
-       {
-               jsonld_set_document_loader('Friendica\Protocol\ActivityPub::jsonld_document_loader');
-
-               $context = (object)['as' => 'https://www.w3.org/ns/activitystreams',
-                       'w3sec' => 'https://w3id.org/security',
-                       'ostatus' => (object)['@id' => 'http://ostatus.org#', '@type' => '@id'],
-                       'vcard' => (object)['@id' => 'http://www.w3.org/2006/vcard/ns#', '@type' => '@id'],
-                       'uuid' => (object)['@id' => 'http://schema.org/identifier', '@type' => '@id']];
-
-               $jsonobj = json_decode(json_encode($json));
-
-               $compacted = jsonld_compact($jsonobj, $context);
-
-               return json_decode(json_encode($compacted), true);
-       }
-
        public static function isRequest()
        {
                return stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/activity+json') ||
diff --git a/src/Util/JsonLD.php b/src/Util/JsonLD.php
new file mode 100644 (file)
index 0000000..e8ff588
--- /dev/null
@@ -0,0 +1,68 @@
+<?php
+/**
+ * @file src/Util/JsonLD.php
+ */
+namespace Friendica\Util;
+
+use Friendica\Core\Cache;
+use digitalbazaar\jsonld as DBJsonLD;
+
+/**
+ * @brief This class contain methods to work with JsonLD data
+ */
+class JsonLD
+{
+       public static function documentLoader($url)
+       {
+               $recursion = 0;
+
+               $x = debug_backtrace();
+               if ($x) {
+                       foreach ($x as $n) {
+                               if ($n['function'] === __FUNCTION__)  {
+                                       $recursion ++;
+                               }
+                       }
+               }
+
+               if ($recursion > 5) {
+                       logger('jsonld bomb detected at: ' . $url);
+                       exit();
+               }
+
+               $result = Cache::get('documentLoader:' . $url);
+               if (!is_null($result)) {
+                       return $result;
+               }
+
+               $data = jsonld_default_document_loader($url);
+               Cache::set('documentLoader:' . $url, $data, CACHE_DAY);
+               return $data;
+       }
+
+       public static function normalize($json)
+       {
+               jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
+
+               $jsonobj = json_decode(json_encode($json));
+
+               return jsonld_normalize($jsonobj, array('algorithm' => 'URDNA2015', 'format' => 'application/nquads'));
+       }
+
+       public static function compact($json)
+       {
+               jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
+
+               $context = (object)['as' => 'https://www.w3.org/ns/activitystreams',
+                       'w3sec' => 'https://w3id.org/security',
+                       'ostatus' => (object)['@id' => 'http://ostatus.org#', '@type' => '@id'],
+                       'vcard' => (object)['@id' => 'http://www.w3.org/2006/vcard/ns#', '@type' => '@id'],
+                       'uuid' => (object)['@id' => 'http://schema.org/identifier', '@type' => '@id']];
+
+               $jsonobj = json_decode(json_encode($json));
+
+               $compacted = jsonld_compact($jsonobj, $context);
+
+               return json_decode(json_encode($compacted), true);
+       }
+}