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