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