2012-09-24 04:20:36 -04:00
< ? php
/**
* Name : FromApp
* Description : Change the displayed application you are posting from
* Version : 1.0
* Author : Commander Zot
*
*/
2021-11-20 04:56:55 -05:00
use Friendica\App ;
2018-12-26 02:28:16 -05:00
use Friendica\Core\Hook ;
2018-10-29 19:40:18 -04:00
use Friendica\Core\Logger ;
2021-11-20 04:56:55 -05:00
use Friendica\Core\Renderer ;
2019-12-29 21:55:10 -05:00
use Friendica\DI ;
2012-09-24 04:20:36 -04:00
2018-01-22 14:03:11 -05:00
function fromapp_install ()
{
2018-12-26 02:28:16 -05:00
Hook :: register ( 'post_local' , 'addon/fromapp/fromapp.php' , 'fromapp_post_hook' );
Hook :: register ( 'addon_settings' , 'addon/fromapp/fromapp.php' , 'fromapp_settings' );
Hook :: register ( 'addon_settings_post' , 'addon/fromapp/fromapp.php' , 'fromapp_settings_post' );
2021-10-21 02:04:27 -04:00
Logger :: notice ( " installed fromapp " );
2012-09-24 04:20:36 -04:00
}
2023-01-13 21:16:09 -05:00
function fromapp_settings_post ( $post )
2018-01-22 14:03:11 -05:00
{
2022-10-20 17:51:49 -04:00
if ( ! DI :: userSession () -> getLocalUserId () || empty ( $_POST [ 'fromapp-submit' ])) {
2012-09-24 04:20:36 -04:00
return ;
2018-01-22 14:03:11 -05:00
}
2012-09-24 04:20:36 -04:00
2022-10-20 17:51:49 -04:00
DI :: pConfig () -> set ( DI :: userSession () -> getLocalUserId (), 'fromapp' , 'app' , $_POST [ 'fromapp-input' ]);
DI :: pConfig () -> set ( DI :: userSession () -> getLocalUserId (), 'fromapp' , 'force' , intval ( $_POST [ 'fromapp-force' ]));
2012-09-24 04:20:36 -04:00
}
2023-01-13 21:16:09 -05:00
function fromapp_settings ( array & $data )
2018-01-22 14:03:11 -05:00
{
2022-10-20 17:51:49 -04:00
if ( ! DI :: userSession () -> getLocalUserId ()) {
2012-09-24 04:20:36 -04:00
return ;
2018-01-22 14:03:11 -05:00
}
2012-09-24 04:20:36 -04:00
2022-10-20 17:51:49 -04:00
$fromapp = DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'fromapp' , 'app' , '' );
$force = intval ( DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'fromapp' , 'force' ));
2021-11-20 04:56:55 -05:00
$t = Renderer :: getMarkupTemplate ( 'settings.tpl' , 'addon/fromapp/' );
$html = Renderer :: replaceMacros ( $t , [
'$fromapp' => [ 'fromapp-input' , DI :: l10n () -> t ( 'The application name you would like to show your posts originating from. Separate different app names with a comma. A random one will then be selected for every posting.' ), $fromapp ],
'$force' => [ 'fromapp-force' , DI :: l10n () -> t ( 'Use this application name even if another application was used.' ), $force ],
]);
$data = [
'addon' => 'fromapp' ,
'title' => DI :: l10n () -> t ( 'FromApp Settings' ),
'html' => $html ,
];
2012-09-24 04:20:36 -04:00
}
2023-01-13 21:16:09 -05:00
function fromapp_post_hook ( & $item )
2017-12-06 16:02:21 -05:00
{
2022-10-20 17:51:49 -04:00
if ( ! DI :: userSession () -> getLocalUserId ()) {
2017-12-06 16:02:21 -05:00
return ;
}
2012-09-24 04:20:36 -04:00
2022-10-20 17:51:49 -04:00
if ( DI :: userSession () -> getLocalUserId () != $item [ 'uid' ]) {
2017-12-06 16:02:21 -05:00
return ;
}
2012-09-24 04:20:36 -04:00
2022-10-20 17:51:49 -04:00
$app = DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'fromapp' , 'app' );
$force = intval ( DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'fromapp' , 'force' ));
2012-09-24 04:20:36 -04:00
2017-12-06 16:27:55 -05:00
if ( is_null ( $app ) || ( ! strlen ( $app ))) {
2017-12-06 16:02:21 -05:00
return ;
}
2012-09-24 04:20:36 -04:00
2017-12-06 16:27:55 -05:00
if ( strlen ( trim ( $item [ 'app' ])) && ( ! $force )) {
2012-09-24 20:21:30 -04:00
return ;
2017-12-06 16:02:21 -05:00
}
2012-09-24 20:21:30 -04:00
2017-12-06 16:27:55 -05:00
$apps = explode ( ',' , $app );
$item [ 'app' ] = trim ( $apps [ mt_rand ( 0 , count ( $apps ) - 1 )]);
2017-12-06 16:02:21 -05:00
2012-09-24 04:20:36 -04:00
return ;
2013-11-19 07:57:51 -05:00
}