TumblrOAuth

Tumblr OAuth class.

Defined (1)

The class is defined in the following location(s).

/inc-cl/apis/trOAuth.php  
  1. class TumblrOAuth { 
  2. /** Contains the last HTTP status code returned. */ 
  3. public $http_code; 
  4. /** Contains the last API call. */ 
  5. public $url; 
  6. /** Set up the API root URL. */ 
  7. public $host = "http://api.tumblr.com/v2/"; 
  8. /** Set timeout default. */ 
  9. public $timeout = 45; 
  10. /** Set connect timeout. */ 
  11. public $connecttimeout = 45;  
  12. /** Verify SSL Cert. */ 
  13. public $ssl_verifypeer = FALSE; 
  14. /** Respons format. */ 
  15. public $format = 'json'; 
  16. /** Decode returned json data. */ 
  17. public $decode_json = TRUE; 
  18. /** Contains the last HTTP headers returned. */ 
  19. public $http_info; 
  20. /** Set the useragnet. */ 
  21. public $useragent = 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; InfoPath.3; .NET4.0E; .NET CLR 1.1.4322)'; 
  22. /** Immediately retry the API call if the response was not successful. */ 
  23. //public $retry = TRUE; 
  24.  
  25. /** 
  26. * Set API URLS 
  27. */ 
  28. function accessTokenURL() { return 'https://www.tumblr.com/oauth/access_token'; } 
  29. function authenticateURL() { return 'https://www.tumblr.com/oauth/authorize'; } 
  30. function authorizeURL() { return 'https://www.tumblr.com/oauth/authorize'; } 
  31. function requestTokenURL() { return 'https://www.tumblr.com/oauth/request_token'; } 
  32.  
  33. /** 
  34. * Debug helpers 
  35. */ 
  36. function lastStatusCode() { return $this->http_status; } 
  37. function lastAPICall() { return $this->last_api_call; } 
  38.  
  39. /** 
  40. * construct TumblrOAuth object 
  41. */ 
  42. function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) { 
  43. $this->sha1_method = new nsx_trOAuthSignatureMethod_HMAC_SHA1(); 
  44. $this->consumer = new nsx_trOAuthConsumer($consumer_key, $consumer_secret); 
  45. if (!empty($oauth_token) && !empty($oauth_token_secret)) { 
  46. $this->token = new nsx_trOAuthConsumer($oauth_token, $oauth_token_secret); 
  47. } else { 
  48. $this->token = NULL; 
  49.  
  50. /** 
  51. * Get a request_token from Tumblr 
  52. * @returns a key/value array containing oauth_token and oauth_token_secret 
  53. */ 
  54. function getRequestToken($oauth_callback = NULL) { 
  55. $parameters = array(); 
  56. if (!empty($oauth_callback)) { 
  57. $parameters['oauth_callback'] = $oauth_callback; 
  58. }  
  59. $request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters);  
  60. $token = nsx_trOAuthUtil::parse_parameters($request); 
  61. $this->token = new nsx_trOAuthConsumer($token['oauth_token'], $token['oauth_token_secret']); 
  62. return $token; 
  63.  
  64. /** 
  65. * Get the authorize URL 
  66. * @returns a string 
  67. */ 
  68. function getAuthorizeURL($token, $sign_in_with_tumblr = TRUE) { 
  69. if (is_array($token)) { 
  70. $token = $token['oauth_token']; 
  71. if (empty($sign_in_with_tumblr)) { 
  72. return $this->authorizeURL() . "?oauth_token={$token}"; 
  73. } else { 
  74. return $this->authenticateURL() . "?oauth_token={$token}"; 
  75.  
  76. /** 
  77. * Exchange request token and secret for an access token and 
  78. * secret, to sign API calls. 
  79. * @returns array("oauth_token" => "the-access-token",  
  80. * "oauth_token_secret" => "the-access-secret",  
  81. * "user_id" => "9436992",  
  82. * "screen_name" => "abraham") 
  83. */ 
  84. function getAccessToken($oauth_verifier = FALSE) { 
  85. $parameters = array(); 
  86. if (!empty($oauth_verifier)) { 
  87. $parameters['oauth_verifier'] = $oauth_verifier; 
  88. $request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters); 
  89. $token = nsx_trOAuthUtil::parse_parameters($request); 
  90. $this->token = new nsx_trOAuthConsumer($token['oauth_token'], $token['oauth_token_secret']); 
  91. return $token; 
  92.  
  93. /** 
  94. * One time exchange of username and password for access token and secret. 
  95. * @returns array("oauth_token" => "the-access-token",  
  96. * "oauth_token_secret" => "the-access-secret",  
  97. * "user_id" => "9436992",  
  98. * "screen_name" => "abraham",  
  99. * "x_auth_expires" => "0") 
  100. */  
  101. function getXAuthToken($username, $password) { 
  102. $parameters = array(); 
  103. $parameters['x_auth_username'] = $username; 
  104. $parameters['x_auth_password'] = $password; 
  105. $parameters['x_auth_mode'] = 'client_auth'; 
  106. $request = $this->oAuthRequest($this->accessTokenURL(), 'POST', $parameters); 
  107. $token = nsx_trOAuthUtil::parse_parameters($request); 
  108. $this->token = new nsx_trOAuthConsumer($token['oauth_token'], $token['oauth_token_secret']); 
  109. return $token; 
  110.  
  111. /** 
  112. * GET wrapper for oAuthRequest. 
  113. */ 
  114. function get($url, $parameters = array()) { 
  115. $response = $this->oAuthRequest($url, 'GET', $parameters); 
  116. if ($this->format === 'json' && $this->decode_json) { 
  117. return json_decode($response); 
  118. return $response; 
  119.  
  120. /** 
  121. * POST wrapper for oAuthRequest. 
  122. */ 
  123. function post($url, $parameters = array()) { 
  124. $response = $this->oAuthRequest($url, 'POST', $parameters); //prr($response); 
  125. if ($this->format === 'json' && $this->decode_json) { 
  126. return json_decode($response); 
  127. return $response; 
  128.  
  129. /** 
  130. * DELETE wrapper for oAuthReqeust. 
  131. */ 
  132. function delete($url, $parameters = array()) { 
  133. $response = $this->oAuthRequest($url, 'DELETE', $parameters); 
  134. if ($this->format === 'json' && $this->decode_json) { 
  135. return json_decode($response); 
  136. return $response; 
  137.  
  138. /** 
  139. * Format and sign an OAuth / API request 
  140. */ 
  141. function oAuthRequest($url, $method, $parameters) {  
  142. if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) { 
  143. $url = "{$this->host}{$url}"; 
  144. $request = nsx_trOAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters); 
  145. $request->sign_request($this->sha1_method, $this->consumer, $this->token);  
  146. switch ($method) { 
  147. case 'GET': 
  148. return $this->http($request->to_url(), 'GET'); 
  149. default: 
  150. return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata()); 
  151.  
  152. /** 
  153. * Make an HTTP request 
  154. * @return API results 
  155. */ 
  156. function http($url, $method, $postfields = NULL) { $ref = ''; 
  157. $this->http_info = array(); 
  158. if ($method=='DELETE') $ci = curl_init(); else $ci = curl_init($url); 
  159.  
  160. $headers = array(); 
  161. $headers[] = 'Connection: keep-alive'; 
  162. $headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.34 Safari/536.11'; 
  163. $headers[] = 'Accept: text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8'; 
  164. $headers[] = 'Accept-Encoding: gzip, deflate, sdch'; 
  165. $headers[] = 'Accept-Language: en-US, en;q=0.8'; 
  166. $headers[] = 'Accept-Charset: ISO-8859-1, utf-8;q=0.7, *;q=0.3'; 
  167.  
  168. $options = array( 
  169. CURLOPT_RETURNTRANSFER => true, // return web page 
  170. CURLOPT_HEADER => false, // don't return headers 
  171. // CURLOPT_FOLLOWLOCATION => true, // follow redirects 
  172. CURLOPT_ENCODING => "", // handle all encodings 
  173. CURLOPT_USERAGENT => "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; InfoPath.3; .NET4.0E; .NET CLR 1.1.4322)",  
  174. CURLOPT_AUTOREFERER => true, // set referer on redirect 
  175. CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect 
  176. CURLOPT_TIMEOUT => 120, // timeout on response 
  177. // CURLOPT_MAXREDIRS => $redirs, // stop after 10 redirects 
  178. CURLOPT_REFERER => $ref, // stop after 10 redirects  
  179. CURLINFO_HEADER_OUT => true 
  180. );  
  181. curl_setopt_array($ci, $options);  
  182.  
  183. curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout); 
  184. curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout); 
  185. curl_setopt($ci, CURLOPT_HTTPHEADER, $headers); 
  186. curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer); 
  187. // curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader')); 
  188.   
  189.   
  190. switch ($method) { 
  191. case 'POST': 
  192. curl_setopt($ci, CURLOPT_POST, TRUE); 
  193. if (!empty($postfields)) { 
  194. curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); 
  195. break; 
  196. case 'DELETE': 
  197. curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE'); 
  198. if (!empty($postfields)) { 
  199. $url = "{$url}?{$postfields}"; 
  200. //prr($url); 
  201. if ($method=='DELETE') curl_setopt($ci, CURLOPT_URL, $url);  
  202.  
  203. global $nxs_skipSSLCheck; if ($nxs_skipSSLCheck===true) curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false); 
  204.  
  205. $response = curl_exec($ci); $out = array(); 
  206.  
  207. /**  
  208. $err = curl_errno($ci); if ($err==28) { sleep(10); $tm = true; $response = curl_exec($ci); } // echo "##".$err; $errmsg = curl_error($ci); $out['errno'] = $err; $out['errmsg'] = $errmsg; prr($out); 
  209. $err = curl_errno($ci); if ($err>0) { $errmsg = curl_error($ci); $out['errno'] = $err; $out['errmsg'] = $errmsg; curl_close($ci); return $out; } 
  210. */ 
  211. //$err = curl_errno($ci); $errmsg = curl_error($ci); $header = curl_getinfo($ci); $header['errno'] = $err; $header['errmsg'] = $errmsg; $header['content'] = $response; prr($header); //die(); 
  212.  
  213. $err = curl_errno($ci); if ($err>0) { $errmsg = curl_error($ci); $out['errno'] = $err; $out['errmsg'] = $errmsg.". Tumblr API is down. Please try later."; $this->http_code = '404'; curl_close($ci); return json_encode($out); } 
  214.  
  215. $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);  
  216. $this->http_info = array_merge($this->http_info, curl_getinfo($ci)); 
  217. $this->url = $url; //prr($this); 
  218. curl_close ($ci); // prr($response); 
  219. return $response; 
  220.  
  221. /** 
  222. * Get the header info to store. 
  223. */ 
  224. function getHeader($ch, $header) { 
  225. $i = strpos($header, ':'); 
  226. if (!empty($i)) { 
  227. $key = str_replace('-', '_', strtolower(substr($header, 0, $i))); 
  228. $value = trim(substr($header, $i + 2)); 
  229. $this->http_header[$key] = $value; 
  230. return strlen($header);