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