diff options
author | Mario <mario@mariovavti.com> | 2023-07-21 12:07:17 +0000 |
---|---|---|
committer | Mario <mario@mariovavti.com> | 2023-08-27 17:53:40 +0000 |
commit | 9b3e9dcf02fb3d458480ac14c1b88c01f3e8dddc (patch) | |
tree | 48d1eaab1529d15464a135abee132eca9922a090 /include/network.php | |
parent | f203fcc92ef1d1571547d1e7d9e30453a00a300c (diff) | |
download | volse-hubzilla-9b3e9dcf02fb3d458480ac14c1b88c01f3e8dddc.tar.gz volse-hubzilla-9b3e9dcf02fb3d458480ac14c1b88c01f3e8dddc.tar.bz2 volse-hubzilla-9b3e9dcf02fb3d458480ac14c1b88c01f3e8dddc.zip |
fix cached jsonld files fetched via network
Diffstat (limited to 'include/network.php')
-rw-r--r-- | include/network.php | 81 |
1 files changed, 49 insertions, 32 deletions
diff --git a/include/network.php b/include/network.php index b34fdffcc..79c28de01 100644 --- a/include/network.php +++ b/include/network.php @@ -2040,68 +2040,85 @@ function getBestSupportedMimeType($mimeTypes = null, $acceptedTypes = false) { return null; } + /** * @brief Perform caching for jsonld normaliser. * * @param string $url - * @return mixed|boolean|array + * @return mixed|bool|array */ function jsonld_document_loader($url) { - - switch ($url) { - case 'https://www.w3.org/ns/activitystreams': - $url = z_root() . '/library/w3org/activitystreams.jsonld'; - break; - case 'https://w3id.org/identity/v1': - $url = z_root() . '/library/w3org/identity-v1.jsonld'; - break; - case 'https://w3id.org/security/v1': - $url = z_root() . '/library/w3org/security-v1.jsonld'; - break; - default: - logger('URL: ' . $url, LOGGER_DEBUG); - break; - } - - require_once('library/jsonld/jsonld.php'); + $doc = (object) [ + 'contextUrl' => null, + 'document' => null, + 'documentUrl' => $url + ]; $recursion = 0; + $builtins = [ + 'https://www.w3.org/ns/activitystreams' => 'library/w3org/activitystreams.jsonld', + 'https://w3id.org/identity/v1' => 'library/w3org/identity-v1.jsonld', + 'https://w3id.org/security/v1' => 'library/w3org/security-v1.jsonld', + ]; + $x = debug_backtrace(); - if($x) { - foreach($x as $n) { - if($n['function'] === __FUNCTION__) { - $recursion ++; + if ($x) { + foreach ($x as $n) { + if ($n['function'] === __FUNCTION__) { + $recursion++; } } } - if($recursion > 5) { + if ($recursion > 5) { logger('jsonld bomb detected at: ' . $url); killme(); } + foreach ($builtins as $key => $value) { + if ($url === $key) { + $doc->document = file_get_contents($value); + return $doc; + } + } + $cachepath = 'store/[data]/ldcache'; - if(! is_dir($cachepath)) + if(!is_dir($cachepath)) { os_mkdir($cachepath, STORAGE_DEFAULT_PERMISSIONS, true); + } $filename = $cachepath . '/' . urlencode($url); - if(file_exists($filename) && filemtime($filename) > time() - (12 * 60 * 60)) { - return json_decode(file_get_contents($filename)); + + if (file_exists($filename) && filemtime($filename) > time() - (12 * 60 * 60)) { + logger('loading ' . $filename . ' from recent cache'); + + $doc->document = file_get_contents($filename); + return $doc; } $r = jsonld_default_document_loader($url); - if($r) { - file_put_contents($filename, json_encode($r)); + if ($r) { + if (!in_array($url, $builtins)) { + $cache_obj = $r; + // To prevent double encoding we need to decode $cache_obj->document + // before encoding the whole object for storage. + $cache_obj->document = json_decode($cache_obj->document); + file_put_contents($filename, json_encode($cache_obj)); + } return $r; } - logger('not found'); - if(file_exists($filename)) { - return json_decode(file_get_contents($filename)); + if (file_exists($filename)) { + logger('loading ' . $filename . ' from longterm cache'); + $doc->document = file_get_contents($filename); + return $doc; + } + else { + logger($filename . ' does not exist and cannot be loaded'); } - return []; + return $doc; } /** |