@@ -103,7 +103,7 @@ discard block |
||
103 | 103 | */ |
104 | 104 | protected function __construct() |
105 | 105 | { |
106 | - if (! defined('ESPRESSO_ENCRYPT')) { |
|
106 | + if ( ! defined('ESPRESSO_ENCRYPT')) { |
|
107 | 107 | define('ESPRESSO_ENCRYPT', true); |
108 | 108 | } |
109 | 109 | if (extension_loaded('openssl')) { |
@@ -125,7 +125,7 @@ discard block |
||
125 | 125 | public static function instance() |
126 | 126 | { |
127 | 127 | // check if class object is instantiated |
128 | - if (! EE_Encryption::$_instance instanceof EE_Encryption) { |
|
128 | + if ( ! EE_Encryption::$_instance instanceof EE_Encryption) { |
|
129 | 129 | EE_Encryption::$_instance = new self(); |
130 | 130 | } |
131 | 131 | return EE_Encryption::$_instance; |
@@ -332,7 +332,7 @@ discard block |
||
332 | 332 | $iv |
333 | 333 | ); |
334 | 334 | // append the initialization vector |
335 | - $encrypted_text .= EE_Encryption::OPENSSL_IV_DELIMITER . $iv; |
|
335 | + $encrypted_text .= EE_Encryption::OPENSSL_IV_DELIMITER.$iv; |
|
336 | 336 | // trim and maybe encode |
337 | 337 | return $this->_use_base64_encode |
338 | 338 | ? trim(base64_encode($encrypted_text)) |
@@ -515,7 +515,7 @@ discard block |
||
515 | 515 | if (empty($text_string)) { |
516 | 516 | return $text_string; |
517 | 517 | } |
518 | - $key_bits = str_split( |
|
518 | + $key_bits = str_split( |
|
519 | 519 | str_pad( |
520 | 520 | '', |
521 | 521 | strlen($text_string), |
@@ -525,8 +525,8 @@ discard block |
||
525 | 525 | ); |
526 | 526 | $string_bits = str_split($text_string); |
527 | 527 | foreach ($string_bits as $k => $v) { |
528 | - $temp = ord($v) + ord($key_bits[ $k ]); |
|
529 | - $string_bits[ $k ] = chr($temp > 255 ? ($temp - 256) : $temp); |
|
528 | + $temp = ord($v) + ord($key_bits[$k]); |
|
529 | + $string_bits[$k] = chr($temp > 255 ? ($temp - 256) : $temp); |
|
530 | 530 | } |
531 | 531 | $encrypted_text = implode('', $string_bits); |
532 | 532 | $encrypted_text .= EE_Encryption::ACME_ENCRYPTION_FLAG; |
@@ -568,10 +568,10 @@ discard block |
||
568 | 568 | STR_PAD_RIGHT |
569 | 569 | ) |
570 | 570 | ); |
571 | - $string_bits = str_split($encrypted_text); |
|
571 | + $string_bits = str_split($encrypted_text); |
|
572 | 572 | foreach ($string_bits as $k => $v) { |
573 | - $temp = ord($v) - ord($key_bits[ $k ]); |
|
574 | - $string_bits[ $k ] = chr($temp < 0 ? ($temp + 256) : $temp); |
|
573 | + $temp = ord($v) - ord($key_bits[$k]); |
|
574 | + $string_bits[$k] = chr($temp < 0 ? ($temp + 256) : $temp); |
|
575 | 575 | } |
576 | 576 | return implode('', $string_bits); |
577 | 577 | } |
@@ -585,16 +585,16 @@ discard block |
||
585 | 585 | protected function valid_base_64($string) |
586 | 586 | { |
587 | 587 | // ensure data is a string |
588 | - if (! is_string($string) || ! $this->_use_base64_encode) { |
|
588 | + if ( ! is_string($string) || ! $this->_use_base64_encode) { |
|
589 | 589 | return false; |
590 | 590 | } |
591 | 591 | $decoded = base64_decode($string, true); |
592 | 592 | // Check if there is no invalid character in string |
593 | - if (! preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $string)) { |
|
593 | + if ( ! preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $string)) { |
|
594 | 594 | return false; |
595 | 595 | } |
596 | 596 | // Decode the string in strict mode and send the response |
597 | - if (! base64_decode($string, true)) { |
|
597 | + if ( ! base64_decode($string, true)) { |
|
598 | 598 | return false; |
599 | 599 | } |
600 | 600 | // Encode and compare it to original one |
@@ -614,7 +614,7 @@ discard block |
||
614 | 614 | $iterations = ceil($length / 40); |
615 | 615 | $random_string = ''; |
616 | 616 | for ($i = 0; $i < $iterations; $i++) { |
617 | - $random_string .= sha1(microtime(true) . mt_rand(10000, 90000)); |
|
617 | + $random_string .= sha1(microtime(true).mt_rand(10000, 90000)); |
|
618 | 618 | } |
619 | 619 | $random_string = substr($random_string, 0, $length); |
620 | 620 | return $random_string; |
@@ -39,676 +39,676 @@ |
||
39 | 39 | */ |
40 | 40 | class EE_Encryption implements InterminableInterface |
41 | 41 | { |
42 | - /** |
|
43 | - * key used for saving the encryption key to the wp_options table |
|
44 | - */ |
|
45 | - const ENCRYPTION_OPTION_KEY = 'ee_encryption_key'; |
|
46 | - |
|
47 | - /** |
|
48 | - * the OPENSSL cipher method used |
|
49 | - */ |
|
50 | - const OPENSSL_CIPHER_METHOD = 'AES-128-CBC'; |
|
51 | - |
|
52 | - /** |
|
53 | - * WP "options_name" used to store a verified available cipher method |
|
54 | - */ |
|
55 | - const OPENSSL_CIPHER_METHOD_OPTION_NAME = 'ee_openssl_cipher_method'; |
|
56 | - |
|
57 | - /** |
|
58 | - * the OPENSSL digest method used |
|
59 | - */ |
|
60 | - const OPENSSL_DIGEST_METHOD = 'sha512'; |
|
61 | - |
|
62 | - /** |
|
63 | - * separates the encrypted text from the initialization vector |
|
64 | - */ |
|
65 | - const OPENSSL_IV_DELIMITER = ':iv:'; |
|
66 | - |
|
67 | - /** |
|
68 | - * appended to text encrypted using the acme encryption |
|
69 | - */ |
|
70 | - const ACME_ENCRYPTION_FLAG = '::ae'; |
|
71 | - |
|
72 | - |
|
73 | - /** |
|
74 | - * instance of the EE_Encryption object |
|
75 | - */ |
|
76 | - protected static $_instance; |
|
77 | - |
|
78 | - /** |
|
79 | - * @var string $_encryption_key |
|
80 | - */ |
|
81 | - protected $_encryption_key; |
|
82 | - |
|
83 | - /** |
|
84 | - * @var string $cipher_method |
|
85 | - */ |
|
86 | - private $cipher_method = ''; |
|
87 | - |
|
88 | - /** |
|
89 | - * @var array $cipher_methods |
|
90 | - */ |
|
91 | - private $cipher_methods = []; |
|
92 | - |
|
93 | - /** |
|
94 | - * @var array $digest_methods |
|
95 | - */ |
|
96 | - private $digest_methods = []; |
|
97 | - |
|
98 | - /** |
|
99 | - * @var boolean $_use_openssl_encrypt |
|
100 | - */ |
|
101 | - protected $_use_openssl_encrypt = false; |
|
102 | - |
|
103 | - /** |
|
104 | - * @var boolean $_use_mcrypt |
|
105 | - */ |
|
106 | - protected $_use_mcrypt = false; |
|
107 | - |
|
108 | - /** |
|
109 | - * @var boolean $_use_base64_encode |
|
110 | - */ |
|
111 | - protected $_use_base64_encode = false; |
|
112 | - |
|
113 | - |
|
114 | - /** |
|
115 | - * protected constructor to prevent direct creation |
|
116 | - */ |
|
117 | - protected function __construct() |
|
118 | - { |
|
119 | - if (! defined('ESPRESSO_ENCRYPT')) { |
|
120 | - define('ESPRESSO_ENCRYPT', true); |
|
121 | - } |
|
122 | - if (extension_loaded('openssl')) { |
|
123 | - $this->_use_openssl_encrypt = true; |
|
124 | - } elseif (extension_loaded('mcrypt')) { |
|
125 | - $this->_use_mcrypt = true; |
|
126 | - } |
|
127 | - if (function_exists('base64_encode')) { |
|
128 | - $this->_use_base64_encode = true; |
|
129 | - } |
|
130 | - } |
|
131 | - |
|
132 | - |
|
133 | - /** |
|
134 | - * singleton method used to instantiate class object |
|
135 | - * |
|
136 | - * @return EE_Encryption |
|
137 | - */ |
|
138 | - public static function instance() |
|
139 | - { |
|
140 | - // check if class object is instantiated |
|
141 | - if (! EE_Encryption::$_instance instanceof EE_Encryption) { |
|
142 | - EE_Encryption::$_instance = new self(); |
|
143 | - } |
|
144 | - return EE_Encryption::$_instance; |
|
145 | - } |
|
146 | - |
|
147 | - |
|
148 | - /** |
|
149 | - * get encryption key |
|
150 | - * |
|
151 | - * @return string |
|
152 | - */ |
|
153 | - public function get_encryption_key() |
|
154 | - { |
|
155 | - // if encryption key has not been set |
|
156 | - if (empty($this->_encryption_key)) { |
|
157 | - // retrieve encryption_key from db |
|
158 | - $this->_encryption_key = get_option(EE_Encryption::ENCRYPTION_OPTION_KEY, ''); |
|
159 | - // WHAT?? No encryption_key in the db ?? |
|
160 | - if ($this->_encryption_key === '') { |
|
161 | - // let's make one. And md5 it to make it just the right size for a key |
|
162 | - $new_key = md5($this->generate_random_string()); |
|
163 | - // now save it to the db for later |
|
164 | - add_option(EE_Encryption::ENCRYPTION_OPTION_KEY, $new_key); |
|
165 | - // here's the key - FINALLY ! |
|
166 | - $this->_encryption_key = $new_key; |
|
167 | - } |
|
168 | - } |
|
169 | - return $this->_encryption_key; |
|
170 | - } |
|
171 | - |
|
172 | - |
|
173 | - /** |
|
174 | - * encrypts data |
|
175 | - * |
|
176 | - * @param string $text_string - the text to be encrypted |
|
177 | - * @return string |
|
178 | - * @throws RuntimeException |
|
179 | - */ |
|
180 | - public function encrypt($text_string = '') |
|
181 | - { |
|
182 | - // you give me nothing??? GET OUT ! |
|
183 | - if (empty($text_string)) { |
|
184 | - return $text_string; |
|
185 | - } |
|
186 | - if ($this->_use_openssl_encrypt) { |
|
187 | - $encrypted_text = $this->openssl_encrypt($text_string); |
|
188 | - } else { |
|
189 | - $encrypted_text = $this->acme_encrypt($text_string); |
|
190 | - } |
|
191 | - return $encrypted_text; |
|
192 | - } |
|
193 | - |
|
194 | - |
|
195 | - /** |
|
196 | - * decrypts data |
|
197 | - * |
|
198 | - * @param string $encrypted_text - the text to be decrypted |
|
199 | - * @return string |
|
200 | - * @throws RuntimeException |
|
201 | - */ |
|
202 | - public function decrypt($encrypted_text = '') |
|
203 | - { |
|
204 | - // you give me nothing??? GET OUT ! |
|
205 | - if (empty($encrypted_text)) { |
|
206 | - return $encrypted_text; |
|
207 | - } |
|
208 | - // if PHP's mcrypt functions are installed then we'll use them |
|
209 | - if ($this->_use_openssl_encrypt) { |
|
210 | - $decrypted_text = $this->openssl_decrypt($encrypted_text); |
|
211 | - } else { |
|
212 | - $decrypted_text = $this->acme_decrypt($encrypted_text); |
|
213 | - } |
|
214 | - return $decrypted_text; |
|
215 | - } |
|
216 | - |
|
217 | - |
|
218 | - /** |
|
219 | - * encodes string with PHP's base64 encoding |
|
220 | - * |
|
221 | - * @see http://php.net/manual/en/function.base64-encode.php |
|
222 | - * @param string $text_string the text to be encoded |
|
223 | - * @return string |
|
224 | - */ |
|
225 | - public function base64_string_encode($text_string = '') |
|
226 | - { |
|
227 | - // you give me nothing??? GET OUT ! |
|
228 | - if (empty($text_string) || ! $this->_use_base64_encode) { |
|
229 | - return $text_string; |
|
230 | - } |
|
231 | - // encode |
|
232 | - return base64_encode($text_string); |
|
233 | - } |
|
234 | - |
|
235 | - |
|
236 | - /** |
|
237 | - * decodes string that has been encoded with PHP's base64 encoding |
|
238 | - * |
|
239 | - * @see http://php.net/manual/en/function.base64-encode.php |
|
240 | - * @param string $encoded_string the text to be decoded |
|
241 | - * @return string |
|
242 | - * @throws RuntimeException |
|
243 | - */ |
|
244 | - public function base64_string_decode($encoded_string = '') |
|
245 | - { |
|
246 | - // you give me nothing??? GET OUT ! |
|
247 | - if (empty($encoded_string) || ! $this->valid_base_64($encoded_string)) { |
|
248 | - return $encoded_string; |
|
249 | - } |
|
250 | - // decode |
|
251 | - $decoded_string = base64_decode($encoded_string); |
|
252 | - if ($decoded_string === false) { |
|
253 | - throw new RuntimeException( |
|
254 | - esc_html__('Base 64 decoding failed.', 'event_espresso') |
|
255 | - ); |
|
256 | - } |
|
257 | - return $decoded_string; |
|
258 | - } |
|
259 | - |
|
260 | - |
|
261 | - /** |
|
262 | - * encodes url string with PHP's base64 encoding |
|
263 | - * |
|
264 | - * @see http://php.net/manual/en/function.base64-encode.php |
|
265 | - * @param string $text_string the text to be encoded |
|
266 | - * @return string |
|
267 | - */ |
|
268 | - public function base64_url_encode($text_string = '') |
|
269 | - { |
|
270 | - // you give me nothing??? GET OUT ! |
|
271 | - if (empty($text_string) || ! $this->_use_base64_encode) { |
|
272 | - return $text_string; |
|
273 | - } |
|
274 | - // encode |
|
275 | - $encoded_string = base64_encode($text_string); |
|
276 | - // remove chars to make encoding more URL friendly |
|
277 | - return strtr($encoded_string, '+/=', '-_,'); |
|
278 | - } |
|
279 | - |
|
280 | - |
|
281 | - /** |
|
282 | - * decodes url string that has been encoded with PHP's base64 encoding |
|
283 | - * |
|
284 | - * @see http://php.net/manual/en/function.base64-encode.php |
|
285 | - * @param string $encoded_string the text to be decoded |
|
286 | - * @return string |
|
287 | - * @throws RuntimeException |
|
288 | - */ |
|
289 | - public function base64_url_decode($encoded_string = '') |
|
290 | - { |
|
291 | - // replace previously removed characters |
|
292 | - $encoded_string = strtr($encoded_string, '-_,', '+/='); |
|
293 | - // you give me nothing??? GET OUT ! |
|
294 | - if (empty($encoded_string) || ! $this->valid_base_64($encoded_string)) { |
|
295 | - return $encoded_string; |
|
296 | - } |
|
297 | - // decode |
|
298 | - $decoded_string = base64_decode($encoded_string); |
|
299 | - if ($decoded_string === false) { |
|
300 | - throw new RuntimeException( |
|
301 | - esc_html__('Base 64 decoding failed.', 'event_espresso') |
|
302 | - ); |
|
303 | - } |
|
304 | - return $decoded_string; |
|
305 | - } |
|
306 | - |
|
307 | - |
|
308 | - /** |
|
309 | - * encrypts data using PHP's openssl functions |
|
310 | - * |
|
311 | - * @param string $text_string the text to be encrypted |
|
312 | - * @param string $cipher_method |
|
313 | - * @param string $encryption_key |
|
314 | - * @return string |
|
315 | - * @throws RuntimeException |
|
316 | - */ |
|
317 | - protected function openssl_encrypt( |
|
318 | - $text_string = '', |
|
319 | - $cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD, |
|
320 | - $encryption_key = '' |
|
321 | - ) { |
|
322 | - // you give me nothing??? GET OUT ! |
|
323 | - if (empty($text_string)) { |
|
324 | - return $text_string; |
|
325 | - } |
|
326 | - $this->cipher_method = $this->getCipherMethod($cipher_method); |
|
327 | - // get initialization vector size |
|
328 | - $iv_size = openssl_cipher_iv_length($this->cipher_method); |
|
329 | - // generate initialization vector. |
|
330 | - // The second parameter ("crypto_strong") is passed by reference, |
|
331 | - // and is used to determines if the algorithm used was "cryptographically strong" |
|
332 | - // openssl_random_pseudo_bytes() will toggle it to either true or false |
|
333 | - $iv = openssl_random_pseudo_bytes($iv_size, $is_strong); |
|
334 | - if ($iv === false || $is_strong === false) { |
|
335 | - throw new RuntimeException( |
|
336 | - esc_html__('Failed to generate OpenSSL initialization vector.', 'event_espresso') |
|
337 | - ); |
|
338 | - } |
|
339 | - // encrypt it |
|
340 | - $encrypted_text = openssl_encrypt( |
|
341 | - $text_string, |
|
342 | - $this->cipher_method, |
|
343 | - $this->getDigestHashValue(EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key), |
|
344 | - 0, |
|
345 | - $iv |
|
346 | - ); |
|
347 | - // append the initialization vector |
|
348 | - $encrypted_text .= EE_Encryption::OPENSSL_IV_DELIMITER . $iv; |
|
349 | - // trim and maybe encode |
|
350 | - return $this->_use_base64_encode |
|
351 | - ? trim(base64_encode($encrypted_text)) |
|
352 | - : trim($encrypted_text); |
|
353 | - } |
|
354 | - |
|
355 | - |
|
356 | - /** |
|
357 | - * Returns a cipher method that has been verified to work. |
|
358 | - * First checks if the cached cipher has been set already and if so, returns that. |
|
359 | - * Then tests the incoming default and returns that if it's good. |
|
360 | - * If not, then it retrieves the previously tested and saved cipher method. |
|
361 | - * But if that doesn't exist, then calls getAvailableCipherMethod() |
|
362 | - * to see what is available on the server, and returns the results. |
|
363 | - * |
|
364 | - * @param string $cipher_method |
|
365 | - * @return string |
|
366 | - * @throws RuntimeException |
|
367 | - */ |
|
368 | - protected function getCipherMethod($cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD) |
|
369 | - { |
|
370 | - if ($this->cipher_method !== '') { |
|
371 | - return $this->cipher_method; |
|
372 | - } |
|
373 | - // verify that the default cipher method can produce an initialization vector |
|
374 | - if (openssl_cipher_iv_length($cipher_method) === false) { |
|
375 | - // nope? okay let's get what we found in the past to work |
|
376 | - $cipher_method = get_option(EE_Encryption::OPENSSL_CIPHER_METHOD_OPTION_NAME, ''); |
|
377 | - // oops... haven't tested available cipher methods yet |
|
378 | - if ($cipher_method === '' || openssl_cipher_iv_length($cipher_method) === false) { |
|
379 | - $cipher_method = $this->getAvailableCipherMethod($cipher_method); |
|
380 | - } |
|
381 | - } |
|
382 | - return $cipher_method; |
|
383 | - } |
|
384 | - |
|
385 | - |
|
386 | - /** |
|
387 | - * @param string $cipher_method |
|
388 | - * @return string |
|
389 | - * @throws \RuntimeException |
|
390 | - */ |
|
391 | - protected function getAvailableCipherMethod($cipher_method) |
|
392 | - { |
|
393 | - // verify that the incoming cipher method can produce an initialization vector |
|
394 | - if (openssl_cipher_iv_length($cipher_method) === false) { |
|
395 | - // nope? then check the next cipher in the list of available cipher methods |
|
396 | - $cipher_method = next($this->cipher_methods); |
|
397 | - // what? there's no list? then generate that list and cache it, |
|
398 | - if (empty($this->cipher_methods)) { |
|
399 | - $this->cipher_methods = openssl_get_cipher_methods(); |
|
400 | - // then grab the first item from the list |
|
401 | - $cipher_method = reset($this->cipher_methods); |
|
402 | - } |
|
403 | - if ($cipher_method === false) { |
|
404 | - throw new RuntimeException( |
|
405 | - esc_html__( |
|
406 | - 'OpenSSL support appears to be enabled on the server, but no cipher methods are available. Please contact the server administrator.', |
|
407 | - 'event_espresso' |
|
408 | - ) |
|
409 | - ); |
|
410 | - } |
|
411 | - // verify that the next cipher method works |
|
412 | - return $this->getAvailableCipherMethod($cipher_method); |
|
413 | - } |
|
414 | - // if we've gotten this far, then we found an available cipher method that works |
|
415 | - // so save that for next time |
|
416 | - update_option( |
|
417 | - EE_Encryption::OPENSSL_CIPHER_METHOD_OPTION_NAME, |
|
418 | - $cipher_method |
|
419 | - ); |
|
420 | - return $cipher_method; |
|
421 | - } |
|
422 | - |
|
423 | - |
|
424 | - /** |
|
425 | - * decrypts data that has been encrypted with PHP's openssl functions |
|
426 | - * |
|
427 | - * @param string $encrypted_text the text to be decrypted |
|
428 | - * @param string $cipher_method |
|
429 | - * @param string $encryption_key |
|
430 | - * @return string |
|
431 | - * @throws RuntimeException |
|
432 | - */ |
|
433 | - protected function openssl_decrypt( |
|
434 | - $encrypted_text = '', |
|
435 | - $cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD, |
|
436 | - $encryption_key = '' |
|
437 | - ) { |
|
438 | - // you give me nothing??? GET OUT ! |
|
439 | - if (empty($encrypted_text)) { |
|
440 | - return $encrypted_text; |
|
441 | - } |
|
442 | - // decode |
|
443 | - $encrypted_text = $this->valid_base_64($encrypted_text) |
|
444 | - ? $this->base64_url_decode($encrypted_text) |
|
445 | - : $encrypted_text; |
|
446 | - $encrypted_components = explode( |
|
447 | - EE_Encryption::OPENSSL_IV_DELIMITER, |
|
448 | - $encrypted_text, |
|
449 | - 2 |
|
450 | - ); |
|
451 | - // check that iv exists, and if not, maybe text was encoded using mcrypt? |
|
452 | - if ($this->_use_mcrypt && ! isset($encrypted_components[1])) { |
|
453 | - return $this->m_decrypt($encrypted_text); |
|
454 | - } |
|
455 | - // decrypt it |
|
456 | - $decrypted_text = openssl_decrypt( |
|
457 | - $encrypted_components[0], |
|
458 | - $this->getCipherMethod($cipher_method), |
|
459 | - $this->getDigestHashValue(EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key), |
|
460 | - 0, |
|
461 | - $encrypted_components[1] |
|
462 | - ); |
|
463 | - $decrypted_text = trim($decrypted_text); |
|
464 | - return $decrypted_text; |
|
465 | - } |
|
466 | - |
|
467 | - |
|
468 | - /** |
|
469 | - * Computes the digest hash value using the specified digest method. |
|
470 | - * If that digest method fails to produce a valid hash value, |
|
471 | - * then we'll grab the next digest method and recursively try again until something works. |
|
472 | - * |
|
473 | - * @param string $digest_method |
|
474 | - * @param string $encryption_key |
|
475 | - * @return string |
|
476 | - * @throws RuntimeException |
|
477 | - */ |
|
478 | - protected function getDigestHashValue($digest_method = EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key = '') |
|
479 | - { |
|
480 | - $encryption_key = $encryption_key !== '' |
|
481 | - ? $encryption_key |
|
482 | - : $this->get_encryption_key(); |
|
483 | - $digest_hash_value = openssl_digest($encryption_key, $digest_method); |
|
484 | - if ($digest_hash_value === false) { |
|
485 | - return $this->getDigestHashValue($this->getDigestMethod()); |
|
486 | - } |
|
487 | - return $digest_hash_value; |
|
488 | - } |
|
489 | - |
|
490 | - |
|
491 | - /** |
|
492 | - * Returns the NEXT element in the $digest_methods array. |
|
493 | - * If the $digest_methods array is empty, then we populate it |
|
494 | - * with the available values returned from openssl_get_md_methods(). |
|
495 | - * |
|
496 | - * @return string |
|
497 | - * @throws \RuntimeException |
|
498 | - */ |
|
499 | - protected function getDigestMethod() |
|
500 | - { |
|
501 | - $digest_method = prev($this->digest_methods); |
|
502 | - if (empty($this->digest_methods)) { |
|
503 | - $this->digest_methods = openssl_get_md_methods(); |
|
504 | - $digest_method = end($this->digest_methods); |
|
505 | - } |
|
506 | - if ($digest_method === false) { |
|
507 | - throw new RuntimeException( |
|
508 | - esc_html__( |
|
509 | - 'OpenSSL support appears to be enabled on the server, but no digest methods are available. Please contact the server administrator.', |
|
510 | - 'event_espresso' |
|
511 | - ) |
|
512 | - ); |
|
513 | - } |
|
514 | - return $digest_method; |
|
515 | - } |
|
516 | - |
|
517 | - |
|
518 | - /** |
|
519 | - * encrypts data for acme servers that didn't bother to install PHP mcrypt |
|
520 | - * |
|
521 | - * @see http://stackoverflow.com/questions/800922/how-to-encrypt-string-without-mcrypt-library-in-php |
|
522 | - * @param string $text_string the text to be decrypted |
|
523 | - * @return string |
|
524 | - */ |
|
525 | - protected function acme_encrypt($text_string = '') |
|
526 | - { |
|
527 | - // you give me nothing??? GET OUT ! |
|
528 | - if (empty($text_string)) { |
|
529 | - return $text_string; |
|
530 | - } |
|
531 | - $key_bits = str_split( |
|
532 | - str_pad( |
|
533 | - '', |
|
534 | - strlen($text_string), |
|
535 | - $this->get_encryption_key(), |
|
536 | - STR_PAD_RIGHT |
|
537 | - ) |
|
538 | - ); |
|
539 | - $string_bits = str_split($text_string); |
|
540 | - foreach ($string_bits as $k => $v) { |
|
541 | - $temp = ord($v) + ord($key_bits[ $k ]); |
|
542 | - $string_bits[ $k ] = chr($temp > 255 ? ($temp - 256) : $temp); |
|
543 | - } |
|
544 | - $encrypted_text = implode('', $string_bits); |
|
545 | - $encrypted_text .= EE_Encryption::ACME_ENCRYPTION_FLAG; |
|
546 | - return $this->_use_base64_encode |
|
547 | - ? base64_encode($encrypted_text) |
|
548 | - : $encrypted_text; |
|
549 | - } |
|
550 | - |
|
551 | - |
|
552 | - /** |
|
553 | - * decrypts data for acme servers that didn't bother to install PHP mcrypt |
|
554 | - * |
|
555 | - * @see http://stackoverflow.com/questions/800922/how-to-encrypt-string-without-mcrypt-library-in-php |
|
556 | - * @param string $encrypted_text the text to be decrypted |
|
557 | - * @return string |
|
558 | - * @throws RuntimeException |
|
559 | - */ |
|
560 | - protected function acme_decrypt($encrypted_text = '') |
|
561 | - { |
|
562 | - // you give me nothing??? GET OUT ! |
|
563 | - if (empty($encrypted_text)) { |
|
564 | - return $encrypted_text; |
|
565 | - } |
|
566 | - // decode the data ? |
|
567 | - $encrypted_text = $this->valid_base_64($encrypted_text) |
|
568 | - ? $this->base64_url_decode($encrypted_text) |
|
569 | - : $encrypted_text; |
|
570 | - if ( |
|
571 | - $this->_use_mcrypt |
|
572 | - && strpos($encrypted_text, EE_Encryption::ACME_ENCRYPTION_FLAG) === false |
|
573 | - ) { |
|
574 | - return $this->m_decrypt($encrypted_text); |
|
575 | - } |
|
576 | - $encrypted_text = substr($encrypted_text, 0, -4); |
|
577 | - $key_bits = str_split( |
|
578 | - str_pad( |
|
579 | - '', |
|
580 | - strlen($encrypted_text), |
|
581 | - $this->get_encryption_key(), |
|
582 | - STR_PAD_RIGHT |
|
583 | - ) |
|
584 | - ); |
|
585 | - $string_bits = str_split($encrypted_text); |
|
586 | - foreach ($string_bits as $k => $v) { |
|
587 | - $temp = ord($v) - ord($key_bits[ $k ]); |
|
588 | - $string_bits[ $k ] = chr($temp < 0 ? ($temp + 256) : $temp); |
|
589 | - } |
|
590 | - return implode('', $string_bits); |
|
591 | - } |
|
592 | - |
|
593 | - |
|
594 | - /** |
|
595 | - * @see http://stackoverflow.com/questions/2556345/detect-base64-encoding-in-php#30231906 |
|
596 | - * @param $string |
|
597 | - * @return bool |
|
598 | - */ |
|
599 | - protected function valid_base_64($string) |
|
600 | - { |
|
601 | - // ensure data is a string |
|
602 | - if (! is_string($string) || ! $this->_use_base64_encode) { |
|
603 | - return false; |
|
604 | - } |
|
605 | - $decoded = base64_decode($string, true); |
|
606 | - // Check if there is no invalid character in string |
|
607 | - if (! preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $string)) { |
|
608 | - return false; |
|
609 | - } |
|
610 | - // Decode the string in strict mode and send the response |
|
611 | - if (! base64_decode($string, true)) { |
|
612 | - return false; |
|
613 | - } |
|
614 | - // Encode and compare it to original one |
|
615 | - return base64_encode($decoded) === $string; |
|
616 | - } |
|
617 | - |
|
618 | - |
|
619 | - /** |
|
620 | - * generate random string |
|
621 | - * |
|
622 | - * @see http://stackoverflow.com/questions/637278/what-is-the-best-way-to-generate-a-random-key-within-php |
|
623 | - * @param int $length number of characters for random string |
|
624 | - * @return string |
|
625 | - */ |
|
626 | - public function generate_random_string($length = 40) |
|
627 | - { |
|
628 | - $iterations = ceil($length / 40); |
|
629 | - $random_string = ''; |
|
630 | - for ($i = 0; $i < $iterations; $i++) { |
|
631 | - $random_string .= sha1(microtime(true) . mt_rand(10000, 90000)); |
|
632 | - } |
|
633 | - $random_string = substr($random_string, 0, $length); |
|
634 | - return $random_string; |
|
635 | - } |
|
636 | - |
|
637 | - |
|
638 | - /** |
|
639 | - * encrypts data using PHP's mcrypt functions |
|
640 | - * |
|
641 | - * @param string $text_string |
|
642 | - * @return string |
|
643 | - * @throws RuntimeException |
|
644 | - * @deprecated 4.9.39 |
|
645 | - * @internal param $string - the text to be encrypted |
|
646 | - */ |
|
647 | - protected function m_encrypt($text_string = '') |
|
648 | - { |
|
649 | - // you give me nothing??? GET OUT ! |
|
650 | - if (empty($text_string)) { |
|
651 | - return $text_string; |
|
652 | - } |
|
653 | - // get the initialization vector size |
|
654 | - $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); |
|
655 | - // initialization vector |
|
656 | - $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); |
|
657 | - if ($iv === false) { |
|
658 | - throw new RuntimeException( |
|
659 | - esc_html__('Failed to generate mcrypt initialization vector.', 'event_espresso') |
|
660 | - ); |
|
661 | - } |
|
662 | - // encrypt it |
|
663 | - $encrypted_text = mcrypt_encrypt( |
|
664 | - MCRYPT_RIJNDAEL_256, |
|
665 | - $this->get_encryption_key(), |
|
666 | - $text_string, |
|
667 | - MCRYPT_MODE_ECB, |
|
668 | - $iv |
|
669 | - ); |
|
670 | - // trim and maybe encode |
|
671 | - return $this->_use_base64_encode |
|
672 | - ? trim(base64_encode($encrypted_text)) |
|
673 | - : trim($encrypted_text); |
|
674 | - } |
|
675 | - |
|
676 | - |
|
677 | - /** |
|
678 | - * decrypts data that has been encrypted with PHP's mcrypt functions |
|
679 | - * |
|
680 | - * @param string $encrypted_text the text to be decrypted |
|
681 | - * @return string |
|
682 | - * @throws RuntimeException |
|
683 | - * @deprecated 4.9.39 |
|
684 | - */ |
|
685 | - protected function m_decrypt($encrypted_text = '') |
|
686 | - { |
|
687 | - // you give me nothing??? GET OUT ! |
|
688 | - if (empty($encrypted_text)) { |
|
689 | - return $encrypted_text; |
|
690 | - } |
|
691 | - // decode |
|
692 | - $encrypted_text = $this->valid_base_64($encrypted_text) |
|
693 | - ? $this->base64_url_decode($encrypted_text) |
|
694 | - : $encrypted_text; |
|
695 | - // get the initialization vector size |
|
696 | - $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); |
|
697 | - $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); |
|
698 | - if ($iv === false) { |
|
699 | - throw new RuntimeException( |
|
700 | - esc_html__('Failed to generate mcrypt initialization vector.', 'event_espresso') |
|
701 | - ); |
|
702 | - } |
|
703 | - // decrypt it |
|
704 | - $decrypted_text = mcrypt_decrypt( |
|
705 | - MCRYPT_RIJNDAEL_256, |
|
706 | - $this->get_encryption_key(), |
|
707 | - $encrypted_text, |
|
708 | - MCRYPT_MODE_ECB, |
|
709 | - $iv |
|
710 | - ); |
|
711 | - $decrypted_text = trim($decrypted_text); |
|
712 | - return $decrypted_text; |
|
713 | - } |
|
42 | + /** |
|
43 | + * key used for saving the encryption key to the wp_options table |
|
44 | + */ |
|
45 | + const ENCRYPTION_OPTION_KEY = 'ee_encryption_key'; |
|
46 | + |
|
47 | + /** |
|
48 | + * the OPENSSL cipher method used |
|
49 | + */ |
|
50 | + const OPENSSL_CIPHER_METHOD = 'AES-128-CBC'; |
|
51 | + |
|
52 | + /** |
|
53 | + * WP "options_name" used to store a verified available cipher method |
|
54 | + */ |
|
55 | + const OPENSSL_CIPHER_METHOD_OPTION_NAME = 'ee_openssl_cipher_method'; |
|
56 | + |
|
57 | + /** |
|
58 | + * the OPENSSL digest method used |
|
59 | + */ |
|
60 | + const OPENSSL_DIGEST_METHOD = 'sha512'; |
|
61 | + |
|
62 | + /** |
|
63 | + * separates the encrypted text from the initialization vector |
|
64 | + */ |
|
65 | + const OPENSSL_IV_DELIMITER = ':iv:'; |
|
66 | + |
|
67 | + /** |
|
68 | + * appended to text encrypted using the acme encryption |
|
69 | + */ |
|
70 | + const ACME_ENCRYPTION_FLAG = '::ae'; |
|
71 | + |
|
72 | + |
|
73 | + /** |
|
74 | + * instance of the EE_Encryption object |
|
75 | + */ |
|
76 | + protected static $_instance; |
|
77 | + |
|
78 | + /** |
|
79 | + * @var string $_encryption_key |
|
80 | + */ |
|
81 | + protected $_encryption_key; |
|
82 | + |
|
83 | + /** |
|
84 | + * @var string $cipher_method |
|
85 | + */ |
|
86 | + private $cipher_method = ''; |
|
87 | + |
|
88 | + /** |
|
89 | + * @var array $cipher_methods |
|
90 | + */ |
|
91 | + private $cipher_methods = []; |
|
92 | + |
|
93 | + /** |
|
94 | + * @var array $digest_methods |
|
95 | + */ |
|
96 | + private $digest_methods = []; |
|
97 | + |
|
98 | + /** |
|
99 | + * @var boolean $_use_openssl_encrypt |
|
100 | + */ |
|
101 | + protected $_use_openssl_encrypt = false; |
|
102 | + |
|
103 | + /** |
|
104 | + * @var boolean $_use_mcrypt |
|
105 | + */ |
|
106 | + protected $_use_mcrypt = false; |
|
107 | + |
|
108 | + /** |
|
109 | + * @var boolean $_use_base64_encode |
|
110 | + */ |
|
111 | + protected $_use_base64_encode = false; |
|
112 | + |
|
113 | + |
|
114 | + /** |
|
115 | + * protected constructor to prevent direct creation |
|
116 | + */ |
|
117 | + protected function __construct() |
|
118 | + { |
|
119 | + if (! defined('ESPRESSO_ENCRYPT')) { |
|
120 | + define('ESPRESSO_ENCRYPT', true); |
|
121 | + } |
|
122 | + if (extension_loaded('openssl')) { |
|
123 | + $this->_use_openssl_encrypt = true; |
|
124 | + } elseif (extension_loaded('mcrypt')) { |
|
125 | + $this->_use_mcrypt = true; |
|
126 | + } |
|
127 | + if (function_exists('base64_encode')) { |
|
128 | + $this->_use_base64_encode = true; |
|
129 | + } |
|
130 | + } |
|
131 | + |
|
132 | + |
|
133 | + /** |
|
134 | + * singleton method used to instantiate class object |
|
135 | + * |
|
136 | + * @return EE_Encryption |
|
137 | + */ |
|
138 | + public static function instance() |
|
139 | + { |
|
140 | + // check if class object is instantiated |
|
141 | + if (! EE_Encryption::$_instance instanceof EE_Encryption) { |
|
142 | + EE_Encryption::$_instance = new self(); |
|
143 | + } |
|
144 | + return EE_Encryption::$_instance; |
|
145 | + } |
|
146 | + |
|
147 | + |
|
148 | + /** |
|
149 | + * get encryption key |
|
150 | + * |
|
151 | + * @return string |
|
152 | + */ |
|
153 | + public function get_encryption_key() |
|
154 | + { |
|
155 | + // if encryption key has not been set |
|
156 | + if (empty($this->_encryption_key)) { |
|
157 | + // retrieve encryption_key from db |
|
158 | + $this->_encryption_key = get_option(EE_Encryption::ENCRYPTION_OPTION_KEY, ''); |
|
159 | + // WHAT?? No encryption_key in the db ?? |
|
160 | + if ($this->_encryption_key === '') { |
|
161 | + // let's make one. And md5 it to make it just the right size for a key |
|
162 | + $new_key = md5($this->generate_random_string()); |
|
163 | + // now save it to the db for later |
|
164 | + add_option(EE_Encryption::ENCRYPTION_OPTION_KEY, $new_key); |
|
165 | + // here's the key - FINALLY ! |
|
166 | + $this->_encryption_key = $new_key; |
|
167 | + } |
|
168 | + } |
|
169 | + return $this->_encryption_key; |
|
170 | + } |
|
171 | + |
|
172 | + |
|
173 | + /** |
|
174 | + * encrypts data |
|
175 | + * |
|
176 | + * @param string $text_string - the text to be encrypted |
|
177 | + * @return string |
|
178 | + * @throws RuntimeException |
|
179 | + */ |
|
180 | + public function encrypt($text_string = '') |
|
181 | + { |
|
182 | + // you give me nothing??? GET OUT ! |
|
183 | + if (empty($text_string)) { |
|
184 | + return $text_string; |
|
185 | + } |
|
186 | + if ($this->_use_openssl_encrypt) { |
|
187 | + $encrypted_text = $this->openssl_encrypt($text_string); |
|
188 | + } else { |
|
189 | + $encrypted_text = $this->acme_encrypt($text_string); |
|
190 | + } |
|
191 | + return $encrypted_text; |
|
192 | + } |
|
193 | + |
|
194 | + |
|
195 | + /** |
|
196 | + * decrypts data |
|
197 | + * |
|
198 | + * @param string $encrypted_text - the text to be decrypted |
|
199 | + * @return string |
|
200 | + * @throws RuntimeException |
|
201 | + */ |
|
202 | + public function decrypt($encrypted_text = '') |
|
203 | + { |
|
204 | + // you give me nothing??? GET OUT ! |
|
205 | + if (empty($encrypted_text)) { |
|
206 | + return $encrypted_text; |
|
207 | + } |
|
208 | + // if PHP's mcrypt functions are installed then we'll use them |
|
209 | + if ($this->_use_openssl_encrypt) { |
|
210 | + $decrypted_text = $this->openssl_decrypt($encrypted_text); |
|
211 | + } else { |
|
212 | + $decrypted_text = $this->acme_decrypt($encrypted_text); |
|
213 | + } |
|
214 | + return $decrypted_text; |
|
215 | + } |
|
216 | + |
|
217 | + |
|
218 | + /** |
|
219 | + * encodes string with PHP's base64 encoding |
|
220 | + * |
|
221 | + * @see http://php.net/manual/en/function.base64-encode.php |
|
222 | + * @param string $text_string the text to be encoded |
|
223 | + * @return string |
|
224 | + */ |
|
225 | + public function base64_string_encode($text_string = '') |
|
226 | + { |
|
227 | + // you give me nothing??? GET OUT ! |
|
228 | + if (empty($text_string) || ! $this->_use_base64_encode) { |
|
229 | + return $text_string; |
|
230 | + } |
|
231 | + // encode |
|
232 | + return base64_encode($text_string); |
|
233 | + } |
|
234 | + |
|
235 | + |
|
236 | + /** |
|
237 | + * decodes string that has been encoded with PHP's base64 encoding |
|
238 | + * |
|
239 | + * @see http://php.net/manual/en/function.base64-encode.php |
|
240 | + * @param string $encoded_string the text to be decoded |
|
241 | + * @return string |
|
242 | + * @throws RuntimeException |
|
243 | + */ |
|
244 | + public function base64_string_decode($encoded_string = '') |
|
245 | + { |
|
246 | + // you give me nothing??? GET OUT ! |
|
247 | + if (empty($encoded_string) || ! $this->valid_base_64($encoded_string)) { |
|
248 | + return $encoded_string; |
|
249 | + } |
|
250 | + // decode |
|
251 | + $decoded_string = base64_decode($encoded_string); |
|
252 | + if ($decoded_string === false) { |
|
253 | + throw new RuntimeException( |
|
254 | + esc_html__('Base 64 decoding failed.', 'event_espresso') |
|
255 | + ); |
|
256 | + } |
|
257 | + return $decoded_string; |
|
258 | + } |
|
259 | + |
|
260 | + |
|
261 | + /** |
|
262 | + * encodes url string with PHP's base64 encoding |
|
263 | + * |
|
264 | + * @see http://php.net/manual/en/function.base64-encode.php |
|
265 | + * @param string $text_string the text to be encoded |
|
266 | + * @return string |
|
267 | + */ |
|
268 | + public function base64_url_encode($text_string = '') |
|
269 | + { |
|
270 | + // you give me nothing??? GET OUT ! |
|
271 | + if (empty($text_string) || ! $this->_use_base64_encode) { |
|
272 | + return $text_string; |
|
273 | + } |
|
274 | + // encode |
|
275 | + $encoded_string = base64_encode($text_string); |
|
276 | + // remove chars to make encoding more URL friendly |
|
277 | + return strtr($encoded_string, '+/=', '-_,'); |
|
278 | + } |
|
279 | + |
|
280 | + |
|
281 | + /** |
|
282 | + * decodes url string that has been encoded with PHP's base64 encoding |
|
283 | + * |
|
284 | + * @see http://php.net/manual/en/function.base64-encode.php |
|
285 | + * @param string $encoded_string the text to be decoded |
|
286 | + * @return string |
|
287 | + * @throws RuntimeException |
|
288 | + */ |
|
289 | + public function base64_url_decode($encoded_string = '') |
|
290 | + { |
|
291 | + // replace previously removed characters |
|
292 | + $encoded_string = strtr($encoded_string, '-_,', '+/='); |
|
293 | + // you give me nothing??? GET OUT ! |
|
294 | + if (empty($encoded_string) || ! $this->valid_base_64($encoded_string)) { |
|
295 | + return $encoded_string; |
|
296 | + } |
|
297 | + // decode |
|
298 | + $decoded_string = base64_decode($encoded_string); |
|
299 | + if ($decoded_string === false) { |
|
300 | + throw new RuntimeException( |
|
301 | + esc_html__('Base 64 decoding failed.', 'event_espresso') |
|
302 | + ); |
|
303 | + } |
|
304 | + return $decoded_string; |
|
305 | + } |
|
306 | + |
|
307 | + |
|
308 | + /** |
|
309 | + * encrypts data using PHP's openssl functions |
|
310 | + * |
|
311 | + * @param string $text_string the text to be encrypted |
|
312 | + * @param string $cipher_method |
|
313 | + * @param string $encryption_key |
|
314 | + * @return string |
|
315 | + * @throws RuntimeException |
|
316 | + */ |
|
317 | + protected function openssl_encrypt( |
|
318 | + $text_string = '', |
|
319 | + $cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD, |
|
320 | + $encryption_key = '' |
|
321 | + ) { |
|
322 | + // you give me nothing??? GET OUT ! |
|
323 | + if (empty($text_string)) { |
|
324 | + return $text_string; |
|
325 | + } |
|
326 | + $this->cipher_method = $this->getCipherMethod($cipher_method); |
|
327 | + // get initialization vector size |
|
328 | + $iv_size = openssl_cipher_iv_length($this->cipher_method); |
|
329 | + // generate initialization vector. |
|
330 | + // The second parameter ("crypto_strong") is passed by reference, |
|
331 | + // and is used to determines if the algorithm used was "cryptographically strong" |
|
332 | + // openssl_random_pseudo_bytes() will toggle it to either true or false |
|
333 | + $iv = openssl_random_pseudo_bytes($iv_size, $is_strong); |
|
334 | + if ($iv === false || $is_strong === false) { |
|
335 | + throw new RuntimeException( |
|
336 | + esc_html__('Failed to generate OpenSSL initialization vector.', 'event_espresso') |
|
337 | + ); |
|
338 | + } |
|
339 | + // encrypt it |
|
340 | + $encrypted_text = openssl_encrypt( |
|
341 | + $text_string, |
|
342 | + $this->cipher_method, |
|
343 | + $this->getDigestHashValue(EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key), |
|
344 | + 0, |
|
345 | + $iv |
|
346 | + ); |
|
347 | + // append the initialization vector |
|
348 | + $encrypted_text .= EE_Encryption::OPENSSL_IV_DELIMITER . $iv; |
|
349 | + // trim and maybe encode |
|
350 | + return $this->_use_base64_encode |
|
351 | + ? trim(base64_encode($encrypted_text)) |
|
352 | + : trim($encrypted_text); |
|
353 | + } |
|
354 | + |
|
355 | + |
|
356 | + /** |
|
357 | + * Returns a cipher method that has been verified to work. |
|
358 | + * First checks if the cached cipher has been set already and if so, returns that. |
|
359 | + * Then tests the incoming default and returns that if it's good. |
|
360 | + * If not, then it retrieves the previously tested and saved cipher method. |
|
361 | + * But if that doesn't exist, then calls getAvailableCipherMethod() |
|
362 | + * to see what is available on the server, and returns the results. |
|
363 | + * |
|
364 | + * @param string $cipher_method |
|
365 | + * @return string |
|
366 | + * @throws RuntimeException |
|
367 | + */ |
|
368 | + protected function getCipherMethod($cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD) |
|
369 | + { |
|
370 | + if ($this->cipher_method !== '') { |
|
371 | + return $this->cipher_method; |
|
372 | + } |
|
373 | + // verify that the default cipher method can produce an initialization vector |
|
374 | + if (openssl_cipher_iv_length($cipher_method) === false) { |
|
375 | + // nope? okay let's get what we found in the past to work |
|
376 | + $cipher_method = get_option(EE_Encryption::OPENSSL_CIPHER_METHOD_OPTION_NAME, ''); |
|
377 | + // oops... haven't tested available cipher methods yet |
|
378 | + if ($cipher_method === '' || openssl_cipher_iv_length($cipher_method) === false) { |
|
379 | + $cipher_method = $this->getAvailableCipherMethod($cipher_method); |
|
380 | + } |
|
381 | + } |
|
382 | + return $cipher_method; |
|
383 | + } |
|
384 | + |
|
385 | + |
|
386 | + /** |
|
387 | + * @param string $cipher_method |
|
388 | + * @return string |
|
389 | + * @throws \RuntimeException |
|
390 | + */ |
|
391 | + protected function getAvailableCipherMethod($cipher_method) |
|
392 | + { |
|
393 | + // verify that the incoming cipher method can produce an initialization vector |
|
394 | + if (openssl_cipher_iv_length($cipher_method) === false) { |
|
395 | + // nope? then check the next cipher in the list of available cipher methods |
|
396 | + $cipher_method = next($this->cipher_methods); |
|
397 | + // what? there's no list? then generate that list and cache it, |
|
398 | + if (empty($this->cipher_methods)) { |
|
399 | + $this->cipher_methods = openssl_get_cipher_methods(); |
|
400 | + // then grab the first item from the list |
|
401 | + $cipher_method = reset($this->cipher_methods); |
|
402 | + } |
|
403 | + if ($cipher_method === false) { |
|
404 | + throw new RuntimeException( |
|
405 | + esc_html__( |
|
406 | + 'OpenSSL support appears to be enabled on the server, but no cipher methods are available. Please contact the server administrator.', |
|
407 | + 'event_espresso' |
|
408 | + ) |
|
409 | + ); |
|
410 | + } |
|
411 | + // verify that the next cipher method works |
|
412 | + return $this->getAvailableCipherMethod($cipher_method); |
|
413 | + } |
|
414 | + // if we've gotten this far, then we found an available cipher method that works |
|
415 | + // so save that for next time |
|
416 | + update_option( |
|
417 | + EE_Encryption::OPENSSL_CIPHER_METHOD_OPTION_NAME, |
|
418 | + $cipher_method |
|
419 | + ); |
|
420 | + return $cipher_method; |
|
421 | + } |
|
422 | + |
|
423 | + |
|
424 | + /** |
|
425 | + * decrypts data that has been encrypted with PHP's openssl functions |
|
426 | + * |
|
427 | + * @param string $encrypted_text the text to be decrypted |
|
428 | + * @param string $cipher_method |
|
429 | + * @param string $encryption_key |
|
430 | + * @return string |
|
431 | + * @throws RuntimeException |
|
432 | + */ |
|
433 | + protected function openssl_decrypt( |
|
434 | + $encrypted_text = '', |
|
435 | + $cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD, |
|
436 | + $encryption_key = '' |
|
437 | + ) { |
|
438 | + // you give me nothing??? GET OUT ! |
|
439 | + if (empty($encrypted_text)) { |
|
440 | + return $encrypted_text; |
|
441 | + } |
|
442 | + // decode |
|
443 | + $encrypted_text = $this->valid_base_64($encrypted_text) |
|
444 | + ? $this->base64_url_decode($encrypted_text) |
|
445 | + : $encrypted_text; |
|
446 | + $encrypted_components = explode( |
|
447 | + EE_Encryption::OPENSSL_IV_DELIMITER, |
|
448 | + $encrypted_text, |
|
449 | + 2 |
|
450 | + ); |
|
451 | + // check that iv exists, and if not, maybe text was encoded using mcrypt? |
|
452 | + if ($this->_use_mcrypt && ! isset($encrypted_components[1])) { |
|
453 | + return $this->m_decrypt($encrypted_text); |
|
454 | + } |
|
455 | + // decrypt it |
|
456 | + $decrypted_text = openssl_decrypt( |
|
457 | + $encrypted_components[0], |
|
458 | + $this->getCipherMethod($cipher_method), |
|
459 | + $this->getDigestHashValue(EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key), |
|
460 | + 0, |
|
461 | + $encrypted_components[1] |
|
462 | + ); |
|
463 | + $decrypted_text = trim($decrypted_text); |
|
464 | + return $decrypted_text; |
|
465 | + } |
|
466 | + |
|
467 | + |
|
468 | + /** |
|
469 | + * Computes the digest hash value using the specified digest method. |
|
470 | + * If that digest method fails to produce a valid hash value, |
|
471 | + * then we'll grab the next digest method and recursively try again until something works. |
|
472 | + * |
|
473 | + * @param string $digest_method |
|
474 | + * @param string $encryption_key |
|
475 | + * @return string |
|
476 | + * @throws RuntimeException |
|
477 | + */ |
|
478 | + protected function getDigestHashValue($digest_method = EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key = '') |
|
479 | + { |
|
480 | + $encryption_key = $encryption_key !== '' |
|
481 | + ? $encryption_key |
|
482 | + : $this->get_encryption_key(); |
|
483 | + $digest_hash_value = openssl_digest($encryption_key, $digest_method); |
|
484 | + if ($digest_hash_value === false) { |
|
485 | + return $this->getDigestHashValue($this->getDigestMethod()); |
|
486 | + } |
|
487 | + return $digest_hash_value; |
|
488 | + } |
|
489 | + |
|
490 | + |
|
491 | + /** |
|
492 | + * Returns the NEXT element in the $digest_methods array. |
|
493 | + * If the $digest_methods array is empty, then we populate it |
|
494 | + * with the available values returned from openssl_get_md_methods(). |
|
495 | + * |
|
496 | + * @return string |
|
497 | + * @throws \RuntimeException |
|
498 | + */ |
|
499 | + protected function getDigestMethod() |
|
500 | + { |
|
501 | + $digest_method = prev($this->digest_methods); |
|
502 | + if (empty($this->digest_methods)) { |
|
503 | + $this->digest_methods = openssl_get_md_methods(); |
|
504 | + $digest_method = end($this->digest_methods); |
|
505 | + } |
|
506 | + if ($digest_method === false) { |
|
507 | + throw new RuntimeException( |
|
508 | + esc_html__( |
|
509 | + 'OpenSSL support appears to be enabled on the server, but no digest methods are available. Please contact the server administrator.', |
|
510 | + 'event_espresso' |
|
511 | + ) |
|
512 | + ); |
|
513 | + } |
|
514 | + return $digest_method; |
|
515 | + } |
|
516 | + |
|
517 | + |
|
518 | + /** |
|
519 | + * encrypts data for acme servers that didn't bother to install PHP mcrypt |
|
520 | + * |
|
521 | + * @see http://stackoverflow.com/questions/800922/how-to-encrypt-string-without-mcrypt-library-in-php |
|
522 | + * @param string $text_string the text to be decrypted |
|
523 | + * @return string |
|
524 | + */ |
|
525 | + protected function acme_encrypt($text_string = '') |
|
526 | + { |
|
527 | + // you give me nothing??? GET OUT ! |
|
528 | + if (empty($text_string)) { |
|
529 | + return $text_string; |
|
530 | + } |
|
531 | + $key_bits = str_split( |
|
532 | + str_pad( |
|
533 | + '', |
|
534 | + strlen($text_string), |
|
535 | + $this->get_encryption_key(), |
|
536 | + STR_PAD_RIGHT |
|
537 | + ) |
|
538 | + ); |
|
539 | + $string_bits = str_split($text_string); |
|
540 | + foreach ($string_bits as $k => $v) { |
|
541 | + $temp = ord($v) + ord($key_bits[ $k ]); |
|
542 | + $string_bits[ $k ] = chr($temp > 255 ? ($temp - 256) : $temp); |
|
543 | + } |
|
544 | + $encrypted_text = implode('', $string_bits); |
|
545 | + $encrypted_text .= EE_Encryption::ACME_ENCRYPTION_FLAG; |
|
546 | + return $this->_use_base64_encode |
|
547 | + ? base64_encode($encrypted_text) |
|
548 | + : $encrypted_text; |
|
549 | + } |
|
550 | + |
|
551 | + |
|
552 | + /** |
|
553 | + * decrypts data for acme servers that didn't bother to install PHP mcrypt |
|
554 | + * |
|
555 | + * @see http://stackoverflow.com/questions/800922/how-to-encrypt-string-without-mcrypt-library-in-php |
|
556 | + * @param string $encrypted_text the text to be decrypted |
|
557 | + * @return string |
|
558 | + * @throws RuntimeException |
|
559 | + */ |
|
560 | + protected function acme_decrypt($encrypted_text = '') |
|
561 | + { |
|
562 | + // you give me nothing??? GET OUT ! |
|
563 | + if (empty($encrypted_text)) { |
|
564 | + return $encrypted_text; |
|
565 | + } |
|
566 | + // decode the data ? |
|
567 | + $encrypted_text = $this->valid_base_64($encrypted_text) |
|
568 | + ? $this->base64_url_decode($encrypted_text) |
|
569 | + : $encrypted_text; |
|
570 | + if ( |
|
571 | + $this->_use_mcrypt |
|
572 | + && strpos($encrypted_text, EE_Encryption::ACME_ENCRYPTION_FLAG) === false |
|
573 | + ) { |
|
574 | + return $this->m_decrypt($encrypted_text); |
|
575 | + } |
|
576 | + $encrypted_text = substr($encrypted_text, 0, -4); |
|
577 | + $key_bits = str_split( |
|
578 | + str_pad( |
|
579 | + '', |
|
580 | + strlen($encrypted_text), |
|
581 | + $this->get_encryption_key(), |
|
582 | + STR_PAD_RIGHT |
|
583 | + ) |
|
584 | + ); |
|
585 | + $string_bits = str_split($encrypted_text); |
|
586 | + foreach ($string_bits as $k => $v) { |
|
587 | + $temp = ord($v) - ord($key_bits[ $k ]); |
|
588 | + $string_bits[ $k ] = chr($temp < 0 ? ($temp + 256) : $temp); |
|
589 | + } |
|
590 | + return implode('', $string_bits); |
|
591 | + } |
|
592 | + |
|
593 | + |
|
594 | + /** |
|
595 | + * @see http://stackoverflow.com/questions/2556345/detect-base64-encoding-in-php#30231906 |
|
596 | + * @param $string |
|
597 | + * @return bool |
|
598 | + */ |
|
599 | + protected function valid_base_64($string) |
|
600 | + { |
|
601 | + // ensure data is a string |
|
602 | + if (! is_string($string) || ! $this->_use_base64_encode) { |
|
603 | + return false; |
|
604 | + } |
|
605 | + $decoded = base64_decode($string, true); |
|
606 | + // Check if there is no invalid character in string |
|
607 | + if (! preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $string)) { |
|
608 | + return false; |
|
609 | + } |
|
610 | + // Decode the string in strict mode and send the response |
|
611 | + if (! base64_decode($string, true)) { |
|
612 | + return false; |
|
613 | + } |
|
614 | + // Encode and compare it to original one |
|
615 | + return base64_encode($decoded) === $string; |
|
616 | + } |
|
617 | + |
|
618 | + |
|
619 | + /** |
|
620 | + * generate random string |
|
621 | + * |
|
622 | + * @see http://stackoverflow.com/questions/637278/what-is-the-best-way-to-generate-a-random-key-within-php |
|
623 | + * @param int $length number of characters for random string |
|
624 | + * @return string |
|
625 | + */ |
|
626 | + public function generate_random_string($length = 40) |
|
627 | + { |
|
628 | + $iterations = ceil($length / 40); |
|
629 | + $random_string = ''; |
|
630 | + for ($i = 0; $i < $iterations; $i++) { |
|
631 | + $random_string .= sha1(microtime(true) . mt_rand(10000, 90000)); |
|
632 | + } |
|
633 | + $random_string = substr($random_string, 0, $length); |
|
634 | + return $random_string; |
|
635 | + } |
|
636 | + |
|
637 | + |
|
638 | + /** |
|
639 | + * encrypts data using PHP's mcrypt functions |
|
640 | + * |
|
641 | + * @param string $text_string |
|
642 | + * @return string |
|
643 | + * @throws RuntimeException |
|
644 | + * @deprecated 4.9.39 |
|
645 | + * @internal param $string - the text to be encrypted |
|
646 | + */ |
|
647 | + protected function m_encrypt($text_string = '') |
|
648 | + { |
|
649 | + // you give me nothing??? GET OUT ! |
|
650 | + if (empty($text_string)) { |
|
651 | + return $text_string; |
|
652 | + } |
|
653 | + // get the initialization vector size |
|
654 | + $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); |
|
655 | + // initialization vector |
|
656 | + $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); |
|
657 | + if ($iv === false) { |
|
658 | + throw new RuntimeException( |
|
659 | + esc_html__('Failed to generate mcrypt initialization vector.', 'event_espresso') |
|
660 | + ); |
|
661 | + } |
|
662 | + // encrypt it |
|
663 | + $encrypted_text = mcrypt_encrypt( |
|
664 | + MCRYPT_RIJNDAEL_256, |
|
665 | + $this->get_encryption_key(), |
|
666 | + $text_string, |
|
667 | + MCRYPT_MODE_ECB, |
|
668 | + $iv |
|
669 | + ); |
|
670 | + // trim and maybe encode |
|
671 | + return $this->_use_base64_encode |
|
672 | + ? trim(base64_encode($encrypted_text)) |
|
673 | + : trim($encrypted_text); |
|
674 | + } |
|
675 | + |
|
676 | + |
|
677 | + /** |
|
678 | + * decrypts data that has been encrypted with PHP's mcrypt functions |
|
679 | + * |
|
680 | + * @param string $encrypted_text the text to be decrypted |
|
681 | + * @return string |
|
682 | + * @throws RuntimeException |
|
683 | + * @deprecated 4.9.39 |
|
684 | + */ |
|
685 | + protected function m_decrypt($encrypted_text = '') |
|
686 | + { |
|
687 | + // you give me nothing??? GET OUT ! |
|
688 | + if (empty($encrypted_text)) { |
|
689 | + return $encrypted_text; |
|
690 | + } |
|
691 | + // decode |
|
692 | + $encrypted_text = $this->valid_base_64($encrypted_text) |
|
693 | + ? $this->base64_url_decode($encrypted_text) |
|
694 | + : $encrypted_text; |
|
695 | + // get the initialization vector size |
|
696 | + $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); |
|
697 | + $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); |
|
698 | + if ($iv === false) { |
|
699 | + throw new RuntimeException( |
|
700 | + esc_html__('Failed to generate mcrypt initialization vector.', 'event_espresso') |
|
701 | + ); |
|
702 | + } |
|
703 | + // decrypt it |
|
704 | + $decrypted_text = mcrypt_decrypt( |
|
705 | + MCRYPT_RIJNDAEL_256, |
|
706 | + $this->get_encryption_key(), |
|
707 | + $encrypted_text, |
|
708 | + MCRYPT_MODE_ECB, |
|
709 | + $iv |
|
710 | + ); |
|
711 | + $decrypted_text = trim($decrypted_text); |
|
712 | + return $decrypted_text; |
|
713 | + } |
|
714 | 714 | } |
@@ -12,85 +12,85 @@ |
||
12 | 12 | */ |
13 | 13 | interface EncryptionKeyManagerInterface |
14 | 14 | { |
15 | - /** |
|
16 | - * add an encryption key |
|
17 | - * |
|
18 | - * @param string $encryption_key_identifier - name of the encryption key to use |
|
19 | - * @param string $encryption_key - cryptographically secure passphrase. will generate if necessary |
|
20 | - * @param bool $overwrite - prevents accidental overwriting of an existing key which would be bad |
|
21 | - * @return bool |
|
22 | - */ |
|
23 | - public function addEncryptionKey($encryption_key_identifier, $encryption_key = '', $overwrite = false); |
|
24 | - |
|
25 | - |
|
26 | - /** |
|
27 | - * returns true if encryption key has already been generated |
|
28 | - * |
|
29 | - * @param string $encryption_key_identifier - encryption key name |
|
30 | - * @return bool |
|
31 | - */ |
|
32 | - public function encryptionKeyExists($encryption_key_identifier = ''); |
|
33 | - |
|
34 | - |
|
35 | - /** |
|
36 | - * returns cryptographically secure passphrase. will use default if necessary |
|
37 | - * |
|
38 | - * @param string $encryption_key_identifier - encryption key name. will use default if necessary |
|
39 | - * @param bool $generate - will generate a new key if the requested one does not exist |
|
40 | - * @param bool $throw_exception - if TRUE (default), will throw an exception if key is not found |
|
41 | - * @return string |
|
42 | - */ |
|
43 | - public function getEncryptionKey($encryption_key_identifier = '', $generate = false, $throw_exception = true); |
|
44 | - |
|
45 | - |
|
46 | - /** |
|
47 | - * creates a new encryption key |
|
48 | - * |
|
49 | - * @param bool $strong if true (default) will attempt to generate a cryptographically secure key |
|
50 | - * @return string |
|
51 | - */ |
|
52 | - public function generateEncryptionKey($strong = true); |
|
53 | - |
|
54 | - |
|
55 | - /** |
|
56 | - * @return int |
|
57 | - */ |
|
58 | - public function bitDepth(); |
|
59 | - |
|
60 | - |
|
61 | - /** |
|
62 | - * @param int $bit_depth options are 64, 128, 192, or 256 |
|
63 | - */ |
|
64 | - public function setBitDepth($bit_depth); |
|
65 | - |
|
66 | - |
|
67 | - /** |
|
68 | - * @return int |
|
69 | - */ |
|
70 | - public function keyLength(); |
|
71 | - |
|
72 | - |
|
73 | - /** |
|
74 | - * @param int $key_length |
|
75 | - */ |
|
76 | - public function setKeyLength($key_length); |
|
77 | - |
|
78 | - |
|
79 | - /** |
|
80 | - * deletes ALL existing encryption keys from the db |
|
81 | - * |
|
82 | - * @return bool true if keys successfully deleted, false otherwise. |
|
83 | - */ |
|
84 | - public function removeAllEncryptionKeys(); |
|
85 | - |
|
86 | - |
|
87 | - /** |
|
88 | - * deletes an existing encryption key from those saved in the db |
|
89 | - * |
|
90 | - * @param string $encryption_key_identifier encryption key name |
|
91 | - * @return int 1: key removed successfully. |
|
92 | - * 0: key did not exist. |
|
93 | - * -1: failed to remove key |
|
94 | - */ |
|
95 | - public function removeEncryptionKey($encryption_key_identifier = ''); |
|
15 | + /** |
|
16 | + * add an encryption key |
|
17 | + * |
|
18 | + * @param string $encryption_key_identifier - name of the encryption key to use |
|
19 | + * @param string $encryption_key - cryptographically secure passphrase. will generate if necessary |
|
20 | + * @param bool $overwrite - prevents accidental overwriting of an existing key which would be bad |
|
21 | + * @return bool |
|
22 | + */ |
|
23 | + public function addEncryptionKey($encryption_key_identifier, $encryption_key = '', $overwrite = false); |
|
24 | + |
|
25 | + |
|
26 | + /** |
|
27 | + * returns true if encryption key has already been generated |
|
28 | + * |
|
29 | + * @param string $encryption_key_identifier - encryption key name |
|
30 | + * @return bool |
|
31 | + */ |
|
32 | + public function encryptionKeyExists($encryption_key_identifier = ''); |
|
33 | + |
|
34 | + |
|
35 | + /** |
|
36 | + * returns cryptographically secure passphrase. will use default if necessary |
|
37 | + * |
|
38 | + * @param string $encryption_key_identifier - encryption key name. will use default if necessary |
|
39 | + * @param bool $generate - will generate a new key if the requested one does not exist |
|
40 | + * @param bool $throw_exception - if TRUE (default), will throw an exception if key is not found |
|
41 | + * @return string |
|
42 | + */ |
|
43 | + public function getEncryptionKey($encryption_key_identifier = '', $generate = false, $throw_exception = true); |
|
44 | + |
|
45 | + |
|
46 | + /** |
|
47 | + * creates a new encryption key |
|
48 | + * |
|
49 | + * @param bool $strong if true (default) will attempt to generate a cryptographically secure key |
|
50 | + * @return string |
|
51 | + */ |
|
52 | + public function generateEncryptionKey($strong = true); |
|
53 | + |
|
54 | + |
|
55 | + /** |
|
56 | + * @return int |
|
57 | + */ |
|
58 | + public function bitDepth(); |
|
59 | + |
|
60 | + |
|
61 | + /** |
|
62 | + * @param int $bit_depth options are 64, 128, 192, or 256 |
|
63 | + */ |
|
64 | + public function setBitDepth($bit_depth); |
|
65 | + |
|
66 | + |
|
67 | + /** |
|
68 | + * @return int |
|
69 | + */ |
|
70 | + public function keyLength(); |
|
71 | + |
|
72 | + |
|
73 | + /** |
|
74 | + * @param int $key_length |
|
75 | + */ |
|
76 | + public function setKeyLength($key_length); |
|
77 | + |
|
78 | + |
|
79 | + /** |
|
80 | + * deletes ALL existing encryption keys from the db |
|
81 | + * |
|
82 | + * @return bool true if keys successfully deleted, false otherwise. |
|
83 | + */ |
|
84 | + public function removeAllEncryptionKeys(); |
|
85 | + |
|
86 | + |
|
87 | + /** |
|
88 | + * deletes an existing encryption key from those saved in the db |
|
89 | + * |
|
90 | + * @param string $encryption_key_identifier encryption key name |
|
91 | + * @return int 1: key removed successfully. |
|
92 | + * 0: key did not exist. |
|
93 | + * -1: failed to remove key |
|
94 | + */ |
|
95 | + public function removeEncryptionKey($encryption_key_identifier = ''); |
|
96 | 96 | } |
@@ -16,301 +16,301 @@ |
||
16 | 16 | */ |
17 | 17 | class EncryptionKeyManager implements EncryptionKeyManagerInterface |
18 | 18 | { |
19 | - /** |
|
20 | - * @var Base64Encoder |
|
21 | - */ |
|
22 | - protected $base64_encoder; |
|
23 | - |
|
24 | - /** |
|
25 | - * name used for a default encryption key in case no others are set |
|
26 | - * |
|
27 | - * @var string |
|
28 | - */ |
|
29 | - private $default_encryption_key_id; |
|
30 | - |
|
31 | - /** |
|
32 | - * name used for saving encryption keys to the wp_options table |
|
33 | - * |
|
34 | - * @var string |
|
35 | - */ |
|
36 | - private $encryption_keys_option_name; |
|
37 | - |
|
38 | - /** |
|
39 | - * @var array |
|
40 | - */ |
|
41 | - private $encryption_keys = null; |
|
42 | - |
|
43 | - /** |
|
44 | - * number of bits used when generating cryptographically secure keys |
|
45 | - * |
|
46 | - * @var int |
|
47 | - */ |
|
48 | - private $bit_depth = 128; |
|
49 | - |
|
50 | - /** |
|
51 | - * @var int[] |
|
52 | - */ |
|
53 | - private $bit_depth_options = [64, 128, 192, 256]; |
|
54 | - |
|
55 | - /** |
|
56 | - * number of characters used when generating cryptographically weak keys |
|
57 | - * |
|
58 | - * @var int |
|
59 | - */ |
|
60 | - private $key_length = 40; |
|
61 | - |
|
62 | - |
|
63 | - /** |
|
64 | - * @param Base64Encoder $base64_encoder |
|
65 | - * @param string $default_encryption_key_id |
|
66 | - * @param string $encryption_keys_option_name |
|
67 | - */ |
|
68 | - public function __construct(Base64Encoder $base64_encoder, $default_encryption_key_id, $encryption_keys_option_name) |
|
69 | - { |
|
70 | - $this->base64_encoder = $base64_encoder; |
|
71 | - $this->default_encryption_key_id = $default_encryption_key_id; |
|
72 | - $this->encryption_keys_option_name = $encryption_keys_option_name; |
|
73 | - } |
|
74 | - |
|
75 | - |
|
76 | - /** |
|
77 | - * add an encryption key |
|
78 | - * |
|
79 | - * @param string $encryption_key_identifier - name of the encryption key to use |
|
80 | - * @param string $encryption_key - cryptographically secure passphrase. will generate if necessary |
|
81 | - * @param bool $overwrite - prevents accidental overwriting of an existing key which would be bad |
|
82 | - * @return bool |
|
83 | - * @throws Exception |
|
84 | - */ |
|
85 | - public function addEncryptionKey($encryption_key_identifier, $encryption_key = '', $overwrite = false) |
|
86 | - { |
|
87 | - $encryption_key_identifier = $encryption_key_identifier ?: $this->default_encryption_key_id; |
|
88 | - if ($this->encryptionKeyExists($encryption_key_identifier) && ! $overwrite) { |
|
89 | - // WOAH!!! that key already exists and we don't want to overwrite it |
|
90 | - throw new RuntimeException( |
|
91 | - sprintf( |
|
92 | - esc_html__( |
|
93 | - 'The "%1$s" encryption key already exists and can not be overwritten because previously encrypted values would no longer be capable of being decrypted.', |
|
94 | - 'event_espresso' |
|
95 | - ), |
|
96 | - $encryption_key_identifier |
|
97 | - ) |
|
98 | - ); |
|
99 | - } |
|
100 | - $this->encryption_keys[ $encryption_key_identifier ] = $encryption_key ?: $this->generateEncryptionKey(); |
|
101 | - return $this->saveEncryptionKeys(); |
|
102 | - } |
|
103 | - |
|
104 | - |
|
105 | - /** |
|
106 | - * returns true if encryption key has already been generated |
|
107 | - * |
|
108 | - * @param string $encryption_key_identifier - encryption key name |
|
109 | - * @return bool |
|
110 | - * @throws Exception |
|
111 | - * @throws OutOfBoundsException |
|
112 | - */ |
|
113 | - public function encryptionKeyExists($encryption_key_identifier = '') |
|
114 | - { |
|
115 | - // ensure keys are loaded |
|
116 | - $this->retrieveEncryptionKeys(); |
|
117 | - return isset($this->encryption_keys[ $encryption_key_identifier ]); |
|
118 | - } |
|
119 | - |
|
120 | - |
|
121 | - /** |
|
122 | - * returns cryptographically secure passphrase. will use default if necessary |
|
123 | - * |
|
124 | - * @param string $encryption_key_identifier - encryption key name. will use default if necessary |
|
125 | - * @param bool $generate - will generate a new key if the requested one does not exist |
|
126 | - * @param bool $throw_exception - if TRUE (default), will throw an exception if key is not found |
|
127 | - * @return string |
|
128 | - * @throws Exception |
|
129 | - */ |
|
130 | - public function getEncryptionKey($encryption_key_identifier = '', $generate = true, $throw_exception = true) |
|
131 | - { |
|
132 | - $encryption_key_identifier = $encryption_key_identifier ?: $this->default_encryption_key_id; |
|
133 | - // if encryption key has not been set |
|
134 | - if (! $this->encryptionKeyExists($encryption_key_identifier)) { |
|
135 | - if ($generate) { |
|
136 | - $this->addEncryptionKey($encryption_key_identifier); |
|
137 | - } else { |
|
138 | - if (! $throw_exception) { |
|
139 | - return ''; |
|
140 | - } |
|
141 | - throw new OutOfBoundsException( |
|
142 | - sprintf( |
|
143 | - esc_html__('The "%1$s" encryption key was not found or is invalid.', 'event_espresso'), |
|
144 | - $encryption_key_identifier |
|
145 | - ) |
|
146 | - ); |
|
147 | - } |
|
148 | - } |
|
149 | - return $this->encryption_keys[ $encryption_key_identifier ]; |
|
150 | - } |
|
151 | - |
|
152 | - |
|
153 | - /** |
|
154 | - * creates a new encryption key |
|
155 | - * |
|
156 | - * @param bool $strong if true (default) will attempt to generate a cryptographically secure key |
|
157 | - * @return string |
|
158 | - * @throws Exception |
|
159 | - */ |
|
160 | - public function generateEncryptionKey($strong = true) |
|
161 | - { |
|
162 | - return $strong && PHP_VERSION_ID >= 70100 |
|
163 | - ? $this->generateStrongEncryptionKey() |
|
164 | - : $this->generateWeakEncryptionKey(); |
|
165 | - } |
|
166 | - |
|
167 | - |
|
168 | - /** |
|
169 | - * creates a new cryptographically secure encryption key |
|
170 | - * |
|
171 | - * @return string |
|
172 | - * @throws Exception |
|
173 | - */ |
|
174 | - protected function generateStrongEncryptionKey() |
|
175 | - { |
|
176 | - // bit_depth needs to be divided by 8 to convert to bytes |
|
177 | - return $this->base64_encoder->encodeString(random_bytes($this->bit_depth / 8)); |
|
178 | - } |
|
179 | - |
|
180 | - |
|
181 | - /** |
|
182 | - * creates a new encryption key that should not be trusted to be cryptographically secure |
|
183 | - * |
|
184 | - * @return string |
|
185 | - * @throws Exception |
|
186 | - */ |
|
187 | - protected function generateWeakEncryptionKey() |
|
188 | - { |
|
189 | - // @see http://stackoverflow.com/questions/637278/what-is-the-best-way-to-generate-a-random-key-within-php |
|
190 | - $iterations = ceil($this->key_length / 40); |
|
191 | - $random_string = ''; |
|
192 | - for ($i = 0; $i < $iterations; $i++) { |
|
193 | - $random_string .= sha1(microtime(true) . mt_rand(10000, 90000)); |
|
194 | - } |
|
195 | - $random_string = (string) substr($random_string, 0, $this->key_length); |
|
196 | - return $this->base64_encoder->encodeString($random_string); |
|
197 | - } |
|
198 | - |
|
199 | - |
|
200 | - /** |
|
201 | - * @return int |
|
202 | - */ |
|
203 | - public function bitDepth() |
|
204 | - { |
|
205 | - return $this->bit_depth; |
|
206 | - } |
|
207 | - |
|
208 | - |
|
209 | - /** |
|
210 | - * @param int $bit_depth options are 64, 128, 192, or 256 |
|
211 | - */ |
|
212 | - public function setBitDepth($bit_depth) |
|
213 | - { |
|
214 | - $bit_depth = absint($bit_depth); |
|
215 | - $this->bit_depth = in_array($bit_depth, $this->bit_depth_options, true) ? $bit_depth : 128; |
|
216 | - } |
|
217 | - |
|
218 | - |
|
219 | - /** |
|
220 | - * @return int |
|
221 | - */ |
|
222 | - public function keyLength() |
|
223 | - { |
|
224 | - return $this->key_length; |
|
225 | - } |
|
226 | - |
|
227 | - |
|
228 | - /** |
|
229 | - * @param int $key_length |
|
230 | - */ |
|
231 | - public function setKeyLength($key_length) |
|
232 | - { |
|
233 | - // let's not let the key length go below 8 or above 128 |
|
234 | - $this->key_length = min(max(absint($key_length), 8), 128); |
|
235 | - } |
|
236 | - |
|
237 | - |
|
238 | - /** |
|
239 | - * deletes ALL existing encryption keys from the db |
|
240 | - * |
|
241 | - * @return bool true if keys successfully deleted, false otherwise. |
|
242 | - */ |
|
243 | - public function removeAllEncryptionKeys() |
|
244 | - { |
|
245 | - return delete_option($this->encryption_keys_option_name); |
|
246 | - } |
|
247 | - |
|
248 | - |
|
249 | - /** |
|
250 | - * deletes an existing encryption key from those saved in the db |
|
251 | - * |
|
252 | - * @param string $encryption_key_identifier encryption key name |
|
253 | - * @return int 1: key removed successfully. |
|
254 | - * 0: key did not exist. |
|
255 | - * -1: failed to remove key |
|
256 | - * @throws Exception |
|
257 | - */ |
|
258 | - public function removeEncryptionKey($encryption_key_identifier = '') |
|
259 | - { |
|
260 | - // if encryption key has not been set |
|
261 | - if (! $this->encryptionKeyExists($encryption_key_identifier)) { |
|
262 | - return 0; |
|
263 | - } |
|
264 | - unset($this->encryption_keys[ $encryption_key_identifier ]); |
|
265 | - return $this->saveEncryptionKeys() ? 1 : -1; |
|
266 | - } |
|
267 | - |
|
268 | - |
|
269 | - /** |
|
270 | - * retrieves encryption keys from db |
|
271 | - * |
|
272 | - * @return array |
|
273 | - * @throws Exception |
|
274 | - * @throws RuntimeException |
|
275 | - */ |
|
276 | - protected function retrieveEncryptionKeys() |
|
277 | - { |
|
278 | - // if encryption key has not been set |
|
279 | - if (empty($this->encryption_keys)) { |
|
280 | - // retrieve encryption_key from db |
|
281 | - $this->encryption_keys = get_option($this->encryption_keys_option_name, null); |
|
282 | - // WHAT?? No encryption keys in the db ?? |
|
283 | - if ($this->encryption_keys === null) { |
|
284 | - $this->encryption_keys = []; |
|
285 | - // let's create the default key and save it |
|
286 | - $new_key = $this->generateEncryptionKey(); |
|
287 | - $this->encryption_keys[ $this->default_encryption_key_id ] = $new_key; |
|
288 | - if (! $this->saveEncryptionKeys(true)) { |
|
289 | - throw new RuntimeException( |
|
290 | - sprintf( |
|
291 | - esc_html__( |
|
292 | - 'Failed to save the "%1$s" encryption keys array to the database.', |
|
293 | - 'event_espresso' |
|
294 | - ), |
|
295 | - $this->encryption_keys_option_name |
|
296 | - ) |
|
297 | - ); |
|
298 | - } |
|
299 | - } |
|
300 | - } |
|
301 | - return $this->encryption_keys; |
|
302 | - } |
|
303 | - |
|
304 | - |
|
305 | - /** |
|
306 | - * saves encryption keys from db |
|
307 | - * |
|
308 | - * @return bool |
|
309 | - */ |
|
310 | - protected function saveEncryptionKeys($initialize = false) |
|
311 | - { |
|
312 | - return $initialize |
|
313 | - ? add_option($this->encryption_keys_option_name, $this->encryption_keys, '', false) |
|
314 | - : update_option($this->encryption_keys_option_name, $this->encryption_keys, false); |
|
315 | - } |
|
19 | + /** |
|
20 | + * @var Base64Encoder |
|
21 | + */ |
|
22 | + protected $base64_encoder; |
|
23 | + |
|
24 | + /** |
|
25 | + * name used for a default encryption key in case no others are set |
|
26 | + * |
|
27 | + * @var string |
|
28 | + */ |
|
29 | + private $default_encryption_key_id; |
|
30 | + |
|
31 | + /** |
|
32 | + * name used for saving encryption keys to the wp_options table |
|
33 | + * |
|
34 | + * @var string |
|
35 | + */ |
|
36 | + private $encryption_keys_option_name; |
|
37 | + |
|
38 | + /** |
|
39 | + * @var array |
|
40 | + */ |
|
41 | + private $encryption_keys = null; |
|
42 | + |
|
43 | + /** |
|
44 | + * number of bits used when generating cryptographically secure keys |
|
45 | + * |
|
46 | + * @var int |
|
47 | + */ |
|
48 | + private $bit_depth = 128; |
|
49 | + |
|
50 | + /** |
|
51 | + * @var int[] |
|
52 | + */ |
|
53 | + private $bit_depth_options = [64, 128, 192, 256]; |
|
54 | + |
|
55 | + /** |
|
56 | + * number of characters used when generating cryptographically weak keys |
|
57 | + * |
|
58 | + * @var int |
|
59 | + */ |
|
60 | + private $key_length = 40; |
|
61 | + |
|
62 | + |
|
63 | + /** |
|
64 | + * @param Base64Encoder $base64_encoder |
|
65 | + * @param string $default_encryption_key_id |
|
66 | + * @param string $encryption_keys_option_name |
|
67 | + */ |
|
68 | + public function __construct(Base64Encoder $base64_encoder, $default_encryption_key_id, $encryption_keys_option_name) |
|
69 | + { |
|
70 | + $this->base64_encoder = $base64_encoder; |
|
71 | + $this->default_encryption_key_id = $default_encryption_key_id; |
|
72 | + $this->encryption_keys_option_name = $encryption_keys_option_name; |
|
73 | + } |
|
74 | + |
|
75 | + |
|
76 | + /** |
|
77 | + * add an encryption key |
|
78 | + * |
|
79 | + * @param string $encryption_key_identifier - name of the encryption key to use |
|
80 | + * @param string $encryption_key - cryptographically secure passphrase. will generate if necessary |
|
81 | + * @param bool $overwrite - prevents accidental overwriting of an existing key which would be bad |
|
82 | + * @return bool |
|
83 | + * @throws Exception |
|
84 | + */ |
|
85 | + public function addEncryptionKey($encryption_key_identifier, $encryption_key = '', $overwrite = false) |
|
86 | + { |
|
87 | + $encryption_key_identifier = $encryption_key_identifier ?: $this->default_encryption_key_id; |
|
88 | + if ($this->encryptionKeyExists($encryption_key_identifier) && ! $overwrite) { |
|
89 | + // WOAH!!! that key already exists and we don't want to overwrite it |
|
90 | + throw new RuntimeException( |
|
91 | + sprintf( |
|
92 | + esc_html__( |
|
93 | + 'The "%1$s" encryption key already exists and can not be overwritten because previously encrypted values would no longer be capable of being decrypted.', |
|
94 | + 'event_espresso' |
|
95 | + ), |
|
96 | + $encryption_key_identifier |
|
97 | + ) |
|
98 | + ); |
|
99 | + } |
|
100 | + $this->encryption_keys[ $encryption_key_identifier ] = $encryption_key ?: $this->generateEncryptionKey(); |
|
101 | + return $this->saveEncryptionKeys(); |
|
102 | + } |
|
103 | + |
|
104 | + |
|
105 | + /** |
|
106 | + * returns true if encryption key has already been generated |
|
107 | + * |
|
108 | + * @param string $encryption_key_identifier - encryption key name |
|
109 | + * @return bool |
|
110 | + * @throws Exception |
|
111 | + * @throws OutOfBoundsException |
|
112 | + */ |
|
113 | + public function encryptionKeyExists($encryption_key_identifier = '') |
|
114 | + { |
|
115 | + // ensure keys are loaded |
|
116 | + $this->retrieveEncryptionKeys(); |
|
117 | + return isset($this->encryption_keys[ $encryption_key_identifier ]); |
|
118 | + } |
|
119 | + |
|
120 | + |
|
121 | + /** |
|
122 | + * returns cryptographically secure passphrase. will use default if necessary |
|
123 | + * |
|
124 | + * @param string $encryption_key_identifier - encryption key name. will use default if necessary |
|
125 | + * @param bool $generate - will generate a new key if the requested one does not exist |
|
126 | + * @param bool $throw_exception - if TRUE (default), will throw an exception if key is not found |
|
127 | + * @return string |
|
128 | + * @throws Exception |
|
129 | + */ |
|
130 | + public function getEncryptionKey($encryption_key_identifier = '', $generate = true, $throw_exception = true) |
|
131 | + { |
|
132 | + $encryption_key_identifier = $encryption_key_identifier ?: $this->default_encryption_key_id; |
|
133 | + // if encryption key has not been set |
|
134 | + if (! $this->encryptionKeyExists($encryption_key_identifier)) { |
|
135 | + if ($generate) { |
|
136 | + $this->addEncryptionKey($encryption_key_identifier); |
|
137 | + } else { |
|
138 | + if (! $throw_exception) { |
|
139 | + return ''; |
|
140 | + } |
|
141 | + throw new OutOfBoundsException( |
|
142 | + sprintf( |
|
143 | + esc_html__('The "%1$s" encryption key was not found or is invalid.', 'event_espresso'), |
|
144 | + $encryption_key_identifier |
|
145 | + ) |
|
146 | + ); |
|
147 | + } |
|
148 | + } |
|
149 | + return $this->encryption_keys[ $encryption_key_identifier ]; |
|
150 | + } |
|
151 | + |
|
152 | + |
|
153 | + /** |
|
154 | + * creates a new encryption key |
|
155 | + * |
|
156 | + * @param bool $strong if true (default) will attempt to generate a cryptographically secure key |
|
157 | + * @return string |
|
158 | + * @throws Exception |
|
159 | + */ |
|
160 | + public function generateEncryptionKey($strong = true) |
|
161 | + { |
|
162 | + return $strong && PHP_VERSION_ID >= 70100 |
|
163 | + ? $this->generateStrongEncryptionKey() |
|
164 | + : $this->generateWeakEncryptionKey(); |
|
165 | + } |
|
166 | + |
|
167 | + |
|
168 | + /** |
|
169 | + * creates a new cryptographically secure encryption key |
|
170 | + * |
|
171 | + * @return string |
|
172 | + * @throws Exception |
|
173 | + */ |
|
174 | + protected function generateStrongEncryptionKey() |
|
175 | + { |
|
176 | + // bit_depth needs to be divided by 8 to convert to bytes |
|
177 | + return $this->base64_encoder->encodeString(random_bytes($this->bit_depth / 8)); |
|
178 | + } |
|
179 | + |
|
180 | + |
|
181 | + /** |
|
182 | + * creates a new encryption key that should not be trusted to be cryptographically secure |
|
183 | + * |
|
184 | + * @return string |
|
185 | + * @throws Exception |
|
186 | + */ |
|
187 | + protected function generateWeakEncryptionKey() |
|
188 | + { |
|
189 | + // @see http://stackoverflow.com/questions/637278/what-is-the-best-way-to-generate-a-random-key-within-php |
|
190 | + $iterations = ceil($this->key_length / 40); |
|
191 | + $random_string = ''; |
|
192 | + for ($i = 0; $i < $iterations; $i++) { |
|
193 | + $random_string .= sha1(microtime(true) . mt_rand(10000, 90000)); |
|
194 | + } |
|
195 | + $random_string = (string) substr($random_string, 0, $this->key_length); |
|
196 | + return $this->base64_encoder->encodeString($random_string); |
|
197 | + } |
|
198 | + |
|
199 | + |
|
200 | + /** |
|
201 | + * @return int |
|
202 | + */ |
|
203 | + public function bitDepth() |
|
204 | + { |
|
205 | + return $this->bit_depth; |
|
206 | + } |
|
207 | + |
|
208 | + |
|
209 | + /** |
|
210 | + * @param int $bit_depth options are 64, 128, 192, or 256 |
|
211 | + */ |
|
212 | + public function setBitDepth($bit_depth) |
|
213 | + { |
|
214 | + $bit_depth = absint($bit_depth); |
|
215 | + $this->bit_depth = in_array($bit_depth, $this->bit_depth_options, true) ? $bit_depth : 128; |
|
216 | + } |
|
217 | + |
|
218 | + |
|
219 | + /** |
|
220 | + * @return int |
|
221 | + */ |
|
222 | + public function keyLength() |
|
223 | + { |
|
224 | + return $this->key_length; |
|
225 | + } |
|
226 | + |
|
227 | + |
|
228 | + /** |
|
229 | + * @param int $key_length |
|
230 | + */ |
|
231 | + public function setKeyLength($key_length) |
|
232 | + { |
|
233 | + // let's not let the key length go below 8 or above 128 |
|
234 | + $this->key_length = min(max(absint($key_length), 8), 128); |
|
235 | + } |
|
236 | + |
|
237 | + |
|
238 | + /** |
|
239 | + * deletes ALL existing encryption keys from the db |
|
240 | + * |
|
241 | + * @return bool true if keys successfully deleted, false otherwise. |
|
242 | + */ |
|
243 | + public function removeAllEncryptionKeys() |
|
244 | + { |
|
245 | + return delete_option($this->encryption_keys_option_name); |
|
246 | + } |
|
247 | + |
|
248 | + |
|
249 | + /** |
|
250 | + * deletes an existing encryption key from those saved in the db |
|
251 | + * |
|
252 | + * @param string $encryption_key_identifier encryption key name |
|
253 | + * @return int 1: key removed successfully. |
|
254 | + * 0: key did not exist. |
|
255 | + * -1: failed to remove key |
|
256 | + * @throws Exception |
|
257 | + */ |
|
258 | + public function removeEncryptionKey($encryption_key_identifier = '') |
|
259 | + { |
|
260 | + // if encryption key has not been set |
|
261 | + if (! $this->encryptionKeyExists($encryption_key_identifier)) { |
|
262 | + return 0; |
|
263 | + } |
|
264 | + unset($this->encryption_keys[ $encryption_key_identifier ]); |
|
265 | + return $this->saveEncryptionKeys() ? 1 : -1; |
|
266 | + } |
|
267 | + |
|
268 | + |
|
269 | + /** |
|
270 | + * retrieves encryption keys from db |
|
271 | + * |
|
272 | + * @return array |
|
273 | + * @throws Exception |
|
274 | + * @throws RuntimeException |
|
275 | + */ |
|
276 | + protected function retrieveEncryptionKeys() |
|
277 | + { |
|
278 | + // if encryption key has not been set |
|
279 | + if (empty($this->encryption_keys)) { |
|
280 | + // retrieve encryption_key from db |
|
281 | + $this->encryption_keys = get_option($this->encryption_keys_option_name, null); |
|
282 | + // WHAT?? No encryption keys in the db ?? |
|
283 | + if ($this->encryption_keys === null) { |
|
284 | + $this->encryption_keys = []; |
|
285 | + // let's create the default key and save it |
|
286 | + $new_key = $this->generateEncryptionKey(); |
|
287 | + $this->encryption_keys[ $this->default_encryption_key_id ] = $new_key; |
|
288 | + if (! $this->saveEncryptionKeys(true)) { |
|
289 | + throw new RuntimeException( |
|
290 | + sprintf( |
|
291 | + esc_html__( |
|
292 | + 'Failed to save the "%1$s" encryption keys array to the database.', |
|
293 | + 'event_espresso' |
|
294 | + ), |
|
295 | + $this->encryption_keys_option_name |
|
296 | + ) |
|
297 | + ); |
|
298 | + } |
|
299 | + } |
|
300 | + } |
|
301 | + return $this->encryption_keys; |
|
302 | + } |
|
303 | + |
|
304 | + |
|
305 | + /** |
|
306 | + * saves encryption keys from db |
|
307 | + * |
|
308 | + * @return bool |
|
309 | + */ |
|
310 | + protected function saveEncryptionKeys($initialize = false) |
|
311 | + { |
|
312 | + return $initialize |
|
313 | + ? add_option($this->encryption_keys_option_name, $this->encryption_keys, '', false) |
|
314 | + : update_option($this->encryption_keys_option_name, $this->encryption_keys, false); |
|
315 | + } |
|
316 | 316 | } |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | ) |
98 | 98 | ); |
99 | 99 | } |
100 | - $this->encryption_keys[ $encryption_key_identifier ] = $encryption_key ?: $this->generateEncryptionKey(); |
|
100 | + $this->encryption_keys[$encryption_key_identifier] = $encryption_key ?: $this->generateEncryptionKey(); |
|
101 | 101 | return $this->saveEncryptionKeys(); |
102 | 102 | } |
103 | 103 | |
@@ -114,7 +114,7 @@ discard block |
||
114 | 114 | { |
115 | 115 | // ensure keys are loaded |
116 | 116 | $this->retrieveEncryptionKeys(); |
117 | - return isset($this->encryption_keys[ $encryption_key_identifier ]); |
|
117 | + return isset($this->encryption_keys[$encryption_key_identifier]); |
|
118 | 118 | } |
119 | 119 | |
120 | 120 | |
@@ -131,11 +131,11 @@ discard block |
||
131 | 131 | { |
132 | 132 | $encryption_key_identifier = $encryption_key_identifier ?: $this->default_encryption_key_id; |
133 | 133 | // if encryption key has not been set |
134 | - if (! $this->encryptionKeyExists($encryption_key_identifier)) { |
|
134 | + if ( ! $this->encryptionKeyExists($encryption_key_identifier)) { |
|
135 | 135 | if ($generate) { |
136 | 136 | $this->addEncryptionKey($encryption_key_identifier); |
137 | 137 | } else { |
138 | - if (! $throw_exception) { |
|
138 | + if ( ! $throw_exception) { |
|
139 | 139 | return ''; |
140 | 140 | } |
141 | 141 | throw new OutOfBoundsException( |
@@ -146,7 +146,7 @@ discard block |
||
146 | 146 | ); |
147 | 147 | } |
148 | 148 | } |
149 | - return $this->encryption_keys[ $encryption_key_identifier ]; |
|
149 | + return $this->encryption_keys[$encryption_key_identifier]; |
|
150 | 150 | } |
151 | 151 | |
152 | 152 | |
@@ -190,7 +190,7 @@ discard block |
||
190 | 190 | $iterations = ceil($this->key_length / 40); |
191 | 191 | $random_string = ''; |
192 | 192 | for ($i = 0; $i < $iterations; $i++) { |
193 | - $random_string .= sha1(microtime(true) . mt_rand(10000, 90000)); |
|
193 | + $random_string .= sha1(microtime(true).mt_rand(10000, 90000)); |
|
194 | 194 | } |
195 | 195 | $random_string = (string) substr($random_string, 0, $this->key_length); |
196 | 196 | return $this->base64_encoder->encodeString($random_string); |
@@ -258,10 +258,10 @@ discard block |
||
258 | 258 | public function removeEncryptionKey($encryption_key_identifier = '') |
259 | 259 | { |
260 | 260 | // if encryption key has not been set |
261 | - if (! $this->encryptionKeyExists($encryption_key_identifier)) { |
|
261 | + if ( ! $this->encryptionKeyExists($encryption_key_identifier)) { |
|
262 | 262 | return 0; |
263 | 263 | } |
264 | - unset($this->encryption_keys[ $encryption_key_identifier ]); |
|
264 | + unset($this->encryption_keys[$encryption_key_identifier]); |
|
265 | 265 | return $this->saveEncryptionKeys() ? 1 : -1; |
266 | 266 | } |
267 | 267 | |
@@ -284,8 +284,8 @@ discard block |
||
284 | 284 | $this->encryption_keys = []; |
285 | 285 | // let's create the default key and save it |
286 | 286 | $new_key = $this->generateEncryptionKey(); |
287 | - $this->encryption_keys[ $this->default_encryption_key_id ] = $new_key; |
|
288 | - if (! $this->saveEncryptionKeys(true)) { |
|
287 | + $this->encryption_keys[$this->default_encryption_key_id] = $new_key; |
|
288 | + if ( ! $this->saveEncryptionKeys(true)) { |
|
289 | 289 | throw new RuntimeException( |
290 | 290 | sprintf( |
291 | 291 | esc_html__( |
@@ -3,12 +3,12 @@ discard block |
||
3 | 3 | |
4 | 4 | global $post; |
5 | 5 | |
6 | -do_action( 'AHEE__content_espresso_venues_template__before_featured_img', $post ); |
|
6 | +do_action('AHEE__content_espresso_venues_template__before_featured_img', $post); |
|
7 | 7 | |
8 | -if ( has_post_thumbnail( $post->ID )) : |
|
9 | - if ( $img_ID = get_post_thumbnail_id( $post->ID )) : |
|
10 | - if ( $featured_img = wp_get_attachment_image_src( $img_ID, 'large' )) : |
|
11 | - $caption = get_post( get_post( $img_ID ))->post_excerpt; |
|
8 | +if (has_post_thumbnail($post->ID)) : |
|
9 | + if ($img_ID = get_post_thumbnail_id($post->ID)) : |
|
10 | + if ($featured_img = wp_get_attachment_image_src($img_ID, 'large')) : |
|
11 | + $caption = get_post(get_post($img_ID))->post_excerpt; |
|
12 | 12 | $wrap_class .= ' has-img'; |
13 | 13 | ?> |
14 | 14 | <div id="ee-venue-img-dv-<?php echo esc_attr($post->ID); ?>" class="ee-venue-img-dv"> |
@@ -26,5 +26,5 @@ discard block |
||
26 | 26 | endif; |
27 | 27 | endif; |
28 | 28 | ?> |
29 | -<?php do_action( 'AHEE__content_espresso_venues_template__after_featured_img', $post );?> |
|
29 | +<?php do_action('AHEE__content_espresso_venues_template__after_featured_img', $post); ?> |
|
30 | 30 | <!-- .venue-content --> |
@@ -13,20 +13,20 @@ |
||
13 | 13 | <?php if ( ! post_password_required() && ( comments_open() || get_comments_number() ) ) : ?> |
14 | 14 | <span class="comments-link"> |
15 | 15 | <?php comments_popup_link( |
16 | - esc_html__( 'Leave a comment', 'event_espresso' ), |
|
17 | - esc_html__( '1 Comment', 'event_espresso' ), |
|
18 | - esc_html__( '% Comments', 'event_espresso' ) |
|
19 | - ); ?> |
|
16 | + esc_html__( 'Leave a comment', 'event_espresso' ), |
|
17 | + esc_html__( '1 Comment', 'event_espresso' ), |
|
18 | + esc_html__( '% Comments', 'event_espresso' ) |
|
19 | + ); ?> |
|
20 | 20 | </span> |
21 | 21 | |
22 | 22 | <?php |
23 | - endif; |
|
24 | - edit_post_link( |
|
25 | - esc_html__( 'Edit', 'event_espresso' ), |
|
26 | - '<span class="edit-link">', |
|
27 | - '</span>' |
|
28 | - ); |
|
29 | - ?> |
|
23 | + endif; |
|
24 | + edit_post_link( |
|
25 | + esc_html__( 'Edit', 'event_espresso' ), |
|
26 | + '<span class="edit-link">', |
|
27 | + '</span>' |
|
28 | + ); |
|
29 | + ?> |
|
30 | 30 | </div> |
31 | 31 | |
32 | 32 | <?php endif; |
@@ -4,27 +4,27 @@ discard block |
||
4 | 4 | <div class="event-content"> |
5 | 5 | <?php use EventEspresso\core\services\request\sanitizers\AllowedTags; |
6 | 6 | |
7 | -if ( apply_filters( 'FHEE__content_espresso_events_details_template__display_entry_meta', TRUE )): ?> |
|
7 | +if (apply_filters('FHEE__content_espresso_events_details_template__display_entry_meta', TRUE)): ?> |
|
8 | 8 | |
9 | 9 | <div class="entry-meta"> |
10 | 10 | |
11 | 11 | <span class="tags-links"> |
12 | - <?php espresso_event_categories( $post->ID, TRUE, TRUE ); ?> |
|
12 | + <?php espresso_event_categories($post->ID, TRUE, TRUE); ?> |
|
13 | 13 | </span> |
14 | 14 | |
15 | - <?php if ( ! post_password_required() && ( comments_open() || get_comments_number() ) ) : ?> |
|
15 | + <?php if ( ! post_password_required() && (comments_open() || get_comments_number())) : ?> |
|
16 | 16 | <span class="comments-link"> |
17 | 17 | <?php comments_popup_link( |
18 | - esc_html__( 'Leave a comment', 'event_espresso' ), |
|
19 | - esc_html__( '1 Comment', 'event_espresso' ), |
|
20 | - esc_html__( '% Comments', 'event_espresso' ) |
|
18 | + esc_html__('Leave a comment', 'event_espresso'), |
|
19 | + esc_html__('1 Comment', 'event_espresso'), |
|
20 | + esc_html__('% Comments', 'event_espresso') |
|
21 | 21 | ); ?> |
22 | 22 | </span> |
23 | 23 | |
24 | 24 | <?php |
25 | 25 | endif; |
26 | 26 | edit_post_link( |
27 | - esc_html__( 'Edit', 'event_espresso' ), |
|
27 | + esc_html__('Edit', 'event_espresso'), |
|
28 | 28 | '<span class="edit-link">', |
29 | 29 | '</span>' |
30 | 30 | ); |
@@ -32,25 +32,25 @@ discard block |
||
32 | 32 | </div> |
33 | 33 | |
34 | 34 | <?php endif; |
35 | - $event_phone = espresso_event_phone( $post->ID, FALSE ); |
|
35 | + $event_phone = espresso_event_phone($post->ID, FALSE); |
|
36 | 36 | |
37 | - if ( $event_phone != '' ) : ?> |
|
37 | + if ($event_phone != '') : ?> |
|
38 | 38 | <p class="event-phone"> |
39 | 39 | <span class="small-text"> |
40 | - <strong><?php esc_html_e( 'Event Phone:', 'event_espresso' ); ?> </strong> |
|
40 | + <strong><?php esc_html_e('Event Phone:', 'event_espresso'); ?> </strong> |
|
41 | 41 | </span> |
42 | 42 | <?php echo wp_kses($event_phone, AllowedTags::getAllowedTags()); ?> |
43 | 43 | </p> |
44 | -<?php endif; ?> |
|
44 | +<?php endif; ?> |
|
45 | 45 | |
46 | 46 | <?php |
47 | - if ( apply_filters( 'FHEE__content_espresso_events_details_template__display_the_content', true ) ) { |
|
48 | - do_action( 'AHEE_event_details_before_the_content', $post ); |
|
47 | + if (apply_filters('FHEE__content_espresso_events_details_template__display_the_content', true)) { |
|
48 | + do_action('AHEE_event_details_before_the_content', $post); |
|
49 | 49 | echo apply_filters( |
50 | 50 | 'FHEE__content_espresso_events_details_template__the_content', |
51 | - espresso_event_content_or_excerpt( 55, null, false ) |
|
51 | + espresso_event_content_or_excerpt(55, null, false) |
|
52 | 52 | ); |
53 | - do_action( 'AHEE_event_details_after_the_content', $post ); |
|
53 | + do_action('AHEE_event_details_after_the_content', $post); |
|
54 | 54 | } |
55 | 55 | ?> |
56 | 56 | </div> |
@@ -1,16 +1,16 @@ |
||
1 | 1 | <?php //echo '<h1>' . __FILE__ . '</h1>'; |
2 | 2 | global $post; |
3 | -if ( espresso_venue_has_address( $post->ID )) : |
|
3 | +if (espresso_venue_has_address($post->ID)) : |
|
4 | 4 | ?> |
5 | 5 | <div class="venue-location"> |
6 | 6 | |
7 | 7 | <h3 class="venue-address-h3 ee-venue-h3"> |
8 | - <span class="dashicons dashicons-location-alt"></span><?php esc_html_e( 'Location', 'event_espresso' ); ?> |
|
8 | + <span class="dashicons dashicons-location-alt"></span><?php esc_html_e('Location', 'event_espresso'); ?> |
|
9 | 9 | </h3> |
10 | - <span class="small-text"><strong><?php esc_html_e( 'Address:', 'event_espresso' ); ?></strong></span><?php espresso_venue_address( 'inline', $post->ID ); ?> |
|
10 | + <span class="small-text"><strong><?php esc_html_e('Address:', 'event_espresso'); ?></strong></span><?php espresso_venue_address('inline', $post->ID); ?> |
|
11 | 11 | <div class="clear"></div> |
12 | 12 | |
13 | - <div class="venue-gmap"><?php espresso_venue_gmap( $post->ID ); ?></div> |
|
13 | + <div class="venue-gmap"><?php espresso_venue_gmap($post->ID); ?></div> |
|
14 | 14 | <div class="clear"></div> |
15 | 15 | |
16 | 16 | </div> |
@@ -51,8 +51,11 @@ discard block |
||
51 | 51 | <?php echo do_shortcode( $venue_description ); ?> |
52 | 52 | </p> |
53 | 53 | <?php endif; ?> |
54 | - <?php else : ?> |
|
55 | - <?php $venue_excerpt = espresso_venue_excerpt( $VNU_ID, FALSE ); ?> |
|
54 | + <?php else { |
|
55 | + : ?> |
|
56 | + <?php $venue_excerpt = espresso_venue_excerpt( $VNU_ID, FALSE ); |
|
57 | +} |
|
58 | +?> |
|
56 | 59 | <?php if ( $venue_excerpt ) : ?> |
57 | 60 | <p> |
58 | 61 | <strong><?php esc_html_e( 'Description:', 'event_espresso' ); ?></strong><br/> |
@@ -64,12 +67,15 @@ discard block |
||
64 | 67 | <!-- .espresso-venue-dv --> |
65 | 68 | <?php |
66 | 69 | do_action( 'AHEE_event_details_after_venue_details', $post ); |
67 | -else : |
|
70 | +else { |
|
71 | + : |
|
68 | 72 | if ( espresso_venue_is_password_protected() ) : |
69 | 73 | ?> |
70 | 74 | <div class="espresso-venue-dv espresso-password-protected-venue-dv" > |
71 | 75 | <h3 class="event-venues-h3 ee-event-h3"> |
72 | - <?php esc_html_e( 'Location', 'event_espresso' );?> |
|
76 | + <?php esc_html_e( 'Location', 'event_espresso' ); |
|
77 | +} |
|
78 | +?> |
|
73 | 79 | </h3> |
74 | 80 | <?php echo espresso_password_protected_venue_form(); ?> |
75 | 81 | </div> |
@@ -3,75 +3,75 @@ |
||
3 | 3 | use EventEspresso\core\services\request\sanitizers\AllowedTags; |
4 | 4 | |
5 | 5 | if ( |
6 | - ( is_single() && espresso_display_venue_in_event_details() ) |
|
7 | - || ( is_archive() && espresso_display_venue_in_event_list() ) |
|
6 | + (is_single() && espresso_display_venue_in_event_details()) |
|
7 | + || (is_archive() && espresso_display_venue_in_event_list()) |
|
8 | 8 | ) : |
9 | 9 | global $post; |
10 | - do_action( 'AHEE_event_details_before_venue_details', $post ); |
|
11 | - $venue_name = espresso_venue_name( 0, 'details', FALSE ); |
|
12 | - if ( empty( $venue_name ) && espresso_is_venue_private() ) { |
|
13 | - do_action( 'AHEE_event_details_after_venue_details', $post ); |
|
10 | + do_action('AHEE_event_details_before_venue_details', $post); |
|
11 | + $venue_name = espresso_venue_name(0, 'details', FALSE); |
|
12 | + if (empty($venue_name) && espresso_is_venue_private()) { |
|
13 | + do_action('AHEE_event_details_after_venue_details', $post); |
|
14 | 14 | return ''; |
15 | 15 | } |
16 | 16 | ?> |
17 | 17 | |
18 | 18 | <div class="espresso-venue-dv<?php echo espresso_is_venue_private() ? ' espresso-private-venue-dv' : ''; ?>"> |
19 | 19 | <h4> |
20 | - <strong><?php esc_html_e( 'Venue:', 'event_espresso' ); ?></strong> |
|
20 | + <strong><?php esc_html_e('Venue:', 'event_espresso'); ?></strong> |
|
21 | 21 | <strong> <?php echo wp_kses($venue_name, AllowedTags::getAllowedTags()); ?></strong> |
22 | 22 | </h4> |
23 | 23 | <p><span class="smaller-text tags-links"><?php echo espresso_venue_categories(); ?></span></p> |
24 | -<?php if ( $venue_phone = espresso_venue_phone( $post->ID, FALSE )) : ?> |
|
24 | +<?php if ($venue_phone = espresso_venue_phone($post->ID, FALSE)) : ?> |
|
25 | 25 | <p> |
26 | 26 | <span class="small-text"> |
27 | - <strong><?php esc_html_e( 'Venue Phone:', 'event_espresso' ); ?></strong> |
|
27 | + <strong><?php esc_html_e('Venue Phone:', 'event_espresso'); ?></strong> |
|
28 | 28 | </span> |
29 | 29 | <?php echo wp_kses($venue_phone, AllowedTags::getAllowedTags()); ?> |
30 | 30 | </p> |
31 | -<?php endif; ?> |
|
32 | -<?php if ( $venue_website = espresso_venue_website( $post->ID, FALSE )) : ?> |
|
31 | +<?php endif; ?> |
|
32 | +<?php if ($venue_website = espresso_venue_website($post->ID, FALSE)) : ?> |
|
33 | 33 | <p> |
34 | 34 | <span class="small-text"> |
35 | - <strong><?php esc_html_e( 'Venue Website:', 'event_espresso' ); ?></strong> |
|
35 | + <strong><?php esc_html_e('Venue Website:', 'event_espresso'); ?></strong> |
|
36 | 36 | </span> |
37 | 37 | <?php echo wp_kses($venue_website, AllowedTags::getAllowedTags()); ?> |
38 | 38 | </p> |
39 | 39 | <?php endif; ?> |
40 | -<?php if ( espresso_venue_has_address( $post->ID )) : ?> |
|
41 | - <strong><span class="dashicons dashicons-location-alt"></span><?php esc_html_e( 'Address:', 'event_espresso' ); ?></strong> |
|
42 | - <?php espresso_venue_address( 'inline' ); // already escaped ?> |
|
43 | - <?php espresso_venue_gmap( $post->ID ); // already escaped ?> |
|
40 | +<?php if (espresso_venue_has_address($post->ID)) : ?> |
|
41 | + <strong><span class="dashicons dashicons-location-alt"></span><?php esc_html_e('Address:', 'event_espresso'); ?></strong> |
|
42 | + <?php espresso_venue_address('inline'); // already escaped ?> |
|
43 | + <?php espresso_venue_gmap($post->ID); // already escaped ?> |
|
44 | 44 | <div class="clear"><br/></div> |
45 | -<?php endif; ?> |
|
45 | +<?php endif; ?> |
|
46 | 46 | |
47 | - <?php $VNU_ID = espresso_venue_id( $post->ID ); ?> |
|
48 | - <?php if ( is_single() ) : ?> |
|
49 | - <?php $venue_description = espresso_venue_description( $VNU_ID, FALSE ); ?> |
|
50 | - <?php if ( $venue_description ) : ?> |
|
47 | + <?php $VNU_ID = espresso_venue_id($post->ID); ?> |
|
48 | + <?php if (is_single()) : ?> |
|
49 | + <?php $venue_description = espresso_venue_description($VNU_ID, FALSE); ?> |
|
50 | + <?php if ($venue_description) : ?> |
|
51 | 51 | <p> |
52 | - <strong><?php esc_html_e( 'Description:', 'event_espresso' ); ?></strong><br/> |
|
53 | - <?php echo do_shortcode( $venue_description ); ?> |
|
52 | + <strong><?php esc_html_e('Description:', 'event_espresso'); ?></strong><br/> |
|
53 | + <?php echo do_shortcode($venue_description); ?> |
|
54 | 54 | </p> |
55 | - <?php endif; ?> |
|
55 | + <?php endif; ?> |
|
56 | 56 | <?php else : ?> |
57 | - <?php $venue_excerpt = espresso_venue_excerpt( $VNU_ID, FALSE ); ?> |
|
58 | - <?php if ( $venue_excerpt ) : ?> |
|
57 | + <?php $venue_excerpt = espresso_venue_excerpt($VNU_ID, FALSE); ?> |
|
58 | + <?php if ($venue_excerpt) : ?> |
|
59 | 59 | <p> |
60 | - <strong><?php esc_html_e( 'Description:', 'event_espresso' ); ?></strong><br/> |
|
60 | + <strong><?php esc_html_e('Description:', 'event_espresso'); ?></strong><br/> |
|
61 | 61 | <?php echo wp_kses($venue_excerpt, AllowedTags::getAllowedTags()); ?> |
62 | 62 | </p> |
63 | - <?php endif; ?> |
|
64 | - <?php endif; ?> |
|
63 | + <?php endif; ?> |
|
64 | + <?php endif; ?> |
|
65 | 65 | </div> |
66 | 66 | <!-- .espresso-venue-dv --> |
67 | 67 | <?php |
68 | -do_action( 'AHEE_event_details_after_venue_details', $post ); |
|
68 | +do_action('AHEE_event_details_after_venue_details', $post); |
|
69 | 69 | else : |
70 | - if ( espresso_venue_is_password_protected() ) : |
|
70 | + if (espresso_venue_is_password_protected()) : |
|
71 | 71 | ?> |
72 | 72 | <div class="espresso-venue-dv espresso-password-protected-venue-dv" > |
73 | 73 | <h3 class="event-venues-h3 ee-event-h3"> |
74 | - <?php esc_html_e( 'Location', 'event_espresso' );?> |
|
74 | + <?php esc_html_e('Location', 'event_espresso'); ?> |
|
75 | 75 | </h3> |
76 | 76 | <?php echo espresso_password_protected_venue_form(); ?> |
77 | 77 | </div> |
@@ -7,10 +7,10 @@ |
||
7 | 7 | <?php echo "<{$tag} id=\"event-details-{$tag}-{$post->ID}\" class=\"entry-title\">"; ?> |
8 | 8 | <a class="ee-event-header-lnk" |
9 | 9 | href="<?php the_permalink(); ?>" |
10 | - <?php echo \EED_Events_Archive::link_target();?> |
|
10 | + <?php echo \EED_Events_Archive::link_target(); ?> |
|
11 | 11 | > |
12 | 12 | <?php the_title(); ?> |
13 | 13 | </a> |
14 | 14 | <?php echo "</{$tag}>"; ?> |
15 | - <?php if ( ! is_archive() && has_excerpt( $post->ID )): the_excerpt(); endif;?> |
|
15 | + <?php if ( ! is_archive() && has_excerpt($post->ID)): the_excerpt(); endif; ?> |
|
16 | 16 | </header> |
@@ -13,25 +13,25 @@ discard block |
||
13 | 13 | <div id="ee-block-event-attendees" class="ee-core-blocks event-espresso-blocks event-attendees"> |
14 | 14 | <ul> |
15 | 15 | <?php |
16 | - foreach ($attendees as $attendee) { |
|
17 | - $attendee_name = esc_html($attendee->full_name()); |
|
16 | + foreach ($attendees as $attendee) { |
|
17 | + $attendee_name = esc_html($attendee->full_name()); |
|
18 | 18 | |
19 | - $gravatar = $attributes['showGravatar'] |
|
20 | - ? get_avatar_url( |
|
21 | - $attendee->email(), |
|
22 | - array( |
|
23 | - 'width' => $attributes['avatarSize'], |
|
24 | - 'height' => $attributes['avatarSize'] |
|
25 | - ) |
|
26 | - ) |
|
27 | - : ''; |
|
19 | + $gravatar = $attributes['showGravatar'] |
|
20 | + ? get_avatar_url( |
|
21 | + $attendee->email(), |
|
22 | + array( |
|
23 | + 'width' => $attributes['avatarSize'], |
|
24 | + 'height' => $attributes['avatarSize'] |
|
25 | + ) |
|
26 | + ) |
|
27 | + : ''; |
|
28 | 28 | |
29 | - $gravatar_class = $attributes['avatarClass'] |
|
30 | - ? $attributes['avatarClass'] . ' contact-avatar-img avatar' |
|
31 | - : 'contact-avatar-img avatar'; |
|
29 | + $gravatar_class = $attributes['avatarClass'] |
|
30 | + ? $attributes['avatarClass'] . ' contact-avatar-img avatar' |
|
31 | + : 'contact-avatar-img avatar'; |
|
32 | 32 | |
33 | - $gravatar = $gravatar !== '' |
|
34 | - ? ' |
|
33 | + $gravatar = $gravatar !== '' |
|
34 | + ? ' |
|
35 | 35 | <div class="contact-image-wrap-div"> |
36 | 36 | <img class="' . esc_attr($gravatar_class) . '" |
37 | 37 | width="' . esc_attr($attributes['avatarSize']) . '" |
@@ -40,13 +40,13 @@ discard block |
||
40 | 40 | alt="contact avatar" |
41 | 41 | > |
42 | 42 | </div>' |
43 | - : ''; |
|
43 | + : ''; |
|
44 | 44 | |
45 | - echo " |
|
45 | + echo " |
|
46 | 46 | <li> |
47 | 47 | {$gravatar}<span>{$attendee_name}</span> |
48 | 48 | </li>"; |
49 | - } |
|
50 | - ?> |
|
49 | + } |
|
50 | + ?> |
|
51 | 51 | </ul> |
52 | 52 | </div> |
@@ -27,16 +27,16 @@ |
||
27 | 27 | : ''; |
28 | 28 | |
29 | 29 | $gravatar_class = $attributes['avatarClass'] |
30 | - ? $attributes['avatarClass'] . ' contact-avatar-img avatar' |
|
30 | + ? $attributes['avatarClass'].' contact-avatar-img avatar' |
|
31 | 31 | : 'contact-avatar-img avatar'; |
32 | 32 | |
33 | 33 | $gravatar = $gravatar !== '' |
34 | 34 | ? ' |
35 | 35 | <div class="contact-image-wrap-div"> |
36 | - <img class="' . esc_attr($gravatar_class) . '" |
|
37 | - width="' . esc_attr($attributes['avatarSize']) . '" |
|
38 | - height="' . esc_attr($attributes['avatarSize']) . '" |
|
39 | - src="' . esc_url_raw($gravatar) . '" |
|
36 | + <img class="' . esc_attr($gravatar_class).'" |
|
37 | + width="' . esc_attr($attributes['avatarSize']).'" |
|
38 | + height="' . esc_attr($attributes['avatarSize']).'" |
|
39 | + src="' . esc_url_raw($gravatar).'" |
|
40 | 40 | alt="contact avatar" |
41 | 41 | > |
42 | 42 | </div>' |