Merge remote-tracking branch 'upstream/2018.08-rc' into ap1
[friendica.git/.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         private static function objectify($element)
44         {
45                 if (is_array($element)) {
46                         $keys = array_keys($element);
47                         if (is_int(array_pop($keys))) {
48                                 return array_map('objectify', $element);
49                         } else {
50                                 return (object)array_map('objectify', $element);
51                         }
52                 } else {
53                         return $element;
54                 }
55         }
56
57         public static function normalize($json)
58         {
59                 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
60
61 //              $jsonobj = array_map('Friendica\Util\JsonLD::objectify', $json);
62                 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
63
64                 return jsonld_normalize($jsonobj, array('algorithm' => 'URDNA2015', 'format' => 'application/nquads'));
65         }
66
67         public static function compact($json)
68         {
69                 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
70
71                 $context = (object)['as' => 'https://www.w3.org/ns/activitystreams',
72                         'w3sec' => 'https://w3id.org/security',
73                         'ostatus' => (object)['@id' => 'http://ostatus.org#', '@type' => '@id'],
74                         'vcard' => (object)['@id' => 'http://www.w3.org/2006/vcard/ns#', '@type' => '@id'],
75                         'uuid' => (object)['@id' => 'http://schema.org/identifier', '@type' => '@id']];
76
77                 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
78
79                 $compacted = jsonld_compact($jsonobj, $context);
80
81                 return json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);
82         }
83
84         public static function fetchElement($array, $element, $key, $type = null, $type_value = null)
85         {
86                 if (empty($array)) {
87                         return false;
88                 }
89
90                 if (empty($array[$element])) {
91                         return false;
92                 }
93
94                 if (is_string($array[$element])) {
95                         return $array[$element];
96                 }
97
98                 if (is_null($type_value)) {
99                         if (!empty($array[$element][$key])) {
100                                 return $array[$element][$key];
101                         }
102
103                         if (!empty($array[$element][0][$key])) {
104                                 return $array[$element][0][$key];
105                         }
106
107                         return false;
108                 }
109
110                 if (!empty($array[$element][$key]) && !empty($array[$element][$type]) && ($array[$element][$type] == $type_value)) {
111                         return $array[$element][$key];
112                 }
113
114                 /// @todo Add array search
115
116                 return false;
117         }
118 }