PluginProbe
The WP Remote WordPress Plugin / trunk
The WP Remote WordPress Plugin vtrunk
6.72 6.69 6.65 6.62 6.48 6.47 4.87 4.97 5.05 5.09 5.16 5.22 5.24 5.25 5.38 5.41 5.42 5.45 5.47 5.53 5.56 5.65 5.68 5.72 5.73 All 53 releases
← All changes | callback/request.php +64 -42 5.22trunk View file →
@@ -1,18 +1,21 @@
1 1 <?php
2 2
3 3 if (!defined('ABSPATH')) exit;
4 -if (!class_exists('BVCallbackRequest')) :
5 - class BVCallbackRequest {
4 +if (!class_exists('WPRCallbackRequest')) :
5 + class WPRCallbackRequest {
6 6 public $params;
7 7 public $method;
8 8 public $wing;
9 9 public $is_afterload;
10 + public $is_aftershutdown;
11 + public $keep_page_output;
10 12 public $is_admin_ajax;
11 13 public $is_debug;
12 14 public $account;
13 15 public $settings;
14 16 public $sig;
17 + public $sighshalgo;
15 18 public $time;
16 19 public $version;
17 20 public $is_sha1;
18 21 public $bvb64stream;
@@ -20,9 +23,15 @@
20 23 public $checksum;
21 24 public $error = array();
22 25 public $pubkey_name;
23 26 public $bvprmsmac;
27 + public $bvboundry;
24 28
29 + private static $SIG_HASH_ALGO_MAP = array(
30 + '1' => OPENSSL_ALGO_SHA1,
31 + '7' => OPENSSL_ALGO_SHA256
32 + );
33 +
25 34 public function __construct($account, $in_params, $settings) {
26 35 $this->params = array();
27 36 $this->account = $account;
28 37 $this->settings = $settings;
@@ -28,20 +37,25 @@
28 37 $this->settings = $settings;
29 38 $this->wing = $in_params['wing'];
30 39 $this->method = $in_params['bvMethod'];
31 40 $this->is_afterload = array_key_exists('afterload', $in_params);
41 + $this->is_aftershutdown = array_key_exists('aftershutdown', $in_params);
42 + $this->keep_page_output = $this->is_aftershutdown &&
43 + array_key_exists('keeppageoutput', $in_params);
32 44 $this->is_admin_ajax = array_key_exists('adajx', $in_params);
33 45 $this->is_debug = array_key_exists('bvdbg', $in_params);
34 46 $this->sig = $in_params['sig'];
47 + $this->sighshalgo = !empty($in_params['sighshalgo']) ? $in_params['sighshalgo'] : '1';
35 48 $this->time = intval($in_params['bvTime']);
36 49 $this->version = $in_params['bvVersion'];
37 50 $this->is_sha1 = array_key_exists('sha1', $in_params);
38 51 $this->bvb64stream = isset($in_params['bvb64stream']);
39 - $this->bvb64cksize = array_key_exists('bvb64cksize', $in_params) ? intval($in_params['bvb64cksize']) : false;
52 + $this->bvb64cksize = array_key_exists('bvb64cksize', $in_params) ? intval($in_params['bvb64cksize']) : 0;
40 53 $this->checksum = array_key_exists('checksum', $in_params) ? $in_params['checksum'] : false;
41 54 $this->pubkey_name = !empty($in_params['pubkeyname']) ?
42 55 WPRAccount::sanitizeKey($in_params['pubkeyname']) : 'm_public';
43 56 $this->bvprmsmac = !empty($in_params['bvprmsmac']) ? WPRAccount::sanitizeKey($in_params['bvprmsmac']) : "";
57 + $this->bvboundry = !empty($in_params['bvboundry']) ? $in_params['bvboundry'] : "";
44 58 }
45 59
46 60 public function isAPICall() {
47 61 return array_key_exists('apicall', $this->params);
@@ -46,38 +60,25 @@
46 60 public function isAPICall() {
47 61 return array_key_exists('apicall', $this->params);
48 62 }
49 63
50 - public function curlRequest($url, $body) {
51 - $ch = curl_init($url);
52 - curl_setopt($ch, CURLOPT_POST, 1);
53 - curl_setopt($ch, CURLOPT_TIMEOUT, 15);
54 - curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($body));
55 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
56 - return curl_exec($ch);
57 - }
64 + public function http_request($url, $body) {
65 + $body = http_build_query($body);
66 + $response = wp_remote_post($url, array(
67 + 'body' => $body,
68 + 'timeout' => 15,
69 + 'headers' => array(
70 + 'Content-Type' => 'application/x-www-form-urlencoded',
71 + ),
72 + ));
58 73
59 - public function fileGetContentRequest($url, $body) {
60 - $options = array(
61 - 'http' => array(
62 - 'header' => "Content-type: application/x-www-form-urlencoded\r\n",
63 - 'method' => 'POST',
64 - 'content' => http_build_query($body)
65 - )
66 - );
74 + if (is_wp_error($response)) {
75 + return false;
76 + }
67 77
68 - $context = stream_context_create($options);
69 - return file_get_contents($url, false, $context);
78 + return wp_remote_retrieve_body($response);
70 79 }
71 80
72 - public function http_request($url, $body) {
73 - if (in_array('curl', get_loaded_extensions())) {
74 - return $this->curlRequest($url, $body);
75 - } else {
76 - return $this->fileGetContentRequest($url, $body);
77 - }
78 - }
79 -
80 81 public function get_params_via_api($params_key, $apiurl) {
81 82 $res = $this->http_request($apiurl, array('bvkey' => $params_key));
82 83
83 84 if ($res === FALSE) {
@@ -102,8 +103,14 @@
102 103 }
103 104 if ($this->is_afterload) {
104 105 $info["afterload"] = true;
105 106 }
107 + if ($this->is_aftershutdown) {
108 + $info["aftershutdown"] = true;
109 + }
110 + if ($this->keep_page_output) {
111 + $info["keeppageoutput"] = true;
112 + }
106 113 return $info;
107 114 }
108 115
109 116 public function processParams($in_params) {
@@ -133,9 +140,13 @@
133 140 }
134 141 }
135 142
136 143 if (array_key_exists('bvprms', $in_params) && isset($in_params['bvprms'])) {
137 - $calculated_mac = hash_hmac('SHA1', $in_params['bvprms'], $this->account->secret);
144 + if (!empty($in_params['bvprmshshalgo']) && $in_params['bvprmshshalgo'] === 'sha256') {
145 + $calculated_mac = hash_hmac('SHA256', $in_params['bvprms'], $this->account->secret);
146 + } else {
147 + $calculated_mac = hash_hmac('SHA1', $in_params['bvprms'], $this->account->secret);
148 + }
138 149
139 150 if ($this->compare_mac($this->bvprmsmac, $calculated_mac) === true) {
140 151
141 152 if (array_key_exists('b64', $in_params)) {
@@ -155,9 +166,9 @@
155 166 }
156 167
157 168 if (array_key_exists('sersafe', $in_params)) {
158 169 $key = $in_params['sersafe'];
159 - $in_params[$key] = BVCallbackRequest::serialization_safe_decode($in_params[$key]);
170 + $in_params[$key] = WPRCallbackRequest::serialization_safe_decode($in_params[$key]);
160 171 }
161 172
162 173 if (array_key_exists('bvprms', $in_params) && isset($in_params['bvprms'])) {
163 174 $params = $in_params['bvprms'];
@@ -177,8 +188,9 @@
177 188 }
178 189
179 190 if (array_key_exists('memset', $in_params)) {
180 191 $val = intval($in_params['memset']);
192 + // phpcs:ignore Squiz.PHP.DiscouragedFunctions.Discouraged -- Required for memory limit adjustment
181 193 @ini_set('memory_limit', $val.'M');
182 194 }
183 195
184 196 return $params;
@@ -204,9 +216,9 @@
204 216 }
205 217
206 218 public static function serialization_safe_decode($data) {
207 219 if (is_array($data)) {
208 - $data = array_map(array('BVCallbackRequest', 'serialization_safe_decode'), $data);
220 + $data = array_map(array('WPRCallbackRequest', 'serialization_safe_decode'), $data);
209 221 } elseif (is_string($data)) {
210 222 $data = base64_decode($data);
211 223 }
212 224
@@ -224,9 +236,9 @@
224 236 return false;
225 237 }
226 238
227 239 $data = $this->method.$this->account->secret.$this->time.$this->version.$this->bvprmsmac;
228 - if (!$this->verify($data, base64_decode($this->sig))) {
240 + if (!$this->verify($data, base64_decode($this->sig), $this->sighshalgo)) {
229 241 return false;
230 242 }
231 243 $this->settings->updateOption('bvLastRecvTime', $this->time);
232 244
@@ -232,20 +244,28 @@
232 244
233 245 return 1;
234 246 }
235 247
236 - public function verify($data, $sig) {
248 + public function verify($data, $sig, $sighshalgo) {
237 249 if (!function_exists('openssl_verify') || !function_exists('openssl_pkey_get_public')) {
238 250 $this->error["message"] = "OPENSSL_FUNCS_NOT_FOUND";
239 251 return false;
240 252 }
241 253
242 - $key_file = dirname( __FILE__ ) . '/../public_keys/' . $this->pubkey_name . '.pub';
254 + $openssl_algo = array_key_exists($sighshalgo, self::$SIG_HASH_ALGO_MAP) ? self::$SIG_HASH_ALGO_MAP[$sighshalgo] : null;
255 + if ($openssl_algo === null) {
256 + $this->error["message"] = "UNSUPPORTED_HASH_ALGORITHM: " . $sighshalgo;
257 + return false;
258 + }
259 +
260 + $key_file = dirname( __DIR__ ) . '/public_keys/' . $this->pubkey_name . '.pub';
243 261 if (!file_exists($key_file)) {
244 262 $this->error["message"] = "PUBLIC_KEY_NOT_FOUND";
245 263 return false;
246 264 }
247 - $public_key_str = file_get_contents($key_file);
265 +
266 + $public_key_str = WPRWPFileSystem::getInstance()->getContents($key_file);
267 +
248 268 $public_key = openssl_pkey_get_public($public_key_str);
249 269 if (!$public_key) {
250 270 $this->error["message"] = "UNABLE_TO_LOAD_PUBLIC_KEY";
251 271 return false;
@@ -250,9 +270,9 @@
250 270 $this->error["message"] = "UNABLE_TO_LOAD_PUBLIC_KEY";
251 271 return false;
252 272 }
253 273
254 - $verify = openssl_verify($data, $sig, $public_key);
274 + $verify = openssl_verify($data, $sig, $public_key, $openssl_algo);
255 275 if ($verify === 1) {
256 276 return true;
257 277 } elseif ($verify === 0) {
258 278 $this->error["message"] = "INCORRECT_SIGNATURE";
@@ -275,21 +295,23 @@
275 295 }
276 296
277 297 public function authFailedResp() {
278 298 $api_public_key = WPRAccount::getApiPublicKey($this->settings);
279 - $default_secret = WPRRecover::getDefaultSecret($this->settings);
299 + $default_account_pubkey = WPRAccount::getDefaultPublicKey();
280 300 $bvinfo = new WPRInfo($this->settings);
281 301 $resp = array(
282 302 "request_info" => $this->info(),
283 303 "bvinfo" => $bvinfo->info(),
284 304 "statusmsg" => "FAILED_AUTH",
285 - "api_pubkey" => substr($api_public_key, 0, 8),
286 - "def_sigmatch" => substr(hash('sha1', $this->method.$default_secret.$this->time.$this->version), 0, 8)
305 + "api_pubkey" => substr($api_public_key, 0, 8)
287 306 );
288 307
308 + if (is_string($default_account_pubkey) && strlen($default_account_pubkey) >= 32) {
309 + $resp["default_account_pubkey"] = substr($default_account_pubkey, 0, 8);
310 + }
311 +
289 312 if ($this->account) {
290 313 $resp["account_info"] = $this->account->info();
291 - $resp["sigmatch"] = substr(hash('sha1', $this->method.$this->account->secret.$this->time.$this->version), 0, 6);
292 314 } else {
293 315 $resp["account_info"] = array("error" => "ACCOUNT_NOT_FOUND");
294 316 }
295 317
@@ -295,5 +317,5 @@
295 317
296 318 return $resp;
297 319 }
298 320 }
299 -endif;
321 +endif;