helpers
1 week ago
tables
1 week ago
aIProviderInterface.php
1 week ago
assets.php
1 week ago
baseObject.php
1 week ago
builderBlock.php
1 week ago
controller.php
1 week ago
date.php
1 week ago
db.php
1 week ago
dispatcher.php
1 week ago
errors.php
1 week ago
field.php
1 week ago
fieldAdapter.php
1 week ago
frame.php
1 week ago
helper.php
1 week ago
html.php
1 week ago
installer.php
1 week ago
installerDbUpdater.php
1 week ago
integration.php
1 week ago
modInstaller.php
1 week ago
model.php
1 week ago
module.php
1 week ago
req.php
1 week ago
response.php
1 week ago
table.php
1 week ago
uri.php
1 week ago
user.php
1 week ago
utils.php
1 week ago
validator.php
1 week ago
view.php
1 week ago
integration.php
212 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | abstract class WaicIntegration extends WaicBaseObject { |
| 6 | protected $_code = ''; |
| 7 | protected $_category = ''; |
| 8 | protected $_logo = ''; |
| 9 | protected $_type = ''; |
| 10 | protected $_name = ''; |
| 11 | protected $_desc = ''; |
| 12 | protected $_order = ''; |
| 13 | protected $_settings = array(); |
| 14 | protected $_integration = false; |
| 15 | protected $_params = null; |
| 16 | protected $_encrypted = null; |
| 17 | protected $_decrypted = null; |
| 18 | protected $_cipher = 'AES-128-CBC'; |
| 19 | protected $_key = null; |
| 20 | protected $_iv = null; |
| 21 | protected $_privatParams = array(); |
| 22 | protected $_signalParams = array(); |
| 23 | private $_keyiv = 'a23y56f89i12nms6'; |
| 24 | private $_authProxy = 'https://aiwuplugin.com/'; |
| 25 | private $_authProxySecret = 'a9c9022402df8368899a7ebeeed93e08cc63837c33b8efe745c47fe11db4e113'; |
| 26 | |
| 27 | public function getCode() { |
| 28 | return $this->_code; |
| 29 | } |
| 30 | public function getLogo() { |
| 31 | return $this->_logo; |
| 32 | } |
| 33 | public function getCategory() { |
| 34 | return $this->_category; |
| 35 | } |
| 36 | public function getType() { |
| 37 | return $this->_type; |
| 38 | } |
| 39 | public function getName() { |
| 40 | return $this->_name; |
| 41 | } |
| 42 | public function getDesc() { |
| 43 | return $this->_desc; |
| 44 | } |
| 45 | public function getSettings() { |
| 46 | return $this->_settings; |
| 47 | } |
| 48 | public function getOrder() { |
| 49 | return $this->_order; |
| 50 | } |
| 51 | public function getEncriptKey() { |
| 52 | if (is_null($this->_key)) { |
| 53 | $this->_key = substr(hash('sha256', $this->_code . $this->_keyiv, true), 0, 16); |
| 54 | } |
| 55 | return $this->_key; |
| 56 | } |
| 57 | public function getEncriptIv() { |
| 58 | if (is_null($this->_iv)) { |
| 59 | $this->_iv = substr(hash('sha256', $this->_keyiv . $this->_code, true), 0, 16); |
| 60 | } |
| 61 | return $this->_iv; |
| 62 | } |
| 63 | |
| 64 | public function setIntegration( $integration ) { |
| 65 | $this->_integration = $integration; |
| 66 | $this->_params = $this->_integration && is_array($this->_integration) ? $this->_integration : array(); |
| 67 | $this->_encrypted = null; |
| 68 | $this->_decrypted = null; |
| 69 | } |
| 70 | public function getParams() { |
| 71 | if (is_null($this->_params)) { |
| 72 | $this->_params = $this->_integration && is_array($this->_integration) ? $this->_integration : array(); |
| 73 | } |
| 74 | return $this->_params; |
| 75 | } |
| 76 | public function encryptBase64( $value ) { |
| 77 | return base64_encode(openssl_encrypt($value, $this->_cipher, $this->getEncriptKey(), 0, $this->getEncriptIv())); |
| 78 | } |
| 79 | public function decryptBase64( $value ) { |
| 80 | return openssl_decrypt(base64_decode($value), $this->_cipher, $this->getEncriptKey(), 0, $this->getEncriptIv()); |
| 81 | } |
| 82 | public function getEncryptedParams() { |
| 83 | if (is_null($this->_encrypted)) { |
| 84 | $params = $this->getParams(); |
| 85 | if (empty($params['_encrypt'])) { |
| 86 | $settings = $this->getSettings(); |
| 87 | foreach ($params as $key => $value) { |
| 88 | $setting = WaicUtils::getArrayValue('params', array(), 2); |
| 89 | if (in_array($key, $this->_privatParams) || (isset($settings[$key]) && !empty($settings[$key]['encrypt']))) { |
| 90 | $params[$key] = $this->encryptBase64($value); |
| 91 | } |
| 92 | } |
| 93 | } else { |
| 94 | $this->_encrypted = $params; |
| 95 | } |
| 96 | $params['_encrypt'] = 1; |
| 97 | $this->_encrypted = $params; |
| 98 | } |
| 99 | return $this->_encrypted; |
| 100 | } |
| 101 | public function getDecryptedParams( $private = true ) { |
| 102 | if (is_null($this->_decrypted)) { |
| 103 | $params = $this->getParams(); |
| 104 | if (empty($params['_encrypt'])) { |
| 105 | $this->_decrypted = $params; |
| 106 | } else { |
| 107 | $settings = $this->getSettings(); |
| 108 | foreach ($params as $key => $value) { |
| 109 | $setting = WaicUtils::getArrayValue('params', array(), 2); |
| 110 | if (in_array($key, $this->_privatParams) || (isset($settings[$key]) && !empty($settings[$key]['encrypt']))) { |
| 111 | $params[$key] = $this->decryptBase64($value); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | $params['_encrypt'] = 0; |
| 116 | $this->_decrypted = $params; |
| 117 | } |
| 118 | if (!$private && !empty($this->_privatParams)) { |
| 119 | $result = $this->_decrypted; |
| 120 | foreach ($this->_privatParams as $key) { |
| 121 | unset($result[$key]); |
| 122 | } |
| 123 | return $result; |
| 124 | } |
| 125 | return $this->_decrypted; |
| 126 | } |
| 127 | public function getParam( $key, $def = '', $typ = 0, $zero = false, $leer = false ) { |
| 128 | return WaicUtils::getArrayValue($this->getParams(), $key, $def, $typ, false, $zero, $leer); |
| 129 | } |
| 130 | public function getEncryptedParam( $key, $def = '', $typ = 0, $zero = false, $leer = false ) { |
| 131 | return WaicUtils::getArrayValue($this->getEncryptedParams(), $key, $def, $typ, false, $zero, $leer); |
| 132 | } |
| 133 | public function getDecryptedParam( $key, $def = '', $typ = 0, $zero = false, $leer = false ) { |
| 134 | return WaicUtils::getArrayValue($this->getDecryptedParams(), $key, $def, $typ, false, $zero, $leer); |
| 135 | } |
| 136 | |
| 137 | // add only decrypted values |
| 138 | public function addParam( $key, $value ) { |
| 139 | $params = $this->getParams(); |
| 140 | if (!empty($params['_encrypt'])) { |
| 141 | if (in_array($key, $this->_privatParams)) { |
| 142 | $value = $this->encryptBase64($value); |
| 143 | } |
| 144 | } |
| 145 | $this->_params[$key] = $value; |
| 146 | |
| 147 | $this->_encrypted = null; |
| 148 | $this->_decrypted = null; |
| 149 | } |
| 150 | public function doTest( $need = false ) { |
| 151 | $params = $this->getParams(); |
| 152 | if ($need || !isset($params['_status'])) { |
| 153 | $this->addParam('_status', 1); |
| 154 | } |
| 155 | } |
| 156 | public function getAuthProxyUrl() { |
| 157 | return $this->_authProxy . 'wp-json/aops/v1/oauth/init'; |
| 158 | } |
| 159 | public function getAuthProxyRefreshUrl() { |
| 160 | return $this->_authProxy . 'wp-json/aops/v1/oauth/refresh'; |
| 161 | } |
| 162 | public function getAuthProxySecret( $body ) { |
| 163 | return base64_encode(hash_hmac('sha256', $body, $this->_authProxySecret, true)); |
| 164 | } |
| 165 | |
| 166 | public function unpackTokenPackage( $jwt ) { |
| 167 | $secret = $this->_authProxySecret; |
| 168 | $parts = explode('.', $jwt); |
| 169 | if (count($parts) !== 3) { |
| 170 | return 'Invalid format token_package'; |
| 171 | } |
| 172 | //$header = json_decode(base64_decode(strtr($parts[0], '-_', '+/')), true); |
| 173 | $signature = base64_decode(strtr($parts[2], '-_', '+/')); |
| 174 | |
| 175 | $expected = hash_hmac('sha256', $parts[0] . '.' . $parts[1], $secret, true); |
| 176 | if (!hash_equals($expected, $signature)) { |
| 177 | return 'The signature is incorrect'; |
| 178 | } |
| 179 | $payload = json_decode(base64_decode(strtr($parts[1], '-_', '+/')), true); |
| 180 | if (!is_array($payload)) { |
| 181 | return 'Incorrect body JWT'; |
| 182 | } |
| 183 | /*if (isset($payload['iat'], $payload['ttl']) && time() > $payload['iat'] + $payload['ttl']) { |
| 184 | return 'JWT expired'; |
| 185 | }*/ |
| 186 | return $payload; |
| 187 | } |
| 188 | public function addPrivateParams( $old ) { |
| 189 | if (empty($this->_signalParams) || empty($this->_privatParams) || empty($old) || !is_array($old)) { |
| 190 | return; |
| 191 | } |
| 192 | $changed = false; |
| 193 | foreach ($this->_signalParams as $key) { |
| 194 | $value = WaicUtils::getArrayValue($old, $key); |
| 195 | if ($this->getParam($key) != $value) { |
| 196 | $changed = true; |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | if (!$changed) { |
| 201 | $isEnc = !empty($old['_encrypt']); |
| 202 | foreach ($this->_privatParams as $key) { |
| 203 | $value = WaicUtils::getArrayValue($old, $key); |
| 204 | if ($isEnc) { |
| 205 | $value = $this->decryptBase64($value); |
| 206 | } |
| 207 | $this->addParam($key, $value); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 |