blob: 6340e9a0a66f0e8d14d99dee9e32bd192890417e (
plain) (
tree)
|
|
<?php
/**
* Name: UnshortURL
* Description: Expand shortened URLs into their proper URLs
* Version: 0.1.0
* Author: Harald Eilertsen <https://hub.volse.no/channel/harald>
* Maintainer: Harald Eilertsen <https://hub.volse.no/channel/harald>
*
* SPDX-FileCopyrightText: 2021 Harald Eilertsen <haraldei@anduin.net>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
use Zotlabs\Lib\Apps;
use Zotlabs\Extend\Hook;
function unshorturl_install() {
Hook::register('prepare_body', 'addon/unshorturl/unshorturl.php', 'unshorturl_prepare_body', 1);
}
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('/^(?:L|l)ocation: (.*)$/m', $res, $matches)) {
return $matches[1];
}
}
return false;
}
function unshorturl_prepare_body(&$body) {
if (!local_channel() || !Apps::addon_app_installed(local_channel(), 'unshorturl')) {
return;
}
$matches = [];
$num_matches = preg_match_all('/https?:\/\/bit\.ly\/\w+/', $body['html'], $matches);
if ($num_matches > 0) {
foreach ($matches as $links) {
error_log('unshorturl: Links found: ' . implode(', ', $links));
foreach($links as $l) {
$body['html'] = str_replace($l, unshorturl_get_long_url($l), $body['html']);
}
}
}
}
|