782c6b4bbfd2e211c810a9aaa44041f963538ddf
[friendica-addons.git/.git] / tumblr / library / tumblroauth.php
1 <?php
2
3 /*
4  * Abraham Williams (abraham@abrah.am) http://abrah.am
5  *
6  * The first PHP Library to support OAuth for Tumblr's REST API.  (Originally for Twitter, modified for Tumblr by Lucas)
7  */
8
9 /**
10  * Tumblr OAuth class
11  */
12 class TumblrOAuth
13 {
14         /* Set up the API root URL. */
15         public $host = "https://api.tumblr.com/v2/";
16         /* Set timeout default. */
17         public $timeout = 30;
18         /* Set connect timeout. */
19         public $connecttimeout = 30;
20         /* Verify SSL Cert. */
21         public $ssl_verifypeer = FALSE;
22         /* Response format. */
23         public $format = 'json';
24         /* Decode returned json data. */
25         public $decode_json = TRUE;
26         /* Set the useragent. */
27         public $useragent = 'TumblrOAuth v0.2.0-beta2';
28
29         /* Contains the last HTTP status code returned. */
30         public $http_code;
31         /* Contains the last API call. */
32         public $url;
33         /**
34          * Contains the last HTTP headers returned.
35          * @var array
36          */
37         public $http_header;
38         /**
39          * Contains the last HTTP request info
40          * @var string
41          */
42         public $http_info;
43
44         /** @var OAuthToken */
45         private $token;
46         /** @var OAuthConsumer */
47         private $consumer;
48         /** @var OAuthSignatureMethod_HMAC_SHA1 */
49         private $sha1_method;
50
51         /**
52          * Set API URLS
53          */
54         function accessTokenURL()
55         {
56                 return 'https://www.tumblr.com/oauth/access_token';
57         }
58
59         function authenticateURL()
60         {
61                 return 'https://www.tumblr.com/oauth/authorize';
62         }
63
64         function authorizeURL()
65         {
66                 return 'https://www.tumblr.com/oauth/authorize';
67         }
68
69         function requestTokenURL()
70         {
71                 return 'https://www.tumblr.com/oauth/request_token';
72         }
73
74         function __construct($consumer_key, $consumer_secret, $oauth_token = null, $oauth_token_secret = null)
75         {
76                 $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
77                 $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
78                 if (!empty($oauth_token) && !empty($oauth_token_secret)) {
79                         $this->token = new OAuthToken($oauth_token, $oauth_token_secret);
80                 } else {
81                         $this->token = null;
82                 }
83         }
84
85         /**
86          * Get a request_token from Tumblr
87          *
88          * @param callback $oauth_callback
89          * @return array
90          */
91         function getRequestToken($oauth_callback = null)
92         {
93                 $parameters = [];
94                 if (!empty($oauth_callback)) {
95                         $parameters['oauth_callback'] = $oauth_callback;
96                 }
97
98                 $request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters);
99                 $token = OAuthUtil::parse_parameters($request);
100                 $this->token = new OAuthToken($token['oauth_token'], $token['oauth_token_secret']);
101                 return $token;
102         }
103
104         /**
105          * Get the authorize URL
106          *
107          * @param array $token
108          * @param bool $sign_in_with_tumblr
109          * @return string
110          */
111         function getAuthorizeURL($token, $sign_in_with_tumblr = TRUE)
112         {
113                 if (is_array($token)) {
114                         $token = $token['oauth_token'];
115                 }
116
117                 if (empty($sign_in_with_tumblr)) {
118                         return $this->authorizeURL() . "?oauth_token={$token}";
119                 } else {
120                         return $this->authenticateURL() . "?oauth_token={$token}";
121                 }
122         }
123
124         /**
125          * Exchange request token and secret for an access token and
126          * secret, to sign API calls.
127          *
128          * @param bool $oauth_verifier
129          * @return array ("oauth_token" => "the-access-token",
130          *                "oauth_token_secret" => "the-access-secret",
131          *                "user_id" => "9436992",
132          *                "screen_name" => "abraham")
133          */
134         function getAccessToken($oauth_verifier = FALSE)
135         {
136                 $parameters = [];
137                 if (!empty($oauth_verifier)) {
138                         $parameters['oauth_verifier'] = $oauth_verifier;
139                 }
140
141                 $request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters);
142                 $token = OAuthUtil::parse_parameters($request);
143                 $this->token = new OAuthToken($token['oauth_token'], $token['oauth_token_secret']);
144
145                 return $token;
146         }
147
148         /**
149          * One time exchange of username and password for access token and secret.
150          *
151          * @param string $username
152          * @param string $password
153          * @return array ("oauth_token" => "the-access-token",
154          *                "oauth_token_secret" => "the-access-secret",
155          *                "user_id" => "9436992",
156          *                "screen_name" => "abraham",
157          *                "x_auth_expires" => "0")
158          */
159         function getXAuthToken($username, $password)
160         {
161                 $parameters = [];
162                 $parameters['x_auth_username'] = $username;
163                 $parameters['x_auth_password'] = $password;
164                 $parameters['x_auth_mode'] = 'client_auth';
165                 $request = $this->oAuthRequest($this->accessTokenURL(), 'POST', $parameters);
166                 $token = OAuthUtil::parse_parameters($request);
167                 $this->token = new OAuthToken($token['oauth_token'], $token['oauth_token_secret']);
168
169                 return $token;
170         }
171
172         /**
173          * GET wrapper for oAuthRequest.
174          *
175          * @param string $url
176          * @param array $parameters
177          * @return mixed|string
178          */
179         function get($url, $parameters = [])
180         {
181                 $response = $this->oAuthRequest($url, 'GET', $parameters);
182                 if ($this->format === 'json' && $this->decode_json) {
183                         return json_decode($response);
184                 }
185
186                 return $response;
187         }
188
189         /**
190          * POST wrapper for oAuthRequest.
191          *
192          * @param string $url
193          * @param array $parameters
194          * @return mixed|string
195          */
196         function post($url, $parameters = [])
197         {
198                 $response = $this->oAuthRequest($url, 'POST', $parameters);
199                 if ($this->format === 'json' && $this->decode_json) {
200                         return json_decode($response);
201                 }
202
203                 return $response;
204         }
205
206         /**
207          * DELETE wrapper for oAuthReqeust.
208          *
209          * @param string $url
210          * @param array $parameters
211          * @return mixed|string
212          */
213         function delete($url, $parameters = [])
214         {
215                 $response = $this->oAuthRequest($url, 'DELETE', $parameters);
216                 if ($this->format === 'json' && $this->decode_json) {
217                         return json_decode($response);
218                 }
219
220                 return $response;
221         }
222
223         /**
224          * Format and sign an OAuth / API request
225          *
226          * @param string $url
227          * @param string $method
228          * @param array $parameters
229          * @return mixed|string
230          */
231         function oAuthRequest($url, $method, $parameters)
232         {
233                 if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) {
234                         $url = "{$this->host}{$url}";
235                 }
236
237                 $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
238                 $request->sign_request($this->sha1_method, $this->consumer, $this->token);
239                 switch ($method) {
240                         case 'GET':
241                                 return $this->http($request->to_url(), 'GET');
242                         default:
243                                 return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
244                 }
245         }
246
247         /**
248          * Make an HTTP request
249          *
250          * @param string $url
251          * @param string $method
252          * @param mixed  $postfields
253          * @return string API results
254          */
255         function http($url, $method, $postfields = null)
256         {
257                 $this->http_info = [];
258                 $ci = curl_init();
259                 /* Curl settings */
260                 curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
261                 curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
262                 curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
263                 curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
264                 curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
265                 curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
266                 curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
267                 curl_setopt($ci, CURLOPT_HEADER, FALSE);
268
269                 switch ($method) {
270                         case 'POST':
271                                 curl_setopt($ci, CURLOPT_POST, TRUE);
272                                 if (!empty($postfields)) {
273                                         curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
274                                 }
275                                 break;
276                         case 'DELETE':
277                                 curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
278                                 if (!empty($postfields)) {
279                                         $url = "{$url}?{$postfields}";
280                                 }
281                 }
282
283                 curl_setopt($ci, CURLOPT_URL, $url);
284                 $response = curl_exec($ci);
285                 $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
286                 $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
287                 $this->url = $url;
288                 curl_close($ci);
289
290                 return $response;
291         }
292
293         /**
294          * Get the header info to store.
295          *
296          * @param resource $ch
297          * @param string $header
298          * @return int
299          */
300         function getHeader($ch, $header)
301         {
302                 $i = strpos($header, ':');
303                 if (!empty($i)) {
304                         $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
305                         $value = trim(substr($header, $i + 2));
306                         $this->http_header[$key] = $value;
307                 }
308
309                 return strlen($header);
310         }
311 }