This repository has been archived by the owner on Jul 13, 2018. It is now read-only.
/
bearer.php
291 lines (252 loc) · 5.57 KB
/
bearer.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
/**
* Tweet Display Back Module for Joomla!
*
* @copyright Copyright (C) 2010-2015 Michael Babker. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License Version 2 or Later
*/
defined('JPATH_PLATFORM') or die;
/**
* Bearer class for Tweet Display Back
*
* @since 3.1
*/
class BDBearer
{
/**
* The name of the cache file to use to store the bearer token
*
* @var string
* @since 3.1
*/
private $cache_file = 'tweetdisplayback_bearer.json';
/**
* The time in seconds to cache the bearer token
*
* @var integer
* @since 3.1
*/
private $cache_time = -1;
/**
* BDHttp connector
*
* @var BDHttp
* @since 3.1
*/
protected $connector = null;
/**
* The bearer token
*
* @var string
* @since 3.1
*/
public $token = null;
/**
* Constructor
*
* @param JRegistry $params The module parameters
* @param BDHttp $connector BDHttp connector
*
* @since 3.1
*/
public function __construct($params, BDHttp $connector)
{
// Store the module params
$this->params = $params;
// Store the connector
$this->connector = $connector;
// Prepare the token
$this->prepareToken();
}
/**
* Function to create the bearer authentication value for use in HTTP headers
*
* @return string
*
* @since 3.1
*/
protected function prepareBearerAuth()
{
$ckey = rawurlencode($this->params->get('consumer_key', ''));
$csec = rawurlencode($this->params->get('consumer_secret', ''));
return ($ckey && $csec) ? base64_encode("{$ckey}:{$csec}") : '';
}
/**
* Function to convert the bearer_cache_time_unit and _qty into epoch seconds
*
* @return integer The time in seconds to cache the bearer token
*
* @since 3.1
*/
protected function cacheTime()
{
if ($this->cache_time < 0)
{
$this->prepareCacheTime();
}
return (int) $this->cache_time;
}
/**
* Function to retrieve a bearer token from Twitter's API using a consumer key
*
* @return void
*
* @since 3.1
* @throws RuntimeException
*/
protected function callConsumer()
{
$auth = $this->prepareBearerAuth();
if ($auth)
{
$url = "https://api.twitter.com/oauth2/token";
$headers = array(
'Authorization' => "Basic {$auth}",
);
$data = "grant_type=client_credentials";
$response = $this->connector->post($url, $data, $headers);
if ($response->code == 200)
{
$this->token = json_decode($response->body)->access_token;
}
else
{
throw new RuntimeException('Could not retrieve bearer token (consumer)');
}
$this->writeCache();
}
else
{
throw new RuntimeException('Invalid consumer key/secret in configuration');
}
}
/**
* Function to retrieve a bearer token from a remote URL
*
* @return void
*
* @since 3.1
* @throws RuntimeException
*/
protected function callRemoteUrl()
{
$url = $this->params->get('remote_url', 'http://tdbtoken.gopagoda.com/tokenRequest.php');
// Call consumer or RemoteURL
$response = $this->connector->get($url);
if ($response->code == 200)
{
$this->token = str_replace('Bearer ', '', base64_decode($response->body));
}
else
{
throw new RuntimeException('Could not retrieve bearer token (remote)');
}
$this->writeCache();
}
/**
* Function to convert the bearer_cache_time_unit and _qty into epoch seconds
*
* @return void
*
* @since 3.1
*/
protected function prepareCacheTime()
{
$cacheTime = (int) $this->params->get('bearer_cache_time_qty', 1);
$cacheUnit = $this->params->get('bearer_cache_time_unit', '');
if (!$cacheUnit)
{
$cacheUnit = 'day';
$cacheTime = 1;
}
switch ($cacheUnit)
{
case 'hour':
$cacheTime *= 3600;
break;
case 'min':
$cacheTime *= 60;
break;
case 'week':
$cacheTime *= 86400 * 7;
break;
case 'noexp':
$cacheTime = 0;
break;
case 'day':
default:
$cacheTime *= 86400;
break;
}
$this->cache_time = $cacheTime;
}
/**
* Function to obtain a bearer token, if none is cached
*
* @return boolean True if a bearer token is available, otherwise false
*
* @since 3.1
*/
protected function prepareToken()
{
// If we haven't retrieved the bearer yet, get it if in the site application
if (($this->token == null) && JFactory::getApplication()->isSite())
{
$cacheTime = $this->cacheTime();
$cacheFile = JPATH_CACHE . DIRECTORY_SEPARATOR . $this->cache_file;
// Check if we have cached data and use it if unexpired
if (!file_exists($cacheFile) || ($cacheTime && (time() - @filemtime($cacheFile) > $cacheTime)))
{
// call consumer or RemoteURL
switch ($this->params->get('token_source', 'consumer'))
{
case 'remote':
$this->callRemoteUrl();
break;
case 'consumer':
default:
$this->callConsumer();
break;
}
// Write the cache
$this->writeCache();
}
else
{
// Render from the cached data
$this->token = $this->readCache();
}
}
return !empty($this->token);
}
/**
* Function to read the bearer token from cache
*
* @return string
*
* @since 3.1
*/
protected function readCache()
{
$cacheFile = JPATH_CACHE . '/' . $this->cache_file;
$ret = '';
if (file_exists($cacheFile))
{
$ret = file_get_contents($cacheFile);
}
return $ret;
}
/**
* Function to cache the bearer token
*
* @return void
*
* @since 3.1
*/
protected function writeCache()
{
$cacheFile = JPATH_CACHE . DIRECTORY_SEPARATOR . $this->cache_file;
// Write the cache
file_put_contents($cacheFile, $this->token);
}
}