Cleaned code
[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 digitalbazaar\jsonld as DBJsonLD;
9
10 /**
11  * @brief This class contain methods to work with JsonLD data
12  */
13 class JsonLD
14 {
15         public static function documentLoader($url)
16         {
17                 $recursion = 0;
18
19                 $x = debug_backtrace();
20                 if ($x) {
21                         foreach ($x as $n) {
22                                 if ($n['function'] === __FUNCTION__)  {
23                                         $recursion ++;
24                                 }
25                         }
26                 }
27
28                 if ($recursion > 5) {
29                         logger('jsonld bomb detected at: ' . $url);
30                         exit();
31                 }
32
33                 $result = Cache::get('documentLoader:' . $url);
34                 if (!is_null($result)) {
35                         return $result;
36                 }
37
38                 $data = jsonld_default_document_loader($url);
39                 Cache::set('documentLoader:' . $url, $data, CACHE_DAY);
40                 return $data;
41         }
42
43         public static function normalize($json)
44         {
45                 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
46
47                 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
48
49                 return jsonld_normalize($jsonobj, array('algorithm' => 'URDNA2015', 'format' => 'application/nquads'));
50         }
51
52         public static function compact($json)
53         {
54                 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
55
56                 $context = (object)['as' => 'https://www.w3.org/ns/activitystreams',
57                         'w3sec' => 'https://w3id.org/security',
58                         'ostatus' => (object)['@id' => 'http://ostatus.org#', '@type' => '@id'],
59                         'vcard' => (object)['@id' => 'http://www.w3.org/2006/vcard/ns#', '@type' => '@id'],
60                         'uuid' => (object)['@id' => 'http://schema.org/identifier', '@type' => '@id']];
61
62                 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
63
64                 $compacted = jsonld_compact($jsonobj, $context);
65
66                 return json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);
67         }
68
69         public static function fetchElement($array, $element, $key, $type = null, $type_value = null)
70         {
71                 if (empty($array)) {
72                         return false;
73                 }
74
75                 if (empty($array[$element])) {
76                         return false;
77                 }
78
79                 if (is_string($array[$element])) {
80                         return $array[$element];
81                 }
82
83                 if (is_null($type_value)) {
84                         if (!empty($array[$element][$key])) {
85                                 return $array[$element][$key];
86                         }
87
88                         if (!empty($array[$element][0][$key])) {
89                                 return $array[$element][0][$key];
90                         }
91
92                         return false;
93                 }
94
95                 if (!empty($array[$element][$key]) && !empty($array[$element][$type]) && ($array[$element][$type] == $type_value)) {
96                         return $array[$element][$key];
97                 }
98
99                 /// @todo Add array search
100
101                 return false;
102         }
103 }