PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 3.0.1
Defender Security – Malware Scanner, Login Security & Firewall v3.0.1
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / traits / webauthn.php

webauthn.php in Defender Security – Malware Scanner, Login Security & Firewall 3.0.1, at src/traits/webauthn.php

220 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WP_Defender\Traits;
4
5 use Throwable;
6
7 trait Webauthn {
8 /**
9 * Prefix used for option.
10 *
11 * @var string
12 */
13 public $option_prefix = 'wpdef_webauthn_';
14
15 /**
16 * Check if required PHP is installed.
17 *
18 * @return bool
19 */
20 public function check_php_version(): bool {
21 if ( version_compare( PHP_VERSION, '7.2.0', '>=' ) ) {
22 return true;
23 }
24 return false;
25 }
26
27 /**
28 * Check if SSL is used.
29 *
30 * @return bool
31 */
32 public function is_ssl(): bool {
33 if (
34 ( ! empty( $_SERVER['HTTPS'] ) && ( 'on' === strtolower( $_SERVER['HTTPS'] ) || '1' === $_SERVER['HTTPS'] ) ) ||
35 ( ! empty( $_SERVER['REQUEST_SCHEME'] ) && 'https' === $_SERVER['REQUEST_SCHEME'] ) ||
36 ( ! empty( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && 'https' === $_SERVER['HTTP_X_FORWARDED_PROTO'] ) ||
37 ( ! empty( $_SERVER['HTTP_X_FORWARDED_SSL'] ) && 'on' === $_SERVER['HTTP_X_FORWARDED_SSL'] )
38 ) {
39 return true;
40 }
41
42 return false;
43 }
44
45 /**
46 * Check if 'gmp' extension is enabled.
47 *
48 * @return bool
49 */
50 public function is_enabled_gmp(): bool {
51 return extension_loaded( 'gmp' );
52 }
53
54 /**
55 * Check if 'mbstring' extension is enabled.
56 *
57 * @return bool
58 */
59 public function is_enabled_mbstring(): bool {
60 return extension_loaded( 'mbstring' );
61 }
62
63 /**
64 * Check if 'sodium' extension is enabled.
65 *
66 * @return bool
67 */
68 public function is_enabled_sodium(): bool {
69 return extension_loaded( 'sodium' );
70 }
71
72 /**
73 * Check if server requirements are met.
74 *
75 * @return bool
76 */
77 public function check_webauthn_requirements(): bool {
78 return $this->check_php_version() &&
79 $this->is_ssl() &&
80 $this->is_enabled_gmp() &&
81 $this->is_enabled_mbstring() &&
82 $this->is_enabled_sodium();
83 }
84
85 /**
86 * Set transient.
87 *
88 * @param string $name
89 * @param mixed $value
90 * @param string $client_id
91 * @param int $exp
92 *
93 * @return bool
94 */
95 public function set_trans_val( string $name, $value, string $client_id, int $exp = 90 ): bool {
96 $trans_name = $this->option_prefix . $name . '_' . $client_id;
97 $trans_val = serialize( $value );
98
99 return set_transient( $trans_name, $trans_val, $exp );
100 }
101
102 /**
103 * Get transient.
104 *
105 * @param string $name
106 * @param string $client_id
107 *
108 * @return mixed
109 */
110 public function get_trans_val( string $name, string $client_id ) {
111 $trans_name = $this->option_prefix . $name . '_' . $client_id;
112 $trans_val = get_transient( $trans_name );
113
114 return false !== $trans_val ? unserialize( $trans_val ) : false;
115 }
116
117 /**
118 * Delete transient.
119 *
120 * @param string $name
121 * @param string $client_id
122 *
123 * @return bool
124 */
125 public function delete_trans( string $name, string $client_id ): bool {
126 $trans_name = $this->option_prefix . $name . '_' . $client_id;
127
128 return delete_transient( $trans_name );
129 }
130
131 /**
132 * Update option.
133 *
134 * @param string $name
135 * @param mixed $value
136 *
137 * @return bool
138 */
139 public function update_option( string $name, $value ): bool {
140 return update_option( $this->option_prefix . $name, json_encode( $value ) );
141 }
142
143 /**
144 * Get option.
145 *
146 * @param string $name
147 *
148 * @return mixed
149 */
150 public function get_option( string $name ) {
151 $value = get_option( $this->option_prefix . $name );
152 if ( null !== $value ) {
153 try {
154 return json_decode( $value, true );
155 } catch ( Throwable $exception ) {
156 return [];
157 }
158 }
159 return [];
160 }
161
162 /**
163 * Get website name.
164 *
165 * @return string
166 */
167 public function get_site_name(): string {
168 return get_bloginfo( 'name' );
169 }
170
171 /**
172 * Get website domain.
173 *
174 * @return string
175 */
176 public function get_site_domain(): string {
177 $site_url = get_bloginfo( 'url' );
178 $site_domain = preg_replace( '#^http(s)?:\/\/#', '', $site_url );
179 $site_domain = preg_replace( '#^www\.#', '', $site_domain );
180 $site_domain = explode( '/', $site_domain );
181
182 return $site_domain[0] ?? $site_domain;
183 }
184
185 /**
186 * Get user hash.
187 *
188 * @param string $username
189 * @param string $display_name
190 *
191 * @return string
192 */
193 public function get_user_hash( string $username, string $display_name ): string {
194 return hash( 'sha256', $username . '-' . $display_name . '-' . AUTH_SALT );
195 }
196
197 /**
198 * URL safe base64 encoding.
199 *
200 * @param string $data
201 *
202 * @return string
203 */
204 public function base64url_encode( string $data ): string {
205 return rtrim( strtr( base64_encode( $data ), '+/', '-_' ), '=' );
206 }
207
208 /**
209 * Base64 decoding from URL safe base64 encoding.
210 *
211 * @param string $data
212 *
213 * @return string|bool
214 */
215 public function base64url_decode( string $data ) {
216 // No need to add '=' at the end.
217 return base64_decode( strtr( $data, '-_', '+/' ) );
218 }
219 }
220