|
122 | 122 | html_error ('Error fetching upload page'); |
123 | 123 | } |
124 | 124 |
|
125 | | - $uploadPost['chomik_id'] = $chomik_id; |
126 | | - $uploadPost['folder_id'] = '0'; |
127 | | - $uploadPost['key'] = $upload_key; |
128 | | - $uploadPost['time'] = $upload_time; |
129 | | - $uploadPost['client'] = 'MinhaBox.br-2.0.8.1'; |
130 | | - $uploadPost['locale'] = 'BR'; |
| 125 | + $upfiles = MT_upfile($upload_host, $upload_port, $upload_time, $chomik_id, $upload_key, $lfile, $lname, $_GET["uproxy"], $pauth) |
131 | 126 |
|
132 | | - $upfiles = upfile($upload_host, $upload_port, '/file/', 0, 0, $uploadPost, $lfile, $lname, "file", "", $_GET["uproxy"], $pauth, 'Mozilla/5.0'); |
133 | 127 | ?> |
134 | 128 | <script>document.getElementById('progressblock').style.display='none';</script> |
135 | 129 | <?php |
|
197 | 191 |
|
198 | 192 | } |
199 | 193 |
|
| 194 | + |
| 195 | +function MT_upfile($host, $port, $time, $chomik_id, $key, $file, $filename, $proxy = 0, $pauth = 0) { |
| 196 | + global $nn, $lastError, $fp, $fs; |
| 197 | + |
| 198 | + if (!isset($nn)) $nn = "\r\n"; |
| 199 | + |
| 200 | + $scheme = 'http://'; |
| 201 | + $url = '/file/'; |
| 202 | + |
| 203 | + $upagent = 'Mozilla/5.0'; |
| 204 | + $bound = "!CHB$time"; |
| 205 | + $saveToFile = 0; |
| 206 | + $postdata = ''; |
| 207 | + |
| 208 | + //chomik_id |
| 209 | + $postdata .= '--' . $bound . $nn; |
| 210 | + $postdata .= "name=\"chomik_id\"$nn"; |
| 211 | + $postdata .= "Content-Type: text/plain$nn$nn"; |
| 212 | + $postdata .= "$chomik_id$nn"; |
| 213 | + |
| 214 | + //folder_id |
| 215 | + $postdata .= '--' . $bound . $nn; |
| 216 | + $postdata .= "name=\"folder_id\"$nn"; |
| 217 | + $postdata .= "Content-Type: text/plain$nn$nn"; |
| 218 | + $postdata .= "0$nn"; |
| 219 | + |
| 220 | + //key |
| 221 | + $postdata .= '--' . $bound . $nn; |
| 222 | + $postdata .= "name=\"key\"$nn"; |
| 223 | + $postdata .= "Content-Type: text/plain$nn$nn"; |
| 224 | + $postdata .= "$key$nn"; |
| 225 | + |
| 226 | + //time |
| 227 | + $postdata .= '--' . $bound . $nn; |
| 228 | + $postdata .= "name=\"time\"$nn"; |
| 229 | + $postdata .= "Content-Type: text/plain$nn$nn"; |
| 230 | + $postdata .= "$time$nn"; |
| 231 | + |
| 232 | + //client |
| 233 | + $postdata .= '--' . $bound . $nn; |
| 234 | + $postdata .= "name=\"client\"$nn"; |
| 235 | + $postdata .= "Content-Type: text/plain$nn$nn"; |
| 236 | + $postdata .= "MinhaBox.br-2.0.8.1$nn"; |
| 237 | + |
| 238 | + //locale |
| 239 | + $postdata .= '--' . $bound . $nn; |
| 240 | + $postdata .= "name=\"locale\"$nn"; |
| 241 | + $postdata .= "Content-Type: text/plain$nn$nn"; |
| 242 | + $postdata .= "BR$nn"; |
| 243 | + |
| 244 | + $fieldname = 'file'; |
| 245 | + if (!is_readable($file)) { |
| 246 | + $lastError = sprintf(lang(65), $file); |
| 247 | + return FALSE; |
| 248 | + } |
| 249 | + $fileSize = getSize($file); |
| 250 | + |
| 251 | + $postdata .= '--' . $bound . $nn; |
| 252 | + $postdata .= "name=\"$fieldname\"; filename=\"$filename\"$nn$nn"; |
| 253 | + if (!empty($cookie)) { |
| 254 | + if (is_array($cookie)) $cookies = (count($cookie) > 0) ? CookiesToStr($cookie) : 0; |
| 255 | + else $cookies = trim($cookie); |
| 256 | + } |
| 257 | + if ($scheme == 'https://') { |
| 258 | + if (!extension_loaded('openssl')) return html_error('You need to install/enable PHP\'s OpenSSL extension to support uploading via HTTPS.'); |
| 259 | + $scheme = 'tls://'; |
| 260 | + if ($port == 0 || $port == 80) $port = 443; |
| 261 | + } else if ($port == 0) $port = 80; |
| 262 | + if (!empty($referer) && ($pos = strpos("\r\n", $referer)) !== 0) { |
| 263 | + $origin = parse_url($pos ? substr($referer, 0, $pos) : $referer); |
| 264 | + $origin = strtolower($origin['scheme']) . '://' . strtolower($origin['host']) . (!empty($origin['port']) && $origin['port'] != defport(array('scheme' => $origin['scheme'])) ? ':' . $origin['port'] : ''); |
| 265 | + } else $origin = ($scheme == 'tls://' ? 'https://' : $scheme) . $host . ($port != 80 && ($scheme != 'tls://' || $port != 443) ? ':' . $port : ''); |
| 266 | + if ($proxy) { |
| 267 | + list($proxyHost, $proxyPort) = explode(':', $proxy, 2); |
| 268 | + if ($scheme != 'tls://') { |
| 269 | + $host = $host . ($port != 80 && $port != 443 ? ":$port" : ''); |
| 270 | + $url = "$scheme$host$url"; |
| 271 | + } |
| 272 | + } |
| 273 | + if ($scheme != 'tls://') $scheme = ''; |
| 274 | + $cHeaders = readCustomHeaders($referer); |
| 275 | + $request = array(); |
| 276 | + $request[''] = 'POST ' . str_replace(' ', '%20', $url) . ' HTTP/1.1'; |
| 277 | + |
| 278 | + $request['Content-Type'] = "multipart/mixed; boundary=$bound"; |
| 279 | + $request['User-Agent'] = $upagent; |
| 280 | + $request['Pragma'] = 'no-cache'; |
| 281 | + $request['Cache-Control'] = 'no-cache'; |
| 282 | + $request['Host'] = $host.':'.$port; |
| 283 | + |
| 284 | + if (!empty($pauth) && !$scheme) $request['proxy-authorization'] = "Basic $pauth"; |
| 285 | + $request['content-length'] = (strlen($postdata) + strlen($nn . "--" . $bound . "--" . $nn) + $fileSize); |
| 286 | + $request = headers2request(array_merge($request, $cHeaders), $postdata); |
| 287 | + $errno = 0; |
| 288 | + $errstr = ''; |
| 289 | + if ($scheme == 'tls://') { |
| 290 | + $hosts = (!empty($proxyHost) ? $proxyHost : $scheme . $host) . ':' . (!empty($proxyPort) ? $proxyPort : $port); |
| 291 | + if ($proxy) $url = "https://$host$url"; // For the 'connected to' message |
| 292 | + } else $hosts = (!empty($proxyHost) ? $scheme . $proxyHost : $scheme . $host) . ':' . (!empty($proxyPort) ? $proxyPort : $port); |
| 293 | + $fp = @stream_socket_client($hosts, $errno, $errstr, 120, STREAM_CLIENT_CONNECT); |
| 294 | + if (!$fp) { |
| 295 | + if (!function_exists('stream_socket_client')) return html_error('[ERROR] stream_socket_client() is disabled.'); |
| 296 | + $dis_host = !empty($proxyHost) ? $proxyHost : $host; |
| 297 | + $dis_port = !empty($proxyPort) ? $proxyPort : $port; |
| 298 | + return html_error(sprintf(lang(88), $dis_host, $dis_port)); |
| 299 | + } |
| 300 | + if ($errno || $errstr) { |
| 301 | + $lastError = $errstr; |
| 302 | + return false; |
| 303 | + } |
| 304 | + if ($proxy) echo '<p>' . sprintf(lang(89), $proxyHost, $proxyPort) . '<br />UPLOAD: <b>' . htmlspecialchars($url) . "</b>...<br />\n"; |
| 305 | + else echo '<p>'.sprintf(lang(90), $host, $port).'</p>'; |
| 306 | + if ($scheme == 'tls://' && $proxy) { |
| 307 | + $connRequest = array(); |
| 308 | + $connRequest[''] = "CONNECT $host:$port HTTP/1.1"; |
| 309 | + if (!empty($pauth)) $connRequest['proxy-authorization'] = "Basic $pauth"; |
| 310 | + $connRequest['proxy-connection'] = 'Close'; |
| 311 | + $connRequest = headers2request($connRequest); |
| 312 | + fwrite($fp, $connRequest); |
| 313 | + fflush($fp); |
| 314 | + $llen = 0; |
| 315 | + $header = ''; |
| 316 | + do { |
| 317 | + $header .= fgets($fp, 16384); |
| 318 | + $len = strlen($header); |
| 319 | + if (!$header || $len == $llen) { |
| 320 | + $lastError = 'No response from proxy after CONNECT.'; |
| 321 | + stream_socket_shutdown($fp, STREAM_SHUT_RDWR); |
| 322 | + fclose($fp); |
| 323 | + return false; |
| 324 | + } |
| 325 | + $llen = $len; |
| 326 | + } while (strpos($header, $nn . $nn) === false); |
| 327 | + $status = intval(substr($header, 9, 3)); |
| 328 | + if ($status != 200) { |
| 329 | + return html_error("Proxy returned $status after CONNECT."); |
| 330 | + } |
| 331 | + // Start TLS. |
| 332 | + if (!stream_socket_enable_crypto($fp, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) return html_error('TLS Startup Error.'); |
| 333 | + } |
| 334 | + echo(lang(104) . ' <b>' . htmlspecialchars($filename) . '</b>, ' . lang(56) . ' <b>' . bytesToKbOrMbOrGb($fileSize) . '</b>...<br />'); |
| 335 | + $GLOBALS['id'] = md5(time() * rand(0, 10)); |
| 336 | + require (TEMPLATE_DIR . '/uploadui.php'); |
| 337 | + flush(); |
| 338 | + fwrite($fp, $request); |
| 339 | + fflush($fp); |
| 340 | + $timeStart = microtime(true); |
| 341 | + $chunkSize = GetChunkSize($fileSize); |
| 342 | + $fs = fopen($file, 'r'); |
| 343 | + $totalsend = $time = $lastChunkTime = 0; |
| 344 | + while (!feof($fs) && !$errno && !$errstr) { |
| 345 | + $data = fread($fs, $chunkSize); |
| 346 | + if ($data === false) { |
| 347 | + fclose($fs); |
| 348 | + fclose($fp); |
| 349 | + return html_error(lang(112)); |
| 350 | + } |
| 351 | + $sendbyte = @fwrite($fp, $data); |
| 352 | + fflush($fp); |
| 353 | + if ($sendbyte === false || strlen($data) > $sendbyte) { |
| 354 | + fclose($fs); |
| 355 | + fclose($fp); |
| 356 | + return html_error(lang(113)); |
| 357 | + } |
| 358 | + $totalsend += $sendbyte; |
| 359 | + $time = microtime(true) - $timeStart; |
| 360 | + $chunkTime = $time - $lastChunkTime; |
| 361 | + $chunkTime = $chunkTime ? $chunkTime : 1; |
| 362 | + $chunkTime = ($chunkTime > 0) ? $chunkTime : 1; |
| 363 | + $lastChunkTime = $time; |
| 364 | + $speed = round($sendbyte / 1024 / $chunkTime, 2); |
| 365 | + $percent = round($totalsend / $fileSize * 100, 2); |
| 366 | + echo "<script type='text/javascript'>pr('$percent', '" . bytesToKbOrMbOrGb($totalsend) . "', '$speed');</script>\n"; |
| 367 | + flush(); |
| 368 | + } |
| 369 | + if ($errno || $errstr) { |
| 370 | + $lastError = $errstr; |
| 371 | + return false; |
| 372 | + } |
| 373 | + fclose($fs); |
| 374 | + fwrite($fp, $nn . "--" . $bound . "--" . $nn); |
| 375 | + fflush($fp); |
| 376 | + $llen = 0; |
| 377 | + $header = ''; |
| 378 | + do { |
| 379 | + $header .= fgets($fp, 16384); |
| 380 | + $len = strlen($header); |
| 381 | + if (!$header || $len == $llen) { |
| 382 | + $lastError = lang(91); |
| 383 | + stream_socket_shutdown($fp, STREAM_SHUT_RDWR); |
| 384 | + fclose($fp); |
| 385 | + return false; |
| 386 | + } |
| 387 | + $llen = $len; |
| 388 | + } while (strpos($header, $nn . $nn) === false); |
| 389 | + // Array for active stream filters |
| 390 | + $sFilters = array(); |
| 391 | + if (stripos($header, "\nTransfer-Encoding: chunked") !== false && in_array('dechunk', stream_get_filters())) $sFilters['dechunk'] = stream_filter_append($fp, 'dechunk', STREAM_FILTER_READ); // Add built-in dechunk filter |
| 392 | + $length = trim(cut_str($header, "\nContent-Length: ", "\n")); |
| 393 | + if (!$length || !is_numeric($length)) $length = -1; |
| 394 | + $page = stream_get_contents($fp, $length); |
| 395 | + stream_socket_shutdown($fp, STREAM_SHUT_RDWR); |
| 396 | + fclose($fp); |
| 397 | + if (empty($sFilters['dechunk']) && stripos($header, "\nTransfer-Encoding: chunked") !== false && function_exists('http_chunked_decode')) { |
| 398 | + $dechunked = http_chunked_decode($page); |
| 399 | + if ($dechunked !== false) $page = $dechunked; |
| 400 | + unset($dechunked); |
| 401 | + } |
| 402 | + if (stripos($header, "\nContent-Encoding: gzip") !== false) { |
| 403 | + $decompressed = gzinflate(substr($page, 10)); |
| 404 | + if ($decompressed !== false) $page = $decompressed; |
| 405 | + unset($decompressed); |
| 406 | + } else if (stripos($header, "\nContent-Encoding: deflate") !== false) { |
| 407 | + $decompressed = gzinflate(in_array(substr($page, 0, 2), array("x\x01", "x\x9C", "x\xDA")) ? substr($page, 2) : $page); |
| 408 | + if ($decompressed !== false) $page = $decompressed; |
| 409 | + unset($decompressed); |
| 410 | + } |
| 411 | + $page = $header.$page; |
| 412 | + return $page; |
| 413 | +} |
| 414 | + |
| 415 | + |
200 | 416 | // tech - Written in 26/11/2016 |
201 | 417 | ?> |
0 commit comments