Overview

Namespaces

  • TwitterOAuth
    • Auth
    • Common
    • Exception
    • Serializer

Classes

  • ApplicationOnlyAuth
  • AuthAbstract
  • SingleUserAuth
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * TwitterOAuth - https://github.com/ricardoper/TwitterOAuth
  5:  * PHP library to communicate with Twitter OAuth API version 1.1
  6:  *
  7:  * @author Ricardo Pereira <github@ricardopereira.es>
  8:  * @copyright 2014
  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 $headers = null;
 36: 
 37: 
 38:     /**
 39:      * Authentication Base
 40:      *
 41:      * @param array $credentials  Credentials Array
 42:      * @param SerializerInterface $serializer  Output Serializer
 43:      * @throws MissingCredentialsException
 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:      * Gets the Twitter API key
 60:      *
 61:      * @return null|string
 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:      * Gets the Twitter API secret
 74:      *
 75:      * @return null|string
 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:      * Gets Serializer
 88:      *
 89:      * @return null|SerializerInterface
 90:      */
 91:     public function getSerializer()
 92:     {
 93:         if (empty($this->serializer)) {
 94:             return null;
 95:         }
 96: 
 97:         return $this->serializer;
 98:     }
 99: 
100:     /**
101:      * Get response headers
102:      *
103:      * @param null $key
104:      * @return array|string|false
105:      */
106:     public function getHeaders($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:      * Send a GET call to Twitter API via OAuth
121:      *
122:      * @param string $call Twitter resource string
123:      * @param array $getParams GET parameters to send
124:      * @return mixed  Output with selected format
125:      * @throws TwitterException
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:      * Validate Credentials Array
153:      *
154:      * @param $credentials
155:      * @throws MissingCredentialsException
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:      * Getting full URL from a Twitter resource
174:      *
175:      * @return string  Full URL
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:      * Returns raw response body
195:      *
196:      * @return array
197:      * @throws \TwitterOAuth\Exception\CurlException
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:      * Processing Twitter Exceptions in case of error
214:      *
215:      * @param array $response Raw response
216:      * @throws TwitterException
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:      * Build a multipart message
249:      *
250:      * @param string $mimeBoundary MIME boundary ID
251:      * @param string $filename File location
252:      * @return string  Multipart message
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:      * Twitter supported MIME types for media upload
275:      *
276:      * @param string $mime File extension
277:      * @return mixed  MIME type
278:      * @throws UnsupportedMimeException
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:      * Get binary data of a file
299:      *
300:      * @param string $filename File location
301:      * @return string
302:      * @throws FileNotFoundException
303:      * @throws FileNotReadableException
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:      * Reset Call State
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: }
TwitterOAuth (for v1.1 API) API documentation generated by ApiGen