[tumblr] Fix formatting in library/tumblroauth

This commit is contained in:
Benjamin Lorteau 2019-12-11 13:04:36 -05:00
parent cfe921ac75
commit 98c47f24ba

View file

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