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