]> git.mxchange.org Git - friendica.git/blob - src/Util/JsonLD.php
Merge pull request #7200 from annando/tag-process
[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 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 mixed the loaded data
22          * @throws \JsonLdException
23          */
24         public static function documentLoader($url)
25         {
26                 $recursion = 0;
27
28                 $x = debug_backtrace();
29                 if ($x) {
30                         foreach ($x as $n) {
31                                 if ($n['function'] === __FUNCTION__)  {
32                                         $recursion ++;
33                                 }
34                         }
35                 }
36
37                 if ($recursion > 5) {
38                         Logger::error('jsonld bomb detected at: ' . $url);
39                         exit();
40                 }
41
42                 $result = Cache::get('documentLoader:' . $url);
43                 if (!is_null($result)) {
44                         return $result;
45                 }
46
47                 $data = jsonld_default_document_loader($url);
48                 Cache::set('documentLoader:' . $url, $data, Cache::DAY);
49                 return $data;
50         }
51
52         /**
53          * @brief Normalises a given JSON array
54          *
55          * @param array $json
56          *
57          * @return mixed|bool normalized JSON string
58          * @throws Exception
59          */
60         public static function normalize($json)
61         {
62                 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
63
64                 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
65
66                 try {
67                         $normalized = jsonld_normalize($jsonobj, array('algorithm' => 'URDNA2015', 'format' => 'application/nquads'));
68                 }
69                 catch (Exception $e) {
70                         $normalized = false;
71                         $messages = [];
72                         $currentException = $e;
73                         do {
74                                 $messages[] = $currentException->getMessage();
75                         } while($currentException = $currentException->getPrevious());
76
77                         Logger::warning('JsonLD normalize error');
78                         Logger::notice('JsonLD normalize error', ['messages' => $messages]);
79                         Logger::info('JsonLD normalize error', ['trace' => $e->getTraceAsString()]);
80                         Logger::debug('JsonLD normalize error', ['jsonobj' => $jsonobj]);
81                 }
82
83                 return $normalized;
84         }
85
86         /**
87          * @brief Compacts a given JSON array
88          *
89          * @param array $json
90          *
91          * @return array Compacted JSON array
92          * @throws Exception
93          */
94         public static function compact($json)
95         {
96                 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
97
98                 $context = (object)['as' => 'https://www.w3.org/ns/activitystreams#',
99                         'w3id' => 'https://w3id.org/security#',
100                         'ldp' => (object)['@id' => 'http://www.w3.org/ns/ldp#', '@type' => '@id'],
101                         'vcard' => (object)['@id' => 'http://www.w3.org/2006/vcard/ns#', '@type' => '@id'],
102                         'dfrn' => (object)['@id' => 'http://purl.org/macgirvin/dfrn/1.0/', '@type' => '@id'],
103                         'diaspora' => (object)['@id' => 'https://diasporafoundation.org/ns/', '@type' => '@id'],
104                         'ostatus' => (object)['@id' => 'http://ostatus.org#', '@type' => '@id'],
105                         'dc' => (object)['@id' => 'http://purl.org/dc/terms/', '@type' => '@id'],
106                         'toot' => (object)['@id' => 'http://joinmastodon.org/ns#', '@type' => '@id'],
107                         'litepub' => (object)['@id' => 'http://litepub.social/ns#', '@type' => '@id']];
108
109                 // Preparation for adding possibly missing content to the context
110                 if (!empty($json['@context']) && is_string($json['@context'])) {
111                         $json['@context'] = [$json['@context']];
112                 }
113
114                 // Workaround for servers with missing context
115                 // See issue https://github.com/nextcloud/social/issues/330
116                 if (!empty($json['@context']) && is_array($json['@context'])) {
117                         $json['@context'][] = 'https://w3id.org/security/v1';
118                 }
119
120                 // Trying to avoid memory problems with large content fields
121                 if (!empty($json['object']['source']['content'])) {
122                         $content = $json['object']['source']['content'];
123                         $json['object']['source']['content'] = '';
124                 }
125
126                 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
127
128                 try {
129                         $compacted = jsonld_compact($jsonobj, $context);
130                 }
131                 catch (Exception $e) {
132                         $compacted = false;
133                         Logger::error('compacting error');
134                         // Sooner or later we should log some details as well - but currently this leads to memory issues
135                         // Logger::log('compacting error:' . substr(print_r($e, true), 0, 10000), Logger::DEBUG);
136                 }
137
138                 $json = json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);
139
140                 if (isset($json['as:object']['as:source']['as:content']) && !empty($content)) {
141                         $json['as:object']['as:source']['as:content'] = $content;
142                 }
143
144                 return $json;
145         }
146
147         /**
148          * @brief Fetches an element array from a JSON array
149          *
150          * @param $array
151          * @param $element
152          * @param $key
153          *
154          * @return array fetched element
155          */
156         public static function fetchElementArray($array, $element, $key = '@id')
157         {
158                 if (empty($array)) {
159                         return null;
160                 }
161
162                 if (!isset($array[$element])) {
163                         return null;
164                 }
165
166                 // If it isn't an array yet, make it to one
167                 if (!is_int(key($array[$element]))) {
168                         $array[$element] = [$array[$element]];
169                 }
170
171                 $elements = [];
172
173                 foreach ($array[$element] as $entry) {
174                         if (!is_array($entry)) {
175                                 $elements[] = $entry;
176                         } elseif (isset($entry[$key])) {
177                                 $elements[] = $entry[$key];
178                         } elseif (!empty($entry) || !is_array($entry)) {
179                                 $elements[] = $entry;
180                         }
181                 }
182
183                 return $elements;
184         }
185
186         /**
187          * @brief Fetches an element from a JSON array
188          *
189          * @param $array
190          * @param $element
191          * @param $key
192          * @param $type
193          * @param $type_value
194          *
195          * @return string fetched element
196          */
197         public static function fetchElement($array, $element, $key = '@id', $type = null, $type_value = null)
198         {
199                 if (empty($array)) {
200                         return null;
201                 }
202
203                 if (!isset($array[$element])) {
204                         return null;
205                 }
206
207                 if (!is_array($array[$element])) {
208                         return $array[$element];
209                 }
210
211                 if (is_null($type) || is_null($type_value)) {
212                         $element_array = self::fetchElementArray($array, $element, $key);
213                         if (is_null($element_array)) {
214                                 return null;
215                         }
216
217                         return array_shift($element_array);
218                 }
219
220                 $element_array = self::fetchElementArray($array, $element);
221                 if (is_null($element_array)) {
222                         return null;
223                 }
224
225                 foreach ($element_array as $entry) {
226                         if (isset($entry[$key]) && isset($entry[$type]) && ($entry[$type] == $type_value)) {
227                                 return $entry[$key];
228                         }
229                 }
230
231                 return null;
232         }
233 }