PluginProbe
The WP Remote WordPress Plugin / 5.22
The WP Remote WordPress Plugin v5.22
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
wpremote / callback / streams.php

streams.php in The WP Remote WordPress Plugin 5.22, at callback/streams.php

244 lines 6.4 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('BVRespStream')) :
5
6 class BVStream extends BVCallbackBase {
7 public $bvb64stream;
8 public $bvb64cksize;
9 public $checksum;
10
11 function __construct($request) {
12 $this->bvb64stream = $request->bvb64stream;
13 $this->bvb64cksize = $request->bvb64cksize;
14 $this->checksum = $request->checksum;
15 }
16
17 public function writeChunk($chunk) {
18 }
19
20 public static function startStream($account, $request) {
21 $result = array();
22 $params = $request->params;
23 $stream = new BVRespStream($request);
24 if ($request->isAPICall()) {
25 $stream = new BVHttpStream($request);
26 if (!$stream->connect()) {
27 $apicallstatus = array(
28 "httperror" => "Cannot Open Connection to Host",
29 "streamerrno" => $stream->errno,
30 "streamerrstr" => $stream->errstr
31 );
32 return array("apicallstatus" => $apicallstatus);
33 }
34 if (array_key_exists('acbmthd', $params)) {
35 $qstr = http_build_query(array('bvapicheck' => $params['bvapicheck']));
36 $url = '/bvapi/'.$params['acbmthd']."?".$qstr;
37 if (array_key_exists('acbqry', $params)) {
38 $url .= "&".$params['acbqry'];
39 }
40 $stream->multipartChunkedPost($url);
41 } else {
42 return array("apicallstatus" => array("httperror" => "ApiCall method not present"));
43 }
44 }
45 return array('stream' => $stream);
46 }
47
48 public function writeStream($chunk) {
49 if (strlen($chunk) > 0) {
50 $bvb64_prefix = "";
51 if ($this->bvb64stream) {
52 $chunk_size = $this->bvb64cksize;
53 $chunk = $this->base64Encode($chunk, $chunk_size);
54 $bvb64_prefix .= "BVB64" . ":";
55 }
56
57 $hash_prefix = "";
58 if ($this->checksum == 'crc32') {
59 $hash_prefix .= "CRC32" . ":" . crc32($chunk) . ":";
60 } else if ($this->checksum == 'md5') {
61 $hash_prefix .= "MD5" . ":" . md5($chunk) . ":";
62 }
63
64 $chunk = $hash_prefix . $bvb64_prefix . strlen($chunk) . ":" . $chunk;
65
66 $this->writeChunk($chunk);
67 }
68 }
69 }
70
71 class BVRespStream extends BVStream {
72 function __construct($request) {
73 parent::__construct($request);
74 }
75
76 public function writeChunk($chunk) {
77 echo "ckckckckck".$chunk."ckckckckck";
78 }
79 public function endStream() {
80 echo "rerererere";
81
82 return array();
83 }
84 }
85
86 class BVHttpStream extends BVStream {
87 var $user_agent = 'BVHttpStream';
88 var $host;
89 var $port;
90 var $timeout = 20;
91 var $conn;
92 var $errno;
93 var $errstr;
94 var $boundary;
95 var $apissl;
96
97 function __construct($request) {
98 parent::__construct($request);
99 $this->host = $request->params['apihost'];
100 $this->port = intval($request->params['apiport']);
101 $this->apissl = array_key_exists('apissl', $request->params);
102 }
103
104 public function connect() {
105 if ($this->apissl && function_exists('stream_socket_client')) {
106 $this->conn = stream_socket_client("ssl://".$this->host.":".$this->port, $errno, $errstr, $this->timeout);
107 } else {
108 $this->conn = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
109 }
110 if (!$this->conn) {
111 $this->errno = $errno;
112 $this->errstr = $errstr;
113 return false;
114 }
115 socket_set_timeout($this->conn, $this->timeout);
116 return true;
117 }
118
119 public function write($data) {
120 fwrite($this->conn, $data);
121 }
122
123 public function sendChunk($data) {
124 $this->write(sprintf("%x\r\n", strlen($data)));
125 $this->write($data);
126 $this->write("\r\n");
127 }
128
129 public function sendRequest($method, $url, $headers = array(), $body = null) {
130 $def_hdrs = array("Connection" => "keep-alive",
131 "Host" => $this->host);
132 $headers = array_merge($def_hdrs, $headers);
133 $request = strtoupper($method)." ".$url." HTTP/1.1\r\n";
134 if (null != $body) {
135 $headers["Content-length"] = strlen($body);
136 }
137 foreach($headers as $key=>$val) {
138 $request .= $key.":".$val."\r\n";
139 }
140 $request .= "\r\n";
141 if (null != $body) {
142 $request .= $body;
143 }
144 $this->write($request);
145 return $request;
146 }
147
148 public function post($url, $headers = array(), $body = "") {
149 if(is_array($body)) {
150 $b = "";
151 foreach($body as $key=>$val) {
152 $b .= $key."=".urlencode($val)."&";
153 }
154 $body = substr($b, 0, strlen($b) - 1);
155 }
156 $this->sendRequest("POST", $url, $headers, $body);
157 }
158
159 public function streamedPost($url, $headers = array()) {
160 $headers['Transfer-Encoding'] = "chunked";
161 $this->sendRequest("POST", $url, $headers);
162 }
163
164 public function multipartChunkedPost($url) {
165 $mph = array(
166 "Content-Disposition" => "form-data; name=bvinfile; filename=data",
167 "Content-Type" => "application/octet-stream"
168 );
169 $rnd = rand(100000, 999999);
170 $this->boundary = "----".$rnd;
171 $prologue = "--".$this->boundary."\r\n";
172 foreach($mph as $key=>$val) {
173 $prologue .= $key.":".$val."\r\n";
174 }
175 $prologue .= "\r\n";
176 $headers = array('Content-Type' => "multipart/form-data; boundary=".$this->boundary);
177 $this->streamedPost($url, $headers);
178 $this->sendChunk($prologue);
179 }
180
181 public function writeChunk($data) {
182 $this->sendChunk($data);
183 }
184
185 public function closeChunk() {
186 $this->sendChunk("");
187 }
188
189 public function endStream() {
190 $epilogue = "\r\n\r\n--".$this->boundary."--\r\n";
191 $this->sendChunk($epilogue);
192 $this->closeChunk();
193
194 $result = array();
195 $resp = $this->getResponse();
196 if (array_key_exists('httperror', $resp)) {
197 $result["httperror"] = $resp['httperror'];
198 } else {
199 $result["respstatus"] = $resp['status'];
200 $result["respstatus_string"] = $resp['status_string'];
201 }
202 return array("apicallstatus" => $result);
203 }
204
205 public function getResponse() {
206 $response = array();
207 $response['headers'] = array();
208 $state = 1;
209 $conlen = 0;
210 stream_set_timeout($this->conn, 300);
211 while (!feof($this->conn)) {
212 $line = fgets($this->conn, 4096);
213 if (1 == $state) {
214 if (!WPRHelper::safePregMatch('/HTTP\/(\\d\\.\\d)\\s*(\\d+)\\s*(.*)/', $line, $m)) {
215 $response['httperror'] = "Status code line invalid: ".htmlentities($line);
216 return $response;
217 }
218 $response['http_version'] = $m[1];
219 $response['status'] = $m[2];
220 $response['status_string'] = $m[3];
221 $state = 2;
222 } else if (2 == $state) {
223 # End of headers
224 if (2 == strlen($line)) {
225 if ($conlen > 0)
226 $response['body'] = fread($this->conn, $conlen);
227 return $response;
228 }
229 if (!WPRHelper::safePregMatch('/([^:]+):\\s*(.*)/', $line, $m)) {
230 // Skip to the next header
231 continue;
232 }
233 $key = strtolower(trim($m[1]));
234 $val = trim($m[2]);
235 $response['headers'][$key] = $val;
236 if ($key == "content-length") {
237 $conlen = intval($val);
238 }
239 }
240 }
241 return $response;
242 }
243 }
244 endif;