-
Notifications
You must be signed in to change notification settings - Fork 33
/
OauthPhirehose.php
157 lines (130 loc) · 4.31 KB
/
OauthPhirehose.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
<?php
abstract class OauthPhirehose extends Phirehose
{
protected $auth_method;
/**
* The Twitter consumer key. Get it from the application's page on Twitter.
* If not set then the global define TWITTER_CONSUMER_KEY is used instead.
*/
public $consumerKey=null;
/**
* The Twitter consumer secret. Get it from the application's page on Twitter.
* If not set then the global define TWITTER_CONSUMER_SECRET is used instead.
*/
public $consumerSecret=null;
/**
*/
protected function prepareParameters($method = null, $url = null,
array $params)
{
if (empty($method) || empty($url))
return false;
$oauth['oauth_consumer_key'] = $this->consumerKey?$this->consumerKey:TWITTER_CONSUMER_KEY;
$oauth['oauth_nonce'] = md5(uniqid(rand(), true));
$oauth['oauth_signature_method'] = 'HMAC-SHA1';
$oauth['oauth_timestamp'] = time();
$oauth['oauth_version'] = '1.0';
$oauth['oauth_token'] = $this->username;
if (isset($params['oauth_verifier']))
{
$oauth['oauth_verifier'] = $params['oauth_verifier'];
unset($params['oauth_verifier']);
}
// 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);
// 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}";
# sign the signature string
$key = $this->encode_rfc3986($this->consumerSecret?$this->consumerSecret:TWITTER_CONSUMER_SECRET) . '&' . $this->encode_rfc3986($this->password);
return base64_encode(hash_hmac('sha1', $signatureBaseString, $key, true));
}
protected function getOAuthHeader($method, $url, $params = array())
{
$params = $this->prepareParameters($method, $url, $params);
$oauthHeaders = $params['oauth'];
$urlParts = parse_url($url);
$oauth = 'OAuth realm="",';
foreach ($oauthHeaders as $name => $value)
{
$oauth .= "{$name}=\"{$value}\",";
}
$oauth = substr($oauth, 0, -1);
return $oauth;
}
protected function getAuthorizationHeader()
{
$url = self::URL_BASE . $this->method . '.' . $this->format;
$urlParts = parse_url($url);
// Setup params appropriately
$requestParams = array('delimited' => 'length');
// Filter takes additional parameters
if (count($this->trackWords) > 0)
{
$requestParams['track'] = implode(',', $this->trackWords);
}
if (count($this->followIds) > 0)
{
$requestParams['follow'] = implode(',', $this->followIds);
}
return $this->getOAuthHeader('POST', $url, $requestParams);
}
}