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