-
Notifications
You must be signed in to change notification settings - Fork 52
/
TwitterStrategy.php
211 lines (172 loc) · 6.81 KB
/
TwitterStrategy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/**
* Twitter strategy for Opauth
* Based on https://dev.twitter.com/docs/auth/obtaining-access-tokens
*
* More information on Opauth: http://opauth.org
*
* @copyright Copyright © 2012 U-Zyn Chua (http://uzyn.com)
* @link http://opauth.org
* @package Opauth.TwitterStrategy
* @license MIT License
*/
class TwitterStrategy extends OpauthStrategy {
/**
* Compulsory parameters
*/
public $expects = array('key', 'secret');
/**
* Optional parameters
*/
public $defaults = array(
'method' => 'POST', // The HTTP method being used. e.g. POST, GET, HEAD etc
'oauth_callback' => '{complete_url_to_strategy}oauth_callback',
// For Twitter
'request_token_url' => 'https://api.twitter.com/oauth/request_token',
'authorize_url' => 'https://api.twitter.com/oauth/authenticate', // or 'https://api.twitter.com/oauth/authorize'
'access_token_url' => 'https://api.twitter.com/oauth/access_token',
'verify_credentials_json_url' => 'https://api.twitter.com/1.1/account/verify_credentials.json',
'verify_credentials_skip_status' => true,
'twitter_profile_url' => 'http://twitter.com/{screen_name}',
// From tmhOAuth
'user_token' => '',
'user_secret' => '',
'use_ssl' => true,
'debug' => false,
'force_nonce' => false,
'nonce' => false, // used for checking signatures. leave as false for auto
'force_timestamp' => false,
'timestamp' => false, // used for checking signatures. leave as false for auto
'oauth_version' => '1.0',
'curl_connecttimeout' => 30,
'curl_timeout' => 10,
'curl_ssl_verifypeer' => false,
'curl_followlocation' => false, // whether to follow redirects or not
'curl_proxy' => false, // really you don't want to use this if you are using streaming
'curl_proxyuserpwd' => false, // format username:password for proxy, if required
'is_streaming' => false,
'streaming_eol' => "\r\n",
'streaming_metrics_interval' => 60,
'as_header' => true,
);
public function __construct($strategy, $env) {
parent::__construct($strategy, $env);
$this->strategy['consumer_key'] = $this->strategy['key'];
$this->strategy['consumer_secret'] = $this->strategy['secret'];
require dirname(__FILE__).'/Vendor/tmhOAuth/tmhOAuth.php';
$this->tmhOAuth = new tmhOAuth($this->strategy);
}
/**
* Auth request
*/
public function request() {
$params = array(
'oauth_callback' => $this->strategy['oauth_callback']
);
$results = $this->_request('POST', $this->strategy['request_token_url'], $params);
if ($results !== false && !empty($results['oauth_token']) && !empty($results['oauth_token_secret'])){
if (!session_id()) {
session_start();
}
$_SESSION['_opauth_twitter'] = $results;
$this->_authorize($results['oauth_token']);
}
}
/**
* Receives oauth_verifier, requests for access_token and redirect to callback
*/
public function oauth_callback() {
if (!session_id()) {
session_start();
}
$session = $_SESSION['_opauth_twitter'];
unset($_SESSION['_opauth_twitter']);
if (!empty($_REQUEST['oauth_token']) && $_REQUEST['oauth_token'] == $session['oauth_token']) {
$this->tmhOAuth->config['user_token'] = $session['oauth_token'];
$this->tmhOAuth->config['user_secret'] = $session['oauth_token_secret'];
$params = array(
'oauth_verifier' => $_REQUEST['oauth_verifier']
);
$results = $this->_request('POST', $this->strategy['access_token_url'], $params);
if ($results !== false && !empty($results['oauth_token']) && !empty($results['oauth_token_secret'])) {
$credentials = $this->_verify_credentials($results['oauth_token'], $results['oauth_token_secret']);
if (!empty($credentials['id'])) {
$this->auth = array(
'uid' => $credentials['id'],
'info' => array(
'name' => $credentials['name'],
'nickname' => $credentials['screen_name'],
'urls' => array(
'twitter' => str_replace('{screen_name}', $credentials['screen_name'], $this->strategy['twitter_profile_url'])
)
),
'credentials' => array(
'token' => $results['oauth_token'],
'secret' => $results['oauth_token_secret']
),
'raw' => $credentials
);
$this->mapProfile($credentials, 'location', 'info.location');
$this->mapProfile($credentials, 'description', 'info.description');
$this->mapProfile($credentials, 'profile_image_url_https', 'info.image');
$this->mapProfile($credentials, 'url', 'info.urls.website');
$this->mapProfile($credentials, 'email', 'info.email');
$this->callback();
}
}
} else {
$error = array(
'code' => 'access_denied',
'message' => 'User denied access.',
'raw' => $_GET
);
$this->errorCallback($error);
}
}
private function _authorize($oauth_token) {
$params = array(
'oauth_token' => $oauth_token
);
if (!empty($this->strategy['force_login'])) $params['force_login'] = $this->strategy['force_login'];
if (!empty($this->strategy['screen_name'])) $params['screen_name'] = $this->strategy['screen_name'];
$this->clientGet($this->strategy['authorize_url'], $params);
}
private function _verify_credentials($user_token, $user_token_secret) {
$this->tmhOAuth->config['user_token'] = $user_token;
$this->tmhOAuth->config['user_secret'] = $user_token_secret;
$params = array( 'skip_status' => $this->strategy['verify_credentials_skip_status'], 'include_email' => 'true' );
$response = $this->_request('GET', $this->strategy['verify_credentials_json_url'], $params);
return $this->recursiveGetObjectVars($response);
}
/**
* Wrapper of tmhOAuth's request() with Opauth's error handling.
*
* request():
* Make an HTTP request using this library. This method doesn't return anything.
* Instead the response should be inspected directly.
*
* @param string $method the HTTP method being used. e.g. POST, GET, HEAD etc
* @param string $url the request URL without query string parameters
* @param array $params the request parameters as an array of key=value pairs
* @param string $useauth whether to use authentication when making the request. Default true.
* @param string $multipart whether this request contains multipart data. Default false
*/
private function _request($method, $url, $params = array(), $useauth = true, $multipart = false) {
$code = $this->tmhOAuth->request($method, $url, $params, $useauth, $multipart);
if ($code == 200) {
if (strpos($url, '.json') !== false) {
$response = json_decode($this->tmhOAuth->response['response']);
} else {
$response = $this->tmhOAuth->extract_params($this->tmhOAuth->response['response']);
}
return $response;
} else {
$error = array(
'code' => $code,
'raw' => $this->tmhOAuth->response['response']
);
$this->errorCallback($error);
return false;
}
}
}