]> git.mxchange.org Git - friendica.git/blob - src/Util/JsonLD.php
4917c3c015dfc26bafdc6976ad00ded5753e97db
[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
10 /**
11  * @brief This class contain methods to work with JsonLD data
12  */
13 class JsonLD
14 {
15         /**
16          * @brief Loader for LD-JSON validation
17          *
18          * @param $url
19          *
20          * @return the loaded data
21          */
22         public static function documentLoader($url)
23         {
24                 $recursion = 0;
25
26                 $x = debug_backtrace();
27                 if ($x) {
28                         foreach ($x as $n) {
29                                 if ($n['function'] === __FUNCTION__)  {
30                                         $recursion ++;
31                                 }
32                         }
33                 }
34
35                 if ($recursion > 5) {
36                         logger('jsonld bomb detected at: ' . $url);
37                         exit();
38                 }
39
40                 $result = Cache::get('documentLoader:' . $url);
41                 if (!is_null($result)) {
42                         return $result;
43                 }
44
45                 $data = jsonld_default_document_loader($url);
46                 Cache::set('documentLoader:' . $url, $data, CACHE_DAY);
47                 return $data;
48         }
49
50         /**
51          * @brief Normalises a given JSON array
52          *
53          * @param array $json
54          *
55          * @return normalized JSON string
56          */
57         public static function normalize($json)
58         {
59                 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
60
61                 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
62
63                 return jsonld_normalize($jsonobj, array('algorithm' => 'URDNA2015', 'format' => 'application/nquads'));
64         }
65
66         /**
67          * @brief Compacts a given JSON array
68          *
69          * @param array $json
70          *
71          * @return comacted JSON array
72          */
73         public static function compact($json)
74         {
75                 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
76
77                 $context = (object)['as' => 'https://www.w3.org/ns/activitystreams',
78                         'w3sec' => 'https://w3id.org/security',
79                         'ostatus' => (object)['@id' => 'http://ostatus.org#', '@type' => '@id'],
80                         'vcard' => (object)['@id' => 'http://www.w3.org/2006/vcard/ns#', '@type' => '@id'],
81                         'uuid' => (object)['@id' => 'http://schema.org/identifier', '@type' => '@id']];
82
83                 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
84
85                 $compacted = jsonld_compact($jsonobj, $context);
86
87                 return json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);
88         }
89
90         /**
91          * @brief Fetches an element from a JSON array
92          *
93          * @param $array
94          * @param $element
95          * @param $key
96          * @param $type
97          * @param $type_value
98          *
99          * @return fetched element
100          */
101         public static function fetchElement($array, $element, $key, $type = null, $type_value = null)
102         {
103                 if (empty($array)) {
104                         return false;
105                 }
106
107                 if (empty($array[$element])) {
108                         return false;
109                 }
110
111                 if (is_string($array[$element])) {
112                         return $array[$element];
113                 }
114
115                 if (is_null($type_value)) {
116                         if (!empty($array[$element][$key])) {
117                                 return $array[$element][$key];
118                         }
119
120                         if (!empty($array[$element][0][$key])) {
121                                 return $array[$element][0][$key];
122                         }
123
124                         return false;
125                 }
126
127                 if (!empty($array[$element][$key]) && !empty($array[$element][$type]) && ($array[$element][$type] == $type_value)) {
128                         return $array[$element][$key];
129                 }
130
131                 /// @todo Add array search
132
133                 return false;
134         }
135 }