Skip to content
Newer
Older
100644 440 lines (379 sloc) 12.1 KB
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
1 <?php
2 class EpiOAuth
3 {
4 public $version = '1.0';
5
6 protected $requestTokenUrl;
7 protected $accessTokenUrl;
6392fde @jmathai Adding exceptions to EpiOAuth and implementing them in EpiTwitter.
authored
8 protected $authenticateUrl;
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
9 protected $authorizeUrl;
10 protected $consumerKey;
11 protected $consumerSecret;
12 protected $token;
13 protected $tokenSecret;
483fe6c Make handling custom callbacks easier.
Nick Pope authored
14 protected $callback;
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
15 protected $signatureMethod;
10e3e98 @jmathai Added debug option for calls which dump headers.
authored
16 protected $debug = false;
8cf70e2 @jmathai Merging in emmartin's changes and moving ssl logic into EpiOAuth class.
authored
17 protected $useSSL = false;
b000b29 @jmathai Adding follow location as a configurable setting that's false by defa…
authored
18 protected $followLocation = false;
21f91ce @jmathai Adding ability to add custom headers to curl calls.
authored
19 protected $headers = array();
20 protected $userAgent = 'EpiOAuth (http://github.com/jmathai/twitter-async/tree/)';
0cac48d @jmathai Adding implicit blocking in destructor so that all calls are ensured …
authored
21 protected $connectionTimeout = 5;
22 protected $requestTimeout = 30;
21f91ce @jmathai Adding ability to add custom headers to curl calls.
authored
23
24 public function addHeader($header)
25 {
26 if(is_array($header) && !empty($header))
27 $this->headers = array_merge($this->headers, $header);
28 elseif(!empty($header))
29 $this->headers[] = $header;
30 }
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
31
0b05e54 getAccessToken() now accepts $params to pass in the query. Now you ca…
Jon Canady authored
32 public function getAccessToken($params = null)
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
33 {
5d4d8c1 Add oauth_verifer when getting access token.
Nick Pope authored
34 if (isset($_GET['oauth_verifier']) && !isset($params['oauth_verifier']))
35 {
36 $params['oauth_verifier'] = $_GET['oauth_verifier'];
37 }
9856082 @squio Slightly more strict operation: use POST for OAuth handshakes, add oa…
squio authored
38 $resp = $this->httpRequest('POST', $this->getUrl($this->accessTokenUrl), $params);
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
39 return new EpiOAuthResponse($resp);
40 }
41
e4ae371 @jmathai Added support for the force_login parameter for /oauth/authenticate.
authored
42 public function getAuthenticateUrl($token = null, $params = null)
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
43 {
385a782 @jmathai Added proper support for the oauth_callback parameter.
authored
44 $token = $token ? $token : $this->getRequestToken($params);
8923d0d @dingram Bugfixes for EpiOAuth
dingram authored
45 if (is_object($token)) $token = $token->oauth_token;
1912d06 @jmathai Patching http_build_query for PHP < 5.1.2
authored
46 $addlParams = empty($params) ? '' : '&'.http_build_query($params, '', '&');
8923d0d @dingram Bugfixes for EpiOAuth
dingram authored
47 return $this->getUrl($this->authenticateUrl) . '?oauth_token=' . $token . $addlParams;
6392fde @jmathai Adding exceptions to EpiOAuth and implementing them in EpiTwitter.
authored
48 }
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
49
385a782 @jmathai Added proper support for the oauth_callback parameter.
authored
50 public function getAuthorizeUrl($token = null, $params = null)
e4ae371 @jmathai Added support for the force_login parameter for /oauth/authenticate.
authored
51 {
385a782 @jmathai Added proper support for the oauth_callback parameter.
authored
52 $token = $token ? $token : $this->getRequestToken($params);
bfc14b7 @dingram Making EpiOAuth E_STRICT-safe
dingram authored
53 if (is_object($token)) $token = $token->oauth_token;
54 return $this->getUrl($this->authorizeUrl) . '?oauth_token=' . $token;
e4ae371 @jmathai Added support for the force_login parameter for /oauth/authenticate.
authored
55 }
56
57 // DEPRECATED in favor of getAuthorizeUrl()
5188b40 @ericmmartin Added token param to get*Url methods.
ericmmartin authored
58 public function getAuthorizationUrl($token = null)
6392fde @jmathai Adding exceptions to EpiOAuth and implementing them in EpiTwitter.
authored
59 {
385a782 @jmathai Added proper support for the oauth_callback parameter.
authored
60 return $this->getAuthorizeUrl($token);
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
61 }
62
21f91ce @jmathai Adding ability to add custom headers to curl calls.
authored
63 public function getRequestToken($params = null)
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
64 {
483fe6c Make handling custom callbacks easier.
Nick Pope authored
65 if (isset($this->callback) && !isset($params['oauth_callback']))
66 {
67 $params['oauth_callback'] = $this->callback;
68 }
9856082 @squio Slightly more strict operation: use POST for OAuth handshakes, add oa…
squio authored
69 $resp = $this->httpRequest('POST', $this->getUrl($this->requestTokenUrl), $params);
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
70 return new EpiOAuthResponse($resp);
71 }
72
8cf70e2 @jmathai Merging in emmartin's changes and moving ssl logic into EpiOAuth class.
authored
73 public function getUrl($url)
74 {
75 if($this->useSSL === true)
ab5079e @ericmmartin Undefined errors described in issue #9
ericmmartin authored
76 return preg_replace('/^http:/', 'https:', $url);
8cf70e2 @jmathai Merging in emmartin's changes and moving ssl logic into EpiOAuth class.
authored
77
78 return $url;
79 }
80
5fc33a5 @jmathai Adding support for multipart image uploads. It's 1/2 working.
authored
81 public function httpRequest($method = null, $url = null, $params = null, $isMultipart = false)
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
82 {
83 if(empty($method) || empty($url))
84 return false;
85
86 if(empty($params['oauth_signature']))
87 $params = $this->prepareParameters($method, $url, $params);
88
89 switch($method)
90 {
d1636aa @jmathai Fixed a bug with the signature not always being properly encoded.
authored
91 case 'GET':
92 return $this->httpGet($url, $params);
93 break;
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
94 case 'POST':
5fc33a5 @jmathai Adding support for multipart image uploads. It's 1/2 working.
authored
95 return $this->httpPost($url, $params, $isMultipart);
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
96 break;
d5c5286 @jmathai Fixed bugs with DELETE endpoint from last commit.
authored
97 case 'DELETE':
98 return $this->httpDelete($url, $params);
99 break;
100
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
101 }
102 }
103
10e3e98 @jmathai Added debug option for calls which dump headers.
authored
104 public function setDebug($bool=false)
105 {
106 $this->debug = (bool)$bool;
107 }
108
b000b29 @jmathai Adding follow location as a configurable setting that's false by defa…
authored
109 public function setFollowLocation($bool)
110 {
111 $this->followLocation = (bool)$bool;
112 }
113
0cac48d @jmathai Adding implicit blocking in destructor so that all calls are ensured …
authored
114 public function setTimeout($requestTimeout = null, $connectionTimeout = null)
21f91ce @jmathai Adding ability to add custom headers to curl calls.
authored
115 {
0cac48d @jmathai Adding implicit blocking in destructor so that all calls are ensured …
authored
116 if($requestTimeout !== null)
117 $this->requestTimeout = floatval($requestTimeout);
118 if($connectionTimeout !== null)
119 $this->connectionTimeout = floatval($connectionTimeout);
21f91ce @jmathai Adding ability to add custom headers to curl calls.
authored
120 }
121
042c806 @jmathai Fixed bug relating to invalid encoding for OAuth signatures on POST c…
authored
122 public function setToken($token = null, $secret = null)
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
123 {
124 $this->token = $token;
125 $this->tokenSecret = $secret;
b000b29 @jmathai Adding follow location as a configurable setting that's false by defa…
authored
126 }
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
127
483fe6c Make handling custom callbacks easier.
Nick Pope authored
128 public function setCallback($callback = null)
129 {
130 $this->callback = $callback;
131 }
132
8cf70e2 @jmathai Merging in emmartin's changes and moving ssl logic into EpiOAuth class.
authored
133 public function useSSL($use = false)
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
134 {
8cf70e2 @jmathai Merging in emmartin's changes and moving ssl logic into EpiOAuth class.
authored
135 $this->useSSL = (bool)$use;
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
136 }
137
21f91ce @jmathai Adding ability to add custom headers to curl calls.
authored
138 protected function addDefaultHeaders($url, $oauthHeaders)
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
139 {
042c806 @jmathai Fixed bug relating to invalid encoding for OAuth signatures on POST c…
authored
140 $_h = array('Expect:');
13fc3de @jmathai Fixed bug where the OAuth realm was incorrect for GET requests with p…
authored
141 $urlParts = parse_url($url);
8cf70e2 @jmathai Merging in emmartin's changes and moving ssl logic into EpiOAuth class.
authored
142 $oauth = 'Authorization: OAuth realm="' . $urlParts['scheme'] . '://' . $urlParts['host'] . $urlParts['path'] . '",';
042c806 @jmathai Fixed bug relating to invalid encoding for OAuth signatures on POST c…
authored
143 foreach($oauthHeaders as $name => $value)
144 {
145 $oauth .= "{$name}=\"{$value}\",";
146 }
147 $_h[] = substr($oauth, 0, -1);
21f91ce @jmathai Adding ability to add custom headers to curl calls.
authored
148 $_h[] = "User-Agent: {$this->userAgent}";
149 $this->addHeader($_h);
042c806 @jmathai Fixed bug relating to invalid encoding for OAuth signatures on POST c…
authored
150 }
151
ccb2ab5 @jmathai Multipart calls working for udpating profile image.
authored
152 protected function buildHttpQueryRaw($params)
153 {
154 $retval = '';
155 foreach((array)$params as $key => $value)
156 $retval .= "{$key}={$value}&";
157 $retval = substr($retval, 0, -1);
158 return $retval;
159 }
160
8cf70e2 @jmathai Merging in emmartin's changes and moving ssl logic into EpiOAuth class.
authored
161 protected function curlInit($url)
162 {
163 $ch = curl_init($url);
164 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
21f91ce @jmathai Adding ability to add custom headers to curl calls.
authored
165 curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
0cac48d @jmathai Adding implicit blocking in destructor so that all calls are ensured …
authored
166 curl_setopt($ch, CURLOPT_TIMEOUT, $this->requestTimeout);
167 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectionTimeout);
38aa1cf @jmathai Adding CURLOPT_ENCODING.
authored
168 curl_setopt($ch, CURLOPT_ENCODING, '');
b000b29 @jmathai Adding follow location as a configurable setting that's false by defa…
authored
169 if($this->followLocation)
170 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
3d27e5a @jmathai Not using curlopt_interface for empty or localhost.
authored
171 if(isset($_SERVER ['SERVER_ADDR']) && !empty($_SERVER['SERVER_ADDR']) && $_SERVER['SERVER_ADDR'] != '127.0.0.1')
976c5a5 @jmathai Added curlopt interface for shared hosting users.
authored
172 curl_setopt($ch, CURLOPT_INTERFACE, $_SERVER ['SERVER_ADDR']);
edb47e8 @jmathai Making certificate bundle optional.
authored
173
174 // if the certificate exists then use it, else bypass ssl checks
175 if(file_exists($cert = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'ca-bundle.crt'))
176 {
177 curl_setopt($ch, CURLOPT_CAINFO, $cert);
178 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
179 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
180 }
181 else
182 {
183 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
184 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
185 }
8cf70e2 @jmathai Merging in emmartin's changes and moving ssl logic into EpiOAuth class.
authored
186 return $ch;
187 }
188
889fee8 @jmathai Restting headers after each call.
authored
189 protected function emptyHeaders()
190 {
191 $this->headers = array();
192 }
193
8cf70e2 @jmathai Merging in emmartin's changes and moving ssl logic into EpiOAuth class.
authored
194 protected function encode_rfc3986($string)
195 {
196 return str_replace('+', ' ', str_replace('%7E', '~', rawurlencode(($string))));
197 }
198
042c806 @jmathai Fixed bug relating to invalid encoding for OAuth signatures on POST c…
authored
199 protected function generateNonce()
200 {
201 if(isset($this->nonce)) // for unit testing
202 return $this->nonce;
203
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
204 return md5(uniqid(rand(), true));
205 }
206
ccb2ab5 @jmathai Multipart calls working for udpating profile image.
authored
207 // parameters should already have been passed through prepareParameters
208 // no need to double encode
042c806 @jmathai Fixed bug relating to invalid encoding for OAuth signatures on POST c…
authored
209 protected function generateSignature($method = null, $url = null, $params = null)
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
210 {
211 if(empty($method) || empty($url))
212 return false;
213
ccb2ab5 @jmathai Multipart calls working for udpating profile image.
authored
214 // concatenating and encode
215 $concatenatedParams = $this->encode_rfc3986($this->buildHttpQueryRaw($params));
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
216
217 // normalize url
868a15d @jmathai Fixed encode function to support rfc3986.
authored
218 $normalizedUrl = $this->encode_rfc3986($this->normalizeUrl($url));
219 $method = $this->encode_rfc3986($method); // don't need this but why not?
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
220
221 $signatureBaseString = "{$method}&{$normalizedUrl}&{$concatenatedParams}";
222 return $this->signString($signatureBaseString);
223 }
224
c365e61 @jmathai When in synchronous mode use curl instead of multi curl.
authored
225 protected function executeCurl($ch)
226 {
227 if($this->isAsynchronous)
228 return $this->curl->addCurl($ch);
229 else
230 return $this->curl->addEasyCurl($ch);
231 }
232
d5c5286 @jmathai Fixed bugs with DELETE endpoint from last commit.
authored
233 protected function httpDelete($url, $params) {
234 $this->addDefaultHeaders($url, $params['oauth']);
235 $ch = $this->curlInit($url);
236 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
237 curl_setopt($ch, CURLOPT_POSTFIELDS, $this->buildHttpQueryRaw($params['request']));
c365e61 @jmathai When in synchronous mode use curl instead of multi curl.
authored
238 $resp = $this->executeCurl($ch);
d5c5286 @jmathai Fixed bugs with DELETE endpoint from last commit.
authored
239 $this->emptyHeaders();
240 return $resp;
241 }
242
042c806 @jmathai Fixed bug relating to invalid encoding for OAuth signatures on POST c…
authored
243 protected function httpGet($url, $params = null)
d1636aa @jmathai Fixed a bug with the signature not always being properly encoded.
authored
244 {
042c806 @jmathai Fixed bug relating to invalid encoding for OAuth signatures on POST c…
authored
245 if(count($params['request']) > 0)
d1636aa @jmathai Fixed a bug with the signature not always being properly encoded.
authored
246 {
247 $url .= '?';
042c806 @jmathai Fixed bug relating to invalid encoding for OAuth signatures on POST c…
authored
248 foreach($params['request'] as $k => $v)
d1636aa @jmathai Fixed a bug with the signature not always being properly encoded.
authored
249 {
250 $url .= "{$k}={$v}&";
251 }
252 $url = substr($url, 0, -1);
253 }
21f91ce @jmathai Adding ability to add custom headers to curl calls.
authored
254 $this->addDefaultHeaders($url, $params['oauth']);
8cf70e2 @jmathai Merging in emmartin's changes and moving ssl logic into EpiOAuth class.
authored
255 $ch = $this->curlInit($url);
c365e61 @jmathai When in synchronous mode use curl instead of multi curl.
authored
256 $resp = $this->executeCurl($ch);
889fee8 @jmathai Restting headers after each call.
authored
257 $this->emptyHeaders();
d1636aa @jmathai Fixed a bug with the signature not always being properly encoded.
authored
258
259 return $resp;
260 }
261
5fc33a5 @jmathai Adding support for multipart image uploads. It's 1/2 working.
authored
262 protected function httpPost($url, $params = null, $isMultipart)
d1636aa @jmathai Fixed a bug with the signature not always being properly encoded.
authored
263 {
21f91ce @jmathai Adding ability to add custom headers to curl calls.
authored
264 $this->addDefaultHeaders($url, $params['oauth']);
8cf70e2 @jmathai Merging in emmartin's changes and moving ssl logic into EpiOAuth class.
authored
265 $ch = $this->curlInit($url);
042c806 @jmathai Fixed bug relating to invalid encoding for OAuth signatures on POST c…
authored
266 curl_setopt($ch, CURLOPT_POST, 1);
5fc33a5 @jmathai Adding support for multipart image uploads. It's 1/2 working.
authored
267 // php's curl extension automatically sets the content type
268 // based on whether the params are in string or array form
269 if($isMultipart)
270 curl_setopt($ch, CURLOPT_POSTFIELDS, $params['request']);
271 else
ccb2ab5 @jmathai Multipart calls working for udpating profile image.
authored
272 curl_setopt($ch, CURLOPT_POSTFIELDS, $this->buildHttpQueryRaw($params['request']));
c365e61 @jmathai When in synchronous mode use curl instead of multi curl.
authored
273 $resp = $this->executeCurl($ch);
889fee8 @jmathai Restting headers after each call.
authored
274 $this->emptyHeaders();
275
d1636aa @jmathai Fixed a bug with the signature not always being properly encoded.
authored
276 return $resp;
277 }
278
042c806 @jmathai Fixed bug relating to invalid encoding for OAuth signatures on POST c…
authored
279 protected function normalizeUrl($url = null)
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
280 {
281 $urlParts = parse_url($url);
282 $scheme = strtolower($urlParts['scheme']);
283 $host = strtolower($urlParts['host']);
ab5079e @ericmmartin Undefined errors described in issue #9
ericmmartin authored
284 $port = isset($urlParts['port']) ? intval($urlParts['port']) : 0;
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
285
8cf70e2 @jmathai Merging in emmartin's changes and moving ssl logic into EpiOAuth class.
authored
286 $retval = strtolower($scheme) . '://' . strtolower($host);
287
288 if(!empty($port) && (($scheme === 'http' && $port != 80) || ($scheme === 'https' && $port != 443)))
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
289 $retval .= ":{$port}";
8cf70e2 @jmathai Merging in emmartin's changes and moving ssl logic into EpiOAuth class.
authored
290
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
291 $retval .= $urlParts['path'];
292 if(!empty($urlParts['query']))
293 {
294 $retval .= "?{$urlParts['query']}";
295 }
296
297 return $retval;
298 }
299
ab1bc8b @jmathai Fixing a bug where tweets starting with an @ sign using basic auth do…
authored
300 protected function isMultipart($params = null)
301 {
302 if($params)
303 {
304 foreach($params as $k => $v)
305 {
306 if(strncmp('@',$k,1) === 0)
307 return true;
308 }
309 }
310 return false;
311 }
312
042c806 @jmathai Fixed bug relating to invalid encoding for OAuth signatures on POST c…
authored
313 protected function prepareParameters($method = null, $url = null, $params = null)
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
314 {
315 if(empty($method) || empty($url))
316 return false;
317
042c806 @jmathai Fixed bug relating to invalid encoding for OAuth signatures on POST c…
authored
318 $oauth['oauth_consumer_key'] = $this->consumerKey;
319 $oauth['oauth_token'] = $this->token;
320 $oauth['oauth_nonce'] = $this->generateNonce();
321 $oauth['oauth_timestamp'] = !isset($this->timestamp) ? time() : $this->timestamp; // for unit test
322 $oauth['oauth_signature_method'] = $this->signatureMethod;
9856082 @squio Slightly more strict operation: use POST for OAuth handshakes, add oa…
squio authored
323 if(isset($params['oauth_verifier']))
324 {
325 $oauth['oauth_verifier'] = $params['oauth_verifier'];
326 unset($params['oauth_verifier']);
327 }
042c806 @jmathai Fixed bug relating to invalid encoding for OAuth signatures on POST c…
authored
328 $oauth['oauth_version'] = $this->version;
5fc33a5 @jmathai Adding support for multipart image uploads. It's 1/2 working.
authored
329 // encode all oauth values
ccb2ab5 @jmathai Multipart calls working for udpating profile image.
authored
330 foreach($oauth as $k => $v)
331 $oauth[$k] = $this->encode_rfc3986($v);
332 // encode all non '@' params
333 // keep sigParams for signature generation (exclude '@' params)
334 // rename '@key' to 'key'
335 $sigParams = array();
19d52c4 @jmathai Adding some test code to resolve invalid signature message when passi…
authored
336 $hasFile = false;
042c806 @jmathai Fixed bug relating to invalid encoding for OAuth signatures on POST c…
authored
337 if(is_array($params))
5fc33a5 @jmathai Adding support for multipart image uploads. It's 1/2 working.
authored
338 {
339 foreach($params as $k => $v)
340 {
341 if(strncmp('@',$k,1) !== 0)
342 {
ccb2ab5 @jmathai Multipart calls working for udpating profile image.
authored
343 $sigParams[$k] = $this->encode_rfc3986($v);
344 $params[$k] = $this->encode_rfc3986($v);
5fc33a5 @jmathai Adding support for multipart image uploads. It's 1/2 working.
authored
345 }
346 else
347 {
348 $params[substr($k, 1)] = $v;
349 unset($params[$k]);
19d52c4 @jmathai Adding some test code to resolve invalid signature message when passi…
authored
350 $hasFile = true;
5fc33a5 @jmathai Adding support for multipart image uploads. It's 1/2 working.
authored
351 }
352 }
19d52c4 @jmathai Adding some test code to resolve invalid signature message when passi…
authored
353
354 if($hasFile === true)
355 $sigParams = array();
5fc33a5 @jmathai Adding support for multipart image uploads. It's 1/2 working.
authored
356 }
357
358 $sigParams = array_merge($oauth, (array)$sigParams);
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
359
360 // sorting
5fc33a5 @jmathai Adding support for multipart image uploads. It's 1/2 working.
authored
361 ksort($sigParams);
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
362
363 // signing
5fc33a5 @jmathai Adding support for multipart image uploads. It's 1/2 working.
authored
364 $oauth['oauth_signature'] = $this->encode_rfc3986($this->generateSignature($method, $url, $sigParams));
042c806 @jmathai Fixed bug relating to invalid encoding for OAuth signatures on POST c…
authored
365 return array('request' => $params, 'oauth' => $oauth);
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
366 }
367
042c806 @jmathai Fixed bug relating to invalid encoding for OAuth signatures on POST c…
authored
368 protected function signString($string = null)
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
369 {
370 $retval = false;
371 switch($this->signatureMethod)
372 {
373 case 'HMAC-SHA1':
868a15d @jmathai Fixed encode function to support rfc3986.
authored
374 $key = $this->encode_rfc3986($this->consumerSecret) . '&' . $this->encode_rfc3986($this->tokenSecret);
042c806 @jmathai Fixed bug relating to invalid encoding for OAuth signatures on POST c…
authored
375 $retval = base64_encode(hash_hmac('sha1', $string, $key, true));
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
376 break;
377 }
378
379 return $retval;
380 }
381
042c806 @jmathai Fixed bug relating to invalid encoding for OAuth signatures on POST c…
authored
382 public function __construct($consumerKey, $consumerSecret, $signatureMethod='HMAC-SHA1')
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
383 {
042c806 @jmathai Fixed bug relating to invalid encoding for OAuth signatures on POST c…
authored
384 $this->consumerKey = $consumerKey;
385 $this->consumerSecret = $consumerSecret;
386 $this->signatureMethod = $signatureMethod;
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
387 $this->curl = EpiCurl::getInstance();
388 }
389 }
390
391 class EpiOAuthResponse
392 {
d1636aa @jmathai Fixed a bug with the signature not always being properly encoded.
authored
393 private $__resp;
8923d0d @dingram Bugfixes for EpiOAuth
dingram authored
394 protected $debug = false;
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
395
396 public function __construct($resp)
397 {
d1636aa @jmathai Fixed a bug with the signature not always being properly encoded.
authored
398 $this->__resp = $resp;
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
399 }
400
401 public function __get($name)
402 {
6392fde @jmathai Adding exceptions to EpiOAuth and implementing them in EpiTwitter.
authored
403 if($this->__resp->code != 200)
10e3e98 @jmathai Added debug option for calls which dump headers.
authored
404 EpiOAuthException::raise($this->__resp, $this->debug);
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
405
d1636aa @jmathai Fixed a bug with the signature not always being properly encoded.
authored
406 parse_str($this->__resp->data, $result);
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
407 foreach($result as $k => $v)
408 {
409 $this->$k = $v;
410 }
411
bfc14b7 @dingram Making EpiOAuth E_STRICT-safe
dingram authored
412 return isset($result[$name]) ? $result[$name] : null;
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
413 }
5459a58 @jmathai Adding a hack and unit tests to enable support for lists.
authored
414
415 public function __toString()
416 {
417 return $this->__resp->data;
418 }
556f8ea @jmathai Adding EpiOAuth and EpiTwitter classes.
authored
419 }
6392fde @jmathai Adding exceptions to EpiOAuth and implementing them in EpiTwitter.
authored
420
421 class EpiOAuthException extends Exception
422 {
10e3e98 @jmathai Added debug option for calls which dump headers.
authored
423 public static function raise($response, $debug)
6392fde @jmathai Adding exceptions to EpiOAuth and implementing them in EpiTwitter.
authored
424 {
24e9781 @jmathai Merge branch '2.0'
authored
425 $message = $response->responseText;
10e3e98 @jmathai Added debug option for calls which dump headers.
authored
426
427 switch($response->code)
6392fde @jmathai Adding exceptions to EpiOAuth and implementing them in EpiTwitter.
authored
428 {
429 case 400:
8923d0d @dingram Bugfixes for EpiOAuth
dingram authored
430 throw new EpiOAuthBadRequestException($message, $response->code);
6392fde @jmathai Adding exceptions to EpiOAuth and implementing them in EpiTwitter.
authored
431 case 401:
8923d0d @dingram Bugfixes for EpiOAuth
dingram authored
432 throw new EpiOAuthUnauthorizedException($message, $response->code);
6392fde @jmathai Adding exceptions to EpiOAuth and implementing them in EpiTwitter.
authored
433 default:
8923d0d @dingram Bugfixes for EpiOAuth
dingram authored
434 throw new EpiOAuthException($message, $response->code);
6392fde @jmathai Adding exceptions to EpiOAuth and implementing them in EpiTwitter.
authored
435 }
436 }
437 }
438 class EpiOAuthBadRequestException extends EpiOAuthException{}
439 class EpiOAuthUnauthorizedException extends EpiOAuthException{}
Something went wrong with that request. Please try again.