PageRenderTime 54ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/twitter_api/vendors/twitteroauth/twitterOAuth.php

https://github.com/fragilbert/Elgg
PHP | 245 lines | 144 code | 22 blank | 79 comment | 21 complexity | 9811c75e3a696a5472c2d0818a091bce MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, LGPL-2.1, GPL-2.0
  1. <?php
  2. /*
  3. * Abraham Williams (abraham@abrah.am) http://abrah.am
  4. *
  5. * The first PHP Library to support OAuth for Twitter's REST API.
  6. */
  7. /* Load OAuth lib. You can find it at http://oauth.net */
  8. require_once('OAuth.php');
  9. /**
  10. * Twitter OAuth class
  11. */
  12. class TwitterOAuth {
  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 = "https://api.twitter.com/1/";
  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 = 'TwitterOAuth v0.2.0-beta2';
  33. /* Immediately retry the API call if the response was not successful. */
  34. //public $retry = TRUE;
  35. /**
  36. * Set API URLS
  37. */
  38. function accessTokenURL() { return 'https://api.twitter.com/oauth/access_token'; }
  39. function authenticateURL() { return 'https://api.twitter.com/oauth/authenticate'; }
  40. function authorizeURL() { return 'https://api.twitter.com/oauth/authorize'; }
  41. function requestTokenURL() { return 'https://api.twitter.com/oauth/request_token'; }
  42. /**
  43. * Debug helpers
  44. */
  45. function lastStatusCode() { return $this->http_status; }
  46. function lastAPICall() { return $this->last_api_call; }
  47. /**
  48. * construct TwitterOAuth object
  49. */
  50. function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) {
  51. $this->sha1_method = new twitterOAuthSignatureMethod_HMAC_SHA1();
  52. $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
  53. if (!empty($oauth_token) && !empty($oauth_token_secret)) {
  54. $this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
  55. } else {
  56. $this->token = NULL;
  57. }
  58. }
  59. /**
  60. * Get a request_token from Twitter
  61. *
  62. * @returns a key/value array containing oauth_token and oauth_token_secret
  63. */
  64. function getRequestToken($oauth_callback = NULL) {
  65. $parameters = array();
  66. if (!empty($oauth_callback)) {
  67. $parameters['oauth_callback'] = $oauth_callback;
  68. }
  69. $request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters);
  70. $token = OAuthUtil::parse_parameters($request);
  71. $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
  72. return $token;
  73. }
  74. /**
  75. * Get the authorize URL
  76. *
  77. * @returns a string
  78. */
  79. function getAuthorizeURL($token, $sign_in_with_twitter = TRUE) {
  80. if (is_array($token)) {
  81. $token = $token['oauth_token'];
  82. }
  83. if (empty($sign_in_with_twitter)) {
  84. return $this->authorizeURL() . "?oauth_token={$token}";
  85. } else {
  86. return $this->authenticateURL() . "?oauth_token={$token}";
  87. }
  88. }
  89. /**
  90. * Exchange request token and secret for an access token and
  91. * secret, to sign API calls.
  92. *
  93. * @returns array("oauth_token" => "the-access-token",
  94. * "oauth_token_secret" => "the-access-secret",
  95. * "user_id" => "9436992",
  96. * "screen_name" => "abraham")
  97. */
  98. function getAccessToken($oauth_verifier = FALSE) {
  99. $parameters = array();
  100. if (!empty($oauth_verifier)) {
  101. $parameters['oauth_verifier'] = $oauth_verifier;
  102. }
  103. $request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters);
  104. $token = OAuthUtil::parse_parameters($request);
  105. $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
  106. return $token;
  107. }
  108. /**
  109. * One time exchange of username and password for access token and secret.
  110. *
  111. * @returns array("oauth_token" => "the-access-token",
  112. * "oauth_token_secret" => "the-access-secret",
  113. * "user_id" => "9436992",
  114. * "screen_name" => "abraham",
  115. * "x_auth_expires" => "0")
  116. */
  117. function getXAuthToken($username, $password) {
  118. $parameters = array();
  119. $parameters['x_auth_username'] = $username;
  120. $parameters['x_auth_password'] = $password;
  121. $parameters['x_auth_mode'] = 'client_auth';
  122. $request = $this->oAuthRequest($this->accessTokenURL(), 'POST', $parameters);
  123. $token = OAuthUtil::parse_parameters($request);
  124. $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
  125. return $token;
  126. }
  127. /**
  128. * GET wrapper for oAuthRequest.
  129. */
  130. function get($url, $parameters = array()) {
  131. $response = $this->oAuthRequest($url, 'GET', $parameters);
  132. if ($this->format === 'json' && $this->decode_json) {
  133. return json_decode($response);
  134. }
  135. return $response;
  136. }
  137. /**
  138. * POST wrapper for oAuthRequest.
  139. */
  140. function post($url, $parameters = array()) {
  141. $response = $this->oAuthRequest($url, 'POST', $parameters);
  142. if ($this->format === 'json' && $this->decode_json) {
  143. return json_decode($response);
  144. }
  145. return $response;
  146. }
  147. /**
  148. * DELETE wrapper for oAuthReqeust.
  149. */
  150. function delete($url, $parameters = array()) {
  151. $response = $this->oAuthRequest($url, 'DELETE', $parameters);
  152. if ($this->format === 'json' && $this->decode_json) {
  153. return json_decode($response);
  154. }
  155. return $response;
  156. }
  157. /**
  158. * Format and sign an OAuth / API request
  159. */
  160. function oAuthRequest($url, $method, $parameters) {
  161. if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) {
  162. $url = "{$this->host}{$url}.{$this->format}";
  163. }
  164. $request = twitterOAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
  165. $request->sign_request($this->sha1_method, $this->consumer, $this->token);
  166. switch ($method) {
  167. case 'GET':
  168. return $this->http($request->to_url(), 'GET');
  169. default:
  170. return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
  171. }
  172. }
  173. /**
  174. * Make an HTTP request
  175. *
  176. * @return API results
  177. */
  178. function http($url, $method, $postfields = NULL) {
  179. $this->http_info = array();
  180. $ci = curl_init();
  181. /* Curl settings */
  182. curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
  183. curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
  184. curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
  185. curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
  186. curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
  187. curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
  188. curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
  189. curl_setopt($ci, CURLOPT_HEADER, FALSE);
  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. }
  196. break;
  197. case 'DELETE':
  198. curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
  199. if (!empty($postfields)) {
  200. $url = "{$url}?{$postfields}";
  201. }
  202. }
  203. curl_setopt($ci, CURLOPT_URL, $url);
  204. $response = curl_exec($ci);
  205. $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
  206. $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
  207. $this->url = $url;
  208. curl_close ($ci);
  209. return $response;
  210. }
  211. /**
  212. * Get the header info to store.
  213. */
  214. function getHeader($ch, $header) {
  215. $i = strpos($header, ':');
  216. if (!empty($i)) {
  217. $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
  218. $value = trim(substr($header, $i + 2));
  219. $this->http_header[$key] = $value;
  220. }
  221. return strlen($header);
  222. }
  223. }