2018-01-28 19:43:56 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Abraham Williams (abraham@abrah.am) http://abrah.am
|
|
|
|
*
|
|
|
|
* The first PHP Library to support OAuth for Tumblr's REST API. (Originally for Twitter, modified for Tumblr by Lucas)
|
|
|
|
*/
|
|
|
|
|
2023-04-06 19:10:32 +00:00
|
|
|
use Friendica\DI;
|
2020-09-30 09:21:58 +00:00
|
|
|
use Friendica\Security\OAuth1\OAuthConsumer;
|
|
|
|
use Friendica\Security\OAuth1\OAuthRequest;
|
2020-09-30 09:26:52 +00:00
|
|
|
use Friendica\Security\OAuth1\Signature\OAuthSignatureMethod_HMAC_SHA1;
|
2020-09-30 09:21:58 +00:00
|
|
|
use Friendica\Security\OAuth1\OAuthToken;
|
|
|
|
use Friendica\Security\OAuth1\OAuthUtil;
|
|
|
|
|
2018-01-28 19:43:56 +00:00
|
|
|
/**
|
|
|
|
* Tumblr OAuth class
|
|
|
|
*/
|
2019-12-11 18:04:36 +00:00
|
|
|
class TumblrOAuth
|
|
|
|
{
|
2019-12-11 18:18:17 +00:00
|
|
|
/* Contains the last HTTP status code returned. */
|
|
|
|
public $http_code;
|
|
|
|
|
|
|
|
/** @var OAuthConsumer */
|
|
|
|
private $consumer;
|
2020-09-30 09:26:52 +00:00
|
|
|
/** @var \Friendica\Security\OAuth1\Signature\OAuthSignatureMethod_HMAC_SHA1 */
|
2019-12-11 18:18:17 +00:00
|
|
|
private $sha1_method;
|
2019-12-11 18:04:36 +00:00
|
|
|
|
2023-04-06 19:10:32 +00:00
|
|
|
// API URLs
|
|
|
|
const accessTokenURL = 'https://www.tumblr.com/oauth/access_token';
|
|
|
|
const authorizeURL = 'https://www.tumblr.com/oauth/authorize';
|
|
|
|
const requestTokenURL = 'https://www.tumblr.com/oauth/request_token';
|
2019-12-11 18:04:36 +00:00
|
|
|
|
2023-04-06 19:10:32 +00:00
|
|
|
function __construct(string $consumer_key, string $consumer_secret)
|
2019-12-11 18:04:36 +00:00
|
|
|
{
|
|
|
|
$this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
|
2023-04-06 19:10:32 +00:00
|
|
|
$this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
|
2019-12-11 18:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a request_token from Tumblr
|
|
|
|
*
|
2023-04-06 19:10:32 +00:00
|
|
|
* @param string $oauth_callback
|
2019-12-11 18:18:17 +00:00
|
|
|
* @return array
|
2019-12-11 18:04:36 +00:00
|
|
|
*/
|
2023-04-06 19:10:32 +00:00
|
|
|
function getRequestToken(string $oauth_callback): array
|
2019-12-11 18:04:36 +00:00
|
|
|
{
|
2023-04-06 19:10:32 +00:00
|
|
|
$request = $this->oAuthRequest(self::requestTokenURL, ['oauth_callback' => $oauth_callback]);
|
|
|
|
return OAuthUtil::parse_parameters($request);
|
2019-12-11 18:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the authorize URL
|
|
|
|
*
|
2023-04-06 19:10:32 +00:00
|
|
|
* @param string $oauth_token
|
2019-12-11 18:18:17 +00:00
|
|
|
* @return string
|
2019-12-11 18:04:36 +00:00
|
|
|
*/
|
2023-04-06 19:10:32 +00:00
|
|
|
function getAuthorizeURL(string $oauth_token): string
|
2019-12-11 18:04:36 +00:00
|
|
|
{
|
2023-04-06 19:10:32 +00:00
|
|
|
return self::authorizeURL . "?oauth_token={$oauth_token}";
|
2019-12-11 18:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exchange request token and secret for an access token and
|
|
|
|
* secret, to sign API calls.
|
|
|
|
*
|
2023-04-06 19:10:32 +00:00
|
|
|
* @param string $oauth_verifier
|
|
|
|
* @param string $request_token
|
|
|
|
* @param string $request_token_secret
|
2019-12-11 18:18:17 +00:00
|
|
|
* @return array ("oauth_token" => "the-access-token",
|
2019-12-11 18:04:36 +00:00
|
|
|
* "oauth_token_secret" => "the-access-secret",
|
|
|
|
* "user_id" => "9436992",
|
|
|
|
* "screen_name" => "abraham")
|
|
|
|
*/
|
2023-04-06 19:10:32 +00:00
|
|
|
function getAccessToken(string $oauth_verifier, string $request_token, string $request_token_secret): array
|
2019-12-11 18:04:36 +00:00
|
|
|
{
|
2023-04-06 19:10:32 +00:00
|
|
|
$token = new OAuthToken($request_token, $request_token_secret);
|
|
|
|
|
2019-12-11 18:18:17 +00:00
|
|
|
$parameters = [];
|
2019-12-11 18:04:36 +00:00
|
|
|
if (!empty($oauth_verifier)) {
|
|
|
|
$parameters['oauth_verifier'] = $oauth_verifier;
|
|
|
|
}
|
|
|
|
|
2023-04-06 19:10:32 +00:00
|
|
|
$request = $this->oAuthRequest(self::accessTokenURL, $parameters, $token);
|
|
|
|
return OAuthUtil::parse_parameters($request);
|
2019-12-11 18:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format and sign an OAuth / API request
|
2019-12-11 18:18:17 +00:00
|
|
|
*
|
2023-04-06 19:10:32 +00:00
|
|
|
* @param string $url
|
|
|
|
* @param array $parameters
|
|
|
|
* @param OAuthToken $token $name
|
|
|
|
* @return string
|
2019-12-11 18:04:36 +00:00
|
|
|
*/
|
2023-04-06 19:10:32 +00:00
|
|
|
private function oAuthRequest(string $url, array $parameters, OAuthToken $token = null): string
|
2019-12-11 18:04:36 +00:00
|
|
|
{
|
2023-04-06 19:10:32 +00:00
|
|
|
$request = OAuthRequest::from_consumer_and_token($this->consumer, 'GET', $url, $parameters, $token);
|
|
|
|
$request->sign_request($this->sha1_method, $this->consumer, $token);
|
2019-12-11 18:04:36 +00:00
|
|
|
|
2023-04-06 19:10:32 +00:00
|
|
|
$curlResult = DI::httpClient()->get($request->to_url());
|
|
|
|
$this->http_code = $curlResult->getReturnCode();
|
|
|
|
if ($curlResult->isSuccess()) {
|
|
|
|
return $curlResult->getBody();
|
2019-12-11 18:04:36 +00:00
|
|
|
}
|
2023-04-06 19:10:32 +00:00
|
|
|
return '';
|
2019-12-11 18:04:36 +00:00
|
|
|
}
|
2018-01-28 19:43:56 +00:00
|
|
|
}
|