PluginProbe
Photonic Gallery & Lightbox for Flickr, SmugMug & Others / 2.44
Photonic Gallery & Lightbox for Flickr, SmugMug & Others v2.44
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.44, at Admin/Authentication.php

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