2011-09-25 10:56:03 +02:00
< ? php
/**
2023-08-09 22:01:57 +02:00
* Name : Twitter Post Connector
* Description : Post to Twitter
* Version : 2.0
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 >
2018-01-28 05:20:09 +01:00
* Maintainer : Hypolite Petovan < https :// friendica . mrpetovan . com / profile / hypolite >
2023-08-09 22:01:57 +02:00
* Maintainer : Michael Vogel < https :// pirati . ca / profile / heluecht >
2013-05-31 16:26:51 +02:00
*
2023-08-09 22:01:57 +02:00
* Copyright ( c ) 2011 - 2023 Tobias Diekershoff , Michael Vogel , Hypolite Petovan
2013-05-31 16:26:51 +02:00
* All rights reserved .
*
* 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 .
*
* 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
*/
2018-11-11 02:52:57 +01:00
use Friendica\Content\Text\BBCode ;
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 ;
2017-11-05 16:07:52 +01:00
use Friendica\Core\Worker ;
2019-12-16 00:28:30 +01:00
use Friendica\DI ;
2018-01-28 12:19:38 +01:00
use Friendica\Model\Item ;
2020-12-13 14:07:12 +01:00
use Friendica\Model\Post ;
2023-01-01 20:21:56 +01:00
use Friendica\Core\Config\Util\ConfigFileManager ;
2022-12-10 20:09:36 +01:00
use Friendica\Model\Photo ;
2023-08-10 23:26:10 +02:00
use Friendica\Object\Image ;
2023-08-09 22:01:57 +02:00
use GuzzleHttp\Client ;
2023-08-15 22:25:17 +02:00
use GuzzleHttp\Exception\RequestException ;
2023-08-09 22:01:57 +02:00
use GuzzleHttp\HandlerStack ;
use GuzzleHttp\Subscriber\Oauth\Oauth1 ;
2013-01-01 16:11:07 +01:00
2023-08-15 22:25:17 +02:00
const TWITTER_IMAGE_SIZE = [ 2000000 , 1000000 , 500000 , 100000 , 50000 ];
2023-08-10 23:26:10 +02:00
2017-12-08 06:18:01 +01:00
function twitter_install ()
{
2023-08-10 23:36:09 +02:00
Hook :: register ( 'load_config' , __FILE__ , 'twitter_load_config' );
Hook :: register ( 'connector_settings' , __FILE__ , 'twitter_settings' );
2018-12-26 08:28:16 +01:00
Hook :: register ( 'connector_settings_post' , __FILE__ , 'twitter_settings_post' );
2023-08-10 23:36:09 +02:00
Hook :: register ( 'hook_fork' , __FILE__ , 'twitter_hook_fork' );
Hook :: register ( 'post_local' , __FILE__ , 'twitter_post_local' );
Hook :: register ( 'notifier_normal' , __FILE__ , 'twitter_post_hook' );
Hook :: register ( 'jot_networks' , __FILE__ , 'twitter_jot_nets' );
2011-09-25 10:56:03 +02:00
}
2023-01-14 03:16:09 +01:00
function twitter_load_config ( ConfigFileManager $loader )
2018-06-28 05:21:24 +02:00
{
2023-01-14 03:16:09 +01:00
DI :: app () -> getConfigCache () -> load ( $loader -> loadAddonConfig ( 'twitter' ), \Friendica\Core\Config\ValueObject\Cache :: SOURCE_STATIC );
2018-06-28 05:21:24 +02:00
}
2023-01-14 03:16:09 +01:00
function twitter_jot_nets ( 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 (), 'twitter' , 'post' )) {
2019-03-25 03:44:50 +01:00
$jotnets_fields [] = [
'type' => 'checkbox' ,
'field' => [
'twitter_enable' ,
2020-01-18 20:52:33 +01:00
DI :: l10n () -> t ( 'Post to Twitter' ),
2022-10-20 23:51:49 +02:00
DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'twitter' , 'post_by_default' )
2019-03-25 03:44:50 +01:00
]
];
2011-09-25 10:56:03 +02:00
}
}
2023-01-14 03:16:09 +01:00
function twitter_settings_post ()
2017-12-08 06:18:01 +01:00
{
2023-08-09 22:01:57 +02:00
if ( ! DI :: userSession () -> getLocalUserId () || empty ( $_POST [ 'twitter-submit' ])) {
2013-10-15 00:52:01 +02:00
return ;
2017-12-08 06:18:01 +01:00
}
2013-10-15 00:52:01 +02:00
2023-08-16 08:17:18 +02:00
$api_key = DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'twitter' , 'api_key' );
$api_secret = DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'twitter' , 'api_secret' );
$access_token = DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'twitter' , 'access_token' );
$access_secret = DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'twitter' , 'access_secret' );
2023-08-09 22:01:57 +02:00
DI :: pConfig () -> set ( DI :: userSession () -> getLocalUserId (), 'twitter' , 'post' , ( bool ) $_POST [ 'twitter-enable' ]);
DI :: pConfig () -> set ( DI :: userSession () -> getLocalUserId (), 'twitter' , 'post_by_default' , ( bool ) $_POST [ 'twitter-default' ]);
DI :: pConfig () -> set ( DI :: userSession () -> getLocalUserId (), 'twitter' , 'api_key' , $_POST [ 'twitter-api-key' ]);
DI :: pConfig () -> set ( DI :: userSession () -> getLocalUserId (), 'twitter' , 'api_secret' , $_POST [ 'twitter-api-secret' ]);
DI :: pConfig () -> set ( DI :: userSession () -> getLocalUserId (), 'twitter' , 'access_token' , $_POST [ 'twitter-access-token' ]);
DI :: pConfig () -> set ( DI :: userSession () -> getLocalUserId (), 'twitter' , 'access_secret' , $_POST [ 'twitter-access-secret' ]);
2023-08-16 08:17:18 +02:00
2023-08-16 16:20:32 +02:00
if (
empty ( DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'twitter' , 'last_status' )) ||
2023-08-16 08:17:18 +02:00
( $api_key != $_POST [ 'twitter-api-key' ]) || ( $api_secret != $_POST [ 'twitter-api-secret' ]) ||
2023-08-16 16:20:32 +02:00
( $access_token != $_POST [ 'twitter-access-token' ]) || ( $access_secret != $_POST [ 'twitter-access-secret' ])
) {
2023-08-16 08:17:18 +02:00
twitter_test_connection ( DI :: userSession () -> getLocalUserId ());
}
2011-09-25 10:56:03 +02:00
}
2017-12-08 06:18:01 +01:00
2023-01-14 03:16:09 +01:00
function twitter_settings ( array & $data )
2017-12-08 06:18:01 +01:00
{
2022-10-20 23:51:49 +02:00
if ( ! DI :: userSession () -> getLocalUserId ()) {
2014-06-10 20:13:11 +02:00
return ;
2017-12-08 06:18:01 +01:00
}
2021-08-08 19:10:04 +02:00
2023-08-09 22:01:57 +02:00
$enabled = DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'twitter' , 'post' ) ? ? false ;
$def_enabled = DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'twitter' , 'post_by_default' ) ? ? false ;
2021-11-21 23:10:13 +01:00
2023-08-09 22:01:57 +02:00
$api_key = DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'twitter' , 'api_key' );
$api_secret = DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'twitter' , 'api_secret' );
$access_token = DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'twitter' , 'access_token' );
$access_secret = DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'twitter' , 'access_secret' );
2023-08-16 16:20:32 +02:00
2023-08-15 22:25:17 +02:00
$last_status = DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'twitter' , 'last_status' );
2023-08-16 05:28:54 +02:00
if ( ! empty ( $last_status [ 'code' ]) && ! empty ( $last_status [ 'reason' ])) {
$status_title = sprintf ( '%d - %s' , $last_status [ 'code' ], $last_status [ 'reason' ]);
2023-08-15 22:25:17 +02:00
} else {
2023-08-16 05:28:54 +02:00
$status_title = DI :: l10n () -> t ( 'No status.' );
2023-08-15 22:25:17 +02:00
}
2023-08-16 05:28:54 +02:00
$status_content = $last_status [ 'content' ] ? ? '' ;
2021-11-21 23:10:13 +01:00
2023-08-09 22:01:57 +02:00
$t = Renderer :: getMarkupTemplate ( 'connector_settings.tpl' , 'addon/twitter/' );
$html = Renderer :: replaceMacros ( $t , [
'$enable' => [ 'twitter-enable' , DI :: l10n () -> t ( 'Allow posting to Twitter' ), $enabled , DI :: l10n () -> t ( 'If enabled all your <strong>public</strong> postings can be posted to the associated Twitter account. You can choose to do so by default (here) or for every posting separately in the posting options when writing the entry.' )],
'$default' => [ 'twitter-default' , DI :: l10n () -> t ( 'Send public postings to Twitter by default' ), $def_enabled ],
'$api_key' => [ 'twitter-api-key' , DI :: l10n () -> t ( 'API Key' ), $api_key ],
'$api_secret' => [ 'twitter-api-secret' , DI :: l10n () -> t ( 'API Secret' ), $api_secret ],
'$access_token' => [ 'twitter-access-token' , DI :: l10n () -> t ( 'Access Token' ), $access_token ],
'$access_secret' => [ 'twitter-access-secret' , DI :: l10n () -> t ( 'Access Secret' ), $access_secret ],
2023-08-10 23:36:09 +02:00
'$help' => DI :: l10n () -> t ( 'Each user needs to register their own app to be able to post to Twitter. Please visit https://developer.twitter.com/en/portal/projects-and-apps to register a project. Inside the project you then have to register an app. You will find the needed data for the connector on the page "Keys and token" in the app settings.' ),
2023-08-16 05:28:54 +02:00
'$status_title' => [ 'twitter-status-title' , DI :: l10n () -> t ( 'Last Status Summary' ), $status_title , '' , '' , 'readonly' ],
'$status' => [ 'twitter-status' , DI :: l10n () -> t ( 'Last Status Content' ), $status_content , '' , '' , 'readonly' ],
2023-08-09 22:01:57 +02:00
]);
2021-11-21 23:10:13 +01:00
$data = [
'connector' => 'twitter' ,
2023-08-09 22:01:57 +02:00
'title' => DI :: l10n () -> t ( 'Twitter Export' ),
2021-11-21 23:10:13 +01:00
'enabled' => $enabled ,
'image' => 'images/twitter.png' ,
'html' => $html ,
];
2011-09-25 10:56:03 +02:00
}
2023-01-14 03:16:09 +01:00
function twitter_hook_fork ( array & $b )
2018-11-10 17:20:19 +01:00
{
2021-11-23 23:59:08 +01:00
DI :: logger () -> debug ( 'twitter_hook_fork' , $b );
2018-11-10 17:20:19 +01:00
if ( $b [ 'name' ] != 'notifier_normal' ) {
return ;
}
$post = $b [ 'data' ];
2023-08-10 23:36:09 +02:00
if (
$post [ 'deleted' ] || $post [ 'private' ] || ( $post [ 'created' ] !== $post [ 'edited' ]) ||
! strstr ( $post [ 'postopts' ], 'twitter' ) || ( $post [ 'gravity' ] != Item :: GRAVITY_PARENT )
) {
2018-11-10 17:20:19 +01:00
$b [ 'execute' ] = false ;
return ;
}
}
2023-01-14 03:16:09 +01:00
function twitter_post_local ( array & $b )
2017-12-08 06:18:01 +01:00
{
2023-08-09 22:01:57 +02:00
if ( ! DI :: userSession () -> getLocalUserId () || ( DI :: userSession () -> getLocalUserId () != $b [ 'uid' ])) {
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
2023-08-09 22:01:57 +02:00
if ( $b [ 'edit' ] || $b [ 'private' ] || $b [ 'parent' ]) {
2017-09-06 18:16:33 +02:00
return ;
}
2011-10-12 03:46:33 +02:00
2023-08-09 22:01:57 +02:00
$twitter_post = ( bool ) DI :: pConfig () -> get ( DI :: userSession () -> getLocalUserId (), 'twitter' , 'post' );
$twitter_enable = (( $twitter_post && ! empty ( $_REQUEST [ 'twitter_enable' ])) ? ( bool ) $_REQUEST [ 'twitter_enable' ] : false );
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 (), 'twitter' , 'post_by_default' ))) {
2023-08-09 22:01:57 +02:00
$twitter_enable = true ;
2017-09-06 18:16:33 +02:00
}
2011-10-12 03:46:33 +02:00
2017-09-06 18:16:33 +02:00
if ( ! $twitter_enable ) {
2014-06-10 20:13:11 +02:00
return ;
2017-09-06 18:16:33 +02:00
}
2011-10-12 03:46:33 +02:00
2017-09-06 18:16:33 +02:00
if ( strlen ( $b [ 'postopts' ])) {
2014-06-10 20:13:11 +02:00
$b [ 'postopts' ] .= ',' ;
2011-10-12 03:46:33 +02:00
}
2017-09-06 18:16:33 +02:00
$b [ 'postopts' ] .= 'twitter' ;
2011-10-12 03:46:33 +02:00
}
2023-08-09 22:01:57 +02:00
function twitter_post_hook ( array & $b )
2021-11-23 23:52:52 +01:00
{
2023-08-09 22:01:57 +02:00
DI :: logger () -> debug ( 'Invoke post hook' , $b );
2021-11-23 23:52:52 +01:00
2023-08-09 22:01:57 +02:00
if (( $b [ 'gravity' ] != Item :: GRAVITY_PARENT ) || ! strstr ( $b [ 'postopts' ], 'twitter' ) || $b [ 'private' ] || $b [ 'deleted' ] || ( $b [ 'created' ] !== $b [ 'edited' ])) {
2021-11-23 23:52:52 +01:00
return ;
}
2023-08-09 22:01:57 +02:00
$b [ 'body' ] = Post\Media :: addAttachmentsToBody ( $b [ 'uri-id' ], DI :: contentItem () -> addSharedPost ( $b ));
2021-11-23 23:52:52 +01:00
2023-08-09 22:01:57 +02:00
Logger :: notice ( 'twitter post invoked' , [ 'id' => $b [ 'id' ], 'guid' => $b [ 'guid' ]]);
2021-11-23 23:52:52 +01:00
2023-08-09 22:01:57 +02:00
DI :: pConfig () -> load ( $b [ 'uid' ], 'twitter' );
2021-11-23 23:52:52 +01:00
2023-08-09 22:01:57 +02:00
$api_key = DI :: pConfig () -> get ( $b [ 'uid' ], 'twitter' , 'api_key' );
$api_secret = DI :: pConfig () -> get ( $b [ 'uid' ], 'twitter' , 'api_secret' );
$access_token = DI :: pConfig () -> get ( $b [ 'uid' ], 'twitter' , 'access_token' );
$access_secret = DI :: pConfig () -> get ( $b [ 'uid' ], 'twitter' , 'access_secret' );
2021-11-23 23:52:52 +01:00
2023-08-09 22:01:57 +02:00
if ( empty ( $api_key ) || empty ( $api_secret ) || empty ( $access_token ) || empty ( $access_secret )) {
Logger :: info ( 'Missing keys, secrets or tokens.' );
2021-11-23 23:52:52 +01:00
return ;
}
2023-08-09 22:01:57 +02:00
$msgarr = Plaintext :: getPost ( $b , 280 , true , BBCode :: TWITTER );
Logger :: debug ( 'Got plaintext' , [ 'id' => $b [ 'id' ], 'message' => $msgarr ]);
2020-11-28 19:36:00 +01:00
2023-08-09 22:01:57 +02:00
$media_ids = [];
2021-10-09 07:55:26 +02:00
2023-08-09 22:01:57 +02:00
if ( ! empty ( $msgarr [ 'images' ]) || ! empty ( $msgarr [ 'remote_images' ])) {
Logger :: info ( 'Got images' , [ 'id' => $b [ 'id' ], 'images' => $msgarr [ 'images' ] ? ? []]);
2013-10-15 00:52:01 +02:00
2023-08-15 22:25:17 +02:00
$retrial = Worker :: getRetrial ();
if ( $retrial > 4 ) {
return ;
}
2023-08-09 22:01:57 +02:00
foreach ( $msgarr [ 'images' ] ? ? [] as $image ) {
if ( count ( $media_ids ) == 4 ) {
continue ;
}
try {
2023-08-15 22:25:17 +02:00
$media_ids [] = twitter_upload_image ( $b [ 'uid' ], $image , $retrial );
} catch ( RequestException $exception ) {
Logger :: warning ( 'Error while uploading image' , [ 'image' => $image , 'code' => $exception -> getCode (), 'message' => $exception -> getMessage ()]);
Worker :: defer ();
2023-08-10 23:36:09 +02:00
return ;
}
2023-08-09 22:01:57 +02:00
}
2021-10-09 07:55:26 +02:00
}
2013-10-15 00:52:01 +02:00
2023-08-09 22:01:57 +02:00
$in_reply_to_tweet_id = 0 ;
2021-10-09 07:55:26 +02:00
2023-08-09 22:01:57 +02:00
Logger :: debug ( 'Post message' , [ 'id' => $b [ 'id' ], 'parts' => count ( $msgarr [ 'parts' ])]);
foreach ( $msgarr [ 'parts' ] as $key => $part ) {
try {
$id = twitter_post_status ( $b [ 'uid' ], $part , $media_ids , $in_reply_to_tweet_id );
Logger :: info ( 'twitter_post send' , [ 'part' => $key , 'id' => $b [ 'id' ], 'result' => $id ]);
2023-08-15 22:25:17 +02:00
} catch ( RequestException $exception ) {
Logger :: warning ( 'Error while posting message' , [ 'part' => $key , 'id' => $b [ 'id' ], 'code' => $exception -> getCode (), 'message' => $exception -> getMessage ()]);
$status = [
'code' => $exception -> getCode (),
'reason' => $exception -> getResponse () -> getReasonPhrase (),
'content' => $exception -> getMessage ()
];
DI :: pConfig () -> set ( $b [ 'uid' ], 'twitter' , 'last_status' , $status );
if ( $key == 0 ) {
Worker :: defer ();
}
2023-08-09 22:01:57 +02:00
break ;
2021-05-29 21:11:53 +02:00
}
2020-06-21 22:14:08 +02:00
2023-08-09 22:01:57 +02:00
$in_reply_to_tweet_id = $id ;
$media_ids = [];
2020-11-28 19:36:00 +01:00
}
}
2023-08-09 22:01:57 +02:00
function twitter_post_status ( int $uid , string $status , array $media_ids = [], string $in_reply_to_tweet_id = '' ) : string
2017-12-08 06:18:01 +01:00
{
2023-08-09 22:01:57 +02:00
$parameters = [ 'text' => $status ];
if ( ! empty ( $media_ids )) {
$parameters [ 'media' ] = [ 'media_ids' => $media_ids ];
2017-12-08 06:18:01 +01:00
}
2023-08-09 22:01:57 +02:00
if ( ! empty ( $in_reply_to_tweet_id )) {
$parameters [ 'reply' ] = [ 'in_reply_to_tweet_id' => $in_reply_to_tweet_id ];
2017-12-08 06:18:01 +01:00
}
2013-01-01 16:11:07 +01:00
2023-08-09 22:01:57 +02:00
$response = twitter_post ( $uid , 'https://api.twitter.com/2/tweets' , 'json' , $parameters );
2017-10-19 08:40:00 +02:00
2023-08-09 22:01:57 +02:00
return $response -> data -> id ;
2011-09-25 10:56:03 +02:00
}
2023-08-15 22:25:17 +02:00
function twitter_upload_image ( int $uid , array $image , int $retrial )
2022-12-10 20:09:36 +01:00
{
if ( ! empty ( $image [ 'id' ])) {
2023-08-10 23:26:10 +02:00
$photo = Photo :: selectFirst ([], [ 'id' => $image [ 'id' ]]);
2022-12-10 20:09:36 +01:00
} else {
$photo = Photo :: createPhotoForExternalResource ( $image [ 'url' ]);
}
2023-08-10 01:13:37 +02:00
$picturedata = Photo :: getImageForPhoto ( $photo );
2023-08-15 22:25:17 +02:00
$picture = new Image ( $picturedata , $photo [ 'type' ]);
$height = $picture -> getHeight ();
2023-08-16 16:20:32 +02:00
$width = $picture -> getWidth ();
2023-08-15 22:25:17 +02:00
$size = strlen ( $picturedata );
2023-08-10 23:26:10 +02:00
2023-08-15 22:25:17 +02:00
$picture = Photo :: resizeToFileSize ( $picture , TWITTER_IMAGE_SIZE [ $retrial ]);
$new_height = $picture -> getHeight ();
2023-08-16 16:20:32 +02:00
$new_width = $picture -> getWidth ();
2023-08-10 23:26:10 +02:00
$picturedata = $picture -> asString ();
2023-08-15 22:25:17 +02:00
$new_size = strlen ( $picturedata );
2023-08-10 23:26:10 +02:00
2023-08-15 22:25:17 +02:00
Logger :: info ( 'Uploading' , [ 'uid' => $uid , 'retrial' => $retrial , 'height' => $new_height , 'width' => $new_width , 'size' => $new_size , 'orig-height' => $height , 'orig-width' => $width , 'orig-size' => $size , 'image' => $image ]);
2023-08-10 08:46:17 +02:00
$media = twitter_post ( $uid , 'https://upload.twitter.com/1.1/media/upload.json' , 'form_params' , [ 'media' => base64_encode ( $picturedata )]);
2023-08-15 22:25:17 +02:00
Logger :: info ( 'Uploading done' , [ 'uid' => $uid , 'retrial' => $retrial , 'height' => $new_height , 'width' => $new_width , 'size' => $new_size , 'orig-height' => $height , 'orig-width' => $width , 'orig-size' => $size , 'image' => $image ]);
2022-12-10 20:09:36 +01:00
if ( isset ( $media -> media_id_string )) {
$media_id = $media -> media_id_string ;
if ( ! empty ( $image [ 'description' ])) {
2023-08-09 22:01:57 +02:00
$data = [
'media_id' => $media -> media_id_string ,
'alt_text' => [
'text' => substr ( $image [ 'description' ], 0 , 1000 )
]
];
$ret = twitter_post ( $uid , 'https://upload.twitter.com/1.1/media/metadata/create.json' , 'json' , $data );
Logger :: info ( 'Metadata create' , [ 'uid' => $uid , 'data' => $data , 'return' => $ret ]);
2022-12-10 20:09:36 +01:00
}
} else {
2023-08-10 08:46:17 +02:00
Logger :: error ( 'Failed upload' , [ 'uid' => $uid , 'size' => strlen ( $picturedata ), 'image' => $image [ 'url' ], 'return' => $media ]);
2022-12-10 20:09:36 +01:00
throw new Exception ( 'Failed upload of ' . $image [ 'url' ]);
}
return $media_id ;
}
2023-08-09 22:01:57 +02:00
function twitter_post ( int $uid , string $url , string $type , array $data ) : stdClass
2021-11-23 23:59:08 +01:00
{
2023-08-09 22:01:57 +02:00
$stack = HandlerStack :: create ();
2023-08-10 23:36:09 +02:00
2023-08-09 22:01:57 +02:00
$middleware = new Oauth1 ([
'consumer_key' => DI :: pConfig () -> get ( $uid , 'twitter' , 'api_key' ),
'consumer_secret' => DI :: pConfig () -> get ( $uid , 'twitter' , 'api_secret' ),
'token' => DI :: pConfig () -> get ( $uid , 'twitter' , 'access_token' ),
'token_secret' => DI :: pConfig () -> get ( $uid , 'twitter' , 'access_secret' ),
]);
2021-11-23 23:59:08 +01:00
2023-08-09 22:01:57 +02:00
$stack -> push ( $middleware );
2023-08-10 23:36:09 +02:00
2023-08-09 22:01:57 +02:00
$client = new Client ([
'handler' => $stack
]);
2021-11-23 23:59:08 +01:00
2023-08-09 22:01:57 +02:00
$response = $client -> post ( $url , [ 'auth' => 'oauth' , $type => $data ]);
2023-08-16 16:20:32 +02:00
$body = $response -> getBody () -> getContents ();
2018-10-17 07:15:15 +02:00
2023-08-15 22:25:17 +02:00
$status = [
2023-08-16 16:20:32 +02:00
'code' => $response -> getStatusCode (),
'reason' => $response -> getReasonPhrase (),
'content' => $body
2023-08-15 22:25:17 +02:00
];
DI :: pConfig () -> set ( $uid , 'twitter' , 'last_status' , $status );
2023-08-16 16:20:32 +02:00
$content = json_decode ( $body ) ? ? new stdClass ;
2023-08-09 22:01:57 +02:00
Logger :: debug ( 'Success' , [ 'content' => $content ]);
return $content ;
2018-10-17 07:15:15 +02:00
}
2023-08-16 08:17:18 +02:00
function twitter_test_connection ( int $uid )
{
$stack = HandlerStack :: create ();
$middleware = new Oauth1 ([
'consumer_key' => DI :: pConfig () -> get ( $uid , 'twitter' , 'api_key' ),
'consumer_secret' => DI :: pConfig () -> get ( $uid , 'twitter' , 'api_secret' ),
'token' => DI :: pConfig () -> get ( $uid , 'twitter' , 'access_token' ),
'token_secret' => DI :: pConfig () -> get ( $uid , 'twitter' , 'access_secret' ),
]);
$stack -> push ( $middleware );
$client = new Client ([
'handler' => $stack
]);
2023-08-16 16:20:32 +02:00
2023-08-16 08:17:18 +02:00
try {
$response = $client -> get ( 'https://api.twitter.com/2/users/me' , [ 'auth' => 'oauth' ]);
$status = [
'code' => $response -> getStatusCode (),
'reason' => $response -> getReasonPhrase (),
'content' => $response -> getBody () -> getContents ()
];
DI :: pConfig () -> set ( 1 , 'twitter' , 'last_status' , $status );
Logger :: info ( 'Test successful' , [ 'uid' => $uid ]);
} catch ( RequestException $exception ) {
$status = [
2023-08-16 16:20:32 +02:00
'code' => $exception -> getCode (),
'reason' => $exception -> getResponse () -> getReasonPhrase (),
2023-08-16 08:17:18 +02:00
'content' => $exception -> getMessage ()
];
DI :: pConfig () -> set ( 1 , 'twitter' , 'last_status' , $status );
Logger :: info ( 'Test failed' , [ 'uid' => $uid ]);
}
2023-08-16 16:20:32 +02:00
}