PluginProbe ʕ •ᴥ•ʔ
Reviews Feed – Add Testimonials and Customer Reviews From Google Reviews, Yelp, TripAdvisor, and More / 2.11.0
Reviews Feed – Add Testimonials and Customer Reviews From Google Reviews, Yelp, TripAdvisor, and More v2.11.0
2.11.0 2.10.0 2.9.0 2.8.0 2.7.0 2.6.7 2.6.8 2.6.5 2.6.4 2.6.3 2.6.2 2.6.0 2.5.5 2.5.4 2.5.3 2.5.2 trunk 1.0 1.0.1 1.0.2 1.0.3 1.1 1.1.1 1.1.2 1.2.0 2.0 2.1.0 2.1.1 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.5.0 2.5.1
reviews-feed / class / Common / Helpers / Data_Encryption.php
reviews-feed / class / Common / Helpers Last commit date
Data_Encryption.php 1 week ago PluginSilentUpgrader.php 1 week ago PluginSilentUpgraderSkin.php 1 week ago SBR_Error_Handler.php 1 week ago SBR_Install_Skin.php 1 week ago
Data_Encryption.php
200 lines
1 <?php
2
3 /**
4 * Class Data_Encryption
5 *
6 * @copyright 2021 Google LLC
7 * @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
8 * @link https://sitekit.withgoogle.com
9 */
10
11 namespace SmashBalloon\Reviews\Common\Helpers;
12
13 //Exit if accessed directly
14 if (!defined('ABSPATH')) {
15 exit;
16 }
17
18
19 /**
20 * Class responsible for encrypting and decrypting data.
21 *
22 * @since 2.9.4/5.12.4
23 * @access private
24 * @ignore
25 */
26 class Data_Encryption
27 {
28 /**
29 * Key to use for encryption.
30 *
31 * @since 2.9.4/5.12.4
32 * @var string
33 */
34 private $key;
35
36 /**
37 * Salt to use for encryption.
38 *
39 * @since 2.9.4/5.12.4
40 * @var string
41 */
42 private $salt;
43
44 /**
45 * Constructor.
46 *
47 * @since 2.9.4/5.12.4
48 */
49 public function __construct($remote = array())
50 {
51 if (!empty($remote)) {
52 $this->key = $remote['key'];
53 $this->salt = $remote['salt'];
54 } else {
55 $this->key = $this->get_default_key();
56 $this->salt = $this->get_default_salt();
57 }
58 }
59
60 /**
61 * Encrypts a value.
62 *
63 * If a user-based key is set, that key is used. Otherwise the default key is used.
64 *
65 * @since 2.9.4/5.12.4
66 *
67 * @param string $value Value to encrypt.
68 * @return string|bool Encrypted value, or false on failure.
69 */
70 public function encrypt($value)
71 {
72 if (!sbr_doing_openssl()) {
73 return $value;
74 }
75
76 $method = 'aes-256-ctr';
77 $ivlen = openssl_cipher_iv_length($method);
78 $iv = openssl_random_pseudo_bytes($ivlen);
79
80 $raw_value = openssl_encrypt($value . $this->salt, $method, $this->key, 0, $iv);
81 if (!$raw_value) {
82 return false;
83 }
84
85 return base64_encode($iv . $raw_value);
86 }
87
88 /**
89 * Decrypts a value.
90 *
91 * If a user-based key is set, that key is used. Otherwise the default key is used.
92 *
93 * @since 2.9.4/5.12.4
94 *
95 * @param string $raw_value Value to decrypt.
96 * @return string|bool Decrypted value, or false on failure.
97 */
98 public function decrypt($raw_value)
99 {
100 if (!sbr_doing_openssl()) {
101 return $raw_value;
102 }
103
104 $raw_value = base64_decode($raw_value, true);
105
106 $method = 'aes-256-ctr';
107 $ivlen = openssl_cipher_iv_length($method);
108 $iv = substr($raw_value, 0, $ivlen);
109
110 $raw_value = substr($raw_value, $ivlen);
111
112 $value = openssl_decrypt($raw_value, $method, $this->key, 0, $iv);
113 if (!$value || substr($value, - strlen($this->salt)) !== $this->salt) {
114 return false;
115 }
116
117 return substr($value, 0, - strlen($this->salt));
118 }
119
120
121 public function maybe_encrypt($raw_value)
122 {
123 #return $raw_value;
124 $maybe_decrypted = $this->decrypt($raw_value);
125
126 if ($maybe_decrypted) {
127 return $this->encrypt($maybe_decrypted);
128 }
129
130 return $this->encrypt($raw_value);
131 }
132
133 /**
134 * Uses a raw value and attempts to decrypt it
135 *
136 * @param $value
137 *
138 * @return bool|string
139 */
140 public function maybe_decrypt($value)
141 {
142 if (!is_string($value)) {
143 return $value;
144 }
145 if (strpos($value, '{') === 0) {
146 return $value;
147 }
148
149 $decrypted = $this->decrypt($value);
150
151 if (!$decrypted) {
152 return $value;
153 }
154
155 return $decrypted;
156 }
157
158 /**
159 * Gets the default encryption key to use.
160 *
161 * @since 2.9.4/5.12.4
162 *
163 * @return string Default (not user-based) encryption key.
164 */
165 private function get_default_key()
166 {
167 if (defined('CFF_ENCRYPTION_KEY') && '' !== CFF_ENCRYPTION_KEY) {
168 return CFF_ENCRYPTION_KEY;
169 }
170
171 if (defined('LOGGED_IN_KEY') && '' !== LOGGED_IN_KEY) {
172 return LOGGED_IN_KEY;
173 }
174
175 // If this is reached, you're either not on a live site or have a serious security issue.
176 return 'das-ist-kein-geheimer-schluessel';
177 }
178
179 /**
180 * Gets the default encryption salt to use.
181 *
182 * @since 2.9.4/5.12.4
183 *
184 * @return string Encryption salt.
185 */
186 private function get_default_salt()
187 {
188 if (defined('CFF_ENCRYPTION_SALT') && '' !== CFF_ENCRYPTION_SALT) {
189 return CFF_ENCRYPTION_SALT;
190 }
191
192 if (defined('LOGGED_IN_SALT') && '' !== LOGGED_IN_SALT) {
193 return LOGGED_IN_SALT;
194 }
195
196 // If this is reached, you're either not on a live site or have a serious security issue.
197 return 'das-ist-kein-geheimes-salz';
198 }
199 }
200