1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Abraham\TwitterOAuth\TwitterOAuth; |
4
|
|
|
use Abraham\TwitterOAuth\TwitterOAuthException; |
5
|
|
|
|
6
|
|
|
class TwitterService |
|
|
|
|
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* @config |
10
|
|
|
* |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
private static $consumer_key; |
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @config |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private static $consumer_secret; |
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @config |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private static $oauth_callback; |
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Create a Twitter API interface. |
31
|
|
|
* |
32
|
|
|
* @param string $token |
33
|
|
|
* |
34
|
|
|
* @param string $secret |
35
|
|
|
* |
36
|
|
|
* @return Abraham\TwitterOAuth\TwitterOAuth |
37
|
|
|
*/ |
38
|
|
|
public function getClient($token = null, $secret = null) |
39
|
|
|
{ |
40
|
|
|
$consumer_key = Config::inst()->get('TwitterService', 'consumer_key'); |
41
|
|
|
$consumer_secret = Config::inst()->get('TwitterService', 'consumer_secret'); |
42
|
|
|
|
43
|
|
|
if (!$consumer_key) { |
44
|
|
|
user_error( |
45
|
|
|
'Add a consumer_key to config (TwitterService::consumer_key)', |
46
|
|
|
E_USER_ERROR |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (!$consumer_secret) { |
51
|
|
|
user_error( |
52
|
|
|
'Add a consumer_secret to config (TwitterService::consumer_secret)', |
53
|
|
|
E_USER_ERROR |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return new TwitterOAuth($consumer_key, $consumer_secret, $token, $secret); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Gets the OAuth callback URL. |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
public function getOAuthCallback() |
66
|
|
|
{ |
67
|
|
|
$oauth_callback = Config::inst()->get('TwitterService', 'oauth_callback'); |
68
|
|
|
|
69
|
|
|
if (!$oauth_callback) { |
70
|
|
|
user_error( |
71
|
|
|
'Add a oauth_callback to config (TwitterService::oauth_callback)', |
72
|
|
|
E_USER_ERROR |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return Director::absoluteBaseURL() . $oauth_callback; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Gets a temporary request token to kick off the OAuth flow. |
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
public function getOAuthToken() |
85
|
|
|
{ |
86
|
|
|
$requestToken = [ |
87
|
|
|
'oauth_token' => '', |
88
|
|
|
'oauth_token_secret' => '', |
89
|
|
|
]; |
90
|
|
|
|
91
|
|
|
$client = $this->getClient(); |
92
|
|
|
|
93
|
|
|
try { |
94
|
|
|
$response = $client->oauth('oauth/request_token', ['oauth_callback' => $this->getOAuthCallback()]); |
95
|
|
|
|
96
|
|
|
$requestToken['oauth_token'] = $response['oauth_token']; |
97
|
|
|
$requestToken['oauth_token_secret'] = $response['oauth_token_secret']; |
98
|
|
|
} catch (TwitterOAuthException $e) { |
99
|
|
|
user_error($e->getMessage(), E_USER_WARNING); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $requestToken; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Gets the login URL for users to authenticate with. |
107
|
|
|
* |
108
|
|
|
* @param array $token |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function getAuthoriseUrl($token) |
113
|
|
|
{ |
114
|
|
|
$authoriseUrl = ''; |
115
|
|
|
$client = $this->getClient(); |
116
|
|
|
|
117
|
|
|
if (array_key_exists('oauth_token', $token)) { |
118
|
|
|
$authoriseUrl = $client->url('oauth/authorize', ['oauth_token' => $token['oauth_token']]); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $authoriseUrl; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Sets the temporary request token in session. |
126
|
|
|
* |
127
|
|
|
* @param array $token |
128
|
|
|
*/ |
129
|
|
|
public function setSessionOAuthToken($token) |
130
|
|
|
{ |
131
|
|
|
$sessionRequestToken = Session::get('TwitterOAuthToken'); |
132
|
|
|
$sessionRequestToken = $sessionRequestToken ? $sessionRequestToken : []; |
133
|
|
|
|
134
|
|
|
$sessionRequestToken['oauth_token'] = $token['oauth_token']; |
135
|
|
|
$sessionRequestToken['oauth_token_secret'] = $token['oauth_token_secret']; |
136
|
|
|
|
137
|
|
|
Session::set('TwitterOAuthToken', $sessionRequestToken); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Gets the temporary request token from session. |
142
|
|
|
* |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
|
|
public function getSessionOAuthToken() { |
146
|
|
|
return Session::get('TwitterOAuthToken'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Clears the temporary token from session. |
151
|
|
|
*/ |
152
|
|
|
public function clearSessionOAuthToken() |
153
|
|
|
{ |
154
|
|
|
Session::clear('TwitterOAuthToken'); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Gets an access token used for making authenticated requests to the Twitter API. |
159
|
|
|
* |
160
|
|
|
* @param string $token |
161
|
|
|
* |
162
|
|
|
* @param string $secret |
163
|
|
|
* |
164
|
|
|
* @param string $verifier |
165
|
|
|
* |
166
|
|
|
* @return array|null |
167
|
|
|
*/ |
168
|
|
|
public function getAccessToken($token, $secret, $verifier) |
169
|
|
|
{ |
170
|
|
|
$accessToken = null; |
171
|
|
|
|
172
|
|
|
$client = $this->getClient($token, $secret); |
173
|
|
|
|
174
|
|
|
try { |
175
|
|
|
$accessToken = $client->oauth('oauth/access_token', ['oauth_verifier' => $verifier]); |
176
|
|
|
} catch (TwitterOAuthException $e) { |
177
|
|
|
user_error($e->getMessage(), E_USER_WARNING); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $accessToken; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.