]> git.mxchange.org Git - friendica.git/blob - src/Util/JsonLD.php
JSON-LD stuff is now in a separate file
[friendica.git] / src / Util / JsonLD.php
1 <?php
2 /**
3  * @file src/Util/JsonLD.php
4  */
5 namespace Friendica\Util;
6
7 use Friendica\Core\Cache;
8 use digitalbazaar\jsonld as DBJsonLD;
9
10 /**
11  * @brief This class contain methods to work with JsonLD data
12  */
13 class JsonLD
14 {
15         public static function documentLoader($url)
16         {
17                 $recursion = 0;
18
19                 $x = debug_backtrace();
20                 if ($x) {
21                         foreach ($x as $n) {
22                                 if ($n['function'] === __FUNCTION__)  {
23                                         $recursion ++;
24                                 }
25                         }
26                 }
27
28                 if ($recursion > 5) {
29                         logger('jsonld bomb detected at: ' . $url);
30                         exit();
31                 }
32
33                 $result = Cache::get('documentLoader:' . $url);
34                 if (!is_null($result)) {
35                         return $result;
36                 }
37
38                 $data = jsonld_default_document_loader($url);
39                 Cache::set('documentLoader:' . $url, $data, CACHE_DAY);
40                 return $data;
41         }
42
43         public static function normalize($json)
44         {
45                 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
46
47                 $jsonobj = json_decode(json_encode($json));
48
49                 return jsonld_normalize($jsonobj, array('algorithm' => 'URDNA2015', 'format' => 'application/nquads'));
50         }
51
52         public static function compact($json)
53         {
54                 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
55
56                 $context = (object)['as' => 'https://www.w3.org/ns/activitystreams',
57                         'w3sec' => 'https://w3id.org/security',
58                         'ostatus' => (object)['@id' => 'http://ostatus.org#', '@type' => '@id'],
59                         'vcard' => (object)['@id' => 'http://www.w3.org/2006/vcard/ns#', '@type' => '@id'],
60                         'uuid' => (object)['@id' => 'http://schema.org/identifier', '@type' => '@id']];
61
62                 $jsonobj = json_decode(json_encode($json));
63
64                 $compacted = jsonld_compact($jsonobj, $context);
65
66                 return json_decode(json_encode($compacted), true);
67         }
68 }