]> git.mxchange.org Git - friendica-addons.git/blob - nitter/nitter.php
Merge pull request 'Bluesky/Tumblr: Set "received" to "created" if fetched after...
[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\Hook;
28 use Friendica\Core\Renderer;
29 use Friendica\DI;
30
31 function nitter_install()
32 {
33         Hook::register('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()
39 {
40         DI::config()->set('nitter', 'server', rtrim(trim($_POST['nitterserver']), '/'));
41 }
42
43 /* Hook into the admin settings to let the admin choose a
44  * nitter server to use for the replacement.
45  */
46 function nitter_addon_admin(string &$o)
47 {
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'),
54         ]);
55 }
56
57 /*
58  *  replace "twitter.com" with "nitter.net"
59  */
60 function nitter_render(array &$b)
61 {
62         // this needs to be a system setting
63         $replaced = false;
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']);
67                 $replaced = true;
68         }
69         if (strstr($b['html'], 'https://twitter.com')) {
70                 $b['html'] = str_replace('https://twitter.com', $nitter, $b['html']);
71                 $replaced = true;
72         }
73         if ($replaced) {
74                 $b['html'] .= '<hr><p><small>' . DI::l10n()->t('(Nitter addon enabled: Twitter links via %s)', $nitter) . '</small></p>';
75         }
76 }