4 * Description: Replaces links to twitter.com to a nitter server in all displays of postings on a node.
6 * Author: Tobias Diekershoff <tobias@social.diekershoff.de>
8 * Copyright (c) 2020 Tobias Diekershoff
10 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
11 * associated documentation files (the "Software"), to deal in the Software without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
16 * The above copyright notice and this permission notice shall be included in all copies or substantial
17 * portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
20 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 use Friendica\Core\Hook;
28 use Friendica\Core\Renderer;
31 function nitter_install()
33 Hook::register('prepare_body_final', 'addon/nitter/nitter.php', 'nitter_render');
36 /* Handle the send data from the admin settings
38 function nitter_addon_admin_post()
40 DI::config()->set('nitter', 'server', rtrim(trim($_POST['nitterserver']), '/'));
43 /* Hook into the admin settings to let the admin choose a
44 * nitter server to use for the replacement.
46 function nitter_addon_admin(string &$o)
48 $nitterserver = DI::config()->get('nitter', 'server');
49 $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/nitter/');
50 $o = Renderer::replaceMacros($t, [
51 '$settingdescription' => DI::l10n()->t('Which nitter server shall be used for the replacements in the post bodies? Use the URL with servername and protocol. See %s for a list of available public Nitter servers.', 'https://github.com/zedeus/nitter/wiki/Instances'),
52 '$nitterserver' => ['nitterserver', DI::l10n()->t('Nitter server'), $nitterserver, 'https://example.com'],
53 '$submit' => DI::l10n()->t('Save Settings'),
58 * replace "twitter.com" with "nitter.net"
60 function nitter_render(array &$b)
62 // this needs to be a system setting
64 $nitter = DI::config()->get('nitter', 'server', 'https://nitter.net');
65 if (strstr($b['html'], 'https://mobile.twitter.com')) {
66 $b['html'] = str_replace('https://mobile.twitter.com', $nitter, $b['html']);
69 if (strstr($b['html'], 'https://twitter.com')) {
70 $b['html'] = str_replace('https://twitter.com', $nitter, $b['html']);
74 $b['html'] .= '<hr><p><small>' . DI::l10n()->t('(Nitter addon enabled: Twitter links via %s)', $nitter) . '</small></p>';