]> git.mxchange.org Git - friendica-addons.git/blob - tumblr/library/tumblroauth.php
[tumblr] Fix formatting in library/tumblroauth
[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 /**
10  * Tumblr OAuth class
11  */
12 class TumblrOAuth
13 {
14         /* Contains the last HTTP status code returned. */
15         public $http_code;
16         /* Contains the last API call. */
17         public $url;
18         /* Set up the API root URL. */
19         public $host = "https://api.tumblr.com/v2/";
20         /* Set timeout default. */
21         public $timeout = 30;
22         /* Set connect timeout. */
23         public $connecttimeout = 30;
24         /* Verify SSL Cert. */
25         public $ssl_verifypeer = FALSE;
26         /* Respons format. */
27         public $format = 'json';
28         /* Decode returned json data. */
29         public $decode_json = TRUE;
30         /* Contains the last HTTP headers returned. */
31         public $http_info;
32         /* Set the useragnet. */
33         public $useragent = 'TumblrOAuth v0.2.0-beta2';
34         /* Immediately retry the API call if the response was not successful. */
35         //public $retry = TRUE;
36
37
38         /**
39          * Set API URLS
40          */
41         function accessTokenURL()
42         {
43                 return 'https://www.tumblr.com/oauth/access_token';
44         }
45
46         function authenticateURL()
47         {
48                 return 'https://www.tumblr.com/oauth/authorize';
49         }
50
51         function authorizeURL()
52         {
53                 return 'https://www.tumblr.com/oauth/authorize';
54         }
55
56         function requestTokenURL()
57         {
58                 return 'https://www.tumblr.com/oauth/request_token';
59         }
60
61         /**
62          * Debug helpers
63          */
64         function lastStatusCode()
65         {
66                 return $this->http_status;
67         }
68
69         function lastAPICall()
70         {
71                 return $this->last_api_call;
72         }
73
74         /**
75          * construct TumblrOAuth object
76          */
77         function __construct($consumer_key, $consumer_secret, $oauth_token = null, $oauth_token_secret = null)
78         {
79                 $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
80                 $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
81                 if (!empty($oauth_token) && !empty($oauth_token_secret)) {
82                         $this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
83                 } else {
84                         $this->token = null;
85                 }
86         }
87
88
89         /**
90          * Get a request_token from Tumblr
91          *
92          * @returns a key/value array containing oauth_token and oauth_token_secret
93          */
94         function getRequestToken($oauth_callback = null)
95         {
96                 $parameters = array();
97                 if (!empty($oauth_callback)) {
98                         $parameters['oauth_callback'] = $oauth_callback;
99                 }
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_tumblr = TRUE)
113         {
114                 if (is_array($token)) {
115                         $token = $token['oauth_token'];
116                 }
117
118                 if (empty($sign_in_with_tumblr)) {
119                         return $this->authorizeURL() . "?oauth_token={$token}";
120                 } else {
121                         return $this->authenticateURL() . "?oauth_token={$token}";
122                 }
123         }
124
125         /**
126          * Exchange request token and secret for an access token and
127          * secret, to sign API calls.
128          *
129          * @returns 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 = array();
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 OAuthConsumer($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          * @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
167                 return $token;
168         }
169
170         /**
171          * GET wrapper for oAuthRequest.
172          */
173         function get($url, $parameters = array())
174         {
175                 $response = $this->oAuthRequest($url, 'GET', $parameters);
176                 if ($this->format === 'json' && $this->decode_json) {
177                         return json_decode($response);
178                 }
179
180                 return $response;
181         }
182
183         /**
184          * POST wrapper for oAuthRequest.
185          */
186         function post($url, $parameters = array())
187         {
188                 $response = $this->oAuthRequest($url, 'POST', $parameters);
189                 if ($this->format === 'json' && $this->decode_json) {
190                         return json_decode($response);
191                 }
192
193                 return $response;
194         }
195
196         /**
197          * DELETE wrapper for oAuthReqeust.
198          */
199         function delete($url, $parameters = array())
200         {
201                 $response = $this->oAuthRequest($url, 'DELETE', $parameters);
202                 if ($this->format === 'json' && $this->decode_json) {
203                         return json_decode($response);
204                 }
205
206                 return $response;
207         }
208
209         /**
210          * Format and sign an OAuth / API request
211          */
212         function oAuthRequest($url, $method, $parameters)
213         {
214                 if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) {
215                         $url = "{$this->host}{$url}";
216                 }
217
218                 $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
219                 $request->sign_request($this->sha1_method, $this->consumer, $this->token);
220                 switch ($method) {
221                         case 'GET':
222                                 return $this->http($request->to_url(), 'GET');
223                         default:
224                                 return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
225                 }
226         }
227
228         /**
229          * Make an HTTP request
230          *
231          * @return API results
232          */
233         function http($url, $method, $postfields = null)
234         {
235                 $this->http_info = array();
236                 $ci = curl_init();
237                 /* Curl settings */
238                 curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
239                 curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
240                 curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
241                 curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
242                 curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
243                 curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
244                 curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
245                 curl_setopt($ci, CURLOPT_HEADER, FALSE);
246
247                 switch ($method) {
248                         case 'POST':
249                                 curl_setopt($ci, CURLOPT_POST, TRUE);
250                                 if (!empty($postfields)) {
251                                         curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
252                                 }
253                                 break;
254                         case 'DELETE':
255                                 curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
256                                 if (!empty($postfields)) {
257                                         $url = "{$url}?{$postfields}";
258                                 }
259                 }
260
261                 curl_setopt($ci, CURLOPT_URL, $url);
262                 $response = curl_exec($ci);
263                 $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
264                 $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
265                 $this->url = $url;
266                 curl_close($ci);
267
268                 return $response;
269         }
270
271         /**
272          * Get the header info to store.
273          */
274         function getHeader($ch, $header)
275         {
276                 $i = strpos($header, ':');
277                 if (!empty($i)) {
278                         $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
279                         $value = trim(substr($header, $i + 2));
280                         $this->http_header[$key] = $value;
281                 }
282
283                 return strlen($header);
284         }
285 }