[twitter] Update abraham/twitteroauth dependency

This commit is contained in:
Philipp 2021-09-13 20:51:12 +02:00
parent 30445b3c85
commit 569e3f4831
No known key found for this signature in database
GPG key ID: 24A7501396EB5432
167 changed files with 11848 additions and 2157 deletions

View file

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace Abraham\TwitterOAuth;
/**
@ -13,6 +15,11 @@ class Config
protected $timeout = 5;
/** @var int how long to wait while connecting to the API */
protected $connectionTimeout = 5;
/** @var int How many times we retry request when API is down */
protected $maxRetries = 0;
/** @var int Delay in seconds before we retry the request */
protected $retriesDelay = 1;
/**
* Decode JSON Response as associative Array
*
@ -38,32 +45,44 @@ class Config
* @param int $connectionTimeout
* @param int $timeout
*/
public function setTimeouts($connectionTimeout, $timeout)
public function setTimeouts(int $connectionTimeout, int $timeout): void
{
$this->connectionTimeout = (int)$connectionTimeout;
$this->timeout = (int)$timeout;
$this->connectionTimeout = $connectionTimeout;
$this->timeout = $timeout;
}
/**
* Set the number of times to retry on error and how long between each.
*
* @param int $maxRetries
* @param int $retriesDelay
*/
public function setRetries(int $maxRetries, int $retriesDelay): void
{
$this->maxRetries = $maxRetries;
$this->retriesDelay = $retriesDelay;
}
/**
* @param bool $value
*/
public function setDecodeJsonAsArray($value)
public function setDecodeJsonAsArray(bool $value): void
{
$this->decodeJsonAsArray = (bool)$value;
$this->decodeJsonAsArray = $value;
}
/**
* @param string $userAgent
*/
public function setUserAgent($userAgent)
public function setUserAgent(string $userAgent): void
{
$this->userAgent = (string)$userAgent;
$this->userAgent = $userAgent;
}
/**
* @param array $proxy
*/
public function setProxy(array $proxy)
public function setProxy(array $proxy): void
{
$this->proxy = $proxy;
}
@ -73,9 +92,9 @@ class Config
*
* @param boolean $gzipEncoding
*/
public function setGzipEncoding($gzipEncoding)
public function setGzipEncoding(bool $gzipEncoding): void
{
$this->gzipEncoding = (bool)$gzipEncoding;
$this->gzipEncoding = $gzipEncoding;
}
/**
@ -83,8 +102,8 @@ class Config
*
* @param int $value
*/
public function setChunkSize($value)
public function setChunkSize(int $value): void
{
$this->chunkSize = (int)$value;
$this->chunkSize = $value;
}
}

View file

@ -1,8 +1,12 @@
<?php
/**
* The MIT License
* Copyright (c) 2007 Andy Smith
*/
declare(strict_types=1);
namespace Abraham\TwitterOAuth;
class Consumer
@ -15,12 +19,15 @@ class Consumer
public $callbackUrl;
/**
* @param string $key
* @param string $secret
* @param string|null $key
* @param string|null $secret
* @param null $callbackUrl
*/
public function __construct($key, $secret, $callbackUrl = null)
{
public function __construct(
?string $key,
?string $secret,
?string $callbackUrl = null
) {
$this->key = $key;
$this->secret = $secret;
$this->callbackUrl = $callbackUrl;

View file

@ -1,8 +1,12 @@
<?php
/**
* The MIT License
* Copyright (c) 2007 Andy Smith
*/
declare(strict_types=1);
namespace Abraham\TwitterOAuth;
/**
@ -19,17 +23,20 @@ class HmacSha1 extends SignatureMethod
*/
public function getName()
{
return "HMAC-SHA1";
return 'HMAC-SHA1';
}
/**
* {@inheritDoc}
*/
public function buildSignature(Request $request, Consumer $consumer, Token $token = null)
{
public function buildSignature(
Request $request,
Consumer $consumer,
Token $token = null
): string {
$signatureBase = $request->getSignatureBaseString();
$parts = [$consumer->secret, null !== $token ? $token->secret : ""];
$parts = [$consumer->secret, null !== $token ? $token->secret : ''];
$parts = Util::urlencodeRfc3986($parts);
$key = implode('&', $parts);

View file

@ -1,8 +1,12 @@
<?php
/**
* The MIT License
* Copyright (c) 2007 Andy Smith
*/
declare(strict_types=1);
namespace Abraham\TwitterOAuth;
class Request
@ -10,6 +14,7 @@ class Request
protected $parameters;
protected $httpMethod;
protected $httpUrl;
protected $json;
public static $version = '1.0';
/**
@ -19,9 +24,15 @@ class Request
* @param string $httpUrl
* @param array|null $parameters
*/
public function __construct($httpMethod, $httpUrl, array $parameters = [])
{
$parameters = array_merge(Util::parseParameters(parse_url($httpUrl, PHP_URL_QUERY)), $parameters);
public function __construct(
string $httpMethod,
string $httpUrl,
?array $parameters = []
) {
$parameters = array_merge(
Util::parseParameters(parse_url($httpUrl, PHP_URL_QUERY)),
$parameters
);
$this->parameters = $parameters;
$this->httpMethod = $httpMethod;
$this->httpUrl = $httpUrl;
@ -41,21 +52,28 @@ class Request
public static function fromConsumerAndToken(
Consumer $consumer,
Token $token = null,
$httpMethod,
$httpUrl,
array $parameters = []
string $httpMethod,
string $httpUrl,
array $parameters = [],
$json = false
) {
$defaults = [
"oauth_version" => Request::$version,
"oauth_nonce" => Request::generateNonce(),
"oauth_timestamp" => time(),
"oauth_consumer_key" => $consumer->key
'oauth_version' => Request::$version,
'oauth_nonce' => Request::generateNonce(),
'oauth_timestamp' => time(),
'oauth_consumer_key' => $consumer->key,
];
if (null !== $token) {
$defaults['oauth_token'] = $token->key;
}
$parameters = array_merge($defaults, $parameters);
// The json payload is not included in the signature on json requests,
// therefore it shouldn't be included in the parameters array.
if ($json) {
$parameters = $defaults;
} else {
$parameters = array_merge($defaults, $parameters);
}
return new Request($httpMethod, $httpUrl, $parameters);
}
@ -64,33 +82,35 @@ class Request
* @param string $name
* @param string $value
*/
public function setParameter($name, $value)
public function setParameter(string $name, string $value)
{
$this->parameters[$name] = $value;
}
/**
* @param $name
* @param string $name
*
* @return string|null
*/
public function getParameter($name)
public function getParameter(string $name): ?string
{
return isset($this->parameters[$name]) ? $this->parameters[$name] : null;
return isset($this->parameters[$name])
? $this->parameters[$name]
: null;
}
/**
* @return array
*/
public function getParameters()
public function getParameters(): array
{
return $this->parameters;
}
/**
* @param $name
* @param string $name
*/
public function removeParameter($name)
public function removeParameter(string $name): void
{
unset($this->parameters[$name]);
}
@ -100,7 +120,7 @@ class Request
*
* @return string
*/
public function getSignableParameters()
public function getSignableParameters(): string
{
// Grab all parameters
$params = $this->parameters;
@ -123,12 +143,12 @@ class Request
*
* @return string
*/
public function getSignatureBaseString()
public function getSignatureBaseString(): string
{
$parts = [
$this->getNormalizedHttpMethod(),
$this->getNormalizedHttpUrl(),
$this->getSignableParameters()
$this->getSignableParameters(),
];
$parts = Util::urlencodeRfc3986($parts);
@ -141,7 +161,7 @@ class Request
*
* @return string
*/
public function getNormalizedHttpMethod()
public function getNormalizedHttpMethod(): string
{
return strtoupper($this->httpMethod);
}
@ -152,7 +172,7 @@ class Request
*
* @return string
*/
public function getNormalizedHttpUrl()
public function getNormalizedHttpUrl(): string
{
$parts = parse_url($this->httpUrl);
@ -168,7 +188,7 @@ class Request
*
* @return string
*/
public function toUrl()
public function toUrl(): string
{
$postData = $this->toPostdata();
$out = $this->getNormalizedHttpUrl();
@ -183,7 +203,7 @@ class Request
*
* @return string
*/
public function toPostdata()
public function toPostdata(): string
{
return Util::buildHttpQuery($this->parameters);
}
@ -194,19 +214,25 @@ class Request
* @return string
* @throws TwitterOAuthException
*/
public function toHeader()
public function toHeader(): string
{
$first = true;
$out = 'Authorization: OAuth';
foreach ($this->parameters as $k => $v) {
if (substr($k, 0, 5) != "oauth") {
if (substr($k, 0, 5) != 'oauth') {
continue;
}
if (is_array($v)) {
throw new TwitterOAuthException('Arrays not supported in headers');
throw new TwitterOAuthException(
'Arrays not supported in headers'
);
}
$out .= ($first) ? ' ' : ', ';
$out .= Util::urlencodeRfc3986($k) . '="' . Util::urlencodeRfc3986($v) . '"';
$out .= $first ? ' ' : ', ';
$out .=
Util::urlencodeRfc3986($k) .
'="' .
Util::urlencodeRfc3986($v) .
'"';
$first = false;
}
return $out;
@ -215,7 +241,7 @@ class Request
/**
* @return string
*/
public function __toString()
public function __toString(): string
{
return $this->toUrl();
}
@ -225,11 +251,17 @@ class Request
* @param Consumer $consumer
* @param Token $token
*/
public function signRequest(SignatureMethod $signatureMethod, Consumer $consumer, Token $token = null)
{
$this->setParameter("oauth_signature_method", $signatureMethod->getName());
public function signRequest(
SignatureMethod $signatureMethod,
Consumer $consumer,
Token $token = null
) {
$this->setParameter(
'oauth_signature_method',
$signatureMethod->getName()
);
$signature = $this->buildSignature($signatureMethod, $consumer, $token);
$this->setParameter("oauth_signature", $signature);
$this->setParameter('oauth_signature', $signature);
}
/**
@ -239,15 +271,18 @@ class Request
*
* @return string
*/
public function buildSignature(SignatureMethod $signatureMethod, Consumer $consumer, Token $token = null)
{
public function buildSignature(
SignatureMethod $signatureMethod,
Consumer $consumer,
Token $token = null
): string {
return $signatureMethod->buildSignature($this, $consumer, $token);
}
/**
* @return string
*/
public static function generateNonce()
public static function generateNonce(): string
{
return md5(microtime() . mt_rand());
}

View file

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace Abraham\TwitterOAuth;
/**
@ -23,7 +25,7 @@ class Response
/**
* @param string $apiPath
*/
public function setApiPath($apiPath)
public function setApiPath(string $apiPath): void
{
$this->apiPath = $apiPath;
}
@ -31,7 +33,7 @@ class Response
/**
* @return string|null
*/
public function getApiPath()
public function getApiPath(): ?string
{
return $this->apiPath;
}
@ -55,7 +57,7 @@ class Response
/**
* @param int $httpCode
*/
public function setHttpCode($httpCode)
public function setHttpCode(int $httpCode): void
{
$this->httpCode = $httpCode;
}
@ -63,7 +65,7 @@ class Response
/**
* @return int
*/
public function getHttpCode()
public function getHttpCode(): int
{
return $this->httpCode;
}
@ -71,7 +73,7 @@ class Response
/**
* @param array $headers
*/
public function setHeaders(array $headers)
public function setHeaders(array $headers): void
{
foreach ($headers as $key => $value) {
if (substr($key, 0, 1) == 'x') {
@ -84,7 +86,7 @@ class Response
/**
* @return array
*/
public function getsHeaders()
public function getsHeaders(): array
{
return $this->headers;
}
@ -92,7 +94,7 @@ class Response
/**
* @param array $xHeaders
*/
public function setXHeaders(array $xHeaders = [])
public function setXHeaders(array $xHeaders = []): void
{
$this->xHeaders = $xHeaders;
}
@ -100,7 +102,7 @@ class Response
/**
* @return array
*/
public function getXHeaders()
public function getXHeaders(): array
{
return $this->xHeaders;
}

View file

@ -1,8 +1,12 @@
<?php
/**
* The MIT License
* Copyright (c) 2007 Andy Smith
*/
declare(strict_types=1);
namespace Abraham\TwitterOAuth;
/**
@ -30,7 +34,11 @@ abstract class SignatureMethod
*
* @return string
*/
abstract public function buildSignature(Request $request, Consumer $consumer, Token $token = null);
abstract public function buildSignature(
Request $request,
Consumer $consumer,
Token $token = null
);
/**
* Verifies that a given signature is correct
@ -42,8 +50,12 @@ abstract class SignatureMethod
*
* @return bool
*/
public function checkSignature(Request $request, Consumer $consumer, Token $token, $signature)
{
public function checkSignature(
Request $request,
Consumer $consumer,
Token $token,
string $signature
): bool {
$built = $this->buildSignature($request, $consumer, $token);
// Check for zero length, although unlikely here
@ -58,7 +70,7 @@ abstract class SignatureMethod
// Avoid a timing leak with a (hopefully) time insensitive compare
$result = 0;
for ($i = 0; $i < strlen($signature); $i++) {
$result |= ord($built{$i}) ^ ord($signature{$i});
$result |= ord($built[$i]) ^ ord($signature[$i]);
}
return $result == 0;

View file

@ -1,8 +1,12 @@
<?php
/**
* The MIT License
* Copyright (c) 2007 Andy Smith
*/
declare(strict_types=1);
namespace Abraham\TwitterOAuth;
class Token
@ -16,7 +20,7 @@ class Token
* @param string $key The OAuth Token
* @param string $secret The OAuth Token Secret
*/
public function __construct($key, $secret)
public function __construct(?string $key, ?string $secret)
{
$this->key = $key;
$this->secret = $secret;
@ -28,9 +32,10 @@ class Token
*
* @return string
*/
public function __toString()
public function __toString(): string
{
return sprintf("oauth_token=%s&oauth_token_secret=%s",
return sprintf(
'oauth_token=%s&oauth_token_secret=%s',
Util::urlencodeRfc3986($this->key),
Util::urlencodeRfc3986($this->secret)
);

View file

@ -1,12 +1,17 @@
<?php
/**
* The most popular PHP library for use with the Twitter OAuth REST API.
*
* @license MIT
*/
declare(strict_types=1);
namespace Abraham\TwitterOAuth;
use Abraham\TwitterOAuth\Util\JsonDecoder;
use Composer\CaBundle\CaBundle;
/**
* TwitterOAuth class for interacting with the Twitter API.
@ -15,9 +20,9 @@ use Abraham\TwitterOAuth\Util\JsonDecoder;
*/
class TwitterOAuth extends Config
{
const API_VERSION = '1.1';
const API_HOST = 'https://api.twitter.com';
const UPLOAD_HOST = 'https://upload.twitter.com';
private const API_VERSION = '1.1';
private const API_HOST = 'https://api.twitter.com';
private const UPLOAD_HOST = 'https://upload.twitter.com';
/** @var Response details about the result of the last request */
private $response;
@ -29,6 +34,8 @@ class TwitterOAuth extends Config
private $token;
/** @var HmacSha1 OAuth 1 signature type used by Twitter */
private $signatureMethod;
/** @var int Number of attempts we made for the request */
private $attempts = 0;
/**
* Constructor
@ -38,16 +45,20 @@ class TwitterOAuth extends Config
* @param string|null $oauthToken The Client Token (optional)
* @param string|null $oauthTokenSecret The Client Token Secret (optional)
*/
public function __construct($consumerKey, $consumerSecret, $oauthToken = null, $oauthTokenSecret = null)
{
public function __construct(
string $consumerKey,
string $consumerSecret,
?string $oauthToken = null,
?string $oauthTokenSecret = null
) {
$this->resetLastResponse();
$this->signatureMethod = new HmacSha1();
$this->consumer = new Consumer($consumerKey, $consumerSecret);
if (!empty($oauthToken) && !empty($oauthTokenSecret)) {
$this->token = new Token($oauthToken, $oauthTokenSecret);
$this->setOauthToken($oauthToken, $oauthTokenSecret);
}
if (empty($oauthToken) && !empty($oauthTokenSecret)) {
$this->bearer = $oauthTokenSecret;
$this->setBearer($oauthTokenSecret);
}
}
@ -55,15 +66,27 @@ class TwitterOAuth extends Config
* @param string $oauthToken
* @param string $oauthTokenSecret
*/
public function setOauthToken($oauthToken, $oauthTokenSecret)
{
public function setOauthToken(
string $oauthToken,
string $oauthTokenSecret
): void {
$this->token = new Token($oauthToken, $oauthTokenSecret);
$this->bearer = null;
}
/**
* @param string $oauthTokenSecret
*/
public function setBearer(string $oauthTokenSecret): void
{
$this->bearer = $oauthTokenSecret;
$this->token = null;
}
/**
* @return string|null
*/
public function getLastApiPath()
public function getLastApiPath(): ?string
{
return $this->response->getApiPath();
}
@ -71,7 +94,7 @@ class TwitterOAuth extends Config
/**
* @return int
*/
public function getLastHttpCode()
public function getLastHttpCode(): int
{
return $this->response->getHttpCode();
}
@ -79,7 +102,7 @@ class TwitterOAuth extends Config
/**
* @return array
*/
public function getLastXHeaders()
public function getLastXHeaders(): array
{
return $this->response->getXHeaders();
}
@ -95,11 +118,29 @@ class TwitterOAuth extends Config
/**
* Resets the last response cache.
*/
public function resetLastResponse()
public function resetLastResponse(): void
{
$this->response = new Response();
}
/**
* Resets the attempts number.
*/
private function resetAttemptsNumber(): void
{
$this->attempts = 0;
}
/**
* Delays the retries when they're activated.
*/
private function sleepIfNeeded(): void
{
if ($this->maxRetries && $this->attempts) {
sleep($this->retriesDelay);
}
}
/**
* Make URLs for user browser navigation.
*
@ -108,7 +149,7 @@ class TwitterOAuth extends Config
*
* @return string
*/
public function url($path, array $parameters)
public function url(string $path, array $parameters): string
{
$this->resetLastResponse();
$this->response->setApiPath($path);
@ -125,7 +166,7 @@ class TwitterOAuth extends Config
* @return array
* @throws TwitterOAuthException
*/
public function oauth($path, array $parameters = [])
public function oauth(string $path, array $parameters = []): array
{
$response = [];
$this->resetLastResponse();
@ -151,15 +192,28 @@ class TwitterOAuth extends Config
*
* @return array|object
*/
public function oauth2($path, array $parameters = [])
public function oauth2(string $path, array $parameters = [])
{
$method = 'POST';
$this->resetLastResponse();
$this->response->setApiPath($path);
$url = sprintf('%s/%s', self::API_HOST, $path);
$request = Request::fromConsumerAndToken($this->consumer, $this->token, $method, $url, $parameters);
$authorization = 'Authorization: Basic ' . $this->encodeAppAuthorization($this->consumer);
$result = $this->request($request->getNormalizedHttpUrl(), $method, $authorization, $parameters);
$request = Request::fromConsumerAndToken(
$this->consumer,
$this->token,
$method,
$url,
$parameters
);
$authorization =
'Authorization: Basic ' .
$this->encodeAppAuthorization($this->consumer);
$result = $this->request(
$request->getNormalizedHttpUrl(),
$method,
$authorization,
$parameters
);
$response = JsonDecoder::decode($result, $this->decodeJsonAsArray);
$this->response->setBody($response);
return $response;
@ -173,9 +227,9 @@ class TwitterOAuth extends Config
*
* @return array|object
*/
public function get($path, array $parameters = [])
public function get(string $path, array $parameters = [])
{
return $this->http('GET', self::API_HOST, $path, $parameters);
return $this->http('GET', self::API_HOST, $path, $parameters, false);
}
/**
@ -183,12 +237,16 @@ class TwitterOAuth extends Config
*
* @param string $path
* @param array $parameters
* @param bool $json
*
* @return array|object
*/
public function post($path, array $parameters = [])
{
return $this->http('POST', self::API_HOST, $path, $parameters);
public function post(
string $path,
array $parameters = [],
bool $json = false
) {
return $this->http('POST', self::API_HOST, $path, $parameters, $json);
}
/**
@ -199,9 +257,9 @@ class TwitterOAuth extends Config
*
* @return array|object
*/
public function delete($path, array $parameters = [])
public function delete(string $path, array $parameters = [])
{
return $this->http('DELETE', self::API_HOST, $path, $parameters);
return $this->http('DELETE', self::API_HOST, $path, $parameters, false);
}
/**
@ -212,9 +270,9 @@ class TwitterOAuth extends Config
*
* @return array|object
*/
public function put($path, array $parameters = [])
public function put(string $path, array $parameters = [])
{
return $this->http('PUT', self::API_HOST, $path, $parameters);
return $this->http('PUT', self::API_HOST, $path, $parameters, false);
}
/**
@ -226,8 +284,11 @@ class TwitterOAuth extends Config
*
* @return array|object
*/
public function upload($path, array $parameters = [], $chunked = false)
{
public function upload(
string $path,
array $parameters = [],
bool $chunked = false
) {
if ($chunked) {
return $this->uploadMediaChunked($path, $parameters);
} else {
@ -235,6 +296,27 @@ class TwitterOAuth extends Config
}
}
/**
* Progression of media upload
*
* @param string $media_id
*
* @return array|object
*/
public function mediaStatus(string $media_id)
{
return $this->http(
'GET',
self::UPLOAD_HOST,
'media/upload',
[
'command' => 'STATUS',
'media_id' => $media_id,
],
false
);
}
/**
* Private method to upload media (not chunked) to upload.twitter.com.
*
@ -243,12 +325,24 @@ class TwitterOAuth extends Config
*
* @return array|object
*/
private function uploadMediaNotChunked($path, array $parameters)
private function uploadMediaNotChunked(string $path, array $parameters)
{
$file = file_get_contents($parameters['media']);
$base = base64_encode($file);
$parameters['media'] = $base;
return $this->http('POST', self::UPLOAD_HOST, $path, $parameters);
if (
!is_readable($parameters['media']) ||
($file = file_get_contents($parameters['media'])) === false
) {
throw new \InvalidArgumentException(
'You must supply a readable file'
);
}
$parameters['media'] = base64_encode($file);
return $this->http(
'POST',
self::UPLOAD_HOST,
$path,
$parameters,
false
);
}
/**
@ -259,27 +353,46 @@ class TwitterOAuth extends Config
*
* @return array|object
*/
private function uploadMediaChunked($path, array $parameters)
private function uploadMediaChunked(string $path, array $parameters)
{
$init = $this->http('POST', self::UPLOAD_HOST, $path, $this->mediaInitParameters($parameters));
$init = $this->http(
'POST',
self::UPLOAD_HOST,
$path,
$this->mediaInitParameters($parameters),
false
);
// Append
$segmentIndex = 0;
$media = fopen($parameters['media'], 'rb');
while (!feof($media))
{
$this->http('POST', self::UPLOAD_HOST, 'media/upload', [
'command' => 'APPEND',
'media_id' => $init->media_id_string,
'segment_index' => $segmentIndex++,
'media_data' => base64_encode(fread($media, $this->chunkSize))
]);
while (!feof($media)) {
$this->http(
'POST',
self::UPLOAD_HOST,
'media/upload',
[
'command' => 'APPEND',
'media_id' => $init->media_id_string,
'segment_index' => $segmentIndex++,
'media_data' => base64_encode(
fread($media, $this->chunkSize)
),
],
false
);
}
fclose($media);
// Finalize
$finalize = $this->http('POST', self::UPLOAD_HOST, 'media/upload', [
'command' => 'FINALIZE',
'media_id' => $init->media_id_string
]);
$finalize = $this->http(
'POST',
self::UPLOAD_HOST,
'media/upload',
[
'command' => 'FINALIZE',
'media_id' => $init->media_id_string,
],
false
);
return $finalize;
}
@ -291,20 +404,41 @@ class TwitterOAuth extends Config
*
* @return array
*/
private function mediaInitParameters(array $parameters)
private function mediaInitParameters(array $parameters): array
{
$return = [
'command' => 'INIT',
'media_type' => $parameters['media_type'],
'total_bytes' => filesize($parameters['media'])
$allowed_keys = [
'media_type',
'additional_owners',
'media_category',
'shared',
];
if (isset($parameters['additional_owners'])) {
$return['additional_owners'] = $parameters['additional_owners'];
$base = [
'command' => 'INIT',
'total_bytes' => filesize($parameters['media']),
];
$allowed_parameters = array_intersect_key(
$parameters,
array_flip($allowed_keys)
);
return array_merge($base, $allowed_parameters);
}
/**
* Cleanup any parameters that are known not to work.
*
* @param array $parameters
*
* @return array
*/
private function cleanUpParameters(array $parameters)
{
foreach ($parameters as $key => $value) {
// PHP coerces `true` to `"1"` which some Twitter APIs don't like.
if (is_bool($value)) {
$parameters[$key] = var_export($value, true);
}
}
if (isset($parameters['media_category'])) {
$return['media_category'] = $parameters['media_category'];
}
return $return;
return $parameters;
}
/**
@ -312,39 +446,104 @@ class TwitterOAuth extends Config
* @param string $host
* @param string $path
* @param array $parameters
* @param bool $json
*
* @return array|object
*/
private function http($method, $host, $path, array $parameters)
{
private function http(
string $method,
string $host,
string $path,
array $parameters,
bool $json
) {
$this->resetLastResponse();
$this->resetAttemptsNumber();
$url = sprintf('%s/%s/%s.json', $host, self::API_VERSION, $path);
$this->response->setApiPath($path);
$result = $this->oAuthRequest($url, $method, $parameters);
$response = JsonDecoder::decode($result, $this->decodeJsonAsArray);
$this->response->setBody($response);
if (!$json) {
$parameters = $this->cleanUpParameters($parameters);
}
return $this->makeRequests($url, $method, $parameters, $json);
}
/**
*
* Make requests and retry them (if enabled) in case of Twitter's problems.
*
* @param string $method
* @param string $url
* @param string $method
* @param array $parameters
* @param bool $json
*
* @return array|object
*/
private function makeRequests(
string $url,
string $method,
array $parameters,
bool $json
) {
do {
$this->sleepIfNeeded();
$result = $this->oAuthRequest($url, $method, $parameters, $json);
$response = JsonDecoder::decode($result, $this->decodeJsonAsArray);
$this->response->setBody($response);
$this->attempts++;
// Retry up to our $maxRetries number if we get errors greater than 500 (over capacity etc)
} while ($this->requestsAvailable());
return $response;
}
/**
* Checks if we have to retry request if API is down.
*
* @return bool
*/
private function requestsAvailable(): bool
{
return $this->maxRetries &&
$this->attempts <= $this->maxRetries &&
$this->getLastHttpCode() >= 500;
}
/**
* Format and sign an OAuth / API request
*
* @param string $url
* @param string $method
* @param array $parameters
* @param bool $json
*
* @return string
* @throws TwitterOAuthException
*/
private function oAuthRequest($url, $method, array $parameters)
{
$request = Request::fromConsumerAndToken($this->consumer, $this->token, $method, $url, $parameters);
private function oAuthRequest(
string $url,
string $method,
array $parameters,
bool $json = false
) {
$request = Request::fromConsumerAndToken(
$this->consumer,
$this->token,
$method,
$url,
$parameters,
$json
);
if (array_key_exists('oauth_callback', $parameters)) {
// Twitter doesn't like oauth_callback as a parameter.
unset($parameters['oauth_callback']);
}
if ($this->bearer === null) {
$request->signRequest($this->signatureMethod, $this->consumer, $this->token);
$request->signRequest(
$this->signatureMethod,
$this->consumer,
$this->token
);
$authorization = $request->toHeader();
if (array_key_exists('oauth_verifier', $parameters)) {
// Twitter doesn't always work with oauth in the body and in the header
@ -354,7 +553,13 @@ class TwitterOAuth extends Config
} else {
$authorization = 'Authorization: Bearer ' . $this->bearer;
}
return $this->request($request->getNormalizedHttpUrl(), $method, $authorization, $parameters);
return $this->request(
$request->getNormalizedHttpUrl(),
$method,
$authorization,
$parameters,
$json
);
}
/**
@ -362,8 +567,9 @@ class TwitterOAuth extends Config
*
* @return array
*/
private function curlOptions()
private function curlOptions(): array
{
$bundlePath = CaBundle::getSystemCaRootBundlePath();
$options = [
// CURLOPT_VERBOSE => true,
CURLOPT_CONNECTTIMEOUT => $this->connectionTimeout,
@ -373,19 +579,17 @@ class TwitterOAuth extends Config
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_TIMEOUT => $this->timeout,
CURLOPT_USERAGENT => $this->userAgent,
$this->curlCaOpt($bundlePath) => $bundlePath,
];
if ($this->useCAFile()) {
$options[CURLOPT_CAINFO] = __DIR__ . DIRECTORY_SEPARATOR . 'cacert.pem';
}
if($this->gzipEncoding) {
if ($this->gzipEncoding) {
$options[CURLOPT_ENCODING] = 'gzip';
}
if (!empty($this->proxy)) {
$options[CURLOPT_PROXY] = $this->proxy['CURLOPT_PROXY'];
$options[CURLOPT_PROXYUSERPWD] = $this->proxy['CURLOPT_PROXYUSERPWD'];
$options[CURLOPT_PROXYUSERPWD] =
$this->proxy['CURLOPT_PROXYUSERPWD'];
$options[CURLOPT_PROXYPORT] = $this->proxy['CURLOPT_PROXYPORT'];
$options[CURLOPT_PROXYAUTH] = CURLAUTH_BASIC;
$options[CURLOPT_PROXYTYPE] = CURLPROXY_HTTP;
@ -401,22 +605,40 @@ class TwitterOAuth extends Config
* @param string $method
* @param string $authorization
* @param array $postfields
* @param bool $json
*
* @return string
* @throws TwitterOAuthException
*/
private function request($url, $method, $authorization, array $postfields)
{
$options = $this->curlOptions($url, $authorization);
private function request(
string $url,
string $method,
string $authorization,
array $postfields,
bool $json = false
): string {
$options = $this->curlOptions();
$options[CURLOPT_URL] = $url;
$options[CURLOPT_HTTPHEADER] = ['Accept: application/json', $authorization, 'Expect:'];
$options[CURLOPT_HTTPHEADER] = [
'Accept: application/json',
$authorization,
'Expect:',
];
switch ($method) {
case 'GET':
break;
case 'POST':
$options[CURLOPT_POST] = true;
$options[CURLOPT_POSTFIELDS] = Util::buildHttpQuery($postfields);
if ($json) {
$options[CURLOPT_HTTPHEADER][] =
'Content-type: application/json';
$options[CURLOPT_POSTFIELDS] = json_encode($postfields);
} else {
$options[CURLOPT_POSTFIELDS] = Util::buildHttpQuery(
$postfields
);
}
break;
case 'DELETE':
$options[CURLOPT_CUSTOMREQUEST] = 'DELETE';
@ -426,21 +648,28 @@ class TwitterOAuth extends Config
break;
}
if (in_array($method, ['GET', 'PUT', 'DELETE']) && !empty($postfields)) {
if (
in_array($method, ['GET', 'PUT', 'DELETE']) &&
!empty($postfields)
) {
$options[CURLOPT_URL] .= '?' . Util::buildHttpQuery($postfields);
}
$curlHandle = curl_init();
curl_setopt_array($curlHandle, $options);
$response = curl_exec($curlHandle);
// Throw exceptions on cURL errors.
if (curl_errno($curlHandle) > 0) {
throw new TwitterOAuthException(curl_error($curlHandle), curl_errno($curlHandle));
$error = curl_error($curlHandle);
$errorNo = curl_errno($curlHandle);
curl_close($curlHandle);
throw new TwitterOAuthException($error, $errorNo);
}
$this->response->setHttpCode(curl_getinfo($curlHandle, CURLINFO_HTTP_CODE));
$this->response->setHttpCode(
curl_getinfo($curlHandle, CURLINFO_HTTP_CODE)
);
$parts = explode("\r\n\r\n", $response);
$responseBody = array_pop($parts);
$responseHeader = array_pop($parts);
@ -458,12 +687,12 @@ class TwitterOAuth extends Config
*
* @return array
*/
private function parseHeaders($header)
private function parseHeaders(string $header): array
{
$headers = [];
foreach (explode("\r\n", $header) as $line) {
if (strpos($line, ':') !== false) {
list ($key, $value) = explode(': ', $line);
[$key, $value] = explode(': ', $line);
$key = str_replace('-', '_', strtolower($key));
$headers[$key] = trim($value);
}
@ -478,7 +707,7 @@ class TwitterOAuth extends Config
*
* @return string
*/
private function encodeAppAuthorization(Consumer $consumer)
private function encodeAppAuthorization(Consumer $consumer): string
{
$key = rawurlencode($consumer->key);
$secret = rawurlencode($consumer->secret);
@ -486,23 +715,13 @@ class TwitterOAuth extends Config
}
/**
* Is the code running from a Phar module.
* Get Curl CA option based on whether the given path is a directory or file.
*
* @return boolean
* @param string $path
* @return int
*/
private function pharRunning()
private function curlCaOpt(string $path): int
{
return class_exists('Phar') && \Phar::running(false) !== '';
}
/**
* Use included CA file instead of OS provided list.
*
* @return boolean
*/
private function useCAFile()
{
/* Use CACert file when not in a PHAR file. */
return !$this->pharRunning();
return is_dir($path) ? CURLOPT_CAPATH : CURLOPT_CAINFO;
}
}

View file

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace Abraham\TwitterOAuth;
/**

View file

@ -1,24 +1,31 @@
<?php
/**
* The MIT License
* Copyright (c) 2007 Andy Smith
*/
declare(strict_types=1);
namespace Abraham\TwitterOAuth;
class Util
{
/**
* @param $input
* @param mixed $input
*
* @return array|mixed|string
* @return mixed
*/
public static function urlencodeRfc3986($input)
{
$output = '';
if (is_array($input)) {
$output = array_map([__NAMESPACE__ . '\Util', 'urlencodeRfc3986'], $input);
$output = array_map(
[__NAMESPACE__ . '\Util', 'urlencodeRfc3986'],
$input
);
} elseif (is_scalar($input)) {
$output = rawurlencode($input);
$output = rawurlencode((string) $input);
}
return $output;
}
@ -28,7 +35,7 @@ class Util
*
* @return string
*/
public static function urldecodeRfc3986($string)
public static function urldecodeRfc3986($string): string
{
return urldecode($string);
}
@ -42,7 +49,7 @@ class Util
*
* @return array
*/
public static function parseParameters($input)
public static function parseParameters($input): array
{
if (!is_string($input)) {
return [];
@ -79,7 +86,7 @@ class Util
*
* @return string
*/
public static function buildHttpQuery(array $params)
public static function buildHttpQuery(array $params): string
{
if (empty($params)) {
return '';

View file

@ -15,9 +15,12 @@ class JsonDecoder
*
* @return array|object
*/
public static function decode($string, $asArray)
public static function decode(string $string, bool $asArray)
{
if (version_compare(PHP_VERSION, '5.4.0', '>=') && !(defined('JSON_C_VERSION') && PHP_INT_SIZE > 4)) {
if (
version_compare(PHP_VERSION, '5.4.0', '>=') &&
!(defined('JSON_C_VERSION') && PHP_INT_SIZE > 4)
) {
return json_decode($string, $asArray, 512, JSON_BIGINT_AS_STRING);
}

File diff suppressed because it is too large Load diff