function_filesock.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: function_filesock.php 36279 2016-12-09 07:54:31Z nemohou $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. function _dfsockopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE, $ip = '', $timeout = 15, $block = TRUE, $encodetype = 'URLENCODE', $allowcurl = TRUE, $position = 0, $files = array()) {
  12. $return = '';
  13. $matches = parse_url($url);
  14. $scheme = $matches['scheme'];
  15. $host = $matches['host'];
  16. $path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/';
  17. $port = !empty($matches['port']) ? $matches['port'] : ($scheme == 'http' ? '80' : '');
  18. $boundary = $encodetype == 'URLENCODE' ? '' : random(40);
  19. if($post) {
  20. if(!is_array($post)) {
  21. parse_str($post, $post);
  22. }
  23. _format_postkey($post, $postnew);
  24. $post = $postnew;
  25. }
  26. if(function_exists('curl_init') && function_exists('curl_exec') && $allowcurl) {
  27. $ch = curl_init();
  28. $httpheader = array();
  29. if($ip) {
  30. $httpheader[] = "Host: ".$host;
  31. }
  32. if($httpheader) {
  33. curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
  34. }
  35. curl_setopt($ch, CURLOPT_URL, $scheme.'://'.($ip ? $ip : $host).($port ? ':'.$port : '').$path);
  36. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  37. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  38. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  39. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  40. curl_setopt($ch, CURLOPT_HEADER, 1);
  41. if($post) {
  42. curl_setopt($ch, CURLOPT_POST, 1);
  43. if($encodetype == 'URLENCODE') {
  44. curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  45. } else {
  46. foreach($post as $k => $v) {
  47. if(isset($files[$k])) {
  48. $post[$k] = '@'.$files[$k];
  49. }
  50. }
  51. foreach($files as $k => $file) {
  52. if(!isset($post[$k]) && file_exists($file)) {
  53. $post[$k] = '@'.$file;
  54. }
  55. }
  56. curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  57. }
  58. }
  59. if($cookie) {
  60. curl_setopt($ch, CURLOPT_COOKIE, $cookie);
  61. }
  62. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  63. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  64. $data = curl_exec($ch);
  65. $status = curl_getinfo($ch);
  66. $errno = curl_errno($ch);
  67. curl_close($ch);
  68. if($errno || $status['http_code'] != 200) {
  69. return;
  70. } else {
  71. $GLOBALS['filesockheader'] = substr($data, 0, $status['header_size']);
  72. $data = substr($data, $status['header_size']);
  73. return !$limit ? $data : substr($data, 0, $limit);
  74. }
  75. }
  76. if($post) {
  77. if($encodetype == 'URLENCODE') {
  78. $data = http_build_query($post);
  79. } else {
  80. $data = '';
  81. foreach($post as $k => $v) {
  82. $data .= "--$boundary\r\n";
  83. $data .= 'Content-Disposition: form-data; name="'.$k.'"'.(isset($files[$k]) ? '; filename="'.basename($files[$k]).'"; Content-Type: application/octet-stream' : '')."\r\n\r\n";
  84. $data .= $v."\r\n";
  85. }
  86. foreach($files as $k => $file) {
  87. if(!isset($post[$k]) && file_exists($file)) {
  88. if($fp = @fopen($file, 'r')) {
  89. $v = fread($fp, filesize($file));
  90. fclose($fp);
  91. $data .= "--$boundary\r\n";
  92. $data .= 'Content-Disposition: form-data; name="'.$k.'"; filename="'.basename($file).'"; Content-Type: application/octet-stream'."\r\n\r\n";
  93. $data .= $v."\r\n";
  94. }
  95. }
  96. }
  97. $data .= "--$boundary\r\n";
  98. }
  99. $out = "POST $path HTTP/1.0\r\n";
  100. $header = "Accept: */*\r\n";
  101. $header .= "Accept-Language: zh-cn\r\n";
  102. $header .= $encodetype == 'URLENCODE' ? "Content-Type: application/x-www-form-urlencoded\r\n" : "Content-Type: multipart/form-data; boundary=$boundary\r\n";
  103. $header .= 'Content-Length: '.strlen($data)."\r\n";
  104. $header .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
  105. $header .= "Host: $host:$port\r\n";
  106. $header .= "Connection: Close\r\n";
  107. $header .= "Cache-Control: no-cache\r\n";
  108. $header .= "Cookie: $cookie\r\n\r\n";
  109. $out .= $header;
  110. $out .= $data;
  111. } else {
  112. $out = "GET $path HTTP/1.0\r\n";
  113. $header = "Accept: */*\r\n";
  114. $header .= "Accept-Language: zh-cn\r\n";
  115. $header .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
  116. $header .= "Host: $host:$port\r\n";
  117. $header .= "Connection: Close\r\n";
  118. $header .= "Cookie: $cookie\r\n\r\n";
  119. $out .= $header;
  120. }
  121. $fpflag = 0;
  122. if(!$fp = @fsocketopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout)) {
  123. $context = array(
  124. 'http' => array(
  125. 'method' => $post ? 'POST' : 'GET',
  126. 'header' => $header,
  127. 'content' => $post,
  128. 'timeout' => $timeout,
  129. ),
  130. );
  131. $context = stream_context_create($context);
  132. $fp = @fopen($scheme.'://'.($ip ? $ip : $host).':'.$port.$path, 'b', false, $context);
  133. $fpflag = 1;
  134. }
  135. if(!$fp) {
  136. return '';
  137. } else {
  138. stream_set_blocking($fp, $block);
  139. stream_set_timeout($fp, $timeout);
  140. @fwrite($fp, $out);
  141. $status = stream_get_meta_data($fp);
  142. if(!$status['timed_out']) {
  143. while (!feof($fp) && !$fpflag) {
  144. $header = @fgets($fp);
  145. $headers .= $header;
  146. if($header && ($header == "\r\n" || $header == "\n")) {
  147. break;
  148. }
  149. }
  150. $GLOBALS['filesockheader'] = $headers;
  151. if($position) {
  152. for($i=0; $i<$position; $i++) {
  153. $char = fgetc($fp);
  154. if($char == "\n" && $oldchar != "\r") {
  155. $i++;
  156. }
  157. $oldchar = $char;
  158. }
  159. }
  160. if($limit) {
  161. $return = stream_get_contents($fp, $limit);
  162. } else {
  163. $return = stream_get_contents($fp);
  164. }
  165. }
  166. @fclose($fp);
  167. return $return;
  168. }
  169. }
  170. function _format_postkey($post, &$result, $key = '') {
  171. foreach($post as $k => $v) {
  172. $_k = $key ? $key.'['.$k.']' : $k;
  173. if(is_array($v)) {
  174. _format_postkey($v, $result, $_k);
  175. } else {
  176. $result[$_k] = $v;
  177. }
  178. }
  179. }
  180. ?>