diff --git a/rt_replace/LICENSE.md b/rt_replace/LICENSE.md new file mode 100644 index 00000000..09dc8f64 --- /dev/null +++ b/rt_replace/LICENSE.md @@ -0,0 +1,21 @@ +# MIT License + +Copyright © 2024 Dr. Tobias Quathamer + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/rt_replace/README.md b/rt_replace/README.md new file mode 100644 index 00000000..ddbf7161 --- /dev/null +++ b/rt_replace/README.md @@ -0,0 +1,17 @@ +# URL replace + +This addon will replace all occurrences of specified URLs with the address of +alternative servers in all displayed postings on a Friendica node. + +You can use this to switch from Twitter (or X) to a nitter instance, from +Instagram to an proxigram instance, or from some news sites to 12ft.io. + +Note: If you are using the twitter connector on your server, the links to the +contacts profile pages will not be replaced by this addon. Only links in the +body of the postings are affected. + +## Why + +- Access a website without JavaScript enabled to prevent JavaScript analytics + and potential IP-based tracking +- Avoid seeing ads on YouTube videos diff --git a/rt_replace/admin.tpl b/rt_replace/admin.tpl new file mode 100644 index 00000000..f8e588af --- /dev/null +++ b/rt_replace/admin.tpl @@ -0,0 +1,5 @@ +{{include file="field_input.tpl" field=$nitter_server}} +{{include file="field_input.tpl" field=$instagram_server}} +{{include file="field_textarea.tpl" field=$twelvefeet_sites}} + +
diff --git a/rt_replace/messages.po b/rt_replace/messages.po new file mode 100644 index 00000000..8112cb76 --- /dev/null +++ b/rt_replace/messages.po @@ -0,0 +1,51 @@ +# ADDON rt_replace +# Copyright (C) +# This file is distributed under the same license as the Friendica rt_replace addon package. +# +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: \n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2024-03-09 14:23+0100\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" + +#: rt_replace.php:57 +msgid "Nitter server" +msgstr "" + +#: rt_replace.php:59 +msgid "Specify the URL with protocol. The default is https://nitter.net." +msgstr "" + +#: rt_replace.php:65 +msgid "Instagram server" +msgstr "" + +#: rt_replace.php:67 +msgid "" +"Specify the URL with protocol. The default is https://proxigram.lunar.icu." +msgstr "" + +#: rt_replace.php:73 +msgid "Sites which are accessed through 12ft.io" +msgstr "" + +#: rt_replace.php:75 +msgid "Specify the URLs with protocol, one per line." +msgstr "" + +#: rt_replace.php:79 +msgid "Save settings" +msgstr "" + +#: rt_replace.php:127 +msgid "(URL replace addon enabled for X, Instagram and some news sites.)" +msgstr "" diff --git a/rt_replace/rt_replace.php b/rt_replace/rt_replace.php new file mode 100644 index 00000000..39105dca --- /dev/null +++ b/rt_replace/rt_replace.php @@ -0,0 +1,129 @@ + + * Author: Matthias Ebers + * Maintainer: Matthias Ebers + */ + +use Friendica\Core\Hook; +use Friendica\Core\Renderer; +use Friendica\DI; + +function rt_replace_install() +{ + Hook::register('prepare_body_final', 'addon/rt_replace/rt_replace.php', 'rt_replace_render'); +} + +/** + * Handle sent data from admin settings + */ +function rt_replace_addon_admin_post() +{ + DI::config()->set('rt_replace', 'nitter_server', rtrim(trim($_POST['nitter_server']), '/')); + DI::config()->set('rt_replace', 'instagram_server', rtrim(trim($_POST['instagram_server']), '/')); + // 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 + $twelvefeet_sites = array_map(fn($value): string => rtrim(trim(strtolower($value)), '/'), $twelvefeet_sites); + // Do not store empty lines or duplicates + $twelvefeet_sites = array_filter($twelvefeet_sites, fn($value): bool => !empty($value)); + $twelvefeet_sites = array_unique($twelvefeet_sites); + // Ensure a protocol and default to HTTPS + $twelvefeet_sites = array_map( + fn($value): string => substr($value, 0, 4) !== 'http' ? 'https://' . $value : $value, + $twelvefeet_sites + ); + asort($twelvefeet_sites); + DI::config()->set('rt_replace', 'twelvefeet_sites', $twelvefeet_sites); +} + +/** + * Hook into admin settings to enable choosing a different server + * for twitter, instagram, and news sites. + */ +function rt_replace_addon_admin(string &$o) +{ + $nitter_server = DI::config()->get('rt_replace', 'nitter_server'); + $instagram_server = DI::config()->get('rt_replace', 'instagram_server'); + $twelvefeet_sites = implode(PHP_EOL, DI::config()->get('rt_replace', 'twelvefeet_sites') ?? [] ?: []); + + $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/rt_replace/'); + $o = Renderer::replaceMacros($t, [ + '$nitter_server' => [ + 'nitter_server', + DI::l10n()->t('Nitter server'), + $nitter_server, + DI::l10n()->t('Specify the URL with protocol. The default is https://nitter.net.'), + null, + 'placeholder="https://nitter.net"', + ], + '$instagram_server' => [ + 'instagram_server', + DI::l10n()->t('Instagram server'), + $instagram_server, + DI::l10n()->t('Specify the URL with protocol. The default is https://proxigram.lunar.icu.'), + null, + 'placeholder="https://proxigram.lunar.icu"', + ], + '$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, + 'rows="6"' + ], + '$submit' => DI::l10n()->t('Save settings'), + ]); +} + +/** + * Replace proprietary URLs with their specified counterpart + */ +function rt_replace_render(array &$b) +{ + $replaced = false; + + $nitter_server = DI::config()->get('rt_replace', 'nitter_server'); + if (empty($nitter_server)) { + $nitter_server = 'https://nitter.net'; + } + + $instagram_server = DI::config()->get('rt_replace', 'instagram_server'); + if (empty($instagram_server)) { + $instagram_server = 'https://proxigram.lunar.icu'; + } + + // Handle some of twitter and instagram + $replacements = [ + 'https://mobile.twitter.com' => $nitter_server, + 'https://twitter.com' => $nitter_server, + 'https://mobile.x.com' => $nitter_server, + 'https://x.com' => $nitter_server, + 'https://www.instagram.com' => $instagram_server, + 'https://ig.me' => $instagram_server, + + ]; + foreach ($replacements as $server => $replacement) { + if (strpos($b['html'], $server) !== false) { + $b['html'] = str_replace($server, $replacement, $b['html']); + $replaced = true; + } + } + + $twelvefeet_sites = DI::config()->get('rt_replace', 'twelvefeet_sites') ?? [] ?: []; + foreach ($twelvefeet_sites as $twelvefeet_site) { + if (strpos($b['html'], $twelvefeet_site) !== false) { + $b['html'] = str_replace($twelvefeet_site, 'https://12ft.io/' . $twelvefeet_site, $b['html']); + $replaced = true; + } + } + + + if ($replaced) { + $b['html'] .= '

' . DI::l10n()->t('(URL replace addon enabled for X, Instagram and some news sites.)') . '

'; + } +}