PluginProbe
Photonic Gallery & Lightbox for Flickr, SmugMug & Others / 3.33
Photonic Gallery & Lightbox for Flickr, SmugMug & Others v3.33
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 / Authenticator.php

Authenticator.php in Photonic Gallery & Lightbox for Flickr, SmugMug & Others 3.33, at Platforms/Authenticator.php

116 lines 3.6 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 trait Authenticator {
6 abstract public function get_access_token($token);
7
8 public function oauth_signature_method(): string {
9 return 'HMAC-SHA1';
10 }
11
12 /**
13 * Encodes the URL as per RFC3986 specs. This replaces some strings in addition to the ones done by a rawurlencode.
14 * This has been adapted from the OAuth for PHP project.
15 *
16 * @static
17 * @param $input
18 * @return array|mixed|string
19 */
20 public static function urlencode_rfc3986($input) {
21 if (is_array($input)) {
22 return array_map(['Photonic_Plugin\Platforms\Authenticator', 'urlencode_rfc3986'], $input);
23 }
24 elseif (is_scalar($input)) {
25 return str_replace(
26 '+',
27 ' ',
28 str_replace('%7E', '~', rawurlencode($input))
29 );
30 }
31 else {
32 return '';
33 }
34 }
35
36 /**
37 * Takes an array of parameters, then parses it and generates a query string. Prior to generating the query string the parameters are sorted in their natural order.
38 * Without sorting the signatures between this application and the provider might differ.
39 *
40 * @static
41 * @param $params
42 * @return string
43 */
44 public static function build_query($params): string {
45 if (!$params) {
46 return '';
47 }
48 $keys = array_map(['Photonic_Plugin\Platforms\Authenticator', 'urlencode_rfc3986'], array_keys($params));
49 $values = array_map(['Photonic_Plugin\Platforms\Authenticator', 'urlencode_rfc3986'], array_values($params));
50 $params = array_combine($keys, $values);
51
52 // Sort by keys (natsort)
53 uksort($params, 'strnatcmp');
54 $pairs = [];
55 foreach ($params as $key => $value) {
56 if (is_array($value)) {
57 natsort($value);
58 foreach ($value as $v2) {
59 $pairs[] = ('' === $v2) ? "$key=0" : "$key=$v2";
60 }
61 }
62 else {
63 $pairs[] = ('' === $value) ? "$key=0" : "$key=$value";
64 }
65 }
66
67 $string = implode('&', $pairs);
68 return $string;
69 }
70
71 /**
72 * Takes a token response from a request token call, then puts it in an appropriate array.
73 *
74 * @param $response
75 * @return array
76 */
77 abstract public function parse_token($response): array;
78
79 /**
80 * Saves the token in the authentication options. Used only by Flickr and SmugMug; invoked via the Authentication screen.
81 * Transients don't need to be saved as Flickr and SmugMug don't need them.
82 *
83 * @param $token
84 */
85 public function save_token($token) {
86 if (current_user_can('edit_theme_options') && check_ajax_referer($this->provider . '-request-token-' . $this->api_secret)) {
87 $photonic_authentication = get_option('photonic_authentication');
88 if (!isset($photonic_authentication)) {
89 $photonic_authentication = [];
90 }
91 $photonic_authentication[$this->provider] = $token;
92 update_option('photonic_authentication', $photonic_authentication);
93 }
94 }
95
96 /**
97 * Fetches a token from authentication options
98 *
99 * @return array|null
100 */
101 public function get_cached_token() {
102 $transient_token = get_transient('photonic_' . $this->provider . '_token');
103 $photonic_authentication = get_option('photonic_authentication');
104 if (false !== $transient_token) {
105 return $transient_token;
106 }
107
108 if (!empty($photonic_authentication) && isset($photonic_authentication[$this->provider])) {
109 $transient_expiry = $photonic_authentication[$this->provider]['oauth_token_created'] + $photonic_authentication[$this->provider]['oauth_token_expires'] - time();
110 set_transient('photonic_' . $this->provider . '_token', $photonic_authentication[$this->provider], $transient_expiry);
111 return $photonic_authentication[$this->provider];
112 }
113 return null;
114 }
115 }
116