]> git.mxchange.org Git - friendica.git/blob - src/Util/JsonLD.php
Changed comment
[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 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('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                         logger('normalise error:' . print_r($e, true), LOGGER_DEBUG);
69                 }
70
71                 return $normalized;
72         }
73
74         /**
75          * @brief Compacts a given JSON array
76          *
77          * @param array $json
78          *
79          * @return comacted JSON array
80          */
81         public static function compact($json)
82         {
83                 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
84
85                 $context = (object)['as' => 'https://www.w3.org/ns/activitystreams',
86                         'w3sec' => 'https://w3id.org/security',
87                         'ostatus' => (object)['@id' => 'http://ostatus.org#', '@type' => '@id'],
88                         'vcard' => (object)['@id' => 'http://www.w3.org/2006/vcard/ns#', '@type' => '@id'],
89                         'uuid' => (object)['@id' => 'http://schema.org/identifier', '@type' => '@id']];
90
91                 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
92
93                 $compacted = jsonld_compact($jsonobj, $context);
94
95                 return json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);
96         }
97
98         /**
99          * @brief Fetches an element from a JSON array
100          *
101          * @param $array
102          * @param $element
103          * @param $key
104          * @param $type
105          * @param $type_value
106          *
107          * @return fetched element
108          */
109         public static function fetchElement($array, $element, $key, $type = null, $type_value = null)
110         {
111                 if (empty($array)) {
112                         return false;
113                 }
114
115                 if (empty($array[$element])) {
116                         return false;
117                 }
118
119                 if (is_string($array[$element])) {
120                         return $array[$element];
121                 }
122
123                 if (is_null($type_value)) {
124                         if (!empty($array[$element][$key])) {
125                                 return $array[$element][$key];
126                         }
127
128                         if (!empty($array[$element][0][$key])) {
129                                 return $array[$element][0][$key];
130                         }
131
132                         return false;
133                 }
134
135                 if (!empty($array[$element][$key]) && !empty($array[$element][$type]) && ($array[$element][$type] == $type_value)) {
136                         return $array[$element][$key];
137                 }
138
139                 /// @todo Add array search
140
141                 return false;
142         }
143 }