forked from fennb/phirehose
/
UserstreamPhirehose.php
496 lines (393 loc) · 19.3 KB
/
UserstreamPhirehose.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
<?php
abstract class UserstreamPhirehose extends Phirehose {
const URL_BASE = 'https://userstream.twitter.com/2/';
const METHOD_USER = 'user';
const CONNECT_OAUTH = 'oauth';
const CONNECT_BASIC = 'basic';
const USER_AGENT = 'Phirehose (https://github.com/fennb/phirehose)';
protected $auth_method;
public static function Initialize($basic_username = NULL, $basic_password = NULL, $oauth_token = NULL, $oauth_secret = NULL) {
if (! self::$instance instanceof UserstreamPhirehose) {
self::$instance = new UserstreamPhirehose(
$basic_username,
$basic_password,
UserstreamPhirehose::METHOD_USER
);
}
}
public function __construct($username, $password, $method = UserstreamPhirehose::METHOD_USER, $format = self::FORMAT_JSON, $auth_method = UserstreamPhirehose::CONNECT_OAUTH) {
parent::__construct($username, $password, $method, $format);
$this->auth_method = $auth_method;
}
protected function connect() {
if ($this->auth_method === UserstreamPhirehose::CONNECT_OAUTH) {
$this->connect_oauth();
} else {
$this->connect_basic();
}
}
/**
* Connects to the stream URL using the configured method.
*/
protected function connect_basic() {
// Init state
$connectFailures = 0;
$tcpRetry = $this->tcpBackoff / 2;
$httpRetry = $this->httpBackoff / 2;
// Keep trying until connected (or max connect failures exceeded)
do {
// Check filter predicates for every connect (for filter method)
if ($this->method == self::METHOD_FILTER) {
$this->checkFilterPredicates();
}
// Construct URL/HTTP bits
$url = self::URL_BASE . $this->method . '.' . $this->format;
$urlParts = parse_url($url);
$authCredentials = base64_encode($this->username . ':' . $this->password);
// Setup params appropriately
$requestParams = array('delimited' => 'length');
// Filter takes additional parameters
if ($this->method == self::METHOD_USER && count($this->trackWords) > 0) {
$requestParams['track'] = implode(',', $this->trackWords);
}
if ($this->method == self::METHOD_USER && count($this->followIds) > 0) {
$requestParams['follow'] = implode(',', $this->followIds);
}
// Debugging is useful
$this->log('Connecting to twitter stream: ' . $url . ' with params: ' . str_replace("\n", '',
var_export($requestParams, TRUE)));
/**
* Open socket connection to make POST request. It'd be nice to use stream_context_create with the native
* HTTP transport but it hides/abstracts too many required bits (like HTTP error responses).
*/
$errNo = $errStr = NULL;
$scheme = ($urlParts['scheme'] == 'https') ? 'ssl://' : 'tcp://';
$port = ($urlParts['scheme'] == 'https') ? 443 : 80;
/**
* We must perform manual host resolution here as Twitter's IP regularly rotates (ie: DNS TTL of 60 seconds) and
* PHP appears to cache it the result if in a long running process (as per Phirehose).
*/
$streamIPs = gethostbynamel($urlParts['host']);
if (empty($streamIPs)) {
throw new PhirehoseNetworkException("Unable to resolve hostname: '" . $urlParts['host'] . '"');
}
// Choose one randomly (if more than one)
$this->log('Resolved host ' . $urlParts['host'] . ' to ' . implode(', ', $streamIPs));
$streamIP = $streamIPs[rand(0, (count($streamIPs) - 1))];
$this->log('Connecting to ' . $streamIP);
@$this->conn = fsockopen($scheme . $streamIP, $port, $errNo, $errStr, $this->connectTimeout);
// No go - handle errors/backoff
if (!$this->conn || !is_resource($this->conn)) {
$this->lastErrorMsg = $errStr;
$this->lastErrorNo = $errNo;
$connectFailures ++;
if ($connectFailures > $this->connectFailuresMax) {
$msg = 'TCP failure limit exceeded with ' . $connectFailures . ' failures. Last error: ' . $errStr;
$this->log($msg,'error');
throw new PhirehoseConnectLimitExceeded($msg, $errNo); // Throw an exception for other code to handle
}
// Increase retry/backoff up to max
$tcpRetry = ($tcpRetry < $this->tcpBackoffMax) ? $tcpRetry * 2 : $this->tcpBackoffMax;
$this->log('TCP failure ' . $connectFailures . ' of ' . $this->connectFailuresMax . ' connecting to stream: ' .
$errStr . ' (' . $errNo . '). Sleeping for ' . $tcpRetry . ' seconds.','info');
sleep($tcpRetry);
continue;
}
// TCP connect OK, clear last error (if present)
$this->log('Connection established to ' . $streamIP);
$this->lastErrorMsg = NULL;
$this->lastErrorNo = NULL;
// If we have a socket connection, we can attempt a HTTP request - Ensure blocking read for the moment
stream_set_blocking($this->conn, 1);
// Encode request data
$postData = http_build_query($requestParams);
// Do it
fwrite($this->conn, "POST " . $urlParts['path'] . " HTTP/1.0\r\n");
fwrite($this->conn, "Host: " . $urlParts['host'] . "\r\n");
fwrite($this->conn, "Content-type: application/x-www-form-urlencoded\r\n");
fwrite($this->conn, "Content-length: " . strlen($postData) . "\r\n");
fwrite($this->conn, "Accept: */*\r\n");
fwrite($this->conn, 'Authorization: Basic ' . $authCredentials . "\r\n");
fwrite($this->conn, 'User-Agent: ' . self::USER_AGENT . "\r\n");
fwrite($this->conn, "\r\n");
fwrite($this->conn, $postData . "\r\n");
fwrite($this->conn, "\r\n");
// First line is response
list($httpVer, $httpCode, $httpMessage) = preg_split('/\s+/', trim(fgets($this->conn, 1024)), 3);
// Response buffers
$respHeaders = $respBody = '';
// Consume each header response line until we get to body
while ($hLine = trim(fgets($this->conn, 4096))) {
$respHeaders .= $hLine;
}
// If we got a non-200 response, we need to backoff and retry
if ($httpCode != 200) {
$connectFailures ++;
// Twitter will disconnect on error, but we want to consume the rest of the response body (which is useful)
while ($bLine = trim(fgets($this->conn, 4096))) {
$respBody .= $bLine;
}
// Construct error
$errStr = 'HTTP ERROR ' . $httpCode . ': ' . $httpMessage . ' (' . $respBody . ')';
// Set last error state
$this->lastErrorMsg = $errStr;
$this->lastErrorNo = $httpCode;
// Have we exceeded maximum failures?
if ($connectFailures > $this->connectFailuresMax) {
$msg = 'Connection failure limit exceeded with ' . $connectFailures . ' failures. Last error: ' . $errStr;
$this->log($msg,'error');
throw new PhirehoseConnectLimitExceeded($msg, $httpCode); // We eventually throw an exception for other code to handle
}
// Increase retry/backoff up to max
$httpRetry = ($httpRetry < $this->httpBackoffMax) ? $httpRetry * 2 : $this->httpBackoffMax;
$this->log('HTTP failure ' . $connectFailures . ' of ' . $this->connectFailuresMax . ' connecting to stream: ' .
$errStr . '. Sleeping for ' . $httpRetry . ' seconds.','info');
sleep($httpRetry);
continue;
} // End if not http 200
// Loop until connected OK
} while (!is_resource($this->conn) || $httpCode != 200);
// Connected OK, reset connect failures
$connectFailures = 0;
$this->lastErrorMsg = NULL;
$this->lastErrorNo = NULL;
// Switch to non-blocking to consume the stream (important)
stream_set_blocking($this->conn, 0);
// Connect always causes the filterChanged status to be cleared
$this->filterChanged = FALSE;
// Flush stream buffer & (re)assign fdrPool (for reconnect)
$this->fdrPool = array($this->conn);
$this->buff = '';
}
protected function connect_oauth() {
// Init state
$connectFailures = 0;
$tcpRetry = $this->tcpBackoff / 2;
$httpRetry = $this->httpBackoff / 2;
// Keep trying until connected (or max connect failures exceeded)
do {
// Check filter predicates for every connect (for filter method)
if ($this->method == self::METHOD_FILTER) {
$this->checkFilterPredicates();
}
// Construct URL/HTTP bits
$url = self::URL_BASE . $this->method . '.' . $this->format;
$urlParts = parse_url($url);
$authCredentials = base64_encode($this->username . ':' . $this->password);
// Setup params appropriately
$requestParams = array('delimited' => 'length');
// Filter takes additional parameters
if ($this->method == self::METHOD_USER && count($this->trackWords) > 0) {
$requestParams['track'] = implode(',', $this->trackWords);
}
if ($this->method == self::METHOD_USER && count($this->followIds) > 0) {
$requestParams['follow'] = implode(',', $this->followIds);
}
// Debugging is useful
$this->log('Connecting to twitter stream: ' . $url . ' with params: ' . str_replace("\n", '',
var_export($requestParams, TRUE)));
/**
* Open socket connection to make POST request. It'd be nice to use stream_context_create with the native
* HTTP transport but it hides/abstracts too many required bits (like HTTP error responses).
*/
$errNo = $errStr = NULL;
$scheme = ($urlParts['scheme'] == 'https') ? 'ssl://' : 'tcp://';
$port = ($urlParts['scheme'] == 'https') ? 443 : 80;
/**
* We must perform manual host resolution here as Twitter's IP regularly rotates (ie: DNS TTL of 60 seconds) and
* PHP appears to cache it the result if in a long running process (as per Phirehose).
*/
$streamIPs = gethostbynamel($urlParts['host']);
if (empty($streamIPs)) {
throw new PhirehoseNetworkException("Unable to resolve hostname: '" . $urlParts['host'] . '"');
}
// Choose one randomly (if more than one)
$this->log('Resolved host ' . $urlParts['host'] . ' to ' . implode(', ', $streamIPs));
$streamIP = $streamIPs[rand(0, (count($streamIPs) - 1))];
$this->log('Connecting to ' . $streamIP);
@$this->conn = fsockopen($scheme . $streamIP, $port, $errNo, $errStr, $this->connectTimeout);
// No go - handle errors/backoff
if (!$this->conn || !is_resource($this->conn)) {
$this->lastErrorMsg = $errStr;
$this->lastErrorNo = $errNo;
$connectFailures ++;
if ($connectFailures > $this->connectFailuresMax) {
$msg = 'TCP failure limit exceeded with ' . $connectFailures . ' failures. Last error: ' . $errStr;
$this->log($msg,'error');
throw new PhirehoseConnectLimitExceeded($msg, $errNo); // Throw an exception for other code to handle
}
// Increase retry/backoff up to max
$tcpRetry = ($tcpRetry < $this->tcpBackoffMax) ? $tcpRetry * 2 : $this->tcpBackoffMax;
$this->log('TCP failure ' . $connectFailures . ' of ' . $this->connectFailuresMax . ' connecting to stream: ' .
$errStr . ' (' . $errNo . '). Sleeping for ' . $tcpRetry . ' seconds.','info');
sleep($tcpRetry);
continue;
}
// TCP connect OK, clear last error (if present)
$this->log('Connection established to ' . $streamIP);
$this->lastErrorMsg = NULL;
$this->lastErrorNo = NULL;
// If we have a socket connection, we can attempt a HTTP request - Ensure blocking read for the moment
stream_set_blocking($this->conn, 1);
// Encode request data
$postData = http_build_query($requestParams);
// Oauth tokens
$oauthHeader = $this->getOAuthHeader('POST', $url, $requestParams);
// Do it
fwrite($this->conn, "POST " . $urlParts['path'] . " HTTP/1.0\r\n");
fwrite($this->conn, "Host: " . $urlParts['host'].':'.$port . "\r\n");
fwrite($this->conn, "Content-type: application/x-www-form-urlencoded\r\n");
fwrite($this->conn, "Content-length: " . strlen($postData) . "\r\n");
fwrite($this->conn, 'User-Agent: ' . self::USER_AGENT . "\r\n");
fwrite($this->conn, $oauthHeader."\r\n");
fwrite($this->conn, "\r\n");
fwrite($this->conn, $postData . "\r\n");
fwrite($this->conn, "\r\n");
$this->log("POST " . $urlParts['path'] . " HTTP/1.0");
$this->log("Host: " . $urlParts['host'].':'.$port);
$this->log("Content-type: application/x-www-form-urlencoded");
$this->log("Content-length: " . strlen($postData));
$this->log('User-Agent: ' . self::USER_AGENT);
$this->log($oauthHeader);
$this->log('');
$this->log($postData);
$this->log('');
// First line is response
list($httpVer, $httpCode, $httpMessage) = preg_split('/\s+/', trim(fgets($this->conn, 1024)), 3);
// Response buffers
$respHeaders = $respBody = '';
// Consume each header response line until we get to body
while ($hLine = trim(fgets($this->conn, 4096))) {
$respHeaders .= $hLine;
}
// If we got a non-200 response, we need to backoff and retry
if ($httpCode != 200) {
$connectFailures ++;
// Twitter will disconnect on error, but we want to consume the rest of the response body (which is useful)
while ($bLine = trim(fgets($this->conn, 4096))) {
$respBody .= $bLine;
}
// Construct error
$errStr = 'HTTP ERROR ' . $httpCode . ': ' . $httpMessage . ' (' . $respBody . ')';
// Set last error state
$this->lastErrorMsg = $errStr;
$this->lastErrorNo = $httpCode;
// Have we exceeded maximum failures?
if ($connectFailures > $this->connectFailuresMax) {
$msg = 'Connection failure limit exceeded with ' . $connectFailures . ' failures. Last error: ' . $errStr;
$this->log($msg,'error');
throw new PhirehoseConnectLimitExceeded($msg, $httpCode); // We eventually throw an exception for other code to handle
}
// Increase retry/backoff up to max
$httpRetry = ($httpRetry < $this->httpBackoffMax) ? $httpRetry * 2 : $this->httpBackoffMax;
$this->log('HTTP failure ' . $connectFailures . ' of ' . $this->connectFailuresMax . ' connecting to stream: ' .
$errStr . '. Sleeping for ' . $httpRetry . ' seconds.','info');
sleep($httpRetry);
continue;
} // End if not http 200
// Loop until connected OK
} while (!is_resource($this->conn) || $httpCode != 200);
// Connected OK, reset connect failures
$connectFailures = 0;
$this->lastErrorMsg = NULL;
$this->lastErrorNo = NULL;
// Switch to non-blocking to consume the stream (important)
stream_set_blocking($this->conn, 0);
// Connect always causes the filterChanged status to be cleared
$this->filterChanged = FALSE;
// Flush stream buffer & (re)assign fdrPool (for reconnect)
$this->fdrPool = array($this->conn);
$this->buff = '';
}
protected function prepareParameters($method = null, $url = null, $params = null) {
if(empty($method) || empty($url)) {
return false;
}
$oauth['oauth_consumer_key'] = TWITTER_CONSUMER_KEY;
$oauth['oauth_token'] = $this->username;
$oauth['oauth_nonce'] = md5(uniqid(rand(), true));
$oauth['oauth_timestamp'] = time();
$oauth['oauth_signature_method'] = 'HMAC-SHA1';
if(isset($params['oauth_verifier'])) {
$oauth['oauth_verifier'] = $params['oauth_verifier'];
unset($params['oauth_verifier']);
}
$oauth['oauth_version'] = '1.0';
// encode all oauth values
foreach($oauth as $k => $v)
$oauth[$k] = $this->encode_rfc3986($v);
// encode all non '@' params
// keep sigParams for signature generation (exclude '@' params)
// rename '@key' to 'key'
$sigParams = array();
$hasFile = false;
if (is_array($params)) {
foreach($params as $k => $v) {
if(strncmp('@',$k,1) !== 0) {
$sigParams[$k] = $this->encode_rfc3986($v);
$params[$k] = $this->encode_rfc3986($v);
} else {
$params[substr($k, 1)] = $v;
unset($params[$k]);
$hasFile = true;
}
}
if($hasFile === true) {
$sigParams = array();
}
}
$sigParams = array_merge($oauth, (array)$sigParams);
// sorting
ksort($sigParams);
// print_r($sigParams);
// signing
$oauth['oauth_signature'] = $this->encode_rfc3986($this->generateSignature($method, $url, $sigParams));
return array('request' => $params, 'oauth' => $oauth);
}
protected function encode_rfc3986($string) {
return str_replace('+', ' ', str_replace('%7E', '~', rawurlencode(($string))));
}
protected function generateSignature($method = null, $url = null, $params = null) {
if(empty($method) || empty($url)) {
return false;
}
// concatenating and encode
$concat = '';
foreach((array)$params as $key => $value) {
$concat .= "{$key}={$value}&";
}
$concat = substr($concat, 0, -1);
$concatenatedParams = $this->encode_rfc3986($concat);
// normalize url
$urlParts = parse_url($url);
$scheme = strtolower($urlParts['scheme']);
$host = strtolower($urlParts['host']);
$port = isset($urlParts['port']) ? intval($urlParts['port']) : 0;
$retval = strtolower($scheme) . '://' . strtolower($host);
if (!empty($port) && (($scheme === 'http' && $port != 80) || ($scheme === 'https' && $port != 443))) {
$retval .= ":{$port}";
}
$retval .= $urlParts['path'];
if (!empty($urlParts['query'])) {
$retval .= "?{$urlParts['query']}";
}
$normalizedUrl = $this->encode_rfc3986($retval);
$method = $this->encode_rfc3986($method); // don't need this but why not?
$signatureBaseString = "{$method}&{$normalizedUrl}&{$concatenatedParams}";
$this->log('DEBUG: ' . var_export($signatureBaseString, TRUE));
# sign the signature string
$key = $this->encode_rfc3986(TWITTER_CONSUMER_SECRET) . '&' . $this->encode_rfc3986($this->password);
return base64_encode(hash_hmac('sha1', $signatureBaseString, $key, true));
}
protected function getOAuthHeader($method, $url, $data) {
$params = $this->prepareParameters($method, $url, $data);
$oauthHeaders = $params['oauth'];
$urlParts = parse_url($url);
$oauth = 'Authorization: OAuth realm="' . $urlParts['scheme'] . '://' . $urlParts['host'] . $urlParts['path'] . '", ';
foreach($oauthHeaders as $name => $value) {
$oauth .= "{$name}=\"{$value}\", ";
}
$oauth = substr($oauth, 0, -2);
return $oauth;
}
}