PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/tw/twitteroauth.php

https://bitbucket.org/sharifbaba/snaptitoday-dev
PHP | 231 lines | 138 code | 20 blank | 73 comment | 25 complexity | f589d0327e8270b6461b4d070b3d125d MD5 | raw file
  1. <?php
  2. /*
  3. * Abraham Williams (abraham@abrah.am) http://abrah.am
  4. *
  5. * Basic lib to work with Twitter's OAuth beta. This is untested and should not
  6. * be used in production code. Twitter's beta could change at anytime.
  7. *
  8. * Code based on:
  9. * Fire Eagle code - http://github.com/myelin/fireeagle-php-lib
  10. * twitterlibphp - http://github.com/jdp/twitterlibphp
  11. */
  12. /* Load OAuth lib. You can find it at http://oauth.net */
  13. require_once('OAuth.php');
  14. /**
  15. * Twitter OAuth class
  16. */
  17. class TwitterOAuth {
  18. /* Contains the last HTTP status code returned. */
  19. public $http_code;
  20. /* Contains the last API call. */
  21. public $url;
  22. /* Set up the API root URL. */
  23. public $host = "https://api.twitter.com/1/";
  24. /* Set timeout default. */
  25. public $timeout = 30;
  26. /* Set connect timeout. */
  27. public $connecttimeout = 30;
  28. /* Verify SSL Cert. */
  29. public $ssl_verifypeer = FALSE;
  30. /* Respons format. */
  31. public $format = 'json';
  32. /* Decode returned json data. */
  33. public $decode_json = TRUE;
  34. /* Contains the last HTTP headers returned. */
  35. public $http_info;
  36. /* Set the useragnet. */
  37. public $useragent = 'TwitterOAuth v0.2.0-beta2';
  38. /* Immediately retry the API call if the response was not successful. */
  39. //public $retry = TRUE;
  40. /**
  41. * Set API URLS
  42. */
  43. function accessTokenURL() { return 'https://twitter.com/oauth/access_token'; }
  44. function authenticateURL() { return 'https://twitter.com/oauth/authenticate'; }
  45. function authorizeURL() { return 'https://twitter.com/oauth/authorize'; }
  46. function requestTokenURL() { return 'https://twitter.com/oauth/request_token'; }
  47. /**
  48. * Debug helpers
  49. */
  50. function lastStatusCode() { return $this->http_status; }
  51. function lastAPICall() { return $this->last_api_call; }
  52. /**
  53. * construct TwitterOAuth object
  54. */
  55. function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) {
  56. $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
  57. $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
  58. if (!empty($oauth_token) && !empty($oauth_token_secret)) {
  59. $this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
  60. } else {
  61. $this->token = NULL;
  62. }
  63. }
  64. /**
  65. * Get a request_token from Twitter
  66. *
  67. * @returns a key/value array containing oauth_token and oauth_token_secret
  68. */
  69. function getRequestToken($oauth_callback = NULL) {
  70. $parameters = array();
  71. if (!empty($oauth_callback)) {
  72. $parameters['oauth_callback'] = $oauth_callback;
  73. }
  74. $request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters);
  75. $token = OAuthUtil::parse_parameters($request);
  76. if(!isset($token['oauth_token']) || !isset($token['oauth_token_secret']))
  77. return false;
  78. $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
  79. return $token;
  80. }
  81. /**
  82. * Get the authorize URL
  83. *
  84. * @returns a string
  85. */
  86. function getAuthorizeURL($token, $sign_in_with_twitter = TRUE) {
  87. if (is_array($token)) {
  88. $token = $token['oauth_token'];
  89. }
  90. if (empty($sign_in_with_twitter)) {
  91. return $this->authorizeURL() . "?oauth_token={$token}";
  92. } else {
  93. return $this->authenticateURL() . "?oauth_token={$token}";
  94. }
  95. }
  96. /**
  97. * Exchange the request token and secret for an access token and
  98. * secret, to sign API calls.
  99. *
  100. * @returns array("oauth_token" => the access token,
  101. * "oauth_token_secret" => the access secret)
  102. */
  103. function getAccessToken($oauth_verifier = FALSE) {
  104. $parameters = array();
  105. if (!empty($oauth_verifier)) {
  106. $parameters['oauth_verifier'] = $oauth_verifier;
  107. }
  108. $request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters);
  109. $token = OAuthUtil::parse_parameters($request);
  110. if(!isset($token['oauth_token']) || !isset($token['oauth_token_secret']))
  111. return false;
  112. $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
  113. return $token;
  114. }
  115. /**
  116. * GET wrappwer for oAuthRequest.
  117. */
  118. function get($url, $parameters = array()) {
  119. $response = $this->oAuthRequest($url, 'GET', $parameters);
  120. if ($this->format === 'json' && $this->decode_json) {
  121. return json_decode($response);
  122. }
  123. return $response;
  124. }
  125. /**
  126. * POST wreapper for oAuthRequest.
  127. */
  128. function post($url, $parameters = array()) {
  129. $response = $this->oAuthRequest($url, 'POST', $parameters);
  130. if ($this->format === 'json' && $this->decode_json) {
  131. return json_decode($response);
  132. }
  133. return $response;
  134. }
  135. /**
  136. * DELTE wrapper for oAuthReqeust.
  137. */
  138. function delete($url, $parameters = array()) {
  139. $response = $this->oAuthRequest($url, 'DELETE', $parameters);
  140. if ($this->format === 'json' && $this->decode_json) {
  141. return json_decode($response);
  142. }
  143. return $response;
  144. }
  145. /**
  146. * Format and sign an OAuth / API request
  147. */
  148. function oAuthRequest($url, $method, $parameters) {
  149. if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) {
  150. $url = "{$this->host}{$url}.{$this->format}";
  151. }
  152. $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
  153. $request->sign_request($this->sha1_method, $this->consumer, $this->token);
  154. switch ($method) {
  155. case 'GET':
  156. return $this->http($request->to_url(), 'GET');
  157. default:
  158. return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
  159. }
  160. }
  161. /**
  162. * Make an HTTP request
  163. *
  164. * @return API results
  165. */
  166. function http($url, $method, $postfields = NULL) {
  167. $this->http_info = array();
  168. $ci = curl_init();
  169. /* Curl settings */
  170. curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
  171. curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
  172. curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
  173. curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
  174. curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
  175. curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
  176. curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
  177. curl_setopt($ci, CURLOPT_HEADER, FALSE);
  178. switch ($method) {
  179. case 'POST':
  180. curl_setopt($ci, CURLOPT_POST, TRUE);
  181. if (!empty($postfields)) {
  182. curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
  183. }
  184. break;
  185. case 'DELETE':
  186. curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
  187. if (!empty($postfields)) {
  188. $url = "{$url}?{$postfields}";
  189. }
  190. }
  191. curl_setopt($ci, CURLOPT_URL, $url);
  192. $response = curl_exec($ci);
  193. $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
  194. $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
  195. $this->url = $url;
  196. curl_close ($ci);
  197. return $response;
  198. }
  199. /**
  200. * Get the header info to store.
  201. */
  202. function getHeader($ch, $header) {
  203. $i = strpos($header, ':');
  204. if (!empty($i)) {
  205. $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
  206. $value = trim(substr($header, $i + 2));
  207. $this->http_header[$key] = $value;
  208. }
  209. return strlen($header);
  210. }
  211. }