2012-09-24 08:20:36 +00:00
< ? php
/**
* Name : FromApp
* Description : Change the displayed application you are posting from
* Version : 1.0
* Author : Commander Zot
*
*/
2018-12-26 07:28:16 +00:00
use Friendica\Core\Hook ;
2018-01-22 19:03:11 +00:00
use Friendica\Core\L10n ;
2018-10-29 23:40:18 +00:00
use Friendica\Core\Logger ;
2019-12-30 02:55:10 +00:00
use Friendica\DI ;
2012-09-24 08:20:36 +00:00
2018-01-22 19:03:11 +00:00
function fromapp_install ()
{
2018-12-26 07:28:16 +00: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' );
2018-10-29 23:40:18 +00:00
Logger :: log ( " installed fromapp " );
2012-09-24 08:20:36 +00:00
}
2018-01-22 19:03:11 +00:00
function fromapp_uninstall ()
{
2018-12-26 07:28:16 +00:00
Hook :: unregister ( 'post_local' , 'addon/fromapp/fromapp.php' , 'fromapp_post_hook' );
Hook :: unregister ( 'addon_settings' , 'addon/fromapp/fromapp.php' , 'fromapp_settings' );
Hook :: unregister ( 'addon_settings_post' , 'addon/fromapp/fromapp.php' , 'fromapp_settings_post' );
2018-10-29 23:40:18 +00:00
Logger :: log ( " removed fromapp " );
2012-09-24 08:20:36 +00:00
}
2018-01-22 19:03:11 +00:00
function fromapp_settings_post ( $a , $post )
{
2018-11-30 14:11:56 +00:00
if ( ! local_user () || empty ( $_POST [ 'fromapp-submit' ])) {
2012-09-24 08:20:36 +00:00
return ;
2018-01-22 19:03:11 +00:00
}
2012-09-24 08:20:36 +00:00
2020-01-18 15:54:49 +00:00
DI :: pConfig () -> set ( local_user (), 'fromapp' , 'app' , $_POST [ 'fromapp-input' ]);
DI :: pConfig () -> set ( local_user (), 'fromapp' , 'force' , intval ( $_POST [ 'fromapp-force' ]));
2012-09-25 00:21:30 +00:00
2020-01-18 19:52:33 +00:00
info ( DI :: l10n () -> t ( 'Fromapp settings updated.' ) . EOL );
2012-09-24 08:20:36 +00:00
}
2018-01-22 19:03:11 +00:00
function fromapp_settings ( & $a , & $s )
{
if ( ! local_user ()) {
2012-09-24 08:20:36 +00:00
return ;
2018-01-22 19:03:11 +00:00
}
2012-09-24 08:20:36 +00:00
/* Add our stylesheet to the page so we can make our settings look nice */
2019-12-30 20:53:43 +00:00
DI :: page ()[ 'htmlhead' ] .= '<link rel="stylesheet" type="text/css" href="' . DI :: baseUrl () -> get () . '/addon/fromapp/fromapp.css' . '" media="all" />' . " \r \n " ;
2012-09-24 08:20:36 +00:00
/* Get the current state of our config variable */
2020-01-18 15:50:56 +00:00
$fromapp = DI :: pConfig () -> get ( local_user (), 'fromapp' , 'app' , '' );
2012-09-25 00:21:30 +00:00
2020-01-18 15:50:56 +00:00
$force = intval ( DI :: pConfig () -> get ( local_user (), 'fromapp' , 'force' ));
2012-09-25 00:21:30 +00:00
$force_enabled = (( $force ) ? ' checked="checked" ' : '' );
2012-09-24 08:20:36 +00:00
/* Add some HTML to the existing form */
2013-12-01 10:51:30 +00:00
$s .= '<span id="settings_fromapp_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_fromapp_expanded\'); openClose(\'settings_fromapp_inflated\');">' ;
2020-01-18 19:52:33 +00:00
$s .= '<h3>' . DI :: l10n () -> t ( 'FromApp Settings' ) . '</h3>' ;
2013-12-01 10:51:30 +00:00
$s .= '</span>' ;
$s .= '<div id="settings_fromapp_expanded" class="settings-block" style="display: none;">' ;
$s .= '<span class="fakelink" onclick="openClose(\'settings_fromapp_expanded\'); openClose(\'settings_fromapp_inflated\');">' ;
2020-01-18 19:52:33 +00:00
$s .= '<h3>' . DI :: l10n () -> t ( 'FromApp Settings' ) . '</h3>' ;
2013-12-01 10:51:30 +00:00
$s .= '</span>' ;
2012-09-24 08:20:36 +00:00
$s .= '<div id="fromapp-wrapper">' ;
2020-01-18 19:52:33 +00:00
$s .= '<label id="fromapp-label" for="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.' ) . '</label>' ;
2012-09-25 00:21:30 +00:00
$s .= '<input id="fromapp-input" type="text" name="fromapp-input" value="' . $fromapp . '" ' . '/>' ;
$s .= '<div class="clear"></div>' ;
2020-01-18 19:52:33 +00:00
$s .= '<label id="fromapp-force-label" for="fromapp-force">' . DI :: l10n () -> t ( 'Use this application name even if another application was used.' ) . '</label>' ;
2012-09-25 00:21:30 +00:00
$s .= '<input id="fromapp-force" type="checkbox" name="fromapp-force" value="1" ' . $force_enabled . '/>' ;
2012-09-24 08:20:36 +00:00
$s .= '</div><div class="clear"></div>' ;
/* provide a submit button */
2020-01-18 19:52:33 +00:00
$s .= '<div class="settings-submit-wrapper" ><input type="submit" name="fromapp-submit" class="settings-submit" value="' . DI :: l10n () -> t ( 'Save Settings' ) . '" /></div></div>' ;
2012-09-24 08:20:36 +00:00
}
2017-12-06 21:27:55 +00:00
function fromapp_post_hook ( & $a , & $item )
2017-12-06 21:02:21 +00:00
{
2017-12-06 21:27:55 +00:00
if ( ! local_user ()) {
2017-12-06 21:02:21 +00:00
return ;
}
2012-09-24 08:20:36 +00:00
2017-12-06 21:27:55 +00:00
if ( local_user () != $item [ 'uid' ]) {
2017-12-06 21:02:21 +00:00
return ;
}
2012-09-24 08:20:36 +00:00
2020-01-18 15:50:56 +00:00
$app = DI :: pConfig () -> get ( local_user (), 'fromapp' , 'app' );
$force = intval ( DI :: pConfig () -> get ( local_user (), 'fromapp' , 'force' ));
2012-09-24 08:20:36 +00:00
2017-12-06 21:27:55 +00:00
if ( is_null ( $app ) || ( ! strlen ( $app ))) {
2017-12-06 21:02:21 +00:00
return ;
}
2012-09-24 08:20:36 +00:00
2017-12-06 21:27:55 +00:00
if ( strlen ( trim ( $item [ 'app' ])) && ( ! $force )) {
2012-09-25 00:21:30 +00:00
return ;
2017-12-06 21:02:21 +00:00
}
2012-09-25 00:21:30 +00:00
2017-12-06 21:27:55 +00:00
$apps = explode ( ',' , $app );
$item [ 'app' ] = trim ( $apps [ mt_rand ( 0 , count ( $apps ) - 1 )]);
2017-12-06 21:02:21 +00:00
2012-09-24 08:20:36 +00:00
return ;
2013-11-19 12:57:51 +00:00
}