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