Log function
[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 Friendica\Core\Logger;
9 use Exception;
10
11 /**
12  * @brief This class contain methods to work with JsonLD data
13  */
14 class JsonLD
15 {
16         /**
17          * @brief Loader for LD-JSON validation
18          *
19          * @param $url
20          *
21          * @return the loaded data
22          */
23         public static function documentLoader($url)
24         {
25                 $recursion = 0;
26
27                 $x = debug_backtrace();
28                 if ($x) {
29                         foreach ($x as $n) {
30                                 if ($n['function'] === __FUNCTION__)  {
31                                         $recursion ++;
32                                 }
33                         }
34                 }
35
36                 if ($recursion > 5) {
37                         Logger::log('jsonld bomb detected at: ' . $url);
38                         exit();
39                 }
40
41                 $result = Cache::get('documentLoader:' . $url);
42                 if (!is_null($result)) {
43                         return $result;
44                 }
45
46                 $data = jsonld_default_document_loader($url);
47                 Cache::set('documentLoader:' . $url, $data, Cache::DAY);
48                 return $data;
49         }
50
51         /**
52          * @brief Normalises a given JSON array
53          *
54          * @param array $json
55          *
56          * @return normalized JSON string
57          */
58         public static function normalize($json)
59         {
60                 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
61
62                 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
63
64                 try {
65                         $normalized = jsonld_normalize($jsonobj, array('algorithm' => 'URDNA2015', 'format' => 'application/nquads'));
66                 }
67                 catch (Exception $e) {
68                         $normalized = false;
69                         Logger::log('normalise error:' . print_r($e, true), LOGGER_DEBUG);
70                 }
71
72                 return $normalized;
73         }
74
75         /**
76          * @brief Compacts a given JSON array
77          *
78          * @param array $json
79          *
80          * @return comacted JSON array
81          */
82         public static function compact($json)
83         {
84                 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
85
86                 $context = (object)['as' => 'https://www.w3.org/ns/activitystreams#',
87                         'w3id' => 'https://w3id.org/security#',
88                         'vcard' => (object)['@id' => 'http://www.w3.org/2006/vcard/ns#', '@type' => '@id'],
89                         'ostatus' => (object)['@id' => 'http://ostatus.org#', '@type' => '@id'],
90                         'diaspora' => (object)['@id' => 'https://diasporafoundation.org/ns/', '@type' => '@id'],
91                         'dc' => (object)['@id' => 'http://purl.org/dc/terms/', '@type' => '@id'],
92                         'ldp' => (object)['@id' => 'http://www.w3.org/ns/ldp#', '@type' => '@id']];
93
94                 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
95
96
97                 try {
98                         $compacted = jsonld_compact($jsonobj, $context);
99                 }
100                 catch (Exception $e) {
101                         $compacted = false;
102                         Logger::log('compacting error:' . print_r($e, true), LOGGER_DEBUG);
103                 }
104
105                 return json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);
106         }
107
108         /**
109          * @brief Fetches an element array from a JSON array
110          *
111          * @param $array
112          * @param $element
113          * @param $key
114          *
115          * @return fetched element array
116          */
117         public static function fetchElementArray($array, $element, $key = '@id')
118         {
119                 if (empty($array)) {
120                         return null;
121                 }
122
123                 if (!isset($array[$element])) {
124                         return null;
125                 }
126
127                 // If it isn't an array yet, make it to one
128                 if (!is_int(key($array[$element]))) {
129                         $array[$element] = [$array[$element]];
130                 }
131
132                 $elements = [];
133
134                 foreach ($array[$element] as $entry) {
135                         if (!is_array($entry)) {
136                                 $elements[] = $entry;
137                         } elseif (!empty($entry[$key])) {
138                                 $elements[] = $entry[$key];
139                         } elseif (!empty($entry) || !is_array($entry)) {
140                                 $elements[] = $entry;
141                         }
142                 }
143
144                 return $elements;
145         }
146
147         /**
148          * @brief Fetches an element from a JSON array
149          *
150          * @param $array
151          * @param $element
152          * @param $key
153          * @param $type
154          * @param $type_value
155          *
156          * @return fetched element
157          */
158         public static function fetchElement($array, $element, $key = '@id', $type = null, $type_value = null)
159         {
160                 if (empty($array)) {
161                         return null;
162                 }
163
164                 if (!isset($array[$element])) {
165                         return null;
166                 }
167
168                 if (!is_array($array[$element])) {
169                         return $array[$element];
170                 }
171
172                 if (is_null($type) || is_null($type_value)) {
173                         $element_array = self::fetchElementArray($array, $element, $key);
174                         if (is_null($element_array)) {
175                                 return null;
176                         }
177
178                         return array_shift($element_array);
179                 }
180
181                 $element_array = self::fetchElementArray($array, $element);
182                 if (is_null($element_array)) {
183                         return null;
184                 }
185
186                 foreach ($element_array as $entry) {
187                         if (isset($entry[$key]) && isset($entry[$type]) && ($entry[$type] == $type_value)) {
188                                 return $entry[$key];
189                         }
190                 }
191
192                 return null;
193         }
194 }