1: <?php
2:
3: 4: 5: 6: 7: 8: 9:
10:
11: namespace TwitterOAuth\Auth;
12:
13: class SingleUserAuth extends AuthAbstract
14: {
15: 16: 17: 18: 19: 20: 21:
22: protected $requiredCredentials = array(
23: 'consumer_key',
24: 'consumer_secret',
25: );
26:
27: protected $urls = array(
28: 'domain' => 'https://api.twitter.com/',
29: 'upload' => 'https://upload.twitter.com/',
30: 'api' => '1.1/',
31: );
32:
33:
34: 35: 36: 37: 38:
39: public function getAccessToken()
40: {
41: if (empty($this->credentials['oauth_token'])) {
42: return null;
43: }
44:
45: return $this->credentials['oauth_token'];
46: }
47:
48: 49: 50: 51: 52:
53: public function getAccessTokenSecret()
54: {
55: if (empty($this->credentials['oauth_token_secret'])) {
56: return null;
57: }
58:
59: return $this->credentials['oauth_token_secret'];
60: }
61:
62: 63: 64: 65: 66: 67: 68: 69: 70:
71: public function post($call, array $postParams = null, array $getParams = null)
72: {
73: $this->resetCallState();
74:
75: $this->call = $call;
76:
77: $this->method = 'POST';
78:
79: if ($postParams !== null && is_array($postParams)) {
80: $this->postParams = $postParams;
81: }
82:
83: if ($getParams !== null && is_array($getParams)) {
84: $this->getParams = $getParams;
85: }
86:
87: $response = $this->getResponse();
88:
89: $this->findExceptions($response);
90:
91: $this->headers = $response['headers'];
92:
93: unset($call, $postParams, $getParams);
94:
95: return $this->serializer->format($response['body']);
96: }
97:
98: 99: 100: 101: 102: 103: 104: 105: 106:
107: public function postMedia($call, $filename)
108: {
109: $this->resetCallState();
110:
111: $this->call = $call;
112:
113: $this->method = 'POST';
114:
115: $this->withMedia = true;
116:
117: $mimeBoundary = sha1($call . microtime());
118:
119: $params = array(
120: 'post' => $this->buildMultipart($mimeBoundary, $filename),
121: 'headers' => $this->buildUploadMediaHeader($mimeBoundary),
122: );
123:
124: $response = $this->curl->send($this->getUrl(), $params);
125:
126: $obj = json_decode($response['body']);
127:
128: if (!$obj || !isset($obj->token_type) || $obj->token_type != 'bearer') {
129: $this->findExceptions($response);
130: }
131:
132: $this->headers = $response['headers'];
133:
134: $this->withMedia = null;
135:
136: unset($call, $filename, $mimeBoundary, $params, $obj);
137:
138: return $this->serializer->format($response['body']);
139: }
140:
141:
142: 143: 144: 145: 146:
147: protected function getOauthParameters()
148: {
149: $time = time();
150:
151: return array(
152: 'oauth_consumer_key' => $this->getConsumerKey(),
153: 'oauth_nonce' => trim(base64_encode($time), '='),
154: 'oauth_signature_method' => 'HMAC-SHA1',
155: 'oauth_timestamp' => $time,
156: 'oauth_token' => $this->getAccessToken(),
157: 'oauth_version' => '1.0'
158: );
159: }
160:
161: 162: 163: 164: 165:
166: protected function getRequestString()
167: {
168: $params = array_merge($this->getParams, $this->postParams, $this->getOauthParameters());
169:
170: $params = $this->curl->getParams($params);
171:
172: return rawurlencode($params);
173: }
174:
175: 176: 177: 178: 179:
180: protected function getSignatureBaseString()
181: {
182: $method = strtoupper($this->method);
183:
184: $url = rawurlencode($this->getUrl());
185:
186: return $method . '&' . $url . '&' . $this->getRequestString();
187: }
188:
189: 190: 191: 192: 193:
194: protected function getSigningKey()
195: {
196: return $this->getConsumerSecret() . '&' . $this->getAccessTokenSecret();
197: }
198:
199: 200: 201: 202: 203:
204: protected function calculateSignature()
205: {
206: return base64_encode(hash_hmac('sha1', $this->getSignatureBaseString(), $this->getSigningKey(), true));
207: }
208:
209: 210: 211: 212: 213:
214: protected function getOauthString()
215: {
216: $oauth = array_merge($this->getOauthParameters(), array('oauth_signature' => $this->calculateSignature()));
217:
218: ksort($oauth);
219:
220: $values = array();
221:
222: foreach ($oauth as $key => $value) {
223: $values[] = $key . '="' . rawurlencode($value) . '"';
224: }
225:
226: $oauth = implode(', ', $values);
227:
228: unset($values, $key, $value);
229:
230: return $oauth;
231: }
232:
233: 234: 235: 236: 237:
238: protected function ()
239: {
240: return array(
241: 'Authorization: OAuth ' . $this->getOauthString(),
242: 'Expect:'
243: );
244: }
245:
246: 247: 248: 249: 250: 251:
252: protected function ($mimeBoundary)
253: {
254: return array(
255: 'Authorization: OAuth ' . $this->getOauthString(),
256: 'Content-Type: multipart/form-data; boundary=' . $mimeBoundary,
257: 'Expect:'
258: );
259: }
260: }