Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
7.7 KiB

9 years ago
  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('library/OAuth1.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.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://twitter.com/oauth/authenticate'; }
  40. function authorizeURL() { return 'https://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 OAuthSignatureMethod_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 = OAuthRequest::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. case 'UPLOAD':
  170. return $this->http($request->get_normalized_http_url(), 'POST', $request->to_postdata(true));
  171. default:
  172. return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
  173. }
  174. }
  175. /**
  176. * Make an HTTP request
  177. *
  178. * @return API results
  179. */
  180. function http($url, $method, $postfields = NULL) {
  181. $this->http_info = array();
  182. $ci = curl_init();
  183. /* Curl settings */
  184. curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
  185. curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
  186. curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
  187. curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
  188. curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
  189. curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
  190. curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
  191. curl_setopt($ci, CURLOPT_HEADER, FALSE);
  192. switch ($method) {
  193. case 'POST':
  194. curl_setopt($ci, CURLOPT_POST, TRUE);
  195. if (!empty($postfields)) {
  196. curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
  197. }
  198. break;
  199. case 'DELETE':
  200. curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
  201. if (!empty($postfields)) {
  202. $url = "{$url}?{$postfields}";
  203. }
  204. }
  205. curl_setopt($ci, CURLOPT_URL, $url);
  206. $response = curl_exec($ci);
  207. $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
  208. $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
  209. $this->url = $url;
  210. curl_close ($ci);
  211. return $response;
  212. }
  213. /**
  214. * Get the header info to store.
  215. */
  216. function getHeader($ch, $header) {
  217. $i = strpos($header, ':');
  218. if (!empty($i)) {
  219. $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
  220. $value = trim(substr($header, $i + 2));
  221. $this->http_header[$key] = $value;
  222. }
  223. return strlen($header);
  224. }
  225. }