Http.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. /**
  12. * Http 工具类
  13. * 提供一系列的Http方法
  14. * @category ORG
  15. * @package ORG
  16. * @subpackage Net
  17. * @author liu21st <liu21st@gmail.com>
  18. */
  19. class Http {
  20. /**
  21. * 采集远程文件
  22. * @access public
  23. * @param string $remote 远程文件名
  24. * @param string $local 本地保存文件名
  25. * @return mixed
  26. */
  27. static public function curlDownload($remote,$local) {
  28. $cp = curl_init($remote);
  29. $fp = fopen($local,"w");
  30. curl_setopt($cp, CURLOPT_FILE, $fp);
  31. curl_setopt($cp, CURLOPT_HEADER, 0);
  32. curl_exec($cp);
  33. curl_close($cp);
  34. fclose($fp);
  35. }
  36. /**
  37. * 使用 fsockopen 通过 HTTP 协议直接访问(采集)远程文件
  38. * 如果主机或服务器没有开启 CURL 扩展可考虑使用
  39. * fsockopen 比 CURL 稍慢,但性能稳定
  40. * @static
  41. * @access public
  42. * @param string $url 远程URL
  43. * @param array $conf 其他配置信息
  44. * int limit 分段读取字符个数
  45. * string post post的内容,字符串或数组,key=value&形式
  46. * string cookie 携带cookie访问,该参数是cookie内容
  47. * string ip 如果该参数传入,$url将不被使用,ip访问优先
  48. * int timeout 采集超时时间
  49. * bool block 是否阻塞访问,默认为true
  50. * @return mixed
  51. */
  52. static public function fsockopenDownload($url, $conf = array()) {
  53. $return = '';
  54. if(!is_array($conf)) return $return;
  55. $matches = parse_url($url);
  56. !isset($matches['host']) && $matches['host'] = '';
  57. !isset($matches['path']) && $matches['path'] = '';
  58. !isset($matches['query']) && $matches['query'] = '';
  59. !isset($matches['port']) && $matches['port'] = '';
  60. $host = $matches['host'];
  61. $path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/';
  62. $port = !empty($matches['port']) ? $matches['port'] : 80;
  63. $conf_arr = array(
  64. 'limit' => 0,
  65. 'post' => '',
  66. 'cookie' => '',
  67. 'ip' => '',
  68. 'timeout' => 15,
  69. 'block' => TRUE,
  70. );
  71. foreach (array_merge($conf_arr, $conf) as $k=>$v) ${$k} = $v;
  72. if($post) {
  73. if(is_array($post))
  74. {
  75. $post = http_build_query($post);
  76. }
  77. $out = "POST $path HTTP/1.0\r\n";
  78. $out .= "Accept: */*\r\n";
  79. //$out .= "Referer: $boardurl\r\n";
  80. $out .= "Accept-Language: zh-cn\r\n";
  81. $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
  82. $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
  83. $out .= "Host: $host\r\n";
  84. $out .= 'Content-Length: '.strlen($post)."\r\n";
  85. $out .= "Connection: Close\r\n";
  86. $out .= "Cache-Control: no-cache\r\n";
  87. $out .= "Cookie: $cookie\r\n\r\n";
  88. $out .= $post;
  89. } else {
  90. $out = "GET $path HTTP/1.0\r\n";
  91. $out .= "Accept: */*\r\n";
  92. //$out .= "Referer: $boardurl\r\n";
  93. $out .= "Accept-Language: zh-cn\r\n";
  94. $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
  95. $out .= "Host: $host\r\n";
  96. $out .= "Connection: Close\r\n";
  97. $out .= "Cookie: $cookie\r\n\r\n";
  98. }
  99. $fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);
  100. if(!$fp) {
  101. return '';
  102. } else {
  103. stream_set_blocking($fp, $block);
  104. stream_set_timeout($fp, $timeout);
  105. @fwrite($fp, $out);
  106. $status = stream_get_meta_data($fp);
  107. if(!$status['timed_out']) {
  108. while (!feof($fp)) {
  109. if(($header = @fgets($fp)) && ($header == "\r\n" || $header == "\n")) {
  110. break;
  111. }
  112. }
  113. $stop = false;
  114. while(!feof($fp) && !$stop) {
  115. $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));
  116. $return .= $data;
  117. if($limit) {
  118. $limit -= strlen($data);
  119. $stop = $limit <= 0;
  120. }
  121. }
  122. }
  123. @fclose($fp);
  124. return $return;
  125. }
  126. }
  127. /**
  128. * 下载文件
  129. * 可以指定下载显示的文件名,并自动发送相应的Header信息
  130. * 如果指定了content参数,则下载该参数的内容
  131. * @static
  132. * @access public
  133. * @param string $filename 下载文件名
  134. * @param string $showname 下载显示的文件名
  135. * @param string $content 下载的内容
  136. * @param integer $expire 下载内容浏览器缓存时间
  137. * @return void
  138. */
  139. static public function download ($filename, $showname='',$content='',$expire=180) {
  140. if(is_file($filename)) {
  141. $length = filesize($filename);
  142. }elseif(is_file(UPLOAD_PATH.$filename)) {
  143. $filename = UPLOAD_PATH.$filename;
  144. $length = filesize($filename);
  145. }elseif($content != '') {
  146. $length = strlen($content);
  147. }else {
  148. throw_exception($filename.L('下载文件不存在!'));
  149. }
  150. if(empty($showname)) {
  151. $showname = $filename;
  152. }
  153. $showname = basename($showname);
  154. if(!empty($filename)) {
  155. $type = mime_content_type($filename);
  156. }else{
  157. $type = "application/octet-stream";
  158. }
  159. //发送Http Header信息 开始下载
  160. header("Pragma: public");
  161. header("Cache-control: max-age=".$expire);
  162. //header('Cache-Control: no-store, no-cache, must-revalidate');
  163. header("Expires: " . gmdate("D, d M Y H:i:s",time()+$expire) . "GMT");
  164. header("Last-Modified: " . gmdate("D, d M Y H:i:s",time()) . "GMT");
  165. header("Content-Disposition: attachment; filename=".$showname);
  166. header("Content-Length: ".$length);
  167. header("Content-type: ".$type);
  168. header('Content-Encoding: none');
  169. header("Content-Transfer-Encoding: binary" );
  170. if($content == '' ) {
  171. readfile($filename);
  172. }else {
  173. echo($content);
  174. }
  175. exit();
  176. }
  177. /**
  178. * 显示HTTP Header 信息
  179. * @return string
  180. */
  181. static function getHeaderInfo($header='',$echo=true) {
  182. ob_start();
  183. $headers = getallheaders();
  184. if(!empty($header)) {
  185. $info = $headers[$header];
  186. echo($header.':'.$info."\n"); ;
  187. }else {
  188. foreach($headers as $key=>$val) {
  189. echo("$key:$val\n");
  190. }
  191. }
  192. $output = ob_get_clean();
  193. if ($echo) {
  194. echo (nl2br($output));
  195. }else {
  196. return $output;
  197. }
  198. }
  199. /**
  200. * HTTP Protocol defined status codes
  201. * @param int $num
  202. */
  203. static function sendHttpStatus($code) {
  204. static $_status = array(
  205. // Informational 1xx
  206. 100 => 'Continue',
  207. 101 => 'Switching Protocols',
  208. // Success 2xx
  209. 200 => 'OK',
  210. 201 => 'Created',
  211. 202 => 'Accepted',
  212. 203 => 'Non-Authoritative Information',
  213. 204 => 'No Content',
  214. 205 => 'Reset Content',
  215. 206 => 'Partial Content',
  216. // Redirection 3xx
  217. 300 => 'Multiple Choices',
  218. 301 => 'Moved Permanently',
  219. 302 => 'Found', // 1.1
  220. 303 => 'See Other',
  221. 304 => 'Not Modified',
  222. 305 => 'Use Proxy',
  223. // 306 is deprecated but reserved
  224. 307 => 'Temporary Redirect',
  225. // Client Error 4xx
  226. 400 => 'Bad Request',
  227. 401 => 'Unauthorized',
  228. 402 => 'Payment Required',
  229. 403 => 'Forbidden',
  230. 404 => 'Not Found',
  231. 405 => 'Method Not Allowed',
  232. 406 => 'Not Acceptable',
  233. 407 => 'Proxy Authentication Required',
  234. 408 => 'Request Timeout',
  235. 409 => 'Conflict',
  236. 410 => 'Gone',
  237. 411 => 'Length Required',
  238. 412 => 'Precondition Failed',
  239. 413 => 'Request Entity Too Large',
  240. 414 => 'Request-URI Too Long',
  241. 415 => 'Unsupported Media Type',
  242. 416 => 'Requested Range Not Satisfiable',
  243. 417 => 'Expectation Failed',
  244. // Server Error 5xx
  245. 500 => 'Internal Server Error',
  246. 501 => 'Not Implemented',
  247. 502 => 'Bad Gateway',
  248. 503 => 'Service Unavailable',
  249. 504 => 'Gateway Timeout',
  250. 505 => 'HTTP Version Not Supported',
  251. 509 => 'Bandwidth Limit Exceeded'
  252. );
  253. if(isset($_status[$code])) {
  254. header('HTTP/1.1 '.$code.' '.$_status[$code]);
  255. }
  256. }
  257. }//类定义结束
  258. if( !function_exists ('mime_content_type')) {
  259. /**
  260. * 获取文件的mime_content类型
  261. * @return string
  262. */
  263. function mime_content_type($filename) {
  264. static $contentType = array(
  265. 'ai' => 'application/postscript',
  266. 'aif' => 'audio/x-aiff',
  267. 'aifc' => 'audio/x-aiff',
  268. 'aiff' => 'audio/x-aiff',
  269. 'asc' => 'application/pgp', //changed by skwashd - was text/plain
  270. 'asf' => 'video/x-ms-asf',
  271. 'asx' => 'video/x-ms-asf',
  272. 'au' => 'audio/basic',
  273. 'avi' => 'video/x-msvideo',
  274. 'bcpio' => 'application/x-bcpio',
  275. 'bin' => 'application/octet-stream',
  276. 'bmp' => 'image/bmp',
  277. 'c' => 'text/plain', // or 'text/x-csrc', //added by skwashd
  278. 'cc' => 'text/plain', // or 'text/x-c++src', //added by skwashd
  279. 'cs' => 'text/plain', //added by skwashd - for C# src
  280. 'cpp' => 'text/x-c++src', //added by skwashd
  281. 'cxx' => 'text/x-c++src', //added by skwashd
  282. 'cdf' => 'application/x-netcdf',
  283. 'class' => 'application/octet-stream',//secure but application/java-class is correct
  284. 'com' => 'application/octet-stream',//added by skwashd
  285. 'cpio' => 'application/x-cpio',
  286. 'cpt' => 'application/mac-compactpro',
  287. 'csh' => 'application/x-csh',
  288. 'css' => 'text/css',
  289. 'csv' => 'text/comma-separated-values',//added by skwashd
  290. 'dcr' => 'application/x-director',
  291. 'diff' => 'text/diff',
  292. 'dir' => 'application/x-director',
  293. 'dll' => 'application/octet-stream',
  294. 'dms' => 'application/octet-stream',
  295. 'doc' => 'application/msword',
  296. 'dot' => 'application/msword',//added by skwashd
  297. 'dvi' => 'application/x-dvi',
  298. 'dxr' => 'application/x-director',
  299. 'eps' => 'application/postscript',
  300. 'etx' => 'text/x-setext',
  301. 'exe' => 'application/octet-stream',
  302. 'ez' => 'application/andrew-inset',
  303. 'gif' => 'image/gif',
  304. 'gtar' => 'application/x-gtar',
  305. 'gz' => 'application/x-gzip',
  306. 'h' => 'text/plain', // or 'text/x-chdr',//added by skwashd
  307. 'h++' => 'text/plain', // or 'text/x-c++hdr', //added by skwashd
  308. 'hh' => 'text/plain', // or 'text/x-c++hdr', //added by skwashd
  309. 'hpp' => 'text/plain', // or 'text/x-c++hdr', //added by skwashd
  310. 'hxx' => 'text/plain', // or 'text/x-c++hdr', //added by skwashd
  311. 'hdf' => 'application/x-hdf',
  312. 'hqx' => 'application/mac-binhex40',
  313. 'htm' => 'text/html',
  314. 'html' => 'text/html',
  315. 'ice' => 'x-conference/x-cooltalk',
  316. 'ics' => 'text/calendar',
  317. 'ief' => 'image/ief',
  318. 'ifb' => 'text/calendar',
  319. 'iges' => 'model/iges',
  320. 'igs' => 'model/iges',
  321. 'jar' => 'application/x-jar', //added by skwashd - alternative mime type
  322. 'java' => 'text/x-java-source', //added by skwashd
  323. 'jpe' => 'image/jpeg',
  324. 'jpeg' => 'image/jpeg',
  325. 'jpg' => 'image/jpeg',
  326. 'js' => 'application/x-javascript',
  327. 'kar' => 'audio/midi',
  328. 'latex' => 'application/x-latex',
  329. 'lha' => 'application/octet-stream',
  330. 'log' => 'text/plain',
  331. 'lzh' => 'application/octet-stream',
  332. 'm3u' => 'audio/x-mpegurl',
  333. 'man' => 'application/x-troff-man',
  334. 'me' => 'application/x-troff-me',
  335. 'mesh' => 'model/mesh',
  336. 'mid' => 'audio/midi',
  337. 'midi' => 'audio/midi',
  338. 'mif' => 'application/vnd.mif',
  339. 'mov' => 'video/quicktime',
  340. 'movie' => 'video/x-sgi-movie',
  341. 'mp2' => 'audio/mpeg',
  342. 'mp3' => 'audio/mpeg',
  343. 'mpe' => 'video/mpeg',
  344. 'mpeg' => 'video/mpeg',
  345. 'mpg' => 'video/mpeg',
  346. 'mpga' => 'audio/mpeg',
  347. 'ms' => 'application/x-troff-ms',
  348. 'msh' => 'model/mesh',
  349. 'mxu' => 'video/vnd.mpegurl',
  350. 'nc' => 'application/x-netcdf',
  351. 'oda' => 'application/oda',
  352. 'patch' => 'text/diff',
  353. 'pbm' => 'image/x-portable-bitmap',
  354. 'pdb' => 'chemical/x-pdb',
  355. 'pdf' => 'application/pdf',
  356. 'pgm' => 'image/x-portable-graymap',
  357. 'pgn' => 'application/x-chess-pgn',
  358. 'pgp' => 'application/pgp',//added by skwashd
  359. 'php' => 'application/x-httpd-php',
  360. 'php3' => 'application/x-httpd-php3',
  361. 'pl' => 'application/x-perl',
  362. 'pm' => 'application/x-perl',
  363. 'png' => 'image/png',
  364. 'pnm' => 'image/x-portable-anymap',
  365. 'po' => 'text/plain',
  366. 'ppm' => 'image/x-portable-pixmap',
  367. 'ppt' => 'application/vnd.ms-powerpoint',
  368. 'ps' => 'application/postscript',
  369. 'qt' => 'video/quicktime',
  370. 'ra' => 'audio/x-realaudio',
  371. 'rar' => 'application/octet-stream',
  372. 'ram' => 'audio/x-pn-realaudio',
  373. 'ras' => 'image/x-cmu-raster',
  374. 'rgb' => 'image/x-rgb',
  375. 'rm' => 'audio/x-pn-realaudio',
  376. 'roff' => 'application/x-troff',
  377. 'rpm' => 'audio/x-pn-realaudio-plugin',
  378. 'rtf' => 'text/rtf',
  379. 'rtx' => 'text/richtext',
  380. 'sgm' => 'text/sgml',
  381. 'sgml' => 'text/sgml',
  382. 'sh' => 'application/x-sh',
  383. 'shar' => 'application/x-shar',
  384. 'shtml' => 'text/html',
  385. 'silo' => 'model/mesh',
  386. 'sit' => 'application/x-stuffit',
  387. 'skd' => 'application/x-koan',
  388. 'skm' => 'application/x-koan',
  389. 'skp' => 'application/x-koan',
  390. 'skt' => 'application/x-koan',
  391. 'smi' => 'application/smil',
  392. 'smil' => 'application/smil',
  393. 'snd' => 'audio/basic',
  394. 'so' => 'application/octet-stream',
  395. 'spl' => 'application/x-futuresplash',
  396. 'src' => 'application/x-wais-source',
  397. 'stc' => 'application/vnd.sun.xml.calc.template',
  398. 'std' => 'application/vnd.sun.xml.draw.template',
  399. 'sti' => 'application/vnd.sun.xml.impress.template',
  400. 'stw' => 'application/vnd.sun.xml.writer.template',
  401. 'sv4cpio' => 'application/x-sv4cpio',
  402. 'sv4crc' => 'application/x-sv4crc',
  403. 'swf' => 'application/x-shockwave-flash',
  404. 'sxc' => 'application/vnd.sun.xml.calc',
  405. 'sxd' => 'application/vnd.sun.xml.draw',
  406. 'sxg' => 'application/vnd.sun.xml.writer.global',
  407. 'sxi' => 'application/vnd.sun.xml.impress',
  408. 'sxm' => 'application/vnd.sun.xml.math',
  409. 'sxw' => 'application/vnd.sun.xml.writer',
  410. 't' => 'application/x-troff',
  411. 'tar' => 'application/x-tar',
  412. 'tcl' => 'application/x-tcl',
  413. 'tex' => 'application/x-tex',
  414. 'texi' => 'application/x-texinfo',
  415. 'texinfo' => 'application/x-texinfo',
  416. 'tgz' => 'application/x-gtar',
  417. 'tif' => 'image/tiff',
  418. 'tiff' => 'image/tiff',
  419. 'tr' => 'application/x-troff',
  420. 'tsv' => 'text/tab-separated-values',
  421. 'txt' => 'text/plain',
  422. 'ustar' => 'application/x-ustar',
  423. 'vbs' => 'text/plain', //added by skwashd - for obvious reasons
  424. 'vcd' => 'application/x-cdlink',
  425. 'vcf' => 'text/x-vcard',
  426. 'vcs' => 'text/calendar',
  427. 'vfb' => 'text/calendar',
  428. 'vrml' => 'model/vrml',
  429. 'vsd' => 'application/vnd.visio',
  430. 'wav' => 'audio/x-wav',
  431. 'wax' => 'audio/x-ms-wax',
  432. 'wbmp' => 'image/vnd.wap.wbmp',
  433. 'wbxml' => 'application/vnd.wap.wbxml',
  434. 'wm' => 'video/x-ms-wm',
  435. 'wma' => 'audio/x-ms-wma',
  436. 'wmd' => 'application/x-ms-wmd',
  437. 'wml' => 'text/vnd.wap.wml',
  438. 'wmlc' => 'application/vnd.wap.wmlc',
  439. 'wmls' => 'text/vnd.wap.wmlscript',
  440. 'wmlsc' => 'application/vnd.wap.wmlscriptc',
  441. 'wmv' => 'video/x-ms-wmv',
  442. 'wmx' => 'video/x-ms-wmx',
  443. 'wmz' => 'application/x-ms-wmz',
  444. 'wrl' => 'model/vrml',
  445. 'wvx' => 'video/x-ms-wvx',
  446. 'xbm' => 'image/x-xbitmap',
  447. 'xht' => 'application/xhtml+xml',
  448. 'xhtml' => 'application/xhtml+xml',
  449. 'xls' => 'application/vnd.ms-excel',
  450. 'xlt' => 'application/vnd.ms-excel',
  451. 'xml' => 'application/xml',
  452. 'xpm' => 'image/x-xpixmap',
  453. 'xsl' => 'text/xml',
  454. 'xwd' => 'image/x-xwindowdump',
  455. 'xyz' => 'chemical/x-xyz',
  456. 'z' => 'application/x-compress',
  457. 'zip' => 'application/zip',
  458. );
  459. $type = strtolower(substr(strrchr($filename, '.'),1));
  460. if(isset($contentType[$type])) {
  461. $mime = $contentType[$type];
  462. }else {
  463. $mime = 'application/octet-stream';
  464. }
  465. return $mime;
  466. }
  467. }
  468. if(!function_exists('image_type_to_extension')){
  469. function image_type_to_extension($imagetype) {
  470. if(empty($imagetype)) return false;
  471. switch($imagetype) {
  472. case IMAGETYPE_GIF : return '.gif';
  473. case IMAGETYPE_JPEG : return '.jpg';
  474. case IMAGETYPE_PNG : return '.png';
  475. case IMAGETYPE_SWF : return '.swf';
  476. case IMAGETYPE_PSD : return '.psd';
  477. case IMAGETYPE_BMP : return '.bmp';
  478. case IMAGETYPE_TIFF_II : return '.tiff';
  479. case IMAGETYPE_TIFF_MM : return '.tiff';
  480. case IMAGETYPE_JPC : return '.jpc';
  481. case IMAGETYPE_JP2 : return '.jp2';
  482. case IMAGETYPE_JPX : return '.jpf';
  483. case IMAGETYPE_JB2 : return '.jb2';
  484. case IMAGETYPE_SWC : return '.swc';
  485. case IMAGETYPE_IFF : return '.aiff';
  486. case IMAGETYPE_WBMP : return '.wbmp';
  487. case IMAGETYPE_XBM : return '.xbm';
  488. default : return false;
  489. }
  490. }
  491. }