]> git.mxchange.org Git - friendica-addons.git/blob - tumblr/library/tumblroauth.php
[markdown] Escape mentions which username can contain Markdown-like syntax
[friendica-addons.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 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;
14
15 /**
16  * Tumblr OAuth class
17  */
18 class TumblrOAuth
19 {
20         /* Set up the API root URL. */
21         public $host = "https://api.tumblr.com/v2/";
22         /* Set timeout default. */
23         public $timeout = 30;
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';
34
35         /* Contains the last HTTP status code returned. */
36         public $http_code;
37         /* Contains the last API call. */
38         public $url;
39         /**
40          * Contains the last HTTP headers returned.
41          * @var array
42          */
43         public $http_header;
44         /**
45          * Contains the last HTTP request info
46          * @var string
47          */
48         public $http_info;
49
50         /** @var OAuthToken */
51         private $token;
52         /** @var OAuthConsumer */
53         private $consumer;
54         /** @var \Friendica\Security\OAuth1\Signature\OAuthSignatureMethod_HMAC_SHA1 */
55         private $sha1_method;
56
57         /**
58          * Set API URLS
59          */
60         function accessTokenURL()
61         {
62                 return 'https://www.tumblr.com/oauth/access_token';
63         }
64
65         function authenticateURL()
66         {
67                 return 'https://www.tumblr.com/oauth/authorize';
68         }
69
70         function authorizeURL()
71         {
72                 return 'https://www.tumblr.com/oauth/authorize';
73         }
74
75         function requestTokenURL()
76         {
77                 return 'https://www.tumblr.com/oauth/request_token';
78         }
79
80         function __construct($consumer_key, $consumer_secret, $oauth_token = null, $oauth_token_secret = null)
81         {
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);
86                 } else {
87                         $this->token = null;
88                 }
89         }
90
91         /**
92          * Get a request_token from Tumblr
93          *
94          * @param callback $oauth_callback
95          * @return array
96          */
97         function getRequestToken($oauth_callback = null)
98         {
99                 $parameters = [];
100                 if (!empty($oauth_callback)) {
101                         $parameters['oauth_callback'] = $oauth_callback;
102                 }
103
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']);
107                 return $token;
108         }
109
110         /**
111          * Get the authorize URL
112          *
113          * @param array $token
114          * @param bool $sign_in_with_tumblr
115          * @return string
116          */
117         function getAuthorizeURL($token, $sign_in_with_tumblr = TRUE)
118         {
119                 if (is_array($token)) {
120                         $token = $token['oauth_token'];
121                 }
122
123                 if (empty($sign_in_with_tumblr)) {
124                         return $this->authorizeURL() . "?oauth_token={$token}";
125                 } else {
126                         return $this->authenticateURL() . "?oauth_token={$token}";
127                 }
128         }
129
130         /**
131          * Exchange request token and secret for an access token and
132          * secret, to sign API calls.
133          *
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")
139          */
140         function getAccessToken($oauth_verifier = FALSE)
141         {
142                 $parameters = [];
143                 if (!empty($oauth_verifier)) {
144                         $parameters['oauth_verifier'] = $oauth_verifier;
145                 }
146
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']);
150
151                 return $token;
152         }
153
154         /**
155          * One time exchange of username and password for access token and secret.
156          *
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")
164          */
165         function getXAuthToken($username, $password)
166         {
167                 $parameters = [];
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']);
174
175                 return $token;
176         }
177
178         /**
179          * GET wrapper for oAuthRequest.
180          *
181          * @param string $url
182          * @param array $parameters
183          * @return mixed|string
184          */
185         function get($url, $parameters = [])
186         {
187                 $response = $this->oAuthRequest($url, 'GET', $parameters);
188                 if ($this->format === 'json' && $this->decode_json) {
189                         return json_decode($response);
190                 }
191
192                 return $response;
193         }
194
195         /**
196          * POST wrapper for oAuthRequest.
197          *
198          * @param string $url
199          * @param array $parameters
200          * @return mixed|string
201          */
202         function post($url, $parameters = [])
203         {
204                 $response = $this->oAuthRequest($url, 'POST', $parameters);
205                 if ($this->format === 'json' && $this->decode_json) {
206                         return json_decode($response);
207                 }
208
209                 return $response;
210         }
211
212         /**
213          * DELETE wrapper for oAuthReqeust.
214          *
215          * @param string $url
216          * @param array $parameters
217          * @return mixed|string
218          */
219         function delete($url, $parameters = [])
220         {
221                 $response = $this->oAuthRequest($url, 'DELETE', $parameters);
222                 if ($this->format === 'json' && $this->decode_json) {
223                         return json_decode($response);
224                 }
225
226                 return $response;
227         }
228
229         /**
230          * Format and sign an OAuth / API request
231          *
232          * @param string $url
233          * @param string $method
234          * @param array $parameters
235          * @return mixed|string
236          */
237         function oAuthRequest($url, $method, $parameters)
238         {
239                 if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) {
240                         $url = "{$this->host}{$url}";
241                 }
242
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);
245                 switch ($method) {
246                         case 'GET':
247                                 return $this->http($request->to_url(), 'GET');
248                         default:
249                                 return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
250                 }
251         }
252
253         /**
254          * Make an HTTP request
255          *
256          * @param string $url
257          * @param string $method
258          * @param mixed  $postfields
259          * @return string API results
260          */
261         function http($url, $method, $postfields = null)
262         {
263                 $this->http_info = [];
264                 $ci = curl_init();
265                 /* Curl settings */
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);
274
275                 switch ($method) {
276                         case 'POST':
277                                 curl_setopt($ci, CURLOPT_POST, TRUE);
278                                 if (!empty($postfields)) {
279                                         curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
280                                 }
281                                 break;
282                         case 'DELETE':
283                                 curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
284                                 if (!empty($postfields)) {
285                                         $url = "{$url}?{$postfields}";
286                                 }
287                 }
288
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));
293                 $this->url = $url;
294                 curl_close($ci);
295
296                 return $response;
297         }
298
299         /**
300          * Get the header info to store.
301          *
302          * @param resource $ch
303          * @param string $header
304          * @return int
305          */
306         function getHeader($ch, $header)
307         {
308                 $i = strpos($header, ':');
309                 if (!empty($i)) {
310                         $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
311                         $value = trim(substr($header, $i + 2));
312                         $this->http_header[$key] = $value;
313                 }
314
315                 return strlen($header);
316         }
317 }