]> git.mxchange.org Git - friendica-addons.git/blob - statusnet/library/twitteroauth.php
[statusnet] Remove explicit duplicate OAuth1 library
[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         /* Contains the last HTTP status code returned. */
17         public $http_code;
18         /* Contains the last API call. */
19         public $url;
20         /* Set up the API root URL. */
21         public $host = "https://api.twitter.com/1.1/";
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         /* Respons format. */
29         public $format = 'json';
30         /* Decode returned json data. */
31         public $decode_json = TRUE;
32         /* Contains the last HTTP headers returned. */
33         public $http_info;
34         /* Set the useragnet. */
35         public $useragent = 'TwitterOAuth v0.2.0-beta2';
36
37         /* Immediately retry the API call if the response was not successful. */
38         //public $retry = TRUE;
39
40         /**
41          * Set API URLS
42          */
43         function accessTokenURL()
44         {
45                 return 'https://api.twitter.com/oauth/access_token';
46         }
47
48         function authenticateURL()
49         {
50                 return 'https://twitter.com/oauth/authenticate';
51         }
52
53         function authorizeURL()
54         {
55                 return 'https://twitter.com/oauth/authorize';
56         }
57
58         function requestTokenURL()
59         {
60                 return 'https://api.twitter.com/oauth/request_token';
61         }
62
63         /**
64          * Debug helpers
65          */
66         function lastStatusCode()
67         {
68                 return $this->http_status;
69         }
70
71         function lastAPICall()
72         {
73                 return $this->last_api_call;
74         }
75
76         /**
77          * construct TwitterOAuth object
78          */
79         function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL)
80         {
81                 $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
82                 $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
83                 if (!empty($oauth_token) && !empty($oauth_token_secret)) {
84                         $this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
85                 } else {
86                         $this->token = NULL;
87                 }
88         }
89
90         /**
91          * Get a request_token from Twitter
92          *
93          * @returns a key/value array containing oauth_token and oauth_token_secret
94          */
95         function getRequestToken($oauth_callback = NULL)
96         {
97                 $parameters = array();
98                 if (!empty($oauth_callback)) {
99                         $parameters['oauth_callback'] = $oauth_callback;
100                 }
101                 $request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters);
102                 $token = OAuthUtil::parse_parameters($request);
103                 $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
104                 return $token;
105         }
106
107         /**
108          * Get the authorize URL
109          *
110          * @returns a string
111          */
112         function getAuthorizeURL($token, $sign_in_with_twitter = TRUE)
113         {
114                 if (is_array($token)) {
115                         $token = $token['oauth_token'];
116                 }
117                 if (empty($sign_in_with_twitter)) {
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          * @returns array("oauth_token" => "the-access-token",
129          *                "oauth_token_secret" => "the-access-secret",
130          *                "user_id" => "9436992",
131          *                "screen_name" => "abraham")
132          */
133         function getAccessToken($oauth_verifier = FALSE)
134         {
135                 $parameters = array();
136                 if (!empty($oauth_verifier)) {
137                         $parameters['oauth_verifier'] = $oauth_verifier;
138                 }
139                 $request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters);
140                 $token = OAuthUtil::parse_parameters($request);
141                 $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
142                 return $token;
143         }
144
145         /**
146          * One time exchange of username and password for access token and secret.
147          *
148          * @returns array("oauth_token" => "the-access-token",
149          *                "oauth_token_secret" => "the-access-secret",
150          *                "user_id" => "9436992",
151          *                "screen_name" => "abraham",
152          *                "x_auth_expires" => "0")
153          */
154         function getXAuthToken($username, $password)
155         {
156                 $parameters = array();
157                 $parameters['x_auth_username'] = $username;
158                 $parameters['x_auth_password'] = $password;
159                 $parameters['x_auth_mode'] = 'client_auth';
160                 $request = $this->oAuthRequest($this->accessTokenURL(), 'POST', $parameters);
161                 $token = OAuthUtil::parse_parameters($request);
162                 $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
163                 return $token;
164         }
165
166         /**
167          * GET wrapper for oAuthRequest.
168          */
169         function get($url, $parameters = array())
170         {
171                 $response = $this->oAuthRequest($url, 'GET', $parameters);
172                 if ($this->format === 'json' && $this->decode_json) {
173                         return json_decode($response);
174                 }
175                 return $response;
176         }
177
178         /**
179          * POST wrapper for oAuthRequest.
180          */
181         function post($url, $parameters = array())
182         {
183                 $response = $this->oAuthRequest($url, 'POST', $parameters);
184                 if ($this->format === 'json' && $this->decode_json) {
185                         return json_decode($response);
186                 }
187                 return $response;
188         }
189
190         /**
191          * DELETE wrapper for oAuthReqeust.
192          */
193         function delete($url, $parameters = array())
194         {
195                 $response = $this->oAuthRequest($url, 'DELETE', $parameters);
196                 if ($this->format === 'json' && $this->decode_json) {
197                         return json_decode($response);
198                 }
199                 return $response;
200         }
201
202         /**
203          * Format and sign an OAuth / API request
204          */
205         function oAuthRequest($url, $method, $parameters)
206         {
207                 if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) {
208                         $url = "{$this->host}{$url}.{$this->format}";
209                 }
210                 $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
211                 $request->sign_request($this->sha1_method, $this->consumer, $this->token);
212                 switch ($method) {
213                         case 'GET':
214                                 return $this->http($request->to_url(), 'GET');
215                         case 'UPLOAD':
216                                 return $this->http($request->get_normalized_http_url(), 'POST', $request->to_postdata(true));
217                         default:
218                                 return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
219                 }
220         }
221
222         /**
223          * Make an HTTP request
224          *
225          * @return API results
226          */
227         function http($url, $method, $postfields = NULL)
228         {
229                 $this->http_info = array();
230                 $ci = curl_init();
231                 /* Curl settings */
232                 curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
233                 curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
234                 curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
235                 curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
236                 curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
237                 curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
238                 curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
239                 curl_setopt($ci, CURLOPT_HEADER, FALSE);
240
241                 switch ($method) {
242                         case 'POST':
243                                 curl_setopt($ci, CURLOPT_POST, TRUE);
244                                 if (!empty($postfields)) {
245                                         curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
246                                 }
247                                 break;
248                         case 'DELETE':
249                                 curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
250                                 if (!empty($postfields)) {
251                                         $url = "{$url}?{$postfields}";
252                                 }
253                 }
254
255                 curl_setopt($ci, CURLOPT_URL, $url);
256                 $response = curl_exec($ci);
257                 $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
258                 $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
259                 $this->url = $url;
260                 curl_close($ci);
261                 return $response;
262         }
263
264         /**
265          * Get the header info to store.
266          */
267         function getHeader($ch, $header)
268         {
269                 $i = strpos($header, ':');
270                 if (!empty($i)) {
271                         $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
272                         $value = trim(substr($header, $i + 2));
273                         $this->http_header[$key] = $value;
274                 }
275                 return strlen($header);
276         }
277 }