2024-01-05 00:12:20 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Name: URL Replace
|
|
|
|
* Description: Replaces occurrences of specified URLs with the address of alternative servers in all displays of postings on a node.
|
2024-03-13 22:57:16 +01:00
|
|
|
* Version: 1.1
|
2024-01-05 00:12:20 +01:00
|
|
|
* Author: Dr. Tobias Quathamer <https://social.anoxinon.de/@toddy>
|
|
|
|
* Maintainer: Dr. Tobias Quathamer <https://social.anoxinon.de/@toddy>
|
|
|
|
*/
|
2024-03-07 13:21:56 +01:00
|
|
|
|
2024-01-05 00:12:20 +01:00
|
|
|
use Friendica\Core\Hook;
|
|
|
|
use Friendica\Core\Renderer;
|
|
|
|
use Friendica\DI;
|
|
|
|
|
2024-03-14 09:31:58 +01:00
|
|
|
const URL_REPLACE_NITTER_DEFAULT = 'https://nitter.net';
|
2024-03-18 11:11:24 +01:00
|
|
|
const URL_REPLACE_INVIDIOUS_DEFAULT = 'https://invidio.us';
|
2024-03-14 09:31:58 +01:00
|
|
|
const URL_REPLACE_PROXIGRAM_DEFAULT = 'https://proxigram.lunar.icu';
|
2024-03-14 09:03:12 +01:00
|
|
|
|
2024-01-05 00:12:20 +01:00
|
|
|
function url_replace_install()
|
|
|
|
{
|
|
|
|
Hook::register('prepare_body_final', 'addon/url_replace/url_replace.php', 'url_replace_render');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle sent data from admin settings
|
|
|
|
*/
|
|
|
|
function url_replace_addon_admin_post()
|
|
|
|
{
|
2024-03-13 22:40:15 +01:00
|
|
|
DI::config()->set('url_replace', 'nitter_server_enabled', !empty($_POST['nitter_server_enabled']));
|
2024-03-19 17:56:08 +01:00
|
|
|
$nitter_server = rtrim(trim($_POST['nitter_server']), '/');
|
|
|
|
if ($nitter_server) {
|
|
|
|
DI::config()->set('url_replace', 'nitter_server', $nitter_server);
|
2024-03-16 11:33:48 +01:00
|
|
|
} else {
|
2024-03-19 17:56:08 +01:00
|
|
|
DI::config()->delete('url_replace', 'nitter_server');
|
2024-03-16 11:33:48 +01:00
|
|
|
}
|
2024-03-13 22:40:15 +01:00
|
|
|
DI::config()->set('url_replace', 'invidious_server_enabled', !empty($_POST['invidious_server_enabled']));
|
2024-03-19 17:56:08 +01:00
|
|
|
$invidious_server = rtrim(trim($_POST['invidious_server']), '/');
|
|
|
|
if ($invidious_server) {
|
|
|
|
DI::config()->set('url_replace', 'invidious_server', $invidious_server);
|
2024-03-16 11:33:48 +01:00
|
|
|
} else {
|
2024-03-19 17:56:08 +01:00
|
|
|
DI::config()->delete('url_replace', 'invidious_server');
|
2024-03-16 11:33:48 +01:00
|
|
|
}
|
2024-03-13 22:40:15 +01:00
|
|
|
DI::config()->set('url_replace', 'proxigram_server_enabled', !empty($_POST['proxigram_server_enabled']));
|
2024-03-19 17:56:08 +01:00
|
|
|
$proxigram_server = rtrim(trim($_POST['proxigram_server']), '/');
|
|
|
|
if ($proxigram_server) {
|
|
|
|
DI::config()->set('url_replace', 'proxigram_server', $proxigram_server);
|
2024-03-16 11:33:48 +01:00
|
|
|
} else {
|
2024-03-19 17:56:08 +01:00
|
|
|
DI::config()->delete('url_replace', 'proxigram_server');
|
2024-03-16 11:33:48 +01:00
|
|
|
}
|
2024-01-05 00:12:20 +01:00
|
|
|
// Convert twelvefeet_sites into an array before setting the new value
|
|
|
|
$twelvefeet_sites = explode(PHP_EOL, $_POST['twelvefeet_sites']);
|
|
|
|
// Normalize URLs by using lower case, removing a trailing slash and whitespace
|
2024-03-13 22:53:08 +01:00
|
|
|
$twelvefeet_sites = array_map(fn ($value): string => rtrim(trim(strtolower($value)), '/'), $twelvefeet_sites);
|
2024-01-05 00:12:20 +01:00
|
|
|
// Do not store empty lines or duplicates
|
2024-03-13 22:53:08 +01:00
|
|
|
$twelvefeet_sites = array_filter($twelvefeet_sites, fn ($value): bool => !empty($value));
|
2024-01-05 00:12:20 +01:00
|
|
|
$twelvefeet_sites = array_unique($twelvefeet_sites);
|
|
|
|
// Ensure a protocol and default to HTTPS
|
|
|
|
$twelvefeet_sites = array_map(
|
2024-03-13 22:53:08 +01:00
|
|
|
fn ($value): string => substr($value, 0, 4) !== 'http' ? 'https://' . $value : $value,
|
2024-01-05 00:12:20 +01:00
|
|
|
$twelvefeet_sites
|
|
|
|
);
|
|
|
|
asort($twelvefeet_sites);
|
|
|
|
DI::config()->set('url_replace', 'twelvefeet_sites', $twelvefeet_sites);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook into admin settings to enable choosing a different server
|
2024-03-19 17:56:08 +01:00
|
|
|
* for Twitter, YouTube, Instagram, and news sites.
|
2024-01-05 00:12:20 +01:00
|
|
|
*/
|
|
|
|
function url_replace_addon_admin(string &$o)
|
|
|
|
{
|
2024-03-19 17:56:08 +01:00
|
|
|
$nitter_server_enabled = DI::config()->get('url_replace', 'nitter_server_enabled') ?? true;
|
|
|
|
$nitter_server = DI::config()->get('url_replace', 'nitter_server') ?? '';
|
|
|
|
$invidious_server_enabled = DI::config()->get('url_replace', 'invidious_server_enabled') ?? true;
|
|
|
|
$invidious_server = DI::config()->get('url_replace', 'invidious_server') ?? '';
|
|
|
|
$proxigram_server_enabled = DI::config()->get('url_replace', 'proxigram_server_enabled') ?? true;
|
|
|
|
$proxigram_server = DI::config()->get('url_replace', 'proxigram_server') ?? '';
|
2024-03-13 22:53:08 +01:00
|
|
|
$twelvefeet_sites = implode(PHP_EOL, DI::config()->get('url_replace', 'twelvefeet_sites') ?? []);
|
2024-03-07 13:21:56 +01:00
|
|
|
|
|
|
|
$t = Renderer::getMarkupTemplate('admin.tpl', 'addon/url_replace/');
|
|
|
|
$o = Renderer::replaceMacros($t, [
|
2024-03-13 22:40:15 +01:00
|
|
|
'$nitter_server_enabled' => [
|
|
|
|
'nitter_server_enabled',
|
|
|
|
DI::l10n()->t('Replace links to X.'),
|
|
|
|
$nitter_server_enabled,
|
|
|
|
],
|
2024-01-05 00:12:20 +01:00
|
|
|
'$nitter_server' => [
|
|
|
|
'nitter_server',
|
|
|
|
DI::l10n()->t('Nitter server'),
|
|
|
|
$nitter_server,
|
2024-03-14 09:03:12 +01:00
|
|
|
DI::l10n()->t('Specify the URL with protocol. The default is %s.', URL_REPLACE_NITTER_DEFAULT),
|
2024-01-05 00:12:20 +01:00
|
|
|
null,
|
2024-03-16 20:26:38 +01:00
|
|
|
'placeholder="' . URL_REPLACE_NITTER_DEFAULT . '"',
|
2024-01-05 00:12:20 +01:00
|
|
|
],
|
2024-03-13 22:40:15 +01:00
|
|
|
'$invidious_server_enabled' => [
|
|
|
|
'invidious_server_enabled',
|
|
|
|
DI::l10n()->t('Replace links to YouTube.'),
|
|
|
|
$invidious_server_enabled,
|
|
|
|
],
|
2024-01-05 00:12:20 +01:00
|
|
|
'$invidious_server' => [
|
|
|
|
'invidious_server',
|
|
|
|
DI::l10n()->t('Invidious server'),
|
|
|
|
$invidious_server,
|
2024-03-14 09:03:12 +01:00
|
|
|
DI::l10n()->t('Specify the URL with protocol. The default is %s.', URL_REPLACE_INVIDIOUS_DEFAULT),
|
2024-01-05 00:12:20 +01:00
|
|
|
null,
|
2024-03-16 20:26:38 +01:00
|
|
|
'placeholder="' . URL_REPLACE_INVIDIOUS_DEFAULT . '"',
|
2024-01-05 00:12:20 +01:00
|
|
|
],
|
2024-03-13 22:40:15 +01:00
|
|
|
'$proxigram_server_enabled' => [
|
|
|
|
'proxigram_server_enabled',
|
|
|
|
DI::l10n()->t('Replace links to Instagram.'),
|
|
|
|
$proxigram_server_enabled,
|
|
|
|
],
|
2024-03-13 17:07:08 +01:00
|
|
|
'$proxigram_server' => [
|
|
|
|
'proxigram_server',
|
|
|
|
DI::l10n()->t('Proxigram server'),
|
|
|
|
$proxigram_server,
|
2024-03-14 09:03:12 +01:00
|
|
|
DI::l10n()->t('Specify the URL with protocol. The default is %s.', URL_REPLACE_PROXIGRAM_DEFAULT),
|
2024-03-13 17:07:08 +01:00
|
|
|
null,
|
2024-03-16 20:26:38 +01:00
|
|
|
'placeholder="' . URL_REPLACE_PROXIGRAM_DEFAULT . '"',
|
2024-03-13 17:07:08 +01:00
|
|
|
],
|
2024-01-05 00:12:20 +01:00
|
|
|
'$twelvefeet_sites' => [
|
|
|
|
'twelvefeet_sites',
|
|
|
|
DI::l10n()->t('Sites which are accessed through 12ft.io'),
|
|
|
|
$twelvefeet_sites,
|
|
|
|
DI::l10n()->t('Specify the URLs with protocol, one per line.'),
|
|
|
|
null,
|
2024-01-08 10:36:21 +01:00
|
|
|
'rows="6"'
|
2024-01-05 00:12:20 +01:00
|
|
|
],
|
|
|
|
'$submit' => DI::l10n()->t('Save settings'),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace proprietary URLs with their specified counterpart
|
|
|
|
*/
|
|
|
|
function url_replace_render(array &$b)
|
|
|
|
{
|
2024-03-18 11:05:09 +01:00
|
|
|
$replaced = false;
|
2024-03-13 22:40:15 +01:00
|
|
|
$replacements = [];
|
2024-01-05 00:12:20 +01:00
|
|
|
|
2024-03-16 20:26:38 +01:00
|
|
|
$nitter_server = DI::config()->get('url_replace', 'nitter_server') ?? URL_REPLACE_NITTER_DEFAULT;
|
|
|
|
$nitter_server_enabled = DI::config()->get('url_replace', 'nitter_server_enabled') ?? true;
|
2024-03-13 22:40:15 +01:00
|
|
|
if ($nitter_server_enabled) {
|
|
|
|
$replacements = array_merge($replacements, [
|
|
|
|
'https://mobile.twitter.com' => $nitter_server,
|
|
|
|
'https://twitter.com' => $nitter_server,
|
|
|
|
'https://mobile.x.com' => $nitter_server,
|
|
|
|
'https://x.com' => $nitter_server,
|
|
|
|
]);
|
|
|
|
}
|
2024-01-05 00:12:20 +01:00
|
|
|
|
2024-03-16 20:26:38 +01:00
|
|
|
$invidious_server = DI::config()->get('url_replace', 'invidious_server') ?? URL_REPLACE_INVIDIOUS_DEFAULT;
|
|
|
|
$invidious_server_enabled = DI::config()->get('url_replace', 'invidious_server_enabled') ?? true;
|
2024-03-13 22:40:15 +01:00
|
|
|
if ($invidious_server_enabled) {
|
|
|
|
$replacements = array_merge($replacements, [
|
2024-03-13 22:53:08 +01:00
|
|
|
'https://www.youtube.com' => $invidious_server,
|
|
|
|
'https://youtube.com' => $invidious_server,
|
|
|
|
'https://m.youtube.com' => $invidious_server,
|
|
|
|
'https://youtu.be' => $invidious_server,
|
2024-03-13 22:40:15 +01:00
|
|
|
]);
|
|
|
|
}
|
2024-01-05 00:12:20 +01:00
|
|
|
|
2024-03-16 20:26:38 +01:00
|
|
|
$proxigram_server = DI::config()->get('url_replace', 'proxigram_server') ?? URL_REPLACE_PROXIGRAM_DEFAULT;
|
|
|
|
$proxigram_server_enabled = DI::config()->get('url_replace', 'proxigram_server_enabled') ?? true;
|
2024-03-13 22:40:15 +01:00
|
|
|
if ($proxigram_server_enabled) {
|
|
|
|
$replacements = array_merge($replacements, [
|
2024-03-13 22:53:08 +01:00
|
|
|
'https://www.instagram.com' => $proxigram_server,
|
|
|
|
'https://instagram.com' => $proxigram_server,
|
|
|
|
'https://ig.me' => $proxigram_server,
|
2024-03-13 22:40:15 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2024-01-05 00:12:20 +01:00
|
|
|
foreach ($replacements as $server => $replacement) {
|
|
|
|
if (strpos($b['html'], $server) !== false) {
|
|
|
|
$b['html'] = str_replace($server, $replacement, $b['html']);
|
2024-03-18 11:05:09 +01:00
|
|
|
$replaced = true;
|
2024-01-05 00:12:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 15:39:21 +01:00
|
|
|
$twelvefeet_sites = DI::config()->get('url_replace', 'twelvefeet_sites') ?? [];
|
2024-01-05 00:12:20 +01:00
|
|
|
foreach ($twelvefeet_sites as $twelvefeet_site) {
|
|
|
|
if (strpos($b['html'], $twelvefeet_site) !== false) {
|
2024-03-07 13:21:56 +01:00
|
|
|
$b['html'] = str_replace($twelvefeet_site, 'https://12ft.io/' . $twelvefeet_site, $b['html']);
|
2024-03-18 11:05:09 +01:00
|
|
|
$replaced = true;
|
2024-01-05 00:12:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-18 11:05:09 +01:00
|
|
|
if ($replaced) {
|
2024-03-13 17:07:08 +01:00
|
|
|
$b['html'] .= '<hr><p><small>' . DI::l10n()->t('(URL replace addon enabled for X, YouTube, Instagram and some news sites.)') . '</small></p>';
|
2024-01-05 00:12:20 +01:00
|
|
|
}
|
|
|
|
}
|