2011-09-25 08:56:03 +00:00
< ? php
2017-12-08 05:18:01 +00:00
2011-09-25 08:56:03 +00:00
/**
2015-07-12 19:43:59 +00:00
* Name : GNU Social Connector
2015-11-14 08:22:32 +00:00
* Description : Bidirectional ( posting , relaying and reading ) connector for GNU Social .
2012-04-30 07:21:05 +00:00
* Version : 1.0 . 5
2013-06-14 17:40:10 +00:00
* Author : Tobias Diekershoff < https :// f . diekershoff . de / profile / tobias >
2013-04-01 07:02:38 +00:00
* Author : Michael Vogel < https :// pirati . ca / profile / heluecht >
2011-09-25 08:56:03 +00:00
*
2013-05-31 11:27:31 +00:00
* Copyright ( c ) 2011 - 2013 Tobias Diekershoff , Michael Vogel
* All rights reserved .
2011-09-25 08:56:03 +00:00
*
2013-05-31 11:27:31 +00:00
* Redistribution and use in source and binary forms , with or without
* modification , are permitted provided that the following conditions are met :
* * Redistributions of source code must retain the above copyright notice ,
* this list of conditions and the following disclaimer .
* * Redistributions in binary form must reproduce the above
* * copyright notice , this list of conditions and the following disclaimer in
* the documentation and / or other materials provided with the distribution .
* * Neither the name of the < organization > nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission .
2011-09-25 08:56:03 +00:00
*
2013-05-31 11:27:31 +00:00
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS " AS IS " AND
* ANY EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED TO , THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED . IN NO EVENT SHALL < COPYRIGHT HOLDER > BE LIABLE FOR ANY DIRECT ,
* INDIRECT , INCIDENTAL , SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES ( INCLUDING ,
* BUT NOT LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ; LOSS OF USE ,
* DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY , OR TORT ( INCLUDING NEGLIGENCE
* OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE , EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
2011-09-25 08:56:03 +00:00
*
*/
2013-01-01 22:18:15 +00:00
define ( 'STATUSNET_DEFAULT_POLL_INTERVAL' , 5 ); // given in minutes
2013-01-01 17:20:56 +00:00
2018-01-28 19:33:47 +00:00
require_once __DIR__ . DIRECTORY_SEPARATOR . 'library' . DIRECTORY_SEPARATOR . 'statusnetoauth.php' ;
2019-10-24 22:10:19 +00:00
2018-01-25 02:29:09 +00:00
use CodebirdSN\CodebirdSN ;
2017-12-08 05:18:01 +00:00
use Friendica\App ;
2018-01-27 01:30:38 +00:00
use Friendica\Content\Text\Plaintext ;
2018-12-26 07:28:16 +00:00
use Friendica\Core\Hook ;
2018-10-29 23:40:18 +00:00
use Friendica\Core\Logger ;
2018-10-31 14:55:15 +00:00
use Friendica\Core\Renderer ;
2021-11-04 20:32:16 +00:00
use Friendica\Core\System ;
2018-07-20 12:20:48 +00:00
use Friendica\Database\DBA ;
2019-12-15 23:28:30 +00:00
use Friendica\DI ;
2018-01-28 11:19:38 +00:00
use Friendica\Model\Item ;
2021-01-17 00:00:32 +00:00
use Friendica\Model\Post ;
2017-12-09 18:50:29 +00:00
use Friendica\Model\User ;
2017-12-08 05:18:01 +00:00
function statusnet_install ()
{
2011-09-25 08:56:03 +00:00
// we need some hooks, for the configuration and for sending tweets
2018-12-26 07:28:16 +00:00
Hook :: register ( 'connector_settings' , 'addon/statusnet/statusnet.php' , 'statusnet_settings' );
Hook :: register ( 'connector_settings_post' , 'addon/statusnet/statusnet.php' , 'statusnet_settings_post' );
Hook :: register ( 'notifier_normal' , 'addon/statusnet/statusnet.php' , 'statusnet_post_hook' );
Hook :: register ( 'hook_fork' , 'addon/statusnet/statusnet.php' , 'statusnet_hook_fork' );
Hook :: register ( 'post_local' , 'addon/statusnet/statusnet.php' , 'statusnet_post_local' );
Hook :: register ( 'jot_networks' , 'addon/statusnet/statusnet.php' , 'statusnet_jot_nets' );
2022-06-22 16:17:09 +00:00
Logger :: notice ( 'installed GNU Social' );
2011-09-25 08:56:03 +00:00
}
2023-01-14 02:16:09 +00:00
function statusnet_jot_nets ( array & $jotnets_fields )
2017-12-08 05:18:01 +00:00
{
2022-10-20 21:51:49 +00:00
if ( ! DI :: userSession () -> getLocalUserId ()) {
2011-09-25 08:56:03 +00:00
return ;
2017-12-08 05:18:01 +00:00
}
2011-09-25 08:56:03 +00:00
2022-10-20 21:51:49 +00:00
if ( DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'post' )) {
2019-03-25 02:44:50 +00:00
$jotnets_fields [] = [
'type' => 'checkbox' ,
'field' => [
'statusnet_enable' ,
2020-01-18 19:52:33 +00:00
DI :: l10n () -> t ( 'Post to GNU Social' ),
2022-10-20 21:51:49 +00:00
DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'post_by_default' )
2019-03-25 02:44:50 +00:00
]
];
2011-09-25 08:56:03 +00:00
}
}
2023-01-14 02:16:09 +00:00
function statusnet_settings_post ( $post )
2017-12-08 05:18:01 +00:00
{
2022-10-20 21:51:49 +00:00
if ( ! DI :: userSession () -> getLocalUserId ()) {
2014-04-04 09:10:01 +00:00
return ;
2017-12-08 05:18:01 +00:00
}
2015-07-12 19:43:59 +00:00
// don't check GNU Social settings if GNU Social submit button is not clicked
2021-11-21 22:10:13 +00:00
if ( empty ( $_POST [ 'statusnet-submit' ]) && empty ( $_POST [ 'statusnet-disconnect' ])) {
2014-04-04 09:10:01 +00:00
return ;
2017-12-08 05:18:01 +00:00
}
2014-03-16 19:50:36 +00:00
2021-11-21 22:10:13 +00:00
if ( ! empty ( $_POST [ 'statusnet-disconnect' ])) {
2017-12-08 05:18:01 +00:00
/* * *
2021-11-21 22:10:13 +00:00
* if the GNU Social - disconnect button is clicked , clear the GNU Social configuration
2014-04-04 09:10:01 +00:00
*/
2022-10-20 21:51:49 +00:00
DI :: pConfig () -> delete ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'consumerkey' );
DI :: pConfig () -> delete ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'consumersecret' );
DI :: pConfig () -> delete ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'post' );
DI :: pConfig () -> delete ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'post_by_default' );
DI :: pConfig () -> delete ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'oauthtoken' );
DI :: pConfig () -> delete ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'oauthsecret' );
DI :: pConfig () -> delete ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'baseapi' );
2011-09-25 08:56:03 +00:00
} else {
2017-12-08 05:18:01 +00:00
if ( isset ( $_POST [ 'statusnet-preconf-apiurl' ])) {
/* * *
* If the user used one of the preconfigured GNU Social server credentials
* use them . All the data are available in the global config .
* Check the API Url never the less and blame the admin if it ' s not working ^^
*/
2020-01-19 20:21:12 +00:00
$globalsn = DI :: config () -> get ( 'statusnet' , 'sites' );
2017-12-08 05:18:01 +00:00
foreach ( $globalsn as $asn ) {
if ( $asn [ 'apiurl' ] == $_POST [ 'statusnet-preconf-apiurl' ]) {
$apibase = $asn [ 'apiurl' ];
2021-08-25 19:54:54 +00:00
$c = DI :: httpClient () -> fetch ( $apibase . 'statusnet/version.xml' );
2017-12-08 05:18:01 +00:00
if ( strlen ( $c ) > 0 ) {
2022-10-20 21:51:49 +00:00
DI :: pConfig () -> set ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'consumerkey' , $asn [ 'consumerkey' ]);
DI :: pConfig () -> set ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'consumersecret' , $asn [ 'consumersecret' ]);
DI :: pConfig () -> set ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'baseapi' , $asn [ 'apiurl' ]);
//DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'statusnet', 'application_name', $asn['applicationname'] );
2017-12-08 05:18:01 +00:00
} else {
2022-10-18 12:28:46 +00:00
DI :: sysmsg () -> addNotice ( DI :: l10n () -> t ( 'Please contact your site administrator.<br />The provided API URL is not valid.' ) . '<br />' . $asn [ 'apiurl' ]);
2017-12-08 05:18:01 +00:00
}
2017-12-08 01:47:23 +00:00
}
2017-12-08 04:24:41 +00:00
}
} else {
2017-12-08 05:18:01 +00:00
if ( isset ( $_POST [ 'statusnet-consumersecret' ])) {
// check if we can reach the API of the GNU Social server
// we'll check the API Version for that, if we don't get one we'll try to fix the path but will
// resign quickly after this one try to fix the path ;-)
$apibase = $_POST [ 'statusnet-baseapi' ];
2021-08-25 19:54:54 +00:00
$c = DI :: httpClient () -> fetch ( $apibase . 'statusnet/version.xml' );
2017-12-08 05:18:01 +00:00
if ( strlen ( $c ) > 0 ) {
// ok the API path is correct, let's save the settings
2022-10-20 21:51:49 +00:00
DI :: pConfig () -> set ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'consumerkey' , $_POST [ 'statusnet-consumerkey' ]);
DI :: pConfig () -> set ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'consumersecret' , $_POST [ 'statusnet-consumersecret' ]);
DI :: pConfig () -> set ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'baseapi' , $apibase );
//DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'statusnet', 'application_name', $_POST['statusnet-applicationname'] );
2017-12-08 05:18:01 +00:00
} else {
// the API path is not correct, maybe missing trailing / ?
$apibase = $apibase . '/' ;
2021-08-25 19:54:54 +00:00
$c = DI :: httpClient () -> fetch ( $apibase . 'statusnet/version.xml' );
2017-12-08 05:18:01 +00:00
if ( strlen ( $c ) > 0 ) {
// ok the API path is now correct, let's save the settings
2022-10-20 21:51:49 +00:00
DI :: pConfig () -> set ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'consumerkey' , $_POST [ 'statusnet-consumerkey' ]);
DI :: pConfig () -> set ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'consumersecret' , $_POST [ 'statusnet-consumersecret' ]);
DI :: pConfig () -> set ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'baseapi' , $apibase );
2017-12-08 05:18:01 +00:00
} else {
// still not the correct API base, let's do noting
2022-10-17 21:00:03 +00:00
DI :: sysmsg () -> addNotice ( DI :: l10n () -> t ( 'We could not contact the GNU Social API with the Path you entered.' ));
2017-12-08 05:18:01 +00:00
}
}
2014-04-04 09:10:01 +00:00
} else {
2017-12-08 05:18:01 +00:00
if ( isset ( $_POST [ 'statusnet-pin' ])) {
// if the user supplied us with a PIN from GNU Social, let the magic of OAuth happen
2022-10-20 21:51:49 +00:00
$api = DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'baseapi' );
$ckey = DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'consumerkey' );
$csecret = DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'consumersecret' );
2017-12-08 05:18:01 +00:00
// the token and secret for which the PIN was generated were hidden in the settings
// form as token and token2, we need a new connection to GNU Social using these token
// and secret to request a Access Token with the PIN
$connection = new StatusNetOAuth ( $api , $ckey , $csecret , $_POST [ 'statusnet-token' ], $_POST [ 'statusnet-token2' ]);
$token = $connection -> getAccessToken ( $_POST [ 'statusnet-pin' ]);
// ok, now that we have the Access Token, save them in the user config
2022-10-20 21:51:49 +00:00
DI :: pConfig () -> set ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'oauthtoken' , $token [ 'oauth_token' ]);
DI :: pConfig () -> set ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'oauthsecret' , $token [ 'oauth_token_secret' ]);
DI :: pConfig () -> set ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'post' , 1 );
DI :: pConfig () -> set ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'post_taglinks' , 1 );
2017-12-08 05:18:01 +00:00
// reload the Addon Settings page, if we don't do it see Bug #42
} else {
// if no PIN is supplied in the POST variables, the user has changed the setting
// to post a dent for every new __public__ posting to the wall
2022-10-20 21:51:49 +00:00
DI :: pConfig () -> set ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'post' , intval ( $_POST [ 'statusnet-enable' ]));
DI :: pConfig () -> set ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'post_by_default' , intval ( $_POST [ 'statusnet-default' ]));
2017-12-08 05:18:01 +00:00
}
2014-04-04 09:10:01 +00:00
}
}
2017-12-08 05:18:01 +00:00
}
2011-09-25 08:56:03 +00:00
}
2017-12-08 05:18:01 +00:00
2023-01-14 02:16:09 +00:00
function statusnet_settings ( array & $data )
2017-12-08 05:18:01 +00:00
{
2022-10-20 21:51:49 +00:00
if ( ! DI :: userSession () -> getLocalUserId ()) {
2014-04-04 09:10:01 +00:00
return ;
2017-12-08 05:18:01 +00:00
}
2021-08-08 17:10:04 +00:00
2021-11-21 22:10:13 +00:00
DI :: page () -> registerStylesheet ( __DIR__ . '/statusnet.css' , 'all' );
2021-08-08 17:10:04 +00:00
2017-12-08 05:18:01 +00:00
/* * *
2011-09-25 08:56:03 +00:00
* 1 ) Check that we have a base api url and a consumer key & secret
* 2 ) If no OAuthtoken & stuff is present , generate button to get some
2014-04-04 09:10:01 +00:00
* allow the user to cancel the connection process at this step
2011-09-25 08:56:03 +00:00
* 3 ) Checkbox for " Send public notices (respect size limitation)
*/
2022-12-21 22:20:34 +00:00
$baseapi = DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'baseapi' );
$ckey = DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'consumerkey' );
$csecret = DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'consumersecret' );
$otoken = DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'oauthtoken' );
$osecret = DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'oauthsecret' );
$enabled = DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'post' , false );
$def_enabled = DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'post_by_default' , false );
2021-11-21 22:10:13 +00:00
// Radio button list to select existing application credentials
$sites = array_map ( function ( $site ) {
return [ 'statusnet-preconf-apiurl' , $site [ 'sitename' ], $site [ 'apiurl' ]];
}, DI :: config () -> get ( 'statusnet' , 'sites' , []));
$submit = [ 'statusnet-submit' => DI :: l10n () -> t ( 'Save Settings' )];
if ( $ckey && $csecret ) {
if ( $otoken && $osecret ) {
2017-12-08 05:18:01 +00:00
/* * *
2011-09-25 08:56:03 +00:00
* we have an OAuth key / secret pair for the user
2015-07-12 19:43:59 +00:00
* so let ' s give a chance to disable the postings to GNU Social
2011-09-25 08:56:03 +00:00
*/
2021-11-21 22:10:13 +00:00
$connection = new StatusNetOAuth ( $baseapi , $ckey , $csecret , $otoken , $osecret );
$account = $connection -> get ( 'account/verify_credentials' );
if ( ! empty ( $account )) {
$connected_account = DI :: l10n () -> t ( 'Currently connected to: <a href="%s" target="_statusnet">%s</a>' , $account -> statusnet_profile_url , $account -> screen_name );
2018-08-09 04:16:56 +00:00
}
2021-11-21 22:10:13 +00:00
2022-10-20 21:51:49 +00:00
$user = User :: getById ( DI :: userSession () -> getLocalUserId ());
2021-08-08 17:10:04 +00:00
if ( $user [ 'hidewall' ]) {
2021-11-21 22:10:13 +00:00
$privacy_warning = DI :: l10n () -> t ( '<strong>Note</strong>: Due your privacy settings (<em>Hide your profile details from unknown viewers?</em>) the link potentially included in public postings relayed to GNU Social will lead the visitor to a blank page informing the visitor that the access to your profile has been restricted.' );
2014-04-04 09:10:01 +00:00
}
2021-11-21 22:10:13 +00:00
$submit [ 'statusnet-disconnect' ] = DI :: l10n () -> t ( 'Clear OAuth configuration' );
} else {
/* * *
* the user has not yet connected the account to GNU Social
* get a temporary OAuth key / secret pair and display a button with
* which the user can request a PIN to connect the account to a
* account at GNU Social
2017-12-08 05:18:01 +00:00
*/
2021-11-21 22:10:13 +00:00
$connection = new StatusNetOAuth ( $baseapi , $ckey , $csecret );
$request_token = $connection -> getRequestToken ( 'oob' );
$authorize_url = $connection -> getAuthorizeURL ( $request_token [ 'oauth_token' ], false );
$submit [ 'statusnet-disconnect' ] = DI :: l10n () -> t ( 'Cancel GNU Social Connection' );
2011-09-25 08:56:03 +00:00
}
}
2021-11-21 22:10:13 +00:00
$t = Renderer :: getMarkupTemplate ( 'connector_settings.tpl' , 'addon/statusnet/' );
$html = Renderer :: replaceMacros ( $t , [
'$l10n' => [
'global_title' => DI :: l10n () -> t ( 'Globally Available GNU Social OAuthKeys' ),
'global_info' => DI :: l10n () -> t ( DI :: l10n () -> t ( 'There are preconfigured OAuth key pairs for some GNU Social servers available. If you are using one of them, please use these credentials. If not feel free to connect to any other GNU Social instance (see below).' )),
'credentials_title' => DI :: l10n () -> t ( 'Provide your own OAuth Credentials' ),
'credentials_info' => DI :: l10n () -> t ( 'No consumer key pair for GNU Social found. Register your Friendica Account as a desktop application on your GNU Social account, copy the consumer key pair here and enter the API base root.<br />Before you register your own OAuth key pair ask the administrator if there is already a key pair for this Friendica installation at your favorite GNU Social installation.' ),
'oauth_info' => DI :: l10n () -> t ( 'To connect to your GNU Social account click the button below to get a security code from GNU Social which you have to copy into the input box below and submit the form. Only your <strong>public</strong> posts will be posted to GNU Social.' ),
'oauth_alt' => DI :: l10n () -> t ( 'Log in with GNU Social' ),
'oauth_cancel' => DI :: l10n () -> t ( 'Cancel Connection Process' ),
'oauth_api' => DI :: l10n () -> t ( 'Current GNU Social API is: %s' , $baseapi ),
'connected_account' => $connected_account ? ? '' ,
'privacy_warning' => $privacy_warning ? ? '' ,
],
'$ckey' => $ckey ,
'$csecret' => $csecret ,
'$otoken' => $otoken ,
'$osecret' => $osecret ,
'$sites' => $sites ,
'$authorize_url' => $authorize_url ? ? '' ,
'$request_token' => $request_token ? ? null ,
'$account' => $account ? ? null ,
2023-02-18 19:57:09 +00:00
'$authenticate_url' => DI :: baseUrl () . '/statusnet/connect' ,
2021-11-21 22:10:13 +00:00
'$consumerkey' => [ 'statusnet-consumerkey' , DI :: l10n () -> t ( 'OAuth Consumer Key' ), '' , '' , false , ' size="35' ],
'$consumersecret' => [ 'statusnet-consumersecret' , DI :: l10n () -> t ( 'OAuth Consumer Secret' ), '' , '' , false , ' size="35' ],
'$baseapi' => [ 'statusnet-baseapi' , DI :: l10n () -> t ( 'Base API Path (remember the trailing /)' ), '' , '' , false , ' size="35' ],
'$pin' => [ 'statusnet-pin' , DI :: l10n () -> t ( 'Copy the security code from GNU Social here' )],
'$enable' => [ 'statusnet-enabled' , DI :: l10n () -> t ( 'Allow posting to GNU Social' ), $enabled , DI :: l10n () -> t ( 'If enabled all your <strong>public</strong> postings can be posted to the associated GNU Social account. You can choose to do so by default (here) or for every posting separately in the posting options when writing the entry.' )],
'$default' => [ 'statusnet-default' , DI :: l10n () -> t ( 'Post to GNU Social by default' ), $def_enabled ],
]);
$data = [
'connector' => 'statusnet' ,
'title' => DI :: l10n () -> t ( 'GNU Social Import/Export/Mirror' ),
'image' => 'images/gnusocial.png' ,
'enabled' => $enabled ,
'html' => $html ,
'submit' => $submit ,
];
2011-09-25 08:56:03 +00:00
}
2023-01-14 02:16:09 +00:00
function statusnet_hook_fork ( array & $b )
2018-11-10 16:20:19 +00:00
{
if ( $b [ 'name' ] != 'notifier_normal' ) {
return ;
}
$post = $b [ 'data' ];
2022-12-21 22:29:38 +00:00
if ( $post [ 'deleted' ] || ( $post [ 'created' ] !== $post [ 'edited' ]) || strpos ( $post [ 'postopts' ] ? ? '' , 'statusnet' ) === false || ( $post [ 'parent' ] != $post [ 'id' ]) || $post [ 'private' ]) {
2018-11-10 16:20:19 +00:00
$b [ 'execute' ] = false ;
return ;
}
}
2023-01-14 02:16:09 +00:00
function statusnet_post_local ( array & $b )
2017-12-08 05:18:01 +00:00
{
2017-09-06 16:16:33 +00:00
if ( $b [ 'edit' ]) {
2011-10-12 01:46:33 +00:00
return ;
2017-09-06 16:16:33 +00:00
}
2011-10-12 01:46:33 +00:00
2022-10-20 21:51:49 +00:00
if ( ! DI :: userSession () -> getLocalUserId () || ( DI :: userSession () -> getLocalUserId () != $b [ 'uid' ])) {
2017-09-06 16:16:33 +00:00
return ;
}
2011-10-12 01:46:33 +00:00
2022-10-20 21:51:49 +00:00
$statusnet_post = DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'post' );
2018-11-30 14:11:56 +00:00
$statusnet_enable = (( $statusnet_post && ! empty ( $_REQUEST [ 'statusnet_enable' ])) ? intval ( $_REQUEST [ 'statusnet_enable' ]) : 0 );
2011-10-12 01:46:33 +00:00
2017-09-06 16:16:33 +00:00
// if API is used, default to the chosen settings
2022-10-20 21:51:49 +00:00
if ( $b [ 'api_source' ] && intval ( DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'post_by_default' ))) {
2017-09-06 16:16:33 +00:00
$statusnet_enable = 1 ;
}
2011-10-12 01:46:33 +00:00
2017-09-06 16:16:33 +00:00
if ( ! $statusnet_enable ) {
return ;
}
2011-10-12 01:46:33 +00:00
2017-09-06 16:16:33 +00:00
if ( strlen ( $b [ 'postopts' ])) {
$b [ 'postopts' ] .= ',' ;
2014-04-04 09:10:01 +00:00
}
2017-09-06 16:16:33 +00:00
$b [ 'postopts' ] .= 'statusnet' ;
2011-10-12 01:46:33 +00:00
}
2023-01-14 02:16:09 +00:00
function statusnet_post_hook ( array & $b )
2017-12-08 05:18:01 +00:00
{
2011-09-25 08:56:03 +00:00
/**
2015-07-12 19:43:59 +00:00
* Post to GNU Social
2011-09-25 08:56:03 +00:00
*/
2022-12-21 22:20:34 +00:00
if ( $b [ 'deleted' ] || $b [ 'private' ] || ( $b [ 'created' ] !== $b [ 'edited' ])) {
return ;
2014-04-04 09:10:01 +00:00
}
2011-09-25 08:56:03 +00:00
2022-10-29 22:14:12 +00:00
$b [ 'body' ] = Post\Media :: addAttachmentsToBody ( $b [ 'uri-id' ], DI :: contentItem () -> addSharedPost ( $b ));
2021-05-01 08:59:14 +00:00
2022-06-22 16:17:09 +00:00
$api = DI :: pConfig () -> get ( $b [ 'uid' ], 'statusnet' , 'baseapi' );
2014-04-04 09:10:01 +00:00
$hostname = preg_replace ( " =https?://([ \ w \ .]*)/.*=ism " , " $ 1 " , $api );
2011-09-25 08:56:03 +00:00
2022-12-21 22:20:34 +00:00
if ( $b [ 'private' ] || ! strstr ( $b [ 'postopts' ], 'statusnet' )) {
2014-11-07 19:06:51 +00:00
return ;
2017-12-08 05:18:01 +00:00
}
2014-11-07 19:06:51 +00:00
2022-12-21 22:20:34 +00:00
// Dont't post if the post doesn't belong to us.
// This is a check for forum postings
$self = DBA :: selectFirst ( 'contact' , [ 'id' ], [ 'uid' => $b [ 'uid' ], 'self' => true ]);
if ( $b [ 'contact-id' ] != $self [ 'id' ]) {
2013-01-01 17:20:56 +00:00
return ;
2017-12-08 05:18:01 +00:00
}
2013-01-01 17:20:56 +00:00
2021-10-03 17:35:20 +00:00
Logger :: notice ( 'GNU Socialpost invoked' );
2013-01-01 17:20:56 +00:00
2020-01-18 14:59:01 +00:00
DI :: pConfig () -> load ( $b [ 'uid' ], 'statusnet' );
2013-01-01 17:20:56 +00:00
2020-01-18 15:50:56 +00:00
$api = DI :: pConfig () -> get ( $b [ 'uid' ], 'statusnet' , 'baseapi' );
$ckey = DI :: pConfig () -> get ( $b [ 'uid' ], 'statusnet' , 'consumerkey' );
$csecret = DI :: pConfig () -> get ( $b [ 'uid' ], 'statusnet' , 'consumersecret' );
$otoken = DI :: pConfig () -> get ( $b [ 'uid' ], 'statusnet' , 'oauthtoken' );
$osecret = DI :: pConfig () -> get ( $b [ 'uid' ], 'statusnet' , 'oauthsecret' );
2011-10-12 01:46:33 +00:00
2017-12-08 05:18:01 +00:00
if ( $ckey && $csecret && $otoken && $osecret ) {
$dent = new StatusNetOAuth ( $api , $ckey , $csecret , $otoken , $osecret );
2014-04-04 09:10:01 +00:00
$max_char = $dent -> get_maxlength (); // max. length for a dent
2012-07-08 16:29:31 +00:00
2020-01-18 15:54:49 +00:00
DI :: pConfig () -> set ( $b [ 'uid' ], 'statusnet' , 'max_char' , $max_char );
2014-07-09 20:31:52 +00:00
2022-06-22 16:17:09 +00:00
$tempfile = '' ;
2021-01-30 23:14:05 +00:00
$msgarr = Plaintext :: getPost ( $b , $max_char , true , 7 );
2022-06-22 16:17:09 +00:00
$msg = $msgarr [ 'text' ];
2014-05-29 10:42:25 +00:00
2022-06-22 16:17:09 +00:00
if (( $msg == '' ) && isset ( $msgarr [ 'title' ]))
$msg = Plaintext :: shorten ( $msgarr [ 'title' ], $max_char - 50 , $b [ 'uid' ]);
2014-05-29 10:42:25 +00:00
2022-06-22 16:17:09 +00:00
$image = '' ;
2014-05-29 10:42:25 +00:00
2022-06-22 16:17:09 +00:00
if ( isset ( $msgarr [ 'url' ]) && ( $msgarr [ 'type' ] != 'photo' )) {
$msg .= " \n " . $msgarr [ 'url' ];
} elseif ( isset ( $msgarr [ 'image' ]) && ( $msgarr [ 'type' ] != 'video' )) {
$image = $msgarr [ 'image' ];
2017-12-08 05:18:01 +00:00
}
2014-05-29 10:42:25 +00:00
2022-06-22 16:17:09 +00:00
if ( $image != '' ) {
2021-08-25 19:54:54 +00:00
$img_str = DI :: httpClient () -> fetch ( $image );
2022-06-22 16:17:09 +00:00
$tempfile = tempnam ( System :: getTempPath (), 'cache' );
2014-05-29 10:42:25 +00:00
file_put_contents ( $tempfile , $img_str );
2022-06-22 16:17:09 +00:00
$postdata = [ 'status' => $msg , 'media[]' => $tempfile ];
2017-12-08 05:18:01 +00:00
} else {
2022-06-22 16:17:09 +00:00
$postdata = [ 'status' => $msg ];
2017-12-08 05:18:01 +00:00
}
2012-07-08 16:29:31 +00:00
2018-01-28 19:33:47 +00:00
// and now send it :-)
2017-12-08 05:18:01 +00:00
if ( strlen ( $msg )) {
2014-04-04 09:10:01 +00:00
// New code that is able to post pictures
2018-01-28 19:33:47 +00:00
require_once __DIR__ . DIRECTORY_SEPARATOR . 'library' . DIRECTORY_SEPARATOR . 'codebirdsn.php' ;
2018-03-07 21:48:34 +00:00
$cb = CodebirdSN :: getInstance ();
2014-04-04 09:10:01 +00:00
$cb -> setAPIEndpoint ( $api );
$cb -> setConsumerKey ( $ckey , $csecret );
$cb -> setToken ( $otoken , $osecret );
$result = $cb -> statuses_update ( $postdata );
//$result = $dent->post('statuses/update', $postdata);
2021-10-03 17:35:20 +00:00
Logger :: info ( 'statusnet_post send, result: ' . print_r ( $result , true ) .
" \n message: " . $msg . " \n Original post: " . print_r ( $b , true ) . " \n Post Data: " . print_r ( $postdata , true ));
2015-03-22 07:55:45 +00:00
2018-07-15 18:40:20 +00:00
if ( ! empty ( $result -> source )) {
2022-06-22 16:17:09 +00:00
DI :: pConfig () -> set ( $b [ 'uid' ], 'statusnet' , 'application_name' , strip_tags ( $result -> source ));
2017-12-08 05:18:01 +00:00
}
2015-03-22 07:55:45 +00:00
2018-07-15 18:40:20 +00:00
if ( ! empty ( $result -> error )) {
2021-10-03 17:35:20 +00:00
Logger :: notice ( 'Send to GNU Social failed: "' . $result -> error . '"' );
2014-04-04 09:10:01 +00:00
} elseif ( $iscomment ) {
2022-06-22 16:17:09 +00:00
Logger :: notice ( 'statusnet_post: Update extid ' . $result -> id . ' for post id ' . $b [ 'id' ]);
Item :: update ([ 'extid' => $hostname . '::' . $result -> id , 'body' => $result -> text ], [ 'id' => $b [ 'id' ]]);
2014-04-04 09:10:01 +00:00
}
}
2022-06-22 16:17:09 +00:00
if ( $tempfile != '' ) {
2012-11-22 14:29:20 +00:00
unlink ( $tempfile );
2017-12-08 05:18:01 +00:00
}
2011-10-12 01:46:33 +00:00
}
2011-09-25 08:56:03 +00:00
}
2023-01-14 02:16:09 +00:00
function statusnet_addon_admin_post ()
2017-12-08 05:18:01 +00:00
{
2018-01-15 13:15:33 +00:00
$sites = [];
2013-08-25 07:36:32 +00:00
2017-12-08 05:18:01 +00:00
foreach ( $_POST [ 'sitename' ] as $id => $sitename ) {
$sitename = trim ( $sitename );
$apiurl = trim ( $_POST [ 'apiurl' ][ $id ]);
if ( ! ( substr ( $apiurl , - 1 ) == '/' )) {
$apiurl = $apiurl . '/' ;
}
$secret = trim ( $_POST [ 'secret' ][ $id ]);
$key = trim ( $_POST [ 'key' ][ $id ]);
2018-11-30 14:11:56 +00:00
//$applicationname = (!empty($_POST['applicationname']) ? Strings::escapeTags(trim($_POST['applicationname'][$id])):'');
2022-06-22 16:17:09 +00:00
if ( $sitename != '' &&
$apiurl != '' &&
$secret != '' &&
$key != '' &&
2018-11-30 14:11:56 +00:00
empty ( $_POST [ 'delete' ][ $id ])) {
2017-12-08 05:18:01 +00:00
2018-01-15 13:15:33 +00:00
$sites [] = [
2017-12-08 05:18:01 +00:00
'sitename' => $sitename ,
'apiurl' => $apiurl ,
'consumersecret' => $secret ,
'consumerkey' => $key ,
//'applicationname' => $applicationname
2018-01-15 13:15:33 +00:00
];
2011-09-25 08:56:03 +00:00
}
}
2014-04-04 09:10:01 +00:00
2020-01-19 20:21:52 +00:00
$sites = DI :: config () -> set ( 'statusnet' , 'sites' , $sites );
2011-09-25 08:56:03 +00:00
}
2023-01-14 02:16:09 +00:00
function statusnet_addon_admin ( string & $o )
2017-12-08 05:18:01 +00:00
{
2020-01-19 20:21:12 +00:00
$sites = DI :: config () -> get ( 'statusnet' , 'sites' );
2018-01-15 13:15:33 +00:00
$sitesform = [];
2017-12-08 05:18:01 +00:00
if ( is_array ( $sites )) {
foreach ( $sites as $id => $s ) {
2018-01-15 13:15:33 +00:00
$sitesform [] = [
'sitename' => [ " sitename[ $id ] " , " Site name " , $s [ 'sitename' ], " " ],
2020-01-18 19:52:33 +00:00
'apiurl' => [ " apiurl[ $id ] " , " Api url " , $s [ 'apiurl' ], DI :: l10n () -> t ( " Base API Path \x28 remember the trailing / \x29 " )],
2018-01-15 13:15:33 +00:00
'secret' => [ " secret[ $id ] " , " Secret " , $s [ 'consumersecret' ], " " ],
'key' => [ " key[ $id ] " , " Key " , $s [ 'consumerkey' ], " " ],
2015-03-22 08:26:18 +00:00
//'applicationname' => Array("applicationname[$id]", "Application name", $s['applicationname'], ""),
2018-01-15 13:15:33 +00:00
'delete' => [ " delete[ $id ] " , " Delete " , False , " Check to delete this preset " ],
];
2011-09-25 08:56:03 +00:00
}
}
/* empty form to add new site */
2018-10-17 00:06:44 +00:00
$id = count ( $sitesform );
2018-01-15 13:15:33 +00:00
$sitesform [] = [
2020-01-18 19:52:33 +00:00
'sitename' => [ " sitename[ $id ] " , DI :: l10n () -> t ( " Site name " ), " " , " " ],
'apiurl' => [ " apiurl[ $id ] " , " Api url " , " " , DI :: l10n () -> t ( " Base API Path \x28 remember the trailing / \x29 " )],
'secret' => [ " secret[ $id ] " , DI :: l10n () -> t ( " Consumer Secret " ), " " , " " ],
'key' => [ " key[ $id ] " , DI :: l10n () -> t ( " Consumer Key " ), " " , " " ],
//'applicationname' => Array("applicationname[$id]", DI::l10n()->t("Application name"), "", ""),
2018-01-15 13:15:33 +00:00
];
2011-09-25 08:56:03 +00:00
2022-06-22 16:17:09 +00:00
$t = Renderer :: getMarkupTemplate ( 'admin.tpl' , 'addon/statusnet/' );
2018-10-31 14:55:15 +00:00
$o = Renderer :: replaceMacros ( $t , [
2020-01-18 19:52:33 +00:00
'$submit' => DI :: l10n () -> t ( 'Save Settings' ),
2011-09-25 08:56:03 +00:00
'$sites' => $sitesform ,
2018-01-15 13:15:33 +00:00
]);
2011-09-25 08:56:03 +00:00
}