2023-12-03 13:07:45 +01:00
< ? php
/*
* Name : invidious
* Description : Replaces links to youtube . com to an invidious instance in all displays of postings on a node .
2024-04-14 19:45:06 +02:00
* Version : 0.4
2023-12-05 22:40:49 +01:00
* Author : Matthias Ebers < https :// loma . ml / profile / feb >
2023-12-18 18:47:19 +01:00
* Author : Michael Vogel < https :// pirati . ca / profile / heluecht >
2024-03-19 17:56:08 +01:00
* Status : Unsupported
* Note : Please use the URL Replace addon instead
2023-12-03 13:07:45 +01:00
*/
use Friendica\Core\Hook ;
use Friendica\Core\Renderer ;
use Friendica\DI ;
2023-12-18 18:47:19 +01:00
CONST INVIDIOUS_DEFAULT = 'https://invidio.us' ;
2023-12-03 13:07:45 +01:00
function invidious_install ()
{
2023-12-18 18:51:34 +01:00
Hook :: register ( 'prepare_body_final' , __FILE__ , 'invidious_render' );
2023-12-18 18:47:19 +01:00
Hook :: register ( 'addon_settings' , __FILE__ , 'invidious_settings' );
Hook :: register ( 'addon_settings_post' , __FILE__ , 'invidious_settings_post' );
2023-12-03 13:07:45 +01:00
}
/* Handle the send data from the admin settings
*/
function invidious_addon_admin_post ()
{
2023-12-18 18:51:34 +01:00
DI :: config () -> set ( 'invidious' , 'server' , trim ( $_POST [ 'invidiousserver' ], " \n \r \t \ v \x00 / " ));
2023-12-03 13:07:45 +01:00
}
/* Hook into the admin settings to let the admin choose an
* invidious server to use for the replacement .
*/
function invidious_addon_admin ( string & $o )
{
2023-12-18 18:51:34 +01:00
$invidiousserver = DI :: config () -> get ( 'invidious' , 'server' , INVIDIOUS_DEFAULT );
$t = Renderer :: getMarkupTemplate ( 'admin.tpl' , 'addon/invidious/' );
$o = Renderer :: replaceMacros ( $t , [
'$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' ),
'$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>' )],
'$submit' => DI :: l10n () -> t ( 'Save Settings' ),
]);
2023-12-03 13:07:45 +01:00
}
2023-12-18 18:47:19 +01:00
function invidious_settings ( array & $data )
{
if ( ! DI :: userSession () -> getLocalUserId ()) {
return ;
}
$enabled = DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'invidious' , 'enabled' );
$server = DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'invidious' , 'server' , DI :: config () -> get ( 'invidious' , 'server' , INVIDIOUS_DEFAULT ));
$t = Renderer :: getMarkupTemplate ( 'settings.tpl' , 'addon/invidious/' );
$html = Renderer :: replaceMacros ( $t , [
'$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.' )],
2023-12-18 18:51:34 +01:00
'$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>' )],
2023-12-18 18:47:19 +01:00
]);
$data = [
'addon' => 'invidious' ,
'title' => DI :: l10n () -> t ( 'Invidious Settings' ),
'html' => $html ,
];
}
function invidious_settings_post ( array & $b )
{
if ( ! DI :: userSession () -> getLocalUserId () || empty ( $_POST [ 'invidious-submit' ])) {
return ;
}
2023-12-18 18:51:34 +01:00
DI :: pConfig () -> set ( DI :: userSession () -> getLocalUserId (), 'invidious' , 'enabled' , ( bool ) $_POST [ 'enabled' ]);
2023-12-18 18:47:19 +01:00
2023-12-18 18:51:34 +01:00
$server = trim ( $_POST [ 'server' ], " \n \r \t \ v \x00 / " );
if ( $server != DI :: config () -> get ( 'invidious' , 'server' , INVIDIOUS_DEFAULT ) && ! empty ( $server )) {
DI :: pConfig () -> set ( DI :: userSession () -> getLocalUserId (), 'invidious' , 'server' , $server );
} else {
DI :: pConfig () -> delete ( DI :: userSession () -> getLocalUserId (), 'invidious' , 'server' );
}
2023-12-18 18:47:19 +01:00
}
2023-12-03 13:07:45 +01:00
/*
* replace " youtube.com " with the chosen Invidious instance
*/
function invidious_render ( array & $b )
{
2023-12-18 18:51:34 +01:00
if ( ! DI :: userSession () -> getLocalUserId () || ! DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'invidious' , 'enabled' )) {
return ;
}
2023-12-18 18:47:19 +01:00
2023-12-18 18:51:34 +01:00
$original = $b [ 'html' ];
$server = DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'invidious' , 'server' , DI :: config () -> get ( 'invidious' , 'server' , INVIDIOUS_DEFAULT ));
2024-03-19 17:56:08 +01:00
2024-04-15 06:02:58 +02:00
$b [ 'html' ] = preg_replace ( " ~https?://(?:www \ .)?youtube \ .com/watch \ ?v=(.*?)~ism " , $server . '/watch?v=$1' , $b [ 'html' ]);
$b [ 'html' ] = preg_replace ( " ~https?://(?:www \ .)?youtube \ .com/embed/(.*?)~ism " , $server . '/embed/$1' , $b [ 'html' ]);
$b [ 'html' ] = preg_replace ( " ~https?://(?:www \ .)?youtube \ .com/shorts/(.*?)~ism " , $server . '/shorts/$1' , $b [ 'html' ]);
2023-12-18 18:51:34 +01:00
$b [ 'html' ] = preg_replace ( " /https?: \ / \ /youtu.be \ /(.*?)/ism " , $server . '/watch?v=$1' , $b [ 'html' ]);
2023-12-18 18:47:19 +01:00
2023-12-18 18:51:34 +01:00
if ( $original != $b [ 'html' ]) {
$b [ 'html' ] .= '<hr><p><small>' . DI :: l10n () -> t ( '(Invidious addon enabled: YouTube links via %s)' , $server ) . '</small></p>' ;
}
2023-12-03 13:07:45 +01:00
}