1: <?php
2:
3: 4: 5: 6: 7:
8:
9:
10: require_once(SERVERPATH . '/' . ZENFOLDER . '/' . COMMON_FOLDER . '/oAuth/OAuth.php');
11:
12: 13: 14:
15: class TwitterOAuth {
16:
17:
18: public $http_code;
19:
20: public $url;
21:
22: public $host = "https://api.twitter.com/1.1/";
23:
24: public $timeout = 30;
25:
26: public $connecttimeout = 30;
27:
28: public $ssl_verifypeer = FALSE;
29:
30: public $format = 'json';
31:
32: public $decode_json = TRUE;
33:
34: public $http_info;
35:
36: public $useragent = 'TwitterOAuth v0.2.0-beta2';
37:
38:
39:
40:
41: 42: 43:
44: function accessTokenURL() {
45: return 'https://api.twitter.com/oauth/access_token';
46: }
47:
48: function authenticateURL() {
49: return 'https://api.twitter.com/oauth/authenticate';
50: }
51:
52: function authorizeURL() {
53: return 'https://api.twitter.com/oauth/authorize';
54: }
55:
56: function requestTokenURL() {
57: return 'https://api.twitter.com/oauth/request_token';
58: }
59:
60: 61: 62:
63: function lastStatusCode() {
64: return $this->http_status;
65: }
66:
67: function lastAPICall() {
68: return $this->last_api_call;
69: }
70:
71: 72: 73:
74: function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) {
75: $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
76: $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
77: if (!empty($oauth_token) && !empty($oauth_token_secret)) {
78: $this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
79: } else {
80: $this->token = NULL;
81: }
82: }
83:
84: 85: 86: 87: 88:
89: function getRequestToken($oauth_callback) {
90: $parameters = array();
91: $parameters['oauth_callback'] = $oauth_callback;
92: $request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters);
93: $token = OAuthUtil::parse_parameters($request);
94: $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
95: return $token;
96: }
97:
98: 99: 100: 101: 102:
103: function getAuthorizeURL($token, $sign_in_with_twitter = TRUE) {
104: if (is_array($token)) {
105: $token = $token['oauth_token'];
106: }
107: if (empty($sign_in_with_twitter)) {
108: return $this->authorizeURL() . "?oauth_token={$token}";
109: } else {
110: return $this->authenticateURL() . "?oauth_token={$token}";
111: }
112: }
113:
114: 115: 116: 117: 118: 119: 120: 121: 122:
123: function getAccessToken($oauth_verifier) {
124: $parameters = array();
125: $parameters['oauth_verifier'] = $oauth_verifier;
126: $request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $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: 133: 134: 135: 136: 137: 138: 139: 140:
141: function getXAuthToken($username, $password) {
142: $parameters = array();
143: $parameters['x_auth_username'] = $username;
144: $parameters['x_auth_password'] = $password;
145: $parameters['x_auth_mode'] = 'client_auth';
146: $request = $this->oAuthRequest($this->accessTokenURL(), 'POST', $parameters);
147: $token = OAuthUtil::parse_parameters($request);
148: $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
149: return $token;
150: }
151:
152: 153: 154:
155: function get($url, $parameters = array()) {
156: $response = $this->oAuthRequest($url, 'GET', $parameters);
157: if ($this->format === 'json' && $this->decode_json) {
158: return json_decode($response);
159: }
160: return $response;
161: }
162:
163: 164: 165:
166: function post($url, $parameters = array()) {
167: $response = $this->oAuthRequest($url, 'POST', $parameters);
168: if ($this->format === 'json' && $this->decode_json) {
169: return json_decode($response);
170: }
171: return $response;
172: }
173:
174: 175: 176:
177: function delete($url, $parameters = array()) {
178: $response = $this->oAuthRequest($url, 'DELETE', $parameters);
179: if ($this->format === 'json' && $this->decode_json) {
180: return json_decode($response);
181: }
182: return $response;
183: }
184:
185: 186: 187:
188: function oAuthRequest($url, $method, $parameters) {
189: if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) {
190: $url = "{$this->host}{$url}.{$this->format}";
191: }
192: $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
193: $request->sign_request($this->sha1_method, $this->consumer, $this->token);
194: switch ($method) {
195: case 'GET':
196: return $this->http($request->to_url(), 'GET');
197: default:
198: return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
199: }
200: }
201:
202: 203: 204: 205: 206:
207: function http($url, $method, $postfields = NULL) {
208: $this->http_info = array();
209: $ci = curl_init();
210:
211: curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
212: curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
213: curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
214: curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
215: curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
216: curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
217: curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
218: curl_setopt($ci, CURLOPT_HEADER, FALSE);
219:
220: switch ($method) {
221: case 'POST':
222: curl_setopt($ci, CURLOPT_POST, TRUE);
223: if (!empty($postfields)) {
224: curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
225: }
226: break;
227: case 'DELETE':
228: curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
229: if (!empty($postfields)) {
230: $url = "{$url}?{$postfields}";
231: }
232: }
233:
234: curl_setopt($ci, CURLOPT_URL, $url);
235: $response = curl_exec($ci);
236: $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
237: $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
238: $this->url = $url;
239: curl_close($ci);
240: return $response;
241: }
242:
243: 244: 245:
246: function getHeader($ch, $header) {
247: $i = strpos($header, ':');
248: if (!empty($i)) {
249: $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
250: $value = trim(substr($header, $i + 2));
251: $this->http_header[$key] = $value;
252: }
253: return strlen($header);
254: }
255:
256: }
257: