| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 3.1.9 |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2026 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FireBox\Core\Helpers; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
class Encryption |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Fixed salt for portable encryption across site exports/imports. |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
private $fixed_salt = 'fbox_enc_3b7d50a8f9e84cb1a4627c8d6e1f90b3'; |
| 27 |
|
| 28 |
/** |
| 29 |
* Encrypt value for storage. |
| 30 |
* |
| 31 |
* @param string $value |
| 32 |
* |
| 33 |
* @return string |
| 34 |
*/ |
| 35 |
final public function encrypt($value = '') |
| 36 |
{ |
| 37 |
$value = trim((string) $value); |
| 38 |
if ($value === '') |
| 39 |
{ |
| 40 |
return ''; |
| 41 |
} |
| 42 |
|
| 43 |
$key = $this->getEncryptionKey(); |
| 44 |
if ($key === '') |
| 45 |
{ |
| 46 |
return $value; |
| 47 |
} |
| 48 |
|
| 49 |
$encrypted_payload = $this->encryptWithOpenSSL($value, $key); |
| 50 |
if ($encrypted_payload === false) |
| 51 |
{ |
| 52 |
return $value; |
| 53 |
} |
| 54 |
|
| 55 |
return base64_encode($encrypted_payload); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Decrypt value from storage. |
| 60 |
* |
| 61 |
* @param mixed $value |
| 62 |
* |
| 63 |
* @return string |
| 64 |
*/ |
| 65 |
final public function decrypt($value) |
| 66 |
{ |
| 67 |
$value = trim((string) $value); |
| 68 |
if ($value === '') |
| 69 |
{ |
| 70 |
return ''; |
| 71 |
} |
| 72 |
|
| 73 |
$decoded = base64_decode($value, true); |
| 74 |
if ($decoded === false || $decoded === '') |
| 75 |
{ |
| 76 |
return $value; |
| 77 |
} |
| 78 |
|
| 79 |
$key = $this->getEncryptionKey(); |
| 80 |
if ($key === '') |
| 81 |
{ |
| 82 |
return $value; |
| 83 |
} |
| 84 |
|
| 85 |
$decrypted = $this->decryptWithOpenSSL($decoded, $key); |
| 86 |
if ($decrypted === '') |
| 87 |
{ |
| 88 |
return $value; |
| 89 |
} |
| 90 |
|
| 91 |
return trim((string) $decrypted); |
| 92 |
} |
| 93 |
|
| 94 |
private function getEncryptionKey() |
| 95 |
{ |
| 96 |
$salt = trim((string) $this->fixed_salt); |
| 97 |
if ($salt === '') |
| 98 |
{ |
| 99 |
return ''; |
| 100 |
} |
| 101 |
|
| 102 |
return hash('sha256', $salt, true); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Encrypt plain text using OpenSSL. |
| 107 |
* |
| 108 |
* @param string $value |
| 109 |
* @param string $key |
| 110 |
* |
| 111 |
* @return string|false |
| 112 |
*/ |
| 113 |
private function encryptWithOpenSSL($value = '', $key = '') |
| 114 |
{ |
| 115 |
if (!function_exists('openssl_encrypt') || !function_exists('openssl_cipher_iv_length')) |
| 116 |
{ |
| 117 |
return false; |
| 118 |
} |
| 119 |
|
| 120 |
$cipher = 'aes-256-cbc'; |
| 121 |
$iv_length = openssl_cipher_iv_length($cipher); |
| 122 |
if (!is_int($iv_length) || $iv_length <= 0) |
| 123 |
{ |
| 124 |
return false; |
| 125 |
} |
| 126 |
|
| 127 |
try |
| 128 |
{ |
| 129 |
$iv = random_bytes($iv_length); |
| 130 |
} |
| 131 |
catch (\Exception $e) |
| 132 |
{ |
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
$openssl_raw_data = defined('OPENSSL_RAW_DATA') ? OPENSSL_RAW_DATA : 1; |
| 137 |
$encrypted = openssl_encrypt($value, $cipher, $key, $openssl_raw_data, $iv); |
| 138 |
if ($encrypted === false) |
| 139 |
{ |
| 140 |
return false; |
| 141 |
} |
| 142 |
|
| 143 |
return $iv . $encrypted; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Decrypt OpenSSL encrypted payload. |
| 148 |
* |
| 149 |
* @param string $payload |
| 150 |
* @param string $key |
| 151 |
* |
| 152 |
* @return string |
| 153 |
*/ |
| 154 |
private function decryptWithOpenSSL($payload = '', $key = '') |
| 155 |
{ |
| 156 |
if (!function_exists('openssl_decrypt') || !function_exists('openssl_cipher_iv_length')) |
| 157 |
{ |
| 158 |
return ''; |
| 159 |
} |
| 160 |
|
| 161 |
$cipher = 'aes-256-cbc'; |
| 162 |
$iv_length = openssl_cipher_iv_length($cipher); |
| 163 |
if (!is_int($iv_length) || $iv_length <= 0 || strlen($payload) <= $iv_length) |
| 164 |
{ |
| 165 |
return ''; |
| 166 |
} |
| 167 |
|
| 168 |
$iv = substr($payload, 0, $iv_length); |
| 169 |
$encrypted = substr($payload, $iv_length); |
| 170 |
if ($iv === false || $encrypted === false) |
| 171 |
{ |
| 172 |
return ''; |
| 173 |
} |
| 174 |
|
| 175 |
$openssl_raw_data = defined('OPENSSL_RAW_DATA') ? OPENSSL_RAW_DATA : 1; |
| 176 |
$decrypted = openssl_decrypt($encrypted, $cipher, $key, $openssl_raw_data, $iv); |
| 177 |
|
| 178 |
return $decrypted === false ? '' : (string) $decrypted; |
| 179 |
} |
| 180 |
|
| 181 |
} |
| 182 |
|