4 * Abraham Williams (abraham@abrah.am) http://abrah.am
6 * The first PHP Library to support OAuth for Tumblr's REST API. (Originally for Twitter, modified for Tumblr by Lucas)
9 use Friendica\Security\OAuth1\OAuthConsumer;
10 use Friendica\Security\OAuth1\OAuthRequest;
11 use Friendica\Security\OAuth1\Signature\OAuthSignatureMethod_HMAC_SHA1;
12 use Friendica\Security\OAuth1\OAuthToken;
13 use Friendica\Security\OAuth1\OAuthUtil;
20 /* Set up the API root URL. */
21 public $host = "https://api.tumblr.com/v2/";
22 /* Set timeout default. */
24 /* Set connect timeout. */
25 public $connecttimeout = 30;
26 /* Verify SSL Cert. */
27 public $ssl_verifypeer = FALSE;
28 /* Response format. */
29 public $format = 'json';
30 /* Decode returned json data. */
31 public $decode_json = TRUE;
32 /* Set the useragent. */
33 public $useragent = 'TumblrOAuth v0.2.0-beta2';
35 /* Contains the last HTTP status code returned. */
37 /* Contains the last API call. */
40 * Contains the last HTTP headers returned.
45 * Contains the last HTTP request info
50 /** @var OAuthToken */
52 /** @var OAuthConsumer */
54 /** @var \Friendica\Security\OAuth1\Signature\OAuthSignatureMethod_HMAC_SHA1 */
60 function accessTokenURL()
62 return 'https://www.tumblr.com/oauth/access_token';
65 function authenticateURL()
67 return 'https://www.tumblr.com/oauth/authorize';
70 function authorizeURL()
72 return 'https://www.tumblr.com/oauth/authorize';
75 function requestTokenURL()
77 return 'https://www.tumblr.com/oauth/request_token';
80 function __construct($consumer_key, $consumer_secret, $oauth_token = null, $oauth_token_secret = null)
82 $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
83 $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
84 if (!empty($oauth_token) && !empty($oauth_token_secret)) {
85 $this->token = new OAuthToken($oauth_token, $oauth_token_secret);
92 * Get a request_token from Tumblr
94 * @param callback $oauth_callback
97 function getRequestToken($oauth_callback = null)
100 if (!empty($oauth_callback)) {
101 $parameters['oauth_callback'] = $oauth_callback;
104 $request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters);
105 $token = OAuthUtil::parse_parameters($request);
106 $this->token = new OAuthToken($token['oauth_token'], $token['oauth_token_secret']);
111 * Get the authorize URL
113 * @param array $token
114 * @param bool $sign_in_with_tumblr
117 function getAuthorizeURL($token, $sign_in_with_tumblr = TRUE)
119 if (is_array($token)) {
120 $token = $token['oauth_token'];
123 if (empty($sign_in_with_tumblr)) {
124 return $this->authorizeURL() . "?oauth_token={$token}";
126 return $this->authenticateURL() . "?oauth_token={$token}";
131 * Exchange request token and secret for an access token and
132 * secret, to sign API calls.
134 * @param bool $oauth_verifier
135 * @return array ("oauth_token" => "the-access-token",
136 * "oauth_token_secret" => "the-access-secret",
137 * "user_id" => "9436992",
138 * "screen_name" => "abraham")
140 function getAccessToken($oauth_verifier = FALSE)
143 if (!empty($oauth_verifier)) {
144 $parameters['oauth_verifier'] = $oauth_verifier;
147 $request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters);
148 $token = OAuthUtil::parse_parameters($request);
149 $this->token = new OAuthToken($token['oauth_token'], $token['oauth_token_secret']);
155 * One time exchange of username and password for access token and secret.
157 * @param string $username
158 * @param string $password
159 * @return array ("oauth_token" => "the-access-token",
160 * "oauth_token_secret" => "the-access-secret",
161 * "user_id" => "9436992",
162 * "screen_name" => "abraham",
163 * "x_auth_expires" => "0")
165 function getXAuthToken($username, $password)
168 $parameters['x_auth_username'] = $username;
169 $parameters['x_auth_password'] = $password;
170 $parameters['x_auth_mode'] = 'client_auth';
171 $request = $this->oAuthRequest($this->accessTokenURL(), 'POST', $parameters);
172 $token = OAuthUtil::parse_parameters($request);
173 $this->token = new OAuthToken($token['oauth_token'], $token['oauth_token_secret']);
179 * GET wrapper for oAuthRequest.
182 * @param array $parameters
183 * @return mixed|string
185 function get($url, $parameters = [])
187 $response = $this->oAuthRequest($url, 'GET', $parameters);
188 if ($this->format === 'json' && $this->decode_json) {
189 return json_decode($response);
196 * POST wrapper for oAuthRequest.
199 * @param array $parameters
200 * @return mixed|string
202 function post($url, $parameters = [])
204 $response = $this->oAuthRequest($url, 'POST', $parameters);
205 if ($this->format === 'json' && $this->decode_json) {
206 return json_decode($response);
213 * DELETE wrapper for oAuthReqeust.
216 * @param array $parameters
217 * @return mixed|string
219 function delete($url, $parameters = [])
221 $response = $this->oAuthRequest($url, 'DELETE', $parameters);
222 if ($this->format === 'json' && $this->decode_json) {
223 return json_decode($response);
230 * Format and sign an OAuth / API request
233 * @param string $method
234 * @param array $parameters
235 * @return mixed|string
237 function oAuthRequest($url, $method, $parameters)
239 if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) {
240 $url = "{$this->host}{$url}";
243 $request = OAuthRequest::from_consumer_and_token($this->consumer, $method, $url, $parameters, $this->token);
244 $request->sign_request($this->sha1_method, $this->consumer, $this->token);
247 return $this->http($request->to_url(), 'GET');
249 return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
254 * Make an HTTP request
257 * @param string $method
258 * @param mixed $postfields
259 * @return string API results
261 function http($url, $method, $postfields = null)
263 $this->http_info = [];
266 curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
267 curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
268 curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
269 curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
270 curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
271 curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
272 curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
273 curl_setopt($ci, CURLOPT_HEADER, FALSE);
277 curl_setopt($ci, CURLOPT_POST, TRUE);
278 if (!empty($postfields)) {
279 curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
283 curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
284 if (!empty($postfields)) {
285 $url = "{$url}?{$postfields}";
289 curl_setopt($ci, CURLOPT_URL, $url);
290 $response = curl_exec($ci);
291 $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
292 $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
300 * Get the header info to store.
302 * @param resource $ch
303 * @param string $header
306 function getHeader($ch, $header)
308 $i = strpos($header, ':');
310 $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
311 $value = trim(substr($header, $i + 2));
312 $this->http_header[$key] = $value;
315 return strlen($header);