| @@ -1,6 +1,13 @@ | ||
| 1 | 1 | <?php |
| 2 | - | |
| 2 | +// phpcs:disable WordPress.WP.AlternativeFunctions.file_system_operations_fread | |
| 3 | +// phpcs:disable WordPress.WP.AlternativeFunctions.file_system_operations_fwrite | |
| 4 | +// phpcs:disable WordPress.WP.AlternativeFunctions.file_system_operations_fsockopen | |
| 5 | +// We use php method like fread, fwrite to read only a portion of a file, we don't have a direct method for partial reads since WP_Filesystem doesn't support a direct equivalent to fread, fwrite. | |
| 6 | +// The entire file deals with custom stream handling | |
| 7 | +// We need direct socket/file operations for this functionality | |
| 8 | +// WordPress filesystem alternatives wouldn't work for this use case | |
| 9 | +// It's better to disable these specific rules at file level than adding individual ignore comments | |
| 3 | 10 | if (!defined('ABSPATH')) exit; |
| 4 | 11 | if (!class_exists('BVRespStream')) : |
| 5 | 12 | |
| 6 | 13 | class BVStream extends BVCallbackBase { |
| @@ -44,22 +51,26 @@ | ||
| 44 | 51 | } |
| 45 | 52 | return array('stream' => $stream); |
| 46 | 53 | } |
| 47 | 54 | |
| 48 | - public function writeStream($_string) { | |
| 49 | - if (strlen($_string) > 0) { | |
| 50 | - $chunk = ""; | |
| 55 | + public function writeStream($chunk) { | |
| 56 | + if (strlen($chunk) > 0) { | |
| 57 | + $bvb64_prefix = ""; | |
| 51 | 58 | if ($this->bvb64stream) { |
| 52 | 59 | $chunk_size = $this->bvb64cksize; |
| 53 | - $_string = $this->base64Encode($_string, $chunk_size); | |
| 54 | - $chunk .= "BVB64" . ":"; | |
| 60 | + $chunk = $this->base64Encode($chunk, $chunk_size); | |
| 61 | + $bvb64_prefix .= "BVB64" . ":"; | |
| 55 | 62 | } |
| 56 | - $chunk .= (strlen($_string) . ":" . $_string); | |
| 63 | + | |
| 64 | + $hash_prefix = ""; | |
| 57 | 65 | if ($this->checksum == 'crc32') { |
| 58 | - $chunk = "CRC32" . ":" . crc32($_string) . ":" . $chunk; | |
| 66 | + $hash_prefix .= "CRC32" . ":" . crc32($chunk) . ":"; | |
| 59 | 67 | } else if ($this->checksum == 'md5') { |
| 60 | - $chunk = "MD5" . ":" . md5($_string) . ":" . $chunk; | |
| 68 | + $hash_prefix .= "MD5" . ":" . md5($chunk) . ":"; | |
| 61 | 69 | } |
| 70 | + | |
| 71 | + $chunk = $hash_prefix . $bvb64_prefix . strlen($chunk) . ":" . $chunk; | |
| 72 | + | |
| 62 | 73 | $this->writeChunk($chunk); |
| 63 | 74 | } |
| 64 | 75 | } |
| 65 | 76 | } |
| @@ -64,18 +75,20 @@ | ||
| 64 | 75 | } |
| 65 | 76 | } |
| 66 | 77 | |
| 67 | 78 | class BVRespStream extends BVStream { |
| 79 | + public $bvboundry; | |
| 80 | + | |
| 68 | 81 | function __construct($request) { |
| 69 | 82 | parent::__construct($request); |
| 83 | + $this->bvboundry = $request->bvboundry; | |
| 70 | 84 | } |
| 71 | 85 | |
| 72 | - public function writeChunk($_string) { | |
| 73 | - echo "ckckckckck".$_string."ckckckckck"; | |
| 86 | + public function writeChunk($chunk) { | |
| 87 | + echo $this->bvboundry . "ckckckckck" . $chunk . $this->bvboundry . "ckckckckck"; | |
| 74 | 88 | } |
| 75 | - | |
| 76 | 89 | public function endStream() { |
| 77 | - echo "rerererere"; | |
| 90 | + echo $this->bvboundry . "rerererere"; | |
| 78 | 91 | |
| 79 | 92 | return array(); |
| 80 | 93 | } |
| 81 | 94 | } |
| @@ -162,9 +175,9 @@ | ||
| 162 | 175 | $mph = array( |
| 163 | 176 | "Content-Disposition" => "form-data; name=bvinfile; filename=data", |
| 164 | 177 | "Content-Type" => "application/octet-stream" |
| 165 | 178 | ); |
| 166 | - $rnd = rand(100000, 999999); | |
| 179 | + $rnd = rand(100000, 999999); // phpcs:ignore WordPress.WP.AlternativeFunctions.rand_rand | |
| 167 | 180 | $this->boundary = "----".$rnd; |
| 168 | 181 | $prologue = "--".$this->boundary."\r\n"; |
| 169 | 182 | foreach($mph as $key=>$val) { |
| 170 | 183 | $prologue .= $key.":".$val."\r\n"; |
| @@ -207,9 +220,9 @@ | ||
| 207 | 220 | stream_set_timeout($this->conn, 300); |
| 208 | 221 | while (!feof($this->conn)) { |
| 209 | 222 | $line = fgets($this->conn, 4096); |
| 210 | 223 | if (1 == $state) { |
| 211 | - if (!preg_match('/HTTP\/(\\d\\.\\d)\\s*(\\d+)\\s*(.*)/', $line, $m)) { | |
| 224 | + if (!WPRHelper::safePregMatch('/HTTP\/(\\d\\.\\d)\\s*(\\d+)\\s*(.*)/', $line, $m)) { | |
| 212 | 225 | $response['httperror'] = "Status code line invalid: ".htmlentities($line); |
| 213 | 226 | return $response; |
| 214 | 227 | } |
| 215 | 228 | $response['http_version'] = $m[1]; |
| @@ -222,9 +235,9 @@ | ||
| 222 | 235 | if ($conlen > 0) |
| 223 | 236 | $response['body'] = fread($this->conn, $conlen); |
| 224 | 237 | return $response; |
| 225 | 238 | } |
| 226 | - if (!preg_match('/([^:]+):\\s*(.*)/', $line, $m)) { | |
| 239 | + if (!WPRHelper::safePregMatch('/([^:]+):\\s*(.*)/', $line, $m)) { | |
| 227 | 240 | // Skip to the next header |
| 228 | 241 | continue; |
| 229 | 242 | } |
| 230 | 243 | $key = strtolower(trim($m[1])); |
| @@ -237,5 +250,6 @@ | ||
| 237 | 250 | } |
| 238 | 251 | return $response; |
| 239 | 252 | } |
| 240 | 253 | } |
| 254 | +// phpcs:enable | |
| 241 | 255 | endif; |