| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) exit; |
| 4 |
if (!class_exists('BVCallbackRequest')) : |
| 5 |
class BVCallbackRequest { |
| 6 |
public $params; |
| 7 |
public $method; |
| 8 |
public $wing; |
| 9 |
public $is_afterload; |
| 10 |
public $is_admin_ajax; |
| 11 |
public $is_debug; |
| 12 |
public $account; |
| 13 |
public $calculated_mac; |
| 14 |
public $sig; |
| 15 |
public $time; |
| 16 |
public $version; |
| 17 |
public $is_sha1; |
| 18 |
public $bvb64stream; |
| 19 |
public $bvb64cksize; |
| 20 |
public $checksum; |
| 21 |
|
| 22 |
public function __construct($account, $in_params) { |
| 23 |
$this->params = array(); |
| 24 |
$this->account = $account; |
| 25 |
$this->wing = $in_params['wing']; |
| 26 |
$this->method = $in_params['bvMethod']; |
| 27 |
$this->is_afterload = array_key_exists('afterload', $in_params); |
| 28 |
$this->is_admin_ajax = array_key_exists('adajx', $in_params); |
| 29 |
$this->is_debug = array_key_exists('bvdbg', $in_params); |
| 30 |
$this->sig = $in_params['sig']; |
| 31 |
$this->time = intval($in_params['bvTime']); |
| 32 |
$this->version = $in_params['bvVersion']; |
| 33 |
$this->is_sha1 = array_key_exists('sha1', $in_params); |
| 34 |
$this->bvb64stream = isset($in_params['bvb64stream']); |
| 35 |
$this->bvb64cksize = array_key_exists('bvb64cksize', $in_params) ? intval($in_params['bvb64cksize']) : false; |
| 36 |
$this->checksum = array_key_exists('checksum', $in_params) ? $in_params['checksum'] : false; |
| 37 |
} |
| 38 |
|
| 39 |
public function isAPICall() { |
| 40 |
return array_key_exists('apicall', $this->params); |
| 41 |
} |
| 42 |
|
| 43 |
public function curlRequest($url, $body) { |
| 44 |
$ch = curl_init($url); |
| 45 |
curl_setopt($ch, CURLOPT_POST, 1); |
| 46 |
curl_setopt($ch, CURLOPT_TIMEOUT, 15); |
| 47 |
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($body)); |
| 48 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 49 |
return curl_exec($ch); |
| 50 |
} |
| 51 |
|
| 52 |
public function fileGetContentRequest($url, $body) { |
| 53 |
$options = array( |
| 54 |
'http' => array( |
| 55 |
'header' => "Content-type: application/x-www-form-urlencoded\r\n", |
| 56 |
'method' => 'POST', |
| 57 |
'content' => http_build_query($body) |
| 58 |
) |
| 59 |
); |
| 60 |
|
| 61 |
$context = stream_context_create($options); |
| 62 |
return file_get_contents($url, false, $context); |
| 63 |
} |
| 64 |
|
| 65 |
public function http_request($url, $body) { |
| 66 |
if (in_array('curl', get_loaded_extensions())) { |
| 67 |
return $this->curlRequest($url, $body); |
| 68 |
} else { |
| 69 |
return $this->fileGetContentRequest($url, $body); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
public function get_params_via_api($params_key, $apiurl) { |
| 74 |
$res = $this->http_request($apiurl, array('bvkey' => $params_key)); |
| 75 |
|
| 76 |
if ($res === FALSE) { |
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
return $res; |
| 81 |
} |
| 82 |
|
| 83 |
public function info() { |
| 84 |
$info = array( |
| 85 |
"requestedsig" => $this->sig, |
| 86 |
"requestedtime" => $this->time, |
| 87 |
"requestedversion" => $this->version |
| 88 |
); |
| 89 |
if ($this->is_debug) { |
| 90 |
$info["inreq"] = $this->params; |
| 91 |
} |
| 92 |
if ($this->is_admin_ajax) { |
| 93 |
$info["adajx"] = true; |
| 94 |
} |
| 95 |
if ($this->is_afterload) { |
| 96 |
$info["afterload"] = true; |
| 97 |
} |
| 98 |
if ($this->calculated_mac) { |
| 99 |
$info["calculated_mac"] = $this->calculated_mac; |
| 100 |
} |
| 101 |
return $info; |
| 102 |
} |
| 103 |
|
| 104 |
public function processParams($in_params) { |
| 105 |
$params = array(); |
| 106 |
|
| 107 |
if (array_key_exists('obend', $in_params) && function_exists('ob_end_clean')) |
| 108 |
@ob_end_clean(); |
| 109 |
|
| 110 |
if (array_key_exists('op_reset', $in_params) && function_exists('output_reset_rewrite_vars')) |
| 111 |
@output_reset_rewrite_vars(); |
| 112 |
|
| 113 |
if (array_key_exists('concat', $in_params)) { |
| 114 |
foreach ($in_params['concat'] as $key) { |
| 115 |
$concated = ''; |
| 116 |
$count = intval($in_params[$key]); |
| 117 |
for ($i = 1; $i <= $count; $i++) { |
| 118 |
$concated .= $in_params[$key."_bv_".$i]; |
| 119 |
} |
| 120 |
$in_params[$key] = $concated; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
if (isset($in_params['bvpdataviaapi']) && isset($in_params['bvapiurl'])) { |
| 125 |
$pdata = $this->get_params_via_api($in_params['bvpdataviaapi'], $in_params['bvapiurl']); |
| 126 |
if ($pdata !== false) { |
| 127 |
$in_params["bvprms"] = $pdata; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
if (array_key_exists('bvprms', $in_params) && isset($in_params['bvprms']) && |
| 132 |
array_key_exists('bvprmsmac', $in_params) && isset($in_params['bvprmsmac'])) { |
| 133 |
$digest_algo = 'SHA1'; |
| 134 |
$sent_mac = WPRAccount::sanitizeKey($in_params['bvprmsmac']); |
| 135 |
|
| 136 |
if (array_key_exists('bvprmshshalgo', $in_params) && isset($in_params['bvprmshshalgo'])) { |
| 137 |
$digest_algo = $in_params['bvprmshshalgo']; |
| 138 |
} |
| 139 |
|
| 140 |
$calculated_mac = hash_hmac($digest_algo, $in_params['bvprms'], $this->account->secret); |
| 141 |
$this->calculated_mac = substr($calculated_mac, 0, 6); |
| 142 |
|
| 143 |
if ($this->compare_mac($sent_mac, $calculated_mac) === true) { |
| 144 |
|
| 145 |
if (array_key_exists('b64', $in_params)) { |
| 146 |
foreach ($in_params['b64'] as $key) { |
| 147 |
if (is_array($in_params[$key])) { |
| 148 |
$in_params[$key] = array_map('base64_decode', $in_params[$key]); |
| 149 |
} else { |
| 150 |
$in_params[$key] = base64_decode($in_params[$key]); |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
if (array_key_exists('unser', $in_params)) { |
| 156 |
foreach ($in_params['unser'] as $key) { |
| 157 |
$in_params[$key] = json_decode($in_params[$key], TRUE); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
if (array_key_exists('sersafe', $in_params)) { |
| 162 |
$key = $in_params['sersafe']; |
| 163 |
$in_params[$key] = BVCallbackRequest::serialization_safe_decode($in_params[$key]); |
| 164 |
} |
| 165 |
|
| 166 |
if (array_key_exists('bvprms', $in_params) && isset($in_params['bvprms'])) { |
| 167 |
$params = $in_params['bvprms']; |
| 168 |
} |
| 169 |
|
| 170 |
if (array_key_exists('clacts', $in_params)) { |
| 171 |
foreach ($in_params['clacts'] as $action) { |
| 172 |
remove_all_actions($action); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
if (array_key_exists('clallacts', $in_params)) { |
| 177 |
global $wp_filter; |
| 178 |
foreach ( $wp_filter as $filter => $val ){ |
| 179 |
remove_all_actions($filter); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
if (array_key_exists('memset', $in_params)) { |
| 184 |
$val = intval(urldecode($in_params['memset'])); |
| 185 |
@ini_set('memory_limit', $val.'M'); |
| 186 |
} |
| 187 |
|
| 188 |
return $params; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
return false; |
| 193 |
} |
| 194 |
|
| 195 |
private function compare_mac($l_hash, $r_hash) { |
| 196 |
if (!is_string($l_hash) || !is_string($r_hash)) { |
| 197 |
return false; |
| 198 |
} |
| 199 |
|
| 200 |
if (strlen($l_hash) !== strlen($r_hash)) { |
| 201 |
return false; |
| 202 |
} |
| 203 |
|
| 204 |
if (function_exists('hash_equals')) { |
| 205 |
return hash_equals($l_hash, $r_hash); |
| 206 |
} else { |
| 207 |
return $l_hash === $r_hash; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
public static function serialization_safe_decode($data) { |
| 212 |
if (is_array($data)) { |
| 213 |
$data = array_map(array('BVCallbackRequest', 'serialization_safe_decode'), $data); |
| 214 |
} elseif (is_string($data)) { |
| 215 |
$data = base64_decode($data); |
| 216 |
} |
| 217 |
|
| 218 |
return $data; |
| 219 |
} |
| 220 |
} |
| 221 |
endif; |