PluginProbe
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions / 260805
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions v260805
260917 260913 260909 260829 260814 260805 110710 110731 110812 110815 110912 110913 110915 110926 110927 111002 111003 111011 111017 111029 111105 111206 111216 111220 120213 All 189 releases
s2member / src / includes / classes / utils-defuse.inc.php

utils-defuse.inc.php in s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions 260805, at src/includes/classes/utils-defuse.inc.php

132 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // @codingStandardsIgnoreFile
3 /**
4 * Encryption utilities.
5 *
6 * @since 170418 Defuse.
7 */
8 use Defuse\Crypto\Key;
9 use Defuse\Crypto\Crypto;
10 use c_ws_plugin__s2member_utils_strings as s;
11
12 if (!defined('WPINC')) { // MUST have.
13 exit('Do not access this file directly.');
14 }
15 if (!class_exists('c_ws_plugin__s2member_utils_defuse')) {
16 /**
17 * Encryption utilities.
18 *
19 * @since 170418 Defuse.
20 */
21 class c_ws_plugin__s2member_utils_defuse
22 {
23 /**
24 * Defuse key.
25 *
26 * @since 170418 Defuse.
27 *
28 * @param string $key A custom key.
29 *
30 * @return string Defuse encryption key.
31 */
32 public static function key($key = '')
33 {
34 $key = (string) $key;
35
36 if (isset($key[0])) { // Custom?
37 if (strpos($key, 'def00000') === 0) {
38 return $key; // Defuse key.
39 }
40 $sec_key = c_ws_plugin__s2member_utils_encryption::key($key);
41 $def_combo_keys = $GLOBALS['WS_PLUGIN__']['s2member']['o']['def_custom_combo_encryption_keys'];
42
43 if ($sec_key && !empty($def_combo_keys[$sec_key])) {
44 return $def_combo_keys[$sec_key]; // Use existing key.
45 }
46 try { // Catch Defuse exceptions.
47 if (!($def_key = Key::createNewRandomKey()->saveToAsciiSafeString())) {
48 throw new Exception('Defuse keygen failure.');
49 }
50 } catch (Throwable $Exception) {
51 throw new Exception($Exception->getMessage());
52 }
53 $def_combo_keys[$sec_key] = $def_key;
54 array_unshift($def_combo_keys, 'update-signal');
55 $options['ws_plugin__s2member_def_custom_combo_encryption_keys'] = $def_combo_keys;
56 c_ws_plugin__s2member_menu_pages::update_all_options($options, true, false, false, false, false);
57 //
58 } else { // Default behavior is to use the configured key.
59 $sec_key = c_ws_plugin__s2member_utils_encryption::key();
60 $def_combo_key = $GLOBALS['WS_PLUGIN__']['s2member']['o']['def_combo_encryption_key'];
61
62 if ($sec_key && $def_combo_key && strpos($def_combo_key, $sec_key."\n") === 0
63 && ($def_key = str_replace($sec_key."\n", '', $def_combo_key))) {
64 return $def_key; // Use existing key.
65 }
66 try { // Catch Defuse exceptions.
67 if (!($def_key = Key::createNewRandomKey()->saveToAsciiSafeString())) {
68 throw new Exception('Defuse keygen failure.');
69 }
70 } catch (Throwable $Exception) {
71 throw new Exception($Exception->getMessage());
72 }
73 $options['ws_plugin__s2member_def_combo_encryption_key'] = $sec_key."\n".$def_key;
74 c_ws_plugin__s2member_menu_pages::update_all_options($options, true, false, false, false, false);
75 }
76 return $def_key;
77 }
78
79 /**
80 * Defuse encryption.
81 *
82 * @since 170418 Defuse.
83 *
84 * @param string $string String to encrypt.
85 * @param string $key A custom key passed to `::key()`.
86 *
87 * @return string Encrypted string w/ a URL-safe base64 wrapper.
88 */
89 public static function encrypt($string, $key = '')
90 {
91 $string = (string) $string;
92
93 if (!isset($string[0])) {
94 return ''; // Not possible.
95 } // Empty string is an empty string.
96
97 try { // Catch Defuse exceptions.
98 $Key = Key::loadFromAsciiSafeString(self::key($key));
99 $encrypted = Crypto::encrypt($string, $Key, false);
100 return $base64 = s::base64_url_safe_encode($encrypted);
101 } catch (Throwable $Exception) {
102 throw new Exception($Exception->getMessage());
103 }
104 }
105
106 /**
107 * Defuse decryption.
108 *
109 * @since 170418 Defuse.
110 *
111 * @param string $base64 String to decrypt.
112 * @param string $key A custom key passed to `::key()`.
113 *
114 * @return string Decrypted string.
115 */
116 public static function decrypt($base64, $key = '')
117 {
118 if (!is_string($base64) || !isset($base64[0])) {
119 return ''; // Not possible.
120 } // Fail when not a string or empty.
121
122 try { // Catch Defuse exceptions.
123 $Key = Key::loadFromAsciiSafeString(self::key($key));
124 $encrypted = s::base64_url_safe_decode($base64);
125 return $string = Crypto::decrypt($encrypted, $Key, false);
126 } catch (Throwable $Exception) {
127 return ''; // Soft failure.
128 }
129 }
130 }
131 }
132