2011-09-25 10:56:03 +02:00
< ? php
2017-12-08 06:18:01 +01:00
2011-09-25 10:56:03 +02:00
/**
2015-07-12 21:43:59 +02:00
* Name : GNU Social Connector
2015-11-14 09:22:32 +01:00
* Description : Bidirectional ( posting , relaying and reading ) connector for GNU Social .
2012-04-30 09:21:05 +02:00
* Version : 1.0 . 5
2013-06-14 19:40:10 +02:00
* Author : Tobias Diekershoff < https :// f . diekershoff . de / profile / tobias >
2013-04-01 09:02:38 +02:00
* Author : Michael Vogel < https :// pirati . ca / profile / heluecht >
2011-09-25 10:56:03 +02:00
*
2013-05-31 13:27:31 +02:00
* Copyright ( c ) 2011 - 2013 Tobias Diekershoff , Michael Vogel
* All rights reserved .
2011-09-25 10:56:03 +02:00
*
2013-05-31 13:27:31 +02: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 10:56:03 +02:00
*
2013-05-31 13:27:31 +02: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 10:56:03 +02:00
*
*/
2013-01-01 23:18:15 +01:00
define ( 'STATUSNET_DEFAULT_POLL_INTERVAL' , 5 ); // given in minutes
2013-01-01 18:20:56 +01:00
2018-01-28 20:33:47 +01:00
require_once __DIR__ . DIRECTORY_SEPARATOR . 'library' . DIRECTORY_SEPARATOR . 'statusnetoauth.php' ;
2019-10-25 00:10:19 +02:00
2018-01-25 03:29:09 +01:00
use CodebirdSN\CodebirdSN ;
2017-12-08 06:18:01 +01:00
use Friendica\App ;
2018-01-27 02:30:38 +01:00
use Friendica\Content\Text\Plaintext ;
2018-12-26 08:28:16 +01:00
use Friendica\Core\Hook ;
2018-10-30 00:40:18 +01:00
use Friendica\Core\Logger ;
2018-10-31 15:55:15 +01:00
use Friendica\Core\Renderer ;
2021-11-04 21:32:16 +01:00
use Friendica\Core\System ;
2018-07-20 14:20:48 +02:00
use Friendica\Database\DBA ;
2019-12-16 00:28:30 +01:00
use Friendica\DI ;
2018-01-28 12:19:38 +01:00
use Friendica\Model\Item ;
2021-01-17 01:00:32 +01:00
use Friendica\Model\Post ;
2017-12-09 19:50:29 +01:00
use Friendica\Model\User ;
2017-12-08 06:18:01 +01:00
function statusnet_install ()
{
2011-09-25 10:56:03 +02:00
// we need some hooks, for the configuration and for sending tweets
2018-12-26 08:28:16 +01: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 18:17:09 +02:00
Logger :: notice ( 'installed GNU Social' );
2011-09-25 10:56:03 +02:00
}
2019-03-25 03:44:50 +01:00
function statusnet_jot_nets ( App $a , array & $jotnets_fields )
2017-12-08 06:18:01 +01:00
{
2022-10-20 23:51:49 +02:00
if ( ! DI :: userSession () -> getLocalUserId ()) {
2011-09-25 10:56:03 +02:00
return ;
2017-12-08 06:18:01 +01:00
}
2011-09-25 10:56:03 +02:00
2022-10-20 23:51:49 +02:00
if ( DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'post' )) {
2019-03-25 03:44:50 +01:00
$jotnets_fields [] = [
'type' => 'checkbox' ,
'field' => [
'statusnet_enable' ,
2020-01-18 20:52:33 +01:00
DI :: l10n () -> t ( 'Post to GNU Social' ),
2022-10-20 23:51:49 +02:00
DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'post_by_default' )
2019-03-25 03:44:50 +01:00
]
];
2011-09-25 10:56:03 +02:00
}
}
2017-12-08 06:18:01 +01:00
function statusnet_settings_post ( App $a , $post )
{
2022-10-20 23:51:49 +02:00
if ( ! DI :: userSession () -> getLocalUserId ()) {
2014-04-04 11:10:01 +02:00
return ;
2017-12-08 06:18:01 +01:00
}
2015-07-12 21:43:59 +02:00
// don't check GNU Social settings if GNU Social submit button is not clicked
2021-11-21 23:10:13 +01:00
if ( empty ( $_POST [ 'statusnet-submit' ]) && empty ( $_POST [ 'statusnet-disconnect' ])) {
2014-04-04 11:10:01 +02:00
return ;
2017-12-08 06:18:01 +01:00
}
2014-03-16 20:50:36 +01:00
2021-11-21 23:10:13 +01:00
if ( ! empty ( $_POST [ 'statusnet-disconnect' ])) {
2017-12-08 06:18:01 +01:00
/* * *
2021-11-21 23:10:13 +01:00
* if the GNU Social - disconnect button is clicked , clear the GNU Social configuration
2014-04-04 11:10:01 +02:00
*/
2022-10-20 23:51:49 +02: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 10:56:03 +02:00
} else {
2017-12-08 06:18:01 +01: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 21:21:12 +01:00
$globalsn = DI :: config () -> get ( 'statusnet' , 'sites' );
2017-12-08 06:18:01 +01:00
foreach ( $globalsn as $asn ) {
if ( $asn [ 'apiurl' ] == $_POST [ 'statusnet-preconf-apiurl' ]) {
$apibase = $asn [ 'apiurl' ];
2021-08-25 21:54:54 +02:00
$c = DI :: httpClient () -> fetch ( $apibase . 'statusnet/version.xml' );
2017-12-08 06:18:01 +01:00
if ( strlen ( $c ) > 0 ) {
2022-10-20 23:51:49 +02: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 06:18:01 +01:00
} else {
2022-10-18 14:28:46 +02: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 06:18:01 +01:00
}
2017-12-08 02:47:23 +01:00
}
2017-12-08 05:24:41 +01:00
}
} else {
2017-12-08 06:18:01 +01: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 21:54:54 +02:00
$c = DI :: httpClient () -> fetch ( $apibase . 'statusnet/version.xml' );
2017-12-08 06:18:01 +01:00
if ( strlen ( $c ) > 0 ) {
// ok the API path is correct, let's save the settings
2022-10-20 23:51:49 +02: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 06:18:01 +01:00
} else {
// the API path is not correct, maybe missing trailing / ?
$apibase = $apibase . '/' ;
2021-08-25 21:54:54 +02:00
$c = DI :: httpClient () -> fetch ( $apibase . 'statusnet/version.xml' );
2017-12-08 06:18:01 +01:00
if ( strlen ( $c ) > 0 ) {
// ok the API path is now correct, let's save the settings
2022-10-20 23:51:49 +02: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 06:18:01 +01:00
} else {
// still not the correct API base, let's do noting
2022-10-17 23:00:03 +02:00
DI :: sysmsg () -> addNotice ( DI :: l10n () -> t ( 'We could not contact the GNU Social API with the Path you entered.' ));
2017-12-08 06:18:01 +01:00
}
}
2014-04-04 11:10:01 +02:00
} else {
2017-12-08 06:18:01 +01: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 23:51:49 +02: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 06:18:01 +01: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 23:51:49 +02: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 06:18:01 +01: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 23:51:49 +02: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 06:18:01 +01:00
}
2014-04-04 11:10:01 +02:00
}
}
2017-12-08 06:18:01 +01:00
}
2011-09-25 10:56:03 +02:00
}
2017-12-08 06:18:01 +01:00
2021-11-21 23:10:13 +01:00
function statusnet_settings ( App $a , array & $data )
2017-12-08 06:18:01 +01:00
{
2022-10-20 23:51:49 +02:00
if ( ! DI :: userSession () -> getLocalUserId ()) {
2014-04-04 11:10:01 +02:00
return ;
2017-12-08 06:18:01 +01:00
}
2021-08-08 19:10:04 +02:00
2021-11-21 23:10:13 +01:00
DI :: page () -> registerStylesheet ( __DIR__ . '/statusnet.css' , 'all' );
2021-08-08 19:10:04 +02:00
2017-12-08 06:18:01 +01:00
/* * *
2011-09-25 10:56:03 +02: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 11:10:01 +02:00
* allow the user to cancel the connection process at this step
2011-09-25 10:56:03 +02:00
* 3 ) Checkbox for " Send public notices (respect size limitation)
*/
2022-12-21 23:20:34 +01: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 23:10:13 +01: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 06:18:01 +01:00
/* * *
2011-09-25 10:56:03 +02:00
* we have an OAuth key / secret pair for the user
2015-07-12 21:43:59 +02:00
* so let ' s give a chance to disable the postings to GNU Social
2011-09-25 10:56:03 +02:00
*/
2021-11-21 23:10:13 +01: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 06:16:56 +02:00
}
2021-11-21 23:10:13 +01:00
2022-10-20 23:51:49 +02:00
$user = User :: getById ( DI :: userSession () -> getLocalUserId ());
2021-08-08 19:10:04 +02:00
if ( $user [ 'hidewall' ]) {
2021-11-21 23:10:13 +01: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 11:10:01 +02:00
}
2021-11-21 23:10:13 +01: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 06:18:01 +01:00
*/
2021-11-21 23:10:13 +01: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 10:56:03 +02:00
}
}
2021-11-21 23:10:13 +01: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 ,
'$authenticate_url' => DI :: baseUrl () -> get () . '/statusnet/connect' ,
'$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 10:56:03 +02:00
}
2018-11-10 17:20:19 +01:00
function statusnet_hook_fork ( App $a , array & $b )
{
if ( $b [ 'name' ] != 'notifier_normal' ) {
return ;
}
$post = $b [ 'data' ];
// Deleting and editing is not supported by the addon
if ( $post [ 'deleted' ] || ( $post [ 'created' ] !== $post [ 'edited' ])) {
$b [ 'execute' ] = false ;
return ;
}
2022-12-21 23:20:34 +01:00
if ( $post [ 'app' ] == 'StatusNet' ) {
2018-11-10 17:20:19 +01:00
$b [ 'execute' ] = false ;
return ;
}
2022-12-21 23:20:34 +01:00
// Comments are never exported when we don't import the GNU Social timeline
if ( strpos ( $post [ 'postopts' ] ? ? '' , 'statusnet' ) === false || ( $post [ 'parent' ] != $post [ 'id' ]) || $post [ 'private' ]) {
2018-11-10 17:20:19 +01:00
$b [ 'execute' ] = false ;
return ;
}
}
2022-06-23 07:16:22 +02:00
function statusnet_post_local ( App $a , array & $b )
2017-12-08 06:18:01 +01:00
{
2017-09-06 18:16:33 +02:00
if ( $b [ 'edit' ]) {
2011-10-12 03:46:33 +02:00
return ;
2017-09-06 18:16:33 +02:00
}
2011-10-12 03:46:33 +02:00
2022-10-20 23:51:49 +02:00
if ( ! DI :: userSession () -> getLocalUserId () || ( DI :: userSession () -> getLocalUserId () != $b [ 'uid' ])) {
2017-09-06 18:16:33 +02:00
return ;
}
2011-10-12 03:46:33 +02:00
2022-10-20 23:51:49 +02:00
$statusnet_post = DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'post' );
2018-11-30 15:11:56 +01:00
$statusnet_enable = (( $statusnet_post && ! empty ( $_REQUEST [ 'statusnet_enable' ])) ? intval ( $_REQUEST [ 'statusnet_enable' ]) : 0 );
2011-10-12 03:46:33 +02:00
2017-09-06 18:16:33 +02:00
// if API is used, default to the chosen settings
2022-10-20 23:51:49 +02:00
if ( $b [ 'api_source' ] && intval ( DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'statusnet' , 'post_by_default' ))) {
2017-09-06 18:16:33 +02:00
$statusnet_enable = 1 ;
}
2011-10-12 03:46:33 +02:00
2017-09-06 18:16:33 +02:00
if ( ! $statusnet_enable ) {
return ;
}
2011-10-12 03:46:33 +02:00
2017-09-06 18:16:33 +02:00
if ( strlen ( $b [ 'postopts' ])) {
$b [ 'postopts' ] .= ',' ;
2014-04-04 11:10:01 +02:00
}
2017-09-06 18:16:33 +02:00
$b [ 'postopts' ] .= 'statusnet' ;
2011-10-12 03:46:33 +02:00
}
2022-06-23 07:16:22 +02:00
function statusnet_post_hook ( App $a , array & $b )
2017-12-08 06:18:01 +01:00
{
2011-09-25 10:56:03 +02:00
/**
2015-07-12 21:43:59 +02:00
* Post to GNU Social
2011-09-25 10:56:03 +02:00
*/
2022-12-21 23:20:34 +01:00
if ( $b [ 'deleted' ] || $b [ 'private' ] || ( $b [ 'created' ] !== $b [ 'edited' ])) {
return ;
2014-04-04 11:10:01 +02:00
}
2011-09-25 10:56:03 +02:00
2022-10-30 00:14:12 +02:00
$b [ 'body' ] = Post\Media :: addAttachmentsToBody ( $b [ 'uri-id' ], DI :: contentItem () -> addSharedPost ( $b ));
2021-05-01 10:59:14 +02:00
2022-06-22 18:17:09 +02:00
$api = DI :: pConfig () -> get ( $b [ 'uid' ], 'statusnet' , 'baseapi' );
2014-04-04 11:10:01 +02:00
$hostname = preg_replace ( " =https?://([ \ w \ .]*)/.*=ism " , " $ 1 " , $api );
2011-09-25 10:56:03 +02:00
2022-12-21 23:20:34 +01:00
if ( $b [ 'private' ] || ! strstr ( $b [ 'postopts' ], 'statusnet' )) {
2014-11-07 20:06:51 +01:00
return ;
2017-12-08 06:18:01 +01:00
}
2014-11-07 20:06:51 +01:00
2022-12-21 23:20:34 +01: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 18:20:56 +01:00
return ;
2017-12-08 06:18:01 +01:00
}
2013-01-01 18:20:56 +01:00
2021-10-03 19:35:20 +02:00
Logger :: notice ( 'GNU Socialpost invoked' );
2013-01-01 18:20:56 +01:00
2020-01-18 15:59:01 +01:00
DI :: pConfig () -> load ( $b [ 'uid' ], 'statusnet' );
2013-01-01 18:20:56 +01:00
2020-01-18 16:50:56 +01: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 03:46:33 +02:00
2017-12-08 06:18:01 +01:00
if ( $ckey && $csecret && $otoken && $osecret ) {
$dent = new StatusNetOAuth ( $api , $ckey , $csecret , $otoken , $osecret );
2014-04-04 11:10:01 +02:00
$max_char = $dent -> get_maxlength (); // max. length for a dent
2012-07-08 18:29:31 +02:00
2020-01-18 16:54:49 +01:00
DI :: pConfig () -> set ( $b [ 'uid' ], 'statusnet' , 'max_char' , $max_char );
2014-07-09 22:31:52 +02:00
2022-06-22 18:17:09 +02:00
$tempfile = '' ;
2021-01-31 00:14:05 +01:00
$msgarr = Plaintext :: getPost ( $b , $max_char , true , 7 );
2022-06-22 18:17:09 +02:00
$msg = $msgarr [ 'text' ];
2014-05-29 12:42:25 +02:00
2022-06-22 18:17:09 +02:00
if (( $msg == '' ) && isset ( $msgarr [ 'title' ]))
$msg = Plaintext :: shorten ( $msgarr [ 'title' ], $max_char - 50 , $b [ 'uid' ]);
2014-05-29 12:42:25 +02:00
2022-06-22 18:17:09 +02:00
$image = '' ;
2014-05-29 12:42:25 +02:00
2022-06-22 18:17:09 +02: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 06:18:01 +01:00
}
2014-05-29 12:42:25 +02:00
2022-06-22 18:17:09 +02:00
if ( $image != '' ) {
2021-08-25 21:54:54 +02:00
$img_str = DI :: httpClient () -> fetch ( $image );
2022-06-22 18:17:09 +02:00
$tempfile = tempnam ( System :: getTempPath (), 'cache' );
2014-05-29 12:42:25 +02:00
file_put_contents ( $tempfile , $img_str );
2022-06-22 18:17:09 +02:00
$postdata = [ 'status' => $msg , 'media[]' => $tempfile ];
2017-12-08 06:18:01 +01:00
} else {
2022-06-22 18:17:09 +02:00
$postdata = [ 'status' => $msg ];
2017-12-08 06:18:01 +01:00
}
2012-07-08 18:29:31 +02:00
2018-01-28 20:33:47 +01:00
// and now send it :-)
2017-12-08 06:18:01 +01:00
if ( strlen ( $msg )) {
2014-04-04 11:10:01 +02:00
// New code that is able to post pictures
2018-01-28 20:33:47 +01:00
require_once __DIR__ . DIRECTORY_SEPARATOR . 'library' . DIRECTORY_SEPARATOR . 'codebirdsn.php' ;
2018-03-07 22:48:34 +01:00
$cb = CodebirdSN :: getInstance ();
2014-04-04 11:10:01 +02: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 19:35:20 +02: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 08:55:45 +01:00
2018-07-15 20:40:20 +02:00
if ( ! empty ( $result -> source )) {
2022-06-22 18:17:09 +02:00
DI :: pConfig () -> set ( $b [ 'uid' ], 'statusnet' , 'application_name' , strip_tags ( $result -> source ));
2017-12-08 06:18:01 +01:00
}
2015-03-22 08:55:45 +01:00
2018-07-15 20:40:20 +02:00
if ( ! empty ( $result -> error )) {
2021-10-03 19:35:20 +02:00
Logger :: notice ( 'Send to GNU Social failed: "' . $result -> error . '"' );
2014-04-04 11:10:01 +02:00
} elseif ( $iscomment ) {
2022-06-22 18:17:09 +02: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 11:10:01 +02:00
}
}
2022-06-22 18:17:09 +02:00
if ( $tempfile != '' ) {
2012-11-22 15:29:20 +01:00
unlink ( $tempfile );
2017-12-08 06:18:01 +01:00
}
2011-10-12 03:46:33 +02:00
}
2011-09-25 10:56:03 +02:00
}
2018-01-20 14:57:41 +01:00
function statusnet_addon_admin_post ( App $a )
2017-12-08 06:18:01 +01:00
{
2018-01-15 14:15:33 +01:00
$sites = [];
2013-08-25 09:36:32 +02:00
2017-12-08 06:18:01 +01: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 15:11:56 +01:00
//$applicationname = (!empty($_POST['applicationname']) ? Strings::escapeTags(trim($_POST['applicationname'][$id])):'');
2022-06-22 18:17:09 +02:00
if ( $sitename != '' &&
$apiurl != '' &&
$secret != '' &&
$key != '' &&
2018-11-30 15:11:56 +01:00
empty ( $_POST [ 'delete' ][ $id ])) {
2017-12-08 06:18:01 +01:00
2018-01-15 14:15:33 +01:00
$sites [] = [
2017-12-08 06:18:01 +01:00
'sitename' => $sitename ,
'apiurl' => $apiurl ,
'consumersecret' => $secret ,
'consumerkey' => $key ,
//'applicationname' => $applicationname
2018-01-15 14:15:33 +01:00
];
2011-09-25 10:56:03 +02:00
}
}
2014-04-04 11:10:01 +02:00
2020-01-19 21:21:52 +01:00
$sites = DI :: config () -> set ( 'statusnet' , 'sites' , $sites );
2011-09-25 10:56:03 +02:00
}
2022-06-30 13:32:13 +02:00
function statusnet_addon_admin ( App $a , string & $o )
2017-12-08 06:18:01 +01:00
{
2020-01-19 21:21:12 +01:00
$sites = DI :: config () -> get ( 'statusnet' , 'sites' );
2018-01-15 14:15:33 +01:00
$sitesform = [];
2017-12-08 06:18:01 +01:00
if ( is_array ( $sites )) {
foreach ( $sites as $id => $s ) {
2018-01-15 14:15:33 +01:00
$sitesform [] = [
'sitename' => [ " sitename[ $id ] " , " Site name " , $s [ 'sitename' ], " " ],
2020-01-18 20:52:33 +01:00
'apiurl' => [ " apiurl[ $id ] " , " Api url " , $s [ 'apiurl' ], DI :: l10n () -> t ( " Base API Path \x28 remember the trailing / \x29 " )],
2018-01-15 14:15:33 +01:00
'secret' => [ " secret[ $id ] " , " Secret " , $s [ 'consumersecret' ], " " ],
'key' => [ " key[ $id ] " , " Key " , $s [ 'consumerkey' ], " " ],
2015-03-22 09:26:18 +01:00
//'applicationname' => Array("applicationname[$id]", "Application name", $s['applicationname'], ""),
2018-01-15 14:15:33 +01:00
'delete' => [ " delete[ $id ] " , " Delete " , False , " Check to delete this preset " ],
];
2011-09-25 10:56:03 +02:00
}
}
/* empty form to add new site */
2018-10-17 02:06:44 +02:00
$id = count ( $sitesform );
2018-01-15 14:15:33 +01:00
$sitesform [] = [
2020-01-18 20:52:33 +01: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 14:15:33 +01:00
];
2011-09-25 10:56:03 +02:00
2022-06-22 18:17:09 +02:00
$t = Renderer :: getMarkupTemplate ( 'admin.tpl' , 'addon/statusnet/' );
2018-10-31 15:55:15 +01:00
$o = Renderer :: replaceMacros ( $t , [
2020-01-18 20:52:33 +01:00
'$submit' => DI :: l10n () -> t ( 'Save Settings' ),
2011-09-25 10:56:03 +02:00
'$sites' => $sitesform ,
2018-01-15 14:15:33 +01:00
]);
2011-09-25 10:56:03 +02:00
}