From 89d075c749b5b25dbc901c6d1337dcdad2c0670a Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 10 Jul 2021 18:35:05 +0200 Subject: unshorturl: replace with long url --- unshorturl/unshorturl.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/unshorturl/unshorturl.php b/unshorturl/unshorturl.php index e8c747a..f172b08 100644 --- a/unshorturl/unshorturl.php +++ b/unshorturl/unshorturl.php @@ -22,6 +22,23 @@ function unshorturl_uninstall() { Hook::unregister('prepare_body', 'addon/unshorturl/unshorturl.php', 'unshorturl_prepare_body'); } +function unshorturl_get_long_url($shorturl) { + $ch = curl_init($shorturl); + curl_setopt_array($ch, [ + CURLOPT_HEADER => true, + CURLOPT_NOBODY => true, + CURLOPT_RETURNTRANSFER => true + ]); + $res = curl_exec($ch); + + if ($res !== false) { + $matches = []; + if (preg_match('/^Location: (.*)$/', $res, $matches)) { + return $matches[1]; + } + } +} + function unshorturl_prepare_body(&$body) { if (!local_channel() || !Apps::addon_app_installed(local_channel(), 'unshorturl')) { return; @@ -33,7 +50,7 @@ function unshorturl_prepare_body(&$body) { foreach ($matches as $links) { error_log('unshorturl: Links found: ' . implode(', ', $links)); foreach($links as $l) { - $body['html'] = str_replace($l, '[filtered bit.ly url]', $body['html']); + $body['html'] = str_replace($l, unshorturl_get_long_url($l), $body['html']); } } } -- cgit v1.2.3