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