Merge pull request #5800 from JonnyTischbein/issue_return_path
[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 Exception;
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                 try {
64                         $normalized = jsonld_normalize($jsonobj, array('algorithm' => 'URDNA2015', 'format' => 'application/nquads'));
65                 }
66                 catch (Exception $e) {
67                         $normalized = false;
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 }