PageRenderTime 26ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/monarch/includes/twitter_auth.php

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