1<?php
2
3namespace OAuth\OAuth1\Service;
4
5use OAuth\OAuth1\Signature\SignatureInterface;
6use OAuth\OAuth1\Token\StdOAuth1Token;
7use OAuth\Common\Http\Exception\TokenResponseException;
8use OAuth\Common\Http\Uri\Uri;
9use OAuth\Common\Consumer\CredentialsInterface;
10use OAuth\Common\Http\Uri\UriInterface;
11use OAuth\Common\Storage\TokenStorageInterface;
12use OAuth\Common\Http\Client\ClientInterface;
13
14class Flickr extends AbstractService
15{
16    protected $format;
17
18    public function __construct(
19        CredentialsInterface $credentials,
20        ClientInterface $httpClient,
21        TokenStorageInterface $storage,
22        SignatureInterface $signature,
23        UriInterface $baseApiUri = null
24    ) {
25        parent::__construct($credentials, $httpClient, $storage, $signature, $baseApiUri);
26        if ($baseApiUri === null) {
27            $this->baseApiUri = new Uri('https://api.flickr.com/services/rest/');
28        }
29    }
30
31    public function getRequestTokenEndpoint()
32    {
33        return new Uri('https://www.flickr.com/services/oauth/request_token');
34    }
35
36    public function getAuthorizationEndpoint()
37    {
38        return new Uri('https://www.flickr.com/services/oauth/authorize');
39    }
40
41    public function getAccessTokenEndpoint()
42    {
43        return new Uri('https://www.flickr.com/services/oauth/access_token');
44    }
45
46    protected function parseRequestTokenResponse($responseBody)
47    {
48        parse_str($responseBody, $data);
49        if (null === $data || !is_array($data)) {
50            throw new TokenResponseException('Unable to parse response.');
51        } elseif (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] != 'true') {
52            throw new TokenResponseException('Error in retrieving token.');
53        }
54        return $this->parseAccessTokenResponse($responseBody);
55    }
56
57    protected function parseAccessTokenResponse($responseBody)
58    {
59        parse_str($responseBody, $data);
60        if ($data === null || !is_array($data)) {
61            throw new TokenResponseException('Unable to parse response.');
62        } elseif (isset($data['error'])) {
63            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
64        }
65
66        $token = new StdOAuth1Token();
67        $token->setRequestToken($data['oauth_token']);
68        $token->setRequestTokenSecret($data['oauth_token_secret']);
69        $token->setAccessToken($data['oauth_token']);
70        $token->setAccessTokenSecret($data['oauth_token_secret']);
71        $token->setEndOfLife(StdOAuth1Token::EOL_NEVER_EXPIRES);
72        unset($data['oauth_token'], $data['oauth_token_secret']);
73        $token->setExtraParams($data);
74
75        return $token;
76    }
77
78    public function request($path, $method = 'GET', $body = null, array $extraHeaders = array())
79    {
80        $uri = $this->determineRequestUriFromPath('/', $this->baseApiUri);
81        $uri->addToQuery('method', $path);
82
83        if (!empty($this->format)) {
84            $uri->addToQuery('format', $this->format);
85
86            if ($this->format === 'json') {
87                $uri->addToQuery('nojsoncallback', 1);
88            }
89        }
90
91        $token = $this->storage->retrieveAccessToken($this->service());
92        $extraHeaders = array_merge($this->getExtraApiHeaders(), $extraHeaders);
93        $authorizationHeader = array(
94            'Authorization' => $this->buildAuthorizationHeaderForAPIRequest($method, $uri, $token, $body)
95        );
96        $headers = array_merge($authorizationHeader, $extraHeaders);
97
98        return $this->httpClient->retrieveResponse($uri, $body, $headers, $method);
99    }
100
101    public function requestRest($path, $method = 'GET', $body = null, array $extraHeaders = array())
102    {
103        return $this->request($path, $method, $body, $extraHeaders);
104    }
105
106    public function requestXmlrpc($path, $method = 'GET', $body = null, array $extraHeaders = array())
107    {
108        $this->format = 'xmlrpc';
109
110        return $this->request($path, $method, $body, $extraHeaders);
111    }
112
113    public function requestSoap($path, $method = 'GET', $body = null, array $extraHeaders = array())
114    {
115        $this->format = 'soap';
116
117        return $this->request($path, $method, $body, $extraHeaders);
118    }
119
120    public function requestJson($path, $method = 'GET', $body = null, array $extraHeaders = array())
121    {
122        $this->format = 'json';
123
124        return $this->request($path, $method, $body, $extraHeaders);
125    }
126
127    public function requestPhp($path, $method = 'GET', $body = null, array $extraHeaders = array())
128    {
129        $this->format = 'php_serial';
130
131        return $this->request($path, $method, $body, $extraHeaders);
132    }
133}
134