misc.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /*
  3. [UCenter] (C)2001-2099 Comsenz Inc.
  4. This is NOT a freeware, use is subject to license terms
  5. $Id: misc.php 1127 2011-12-14 04:24:58Z svn_project_zhangjie $
  6. */
  7. !defined('IN_UC') && exit('Access Denied');
  8. define('UC_ARRAY_SEP_1', 'UC_ARRAY_SEP_1');
  9. define('UC_ARRAY_SEP_2', 'UC_ARRAY_SEP_2');
  10. class miscmodel {
  11. var $db;
  12. var $base;
  13. function __construct(&$base) {
  14. $this->miscmodel($base);
  15. }
  16. function miscmodel(&$base) {
  17. $this->base = $base;
  18. $this->db = $base->db;
  19. }
  20. function get_host_by_url($url) {
  21. $m = parse_url($url);
  22. if(!$m['host']) {
  23. return -1;
  24. }
  25. if(!preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/", $m['host'])) {
  26. $ip = @gethostbyname($m['host']);
  27. if(!$ip || $ip == $m['host']) {
  28. return -2;
  29. }
  30. return $ip;
  31. } else {
  32. return $m['host'];
  33. }
  34. }
  35. function check_url($url) {
  36. return preg_match("/(https?){1}:\/\/|www\.([^\[\"']+?)?/i", $url);
  37. }
  38. function check_ip($url) {
  39. return preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/", $url);
  40. }
  41. function dfopen2($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE, $ip = '', $timeout = 15, $block = TRUE, $encodetype = 'URLENCODE') {
  42. $__times__ = isset($_GET['__times__']) ? intval($_GET['__times__']) + 1 : 1;
  43. if($__times__ > 2) {
  44. return '';
  45. }
  46. $url .= (strpos($url, '?') === FALSE ? '?' : '&')."__times__=$__times__";
  47. return $this->dfopen($url, $limit, $post, $cookie, $bysocket, $ip, $timeout, $block, $encodetype);
  48. }
  49. function dfopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE , $ip = '', $timeout = 15, $block = TRUE, $encodetype = 'URLENCODE') {
  50. $return = '';
  51. $matches = parse_url($url);
  52. $scheme = $matches['scheme'];
  53. $host = $matches['host'];
  54. $path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/';
  55. $port = !empty($matches['port']) ? $matches['port'] : ($matches['scheme'] == 'https' ? 443 : 80);
  56. if($post) {
  57. $out = "POST $path HTTP/1.0\r\n";
  58. $header = "Accept: */*\r\n";
  59. $header .= "Accept-Language: zh-cn\r\n";
  60. $boundary = $encodetype == 'URLENCODE' ? '' : ';'.substr($post, 0, trim(strpos($post, "\n")));
  61. $header .= $encodetype == 'URLENCODE' ? "Content-Type: application/x-www-form-urlencoded\r\n" : "Content-Type: multipart/form-data$boundary\r\n";
  62. $header .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
  63. $header .= "Host: $host:$port\r\n";
  64. $header .= 'Content-Length: '.strlen($post)."\r\n";
  65. $header .= "Connection: Close\r\n";
  66. $header .= "Cache-Control: no-cache\r\n";
  67. $header .= "Cookie: $cookie\r\n\r\n";
  68. $out .= $header.$post;
  69. } else {
  70. $out = "GET $path HTTP/1.0\r\n";
  71. $header = "Accept: */*\r\n";
  72. $header .= "Accept-Language: zh-cn\r\n";
  73. $header .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
  74. $header .= "Host: $host:$port\r\n";
  75. $header .= "Connection: Close\r\n";
  76. $header .= "Cookie: $cookie\r\n\r\n";
  77. $out .= $header;
  78. }
  79. $fpflag = 0;
  80. if(!$fp = @fsocketopen(($scheme == 'https' ? 'ssl' : $scheme).'://'.($scheme == 'https' ? $host : ($ip ? $ip : $host)), $port, $errno, $errstr, $timeout)) {
  81. $context = array(
  82. 'http' => array(
  83. 'method' => $post ? 'POST' : 'GET',
  84. 'header' => $header,
  85. 'content' => $post,
  86. 'timeout' => $timeout,
  87. ),
  88. );
  89. $context = stream_context_create($context);
  90. $fp = @fopen($scheme.'://'.($scheme == 'https' ? $host : ($ip ? $ip : $host)).':'.$port.$path, 'b', false, $context);
  91. $fpflag = 1;
  92. }
  93. if(!$fp) {
  94. return '';
  95. } else {
  96. stream_set_blocking($fp, $block);
  97. stream_set_timeout($fp, $timeout);
  98. @fwrite($fp, $out);
  99. $status = stream_get_meta_data($fp);
  100. if(!$status['timed_out']) {
  101. while (!feof($fp) && !$fpflag) {
  102. if(($header = @fgets($fp)) && ($header == "\r\n" || $header == "\n")) {
  103. break;
  104. }
  105. }
  106. $stop = false;
  107. while(!feof($fp) && !$stop) {
  108. $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));
  109. $return .= $data;
  110. if($limit) {
  111. $limit -= strlen($data);
  112. $stop = $limit <= 0;
  113. }
  114. }
  115. }
  116. @fclose($fp);
  117. return $return;
  118. }
  119. }
  120. function array2string($arr) {
  121. $s = $sep = '';
  122. if($arr && is_array($arr)) {
  123. foreach($arr as $k => $v) {
  124. $s .= $sep.addslashes($k).UC_ARRAY_SEP_1.$v;
  125. $sep = UC_ARRAY_SEP_2;
  126. }
  127. }
  128. return $s;
  129. }
  130. function string2array($s) {
  131. $arr = explode(UC_ARRAY_SEP_2, $s);
  132. $arr2 = array();
  133. foreach($arr as $k => $v) {
  134. list($key, $val) = explode(UC_ARRAY_SEP_1, $v);
  135. $arr2[$key] = $val;
  136. }
  137. return $arr2;
  138. }
  139. }
  140. ?>