get_param('part_offset') === null) { return Chat4U_Protocol::response($request, $value); } $raw = wp_json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); if ($raw === false) { return new WP_Error('chat4u_transfer_invalid', __('The public metadata could not be encoded. Retry the connection check.', 'chat4u'), ['status' => 503]); } $hash = hash('sha256', $raw); $descriptor = ['size' => strlen($raw), 'sha256' => $hash, 'part_bytes' => self::PART_BYTES]; if ($request->get_param('part_offset') === null) { return Chat4U_Protocol::response($request, ['transfer' => $descriptor]); } $offset = (string) $request->get_param('part_offset'); $expected = (string) $request->get_param('part_hash'); if (!ctype_digit($offset) || (int) $offset >= strlen($raw) || !hash_equals($hash, $expected)) { return new WP_Error('chat4u_transfer_changed', __('The public metadata changed during transfer. Retry from the current version.', 'chat4u'), ['status' => 409]); } $part = substr($raw, (int) $offset, self::PART_BYTES); return Chat4U_Protocol::response($request, $descriptor + ['offset' => (int) $offset, 'data' => base64_encode($part), 'part_sha256' => hash('sha256', $part)]); } }