PluginProbe
Photonic Gallery & Lightbox for Flickr, SmugMug & Others / 2.40
Photonic Gallery & Lightbox for Flickr, SmugMug & Others v2.40
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 2.44 All 141 releases
photonic / Admin / Authentication.php

Authentication.php in Photonic Gallery & Lightbox for Flickr, SmugMug & Others 2.40, at Admin/Authentication.php

416 lines 18.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Photonic_Plugin\Admin;
3
4 use Photonic_Plugin\Core\Photonic;
5 use Photonic_Plugin\Modules\Authenticator;
6 use Photonic_Plugin\Modules\Core;
7 use Photonic_Plugin\Modules\Flickr;
8 use Photonic_Plugin\Modules\Google_Photos;
9 use Photonic_Plugin\Modules\Instagram;
10 use Photonic_Plugin\Modules\SmugMug;
11 use Photonic_Plugin\Modules\Zenfolio;
12
13 require_once('Admin_Page.php');
14
15 class Authentication extends Admin_Page {
16 private static $instance;
17
18 private function __construct() {
19 require_once(PHOTONIC_PATH."/Modules/Instagram.php");
20 require_once(PHOTONIC_PATH."/Modules/Zenfolio.php");
21 }
22
23 static function get_instance() {
24 if (self::$instance == null) {
25 self::$instance = new Authentication();
26 }
27 return self::$instance;
28 }
29
30 function render_content() {
31 ?>
32 <form method="post" id="photonic-auth-form">
33 <h2 id="#photonic-flickr-auth-section" class="photonic-section">Flickr</h2>
34 <?php $this->display_flickr(); ?>
35
36 <h2 id="#photonic-smugmug-auth-section" class="photonic-section">SmugMug</h2>
37 <?php $this->display_smugmug();?>
38
39 <h2 id="#photonic-google-auth-section" class="photonic-section">Google Photos</h2>
40 <?php $this->display_google(); ?>
41
42 <!-- <h3 id="#photonic-google-auth-section-bundled">Google Photos</h3>
43 --><?php /*$this->display_google_bundled_token_getter(); */?>
44
45 <h2 id="#photonic-instagram-auth-section" class="photonic-section">Instagram</h2>
46 <?php $this->display_instagram(); ?>
47
48 <h2 id="#photonic-zenfolio-auth-section" class="photonic-section">Zenfolio</h2>
49 <?php $this->display_zenfolio(); ?>
50 </form>
51 <?php
52 }
53
54 function display_flickr() {
55 global $photonic_flickr_api_key, $photonic_flickr_api_secret, $photonic_flickr_access_token;
56 $auth = [
57 'api_key' => trim($photonic_flickr_api_key),
58 'api_secret' => trim($photonic_flickr_api_secret),
59 'token' => trim($photonic_flickr_access_token),
60 ];
61 $this->show_token_section($auth, 'flickr', 'Flickr');
62 }
63
64 function display_smugmug() {
65 global $photonic_smug_api_key, $photonic_smug_api_secret, $photonic_smug_access_token;
66 $auth = [
67 'api_key' => !empty($photonic_smug_api_key) ? trim($photonic_smug_api_key) : '86MZ8N8TqJf5x2fQ4FRWXRtJ3C6Jm7XV',
68 'api_secret' => trim($photonic_smug_api_secret),
69 'token' => trim($photonic_smug_access_token),
70 ];
71 $this->show_token_section($auth, 'smug', 'SmugMug');
72 }
73
74 function display_google() {
75 global $photonic_google_client_id, $photonic_google_client_secret, $photonic_google_refresh_token;
76 echo "<div class=\"photonic-token-header\">\n";
77 if (empty($photonic_google_client_id) || empty($photonic_google_client_secret)) {
78 echo sprintf(esc_html__('Please set up your Google Client ID and Client Secret under %s', 'photonic'),
79 '<em>Photonic &rarr; Settings &rarr; Google Photos &rarr; Google Photos Settings</em>');
80 }
81 else {
82 $parameters = Core::parse_parameters($_SERVER['QUERY_STRING']);
83
84 if (!empty($photonic_google_refresh_token)) {
85 $this->print_auth_done_all_good();
86 }
87
88 echo "</div>\n";
89 echo "<div class=\"photonic-token-header\">\n";
90 esc_html_e("You first have to authorize Photonic to connect to your Google account.", 'photonic');
91 echo "<br/>\n";
92 if (!isset($parameters['code']) || !isset($parameters['source']) || $parameters['source'] != 'google') {
93
94 $url = add_query_arg('test', 'test');
95 $url = remove_query_arg('test', $url);
96 $parameters = [
97 'response_type' => 'code',
98 'redirect_uri' => admin_url('admin.php?page=photonic-auth&source=google'),
99 'client_id' => $photonic_google_client_id,
100 'scope' => 'https://www.googleapis.com/auth/photoslibrary.readonly',
101 'access_type' => 'offline',
102 'state' => md5($photonic_google_client_secret.'google').'::'.urlencode($url),
103 'prompt' => 'consent',
104 ];
105 $url = 'https://accounts.google.com/o/oauth2/auth?'.Authenticator::build_query($parameters);
106
107 echo "<a href='".$url."' class='button button-primary'>".esc_html__('Step 1: Authenticate', 'photonic')."</a>";
108 // echo "<a href='".$module->get_authorization_url(['redirect_uri' => admin_url('admin.php?page=photonic-auth&source=google'), 'prompt' => 'consent'])."' class='button button-primary'>".
109 // esc_html__('Step 1: Authenticate', 'photonic')."</a>";
110 echo "</div>\n";
111 echo "<div class=\"photonic-token-header\">\n";
112 echo esc_html__("Next, you have to obtain the token.", 'photonic').'<br/>';
113 echo "<span class='button photonic-helper-button-disabled'>".
114 esc_html__('Step 2: Obtain Token', 'photonic')."</span>";
115 }
116 else {
117 echo "<span class='button photonic-helper-button-disabled'>".
118 esc_html__('Step 1: Authenticate', 'photonic')."</span>";
119 echo "</div>\n";
120 echo "<div class=\"photonic-token-header\">\n";
121 echo esc_html__("Next, you have to obtain the token.", 'photonic').'<br/>';
122 echo "<a href='#' class='button button-primary photonic-google-refresh'>".
123 esc_html__('Step 2: Obtain Token', 'photonic')."</a>";
124 echo '<input type="hidden" value="'.$parameters['code'].'" id="photonic-google-oauth-code"/>';
125 echo '<input type="hidden" value="'.$parameters['state'].'" id="photonic-google-oauth-state"/>';
126 }
127 }
128 echo "</div>\n";
129 echo '<div class="result" id="google-result">&nbsp;</div>';
130 echo sprintf(esc_html__('If you are facing issues with the authentication please follow the workaround %1$shere%2$s', 'photonic'),
131 '<a href="https://aquoid.com/plugins/photonic/google-photos/#auth-workaround" target="_blank">',
132 '</a>');
133 }
134
135 function display_google_bundled() {
136 global $photonic_google_refresh_token;
137 echo "<div class=\"photonic-token-header\">\n";
138
139 require_once(PHOTONIC_PATH."/Modules/Google_Photos.php");
140 $module = Google_Photos::get_instance();
141 $parameters = Core::parse_parameters($_SERVER['QUERY_STRING']);
142
143 if (!empty($photonic_google_refresh_token) && $module->refresh_token_valid) {
144 $this->print_auth_done_all_good();
145 }
146
147 echo "</div>\n";
148 echo "<div class=\"photonic-token-header\">\n";
149 esc_html_e("You first have to authorize Photonic to connect to your Google account.", 'photonic');
150 echo "<br/>\n";
151 if (!isset($parameters['code']) || !isset($parameters['source']) || $parameters['source'] != 'google') {
152 echo "<a href='".$module->get_authorization_url([
153 'redirect_uri' => 'https://aquoid.com/photonic-router/google.php',
154 'state' => admin_url('admin.php?page=photonic-auth&source=google'),
155 'prompt' => 'consent',
156 ])."' class='button button-primary'>".
157 esc_html__('Step 1: Authenticate', 'photonic')."</a>";
158 echo "</div>\n";
159 echo "<div class=\"photonic-token-header\">\n";
160 echo esc_html__("Next, you have to obtain the token.", 'photonic').'<br/>';
161 echo "<span class='button photonic-helper-button-disabled'>".
162 esc_html__('Step 2: Obtain Token', 'photonic')."</span>";
163 }
164 else {
165 echo "<span class='button photonic-helper-button-disabled'>".
166 esc_html__('Step 1: Authenticate', 'photonic')."</span>";
167 echo "</div>\n";
168 echo "<div class=\"photonic-token-header\">\n";
169 echo esc_html__("Next, you have to obtain the token.", 'photonic').'<br/>';
170 echo "<a href='#' class='button button-primary photonic-google-refresh'>".
171 esc_html__('Step 2: Obtain Token', 'photonic')."</a>";
172 echo '<input type="hidden" value="'.$parameters['code'].'" id="photonic-google-oauth-code"/>';
173 }
174 echo "</div>\n";
175 echo '<div class="result" id="google-result">&nbsp;</div>';
176
177 echo "<div class=\"photonic-token-header\">\n";
178 echo esc_html__("If the above method is not working due to conflicts with your site's security setup try an alternative method where the authentication happens on a different site.", 'photonic').'<br/>';
179 echo "<a href='".$module->get_authorization_url([
180 'redirect_uri' => 'https://aquoid.com/photonic-router/google.php',
181 'prompt' => 'consent',
182 'state' => '',
183 ])."' class='button button-primary' target='_blank'>".
184 esc_html__("Authenticate using Photonic's website", 'photonic')."</a>";
185 echo "</div>\n";
186 }
187
188 function display_instagram() {
189 global $photonic_instagram_access_token;
190 $auth = [
191 'api_key' => 'not-required-but-not-empty',
192 'api_secret' => 'not-required-but-not-empty',
193 'token' => trim($photonic_instagram_access_token),
194 ];
195
196 $this->show_token_section_header($auth, 'Instagram');
197 $response = Core::parse_parameters($_SERVER['QUERY_STRING']);
198 if (empty($response['access_token'])) {
199 echo "<a href='https://api.instagram.com/oauth/authorize/?client_id=1089620424711320&scope=user_profile,user_media&redirect_uri=https://aquoid.com/photonic-router/instagram/&state=" .
200 admin_url('admin.php?page=photonic-auth') . "&response_type=code' class='button button-primary'>" .
201 $this->get_login_button('Instagram'). "</a>";
202 }
203 else if (!empty($response['access_token'])) {
204 echo "<span class='button photonic-helper-button-disabled'>" . $this->get_login_button('Instagram') . "</span>";
205 echo '<div class="result">'.
206 (!empty($response['access_token']) ? 'Access token: <code id="instagram-token">'.$response['access_token'].'</code><br/>' : '&nbsp;').
207 (!empty($response['expires_in']) ? '<input type="hidden" id="instagram-token-expires-in" value="'.$response['expires_in'].'" />' : '').
208 (!empty($response['user_id']) ? '<input type="hidden" id="instagram-token-client-id" value="'.$response['user_id'].'" />' : '').
209 (!empty($response['user']) ? 'User name: <code id="instagram-token-user">'.$response['user'].'</code><br/>' : '').
210 '</div>';
211
212 echo "<a href='#' class='button button-primary photonic-save-token' data-photonic-provider='instagram'>" . esc_html__('Save Token', 'photonic') . "</a>";
213 }
214 }
215
216 function display_zenfolio() {
217 global $photonic_zenfolio_default_user;
218 $gallery = Zenfolio::get_instance();
219
220 echo "<div class=\"photonic-token-header\">\n";
221 if (empty($photonic_zenfolio_default_user)) {
222 echo sprintf(esc_html__('Please set up the default user for Zenfolio under %s', 'photonic'),
223 '<em>Photonic &rarr; Settings &rarr; Zenfolio &rarr; Zenfolio Photo Settings &rarr; Default User</em>')."\n";
224 }
225 else if (!empty($gallery->token)) {
226 $this->print_auth_done_all_good();
227 }
228 echo "</div>\n";
229
230 $response = Core::parse_parameters($_SERVER['QUERY_STRING']);
231 if (!empty($photonic_zenfolio_default_user) && (empty($response['provider']) || 'zenfolio' !== $response['provider'])) {
232 echo "<label>".esc_html__('Password:', 'photonic')."<input type='password' name='zenfolio-password' id='zenfolio-password'></label>";
233 }
234
235 echo "<div style='display: block; width: 100%;'>\n";
236 if (!empty($photonic_zenfolio_default_user) && (empty($response['provider']) || 'zenfolio' !== $response['provider'])) {
237 echo "<a href='#' class='button button-primary' data-photonic-provider='zenfolio' style='margin-right: 1em;'>" . esc_html__('Login and Authenticate', 'photonic') . "</a>";
238 }
239
240 if (!empty($gallery->token)) {
241 echo "<a href='#' class='button button-primary photonic-zenfolio-delete'>".esc_html__('Delete current Zenfolio authentication data', 'photonic')."</a>";
242 }
243 echo "</div>\n";
244
245 echo '<div class="result" id="zenfolio-result">&nbsp;</div>';
246 }
247
248 private function show_token_section($auth, $provider_slug, $provider_text) {
249 $this->show_token_section_header($auth, $provider_text);
250 if (!empty($auth['api_key']) && !empty($auth['api_secret'])) {
251 $this->show_token_section_body($provider_slug, $provider_text);
252 }
253 else {
254 echo '<div class="result" id="'.$provider_slug.'-result">&nbsp;</div>';
255 }
256 }
257
258 /**
259 * @param $auth array
260 * @param $provider string
261 */
262 private function show_token_section_header($auth, $provider) {
263 echo "<div class=\"photonic-token-header\">\n";
264
265 if (empty($auth['api_key']) || empty($auth['api_secret'])) {
266 echo sprintf(esc_html__('Please set up your %1$s API key under %2$s.', 'photonic'), $provider, sprintf('<em>Photonic &rarr; Settings &rarr; %1$s &rarr; %1$s Settings</em>', $provider));
267 }
268 else if ($provider == 'Instagram' && !empty($auth['token'])) {
269 require_once(PHOTONIC_PATH."/Modules/Instagram.php");
270 $module = Instagram::get_instance();
271 $expiring_soon = $module->is_token_expiring_soon(30);
272 if (is_null($expiring_soon)) {
273 // Not yet authenticated with the new API.
274 echo '<p class="notice notice-error">'.esc_html__('Your authentication credentials are for the old Instagram API. Please reauthenticate to keep Photonic working.', 'photonic').'</p><br/>';
275 }
276 else if ($expiring_soon === 1) {
277 echo '<p class="notice notice-warning">'.esc_html__('Your authentication credentials are expiring soon. Please reauthenticate to keep Photonic working.', 'photonic').'</p><br/>';
278 }
279 else if ($expiring_soon === -1) {
280 echo '<p class="notice notice-error">'.esc_html__('Your authentication credentials have expired! Please reauthenticate to keep Photonic working.', 'photonic').'</p><br/>';
281 }
282 else {
283 $cached_token = $module->get_cached_token();
284
285 if (!empty($cached_token) && !empty($cached_token['user'])) {
286 $this->print_auth_done_all_good(sprintf(esc_html__('You are logged in as %1$s.', 'photonic'), '<code>'.$cached_token['user'].'</code>'));
287 }
288 else {
289 $this->print_auth_done_all_good();
290 }
291 }
292 }
293 else if (!empty($auth['token'])) {
294 $this->print_auth_done_all_good();
295 }
296 echo "</div>\n";
297 }
298
299 /**
300 * @param $provider
301 * @param $provider_text
302 */
303 public function show_token_section_body($provider, $provider_text) {
304 $photonic_authentication = get_option('photonic_authentication');
305 $response = Core::parse_parameters($_SERVER['QUERY_STRING']);
306
307 if (empty($response['provider']) || (!empty($response['provider']) && $provider !== $response['provider'])) {
308 echo "<a href='#' class='button button-primary photonic-token-request' data-photonic-provider='$provider'>" . $this->get_login_button($provider_text) . "</a>";
309 }
310 else if (!empty($response['oauth_token']) && !empty($response['oauth_verifier'])) {
311 if (in_array($provider, ['flickr', 'smug', 'smugmug'])) {
312 if ($provider == 'flickr') {
313 require_once(PHOTONIC_PATH."/Modules/Flickr.php");
314 $module = Flickr::get_instance();
315 }
316 else {
317 require_once(PHOTONIC_PATH."/Modules/SmugMug.php");
318 $module = SmugMug::get_instance();
319 }
320 echo "<span class='button photonic-helper-button-disabled'>" . $this->get_login_button($provider_text) . "</span>";
321 $authorization = ['oauth_token' => $response['oauth_token'], 'oauth_verifier' => $response['oauth_verifier']];
322 if (isset($photonic_authentication) && isset($photonic_authentication[$provider]) && isset($photonic_authentication[$provider]['oauth_token_secret'])) {
323 $authorization['oauth_token_secret'] = $photonic_authentication[$provider]['oauth_token_secret'];
324 }
325 $access_token = $module->get_access_token($authorization);
326 if (isset($access_token['oauth_token'])) {
327 echo '<div class="result">Access Token: <code id="'.$provider.'-token">' . $access_token['oauth_token'] . '</code><br/>Access Token Secret: <code id="'.$provider.'-token-secret">' . $access_token['oauth_token_secret'] . '</code></div>'."\n";
328 echo "<a href='#' class='button button-primary photonic-save-token' data-photonic-provider='$provider'>" . esc_html__('Save Token', 'photonic') . "</a>";
329 }
330 }
331 }
332 }
333
334 private function print_auth_done_all_good($additional_msg = null) {
335 echo '<p class="notice notice-success">';
336 esc_html_e('You have already set up your authentication. Unless you wish to regenerate the token this step is not required. ', 'photonic');
337 if (!empty($additional_msg)) {
338 echo $additional_msg;
339 }
340 echo '</p>';
341 }
342
343 private function get_login_button($provider) {
344 return sprintf(esc_html__('Login and get Access Token from %s', 'photonic'), $provider);
345 }
346
347 function obtain_token() {
348 $provider = sanitize_text_field($_POST['provider']);
349 if ($provider == 'google') {
350 $code = esc_attr($_POST['code']);
351 require_once(PHOTONIC_PATH."/Modules/Google_Photos.php");
352 $module = Google_Photos::get_instance();
353 global $photonic_google_use_own_keys, $photonic_google_client_id, $photonic_google_client_secret;
354 // if (!empty($photonic_google_use_own_keys) || (!empty($photonic_google_client_id) && !empty($photonic_google_client_secret))) {
355 $response = Photonic::http($module->access_token_URL(), 'POST', [
356 'code' => $code,
357 'grant_type' => 'authorization_code',
358 'client_id' => $module->client_id,
359 'client_secret' => $module->client_secret,
360 'redirect_uri' => admin_url('admin.php?page=photonic-auth&source=google'),
361 ]);
362 /* }
363 else {
364 $response = Photonic::http($module->access_token_URL(), 'POST', [
365 'code' => $code,
366 'grant_type' => 'authorization_code',
367 'client_id' => $module->client_id,
368 'client_secret' => $module->client_secret,
369 'redirect_uri' => 'https://aquoid.com/photonic-router/google.php',
370 'state' => admin_url('admin.php?page=photonic-auth&source=google'),
371 ]);
372 }*/
373
374 if (!is_wp_error($response) && is_array($response)) {
375 echo($response['body']);
376 }
377 else {
378 }
379 }
380 else if ($provider == 'flickr') {
381 require_once(PHOTONIC_PATH."/Modules/Flickr.php");
382 $module = Flickr::get_instance();
383 if (empty($_POST['oauth_token']) && empty($_POST['oauth_verifier'])) {
384 $request_token = $module->get_request_token(admin_url('admin.php?page=photonic-auth&provider=flickr'));
385 $authorize_url = $module->get_authorize_URL($request_token);
386 $authorize_url .= '&perms=read';
387 $module->save_token($request_token);
388 echo $authorize_url;
389 }
390 }
391 else if ($provider == 'smug') {
392 require_once(PHOTONIC_PATH."/Modules/SmugMug.php");
393 $module = SmugMug::get_instance();
394 if (empty($_POST['oauth_token']) && empty($_POST['oauth_verifier'])) {
395 $request_token = $module->get_request_token(admin_url('admin.php?page=photonic-auth&provider=smug'));
396 $authorize_url = $module->get_authorize_URL($request_token);
397 $authorize_url .= '&Access=Full&Permissions=Read';
398 $module->save_token($request_token);
399 echo $authorize_url;
400 }
401 }
402 else if ($provider == 'zenfolio') {
403 $module = Zenfolio::get_instance();
404 if (!empty($_POST['password'])) {
405 $response = $module->authenticate($_POST['password']);
406 if (!empty($response['error'])) {
407 echo $response['error'];
408 }
409 else if (!empty($response['success'])) {
410 esc_html_e('Authentication successful! All your galleries will be displayed with Authentication in place.', 'photonic');
411 }
412 }
413 }
414 }
415 }
416