]> git.mxchange.org Git - friendica.git/blob - src/Util/JsonLD.php
Merge pull request #6256 from MrPetovan/bug/6255-hide-connect-remote
[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 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                         'ldp' => (object)['@id' => 'http://www.w3.org/ns/ldp#', '@type' => '@id'],
89                         'vcard' => (object)['@id' => 'http://www.w3.org/2006/vcard/ns#', '@type' => '@id'],
90                         'dfrn' => (object)['@id' => 'http://purl.org/macgirvin/dfrn/1.0/', '@type' => '@id'],
91                         'diaspora' => (object)['@id' => 'https://diasporafoundation.org/ns/', '@type' => '@id'],
92                         'ostatus' => (object)['@id' => 'http://ostatus.org#', '@type' => '@id'],
93                         'dc' => (object)['@id' => 'http://purl.org/dc/terms/', '@type' => '@id'],
94                         'toot' => (object)['@id' => 'http://joinmastodon.org/ns#', '@type' => '@id']];
95
96                 // Trying to avoid memory problems with large content fields
97                 if (!empty($json['object']['source']['content'])) {
98                         $content = $json['object']['source']['content'];
99                         $json['object']['source']['content'] = '';
100                 }
101
102                 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
103
104                 try {
105                         $compacted = jsonld_compact($jsonobj, $context);
106                 }
107                 catch (Exception $e) {
108                         $compacted = false;
109                         Logger::log('compacting error:' . print_r($e, true), Logger::DEBUG);
110                 }
111
112                 $json = json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);
113
114                 if (isset($json['as:object']['as:source']['as:content']) && !empty($content)) {
115                         $json['as:object']['as:source']['as:content'] = $content;
116                 }
117
118                 return $json;
119         }
120
121         /**
122          * @brief Fetches an element array from a JSON array
123          *
124          * @param $array
125          * @param $element
126          * @param $key
127          *
128          * @return fetched element array
129          */
130         public static function fetchElementArray($array, $element, $key = '@id')
131         {
132                 if (empty($array)) {
133                         return null;
134                 }
135
136                 if (!isset($array[$element])) {
137                         return null;
138                 }
139
140                 // If it isn't an array yet, make it to one
141                 if (!is_int(key($array[$element]))) {
142                         $array[$element] = [$array[$element]];
143                 }
144
145                 $elements = [];
146
147                 foreach ($array[$element] as $entry) {
148                         if (!is_array($entry)) {
149                                 $elements[] = $entry;
150                         } elseif (!empty($entry[$key])) {
151                                 $elements[] = $entry[$key];
152                         } elseif (!empty($entry) || !is_array($entry)) {
153                                 $elements[] = $entry;
154                         }
155                 }
156
157                 return $elements;
158         }
159
160         /**
161          * @brief Fetches an element from a JSON array
162          *
163          * @param $array
164          * @param $element
165          * @param $key
166          * @param $type
167          * @param $type_value
168          *
169          * @return fetched element
170          */
171         public static function fetchElement($array, $element, $key = '@id', $type = null, $type_value = null)
172         {
173                 if (empty($array)) {
174                         return null;
175                 }
176
177                 if (!isset($array[$element])) {
178                         return null;
179                 }
180
181                 if (!is_array($array[$element])) {
182                         return $array[$element];
183                 }
184
185                 if (is_null($type) || is_null($type_value)) {
186                         $element_array = self::fetchElementArray($array, $element, $key);
187                         if (is_null($element_array)) {
188                                 return null;
189                         }
190
191                         return array_shift($element_array);
192                 }
193
194                 $element_array = self::fetchElementArray($array, $element);
195                 if (is_null($element_array)) {
196                         return null;
197                 }
198
199                 foreach ($element_array as $entry) {
200                         if (isset($entry[$key]) && isset($entry[$type]) && ($entry[$type] == $type_value)) {
201                                 return $entry[$key];
202                         }
203                 }
204
205                 return null;
206         }
207 }