@@ -143,7 +143,7 @@ discard block |
||
143 | 143 | ); |
144 | 144 | $this->validateEncryption($encrypted_text); |
145 | 145 | // concatenate everything into one big string |
146 | - return $iv . $tag . $encrypted_text; |
|
146 | + return $iv.$tag.$encrypted_text; |
|
147 | 147 | } |
148 | 148 | |
149 | 149 | |
@@ -165,7 +165,7 @@ discard block |
||
165 | 165 | ); |
166 | 166 | $this->validateEncryption($encrypted_text); |
167 | 167 | // prepend the initialization vector |
168 | - return $iv . $encrypted_text; |
|
168 | + return $iv.$encrypted_text; |
|
169 | 169 | } |
170 | 170 | |
171 | 171 |
@@ -23,221 +23,221 @@ |
||
23 | 23 | */ |
24 | 24 | class OpenSSLv2 extends OpenSSL |
25 | 25 | { |
26 | - /** |
|
27 | - * name used for a default encryption key in case no others are set |
|
28 | - */ |
|
29 | - const DEFAULT_ENCRYPTION_KEY_ID = 'default_openssl_v2_key'; |
|
30 | - |
|
31 | - /** |
|
32 | - * name used for saving encryption keys to the wp_options table |
|
33 | - */ |
|
34 | - const ENCRYPTION_KEYS_OPTION_NAME = 'ee_openssl_v2_encryption_keys'; |
|
35 | - |
|
36 | - /** |
|
37 | - * the OPENSSL cipher method used |
|
38 | - */ |
|
39 | - const CIPHER_METHOD = 'aes-256-gcm'; |
|
40 | - |
|
41 | - /** |
|
42 | - * WP "options_name" used to store a verified available cipher method |
|
43 | - */ |
|
44 | - const CIPHER_METHOD_OPTION_NAME = 'ee_openssl_v2_cipher_method'; |
|
45 | - |
|
46 | - /** |
|
47 | - * The length of the authentication tag. Its value can be between 4 and 16 for GCM mode. |
|
48 | - */ |
|
49 | - const AUTH_TAG_LENGTH = 16; |
|
50 | - |
|
51 | - |
|
52 | - /** |
|
53 | - * To use custom a cipher method and/or encryption keys and/or minimum PHP version: |
|
54 | - * - extend this class |
|
55 | - * - configure a new CipherMethod / EncryptionKeyManager in the constructor |
|
56 | - * - pass those to this constructor, like so: |
|
57 | - * |
|
58 | - * public function __construct(Base64Encoder $base64_encoder) { |
|
59 | - * parent::__construct( |
|
60 | - * $base64_encoder, |
|
61 | - * new CipherMethod(CIPHER_METHOD, CIPHER_METHOD_OPTION_NAME), |
|
62 | - * new EncryptionKeyManager(CUSTOM_KEY_ID, CUSTOM_KEYS_OPTION_NAME), |
|
63 | - * '7.1.0' |
|
64 | - * ); |
|
65 | - * } |
|
66 | - * |
|
67 | - * @param Base64Encoder $base64_encoder |
|
68 | - * @param CipherMethod|null $cipher_method |
|
69 | - * @param EncryptionKeyManagerInterface|null $encryption_key_manager |
|
70 | - * @param string $min_php_version defaults to 7.1.0 |
|
71 | - * (when openssl auth tag and random_bytes() were added) |
|
72 | - */ |
|
73 | - public function __construct( |
|
74 | - Base64Encoder $base64_encoder, |
|
75 | - CipherMethod $cipher_method = null, |
|
76 | - EncryptionKeyManagerInterface $encryption_key_manager = null, |
|
77 | - $min_php_version = '7.1.0' |
|
78 | - ) { |
|
79 | - parent::__construct( |
|
80 | - $base64_encoder, |
|
81 | - $cipher_method instanceof CipherMethod |
|
82 | - ? $cipher_method |
|
83 | - : new CipherMethod( |
|
84 | - OpenSSLv2::CIPHER_METHOD, |
|
85 | - OpenSSLv2::CIPHER_METHOD_OPTION_NAME |
|
86 | - ), |
|
87 | - $encryption_key_manager instanceof EncryptionKeyManager |
|
88 | - ? $encryption_key_manager |
|
89 | - : new EncryptionKeyManager( |
|
90 | - $base64_encoder, |
|
91 | - OpenSSLv2::DEFAULT_ENCRYPTION_KEY_ID, |
|
92 | - OpenSSLv2::ENCRYPTION_KEYS_OPTION_NAME |
|
93 | - ), |
|
94 | - $min_php_version |
|
95 | - ); |
|
96 | - } |
|
97 | - |
|
98 | - |
|
99 | - /** |
|
100 | - * encrypts data |
|
101 | - * |
|
102 | - * @param string $text_to_encrypt - the text to be encrypted |
|
103 | - * @param string $encryption_key_identifier - [optional] cryptographically secure passphrase. generated if not set |
|
104 | - * @param string $aad - [optional] additional authentication data |
|
105 | - * @return string |
|
106 | - * @throws Exception |
|
107 | - */ |
|
108 | - public function encrypt($text_to_encrypt, $encryption_key_identifier = '', $aad = '') |
|
109 | - { |
|
110 | - $cipher_method = $this->cipher_method->getCipherMethod(); |
|
111 | - $encryption_key = $this->encryption_key_manager->getEncryptionKey($encryption_key_identifier); |
|
112 | - // generate initialization vector for the cipher method. |
|
113 | - $iv = random_bytes(openssl_cipher_iv_length($cipher_method)); |
|
114 | - // encrypt it (encode to remove special characters) |
|
115 | - $text_to_encrypt = $this->base64_encoder->encodeString($text_to_encrypt); |
|
116 | - $encrypted_text = $this->cipher_method->usesAuthenticatedEncryptionMode() |
|
117 | - ? $this->authenticatedEncrypt($text_to_encrypt, $cipher_method, $encryption_key, $iv, $aad) |
|
118 | - : $this->nonAuthenticatedEncrypt($text_to_encrypt, $cipher_method, $encryption_key, $iv); |
|
119 | - return $this->base64_encoder->encodeString($encrypted_text); |
|
120 | - } |
|
121 | - |
|
122 | - |
|
123 | - /** |
|
124 | - * @param string $text_to_encrypt - the text to be encrypted |
|
125 | - * @param string $cipher_method - the OpenSSL cipher method used during encryption |
|
126 | - * @param string $encryption_key - cryptographically secure passphrase uses default if not set |
|
127 | - * @param string $iv - the initialization vector |
|
128 | - * @param string $aad - additional authentication data |
|
129 | - * @return string |
|
130 | - */ |
|
131 | - private function authenticatedEncrypt($text_to_encrypt, $cipher_method, $encryption_key, $iv, $aad) |
|
132 | - { |
|
133 | - $encrypted_text = openssl_encrypt( |
|
134 | - $text_to_encrypt, |
|
135 | - $cipher_method, |
|
136 | - $this->getDigestHashValue($encryption_key, OpenSSL::DEFAULT_DIGEST_METHOD, OPENSSL_RAW_DATA), |
|
137 | - 0, |
|
138 | - $iv, |
|
139 | - $tag, |
|
140 | - $aad, |
|
141 | - OpenSSLv2::AUTH_TAG_LENGTH |
|
142 | - ); |
|
143 | - $this->validateEncryption($encrypted_text); |
|
144 | - // concatenate everything into one big string |
|
145 | - return $iv . $tag . $encrypted_text; |
|
146 | - } |
|
147 | - |
|
148 | - |
|
149 | - /** |
|
150 | - * @param string $text_to_encrypt - the text to be encrypted |
|
151 | - * @param string $cipher_method - the OpenSSL cipher method used during encryption |
|
152 | - * @param string $encryption_key - cryptographically secure passphrase uses default if not set |
|
153 | - * @param string $iv - the initialization vector |
|
154 | - * @return string |
|
155 | - */ |
|
156 | - private function nonAuthenticatedEncrypt($text_to_encrypt, $cipher_method, $encryption_key, $iv) |
|
157 | - { |
|
158 | - $encrypted_text = openssl_encrypt( |
|
159 | - $text_to_encrypt, |
|
160 | - $cipher_method, |
|
161 | - $this->getDigestHashValue($encryption_key), |
|
162 | - 0, |
|
163 | - $iv |
|
164 | - ); |
|
165 | - $this->validateEncryption($encrypted_text); |
|
166 | - // prepend the initialization vector |
|
167 | - return $iv . $encrypted_text; |
|
168 | - } |
|
169 | - |
|
170 | - |
|
171 | - /** |
|
172 | - * decrypts data |
|
173 | - * |
|
174 | - * @param string $encrypted_text - the text to be decrypted |
|
175 | - * @param string $encryption_key_identifier - [optional] cryptographically secure passphrase uses default if not set |
|
176 | - * @param string $aad - [optional] additional authentication data |
|
177 | - * @return string |
|
178 | - */ |
|
179 | - public function decrypt($encrypted_text, $encryption_key_identifier = '', $aad = '') |
|
180 | - { |
|
181 | - $cipher_method = $this->cipher_method->getCipherMethod(); |
|
182 | - $encryption_key = $this->encryption_key_manager->getEncryptionKey($encryption_key_identifier); |
|
183 | - // maybe decode |
|
184 | - $encrypted_text = $this->base64_encoder->decodeString($encrypted_text); |
|
185 | - $iv_length = openssl_cipher_iv_length($cipher_method); |
|
186 | - // use the iv length to snip it from the decoded string |
|
187 | - $iv = substr($encrypted_text, 0, $iv_length); |
|
188 | - // then remove it from the rest of the decoded string |
|
189 | - $encrypted_text = substr($encrypted_text, $iv_length); |
|
190 | - // decrypt it |
|
191 | - $decrypted_text = $this->cipher_method->usesAuthenticatedEncryptionMode() |
|
192 | - ? $this->authenticatedDecrypt($encrypted_text, $cipher_method, $encryption_key, $iv, $aad) |
|
193 | - : $this->nonAuthenticatedDecrypt($encrypted_text, $cipher_method, $encryption_key, $iv); |
|
194 | - |
|
195 | - $this->validateDecryption($decrypted_text); |
|
196 | - return trim($this->base64_encoder->decodeString($decrypted_text)); |
|
197 | - } |
|
198 | - |
|
199 | - |
|
200 | - /** |
|
201 | - * @param string $encrypted_text - the text to be decrypted |
|
202 | - * @param string $cipher_method - the OpenSSL cipher method used during encryption |
|
203 | - * @param string $encryption_key - cryptographically secure passphrase uses default if not set |
|
204 | - * @param string $iv - the initialization vector |
|
205 | - * @param string $aad - additional authentication data |
|
206 | - * @return string|false |
|
207 | - */ |
|
208 | - private function authenticatedDecrypt($encrypted_text, $cipher_method, $encryption_key, $iv, $aad) |
|
209 | - { |
|
210 | - // use the tag length to snip it from the decoded string |
|
211 | - $tag = substr($encrypted_text, 0, OpenSSLv2::AUTH_TAG_LENGTH); |
|
212 | - // then remove it from the rest of the decoded string |
|
213 | - $encrypted_text = substr($encrypted_text, OpenSSLv2::AUTH_TAG_LENGTH); |
|
214 | - return openssl_decrypt( |
|
215 | - $encrypted_text, |
|
216 | - $cipher_method, |
|
217 | - $this->getDigestHashValue($encryption_key, OpenSSL::DEFAULT_DIGEST_METHOD, OPENSSL_RAW_DATA), |
|
218 | - 0, |
|
219 | - $iv, |
|
220 | - $tag, |
|
221 | - $aad |
|
222 | - ); |
|
223 | - } |
|
224 | - |
|
225 | - |
|
226 | - /** |
|
227 | - * @param string $encrypted_text - the text to be decrypted |
|
228 | - * @param string $cipher_method - the OpenSSL cipher method used during encryption |
|
229 | - * @param string $encryption_key - cryptographically secure passphrase uses default if not set |
|
230 | - * @param string $iv - the initialization vector |
|
231 | - * @return string|false |
|
232 | - */ |
|
233 | - private function nonAuthenticatedDecrypt($encrypted_text, $cipher_method, $encryption_key, $iv) |
|
234 | - { |
|
235 | - return openssl_decrypt( |
|
236 | - $encrypted_text, |
|
237 | - $cipher_method, |
|
238 | - $this->getDigestHashValue($encryption_key), |
|
239 | - 0, |
|
240 | - $iv |
|
241 | - ); |
|
242 | - } |
|
26 | + /** |
|
27 | + * name used for a default encryption key in case no others are set |
|
28 | + */ |
|
29 | + const DEFAULT_ENCRYPTION_KEY_ID = 'default_openssl_v2_key'; |
|
30 | + |
|
31 | + /** |
|
32 | + * name used for saving encryption keys to the wp_options table |
|
33 | + */ |
|
34 | + const ENCRYPTION_KEYS_OPTION_NAME = 'ee_openssl_v2_encryption_keys'; |
|
35 | + |
|
36 | + /** |
|
37 | + * the OPENSSL cipher method used |
|
38 | + */ |
|
39 | + const CIPHER_METHOD = 'aes-256-gcm'; |
|
40 | + |
|
41 | + /** |
|
42 | + * WP "options_name" used to store a verified available cipher method |
|
43 | + */ |
|
44 | + const CIPHER_METHOD_OPTION_NAME = 'ee_openssl_v2_cipher_method'; |
|
45 | + |
|
46 | + /** |
|
47 | + * The length of the authentication tag. Its value can be between 4 and 16 for GCM mode. |
|
48 | + */ |
|
49 | + const AUTH_TAG_LENGTH = 16; |
|
50 | + |
|
51 | + |
|
52 | + /** |
|
53 | + * To use custom a cipher method and/or encryption keys and/or minimum PHP version: |
|
54 | + * - extend this class |
|
55 | + * - configure a new CipherMethod / EncryptionKeyManager in the constructor |
|
56 | + * - pass those to this constructor, like so: |
|
57 | + * |
|
58 | + * public function __construct(Base64Encoder $base64_encoder) { |
|
59 | + * parent::__construct( |
|
60 | + * $base64_encoder, |
|
61 | + * new CipherMethod(CIPHER_METHOD, CIPHER_METHOD_OPTION_NAME), |
|
62 | + * new EncryptionKeyManager(CUSTOM_KEY_ID, CUSTOM_KEYS_OPTION_NAME), |
|
63 | + * '7.1.0' |
|
64 | + * ); |
|
65 | + * } |
|
66 | + * |
|
67 | + * @param Base64Encoder $base64_encoder |
|
68 | + * @param CipherMethod|null $cipher_method |
|
69 | + * @param EncryptionKeyManagerInterface|null $encryption_key_manager |
|
70 | + * @param string $min_php_version defaults to 7.1.0 |
|
71 | + * (when openssl auth tag and random_bytes() were added) |
|
72 | + */ |
|
73 | + public function __construct( |
|
74 | + Base64Encoder $base64_encoder, |
|
75 | + CipherMethod $cipher_method = null, |
|
76 | + EncryptionKeyManagerInterface $encryption_key_manager = null, |
|
77 | + $min_php_version = '7.1.0' |
|
78 | + ) { |
|
79 | + parent::__construct( |
|
80 | + $base64_encoder, |
|
81 | + $cipher_method instanceof CipherMethod |
|
82 | + ? $cipher_method |
|
83 | + : new CipherMethod( |
|
84 | + OpenSSLv2::CIPHER_METHOD, |
|
85 | + OpenSSLv2::CIPHER_METHOD_OPTION_NAME |
|
86 | + ), |
|
87 | + $encryption_key_manager instanceof EncryptionKeyManager |
|
88 | + ? $encryption_key_manager |
|
89 | + : new EncryptionKeyManager( |
|
90 | + $base64_encoder, |
|
91 | + OpenSSLv2::DEFAULT_ENCRYPTION_KEY_ID, |
|
92 | + OpenSSLv2::ENCRYPTION_KEYS_OPTION_NAME |
|
93 | + ), |
|
94 | + $min_php_version |
|
95 | + ); |
|
96 | + } |
|
97 | + |
|
98 | + |
|
99 | + /** |
|
100 | + * encrypts data |
|
101 | + * |
|
102 | + * @param string $text_to_encrypt - the text to be encrypted |
|
103 | + * @param string $encryption_key_identifier - [optional] cryptographically secure passphrase. generated if not set |
|
104 | + * @param string $aad - [optional] additional authentication data |
|
105 | + * @return string |
|
106 | + * @throws Exception |
|
107 | + */ |
|
108 | + public function encrypt($text_to_encrypt, $encryption_key_identifier = '', $aad = '') |
|
109 | + { |
|
110 | + $cipher_method = $this->cipher_method->getCipherMethod(); |
|
111 | + $encryption_key = $this->encryption_key_manager->getEncryptionKey($encryption_key_identifier); |
|
112 | + // generate initialization vector for the cipher method. |
|
113 | + $iv = random_bytes(openssl_cipher_iv_length($cipher_method)); |
|
114 | + // encrypt it (encode to remove special characters) |
|
115 | + $text_to_encrypt = $this->base64_encoder->encodeString($text_to_encrypt); |
|
116 | + $encrypted_text = $this->cipher_method->usesAuthenticatedEncryptionMode() |
|
117 | + ? $this->authenticatedEncrypt($text_to_encrypt, $cipher_method, $encryption_key, $iv, $aad) |
|
118 | + : $this->nonAuthenticatedEncrypt($text_to_encrypt, $cipher_method, $encryption_key, $iv); |
|
119 | + return $this->base64_encoder->encodeString($encrypted_text); |
|
120 | + } |
|
121 | + |
|
122 | + |
|
123 | + /** |
|
124 | + * @param string $text_to_encrypt - the text to be encrypted |
|
125 | + * @param string $cipher_method - the OpenSSL cipher method used during encryption |
|
126 | + * @param string $encryption_key - cryptographically secure passphrase uses default if not set |
|
127 | + * @param string $iv - the initialization vector |
|
128 | + * @param string $aad - additional authentication data |
|
129 | + * @return string |
|
130 | + */ |
|
131 | + private function authenticatedEncrypt($text_to_encrypt, $cipher_method, $encryption_key, $iv, $aad) |
|
132 | + { |
|
133 | + $encrypted_text = openssl_encrypt( |
|
134 | + $text_to_encrypt, |
|
135 | + $cipher_method, |
|
136 | + $this->getDigestHashValue($encryption_key, OpenSSL::DEFAULT_DIGEST_METHOD, OPENSSL_RAW_DATA), |
|
137 | + 0, |
|
138 | + $iv, |
|
139 | + $tag, |
|
140 | + $aad, |
|
141 | + OpenSSLv2::AUTH_TAG_LENGTH |
|
142 | + ); |
|
143 | + $this->validateEncryption($encrypted_text); |
|
144 | + // concatenate everything into one big string |
|
145 | + return $iv . $tag . $encrypted_text; |
|
146 | + } |
|
147 | + |
|
148 | + |
|
149 | + /** |
|
150 | + * @param string $text_to_encrypt - the text to be encrypted |
|
151 | + * @param string $cipher_method - the OpenSSL cipher method used during encryption |
|
152 | + * @param string $encryption_key - cryptographically secure passphrase uses default if not set |
|
153 | + * @param string $iv - the initialization vector |
|
154 | + * @return string |
|
155 | + */ |
|
156 | + private function nonAuthenticatedEncrypt($text_to_encrypt, $cipher_method, $encryption_key, $iv) |
|
157 | + { |
|
158 | + $encrypted_text = openssl_encrypt( |
|
159 | + $text_to_encrypt, |
|
160 | + $cipher_method, |
|
161 | + $this->getDigestHashValue($encryption_key), |
|
162 | + 0, |
|
163 | + $iv |
|
164 | + ); |
|
165 | + $this->validateEncryption($encrypted_text); |
|
166 | + // prepend the initialization vector |
|
167 | + return $iv . $encrypted_text; |
|
168 | + } |
|
169 | + |
|
170 | + |
|
171 | + /** |
|
172 | + * decrypts data |
|
173 | + * |
|
174 | + * @param string $encrypted_text - the text to be decrypted |
|
175 | + * @param string $encryption_key_identifier - [optional] cryptographically secure passphrase uses default if not set |
|
176 | + * @param string $aad - [optional] additional authentication data |
|
177 | + * @return string |
|
178 | + */ |
|
179 | + public function decrypt($encrypted_text, $encryption_key_identifier = '', $aad = '') |
|
180 | + { |
|
181 | + $cipher_method = $this->cipher_method->getCipherMethod(); |
|
182 | + $encryption_key = $this->encryption_key_manager->getEncryptionKey($encryption_key_identifier); |
|
183 | + // maybe decode |
|
184 | + $encrypted_text = $this->base64_encoder->decodeString($encrypted_text); |
|
185 | + $iv_length = openssl_cipher_iv_length($cipher_method); |
|
186 | + // use the iv length to snip it from the decoded string |
|
187 | + $iv = substr($encrypted_text, 0, $iv_length); |
|
188 | + // then remove it from the rest of the decoded string |
|
189 | + $encrypted_text = substr($encrypted_text, $iv_length); |
|
190 | + // decrypt it |
|
191 | + $decrypted_text = $this->cipher_method->usesAuthenticatedEncryptionMode() |
|
192 | + ? $this->authenticatedDecrypt($encrypted_text, $cipher_method, $encryption_key, $iv, $aad) |
|
193 | + : $this->nonAuthenticatedDecrypt($encrypted_text, $cipher_method, $encryption_key, $iv); |
|
194 | + |
|
195 | + $this->validateDecryption($decrypted_text); |
|
196 | + return trim($this->base64_encoder->decodeString($decrypted_text)); |
|
197 | + } |
|
198 | + |
|
199 | + |
|
200 | + /** |
|
201 | + * @param string $encrypted_text - the text to be decrypted |
|
202 | + * @param string $cipher_method - the OpenSSL cipher method used during encryption |
|
203 | + * @param string $encryption_key - cryptographically secure passphrase uses default if not set |
|
204 | + * @param string $iv - the initialization vector |
|
205 | + * @param string $aad - additional authentication data |
|
206 | + * @return string|false |
|
207 | + */ |
|
208 | + private function authenticatedDecrypt($encrypted_text, $cipher_method, $encryption_key, $iv, $aad) |
|
209 | + { |
|
210 | + // use the tag length to snip it from the decoded string |
|
211 | + $tag = substr($encrypted_text, 0, OpenSSLv2::AUTH_TAG_LENGTH); |
|
212 | + // then remove it from the rest of the decoded string |
|
213 | + $encrypted_text = substr($encrypted_text, OpenSSLv2::AUTH_TAG_LENGTH); |
|
214 | + return openssl_decrypt( |
|
215 | + $encrypted_text, |
|
216 | + $cipher_method, |
|
217 | + $this->getDigestHashValue($encryption_key, OpenSSL::DEFAULT_DIGEST_METHOD, OPENSSL_RAW_DATA), |
|
218 | + 0, |
|
219 | + $iv, |
|
220 | + $tag, |
|
221 | + $aad |
|
222 | + ); |
|
223 | + } |
|
224 | + |
|
225 | + |
|
226 | + /** |
|
227 | + * @param string $encrypted_text - the text to be decrypted |
|
228 | + * @param string $cipher_method - the OpenSSL cipher method used during encryption |
|
229 | + * @param string $encryption_key - cryptographically secure passphrase uses default if not set |
|
230 | + * @param string $iv - the initialization vector |
|
231 | + * @return string|false |
|
232 | + */ |
|
233 | + private function nonAuthenticatedDecrypt($encrypted_text, $cipher_method, $encryption_key, $iv) |
|
234 | + { |
|
235 | + return openssl_decrypt( |
|
236 | + $encrypted_text, |
|
237 | + $cipher_method, |
|
238 | + $this->getDigestHashValue($encryption_key), |
|
239 | + 0, |
|
240 | + $iv |
|
241 | + ); |
|
242 | + } |
|
243 | 243 | } |
@@ -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; |
@@ -25,582 +25,582 @@ |
||
25 | 25 | */ |
26 | 26 | class EE_Encryption implements InterminableInterface |
27 | 27 | { |
28 | - /** |
|
29 | - * key used for saving the encryption key to the wp_options table |
|
30 | - */ |
|
31 | - const ENCRYPTION_OPTION_KEY = 'ee_encryption_key'; |
|
32 | - |
|
33 | - /** |
|
34 | - * the OPENSSL cipher method used |
|
35 | - */ |
|
36 | - const OPENSSL_CIPHER_METHOD = 'AES-128-CBC'; |
|
37 | - |
|
38 | - /** |
|
39 | - * WP "options_name" used to store a verified available cipher method |
|
40 | - */ |
|
41 | - const OPENSSL_CIPHER_METHOD_OPTION_NAME = 'ee_openssl_cipher_method'; |
|
42 | - |
|
43 | - /** |
|
44 | - * the OPENSSL digest method used |
|
45 | - */ |
|
46 | - const OPENSSL_DIGEST_METHOD = 'sha512'; |
|
47 | - |
|
48 | - /** |
|
49 | - * separates the encrypted text from the initialization vector |
|
50 | - */ |
|
51 | - const OPENSSL_IV_DELIMITER = ':iv:'; |
|
52 | - |
|
53 | - /** |
|
54 | - * appended to text encrypted using the acme encryption |
|
55 | - */ |
|
56 | - const ACME_ENCRYPTION_FLAG = '::ae'; |
|
57 | - |
|
58 | - |
|
59 | - /** |
|
60 | - * instance of the EE_Encryption object |
|
61 | - */ |
|
62 | - protected static $_instance; |
|
63 | - |
|
64 | - /** |
|
65 | - * @var string $_encryption_key |
|
66 | - */ |
|
67 | - protected $_encryption_key; |
|
68 | - |
|
69 | - /** |
|
70 | - * @var string $cipher_method |
|
71 | - */ |
|
72 | - private $cipher_method = ''; |
|
73 | - |
|
74 | - /** |
|
75 | - * @var array $cipher_methods |
|
76 | - */ |
|
77 | - private $cipher_methods = []; |
|
78 | - |
|
79 | - /** |
|
80 | - * @var array $digest_methods |
|
81 | - */ |
|
82 | - private $digest_methods = []; |
|
83 | - |
|
84 | - /** |
|
85 | - * @var boolean $_use_openssl_encrypt |
|
86 | - */ |
|
87 | - protected $_use_openssl_encrypt = false; |
|
88 | - |
|
89 | - /** |
|
90 | - * @var boolean $_use_base64_encode |
|
91 | - */ |
|
92 | - protected $_use_base64_encode = false; |
|
93 | - |
|
94 | - |
|
95 | - /** |
|
96 | - * protected constructor to prevent direct creation |
|
97 | - */ |
|
98 | - protected function __construct() |
|
99 | - { |
|
100 | - if (! defined('ESPRESSO_ENCRYPT')) { |
|
101 | - define('ESPRESSO_ENCRYPT', true); |
|
102 | - } |
|
103 | - if (extension_loaded('openssl')) { |
|
104 | - $this->_use_openssl_encrypt = true; |
|
105 | - } |
|
106 | - if (function_exists('base64_encode')) { |
|
107 | - $this->_use_base64_encode = true; |
|
108 | - } |
|
109 | - EE_Error::doing_it_wrong(__METHOD__, esc_html__('Usage is deprecated.', 'event_espresso'), '5.0.8.p'); |
|
110 | - } |
|
111 | - |
|
112 | - |
|
113 | - /** |
|
114 | - * singleton method used to instantiate class object |
|
115 | - * |
|
116 | - * @return EE_Encryption |
|
117 | - */ |
|
118 | - public static function instance() |
|
119 | - { |
|
120 | - // check if class object is instantiated |
|
121 | - if (! EE_Encryption::$_instance instanceof EE_Encryption) { |
|
122 | - EE_Encryption::$_instance = new self(); |
|
123 | - } |
|
124 | - return EE_Encryption::$_instance; |
|
125 | - } |
|
126 | - |
|
127 | - |
|
128 | - /** |
|
129 | - * get encryption key |
|
130 | - * |
|
131 | - * @return string |
|
132 | - */ |
|
133 | - public function get_encryption_key() |
|
134 | - { |
|
135 | - // if encryption key has not been set |
|
136 | - if (empty($this->_encryption_key)) { |
|
137 | - // retrieve encryption_key from db |
|
138 | - $this->_encryption_key = get_option(EE_Encryption::ENCRYPTION_OPTION_KEY, ''); |
|
139 | - // WHAT?? No encryption_key in the db ?? |
|
140 | - if ($this->_encryption_key === '') { |
|
141 | - // let's make one. And md5 it to make it just the right size for a key |
|
142 | - $new_key = md5($this->generate_random_string()); |
|
143 | - // now save it to the db for later |
|
144 | - add_option(EE_Encryption::ENCRYPTION_OPTION_KEY, $new_key); |
|
145 | - // here's the key - FINALLY ! |
|
146 | - $this->_encryption_key = $new_key; |
|
147 | - } |
|
148 | - } |
|
149 | - return $this->_encryption_key; |
|
150 | - } |
|
151 | - |
|
152 | - |
|
153 | - /** |
|
154 | - * encrypts data |
|
155 | - * |
|
156 | - * @param string $text_string - the text to be encrypted |
|
157 | - * @return string |
|
158 | - * @throws RuntimeException |
|
159 | - */ |
|
160 | - public function encrypt($text_string = '') |
|
161 | - { |
|
162 | - // you give me nothing??? GET OUT ! |
|
163 | - if (empty($text_string)) { |
|
164 | - return $text_string; |
|
165 | - } |
|
166 | - if ($this->_use_openssl_encrypt) { |
|
167 | - $encrypted_text = $this->openssl_encrypt($text_string); |
|
168 | - } else { |
|
169 | - $encrypted_text = $this->acme_encrypt($text_string); |
|
170 | - } |
|
171 | - return $encrypted_text; |
|
172 | - } |
|
173 | - |
|
174 | - |
|
175 | - /** |
|
176 | - * decrypts data |
|
177 | - * |
|
178 | - * @param string $encrypted_text - the text to be decrypted |
|
179 | - * @return string |
|
180 | - * @throws RuntimeException |
|
181 | - */ |
|
182 | - public function decrypt($encrypted_text = '') |
|
183 | - { |
|
184 | - // you give me nothing??? GET OUT ! |
|
185 | - if (empty($encrypted_text)) { |
|
186 | - return $encrypted_text; |
|
187 | - } |
|
188 | - // if PHP's mcrypt functions are installed then we'll use them |
|
189 | - if ($this->_use_openssl_encrypt) { |
|
190 | - $decrypted_text = $this->openssl_decrypt($encrypted_text); |
|
191 | - } else { |
|
192 | - $decrypted_text = $this->acme_decrypt($encrypted_text); |
|
193 | - } |
|
194 | - return $decrypted_text; |
|
195 | - } |
|
196 | - |
|
197 | - |
|
198 | - /** |
|
199 | - * encodes string with PHP's base64 encoding |
|
200 | - * |
|
201 | - * @see http://php.net/manual/en/function.base64-encode.php |
|
202 | - * @param string $text_string the text to be encoded |
|
203 | - * @return string |
|
204 | - */ |
|
205 | - public function base64_string_encode($text_string = '') |
|
206 | - { |
|
207 | - // you give me nothing??? GET OUT ! |
|
208 | - if (empty($text_string) || ! $this->_use_base64_encode) { |
|
209 | - return $text_string; |
|
210 | - } |
|
211 | - // encode |
|
212 | - return base64_encode($text_string); |
|
213 | - } |
|
214 | - |
|
215 | - |
|
216 | - /** |
|
217 | - * decodes string that has been encoded with PHP's base64 encoding |
|
218 | - * |
|
219 | - * @see http://php.net/manual/en/function.base64-encode.php |
|
220 | - * @param string $encoded_string the text to be decoded |
|
221 | - * @return string |
|
222 | - * @throws RuntimeException |
|
223 | - */ |
|
224 | - public function base64_string_decode($encoded_string = '') |
|
225 | - { |
|
226 | - // you give me nothing??? GET OUT ! |
|
227 | - if (empty($encoded_string) || ! $this->valid_base_64($encoded_string)) { |
|
228 | - return $encoded_string; |
|
229 | - } |
|
230 | - // decode |
|
231 | - $decoded_string = base64_decode($encoded_string); |
|
232 | - if ($decoded_string === false) { |
|
233 | - throw new RuntimeException( |
|
234 | - esc_html__('Base 64 decoding failed.', 'event_espresso') |
|
235 | - ); |
|
236 | - } |
|
237 | - return $decoded_string; |
|
238 | - } |
|
239 | - |
|
240 | - |
|
241 | - /** |
|
242 | - * encodes url string with PHP's base64 encoding |
|
243 | - * |
|
244 | - * @see http://php.net/manual/en/function.base64-encode.php |
|
245 | - * @param string $text_string the text to be encoded |
|
246 | - * @return string |
|
247 | - */ |
|
248 | - public function base64_url_encode($text_string = '') |
|
249 | - { |
|
250 | - // you give me nothing??? GET OUT ! |
|
251 | - if (empty($text_string) || ! $this->_use_base64_encode) { |
|
252 | - return $text_string; |
|
253 | - } |
|
254 | - // encode |
|
255 | - $encoded_string = base64_encode($text_string); |
|
256 | - // remove chars to make encoding more URL friendly |
|
257 | - return strtr($encoded_string, '+/=', '-_,'); |
|
258 | - } |
|
259 | - |
|
260 | - |
|
261 | - /** |
|
262 | - * decodes url string that has been encoded with PHP's base64 encoding |
|
263 | - * |
|
264 | - * @see http://php.net/manual/en/function.base64-encode.php |
|
265 | - * @param string $encoded_string the text to be decoded |
|
266 | - * @return string |
|
267 | - * @throws RuntimeException |
|
268 | - */ |
|
269 | - public function base64_url_decode($encoded_string = '') |
|
270 | - { |
|
271 | - // replace previously removed characters |
|
272 | - $encoded_string = strtr($encoded_string, '-_,', '+/='); |
|
273 | - // you give me nothing??? GET OUT ! |
|
274 | - if (empty($encoded_string) || ! $this->valid_base_64($encoded_string)) { |
|
275 | - return $encoded_string; |
|
276 | - } |
|
277 | - // decode |
|
278 | - $decoded_string = base64_decode($encoded_string); |
|
279 | - if ($decoded_string === false) { |
|
280 | - throw new RuntimeException( |
|
281 | - esc_html__('Base 64 decoding failed.', 'event_espresso') |
|
282 | - ); |
|
283 | - } |
|
284 | - return $decoded_string; |
|
285 | - } |
|
286 | - |
|
287 | - |
|
288 | - /** |
|
289 | - * encrypts data using PHP's openssl functions |
|
290 | - * |
|
291 | - * @param string $text_string the text to be encrypted |
|
292 | - * @param string $cipher_method |
|
293 | - * @param string $encryption_key |
|
294 | - * @return string |
|
295 | - * @throws RuntimeException |
|
296 | - */ |
|
297 | - protected function openssl_encrypt( |
|
298 | - $text_string = '', |
|
299 | - $cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD, |
|
300 | - $encryption_key = '' |
|
301 | - ) { |
|
302 | - // you give me nothing??? GET OUT ! |
|
303 | - if (empty($text_string)) { |
|
304 | - return $text_string; |
|
305 | - } |
|
306 | - $this->cipher_method = $this->getCipherMethod($cipher_method); |
|
307 | - // get initialization vector size |
|
308 | - $iv_size = openssl_cipher_iv_length($this->cipher_method); |
|
309 | - // generate initialization vector. |
|
310 | - // The second parameter ("crypto_strong") is passed by reference, |
|
311 | - // and is used to determines if the algorithm used was "cryptographically strong" |
|
312 | - // openssl_random_pseudo_bytes() will toggle it to either true or false |
|
313 | - $iv = openssl_random_pseudo_bytes($iv_size, $is_strong); |
|
314 | - if ($iv === false || $is_strong === false) { |
|
315 | - throw new RuntimeException( |
|
316 | - esc_html__('Failed to generate OpenSSL initialization vector.', 'event_espresso') |
|
317 | - ); |
|
318 | - } |
|
319 | - // encrypt it |
|
320 | - $encrypted_text = openssl_encrypt( |
|
321 | - $text_string, |
|
322 | - $this->cipher_method, |
|
323 | - $this->getDigestHashValue(EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key), |
|
324 | - 0, |
|
325 | - $iv |
|
326 | - ); |
|
327 | - // append the initialization vector |
|
328 | - $encrypted_text .= EE_Encryption::OPENSSL_IV_DELIMITER . $iv; |
|
329 | - // trim and maybe encode |
|
330 | - return $this->_use_base64_encode |
|
331 | - ? trim(base64_encode($encrypted_text)) |
|
332 | - : trim($encrypted_text); |
|
333 | - } |
|
334 | - |
|
335 | - |
|
336 | - /** |
|
337 | - * Returns a cipher method that has been verified to work. |
|
338 | - * First checks if the cached cipher has been set already and if so, returns that. |
|
339 | - * Then tests the incoming default and returns that if it's good. |
|
340 | - * If not, then it retrieves the previously tested and saved cipher method. |
|
341 | - * But if that doesn't exist, then calls getAvailableCipherMethod() |
|
342 | - * to see what is available on the server, and returns the results. |
|
343 | - * |
|
344 | - * @param string $cipher_method |
|
345 | - * @return string |
|
346 | - * @throws RuntimeException |
|
347 | - */ |
|
348 | - protected function getCipherMethod($cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD) |
|
349 | - { |
|
350 | - if ($this->cipher_method !== '') { |
|
351 | - return $this->cipher_method; |
|
352 | - } |
|
353 | - // verify that the default cipher method can produce an initialization vector |
|
354 | - if (openssl_cipher_iv_length($cipher_method) === false) { |
|
355 | - // nope? okay let's get what we found in the past to work |
|
356 | - $cipher_method = get_option(EE_Encryption::OPENSSL_CIPHER_METHOD_OPTION_NAME, ''); |
|
357 | - // oops... haven't tested available cipher methods yet |
|
358 | - if ($cipher_method === '' || openssl_cipher_iv_length($cipher_method) === false) { |
|
359 | - $cipher_method = $this->getAvailableCipherMethod($cipher_method); |
|
360 | - } |
|
361 | - } |
|
362 | - return $cipher_method; |
|
363 | - } |
|
364 | - |
|
365 | - |
|
366 | - /** |
|
367 | - * @param string $cipher_method |
|
368 | - * @return string |
|
369 | - * @throws \RuntimeException |
|
370 | - */ |
|
371 | - protected function getAvailableCipherMethod($cipher_method) |
|
372 | - { |
|
373 | - // verify that the incoming cipher method can produce an initialization vector |
|
374 | - if (openssl_cipher_iv_length($cipher_method) === false) { |
|
375 | - // nope? then check the next cipher in the list of available cipher methods |
|
376 | - $cipher_method = next($this->cipher_methods); |
|
377 | - // what? there's no list? then generate that list and cache it, |
|
378 | - if (empty($this->cipher_methods)) { |
|
379 | - $this->cipher_methods = openssl_get_cipher_methods(); |
|
380 | - // then grab the first item from the list |
|
381 | - $cipher_method = reset($this->cipher_methods); |
|
382 | - } |
|
383 | - if ($cipher_method === false) { |
|
384 | - throw new RuntimeException( |
|
385 | - esc_html__( |
|
386 | - 'OpenSSL support appears to be enabled on the server, but no cipher methods are available. Please contact the server administrator.', |
|
387 | - 'event_espresso' |
|
388 | - ) |
|
389 | - ); |
|
390 | - } |
|
391 | - // verify that the next cipher method works |
|
392 | - return $this->getAvailableCipherMethod($cipher_method); |
|
393 | - } |
|
394 | - // if we've gotten this far, then we found an available cipher method that works |
|
395 | - // so save that for next time |
|
396 | - update_option( |
|
397 | - EE_Encryption::OPENSSL_CIPHER_METHOD_OPTION_NAME, |
|
398 | - $cipher_method |
|
399 | - ); |
|
400 | - return $cipher_method; |
|
401 | - } |
|
402 | - |
|
403 | - |
|
404 | - /** |
|
405 | - * decrypts data that has been encrypted with PHP's openssl functions |
|
406 | - * |
|
407 | - * @param string $encrypted_text the text to be decrypted |
|
408 | - * @param string $cipher_method |
|
409 | - * @param string $encryption_key |
|
410 | - * @return string |
|
411 | - * @throws RuntimeException |
|
412 | - */ |
|
413 | - protected function openssl_decrypt( |
|
414 | - $encrypted_text = '', |
|
415 | - $cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD, |
|
416 | - $encryption_key = '' |
|
417 | - ) { |
|
418 | - // you give me nothing??? GET OUT ! |
|
419 | - if (empty($encrypted_text)) { |
|
420 | - return $encrypted_text; |
|
421 | - } |
|
422 | - // decode |
|
423 | - $encrypted_text = $this->valid_base_64($encrypted_text) |
|
424 | - ? $this->base64_url_decode($encrypted_text) |
|
425 | - : $encrypted_text; |
|
426 | - $encrypted_components = explode( |
|
427 | - EE_Encryption::OPENSSL_IV_DELIMITER, |
|
428 | - $encrypted_text, |
|
429 | - 2 |
|
430 | - ); |
|
431 | - // decrypt it |
|
432 | - $decrypted_text = openssl_decrypt( |
|
433 | - $encrypted_components[0], |
|
434 | - $this->getCipherMethod($cipher_method), |
|
435 | - $this->getDigestHashValue(EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key), |
|
436 | - 0, |
|
437 | - $encrypted_components[1] |
|
438 | - ); |
|
439 | - $decrypted_text = trim($decrypted_text); |
|
440 | - return $decrypted_text; |
|
441 | - } |
|
442 | - |
|
443 | - |
|
444 | - /** |
|
445 | - * Computes the digest hash value using the specified digest method. |
|
446 | - * If that digest method fails to produce a valid hash value, |
|
447 | - * then we'll grab the next digest method and recursively try again until something works. |
|
448 | - * |
|
449 | - * @param string $digest_method |
|
450 | - * @param string $encryption_key |
|
451 | - * @return string |
|
452 | - * @throws RuntimeException |
|
453 | - */ |
|
454 | - protected function getDigestHashValue($digest_method = EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key = '') |
|
455 | - { |
|
456 | - $encryption_key = $encryption_key !== '' |
|
457 | - ? $encryption_key |
|
458 | - : $this->get_encryption_key(); |
|
459 | - $digest_hash_value = openssl_digest($encryption_key, $digest_method); |
|
460 | - if ($digest_hash_value === false) { |
|
461 | - return $this->getDigestHashValue($this->getDigestMethod()); |
|
462 | - } |
|
463 | - return $digest_hash_value; |
|
464 | - } |
|
465 | - |
|
466 | - |
|
467 | - /** |
|
468 | - * Returns the NEXT element in the $digest_methods array. |
|
469 | - * If the $digest_methods array is empty, then we populate it |
|
470 | - * with the available values returned from openssl_get_md_methods(). |
|
471 | - * |
|
472 | - * @return string |
|
473 | - * @throws \RuntimeException |
|
474 | - */ |
|
475 | - protected function getDigestMethod() |
|
476 | - { |
|
477 | - $digest_method = prev($this->digest_methods); |
|
478 | - if (empty($this->digest_methods)) { |
|
479 | - $this->digest_methods = openssl_get_md_methods(); |
|
480 | - $digest_method = end($this->digest_methods); |
|
481 | - } |
|
482 | - if ($digest_method === false) { |
|
483 | - throw new RuntimeException( |
|
484 | - esc_html__( |
|
485 | - 'OpenSSL support appears to be enabled on the server, but no digest methods are available. Please contact the server administrator.', |
|
486 | - 'event_espresso' |
|
487 | - ) |
|
488 | - ); |
|
489 | - } |
|
490 | - return $digest_method; |
|
491 | - } |
|
492 | - |
|
493 | - |
|
494 | - /** |
|
495 | - * encrypts data for acme servers that didn't bother to install PHP mcrypt |
|
496 | - * |
|
497 | - * @see http://stackoverflow.com/questions/800922/how-to-encrypt-string-without-mcrypt-library-in-php |
|
498 | - * @param string $text_string the text to be decrypted |
|
499 | - * @return string |
|
500 | - */ |
|
501 | - protected function acme_encrypt($text_string = '') |
|
502 | - { |
|
503 | - // you give me nothing??? GET OUT ! |
|
504 | - if (empty($text_string)) { |
|
505 | - return $text_string; |
|
506 | - } |
|
507 | - $key_bits = str_split( |
|
508 | - str_pad( |
|
509 | - '', |
|
510 | - strlen($text_string), |
|
511 | - $this->get_encryption_key(), |
|
512 | - STR_PAD_RIGHT |
|
513 | - ) |
|
514 | - ); |
|
515 | - $string_bits = str_split($text_string); |
|
516 | - foreach ($string_bits as $k => $v) { |
|
517 | - $temp = ord($v) + ord($key_bits[ $k ]); |
|
518 | - $string_bits[ $k ] = chr($temp > 255 ? ($temp - 256) : $temp); |
|
519 | - } |
|
520 | - $encrypted_text = implode('', $string_bits); |
|
521 | - $encrypted_text .= EE_Encryption::ACME_ENCRYPTION_FLAG; |
|
522 | - return $this->_use_base64_encode |
|
523 | - ? base64_encode($encrypted_text) |
|
524 | - : $encrypted_text; |
|
525 | - } |
|
526 | - |
|
527 | - |
|
528 | - /** |
|
529 | - * decrypts data for acme servers that didn't bother to install PHP mcrypt |
|
530 | - * |
|
531 | - * @see http://stackoverflow.com/questions/800922/how-to-encrypt-string-without-mcrypt-library-in-php |
|
532 | - * @param string $encrypted_text the text to be decrypted |
|
533 | - * @return string |
|
534 | - * @throws RuntimeException |
|
535 | - */ |
|
536 | - protected function acme_decrypt($encrypted_text = '') |
|
537 | - { |
|
538 | - // you give me nothing??? GET OUT ! |
|
539 | - if (empty($encrypted_text)) { |
|
540 | - return $encrypted_text; |
|
541 | - } |
|
542 | - // decode the data ? |
|
543 | - $encrypted_text = $this->valid_base_64($encrypted_text) |
|
544 | - ? $this->base64_url_decode($encrypted_text) |
|
545 | - : $encrypted_text; |
|
546 | - $encrypted_text = substr($encrypted_text, 0, -4); |
|
547 | - $key_bits = str_split( |
|
548 | - str_pad( |
|
549 | - '', |
|
550 | - strlen($encrypted_text), |
|
551 | - $this->get_encryption_key(), |
|
552 | - STR_PAD_RIGHT |
|
553 | - ) |
|
554 | - ); |
|
555 | - $string_bits = str_split($encrypted_text); |
|
556 | - foreach ($string_bits as $k => $v) { |
|
557 | - $temp = ord($v) - ord($key_bits[ $k ]); |
|
558 | - $string_bits[ $k ] = chr($temp < 0 ? ($temp + 256) : $temp); |
|
559 | - } |
|
560 | - return implode('', $string_bits); |
|
561 | - } |
|
562 | - |
|
563 | - |
|
564 | - /** |
|
565 | - * @see http://stackoverflow.com/questions/2556345/detect-base64-encoding-in-php#30231906 |
|
566 | - * @param $string |
|
567 | - * @return bool |
|
568 | - */ |
|
569 | - protected function valid_base_64($string) |
|
570 | - { |
|
571 | - // ensure data is a string |
|
572 | - if (! is_string($string) || ! $this->_use_base64_encode) { |
|
573 | - return false; |
|
574 | - } |
|
575 | - $decoded = base64_decode($string, true); |
|
576 | - // Check if there is no invalid character in string |
|
577 | - if (! preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $string)) { |
|
578 | - return false; |
|
579 | - } |
|
580 | - // Decode the string in strict mode and send the response |
|
581 | - if (! base64_decode($string, true)) { |
|
582 | - return false; |
|
583 | - } |
|
584 | - // Encode and compare it to original one |
|
585 | - return base64_encode($decoded) === $string; |
|
586 | - } |
|
587 | - |
|
588 | - |
|
589 | - /** |
|
590 | - * generate random string |
|
591 | - * |
|
592 | - * @see http://stackoverflow.com/questions/637278/what-is-the-best-way-to-generate-a-random-key-within-php |
|
593 | - * @param int $length number of characters for random string |
|
594 | - * @return string |
|
595 | - */ |
|
596 | - public function generate_random_string($length = 40) |
|
597 | - { |
|
598 | - $iterations = ceil($length / 40); |
|
599 | - $random_string = ''; |
|
600 | - for ($i = 0; $i < $iterations; $i++) { |
|
601 | - $random_string .= sha1(microtime(true) . mt_rand(10000, 90000)); |
|
602 | - } |
|
603 | - $random_string = substr($random_string, 0, $length); |
|
604 | - return $random_string; |
|
605 | - } |
|
28 | + /** |
|
29 | + * key used for saving the encryption key to the wp_options table |
|
30 | + */ |
|
31 | + const ENCRYPTION_OPTION_KEY = 'ee_encryption_key'; |
|
32 | + |
|
33 | + /** |
|
34 | + * the OPENSSL cipher method used |
|
35 | + */ |
|
36 | + const OPENSSL_CIPHER_METHOD = 'AES-128-CBC'; |
|
37 | + |
|
38 | + /** |
|
39 | + * WP "options_name" used to store a verified available cipher method |
|
40 | + */ |
|
41 | + const OPENSSL_CIPHER_METHOD_OPTION_NAME = 'ee_openssl_cipher_method'; |
|
42 | + |
|
43 | + /** |
|
44 | + * the OPENSSL digest method used |
|
45 | + */ |
|
46 | + const OPENSSL_DIGEST_METHOD = 'sha512'; |
|
47 | + |
|
48 | + /** |
|
49 | + * separates the encrypted text from the initialization vector |
|
50 | + */ |
|
51 | + const OPENSSL_IV_DELIMITER = ':iv:'; |
|
52 | + |
|
53 | + /** |
|
54 | + * appended to text encrypted using the acme encryption |
|
55 | + */ |
|
56 | + const ACME_ENCRYPTION_FLAG = '::ae'; |
|
57 | + |
|
58 | + |
|
59 | + /** |
|
60 | + * instance of the EE_Encryption object |
|
61 | + */ |
|
62 | + protected static $_instance; |
|
63 | + |
|
64 | + /** |
|
65 | + * @var string $_encryption_key |
|
66 | + */ |
|
67 | + protected $_encryption_key; |
|
68 | + |
|
69 | + /** |
|
70 | + * @var string $cipher_method |
|
71 | + */ |
|
72 | + private $cipher_method = ''; |
|
73 | + |
|
74 | + /** |
|
75 | + * @var array $cipher_methods |
|
76 | + */ |
|
77 | + private $cipher_methods = []; |
|
78 | + |
|
79 | + /** |
|
80 | + * @var array $digest_methods |
|
81 | + */ |
|
82 | + private $digest_methods = []; |
|
83 | + |
|
84 | + /** |
|
85 | + * @var boolean $_use_openssl_encrypt |
|
86 | + */ |
|
87 | + protected $_use_openssl_encrypt = false; |
|
88 | + |
|
89 | + /** |
|
90 | + * @var boolean $_use_base64_encode |
|
91 | + */ |
|
92 | + protected $_use_base64_encode = false; |
|
93 | + |
|
94 | + |
|
95 | + /** |
|
96 | + * protected constructor to prevent direct creation |
|
97 | + */ |
|
98 | + protected function __construct() |
|
99 | + { |
|
100 | + if (! defined('ESPRESSO_ENCRYPT')) { |
|
101 | + define('ESPRESSO_ENCRYPT', true); |
|
102 | + } |
|
103 | + if (extension_loaded('openssl')) { |
|
104 | + $this->_use_openssl_encrypt = true; |
|
105 | + } |
|
106 | + if (function_exists('base64_encode')) { |
|
107 | + $this->_use_base64_encode = true; |
|
108 | + } |
|
109 | + EE_Error::doing_it_wrong(__METHOD__, esc_html__('Usage is deprecated.', 'event_espresso'), '5.0.8.p'); |
|
110 | + } |
|
111 | + |
|
112 | + |
|
113 | + /** |
|
114 | + * singleton method used to instantiate class object |
|
115 | + * |
|
116 | + * @return EE_Encryption |
|
117 | + */ |
|
118 | + public static function instance() |
|
119 | + { |
|
120 | + // check if class object is instantiated |
|
121 | + if (! EE_Encryption::$_instance instanceof EE_Encryption) { |
|
122 | + EE_Encryption::$_instance = new self(); |
|
123 | + } |
|
124 | + return EE_Encryption::$_instance; |
|
125 | + } |
|
126 | + |
|
127 | + |
|
128 | + /** |
|
129 | + * get encryption key |
|
130 | + * |
|
131 | + * @return string |
|
132 | + */ |
|
133 | + public function get_encryption_key() |
|
134 | + { |
|
135 | + // if encryption key has not been set |
|
136 | + if (empty($this->_encryption_key)) { |
|
137 | + // retrieve encryption_key from db |
|
138 | + $this->_encryption_key = get_option(EE_Encryption::ENCRYPTION_OPTION_KEY, ''); |
|
139 | + // WHAT?? No encryption_key in the db ?? |
|
140 | + if ($this->_encryption_key === '') { |
|
141 | + // let's make one. And md5 it to make it just the right size for a key |
|
142 | + $new_key = md5($this->generate_random_string()); |
|
143 | + // now save it to the db for later |
|
144 | + add_option(EE_Encryption::ENCRYPTION_OPTION_KEY, $new_key); |
|
145 | + // here's the key - FINALLY ! |
|
146 | + $this->_encryption_key = $new_key; |
|
147 | + } |
|
148 | + } |
|
149 | + return $this->_encryption_key; |
|
150 | + } |
|
151 | + |
|
152 | + |
|
153 | + /** |
|
154 | + * encrypts data |
|
155 | + * |
|
156 | + * @param string $text_string - the text to be encrypted |
|
157 | + * @return string |
|
158 | + * @throws RuntimeException |
|
159 | + */ |
|
160 | + public function encrypt($text_string = '') |
|
161 | + { |
|
162 | + // you give me nothing??? GET OUT ! |
|
163 | + if (empty($text_string)) { |
|
164 | + return $text_string; |
|
165 | + } |
|
166 | + if ($this->_use_openssl_encrypt) { |
|
167 | + $encrypted_text = $this->openssl_encrypt($text_string); |
|
168 | + } else { |
|
169 | + $encrypted_text = $this->acme_encrypt($text_string); |
|
170 | + } |
|
171 | + return $encrypted_text; |
|
172 | + } |
|
173 | + |
|
174 | + |
|
175 | + /** |
|
176 | + * decrypts data |
|
177 | + * |
|
178 | + * @param string $encrypted_text - the text to be decrypted |
|
179 | + * @return string |
|
180 | + * @throws RuntimeException |
|
181 | + */ |
|
182 | + public function decrypt($encrypted_text = '') |
|
183 | + { |
|
184 | + // you give me nothing??? GET OUT ! |
|
185 | + if (empty($encrypted_text)) { |
|
186 | + return $encrypted_text; |
|
187 | + } |
|
188 | + // if PHP's mcrypt functions are installed then we'll use them |
|
189 | + if ($this->_use_openssl_encrypt) { |
|
190 | + $decrypted_text = $this->openssl_decrypt($encrypted_text); |
|
191 | + } else { |
|
192 | + $decrypted_text = $this->acme_decrypt($encrypted_text); |
|
193 | + } |
|
194 | + return $decrypted_text; |
|
195 | + } |
|
196 | + |
|
197 | + |
|
198 | + /** |
|
199 | + * encodes string with PHP's base64 encoding |
|
200 | + * |
|
201 | + * @see http://php.net/manual/en/function.base64-encode.php |
|
202 | + * @param string $text_string the text to be encoded |
|
203 | + * @return string |
|
204 | + */ |
|
205 | + public function base64_string_encode($text_string = '') |
|
206 | + { |
|
207 | + // you give me nothing??? GET OUT ! |
|
208 | + if (empty($text_string) || ! $this->_use_base64_encode) { |
|
209 | + return $text_string; |
|
210 | + } |
|
211 | + // encode |
|
212 | + return base64_encode($text_string); |
|
213 | + } |
|
214 | + |
|
215 | + |
|
216 | + /** |
|
217 | + * decodes string that has been encoded with PHP's base64 encoding |
|
218 | + * |
|
219 | + * @see http://php.net/manual/en/function.base64-encode.php |
|
220 | + * @param string $encoded_string the text to be decoded |
|
221 | + * @return string |
|
222 | + * @throws RuntimeException |
|
223 | + */ |
|
224 | + public function base64_string_decode($encoded_string = '') |
|
225 | + { |
|
226 | + // you give me nothing??? GET OUT ! |
|
227 | + if (empty($encoded_string) || ! $this->valid_base_64($encoded_string)) { |
|
228 | + return $encoded_string; |
|
229 | + } |
|
230 | + // decode |
|
231 | + $decoded_string = base64_decode($encoded_string); |
|
232 | + if ($decoded_string === false) { |
|
233 | + throw new RuntimeException( |
|
234 | + esc_html__('Base 64 decoding failed.', 'event_espresso') |
|
235 | + ); |
|
236 | + } |
|
237 | + return $decoded_string; |
|
238 | + } |
|
239 | + |
|
240 | + |
|
241 | + /** |
|
242 | + * encodes url string with PHP's base64 encoding |
|
243 | + * |
|
244 | + * @see http://php.net/manual/en/function.base64-encode.php |
|
245 | + * @param string $text_string the text to be encoded |
|
246 | + * @return string |
|
247 | + */ |
|
248 | + public function base64_url_encode($text_string = '') |
|
249 | + { |
|
250 | + // you give me nothing??? GET OUT ! |
|
251 | + if (empty($text_string) || ! $this->_use_base64_encode) { |
|
252 | + return $text_string; |
|
253 | + } |
|
254 | + // encode |
|
255 | + $encoded_string = base64_encode($text_string); |
|
256 | + // remove chars to make encoding more URL friendly |
|
257 | + return strtr($encoded_string, '+/=', '-_,'); |
|
258 | + } |
|
259 | + |
|
260 | + |
|
261 | + /** |
|
262 | + * decodes url string that has been encoded with PHP's base64 encoding |
|
263 | + * |
|
264 | + * @see http://php.net/manual/en/function.base64-encode.php |
|
265 | + * @param string $encoded_string the text to be decoded |
|
266 | + * @return string |
|
267 | + * @throws RuntimeException |
|
268 | + */ |
|
269 | + public function base64_url_decode($encoded_string = '') |
|
270 | + { |
|
271 | + // replace previously removed characters |
|
272 | + $encoded_string = strtr($encoded_string, '-_,', '+/='); |
|
273 | + // you give me nothing??? GET OUT ! |
|
274 | + if (empty($encoded_string) || ! $this->valid_base_64($encoded_string)) { |
|
275 | + return $encoded_string; |
|
276 | + } |
|
277 | + // decode |
|
278 | + $decoded_string = base64_decode($encoded_string); |
|
279 | + if ($decoded_string === false) { |
|
280 | + throw new RuntimeException( |
|
281 | + esc_html__('Base 64 decoding failed.', 'event_espresso') |
|
282 | + ); |
|
283 | + } |
|
284 | + return $decoded_string; |
|
285 | + } |
|
286 | + |
|
287 | + |
|
288 | + /** |
|
289 | + * encrypts data using PHP's openssl functions |
|
290 | + * |
|
291 | + * @param string $text_string the text to be encrypted |
|
292 | + * @param string $cipher_method |
|
293 | + * @param string $encryption_key |
|
294 | + * @return string |
|
295 | + * @throws RuntimeException |
|
296 | + */ |
|
297 | + protected function openssl_encrypt( |
|
298 | + $text_string = '', |
|
299 | + $cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD, |
|
300 | + $encryption_key = '' |
|
301 | + ) { |
|
302 | + // you give me nothing??? GET OUT ! |
|
303 | + if (empty($text_string)) { |
|
304 | + return $text_string; |
|
305 | + } |
|
306 | + $this->cipher_method = $this->getCipherMethod($cipher_method); |
|
307 | + // get initialization vector size |
|
308 | + $iv_size = openssl_cipher_iv_length($this->cipher_method); |
|
309 | + // generate initialization vector. |
|
310 | + // The second parameter ("crypto_strong") is passed by reference, |
|
311 | + // and is used to determines if the algorithm used was "cryptographically strong" |
|
312 | + // openssl_random_pseudo_bytes() will toggle it to either true or false |
|
313 | + $iv = openssl_random_pseudo_bytes($iv_size, $is_strong); |
|
314 | + if ($iv === false || $is_strong === false) { |
|
315 | + throw new RuntimeException( |
|
316 | + esc_html__('Failed to generate OpenSSL initialization vector.', 'event_espresso') |
|
317 | + ); |
|
318 | + } |
|
319 | + // encrypt it |
|
320 | + $encrypted_text = openssl_encrypt( |
|
321 | + $text_string, |
|
322 | + $this->cipher_method, |
|
323 | + $this->getDigestHashValue(EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key), |
|
324 | + 0, |
|
325 | + $iv |
|
326 | + ); |
|
327 | + // append the initialization vector |
|
328 | + $encrypted_text .= EE_Encryption::OPENSSL_IV_DELIMITER . $iv; |
|
329 | + // trim and maybe encode |
|
330 | + return $this->_use_base64_encode |
|
331 | + ? trim(base64_encode($encrypted_text)) |
|
332 | + : trim($encrypted_text); |
|
333 | + } |
|
334 | + |
|
335 | + |
|
336 | + /** |
|
337 | + * Returns a cipher method that has been verified to work. |
|
338 | + * First checks if the cached cipher has been set already and if so, returns that. |
|
339 | + * Then tests the incoming default and returns that if it's good. |
|
340 | + * If not, then it retrieves the previously tested and saved cipher method. |
|
341 | + * But if that doesn't exist, then calls getAvailableCipherMethod() |
|
342 | + * to see what is available on the server, and returns the results. |
|
343 | + * |
|
344 | + * @param string $cipher_method |
|
345 | + * @return string |
|
346 | + * @throws RuntimeException |
|
347 | + */ |
|
348 | + protected function getCipherMethod($cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD) |
|
349 | + { |
|
350 | + if ($this->cipher_method !== '') { |
|
351 | + return $this->cipher_method; |
|
352 | + } |
|
353 | + // verify that the default cipher method can produce an initialization vector |
|
354 | + if (openssl_cipher_iv_length($cipher_method) === false) { |
|
355 | + // nope? okay let's get what we found in the past to work |
|
356 | + $cipher_method = get_option(EE_Encryption::OPENSSL_CIPHER_METHOD_OPTION_NAME, ''); |
|
357 | + // oops... haven't tested available cipher methods yet |
|
358 | + if ($cipher_method === '' || openssl_cipher_iv_length($cipher_method) === false) { |
|
359 | + $cipher_method = $this->getAvailableCipherMethod($cipher_method); |
|
360 | + } |
|
361 | + } |
|
362 | + return $cipher_method; |
|
363 | + } |
|
364 | + |
|
365 | + |
|
366 | + /** |
|
367 | + * @param string $cipher_method |
|
368 | + * @return string |
|
369 | + * @throws \RuntimeException |
|
370 | + */ |
|
371 | + protected function getAvailableCipherMethod($cipher_method) |
|
372 | + { |
|
373 | + // verify that the incoming cipher method can produce an initialization vector |
|
374 | + if (openssl_cipher_iv_length($cipher_method) === false) { |
|
375 | + // nope? then check the next cipher in the list of available cipher methods |
|
376 | + $cipher_method = next($this->cipher_methods); |
|
377 | + // what? there's no list? then generate that list and cache it, |
|
378 | + if (empty($this->cipher_methods)) { |
|
379 | + $this->cipher_methods = openssl_get_cipher_methods(); |
|
380 | + // then grab the first item from the list |
|
381 | + $cipher_method = reset($this->cipher_methods); |
|
382 | + } |
|
383 | + if ($cipher_method === false) { |
|
384 | + throw new RuntimeException( |
|
385 | + esc_html__( |
|
386 | + 'OpenSSL support appears to be enabled on the server, but no cipher methods are available. Please contact the server administrator.', |
|
387 | + 'event_espresso' |
|
388 | + ) |
|
389 | + ); |
|
390 | + } |
|
391 | + // verify that the next cipher method works |
|
392 | + return $this->getAvailableCipherMethod($cipher_method); |
|
393 | + } |
|
394 | + // if we've gotten this far, then we found an available cipher method that works |
|
395 | + // so save that for next time |
|
396 | + update_option( |
|
397 | + EE_Encryption::OPENSSL_CIPHER_METHOD_OPTION_NAME, |
|
398 | + $cipher_method |
|
399 | + ); |
|
400 | + return $cipher_method; |
|
401 | + } |
|
402 | + |
|
403 | + |
|
404 | + /** |
|
405 | + * decrypts data that has been encrypted with PHP's openssl functions |
|
406 | + * |
|
407 | + * @param string $encrypted_text the text to be decrypted |
|
408 | + * @param string $cipher_method |
|
409 | + * @param string $encryption_key |
|
410 | + * @return string |
|
411 | + * @throws RuntimeException |
|
412 | + */ |
|
413 | + protected function openssl_decrypt( |
|
414 | + $encrypted_text = '', |
|
415 | + $cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD, |
|
416 | + $encryption_key = '' |
|
417 | + ) { |
|
418 | + // you give me nothing??? GET OUT ! |
|
419 | + if (empty($encrypted_text)) { |
|
420 | + return $encrypted_text; |
|
421 | + } |
|
422 | + // decode |
|
423 | + $encrypted_text = $this->valid_base_64($encrypted_text) |
|
424 | + ? $this->base64_url_decode($encrypted_text) |
|
425 | + : $encrypted_text; |
|
426 | + $encrypted_components = explode( |
|
427 | + EE_Encryption::OPENSSL_IV_DELIMITER, |
|
428 | + $encrypted_text, |
|
429 | + 2 |
|
430 | + ); |
|
431 | + // decrypt it |
|
432 | + $decrypted_text = openssl_decrypt( |
|
433 | + $encrypted_components[0], |
|
434 | + $this->getCipherMethod($cipher_method), |
|
435 | + $this->getDigestHashValue(EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key), |
|
436 | + 0, |
|
437 | + $encrypted_components[1] |
|
438 | + ); |
|
439 | + $decrypted_text = trim($decrypted_text); |
|
440 | + return $decrypted_text; |
|
441 | + } |
|
442 | + |
|
443 | + |
|
444 | + /** |
|
445 | + * Computes the digest hash value using the specified digest method. |
|
446 | + * If that digest method fails to produce a valid hash value, |
|
447 | + * then we'll grab the next digest method and recursively try again until something works. |
|
448 | + * |
|
449 | + * @param string $digest_method |
|
450 | + * @param string $encryption_key |
|
451 | + * @return string |
|
452 | + * @throws RuntimeException |
|
453 | + */ |
|
454 | + protected function getDigestHashValue($digest_method = EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key = '') |
|
455 | + { |
|
456 | + $encryption_key = $encryption_key !== '' |
|
457 | + ? $encryption_key |
|
458 | + : $this->get_encryption_key(); |
|
459 | + $digest_hash_value = openssl_digest($encryption_key, $digest_method); |
|
460 | + if ($digest_hash_value === false) { |
|
461 | + return $this->getDigestHashValue($this->getDigestMethod()); |
|
462 | + } |
|
463 | + return $digest_hash_value; |
|
464 | + } |
|
465 | + |
|
466 | + |
|
467 | + /** |
|
468 | + * Returns the NEXT element in the $digest_methods array. |
|
469 | + * If the $digest_methods array is empty, then we populate it |
|
470 | + * with the available values returned from openssl_get_md_methods(). |
|
471 | + * |
|
472 | + * @return string |
|
473 | + * @throws \RuntimeException |
|
474 | + */ |
|
475 | + protected function getDigestMethod() |
|
476 | + { |
|
477 | + $digest_method = prev($this->digest_methods); |
|
478 | + if (empty($this->digest_methods)) { |
|
479 | + $this->digest_methods = openssl_get_md_methods(); |
|
480 | + $digest_method = end($this->digest_methods); |
|
481 | + } |
|
482 | + if ($digest_method === false) { |
|
483 | + throw new RuntimeException( |
|
484 | + esc_html__( |
|
485 | + 'OpenSSL support appears to be enabled on the server, but no digest methods are available. Please contact the server administrator.', |
|
486 | + 'event_espresso' |
|
487 | + ) |
|
488 | + ); |
|
489 | + } |
|
490 | + return $digest_method; |
|
491 | + } |
|
492 | + |
|
493 | + |
|
494 | + /** |
|
495 | + * encrypts data for acme servers that didn't bother to install PHP mcrypt |
|
496 | + * |
|
497 | + * @see http://stackoverflow.com/questions/800922/how-to-encrypt-string-without-mcrypt-library-in-php |
|
498 | + * @param string $text_string the text to be decrypted |
|
499 | + * @return string |
|
500 | + */ |
|
501 | + protected function acme_encrypt($text_string = '') |
|
502 | + { |
|
503 | + // you give me nothing??? GET OUT ! |
|
504 | + if (empty($text_string)) { |
|
505 | + return $text_string; |
|
506 | + } |
|
507 | + $key_bits = str_split( |
|
508 | + str_pad( |
|
509 | + '', |
|
510 | + strlen($text_string), |
|
511 | + $this->get_encryption_key(), |
|
512 | + STR_PAD_RIGHT |
|
513 | + ) |
|
514 | + ); |
|
515 | + $string_bits = str_split($text_string); |
|
516 | + foreach ($string_bits as $k => $v) { |
|
517 | + $temp = ord($v) + ord($key_bits[ $k ]); |
|
518 | + $string_bits[ $k ] = chr($temp > 255 ? ($temp - 256) : $temp); |
|
519 | + } |
|
520 | + $encrypted_text = implode('', $string_bits); |
|
521 | + $encrypted_text .= EE_Encryption::ACME_ENCRYPTION_FLAG; |
|
522 | + return $this->_use_base64_encode |
|
523 | + ? base64_encode($encrypted_text) |
|
524 | + : $encrypted_text; |
|
525 | + } |
|
526 | + |
|
527 | + |
|
528 | + /** |
|
529 | + * decrypts data for acme servers that didn't bother to install PHP mcrypt |
|
530 | + * |
|
531 | + * @see http://stackoverflow.com/questions/800922/how-to-encrypt-string-without-mcrypt-library-in-php |
|
532 | + * @param string $encrypted_text the text to be decrypted |
|
533 | + * @return string |
|
534 | + * @throws RuntimeException |
|
535 | + */ |
|
536 | + protected function acme_decrypt($encrypted_text = '') |
|
537 | + { |
|
538 | + // you give me nothing??? GET OUT ! |
|
539 | + if (empty($encrypted_text)) { |
|
540 | + return $encrypted_text; |
|
541 | + } |
|
542 | + // decode the data ? |
|
543 | + $encrypted_text = $this->valid_base_64($encrypted_text) |
|
544 | + ? $this->base64_url_decode($encrypted_text) |
|
545 | + : $encrypted_text; |
|
546 | + $encrypted_text = substr($encrypted_text, 0, -4); |
|
547 | + $key_bits = str_split( |
|
548 | + str_pad( |
|
549 | + '', |
|
550 | + strlen($encrypted_text), |
|
551 | + $this->get_encryption_key(), |
|
552 | + STR_PAD_RIGHT |
|
553 | + ) |
|
554 | + ); |
|
555 | + $string_bits = str_split($encrypted_text); |
|
556 | + foreach ($string_bits as $k => $v) { |
|
557 | + $temp = ord($v) - ord($key_bits[ $k ]); |
|
558 | + $string_bits[ $k ] = chr($temp < 0 ? ($temp + 256) : $temp); |
|
559 | + } |
|
560 | + return implode('', $string_bits); |
|
561 | + } |
|
562 | + |
|
563 | + |
|
564 | + /** |
|
565 | + * @see http://stackoverflow.com/questions/2556345/detect-base64-encoding-in-php#30231906 |
|
566 | + * @param $string |
|
567 | + * @return bool |
|
568 | + */ |
|
569 | + protected function valid_base_64($string) |
|
570 | + { |
|
571 | + // ensure data is a string |
|
572 | + if (! is_string($string) || ! $this->_use_base64_encode) { |
|
573 | + return false; |
|
574 | + } |
|
575 | + $decoded = base64_decode($string, true); |
|
576 | + // Check if there is no invalid character in string |
|
577 | + if (! preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $string)) { |
|
578 | + return false; |
|
579 | + } |
|
580 | + // Decode the string in strict mode and send the response |
|
581 | + if (! base64_decode($string, true)) { |
|
582 | + return false; |
|
583 | + } |
|
584 | + // Encode and compare it to original one |
|
585 | + return base64_encode($decoded) === $string; |
|
586 | + } |
|
587 | + |
|
588 | + |
|
589 | + /** |
|
590 | + * generate random string |
|
591 | + * |
|
592 | + * @see http://stackoverflow.com/questions/637278/what-is-the-best-way-to-generate-a-random-key-within-php |
|
593 | + * @param int $length number of characters for random string |
|
594 | + * @return string |
|
595 | + */ |
|
596 | + public function generate_random_string($length = 40) |
|
597 | + { |
|
598 | + $iterations = ceil($length / 40); |
|
599 | + $random_string = ''; |
|
600 | + for ($i = 0; $i < $iterations; $i++) { |
|
601 | + $random_string .= sha1(microtime(true) . mt_rand(10000, 90000)); |
|
602 | + } |
|
603 | + $random_string = substr($random_string, 0, $length); |
|
604 | + return $random_string; |
|
605 | + } |
|
606 | 606 | } |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | protected function addData(string $key, $data) |
71 | 71 | { |
72 | 72 | if ($this->validator->propertyNotSet($this->data, $key)) { |
73 | - $this->data[ $key ] = $data; |
|
73 | + $this->data[$key] = $data; |
|
74 | 74 | } |
75 | 75 | } |
76 | 76 | |
@@ -168,7 +168,7 @@ discard block |
||
168 | 168 | EEH_Array::is_associative_array($this->data) |
169 | 169 | ? uasort( |
170 | 170 | $this->data, |
171 | - function ($a, $b) { |
|
171 | + function($a, $b) { |
|
172 | 172 | // check if each incoming argument is a node and if they have an order set |
173 | 173 | // if so, then use that for our sorting comparison. otherwise use the node's name... |
174 | 174 | // unless it's NOT a node, in which case use the arg value if it is scalar, or 0 if not. |
@@ -18,250 +18,250 @@ |
||
18 | 18 | */ |
19 | 19 | abstract class JsonDataNode implements JsonDataNodeInterface |
20 | 20 | { |
21 | - /** |
|
22 | - * @var JsonDataNodeValidator |
|
23 | - */ |
|
24 | - protected $validator; |
|
25 | - |
|
26 | - /** |
|
27 | - * @var array |
|
28 | - */ |
|
29 | - private $data = []; |
|
30 | - |
|
31 | - /** |
|
32 | - * @var string |
|
33 | - */ |
|
34 | - private $domain = ''; |
|
35 | - |
|
36 | - /** |
|
37 | - * @var boolean |
|
38 | - */ |
|
39 | - private $initialized = false; |
|
40 | - |
|
41 | - /** |
|
42 | - * @var string |
|
43 | - */ |
|
44 | - private $node_name = ''; |
|
45 | - |
|
46 | - /** |
|
47 | - * @var int |
|
48 | - */ |
|
49 | - private $order = 50; |
|
50 | - |
|
51 | - |
|
52 | - /** |
|
53 | - * @param JsonDataNodeValidator $validator |
|
54 | - * @throws DomainException |
|
55 | - */ |
|
56 | - public function __construct(JsonDataNodeValidator $validator) |
|
57 | - { |
|
58 | - $this->validator = $validator; |
|
59 | - } |
|
60 | - |
|
61 | - |
|
62 | - /** |
|
63 | - * for adding primitive data like arrays, integers, or strings |
|
64 | - * |
|
65 | - * @param string $key |
|
66 | - * @param mixed $data |
|
67 | - * @throws DomainException |
|
68 | - */ |
|
69 | - protected function addData(string $key, $data) |
|
70 | - { |
|
71 | - if ($this->validator->propertyNotSet($this->data, $key)) { |
|
72 | - $this->data[ $key ] = $data; |
|
73 | - } |
|
74 | - } |
|
75 | - |
|
76 | - |
|
77 | - /** |
|
78 | - * for setting value of the entire data array for the node |
|
79 | - * |
|
80 | - * @param array $data |
|
81 | - * @throws DomainException |
|
82 | - */ |
|
83 | - protected function setDataArray(array $data) |
|
84 | - { |
|
85 | - if ($this->validator->dataArrayEmpty($this)) { |
|
86 | - $this->data = $data; |
|
87 | - } |
|
88 | - } |
|
89 | - |
|
90 | - |
|
91 | - /** |
|
92 | - * for embedding other JsonDataNode objects within this one |
|
93 | - * |
|
94 | - * @param JsonDataNode $data_node |
|
95 | - * @throws DomainException |
|
96 | - */ |
|
97 | - public function addDataNode(JsonDataNode $data_node) |
|
98 | - { |
|
99 | - if ($data_node->isNotInitialized()) { |
|
100 | - // $data_node->initialize(); |
|
101 | - $key = $data_node->nodeName(); |
|
102 | - $this->addData($key, $data_node); |
|
103 | - // if the node being added specifies a domain (use case) |
|
104 | - // and this is the primary data node, then set the domain |
|
105 | - if ($this instanceof PrimaryJsonDataNode && $data_node->domain()) { |
|
106 | - $this->setDomain($data_node->domain()); |
|
107 | - } |
|
108 | - } |
|
109 | - } |
|
110 | - |
|
111 | - |
|
112 | - /** |
|
113 | - * sets the domain (use case) that this data node provides data for |
|
114 | - * |
|
115 | - * @param string $domain |
|
116 | - * @throws DomainException |
|
117 | - */ |
|
118 | - protected function setDomain(string $domain) |
|
119 | - { |
|
120 | - if ($this->domain !== '') { |
|
121 | - $this->validator->overwriteError($domain, 'domain route'); |
|
122 | - } |
|
123 | - $this->domain = $domain; |
|
124 | - } |
|
125 | - |
|
126 | - |
|
127 | - /** |
|
128 | - * used to mark the data node as having been processed |
|
129 | - * |
|
130 | - * @param bool $initialized |
|
131 | - */ |
|
132 | - protected function setInitialized(bool $initialized) |
|
133 | - { |
|
134 | - $this->initialized = filter_var($initialized, FILTER_VALIDATE_BOOLEAN); |
|
135 | - } |
|
136 | - |
|
137 | - |
|
138 | - /** |
|
139 | - * self explanatory (i hope) |
|
140 | - * |
|
141 | - * @param string $node_name |
|
142 | - * @throws DomainException |
|
143 | - */ |
|
144 | - protected function setNodeName(string $node_name) |
|
145 | - { |
|
146 | - $this->validator->validateCriticalProperty($node_name, 'node name'); |
|
147 | - $this->node_name = $node_name; |
|
148 | - // by default set the data node order property by the alphabetical position of the first letter of its name |
|
149 | - // we do this by passing the node name (in UPPERCASE) to ord() to get its ASCII position |
|
150 | - // then we subtract 64 (cuz A has a position of 65) then multiply by 10 just to space things out a bit. |
|
151 | - // this allows a data node to set its order upon construction to some other value |
|
152 | - // so that it can squeak into whatever position it needs to be in, like 55 |
|
153 | - $this->setOrder((ord(strtoupper($this->node_name)) - 64) * 10); |
|
154 | - } |
|
155 | - |
|
156 | - |
|
157 | - /** |
|
158 | - * the actual data in key value array format |
|
159 | - * |
|
160 | - * @param bool $sort |
|
161 | - * @return array |
|
162 | - */ |
|
163 | - public function data(bool $sort = false): array |
|
164 | - { |
|
165 | - if ($sort) { |
|
166 | - // check if data array has non-numerical keys and use a custom sort algorithm, else sort by keys |
|
167 | - EEH_Array::is_associative_array($this->data) |
|
168 | - ? uasort( |
|
169 | - $this->data, |
|
170 | - function ($a, $b) { |
|
171 | - // check if each incoming argument is a node and if they have an order set |
|
172 | - // if so, then use that for our sorting comparison. otherwise use the node's name... |
|
173 | - // unless it's NOT a node, in which case use the arg value if it is scalar, or 0 if not. |
|
174 | - if ($a instanceof JsonDataNode) { |
|
175 | - $a_ord = $a->order() ?: $a->nodeName(); |
|
176 | - } else { |
|
177 | - $a_ord = is_scalar($a) ?: 0; |
|
178 | - } |
|
179 | - if ($b instanceof JsonDataNode) { |
|
180 | - $b_ord = $b->order() ?: $b->nodeName(); |
|
181 | - } else { |
|
182 | - $b_ord = is_scalar($b) ?: 0; |
|
183 | - } |
|
184 | - return $a_ord <=> $b_ord; |
|
185 | - } |
|
186 | - ) |
|
187 | - // sort numerically indexed arrays by their keys |
|
188 | - : ksort($this->data); |
|
189 | - } |
|
190 | - return $this->data; |
|
191 | - } |
|
192 | - |
|
193 | - |
|
194 | - /** |
|
195 | - * the domain (use case) that this data node provides data for |
|
196 | - * |
|
197 | - * @return string |
|
198 | - */ |
|
199 | - public function domain(): string |
|
200 | - { |
|
201 | - return $this->domain; |
|
202 | - } |
|
203 | - |
|
204 | - |
|
205 | - /** |
|
206 | - * true if the data node has been initialized, |
|
207 | - * which entails retrieving the required data and adding it to the data node data array |
|
208 | - * |
|
209 | - * @return bool |
|
210 | - */ |
|
211 | - public function isInitialized(): bool |
|
212 | - { |
|
213 | - return $this->initialized; |
|
214 | - } |
|
215 | - |
|
216 | - |
|
217 | - /** |
|
218 | - * true if the data node has NOT been initialized |
|
219 | - * |
|
220 | - * @return bool |
|
221 | - */ |
|
222 | - public function isNotInitialized(): bool |
|
223 | - { |
|
224 | - return ! $this->initialized; |
|
225 | - } |
|
226 | - |
|
227 | - |
|
228 | - /** |
|
229 | - * Specify data which should be serialized to JSON |
|
230 | - * |
|
231 | - * @link https://php.net/manual/en/jsonserializable.jsonserialize.php |
|
232 | - * @return array data which can be serialized by json_encode |
|
233 | - */ |
|
234 | - public function jsonSerialize(): array |
|
235 | - { |
|
236 | - return $this->data; |
|
237 | - } |
|
238 | - |
|
239 | - |
|
240 | - /** |
|
241 | - * self explanatory (i hope) |
|
242 | - * |
|
243 | - * @return string |
|
244 | - */ |
|
245 | - public function nodeName(): string |
|
246 | - { |
|
247 | - return $this->node_name; |
|
248 | - } |
|
249 | - |
|
250 | - |
|
251 | - /** |
|
252 | - * @return int |
|
253 | - */ |
|
254 | - public function order(): ?int |
|
255 | - { |
|
256 | - return $this->order; |
|
257 | - } |
|
258 | - |
|
259 | - |
|
260 | - /** |
|
261 | - * @param int $order |
|
262 | - */ |
|
263 | - protected function setOrder(int $order): void |
|
264 | - { |
|
265 | - $this->order = absint($order); |
|
266 | - } |
|
21 | + /** |
|
22 | + * @var JsonDataNodeValidator |
|
23 | + */ |
|
24 | + protected $validator; |
|
25 | + |
|
26 | + /** |
|
27 | + * @var array |
|
28 | + */ |
|
29 | + private $data = []; |
|
30 | + |
|
31 | + /** |
|
32 | + * @var string |
|
33 | + */ |
|
34 | + private $domain = ''; |
|
35 | + |
|
36 | + /** |
|
37 | + * @var boolean |
|
38 | + */ |
|
39 | + private $initialized = false; |
|
40 | + |
|
41 | + /** |
|
42 | + * @var string |
|
43 | + */ |
|
44 | + private $node_name = ''; |
|
45 | + |
|
46 | + /** |
|
47 | + * @var int |
|
48 | + */ |
|
49 | + private $order = 50; |
|
50 | + |
|
51 | + |
|
52 | + /** |
|
53 | + * @param JsonDataNodeValidator $validator |
|
54 | + * @throws DomainException |
|
55 | + */ |
|
56 | + public function __construct(JsonDataNodeValidator $validator) |
|
57 | + { |
|
58 | + $this->validator = $validator; |
|
59 | + } |
|
60 | + |
|
61 | + |
|
62 | + /** |
|
63 | + * for adding primitive data like arrays, integers, or strings |
|
64 | + * |
|
65 | + * @param string $key |
|
66 | + * @param mixed $data |
|
67 | + * @throws DomainException |
|
68 | + */ |
|
69 | + protected function addData(string $key, $data) |
|
70 | + { |
|
71 | + if ($this->validator->propertyNotSet($this->data, $key)) { |
|
72 | + $this->data[ $key ] = $data; |
|
73 | + } |
|
74 | + } |
|
75 | + |
|
76 | + |
|
77 | + /** |
|
78 | + * for setting value of the entire data array for the node |
|
79 | + * |
|
80 | + * @param array $data |
|
81 | + * @throws DomainException |
|
82 | + */ |
|
83 | + protected function setDataArray(array $data) |
|
84 | + { |
|
85 | + if ($this->validator->dataArrayEmpty($this)) { |
|
86 | + $this->data = $data; |
|
87 | + } |
|
88 | + } |
|
89 | + |
|
90 | + |
|
91 | + /** |
|
92 | + * for embedding other JsonDataNode objects within this one |
|
93 | + * |
|
94 | + * @param JsonDataNode $data_node |
|
95 | + * @throws DomainException |
|
96 | + */ |
|
97 | + public function addDataNode(JsonDataNode $data_node) |
|
98 | + { |
|
99 | + if ($data_node->isNotInitialized()) { |
|
100 | + // $data_node->initialize(); |
|
101 | + $key = $data_node->nodeName(); |
|
102 | + $this->addData($key, $data_node); |
|
103 | + // if the node being added specifies a domain (use case) |
|
104 | + // and this is the primary data node, then set the domain |
|
105 | + if ($this instanceof PrimaryJsonDataNode && $data_node->domain()) { |
|
106 | + $this->setDomain($data_node->domain()); |
|
107 | + } |
|
108 | + } |
|
109 | + } |
|
110 | + |
|
111 | + |
|
112 | + /** |
|
113 | + * sets the domain (use case) that this data node provides data for |
|
114 | + * |
|
115 | + * @param string $domain |
|
116 | + * @throws DomainException |
|
117 | + */ |
|
118 | + protected function setDomain(string $domain) |
|
119 | + { |
|
120 | + if ($this->domain !== '') { |
|
121 | + $this->validator->overwriteError($domain, 'domain route'); |
|
122 | + } |
|
123 | + $this->domain = $domain; |
|
124 | + } |
|
125 | + |
|
126 | + |
|
127 | + /** |
|
128 | + * used to mark the data node as having been processed |
|
129 | + * |
|
130 | + * @param bool $initialized |
|
131 | + */ |
|
132 | + protected function setInitialized(bool $initialized) |
|
133 | + { |
|
134 | + $this->initialized = filter_var($initialized, FILTER_VALIDATE_BOOLEAN); |
|
135 | + } |
|
136 | + |
|
137 | + |
|
138 | + /** |
|
139 | + * self explanatory (i hope) |
|
140 | + * |
|
141 | + * @param string $node_name |
|
142 | + * @throws DomainException |
|
143 | + */ |
|
144 | + protected function setNodeName(string $node_name) |
|
145 | + { |
|
146 | + $this->validator->validateCriticalProperty($node_name, 'node name'); |
|
147 | + $this->node_name = $node_name; |
|
148 | + // by default set the data node order property by the alphabetical position of the first letter of its name |
|
149 | + // we do this by passing the node name (in UPPERCASE) to ord() to get its ASCII position |
|
150 | + // then we subtract 64 (cuz A has a position of 65) then multiply by 10 just to space things out a bit. |
|
151 | + // this allows a data node to set its order upon construction to some other value |
|
152 | + // so that it can squeak into whatever position it needs to be in, like 55 |
|
153 | + $this->setOrder((ord(strtoupper($this->node_name)) - 64) * 10); |
|
154 | + } |
|
155 | + |
|
156 | + |
|
157 | + /** |
|
158 | + * the actual data in key value array format |
|
159 | + * |
|
160 | + * @param bool $sort |
|
161 | + * @return array |
|
162 | + */ |
|
163 | + public function data(bool $sort = false): array |
|
164 | + { |
|
165 | + if ($sort) { |
|
166 | + // check if data array has non-numerical keys and use a custom sort algorithm, else sort by keys |
|
167 | + EEH_Array::is_associative_array($this->data) |
|
168 | + ? uasort( |
|
169 | + $this->data, |
|
170 | + function ($a, $b) { |
|
171 | + // check if each incoming argument is a node and if they have an order set |
|
172 | + // if so, then use that for our sorting comparison. otherwise use the node's name... |
|
173 | + // unless it's NOT a node, in which case use the arg value if it is scalar, or 0 if not. |
|
174 | + if ($a instanceof JsonDataNode) { |
|
175 | + $a_ord = $a->order() ?: $a->nodeName(); |
|
176 | + } else { |
|
177 | + $a_ord = is_scalar($a) ?: 0; |
|
178 | + } |
|
179 | + if ($b instanceof JsonDataNode) { |
|
180 | + $b_ord = $b->order() ?: $b->nodeName(); |
|
181 | + } else { |
|
182 | + $b_ord = is_scalar($b) ?: 0; |
|
183 | + } |
|
184 | + return $a_ord <=> $b_ord; |
|
185 | + } |
|
186 | + ) |
|
187 | + // sort numerically indexed arrays by their keys |
|
188 | + : ksort($this->data); |
|
189 | + } |
|
190 | + return $this->data; |
|
191 | + } |
|
192 | + |
|
193 | + |
|
194 | + /** |
|
195 | + * the domain (use case) that this data node provides data for |
|
196 | + * |
|
197 | + * @return string |
|
198 | + */ |
|
199 | + public function domain(): string |
|
200 | + { |
|
201 | + return $this->domain; |
|
202 | + } |
|
203 | + |
|
204 | + |
|
205 | + /** |
|
206 | + * true if the data node has been initialized, |
|
207 | + * which entails retrieving the required data and adding it to the data node data array |
|
208 | + * |
|
209 | + * @return bool |
|
210 | + */ |
|
211 | + public function isInitialized(): bool |
|
212 | + { |
|
213 | + return $this->initialized; |
|
214 | + } |
|
215 | + |
|
216 | + |
|
217 | + /** |
|
218 | + * true if the data node has NOT been initialized |
|
219 | + * |
|
220 | + * @return bool |
|
221 | + */ |
|
222 | + public function isNotInitialized(): bool |
|
223 | + { |
|
224 | + return ! $this->initialized; |
|
225 | + } |
|
226 | + |
|
227 | + |
|
228 | + /** |
|
229 | + * Specify data which should be serialized to JSON |
|
230 | + * |
|
231 | + * @link https://php.net/manual/en/jsonserializable.jsonserialize.php |
|
232 | + * @return array data which can be serialized by json_encode |
|
233 | + */ |
|
234 | + public function jsonSerialize(): array |
|
235 | + { |
|
236 | + return $this->data; |
|
237 | + } |
|
238 | + |
|
239 | + |
|
240 | + /** |
|
241 | + * self explanatory (i hope) |
|
242 | + * |
|
243 | + * @return string |
|
244 | + */ |
|
245 | + public function nodeName(): string |
|
246 | + { |
|
247 | + return $this->node_name; |
|
248 | + } |
|
249 | + |
|
250 | + |
|
251 | + /** |
|
252 | + * @return int |
|
253 | + */ |
|
254 | + public function order(): ?int |
|
255 | + { |
|
256 | + return $this->order; |
|
257 | + } |
|
258 | + |
|
259 | + |
|
260 | + /** |
|
261 | + * @param int $order |
|
262 | + */ |
|
263 | + protected function setOrder(int $order): void |
|
264 | + { |
|
265 | + $this->order = absint($order); |
|
266 | + } |
|
267 | 267 | } |
@@ -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> |