PluginProbe
Photonic Gallery & Lightbox for Flickr, SmugMug & Others / 3.36
Photonic Gallery & Lightbox for Flickr, SmugMug & Others v3.36
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 / Platforms / OAuth2.php

OAuth2.php in Photonic Gallery & Lightbox for Flickr, SmugMug & Others 3.36, at Platforms/OAuth2.php

183 lines 5.5 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
7 require_once 'Base.php';
8 require_once 'Authenticator.php';
9
10 abstract class OAuth2 extends Base {
11 use Authenticator;
12
13 public $scope;
14 public $response_type;
15 public $client_id;
16 public $client_secret;
17 public $state;
18 public $access_token;
19 public $auth_error;
20 public $soon_limit;
21
22 protected function __construct() {
23 $this->soon_limit = 30;
24 parent::__construct();
25 }
26
27 abstract public function authentication_URL();
28
29 abstract public function access_token_URL();
30
31 abstract public function renew_token($token): array;
32
33 abstract protected function set_token_validity($validity);
34
35 public function redirect_url() {
36 return get_site_url();
37 }
38
39 /**
40 * Takes an OAuth request token and exchanges it for an access token.
41 *
42 * @param $request_token
43 */
44 public function get_access_token($request_token) {
45 $code = $request_token['code'];
46 $state_args = explode('::', $request_token['state']);
47
48 if (md5($this->client_secret . $this->provider) === $state_args[0]) {
49 $url = urldecode($state_args[1]);
50 $response = Photonic::http(
51 $this->access_token_URL(),
52 'POST',
53 [
54 'code' => $code,
55 'grant_type' => 'authorization_code',
56 'client_id' => $this->client_id,
57 'client_secret' => $this->client_secret,
58 'redirect_uri' => $this->redirect_url(),
59 ]
60 );
61
62 if (is_wp_error($response)) {
63 $url = add_query_arg('error', $response->get_error_code(), $url);
64 }
65 elseif (empty($response)) {
66 $url = add_query_arg('error', 'null', $url);
67 }
68 }
69 else {
70 $url = remove_query_arg(['token', 'code', 'state']);
71 }
72 wp_safe_redirect($url);
73 exit();
74 }
75
76 /**
77 * @param $base_token
78 */
79 public function authenticate($base_token) {
80 $photonic_authentication = get_option('photonic_authentication');
81 if (!isset($photonic_authentication)) {
82 $photonic_authentication = [];
83 }
84
85 $transient_token = get_transient('photonic_' . $this->provider . '_token');
86 if (empty($transient_token) && !isset($photonic_authentication[$this->provider]) && !empty($base_token)) {
87 // Nothing is in the authentication option, but there is a token in overall Photonic Options.
88 // Refresh it if required, and save it as a transient via renew_token.
89 list($token, $error) = $this->renew_token($base_token);
90 }
91 elseif ($transient_token || isset($photonic_authentication[$this->provider])) {
92 $token = empty($transient_token) ? $photonic_authentication[$this->provider] : $transient_token;
93 if (!empty($token)) {
94 if (empty($transient_token) && !($this->is_token_expired($token) || $this->is_token_expiring_soon($this->soon_limit) > 0)) {
95 set_transient('photonic_' . $this->provider . '_token', $token, $token['oauth_token_expires']);
96 $this->set_token_validity(true);
97 }
98 elseif ($this->is_token_expired($token) || $this->is_token_expiring_soon($this->soon_limit) > 0) {
99 list($token, $error) = $this->renew_token($base_token);
100 }
101 else {
102 $this->set_token_validity(true);
103 }
104 }
105 else {
106 list($token, $error) = $this->renew_token($base_token);
107 }
108 }
109
110 if (!empty($token)) {
111 $this->access_token = $token['oauth_token'];
112 $this->auth_error = '';
113 }
114 else {
115 $this->set_token_validity(false);
116 if (!empty($error)) {
117 $this->auth_error = $error;
118 }
119 else {
120 $this->auth_error = '';
121 }
122 }
123 }
124
125 public function is_token_expired($token) {
126 if (empty($token)) {
127 return true;
128 }
129 if (!isset($token['oauth_token']) || !isset($token['oauth_token_created']) || !isset($token['oauth_token_expires'])) {
130 return true;
131 }
132 if (!isset($token['client_id']) || (isset($token['client_id']) && $token['client_id'] !== $this->client_id)) {
133 return true;
134 }
135 $current = time();
136 if ($token['oauth_token_created'] + $token['oauth_token_expires'] < $current) {
137 return true;
138 }
139 return false;
140 }
141
142 /**
143 * Checks if a token will expire soon. This is used to trigger a refresh for sources such as Instagram. Google uses a separate "Refresh Token",
144 * so this is not applicable to it. The <code>soon_limit</code> defines how many days is "soon", and a refresh is triggered if the current date
145 * is in the "soon" range. E.g. If you have a soon limit of 30 days, and your token expires in 15 days when you load the page, this method will
146 * return <code>true</code>.
147 *
148 * For cases where the token does not exist yet, the method returns <code>null</code>.
149 *
150 * @param $soon_limit int Number of days to check the expiry limit for.
151 * @return int|null If there is no token, return null. Otherwise, if there are < $soon_limit days left, return 1, if token is expired return -1, and if there is time return 0.
152 */
153 abstract public function is_token_expiring_soon($soon_limit);
154
155 /**
156 * Takes a token response from a request token call, then puts it in an appropriate array.
157 *
158 * @param $response
159 * @return array
160 */
161 public function parse_token($response): array {
162 $token = [];
163 if (!is_wp_error($response) && is_array($response)) {
164 $body = $response['body'];
165 $body = json_decode($body);
166 if (empty($body->error)) {
167 $token['oauth_token'] = $body->access_token;
168 $token['oauth_token_type'] = $body->token_type;
169 $token['oauth_token_created'] = time();
170 $token['oauth_token_expires'] = $body->expires_in;
171 if (!empty($body->refresh_token)) {
172 $token['oauth_refresh_token'] = $body->refresh_token;
173 }
174 $this->set_token_validity(true);
175 }
176 else {
177 $this->set_token_validity(false);
178 }
179 }
180 return $token;
181 }
182 }
183