[various] Use correct object class for Oauth token #919

Merged
MrPetovan merged 4 commits from bug/tumblr-oauth into 2019.12-rc 2019-12-11 23:13:17 +01:00
Showing only changes of commit 98c47f24ba - Show all commits

View file

@ -9,7 +9,8 @@
/** /**
* Tumblr OAuth class * Tumblr OAuth class
*/ */
class TumblrOAuth { class TumblrOAuth
{
/* Contains the last HTTP status code returned. */ /* Contains the last HTTP status code returned. */
public $http_code; public $http_code;
/* Contains the last API call. */ /* Contains the last API call. */
@ -34,26 +35,47 @@ class TumblrOAuth {
//public $retry = TRUE; //public $retry = TRUE;
/** /**
* Set API URLS * Set API URLS
*/ */
function accessTokenURL() { return 'https://www.tumblr.com/oauth/access_token'; } function accessTokenURL()
function authenticateURL() { return 'https://www.tumblr.com/oauth/authorize'; } {
function authorizeURL() { return 'https://www.tumblr.com/oauth/authorize'; } return 'https://www.tumblr.com/oauth/access_token';
function requestTokenURL() { return 'https://www.tumblr.com/oauth/request_token'; } }
function authenticateURL()
{
return 'https://www.tumblr.com/oauth/authorize';
}
function authorizeURL()
{
return 'https://www.tumblr.com/oauth/authorize';
}
function requestTokenURL()
{
return 'https://www.tumblr.com/oauth/request_token';
}
/** /**
* Debug helpers * Debug helpers
*/ */
function lastStatusCode() { return $this->http_status; } function lastStatusCode()
function lastAPICall() { return $this->last_api_call; } {
return $this->http_status;
}
function lastAPICall()
{
return $this->last_api_call;
}
/** /**
* construct TumblrOAuth object * construct TumblrOAuth object
*/ */
function __construct($consumer_key, $consumer_secret, $oauth_token = null, $oauth_token_secret = null) { function __construct($consumer_key, $consumer_secret, $oauth_token = null, $oauth_token_secret = null)
{
$this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1(); $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
$this->consumer = new OAuthConsumer($consumer_key, $consumer_secret); $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
if (!empty($oauth_token) && !empty($oauth_token_secret)) { if (!empty($oauth_token) && !empty($oauth_token_secret)) {
@ -69,11 +91,13 @@ class TumblrOAuth {
* *
* @returns a key/value array containing oauth_token and oauth_token_secret * @returns a key/value array containing oauth_token and oauth_token_secret
*/ */
function getRequestToken($oauth_callback = null) { function getRequestToken($oauth_callback = null)
{
$parameters = array(); $parameters = array();
if (!empty($oauth_callback)) { if (!empty($oauth_callback)) {
$parameters['oauth_callback'] = $oauth_callback; $parameters['oauth_callback'] = $oauth_callback;
} }
$request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters); $request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters);
$token = OAuthUtil::parse_parameters($request); $token = OAuthUtil::parse_parameters($request);
$this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']); $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
@ -85,10 +109,12 @@ class TumblrOAuth {
* *
* @returns a string * @returns a string
*/ */
function getAuthorizeURL($token, $sign_in_with_tumblr = TRUE) { function getAuthorizeURL($token, $sign_in_with_tumblr = TRUE)
{
if (is_array($token)) { if (is_array($token)) {
$token = $token['oauth_token']; $token = $token['oauth_token'];
} }
if (empty($sign_in_with_tumblr)) { if (empty($sign_in_with_tumblr)) {
return $this->authorizeURL() . "?oauth_token={$token}"; return $this->authorizeURL() . "?oauth_token={$token}";
} else { } else {
@ -105,14 +131,17 @@ class TumblrOAuth {
* "user_id" => "9436992", * "user_id" => "9436992",
* "screen_name" => "abraham") * "screen_name" => "abraham")
*/ */
function getAccessToken($oauth_verifier = FALSE) { function getAccessToken($oauth_verifier = FALSE)
{
$parameters = array(); $parameters = array();
if (!empty($oauth_verifier)) { if (!empty($oauth_verifier)) {
$parameters['oauth_verifier'] = $oauth_verifier; $parameters['oauth_verifier'] = $oauth_verifier;
} }
$request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters); $request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters);
$token = OAuthUtil::parse_parameters($request); $token = OAuthUtil::parse_parameters($request);
$this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']); $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
return $token; return $token;
} }
@ -125,7 +154,8 @@ class TumblrOAuth {
* "screen_name" => "abraham", * "screen_name" => "abraham",
* "x_auth_expires" => "0") * "x_auth_expires" => "0")
*/ */
function getXAuthToken($username, $password) { function getXAuthToken($username, $password)
{
$parameters = array(); $parameters = array();
$parameters['x_auth_username'] = $username; $parameters['x_auth_username'] = $username;
$parameters['x_auth_password'] = $password; $parameters['x_auth_password'] = $password;
@ -133,49 +163,58 @@ class TumblrOAuth {
$request = $this->oAuthRequest($this->accessTokenURL(), 'POST', $parameters); $request = $this->oAuthRequest($this->accessTokenURL(), 'POST', $parameters);
$token = OAuthUtil::parse_parameters($request); $token = OAuthUtil::parse_parameters($request);
$this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']); $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
return $token; return $token;
} }
/** /**
* GET wrapper for oAuthRequest. * GET wrapper for oAuthRequest.
*/ */
function get($url, $parameters = array()) { function get($url, $parameters = array())
{
$response = $this->oAuthRequest($url, 'GET', $parameters); $response = $this->oAuthRequest($url, 'GET', $parameters);
if ($this->format === 'json' && $this->decode_json) { if ($this->format === 'json' && $this->decode_json) {
return json_decode($response); return json_decode($response);
} }
return $response; return $response;
} }
/** /**
* POST wrapper for oAuthRequest. * POST wrapper for oAuthRequest.
*/ */
function post($url, $parameters = array()) { function post($url, $parameters = array())
{
$response = $this->oAuthRequest($url, 'POST', $parameters); $response = $this->oAuthRequest($url, 'POST', $parameters);
if ($this->format === 'json' && $this->decode_json) { if ($this->format === 'json' && $this->decode_json) {
return json_decode($response); return json_decode($response);
} }
return $response; return $response;
} }
/** /**
* DELETE wrapper for oAuthReqeust. * DELETE wrapper for oAuthReqeust.
*/ */
function delete($url, $parameters = array()) { function delete($url, $parameters = array())
{
$response = $this->oAuthRequest($url, 'DELETE', $parameters); $response = $this->oAuthRequest($url, 'DELETE', $parameters);
if ($this->format === 'json' && $this->decode_json) { if ($this->format === 'json' && $this->decode_json) {
return json_decode($response); return json_decode($response);
} }
return $response; return $response;
} }
/** /**
* Format and sign an OAuth / API request * Format and sign an OAuth / API request
*/ */
function oAuthRequest($url, $method, $parameters) { function oAuthRequest($url, $method, $parameters)
{
if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) { if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) {
$url = "{$this->host}{$url}"; $url = "{$this->host}{$url}";
} }
$request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters); $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
$request->sign_request($this->sha1_method, $this->consumer, $this->token); $request->sign_request($this->sha1_method, $this->consumer, $this->token);
switch ($method) { switch ($method) {
@ -191,7 +230,8 @@ class TumblrOAuth {
* *
* @return API results * @return API results
*/ */
function http($url, $method, $postfields = null) { function http($url, $method, $postfields = null)
{
$this->http_info = array(); $this->http_info = array();
$ci = curl_init(); $ci = curl_init();
/* Curl settings */ /* Curl settings */
@ -224,19 +264,22 @@ class TumblrOAuth {
$this->http_info = array_merge($this->http_info, curl_getinfo($ci)); $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
$this->url = $url; $this->url = $url;
curl_close($ci); curl_close($ci);
return $response; return $response;
} }
/** /**
* Get the header info to store. * Get the header info to store.
*/ */
function getHeader($ch, $header) { function getHeader($ch, $header)
{
$i = strpos($header, ':'); $i = strpos($header, ':');
if (!empty($i)) { if (!empty($i)) {
$key = str_replace('-', '_', strtolower(substr($header, 0, $i))); $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
$value = trim(substr($header, $i + 2)); $value = trim(substr($header, $i + 2));
$this->http_header[$key] = $value; $this->http_header[$key] = $value;
} }
return strlen($header); return strlen($header);
} }
} }