PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/http.php

https://github.com/gartz/RapidLeetch
PHP | 662 lines | 430 code | 47 blank | 185 comment | 110 complexity | 1bb32d4cc8e3161bcb2ac86bda7a7e66 MD5 | raw file
  1. <?php
  2. if (! defined ( 'RAPIDLEECH' )) {
  3. require ('../deny.php');
  4. exit ();
  5. }
  6. /**
  7. * Pauses for countdown timer in file hosts
  8. * @param int The number of seconds to count down
  9. * @param string The text you want to display when counting down
  10. * @param string The text you want to display when count down is complete
  11. * @param bool
  12. * @return bool
  13. */
  14. function insert_timer($countd, $caption = "", $timeouttext = "", $hide = false) {
  15. global $disable_timer;
  16. if ($disable_timer === true)
  17. return true;
  18. if (! $countd || ! is_numeric ( $countd ))
  19. return false;
  20. $timerid = rand ( 1000, time () );
  21. echo ('<div align="center">');
  22. echo ('<span id="global' . $timerid . '">');
  23. echo ('<br />');
  24. echo ('<span class="caption">' . $caption . '</span>&nbsp;&nbsp;');
  25. echo ('<span id="timerlabel' . $timerid . '" class="caption"></span></span>');
  26. echo ('</div>');
  27. echo ('<script type="text/javascript">');
  28. echo ('var count' . $timerid . '=' . $countd . ';');
  29. echo ('function timer' . $timerid . '() {');
  30. echo ('if(count' . $timerid . ' > 0) {');
  31. echo ('$("#timerlabel' . $timerid . '").html("' . sprintf ( lang ( 87 ), '" + count' . $timerid . ' + "' ) . '");');
  32. echo ('count' . $timerid . '--;');
  33. echo ('setTimeout("timer' . $timerid . '()", 1000);');
  34. echo ('}');
  35. echo ('}');
  36. echo ('timer' . $timerid . '();');
  37. echo ('</script>');
  38. flush ();
  39. for($nnn = 0; $nnn < $countd; $nnn ++) {
  40. sleep ( 1 );
  41. }
  42. flush ();
  43. if ($hide === true) {
  44. echo ('<script type="text/javascript">$("#global' . $timerid . '").css("display","none");</script>');
  45. flush ();
  46. return true;
  47. }
  48. if ($timeouttext) {
  49. echo ('<script type="text/javascript">$("#global' . $timerid . '").html("' . $timeouttext . '");</script>');
  50. flush ();
  51. return true;
  52. }
  53. return true;
  54. }
  55. /**
  56. * Counter for those filehosts that displays mirror after countdown
  57. * @param int The number of seconds to count down
  58. * @param string Text you want to display above the counter
  59. * @param string The text you want to display when counting down
  60. * @param string The text you want to display when count down is complete
  61. */
  62. function insert_new_timer($countd, $displaytext, $caption = "", $text = "") {
  63. if (! is_numeric ( $countd )) {
  64. html_error ( lang ( 85 ) );
  65. }
  66. echo ('<div id="code"></div>');
  67. echo ('<div align="center">');
  68. echo ('<div id="dl"><h4>' . lang ( 86 ) . '</h4></div></div>');
  69. echo ('<script type="text/javascript">var c = ' . $countd . ';fc("' . $caption . '","' . $displaytext . '");</script>');
  70. if (! empty ( $text )) {
  71. print $text;
  72. }
  73. require (TEMPLATE_DIR . '/footer.php');
  74. }
  75. /**
  76. * Function to check if geturl function has completed successfully
  77. */
  78. function is_page($lpage) {
  79. global $lastError;
  80. if (! $lpage) {
  81. html_error ( lang ( 84 ) . "<br />$lastError", 0 );
  82. }
  83. }
  84. function geturl($host, $port, $url, $referer = 0, $cookie = 0, $post = 0, $saveToFile = 0, $proxy = 0, $pauth = 0, $auth = 0, $scheme = "http", $resume_from = 0) {
  85. global $nn, $lastError, $PHP_SELF, $AUTH, $IS_FTP, $FtpBytesTotal, $FtpBytesReceived, $FtpTimeStart, $FtpChunkSize, $Resume, $bytesReceived, $fs, $force_name, $options;
  86. $scheme .= "://";
  87. if (($post !== 0) && ($scheme == "http://")) {
  88. $method = "POST";
  89. $postdata = formpostdata ( $post );
  90. $length = strlen ( $postdata );
  91. $content_tl = "Content-Type: application/x-www-form-urlencoded" . $nn . "Content-Length: " . $length . $nn;
  92. } else {
  93. $method = "GET";
  94. $postdata = "";
  95. $content_tl = "";
  96. }
  97. if ($cookie) {
  98. if (is_array ( $cookie )) {
  99. for($i = 0; $i < count ( $cookie ); $i ++) {
  100. $cookies .= "Cookie: " . $cookie [$i] . $nn;
  101. }
  102. } else {
  103. $cookies = "Cookie: " . $cookie . $nn;
  104. }
  105. }
  106. $referer = $referer ? "Referer: " . $referer . $nn : "";
  107. if ($scheme == "https://") {
  108. $scheme = "ssl://";
  109. $port = 443;
  110. }
  111. if ($proxy) {
  112. list ( $proxyHost, $proxyPort ) = explode ( ":", $proxy );
  113. $url = $scheme . $host . ":" . $port . $url;
  114. $host = $host . ":" . $port;
  115. }
  116. if ($scheme != "ssl://") {
  117. $scheme = "";
  118. }
  119. $http_auth = ($auth) ? "Authorization: Basic " . $auth . $nn : "";
  120. $proxyauth = ($pauth) ? "Proxy-Authorization: Basic " . $pauth . $nn : "";
  121. $request = $method . " " . str_replace ( " ", "%20", $url ) . " HTTP/1.1" . $nn . "Host: " . $host . $nn . "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14" . $nn . "Accept: */*" . $nn . "Accept-Language: en-us;q=0.7,en;q=0.3" . $nn . "Accept-Charset: utf-8,windows-1251;q=0.7,*;q=0.7" . $nn . "Pragma: no-cache" . $nn . "Cache-Control: no-cache" . $nn . ($Resume ["use"] === TRUE ? "Range: bytes=" . $Resume ["from"] . "-" . $nn : "") . $http_auth . $proxyauth . $referer . $cookies . "Connection: Close" . $nn . $content_tl . $nn . $postdata;
  122. $errno = 0;
  123. $errstr = "";
  124. $hosts = ($proxyHost ? $scheme . $proxyHost : $scheme . $host) . ':' . ($proxyPort ? $proxyPort : $port);
  125. $fp = @stream_socket_client ( $hosts, $errno, $errstr, 120, STREAM_CLIENT_CONNECT );
  126. //$fp = @fsockopen($proxyHost ? $scheme.$proxyHost : $scheme.$host, $proxyPort ? $proxyPort : $port, $errno, $errstr, 15);
  127. if (! $fp) {
  128. $dis_host = $proxyHost ? $proxyHost : $host;
  129. $dis_port = $proxyPort ? $proxyPort : $port;
  130. html_error ( sprintf ( lang ( 88 ), $dis_host, $dis_port ) );
  131. }
  132. if ($errno || $errstr) {
  133. $lastError = $errstr;
  134. return false;
  135. }
  136. if ($saveToFile) {
  137. if ($proxy) {
  138. echo '<p>'.sprintf(lang(89),$proxyHost,$proxyPort).'<br />';
  139. echo "GET: <b>" . $url . "</b>...<br />\n";
  140. } else {
  141. echo "<p>";
  142. printf(lang(90),$host,$port);
  143. echo "</p>";
  144. }
  145. }
  146. #########################################################################
  147. fputs ( $fp, $request );
  148. fflush ( $fp );
  149. $timeStart = getmicrotime ();
  150. // Rewrote the get header function according to the proxy script
  151. // Also made sure it goes faster and I think 8192 is the best value for retrieving headers
  152. // Oops.. The previous function hooked up everything and now I'm returning it back to normal
  153. do {
  154. $header .= fgets ( $fp, 16384 );
  155. } while ( strpos ( $header, $nn . $nn ) === false );
  156. #########################################################################
  157. if (! $header) {
  158. $lastError = lang(91);
  159. return false;
  160. }
  161. $responsecode = "";
  162. preg_match ( '/^HTTP\/1\.0|1 ([0-9]+) .*/', $header, $responsecode );
  163. if (($responsecode [1] == 404 || $responsecode [1] == 403) && $saveToFile) {
  164. // Do some checking, please, at least tell them what error it was
  165. if ($responsecode [1] == 403) {
  166. $lastError = lang(92);
  167. } elseif ($responsecode [1] == 404) {
  168. $lastError = lang(93);
  169. } else {
  170. // Weird, it shouldn't come here...
  171. $lastError = lang(94);
  172. }
  173. return false;
  174. }
  175. if ($saveToFile) {
  176. //$bytesTotal = intval ( trim ( cut_str ( $header, "Content-Length:", "\n" ) ) );
  177. $bytesTotal = trim ( cut_str ( $header, "Content-Length:", "\n" ) );
  178. global $options;
  179. if ($options['file_size_limit'] > 0) {
  180. if ($bytesTotal > $options['file_size_limit']*1024*1024) {
  181. $lastError = lang(336) . bytesToKbOrMbOrGb ( $options['file_size_limit']*1024*1024 ) .".";
  182. return false;
  183. }
  184. }
  185. if (stristr ( $host, "rapidshare" ) && $bytesTotal < 10000) {
  186. while ( ! feof ( $fp ) ) {
  187. $page_src .= fread ( $fp, 1024 * 8 );
  188. }
  189. is_present ( $page_src, "is already in use with another ip", lang(100) );
  190. }
  191. if (stristr ( $host, "imageshack" ) && $bytesTotal < 15000) {
  192. while ( ! feof ( $fp ) ) {
  193. $page_src .= fread ( $fp, 1024 * 8 );
  194. }
  195. is_present ( $page_src, "To avoid creation of corrupted zip files, you cannot create a zip on this torrent until it is done downloading" );
  196. }
  197. $redir = "";
  198. if (trim ( preg_match ( '/[^\-]Location: *(.+)(\r|\n)+/i', $header, $redir ) )) {
  199. $redirect = $redir [1];
  200. $lastError = sprintf(lang(95),$redirect);
  201. return FALSE;
  202. }
  203. if (in_array ( cut_str ( $header, "WWW-Authenticate: ", " " ), array ("Basic", "Digest" ) )) {
  204. $lastError = lang(96);
  205. return FALSE;
  206. }
  207. $ContentType = trim ( cut_str ( $header, "Content-Type:", "\n" ) );
  208. if (stristr ( $host, "rapidshare" ) && stristr ( $ContentType, "text/html" ) && stristr ( $header, "404 Not Found" )) {
  209. unset ( $saveToFile );
  210. $NoDownload = TRUE;
  211. } elseif (stristr ( $host, "megaupload" ) && stristr ( $ContentType, "text/html" )) {
  212. unset ( $saveToFile );
  213. $NoDownload = TRUE;
  214. }
  215. if ($Resume ["use"] === TRUE && ! stristr ( $header, "Content-Range:" )) {
  216. if (stristr ( $header, "503 Limit Exceeded" )) {
  217. $lastError = lang(97);
  218. } else {
  219. $lastError = lang(98);
  220. }
  221. return FALSE;
  222. }
  223. if ($force_name)
  224. {
  225. $FileName = $force_name;
  226. $saveToFile = dirname ( $saveToFile ) . PATH_SPLITTER . $FileName;
  227. }
  228. else
  229. {
  230. $ContentDisposition = trim ( cut_str ( $header, "Content-Disposition:", "\n" ) ) . "\n";
  231. if ($ContentDisposition && stripos ( $ContentDisposition, "filename=" ) !== false)
  232. {
  233. $FileName = trim ( trim ( trim ( trim ( trim ( cut_str ( $ContentDisposition, "filename=", "\n" ) ), "=" ), "?" ), ";" ), '"' );
  234. if (strpos($FileName,"/") !== false) $FileName = basename($FileName);
  235. $saveToFile = dirname ( $saveToFile ) . PATH_SPLITTER . $FileName;
  236. }
  237. }
  238. if (! empty ( $options['rename_prefix'] )) {
  239. $File_Name = $options['rename_prefix'] . '_' . basename ( $saveToFile );
  240. $saveToFile = dirname ( $saveToFile ) . PATH_SPLITTER . $File_Name;
  241. }
  242. if (! empty ( $options['rename_suffix'] )) {
  243. $ext = strrchr ( basename ( $saveToFile ), "." );
  244. $before_ext = explode ( $ext, basename ( $saveToFile ) );
  245. $File_Name = $before_ext [0] . '_' . $options['rename_suffix'] . $ext;
  246. $saveToFile = dirname ( $saveToFile ) . PATH_SPLITTER . $File_Name;
  247. }
  248. if($options['rename_underscore']){
  249. $File_Name = str_replace(array(' ', '%20'), '_', basename($saveToFile));
  250. $saveToFile = dirname($saveToFile).PATH_SPLITTER.$File_Name;
  251. }
  252. $filetype = strrchr ( $saveToFile, "." );
  253. if (is_array ( $options['forbidden_filetypes'] ) && in_array ( strtolower ( $filetype ), $options['forbidden_filetypes'] )) {
  254. if ($options['forbidden_filetypes_block']) {
  255. $lastError = sprintf(lang(82),$filetype);
  256. return false;
  257. }
  258. else {
  259. $saveToFile = str_replace ( $filetype, $options['rename_these_filetypes_to'], $saveToFile );
  260. }
  261. }
  262. if (@file_exists ( $saveToFile ) && $options['bw_save']) {
  263. html_error ( lang(99).': '.link_for_file($saveToFile), 0 );
  264. }
  265. if (@file_exists ( $saveToFile ) && $Resume ["use"] === TRUE) {
  266. $fs = @fopen ( $saveToFile, "ab" );
  267. if (! $fs) {
  268. $lastError = sprintf(lang(101),basename ( $saveToFile ),dirname ( $saveToFile )).'<br />'.lang(102).'<br /><a href="javascript:location.reload();">'.lang(103).'</a>';
  269. return FALSE;
  270. }
  271. } else {
  272. if (@file_exists ( $saveToFile )) {
  273. $saveToFile = dirname ( $saveToFile ) . PATH_SPLITTER . time () . "_" . basename ( $saveToFile );
  274. }
  275. $fs = @fopen ( $saveToFile, "wb" );
  276. if (! $fs) {
  277. $secondName = dirname ( $saveToFile ) . PATH_SPLITTER . str_replace ( ":", "", str_replace ( "?", "", basename ( $saveToFile ) ) );
  278. $fs = @fopen ( $secondName, "wb" );
  279. if (! $fs) {
  280. $lastError = sprintf(lang(101),basename ( $saveToFile ),dirname ( $saveToFile )).'<br />'.lang(102).'<br /><a href="javascript:location.reload();">'.lang(103).'</a>';
  281. return FALSE;
  282. }
  283. }
  284. }
  285. flock ( $fs, LOCK_EX );
  286. if ($Resume ["use"] === TRUE && stristr ( $header, "Content-Range:" )) {
  287. list ( $temp, $Resume ["range"] ) = explode ( " ", trim ( cut_str ( $header, "Content-Range:", "\n" ) ) );
  288. list ( $Resume ["range"], $fileSize ) = explode ( "/", $Resume ["range"] );
  289. $fileSize = bytesToKbOrMbOrGb ( $fileSize );
  290. } else {
  291. $fileSize = bytesToKbOrMbOrGb ( $bytesTotal );
  292. }
  293. $chunkSize = GetChunkSize ( $bytesTotal );
  294. echo(lang(104).' <b>'.$saveToFile.'</b>, '.lang(56).' <b>'.$fileSize.'</b>...<br />');
  295. //$scriptStarted = false;
  296. require (TEMPLATE_DIR . '/transloadui.php');
  297. if ($Resume ["use"] === TRUE) {
  298. $received = bytesToKbOrMbOrGb ( filesize ( $saveToFile ) );
  299. $percent = round ( $Resume ["from"] / ($bytesTotal + $Resume ["from"]) * 100, 2 );
  300. echo '<script type="text/javascript">pr('."'" . $percent . "', '" . $received . "', '0');</script>";
  301. //$scriptStarted = true;
  302. flush ();
  303. }
  304. } else {
  305. $page = $header;
  306. }
  307. do {
  308. $data = @fread ( $fp, ($saveToFile ? $chunkSize : 16384) ); // 16384 saw this value in Pear HTTP_Request2 package // (fix - szal) using this actually just causes massive cpu usage for large files, too much data is flushed to the browser!)
  309. if ($data == '')
  310. break;
  311. if ($saveToFile) {
  312. $bytesSaved = fwrite ( $fs, $data );
  313. if ($bytesSaved > - 1) {
  314. $bytesReceived += $bytesSaved;
  315. } else {
  316. $lastError = sprintf(lang(105),$saveToFile);
  317. return false;
  318. }
  319. if ($bytesReceived >= $bytesTotal) {
  320. $percent = 100;
  321. } else {
  322. $percent = @round ( ($bytesReceived + $Resume ["from"]) / ($bytesTotal + $Resume ["from"]) * 100, 2 );
  323. }
  324. if ($bytesReceived > $last + $chunkSize) {
  325. $received = bytesToKbOrMbOrGb ( $bytesReceived + $Resume ["from"] );
  326. $time = getmicrotime () - $timeStart;
  327. $chunkTime = $time - $lastChunkTime;
  328. $chunkTime = $chunkTime ? $chunkTime : 1;
  329. $lastChunkTime = $time;
  330. $speed = @round ( $chunkSize / 1024 / $chunkTime, 2 );
  331. /*if (!$scriptStarted) {
  332. echo('<script type="text/javascript">');
  333. $scriptStarted = true;
  334. }*/
  335. echo '<script type="text/javascript">pr('."'" . $percent . "', '" . $received . "', '" . $speed . "');</script>";
  336. $last = $bytesReceived;
  337. }
  338. } else {
  339. $page .= $data;
  340. }
  341. }while( strlen($data)> 0 );
  342. //echo('</script>');
  343. if ($saveToFile) {
  344. flock ( $fs, LOCK_UN );
  345. fclose ( $fs );
  346. if ($bytesReceived <= 0) {
  347. $lastError = lang(106);
  348. fclose ( $fp );
  349. return FALSE;
  350. }
  351. }
  352. fclose ( $fp );
  353. if ($saveToFile) {
  354. return array ("time" => sec2time ( round ( $time ) ), "speed" => @round ( $bytesTotal / 1024 / (getmicrotime () - $timeStart), 2 ), "received" => true, "size" => $fileSize, "bytesReceived" => ($bytesReceived + $Resume ["from"]), "bytesTotal" => ($bytesTotal + $Resume ["from"]), "file" => $saveToFile );
  355. } else {
  356. if ($NoDownload) {
  357. if (stristr ( $host, "rapidshare" )) {
  358. is_present ( $page, "You have reached the limit for Free users", lang(107), 0 );
  359. is_present ( $page, "The download session has expired", lang(108), 0 );
  360. is_present ( $page, "Wrong access code.", lang(109), 0 );
  361. is_present ( $page, "You have entered a wrong code too many times", lang(110), 0 );
  362. print $page;
  363. } elseif (stristr ( $host, "megaupload" )) {
  364. is_present ( $page, "Download limit exceeded", lang(111), 0 );
  365. print $page;
  366. }
  367. } else {
  368. return $page;
  369. }
  370. }
  371. }
  372. //simple curl function for https:// logins
  373. function sslcurl($link, $post = 0, $cookie = 0, $refer = 0)
  374. {
  375. $mm = !empty($post) ? 1 : 0;
  376. $ch = curl_init();
  377. curl_setopt($ch, CURLOPT_URL, $link);
  378. curl_setopt($ch, CURLOPT_HEADER, 1);
  379. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U;Windows NT 5.1; de;rv:1.8.0.1)\r\nGecko/20060111\r\nFirefox/1.5.0.1');
  380. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  381. if ($mm == 1)
  382. {
  383. curl_setopt($ch, CURLOPT_POST, 1);
  384. curl_setopt($ch, CURLOPT_POSTFIELDS, formpostdata($post));
  385. }
  386. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  387. curl_setopt($ch, CURLOPT_REFERER, $refer);
  388. curl_setopt($ch, CURLOPT_COOKIE, $cookie) ;
  389. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  390. // curl_setopt ( $ch , CURLOPT_TIMEOUT, 15);
  391. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
  392. $contents .= curl_exec($ch);
  393. // $info = curl_getinfo($ch);
  394. // $stat = $info['http_code'];
  395. curl_close($ch);
  396. return $contents;
  397. }
  398. // This new function requires less line and actually reduces filesize :P
  399. // Besides, using less globals means more variables available for us to use
  400. function formpostdata($post) {
  401. $postdata = "";
  402. foreach ( $post as $k => $v ) {
  403. $postdata .= "$k=$v&";
  404. }
  405. // Remove the last '&'
  406. $postdata = substr ( $postdata, 0, - 1 );
  407. return $postdata;
  408. }
  409. function GetCookies($content) {
  410. // The U option will make sure that it matches the first character
  411. // So that it won't grab other information about cookie such as expire, domain and etc
  412. preg_match_all ( '/Set-Cookie: (.*)(;|\r\n)/U', $content, $temp );
  413. $cookie = $temp [1];
  414. $cook = implode ( '; ', $cookie );
  415. return $cook;
  416. }
  417. function GetChunkSize($fsize) {
  418. if ($fsize <= 1024 * 1024) {
  419. return 4096;
  420. }
  421. if ($fsize <= 1024 * 1024 * 10) {
  422. return 4096 * 10;
  423. }
  424. if ($fsize <= 1024 * 1024 * 40) {
  425. return 4096 * 30;
  426. }
  427. if ($fsize <= 1024 * 1024 * 80) {
  428. return 4096 * 47;
  429. }
  430. if ($fsize <= 1024 * 1024 * 120) {
  431. return 4096 * 65;
  432. }
  433. if ($fsize <= 1024 * 1024 * 150) {
  434. return 4096 * 70;
  435. }
  436. if ($fsize <= 1024 * 1024 * 200) {
  437. return 4096 * 85;
  438. }
  439. if ($fsize <= 1024 * 1024 * 250) {
  440. return 4096 * 100;
  441. }
  442. if ($fsize <= 1024 * 1024 * 300) {
  443. return 4096 * 115;
  444. }
  445. if ($fsize <= 1024 * 1024 * 400) {
  446. return 4096 * 135;
  447. }
  448. if ($fsize <= 1024 * 1024 * 500) {
  449. return 4096 * 170;
  450. }
  451. if ($fsize <= 1024 * 1024 * 1000) {
  452. return 4096 * 200;
  453. }
  454. return 4096 * 210;
  455. }
  456. function upfile($host, $port, $url, $referer, $cookie, $post, $file, $filename, $fieldname, $field2name = "", $proxy = 0, $pauth = 0, $upagent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.1") {
  457. global $nn, $lastError, $sleep_time, $sleep_count;
  458. $scheme = "http://";
  459. $bound = "--------" . md5(microtime());
  460. $saveToFile = 0;
  461. foreach ( $post as $key => $value ) {
  462. $postdata .= "--" . $bound . $nn;
  463. $postdata .= 'Content-Disposition: form-data; name="'.$key.'"' . $nn . $nn;
  464. $postdata .= $value . $nn;
  465. }
  466. $fileSize = getSize ( $file );
  467. $fieldname = $fieldname ? $fieldname : file . md5 ( $filename );
  468. if (! is_readable ( $file )) {
  469. $lastError = sprintf(lang(65),$file);
  470. return FALSE;
  471. }
  472. if ($field2name != '') {
  473. $postdata .= "--" . $bound . $nn;
  474. $postdata .= 'Content-Disposition: form-data; name="'.$field2name.'"; filename=""' . $nn;
  475. $postdata .= "Content-Type: application/octet-stream" . $nn . $nn;
  476. }
  477. $postdata .= "--" . $bound . $nn;
  478. $postdata .= 'Content-Disposition: form-data; name="'.$fieldname.'"; filename="'.$filename.'"' . $nn;
  479. $postdata .= "Content-Type: application/octet-stream" . $nn . $nn;
  480. $cookies = "";
  481. if ($cookie) {
  482. if (is_array ( $cookie )) {
  483. for($h = 0; $h < count ( $cookie ); $h ++) {
  484. $cookies .= "Cookie: " . trim ( $cookie [$h] ) . $nn;
  485. }
  486. } else {
  487. $cookies = "Cookie: " . trim ( $cookie ) . $nn;
  488. }
  489. }
  490. $referer = $referer ? "Referer: " . $referer . $nn : "";
  491. if ($scheme == "https://") {
  492. $scheme = "ssl://";
  493. $port = 443;
  494. }
  495. if ($proxy) {
  496. list ( $proxyHost, $proxyPort ) = explode ( ":", $proxy);
  497. $url = $scheme . $host . ":" . $port . $url;
  498. $host = $host . ":" . $port;
  499. }
  500. if ($scheme != "ssl://") {
  501. $scheme = "";
  502. }
  503. $http_auth = ($auth) ? "Authorization: Basic " . $auth . $nn : "";
  504. $proxyauth = ($pauth) ? "Proxy-Authorization: Basic " . $pauth . $nn : "";
  505. $zapros = "POST " . str_replace ( " ", "%20", $url ) . " HTTP/1.0" . $nn . "Host: " . $host . $nn . $cookies . "Content-Type: multipart/form-data; boundary=" . $bound . $nn . "Content-Length: " . (strlen ( $postdata ) + strlen ( $nn . "--" . $bound . "--" . $nn ) + $fileSize) . $nn . "User-Agent: " . $upagent . $nn . "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" . $nn . "Accept-Language: en-en,en;q=0.5" . $nn . "Accept-Charset: utf-8,windows-1251;koi8-r;q=0.7,*;q=0.7" . $nn . "Connection: Close" . $nn . $http_auth . $proxyauth . $referer . $nn . $postdata;
  506. $errno = 0; $errstr = "";
  507. $posturl = ($proxyHost ? $scheme . $proxyHost : $scheme . $host) . ':' . ($proxyPort ? $proxyPort : $port);
  508. $fp = @stream_socket_client ( $posturl, $errno, $errstr, 120, STREAM_CLIENT_CONNECT );
  509. //$fp = @fsockopen ( $host, $port, $errno, $errstr, 150 );
  510. //stream_set_timeout ( $fp, 300 );
  511. if (! $fp) {
  512. $dis_host = $proxyHost ? $proxyHost : $host;
  513. $dis_port = $proxyPort ? $proxyPort : $port;
  514. html_error ( sprintf ( lang ( 88 ), $dis_host, $dis_port ) );
  515. }
  516. if ($errno || $errstr) {
  517. $lastError = $errstr;
  518. return false;
  519. }
  520. if ($proxy) {
  521. echo '<p>'.sprintf(lang(89),$proxyHost,$proxyPort).'<br />';
  522. echo "UPLOAD: <b>" . $url . "</b>...<br />\n";
  523. } else {
  524. echo "<p>";
  525. printf(lang(90),$host,$port);
  526. echo "</p>";
  527. }
  528. echo(lang(104).' <b>'.$filename.'</b>, '.lang(56).' <b>'.bytesToKbOrMb ( $fileSize ).'</b>...<br />');
  529. global $id;
  530. $id = md5 ( time () * rand ( 0, 10 ) );
  531. require (TEMPLATE_DIR . '/uploadui.php');
  532. flush ();
  533. $timeStart = getmicrotime ();
  534. //$chunkSize = 16384; // Use this value no matter what (using this actually just causes massive cpu usage for large files, too much data is flushed to the browser!)
  535. $chunkSize = GetChunkSize ( $fileSize );
  536. fputs ( $fp, $zapros );
  537. fflush ( $fp );
  538. $fs = fopen ( $file, 'r' );
  539. $local_sleep = $sleep_count;
  540. //echo('<script type="text/javascript">');
  541. while ( ! feof ( $fs ) ) {
  542. $data = fread ( $fs, $chunkSize );
  543. if ($data === false) {
  544. fclose ( $fs );
  545. fclose ( $fp );
  546. html_error ( lang(112) );
  547. }
  548. if (($sleep_count !== false) && ($sleep_time !== false) && is_numeric ( $sleep_time ) && is_numeric ( $sleep_count ) && ($sleep_count > 0) && ($sleep_time > 0)) {
  549. $local_sleep --;
  550. if ($local_sleep == 0) {
  551. usleep ( $sleep_time );
  552. $local_sleep = $sleep_count;
  553. }
  554. }
  555. $sendbyte = fputs ( $fp, $data );
  556. fflush ( $fp );
  557. if ($sendbyte === false) {
  558. fclose ( $fs );
  559. fclose ( $fp );
  560. html_error ( lang(113) );
  561. }
  562. $totalsend += $sendbyte;
  563. $time = getmicrotime () - $timeStart;
  564. $chunkTime = $time - $lastChunkTime;
  565. $chunkTime = $chunkTime ? $chunkTime : 1;
  566. $lastChunkTime = $time;
  567. $speed = round ( $sendbyte / 1024 / $chunkTime, 2 );
  568. $percent = round ( $totalsend / $fileSize * 100, 2 );
  569. echo '<script type="text/javascript">pr('."'" . $percent . "', '" . bytesToKbOrMb ( $totalsend ) . "', '" . $speed . "');</script>\n";
  570. flush ();
  571. }
  572. //echo('</script>');
  573. fclose ( $fs );
  574. fputs ( $fp, $nn . "--" . $bound . "--" . $nn );
  575. fflush ( $fp );
  576. while ( ! feof ( $fp ) ) {
  577. $data = fgets ( $fp, 16384 );
  578. if ($data === false) {
  579. break;
  580. }
  581. $page .= $data;
  582. }
  583. fclose ( $fp );
  584. return $page;
  585. }
  586. ?>