PluginProbe
Photonic Gallery & Lightbox for Flickr, SmugMug & Others / 3.37
Photonic Gallery & Lightbox for Flickr, SmugMug & Others v3.37
3.37 3.36 3.35 3.34 3.33 2.19 2.20 2.21 2.22 2.23 2.24 2.25 2.26 2.27 2.28 2.29 2.30 2.31 2.32 2.33 2.34 2.40 2.41 2.42 2.43 All 142 releases
photonic / Platforms / OAuth1.php

OAuth1.php in Photonic Gallery & Lightbox for Flickr, SmugMug & Others 3.37, at Platforms/OAuth1.php

252 lines 7.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Photonic_Plugin\Platforms;
4
5 use Photonic_Plugin\Core\Photonic;
6 use WP_Error;
7
8 require_once 'Base.php';
9 require_once 'Authenticator.php';
10
11 abstract class OAuth1 extends Base {
12 use Authenticator;
13
14 public $base_url;
15
16 /**
17 * Access Token URL
18 *
19 * @abstract
20 * @return string
21 */
22 abstract public function access_token_URL(): string;
23
24 /**
25 * Authenticate URL
26 *
27 * @abstract
28 * @return string
29 */
30 abstract public function authenticate_URL(): string;
31
32 /**
33 * Authorize URL
34 *
35 * @return string
36 */
37 abstract public function authorize_URL(): string;
38
39 /**
40 * Request Token URL
41 *
42 * @return string
43 */
44 abstract public function request_token_URL(): string;
45
46 /**
47 * Method to validate that the stored token is indeed authenticated.
48 *
49 * @return array|WP_Error
50 */
51 abstract public function check_access_token();
52
53 /**
54 * Get the authorize URL
55 *
56 * @param $token
57 * @param bool $sign_in
58 * @return string
59 */
60 public function get_authorize_URL($token, $sign_in = true): string {
61 if (empty($sign_in)) {
62 return $this->authorize_URL() . "?oauth_token={$token['oauth_token']}";
63 }
64 else {
65 return $this->authenticate_URL() . "?oauth_token={$token['oauth_token']}";
66 }
67 }
68
69 /**
70 * Takes a token response from a request token call, then puts it in an appropriate array.
71 *
72 * @param $response
73 * @return array
74 */
75 public function parse_token($response): array {
76 $body = $response['body'];
77 $token = Base::parse_parameters($body);
78 return $token;
79 }
80
81 /**
82 * Generates the signature for the OAuth call.
83 * See here for the signature-generation methodology: https://www.wackylabs.net/2011/12/oauth-and-flickr-part-2/
84 *
85 * @param null $api_call
86 * @param null $api_args
87 * @param null $method
88 * @param array $oauth_token
89 * @return string
90 */
91 protected function generate_signature($api_call = null, $api_args = null, $method = null, $oauth_token = []): string {
92 $encoded_key = self::urlencode_rfc3986($this->api_secret) . '&';
93 if (isset($oauth_token['oauth_token_secret'])) {
94 $encoded_key .= self::urlencode_rfc3986($oauth_token['oauth_token_secret']);
95 }
96
97 $method = is_null($method) ? 'POST' : $method;
98 $params = [
99 'oauth_consumer_key' => $this->api_key,
100 'oauth_nonce' => $this->nonce,
101 'oauth_signature_method' => $this->oauth_signature_method(),
102 'oauth_timestamp' => $this->oauth_timestamp,
103 'oauth_version' => $this->oauth_version,
104 ];
105
106 if (isset($oauth_token['oauth_token'])) {
107 $params['oauth_token'] = $oauth_token['oauth_token'];
108 }
109
110 if (isset($oauth_token['oauth_verifier'])) {
111 $params['oauth_verifier'] = $oauth_token['oauth_verifier'];
112 }
113
114 $params = (!empty($api_args)) ? array_merge($params, $api_args) : $params;
115 if ('smug' === $this->provider && (false !== stripos($api_call, '?_expand=') || false !== stripos($api_call, '?_config='))) {
116 if (false !== stripos($api_call, '?_expand=')) {
117 $params['_expand'] = substr($api_call, stripos($api_call, '?_expand=') + 9);
118 }
119 if (false !== stripos($api_call, '?_config=')) {
120 $params['_config'] = substr($api_call, stripos($api_call, '?_config=') + 9);
121 }
122 }
123
124 $end_point = 'smug' === $this->provider ? remove_query_arg(['_expand', '_config'], $api_call) : $api_call;
125
126 ksort($params);
127 $string = self::build_query($params);
128 $base_string = $method . '&' . self::urlencode_rfc3986($end_point) . '&' . self::urlencode_rfc3986($string);
129 $sig = base64_encode(hash_hmac('sha1', $base_string, $encoded_key, true));
130
131 $this->signature_parameters = [
132 'parameters' => $params,
133 'base_string' => $base_string,
134 'key' => $encoded_key,
135 ];
136 return $sig;
137 }
138
139 /**
140 * Gets an OAuth request token using the API Key and API Secret provided in the plugin's back-end options.
141 * Once a token has been successfully got, the user is sent to an Authorization page where he can allow access for your site.
142 *
143 * @param null $oauth_callback
144 * @return array
145 */
146 public function get_request_token($oauth_callback): array {
147 $method = 'GET';
148
149 $signature = $this->generate_signature($this->request_token_URL(), ['oauth_callback' => $oauth_callback], $method);
150 $parameters = [
151 'oauth_version' => $this->oauth_version,
152 'oauth_nonce' => $this->nonce,
153 'oauth_timestamp' => $this->oauth_timestamp,
154 'oauth_callback' => $oauth_callback,
155 'oauth_consumer_key' => $this->api_key,
156 'oauth_signature_method' => $this->oauth_signature_method(),
157 'oauth_signature' => $signature,
158 ];
159
160 $end_point = $this->request_token_URL();
161 $end_point .= '?' . self::build_query($parameters);
162 $parameters = null;
163
164 $response = Photonic::http($end_point, $method, $parameters);
165 return $this->parse_token($response);
166 }
167
168 /**
169 * Takes an OAuth request token and exchanges it for an access token.
170 *
171 * @param $request_token
172 * @return array
173 */
174 public function get_access_token($request_token): array {
175 $method = 'GET';
176 $signature = $this->generate_signature($this->access_token_URL(), [], $method, $request_token);
177 $parameters = [
178 'oauth_consumer_key' => $this->api_key,
179 'oauth_nonce' => $this->nonce,
180 'oauth_signature' => $signature,
181 'oauth_signature_method' => $this->oauth_signature_method(),
182 'oauth_timestamp' => $this->oauth_timestamp,
183 'oauth_token' => $request_token['oauth_token'],
184 'oauth_version' => $this->oauth_version,
185 ];
186
187 if (isset($request_token['oauth_verifier'])) {
188 $parameters['oauth_verifier'] = $request_token['oauth_verifier'];
189 }
190
191 $end_point = $this->access_token_URL();
192 $end_point .= '?' . self::build_query($parameters);
193 $parameters = null;
194
195 $response = Photonic::http($end_point, $method, $parameters);
196 return $this->parse_token($response);
197 }
198
199 /**
200 * Takes the response for the "Check access token", then tries to determine whether the check was successful or not.
201 *
202 * @param $response
203 * @return bool
204 */
205 public function is_access_token_valid($response): bool {
206 if (is_wp_error($response)) {
207 return false;
208 }
209
210 $body = $response['body'];
211 $body = json_decode($body);
212
213 if (!isset($body->stat) || 'fail' === $body->stat) {
214 return false;
215 }
216 return true;
217 }
218
219 /**
220 * Checks if authentication has been enabled and the user has authenticated. If so, it signs the call, then adds the additional parameters to it.
221 * This method also attaches the oauth_signature to the parameters.
222 *
223 * @param $api_method
224 * @param $method
225 * @param $parameters
226 * @return mixed
227 */
228 public function sign_call($api_method, $method, $parameters) {
229 if (isset($this->token) && isset($this->token_secret)) {
230 $token = ['oauth_token' => $this->token, 'oauth_token_secret' => $this->token_secret];
231 }
232
233 if (isset($token)) {
234 $this->nonce = $this->nonce();
235 $this->oauth_timestamp = time();
236 $signature = $this->generate_signature($api_method, $parameters, $method, $token);
237 if (isset($this->signature_parameters) && isset($this->signature_parameters['parameters'])) {
238 $this->signature_parameters['parameters']['oauth_signature'] = $signature;
239 return $this->signature_parameters['parameters'];
240 }
241 }
242 return $parameters;
243 }
244
245 public function set_oauth_done() {
246 if (!empty($this->token) && !empty($this->token_secret)) {
247 $token_response = $this->check_access_token();
248 $this->oauth_done = $this->is_access_token_valid($token_response);
249 }
250 }
251 }
252