Merge pull request 'Invidious: The addon is now user definable' (#1449) from heluecht...
[friendica-addons.git/.git] / invidious / invidious.php
1 <?php
2 /*
3  * Name: invidious
4  * Description: Replaces links to youtube.com to an invidious instance in all displays of postings on a node.
5  * Version: 0.3
6  * Author: Matthias Ebers <https://loma.ml/profile/feb>
7  * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
8  *
9  */
10
11 use Friendica\Core\Hook;
12 use Friendica\Core\Renderer;
13 use Friendica\DI;
14
15 CONST INVIDIOUS_DEFAULT = 'https://invidio.us';
16
17 function invidious_install()
18 {
19         Hook::register('prepare_body_final',  __FILE__, 'invidious_render');
20         Hook::register('addon_settings',      __FILE__, 'invidious_settings');
21         Hook::register('addon_settings_post', __FILE__, 'invidious_settings_post');
22 }
23
24 /* Handle the send data from the admin settings
25  */
26 function invidious_addon_admin_post()
27 {
28         DI::config()->set('invidious', 'server', trim($_POST['invidiousserver'], " \n\r\t\v\x00/"));
29 }
30
31 /* Hook into the admin settings to let the admin choose an
32  * invidious server to use for the replacement.
33  */
34 function invidious_addon_admin(string &$o)
35 {
36         $invidiousserver = DI::config()->get('invidious', 'server', INVIDIOUS_DEFAULT);
37         $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/invidious/');
38         $o = Renderer::replaceMacros($t, [
39                 '$settingdescription' => DI::l10n()->t('Which Invidious 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 Invidious servers.', 'https://redirect.invidious.io'),
40                 '$invidiousserver'    => ['invidiousserver', DI::l10n()->t('Invidious server'), $invidiousserver, DI::l10n()->t('See %s for a list of available Invidious servers.', '<a href="https://api.invidious.io/">https://api.invidious.io/</a>')],
41                 '$submit'             => DI::l10n()->t('Save Settings'),
42         ]);
43 }
44
45 function invidious_settings(array &$data)
46 {
47         if (!DI::userSession()->getLocalUserId()) {
48                 return;
49         }
50
51         $enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'invidious', 'enabled');
52         $server  = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'invidious', 'server', DI::config()->get('invidious', 'server', INVIDIOUS_DEFAULT));
53
54         $t    = Renderer::getMarkupTemplate('settings.tpl', 'addon/invidious/');
55         $html = Renderer::replaceMacros($t, [
56                 '$enabled' => ['enabled', DI::l10n()->t('Replace Youtube links with links to an Invidious server'), $enabled, DI::l10n()->t('If enabled, Youtube links are replaced with the links to the specified Invidious server.')],
57                 '$server'  => ['server', DI::l10n()->t('Invidious server'), $server, DI::l10n()->t('See %s for a list of available Invidious servers.', '<a href="https://api.invidious.io/">https://api.invidious.io/</a>')],
58         ]);
59
60         $data = [
61                 'addon' => 'invidious',
62                 'title' => DI::l10n()->t('Invidious Settings'),
63                 'html'  => $html,
64         ];
65 }
66
67 function invidious_settings_post(array &$b)
68 {
69         if (!DI::userSession()->getLocalUserId() || empty($_POST['invidious-submit'])) {
70                 return;
71         }
72
73         DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'invidious', 'enabled', (bool)$_POST['enabled']);
74
75         $server = trim($_POST['server'], " \n\r\t\v\x00/");
76         if ($server != DI::config()->get('invidious', 'server', INVIDIOUS_DEFAULT) && !empty($server)) {
77                 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'invidious', 'server', $server);
78         } else {
79                 DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'invidious', 'server');
80         }
81 }
82
83 /*
84  *  replace "youtube.com" with the chosen Invidious instance
85  */
86 function invidious_render(array &$b)
87 {
88         if (!DI::userSession()->getLocalUserId() || !DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'invidious', 'enabled')) {
89                 return;
90         }
91
92         $original = $b['html'];
93         $server   = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'invidious', 'server', DI::config()->get('invidious', 'server', INVIDIOUS_DEFAULT));
94         
95         $b['html'] = preg_replace("/https?:\/\/www.youtube.com\/watch\?v\=(.*?)/ism", $server . '/watch?v=$1', $b['html']);
96         $b['html'] = preg_replace("/https?:\/\/www.youtube.com\/embed\/(.*?)/ism", $server . '/embed/$1', $b['html']);
97         $b['html'] = preg_replace("/https?:\/\/www.youtube.com\/shorts\/(.*?)/ism", $server . '/shorts/$1', $b['html']);
98         $b['html'] = preg_replace("/https?:\/\/youtu.be\/(.*?)/ism", $server . '/watch?v=$1', $b['html']);
99
100         if ($original != $b['html']) {
101                 $b['html'] .= '<hr><p><small>' . DI::l10n()->t('(Invidious addon enabled: YouTube links via %s)', $server) . '</small></p>';
102         }
103 }