1: <?php
2:
3: 4: 5: 6: 7: 8: 9:
10:
11: namespace TwitterOAuth\Auth;
12:
13: use TwitterOAuth\Common\Curl;
14: use TwitterOAuth\Serializer\SerializerInterface;
15: use TwitterOAuth\Exception\TwitterException;
16: use TwitterOAuth\Exception\FileNotFoundException;
17: use TwitterOAuth\Exception\FileNotReadableException;
18: use TwitterOAuth\Exception\UnsupportedMimeException;
19: use TwitterOAuth\Exception\MissingCredentialsException;
20:
21: abstract class AuthAbstract
22: {
23: const EOL = "\r\n";
24:
25: protected $credentials = array();
26: protected $serializer = null;
27: protected $curl = null;
28:
29: protected $call = null;
30: protected $method = null;
31: protected $withMedia = null;
32: protected $getParams = array();
33: protected $postParams = array();
34:
35: protected = null;
36:
37:
38: 39: 40: 41: 42: 43: 44:
45: public function __construct(array $credentials, SerializerInterface $serializer)
46: {
47: $this->validateCredentials($credentials);
48:
49: $this->credentials = $credentials;
50:
51: $this->serializer = $serializer;
52:
53: $this->curl = new Curl();
54:
55: unset($credentials, $serializer);
56: }
57:
58: 59: 60: 61: 62:
63: public function getConsumerKey()
64: {
65: if (empty($this->credentials['consumer_key'])) {
66: return null;
67: }
68:
69: return $this->credentials['consumer_key'];
70: }
71:
72: 73: 74: 75: 76:
77: public function getConsumerSecret()
78: {
79: if (empty($this->credentials['consumer_secret'])) {
80: return null;
81: }
82:
83: return $this->credentials['consumer_secret'];
84: }
85:
86: 87: 88: 89: 90:
91: public function getSerializer()
92: {
93: if (empty($this->serializer)) {
94: return null;
95: }
96:
97: return $this->serializer;
98: }
99:
100: 101: 102: 103: 104: 105:
106: public function ($key = null)
107: {
108: if ($key === null) {
109: return $this->headers;
110: }
111:
112: if (isset($this->headers[$key])) {
113: return $this->headers[$key];
114: }
115:
116: return false;
117: }
118:
119: 120: 121: 122: 123: 124: 125: 126:
127: public function get($call, array $getParams = null)
128: {
129: $this->resetCallState();
130:
131: $this->call = $call;
132:
133: $this->method = 'GET';
134:
135: if ($getParams !== null && is_array($getParams)) {
136: $this->getParams = $getParams;
137: }
138:
139: $response = $this->getResponse();
140:
141: $this->findExceptions($response);
142:
143: $this->headers = $response['headers'];
144:
145: unset($call, $getParams);
146:
147: return $this->serializer->format($response['body']);
148: }
149:
150:
151: 152: 153: 154: 155: 156:
157: protected function validateCredentials($credentials)
158: {
159: $credentials = array_filter($credentials);
160:
161: $keys = array_keys($credentials);
162:
163: $diff = array_diff($this->requiredCredentials, $keys);
164:
165: if (!empty($diff)) {
166: throw new MissingCredentialsException('Missing Credentials: ' . implode($diff, ', '));
167: }
168:
169: unset($credentials, $keys, $diff);
170: }
171:
172: 173: 174: 175: 176:
177: protected function getUrl()
178: {
179: $domain = $this->urls['domain'];
180:
181: $apiVersion = $this->urls['api'];
182:
183: $jsonExt = '.json';
184:
185:
186: if (isset($this->withMedia) && $this->withMedia === true) {
187: $domain = $this->urls['upload'];
188: }
189:
190: return $domain . $apiVersion . $this->call . $jsonExt;
191: }
192:
193: 194: 195: 196: 197: 198:
199: protected function getResponse()
200: {
201: $url = $this->getUrl();
202:
203: $params = array(
204: 'get' => $this->getParams,
205: 'post' => $this->postParams,
206: 'headers' => $this->buildRequestHeader(),
207: );
208:
209: return $this->curl->send($url, $params);
210: }
211:
212: 213: 214: 215: 216: 217:
218: protected function findExceptions($response)
219: {
220: $response = $response['body'];
221:
222: $data = json_decode($response, true);
223:
224: if (isset($response[0]) && $response[0] !== '{' && $response[0] !== '[' && !$data) {
225: throw new TwitterException($response, 0);
226: }
227:
228: if (!empty($data['errors']) || !empty($data['error'])) {
229: if (!empty($data['errors'])) {
230: $data = current($data['errors']);
231: }
232:
233: if (empty($data['message']) && !empty($data['error'])) {
234: $data['message'] = $data['error'];
235: }
236:
237: if (!isset($data['code']) || empty($data['code'])) {
238: $data['code'] = 0;
239: }
240:
241: throw new TwitterException($data['message'], $data['code']);
242: }
243:
244: unset($response, $data);
245: }
246:
247: 248: 249: 250: 251: 252: 253:
254: protected function buildMultipart($mimeBoundary, $filename)
255: {
256: $binary = $this->getBinaryFile($filename);
257:
258: $details = pathinfo($filename);
259:
260: $type = $this->supportedMimes($details['extension']);
261:
262: $data = '--' . $mimeBoundary . static::EOL;
263: $data .= 'Content-Disposition: form-data; name="media"; filename="' . $details['basename'] . '"' . static::EOL;
264: $data .= 'Content-Type: ' . $type . static::EOL . static::EOL;
265: $data .= $binary . static::EOL;
266: $data .= '--' . $mimeBoundary . '--' . static::EOL . static::EOL;
267:
268: unset($mimeBoundary, $filename, $binary, $details, $type);
269:
270: return $data;
271: }
272:
273: 274: 275: 276: 277: 278: 279:
280: protected function supportedMimes($mime)
281: {
282: $mimes = array(
283: 'png' => 'image/png',
284: 'jpe' => 'image/jpeg',
285: 'jpeg' => 'image/jpeg',
286: 'jpg' => 'image/jpeg',
287: 'gif' => 'image/gif',
288: );
289:
290: if (isset($mimes[$mime])) {
291: return $mimes[$mime];
292: }
293:
294: throw new UnsupportedMimeException;
295: }
296:
297: 298: 299: 300: 301: 302: 303: 304:
305: protected function getBinaryFile($filename)
306: {
307: if (!file_exists($filename)) {
308: throw new FileNotFoundException;
309: }
310:
311: if (!is_readable($filename)) {
312: throw new FileNotReadableException;
313: }
314:
315: ob_start();
316:
317: readfile($filename);
318:
319: $binary = ob_get_contents();
320:
321: ob_end_clean();
322:
323: unset($filename);
324:
325: return $binary;
326: }
327:
328: 329: 330:
331: protected function resetCallState()
332: {
333: $this->call = null;
334: $this->method = null;
335: $this->withMedia = null;
336: $this->getParams = array();
337: $this->postParams = array();
338: $this->headers = null;
339: }
340: }