]> git.mxchange.org Git - friendica-addons.git/blob - nitter/nitter.php
Merge pull request #1173 from MrPetovan/task/10734-twitter-unfollow
[friendica-addons.git] / nitter / nitter.php
1 <?php
2 /*
3  * Name: nitter
4  * Description: Replaces links to twitter.com to a nitter server in all displays of postings on a node.
5  * Version: 2.0
6  * Author: Tobias Diekershoff <tobias@social.diekershoff.de>
7  *
8  * Copyright (c) 2020 Tobias Diekershoff
9  *
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:
15  * 
16  * The above copyright notice and this permission notice shall be included in all copies or substantial
17  * portions of the Software.
18  * 
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.
24  */
25
26 use Friendica\App;
27 use Friendica\Core\Addon;
28 use Friendica\Core\Renderer;
29 use Friendica\DI;
30
31 function nitter_install()
32 {
33         Addon::registerHook ('prepare_body_final', 'addon/nitter/nitter.php', 'nitter_render');
34 }
35
36 /* Handle the send data from the admin settings
37  */
38 function nitter_addon_admin_post(App $a)
39 {
40         $nitterserver = rtrim(trim($_POST['nitterserver']),'/');
41         DI::config()->set('nitter', 'server', $nitterserver);
42 }
43
44 /* Hook into the admin settings to let the admin choose a
45  * nitter server to use for the replacement.
46  */
47 function nitter_addon_admin(App $a, &$o)
48 {
49         $nitterserver = DI::config()->get('nitter', 'server');
50         $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/nitter/');
51         $o = Renderer::replaceMacros($t, [
52                 '$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'),
53                 '$nitterserver' => ['nitterserver', DI::l10n()->t('Nitter server'), $nitterserver, 'http://example.com'], 
54                 '$submit' => DI::l10n()->t('Save Settings'),
55         ]);
56 }
57
58 /*
59  *  replace "twitter.com" with "nitter.net"
60  */
61 function nitter_render(&$a, &$o)
62 {
63         // this needs to be a system setting
64         $replaced = false;
65         $nitter = DI::config()->get('nitter', 'server', 'https://nitter.net');
66         if (strstr($o['html'], 'https://mobile.twitter.com')) {
67                 $o['html'] = str_replace('https://mobile.twitter.com', $nitter, $o['html']);
68                 $replaced = true;
69         }
70         if (strstr($o['html'], 'https://twitter.com')) {
71                 $o['html'] = str_replace('https://twitter.com', $nitter, $o['html']);
72                 $replaced = true;
73         }
74         if ($replaced) {
75                 $o['html'] .= '<hr><p>' . DI::l10n()->t('In an attempt to protect your privacy, links to Twitter in this posting were replaced by links to the Nitter instance at %s', $nitter) . '</p>';
76         }
77 }