PluginProbe
The WP Remote WordPress Plugin / 5.56
The WP Remote WordPress Plugin v5.56
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.56, at callback/streams.php

247 lines 6.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('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 public $bvboundry;
73
74 function __construct($request) {
75 parent::__construct($request);
76 $this->bvboundry = $request->bvboundry;
77 }
78
79 public function writeChunk($chunk) {
80 echo $this->bvboundry . "ckckckckck" . $chunk . $this->bvboundry . "ckckckckck";
81 }
82 public function endStream() {
83 echo $this->bvboundry . "rerererere";
84
85 return array();
86 }
87 }
88
89 class BVHttpStream extends BVStream {
90 var $user_agent = 'BVHttpStream';
91 var $host;
92 var $port;
93 var $timeout = 20;
94 var $conn;
95 var $errno;
96 var $errstr;
97 var $boundary;
98 var $apissl;
99
100 function __construct($request) {
101 parent::__construct($request);
102 $this->host = $request->params['apihost'];
103 $this->port = intval($request->params['apiport']);
104 $this->apissl = array_key_exists('apissl', $request->params);
105 }
106
107 public function connect() {
108 if ($this->apissl && function_exists('stream_socket_client')) {
109 $this->conn = stream_socket_client("ssl://".$this->host.":".$this->port, $errno, $errstr, $this->timeout);
110 } else {
111 $this->conn = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
112 }
113 if (!$this->conn) {
114 $this->errno = $errno;
115 $this->errstr = $errstr;
116 return false;
117 }
118 socket_set_timeout($this->conn, $this->timeout);
119 return true;
120 }
121
122 public function write($data) {
123 fwrite($this->conn, $data);
124 }
125
126 public function sendChunk($data) {
127 $this->write(sprintf("%x\r\n", strlen($data)));
128 $this->write($data);
129 $this->write("\r\n");
130 }
131
132 public function sendRequest($method, $url, $headers = array(), $body = null) {
133 $def_hdrs = array("Connection" => "keep-alive",
134 "Host" => $this->host);
135 $headers = array_merge($def_hdrs, $headers);
136 $request = strtoupper($method)." ".$url." HTTP/1.1\r\n";
137 if (null != $body) {
138 $headers["Content-length"] = strlen($body);
139 }
140 foreach($headers as $key=>$val) {
141 $request .= $key.":".$val."\r\n";
142 }
143 $request .= "\r\n";
144 if (null != $body) {
145 $request .= $body;
146 }
147 $this->write($request);
148 return $request;
149 }
150
151 public function post($url, $headers = array(), $body = "") {
152 if(is_array($body)) {
153 $b = "";
154 foreach($body as $key=>$val) {
155 $b .= $key."=".urlencode($val)."&";
156 }
157 $body = substr($b, 0, strlen($b) - 1);
158 }
159 $this->sendRequest("POST", $url, $headers, $body);
160 }
161
162 public function streamedPost($url, $headers = array()) {
163 $headers['Transfer-Encoding'] = "chunked";
164 $this->sendRequest("POST", $url, $headers);
165 }
166
167 public function multipartChunkedPost($url) {
168 $mph = array(
169 "Content-Disposition" => "form-data; name=bvinfile; filename=data",
170 "Content-Type" => "application/octet-stream"
171 );
172 $rnd = rand(100000, 999999);
173 $this->boundary = "----".$rnd;
174 $prologue = "--".$this->boundary."\r\n";
175 foreach($mph as $key=>$val) {
176 $prologue .= $key.":".$val."\r\n";
177 }
178 $prologue .= "\r\n";
179 $headers = array('Content-Type' => "multipart/form-data; boundary=".$this->boundary);
180 $this->streamedPost($url, $headers);
181 $this->sendChunk($prologue);
182 }
183
184 public function writeChunk($data) {
185 $this->sendChunk($data);
186 }
187
188 public function closeChunk() {
189 $this->sendChunk("");
190 }
191
192 public function endStream() {
193 $epilogue = "\r\n\r\n--".$this->boundary."--\r\n";
194 $this->sendChunk($epilogue);
195 $this->closeChunk();
196
197 $result = array();
198 $resp = $this->getResponse();
199 if (array_key_exists('httperror', $resp)) {
200 $result["httperror"] = $resp['httperror'];
201 } else {
202 $result["respstatus"] = $resp['status'];
203 $result["respstatus_string"] = $resp['status_string'];
204 }
205 return array("apicallstatus" => $result);
206 }
207
208 public function getResponse() {
209 $response = array();
210 $response['headers'] = array();
211 $state = 1;
212 $conlen = 0;
213 stream_set_timeout($this->conn, 300);
214 while (!feof($this->conn)) {
215 $line = fgets($this->conn, 4096);
216 if (1 == $state) {
217 if (!WPRHelper::safePregMatch('/HTTP\/(\\d\\.\\d)\\s*(\\d+)\\s*(.*)/', $line, $m)) {
218 $response['httperror'] = "Status code line invalid: ".htmlentities($line);
219 return $response;
220 }
221 $response['http_version'] = $m[1];
222 $response['status'] = $m[2];
223 $response['status_string'] = $m[3];
224 $state = 2;
225 } else if (2 == $state) {
226 # End of headers
227 if (2 == strlen($line)) {
228 if ($conlen > 0)
229 $response['body'] = fread($this->conn, $conlen);
230 return $response;
231 }
232 if (!WPRHelper::safePregMatch('/([^:]+):\\s*(.*)/', $line, $m)) {
233 // Skip to the next header
234 continue;
235 }
236 $key = strtolower(trim($m[1]));
237 $val = trim($m[2]);
238 $response['headers'][$key] = $val;
239 if ($key == "content-length") {
240 $conlen = intval($val);
241 }
242 }
243 }
244 return $response;
245 }
246 }
247 endif;