PluginProbe
Migrate to WordPress.com / 5.72
Migrate to WordPress.com v5.72
6.72 6.65 trunk 5.72 5.81 5.88
wpcom-migration / callback / request.php

request.php in Migrate to WordPress.com 5.72, at callback/request.php

317 lines 9.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $settings;
14 public $sig;
15 public $sighshalgo;
16 public $time;
17 public $version;
18 public $is_sha1;
19 public $bvb64stream;
20 public $bvb64cksize;
21 public $checksum;
22 public $error = array();
23 public $pubkey_name;
24 public $bvprmsmac;
25 public $bvboundry;
26
27 public function __construct($account, $in_params, $settings) {
28 $this->params = array();
29 $this->account = $account;
30 $this->settings = $settings;
31 $this->wing = $in_params['wing'];
32 $this->method = $in_params['bvMethod'];
33 $this->is_afterload = array_key_exists('afterload', $in_params);
34 $this->is_admin_ajax = array_key_exists('adajx', $in_params);
35 $this->is_debug = array_key_exists('bvdbg', $in_params);
36 $this->sig = $in_params['sig'];
37 $this->sighshalgo = !empty($in_params['sighshalgo']) ? $in_params['sighshalgo'] : null;
38 $this->time = intval($in_params['bvTime']);
39 $this->version = $in_params['bvVersion'];
40 $this->is_sha1 = array_key_exists('sha1', $in_params);
41 $this->bvb64stream = isset($in_params['bvb64stream']);
42 $this->bvb64cksize = array_key_exists('bvb64cksize', $in_params) ? intval($in_params['bvb64cksize']) : false;
43 $this->checksum = array_key_exists('checksum', $in_params) ? $in_params['checksum'] : false;
44 $this->pubkey_name = !empty($in_params['pubkeyname']) ?
45 WPCOMAccount::sanitizeKey($in_params['pubkeyname']) : 'm_public';
46 $this->bvprmsmac = !empty($in_params['bvprmsmac']) ? WPCOMAccount::sanitizeKey($in_params['bvprmsmac']) : "";
47 $this->bvboundry = !empty($in_params['bvboundry']) ? $in_params['bvboundry'] : "";
48 }
49
50 public function isAPICall() {
51 return array_key_exists('apicall', $this->params);
52 }
53
54 public function curlRequest($url, $body) {
55 $ch = curl_init($url);
56 curl_setopt($ch, CURLOPT_POST, 1);
57 curl_setopt($ch, CURLOPT_TIMEOUT, 15);
58 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($body));
59 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
60 return curl_exec($ch);
61 }
62
63 public function fileGetContentRequest($url, $body) {
64 $options = array(
65 'http' => array(
66 'header' => "Content-type: application/x-www-form-urlencoded\r\n",
67 'method' => 'POST',
68 'content' => http_build_query($body)
69 )
70 );
71
72 $context = stream_context_create($options);
73 return file_get_contents($url, false, $context);
74 }
75
76 public function http_request($url, $body) {
77 if (in_array('curl', get_loaded_extensions())) {
78 return $this->curlRequest($url, $body);
79 } else {
80 return $this->fileGetContentRequest($url, $body);
81 }
82 }
83
84 public function get_params_via_api($params_key, $apiurl) {
85 $res = $this->http_request($apiurl, array('bvkey' => $params_key));
86
87 if ($res === FALSE) {
88 return false;
89 }
90
91 return $res;
92 }
93
94 public function info() {
95 $info = array(
96 "requestedsig" => $this->sig,
97 "requestedtime" => $this->time,
98 "requestedversion" => $this->version,
99 "error" => $this->error
100 );
101 if ($this->is_debug) {
102 $info["inreq"] = $this->params;
103 }
104 if ($this->is_admin_ajax) {
105 $info["adajx"] = true;
106 }
107 if ($this->is_afterload) {
108 $info["afterload"] = true;
109 }
110 return $info;
111 }
112
113 public function processParams($in_params) {
114 $params = array();
115
116 if (array_key_exists('obend', $in_params) && function_exists('ob_end_clean'))
117 @ob_end_clean();
118
119 if (array_key_exists('op_reset', $in_params) && function_exists('output_reset_rewrite_vars'))
120 @output_reset_rewrite_vars();
121
122 if (array_key_exists('concat', $in_params)) {
123 foreach ($in_params['concat'] as $key) {
124 $concated = '';
125 $count = intval($in_params[$key]);
126 for ($i = 1; $i <= $count; $i++) {
127 $concated .= $in_params[$key."_bv_".$i];
128 }
129 $in_params[$key] = $concated;
130 }
131 }
132
133 if (isset($in_params['bvpdataviaapi']) && isset($in_params['bvapiurl'])) {
134 $pdata = $this->get_params_via_api($in_params['bvpdataviaapi'], $in_params['bvapiurl']);
135 if ($pdata !== false) {
136 $in_params["bvprms"] = $pdata;
137 }
138 }
139
140 if (array_key_exists('bvprms', $in_params) && isset($in_params['bvprms'])) {
141 if (!empty($in_params['bvprmshshalgo']) && $in_params['bvprmshshalgo'] === 'sha256') {
142 $calculated_mac = hash_hmac('SHA256', $in_params['bvprms'], $this->account->secret);
143 } else {
144 $calculated_mac = hash_hmac('SHA1', $in_params['bvprms'], $this->account->secret);
145 }
146
147 if ($this->compare_mac($this->bvprmsmac, $calculated_mac) === true) {
148
149 if (array_key_exists('b64', $in_params)) {
150 foreach ($in_params['b64'] as $key) {
151 if (is_array($in_params[$key])) {
152 $in_params[$key] = array_map('base64_decode', $in_params[$key]);
153 } else {
154 $in_params[$key] = base64_decode($in_params[$key]);
155 }
156 }
157 }
158
159 if (array_key_exists('unser', $in_params)) {
160 foreach ($in_params['unser'] as $key) {
161 $in_params[$key] = json_decode($in_params[$key], TRUE);
162 }
163 }
164
165 if (array_key_exists('sersafe', $in_params)) {
166 $key = $in_params['sersafe'];
167 $in_params[$key] = BVCallbackRequest::serialization_safe_decode($in_params[$key]);
168 }
169
170 if (array_key_exists('bvprms', $in_params) && isset($in_params['bvprms'])) {
171 $params = $in_params['bvprms'];
172 }
173
174 if (array_key_exists('clacts', $in_params)) {
175 foreach ($in_params['clacts'] as $action) {
176 remove_all_actions($action);
177 }
178 }
179
180 if (array_key_exists('clallacts', $in_params)) {
181 global $wp_filter;
182 foreach ( $wp_filter as $filter => $val ){
183 remove_all_actions($filter);
184 }
185 }
186
187 if (array_key_exists('memset', $in_params)) {
188 $val = intval($in_params['memset']);
189 @ini_set('memory_limit', $val.'M');
190 }
191
192 return $params;
193 }
194 }
195 return false;
196 }
197
198 private function compare_mac($l_hash, $r_hash) {
199 if (!is_string($l_hash) || !is_string($r_hash)) {
200 return false;
201 }
202
203 if (strlen($l_hash) !== strlen($r_hash)) {
204 return false;
205 }
206
207 if (function_exists('hash_equals')) {
208 return hash_equals($l_hash, $r_hash);
209 } else {
210 return $l_hash === $r_hash;
211 }
212 }
213
214 public static function serialization_safe_decode($data) {
215 if (is_array($data)) {
216 $data = array_map(array('BVCallbackRequest', 'serialization_safe_decode'), $data);
217 } elseif (is_string($data)) {
218 $data = base64_decode($data);
219 }
220
221 return $data;
222 }
223
224 public function authenticate() {
225 if (!$this->account) {
226 $this->error["message"] = "ACCOUNT_NOT_FOUND";
227 return false;
228 }
229
230 $bv_last_recv_time = $this->settings->getOption('bvLastRecvTime');
231 if ($this->time < intval($bv_last_recv_time) - 300) {
232 return false;
233 }
234
235 $data = $this->method.$this->account->secret.$this->time.$this->version.$this->bvprmsmac;
236 if (!$this->verify($data, base64_decode($this->sig), $this->sighshalgo)) {
237 return false;
238 }
239 $this->settings->updateOption('bvLastRecvTime', $this->time);
240
241 return 1;
242 }
243
244 public function verify($data, $sig, $sighshalgo) {
245 if (!function_exists('openssl_verify') || !function_exists('openssl_pkey_get_public')) {
246 $this->error["message"] = "OPENSSL_FUNCS_NOT_FOUND";
247 return false;
248 }
249
250 $key_file = dirname( __FILE__ ) . '/../public_keys/' . $this->pubkey_name . '.pub';
251 if (!file_exists($key_file)) {
252 $this->error["message"] = "PUBLIC_KEY_NOT_FOUND";
253 return false;
254 }
255 $public_key_str = file_get_contents($key_file);
256 $public_key = openssl_pkey_get_public($public_key_str);
257 if (!$public_key) {
258 $this->error["message"] = "UNABLE_TO_LOAD_PUBLIC_KEY";
259 return false;
260 }
261
262 if ($sighshalgo === 'sha256') {
263 $verify = openssl_verify($data, $sig, $public_key, OPENSSL_ALGO_SHA256);
264 } else {
265 $verify = openssl_verify($data, $sig, $public_key);
266 }
267 if ($verify === 1) {
268 return true;
269 } elseif ($verify === 0) {
270 $this->error["message"] = "INCORRECT_SIGNATURE";
271 $this->error["pubkey_sig"] = substr(hash('md5', $public_key_str), 0, 8);
272 } else {
273 $this->error["message"] = "OPENSSL_VERIFY_FAILED";
274 }
275 return false;
276 }
277
278 public function corruptedParamsResp() {
279 $bvinfo = new WPCOMInfo($this->settings);
280
281 return array(
282 "account_info" => $this->account->info(),
283 "request_info" => $this->info(),
284 "bvinfo" => $bvinfo->info(),
285 "statusmsg" => "BVPRMS_CORRUPTED"
286 );
287 }
288
289 public function authFailedResp() {
290 $api_public_key = WPCOMAccount::getApiPublicKey($this->settings);
291 $default_secret = WPCOMRecover::getDefaultSecret($this->settings);
292 $default_account_pubkey = WPCOMAccount::getDefaultPublicKey();
293 $bvinfo = new WPCOMInfo($this->settings);
294 $resp = array(
295 "request_info" => $this->info(),
296 "bvinfo" => $bvinfo->info(),
297 "statusmsg" => "FAILED_AUTH",
298 "api_pubkey" => substr($api_public_key, 0, 8),
299 "def_key_status" => WPCOMRecover::getSecretStatus($this->settings),
300 "def_sigmatch" => substr(hash('sha1', $this->method.$default_secret.$this->time.$this->version), 0, 8)
301 );
302
303 if (is_string($default_account_pubkey) && strlen($default_account_pubkey) >= 32) {
304 $resp["default_account_pubkey"] = substr($default_account_pubkey, 0, 8);
305 }
306
307 if ($this->account) {
308 $resp["account_info"] = $this->account->info();
309 $resp["sigmatch"] = substr(hash('sha1', $this->method.$this->account->secret.$this->time.$this->version), 0, 6);
310 } else {
311 $resp["account_info"] = array("error" => "ACCOUNT_NOT_FOUND");
312 }
313
314 return $resp;
315 }
316 }
317 endif;