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 / Authenticator.php

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

115 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|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 return implode('&', $pairs);
68 }
69
70 /**
71 * Takes a token response from a request token call, then puts it in an appropriate array.
72 *
73 * @param $response
74 * @return array
75 */
76 abstract public function parse_token($response): array;
77
78 /**
79 * Saves the token in the authentication options. Used only by Flickr and SmugMug; invoked via the Authentication screen.
80 * Transients don't need to be saved as Flickr and SmugMug don't need them.
81 *
82 * @param $token
83 */
84 public function save_token($token) {
85 if (current_user_can('edit_theme_options') && check_ajax_referer($this->provider . '-request-token-' . $this->api_secret)) {
86 $photonic_authentication = get_option('photonic_authentication');
87 if (!isset($photonic_authentication)) {
88 $photonic_authentication = [];
89 }
90 $photonic_authentication[$this->provider] = $token;
91 update_option('photonic_authentication', $photonic_authentication);
92 }
93 }
94
95 /**
96 * Fetches a token from authentication options
97 *
98 * @return array|null
99 */
100 public function get_cached_token(): ?array {
101 $transient_token = get_transient('photonic_' . $this->provider . '_token');
102 $photonic_authentication = get_option('photonic_authentication');
103 if (false !== $transient_token) {
104 return $transient_token;
105 }
106
107 if (!empty($photonic_authentication) && isset($photonic_authentication[$this->provider])) {
108 $transient_expiry = $photonic_authentication[$this->provider]['oauth_token_created'] + $photonic_authentication[$this->provider]['oauth_token_expires'] - time();
109 set_transient('photonic_' . $this->provider . '_token', $photonic_authentication[$this->provider], $transient_expiry);
110 return $photonic_authentication[$this->provider];
111 }
112 return null;
113 }
114 }
115