communication.func.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. function ihttp_request($url, $post = '', $extra = array(), $timeout = 60) {
  8. if (function_exists('curl_init') && function_exists('curl_exec') && $timeout > 0) {
  9. $ch = ihttp_build_curl($url, $post, $extra, $timeout);
  10. if (is_error($ch)) {
  11. return $ch;
  12. }
  13. $data = curl_exec($ch);
  14. $status = curl_getinfo($ch);
  15. $errno = curl_errno($ch);
  16. $error = curl_error($ch);
  17. curl_close($ch);
  18. if ($errno || empty($data)) {
  19. return error($errno, $error);
  20. } else {
  21. return ihttp_response_parse($data);
  22. }
  23. }
  24. $urlset = ihttp_parse_url($url, true);
  25. if (!empty($urlset['ip'])) {
  26. $urlset['host'] = $urlset['ip'];
  27. }
  28. $body = ihttp_build_httpbody($url, $post, $extra);
  29. if ($urlset['scheme'] == 'https') {
  30. $fp = ihttp_socketopen('ssl://' . $urlset['host'], $urlset['port'], $errno, $error);
  31. } else {
  32. $fp = ihttp_socketopen($urlset['host'], $urlset['port'], $errno, $error);
  33. }
  34. stream_set_blocking($fp, $timeout > 0 ? true : false);
  35. stream_set_timeout($fp, ini_get('default_socket_timeout'));
  36. if (!$fp) {
  37. return error(1, $error);
  38. } else {
  39. fwrite($fp, $body);
  40. $content = '';
  41. if($timeout > 0) {
  42. while (!feof($fp)) {
  43. $content .= fgets($fp, 512);
  44. }
  45. }
  46. fclose($fp);
  47. return ihttp_response_parse($content, true);
  48. }
  49. }
  50. function ihttp_get($url) {
  51. return ihttp_request($url);
  52. }
  53. function ihttp_post($url, $data) {
  54. $headers = array('Content-Type' => 'application/x-www-form-urlencoded');
  55. return ihttp_request($url, $data, $headers);
  56. }
  57. function ihttp_multi_request($urls, $posts = array(), $extra = array(), $timeout = 60) {
  58. if (!is_array($urls)) {
  59. return error(1, '请使用ihttp_request函数');
  60. }
  61. $curl_multi = curl_multi_init();
  62. $curl_client = $response = array();
  63. foreach ($urls as $i => $url) {
  64. if (isset($posts[$i]) && is_array($posts[$i])) {
  65. $post = $posts[$i];
  66. } else {
  67. $post = $posts;
  68. }
  69. if (!empty($url)) {
  70. $curl = ihttp_build_curl($url, $post, $extra, $timeout);
  71. if (is_error($curl)) {
  72. continue;
  73. }
  74. if (curl_multi_add_handle($curl_multi, $curl) === CURLM_OK) {
  75. $curl_client[] = $curl;
  76. }
  77. }
  78. }
  79. if (!empty($curl_client)) {
  80. $active = null;
  81. do {
  82. $mrc = curl_multi_exec($curl_multi, $active);
  83. } while ($mrc == CURLM_CALL_MULTI_PERFORM);
  84. while ($active && $mrc == CURLM_OK) {
  85. if (curl_multi_select($curl_multi) != -1) {
  86. do {
  87. $mrc = curl_multi_exec($curl_multi, $active);
  88. } while ($mrc == CURLM_CALL_MULTI_PERFORM);
  89. } else {
  90. return error(2, '请求失败,请检查URL');
  91. }
  92. }
  93. }
  94. foreach ($curl_client as $i => $curl) {
  95. $response[$i] = curl_multi_getcontent($curl);
  96. curl_multi_remove_handle($curl_multi, $curl);
  97. }
  98. curl_multi_close($curl_multi);
  99. return $response;
  100. }
  101. function ihttp_socketopen($hostname, $port = 80, &$errno, &$errstr, $timeout = 15) {
  102. $fp = '';
  103. if(function_exists('fsockopen')) {
  104. $fp = @fsockopen($hostname, $port, $errno, $errstr, $timeout);
  105. } elseif(function_exists('pfsockopen')) {
  106. $fp = @pfsockopen($hostname, $port, $errno, $errstr, $timeout);
  107. } elseif(function_exists('stream_socket_client')) {
  108. $fp = @stream_socket_client($hostname.':'.$port, $errno, $errstr, $timeout);
  109. }
  110. return $fp;
  111. }
  112. function ihttp_response_parse($data, $chunked = false) {
  113. $rlt = array();
  114. $headermeta = explode('HTTP/', $data);
  115. if (count($headermeta) > 2) {
  116. $data = 'HTTP/' . array_pop($headermeta);
  117. }
  118. $pos = strpos($data, "\r\n\r\n");
  119. $split1[0] = substr($data, 0, $pos);
  120. $split1[1] = substr($data, $pos + 4, strlen($data));
  121. $split2 = explode("\r\n", $split1[0], 2);
  122. preg_match('/^(\S+) (\S+) (.*)$/', $split2[0], $matches);
  123. $rlt['code'] = $matches[2];
  124. $rlt['status'] = $matches[3];
  125. $rlt['responseline'] = $split2[0];
  126. $header = explode("\r\n", $split2[1]);
  127. $isgzip = false;
  128. $ischunk = false;
  129. foreach ($header as $v) {
  130. $pos = strpos($v, ':');
  131. $key = substr($v, 0, $pos);
  132. $value = trim(substr($v, $pos + 1));
  133. if (is_array($rlt['headers'][$key])) {
  134. $rlt['headers'][$key][] = $value;
  135. } elseif (!empty($rlt['headers'][$key])) {
  136. $temp = $rlt['headers'][$key];
  137. unset($rlt['headers'][$key]);
  138. $rlt['headers'][$key][] = $temp;
  139. $rlt['headers'][$key][] = $value;
  140. } else {
  141. $rlt['headers'][$key] = $value;
  142. }
  143. if(!$isgzip && strtolower($key) == 'content-encoding' && strtolower($value) == 'gzip') {
  144. $isgzip = true;
  145. }
  146. if(!$ischunk && strtolower($key) == 'transfer-encoding' && strtolower($value) == 'chunked') {
  147. $ischunk = true;
  148. }
  149. }
  150. if($chunked && $ischunk) {
  151. $rlt['content'] = ihttp_response_parse_unchunk($split1[1]);
  152. } else {
  153. $rlt['content'] = $split1[1];
  154. }
  155. if($isgzip && function_exists('gzdecode')) {
  156. $rlt['content'] = gzdecode($rlt['content']);
  157. }
  158. $rlt['meta'] = $data;
  159. if($rlt['code'] == '100') {
  160. return ihttp_response_parse($rlt['content']);
  161. }
  162. return $rlt;
  163. }
  164. function ihttp_response_parse_unchunk($str = null) {
  165. if(!is_string($str) or strlen($str) < 1) {
  166. return false;
  167. }
  168. $eol = "\r\n";
  169. $add = strlen($eol);
  170. $tmp = $str;
  171. $str = '';
  172. do {
  173. $tmp = ltrim($tmp);
  174. $pos = strpos($tmp, $eol);
  175. if($pos === false) {
  176. return false;
  177. }
  178. $len = hexdec(substr($tmp, 0, $pos));
  179. if(!is_numeric($len) or $len < 0) {
  180. return false;
  181. }
  182. $str .= substr($tmp, ($pos + $add), $len);
  183. $tmp = substr($tmp, ($len + $pos + $add));
  184. $check = trim($tmp);
  185. } while(!empty($check));
  186. unset($tmp);
  187. return $str;
  188. }
  189. function ihttp_parse_url($url, $set_default_port = false) {
  190. if (empty($url)) {
  191. return error(1);
  192. }
  193. $urlset = parse_url($url);
  194. if (!empty($urlset['scheme']) && !in_array($urlset['scheme'], array('http', 'https'))) {
  195. return error(1, '只能使用 http 及 https 协议');
  196. }
  197. if (empty($urlset['path'])) {
  198. $urlset['path'] = '/';
  199. }
  200. if (!empty($urlset['query'])) {
  201. $urlset['query'] = "?{$urlset['query']}";
  202. }
  203. if (strexists($url, 'https://') && !extension_loaded('openssl')) {
  204. if (!extension_loaded("openssl")) {
  205. return error(1,'请开启您PHP环境的openssl', '');
  206. }
  207. }
  208. if (empty($urlset['host'])) {
  209. $current_url = parse_url($GLOBALS['_W']['siteroot']);
  210. $urlset['host'] = $current_url['host'];
  211. $urlset['scheme'] = $current_url['scheme'];
  212. $urlset['path'] = $current_url['path'] . 'web/' . str_replace('./', '', $urlset['path']);
  213. $urlset['ip'] = '127.0.0.1';
  214. } else if (! ihttp_allow_host($urlset['host'])){
  215. return error(1, 'host 非法');
  216. }
  217. if ($set_default_port && empty($urlset['port'])) {
  218. $urlset['port'] = $urlset['scheme'] == 'https' ? '443' : '80';
  219. }
  220. return $urlset;
  221. }
  222. function ihttp_allow_host($host) {
  223. global $_W;
  224. if (strexists($host, '@')) {
  225. return false;
  226. }
  227. $pattern = "/^(10|172|192|127)/";
  228. if (preg_match($pattern, $host) && isset($_W['setting']['ip_white_list'])) {
  229. $ip_white_list = $_W['setting']['ip_white_list'];
  230. if ($ip_white_list && isset($ip_white_list[$host]) && !$ip_white_list[$host]['status']) {
  231. return false;
  232. }
  233. }
  234. return true;
  235. }
  236. function ihttp_build_curl($url, $post, $extra, $timeout) {
  237. if (!function_exists('curl_init') || !function_exists('curl_exec')) {
  238. return error(1, 'curl扩展未开启');
  239. }
  240. $urlset = ihttp_parse_url($url);
  241. if (is_error($urlset)) {
  242. return $urlset;
  243. }
  244. if (!empty($urlset['ip'])) {
  245. $extra['ip'] = $urlset['ip'];
  246. }
  247. $ch = curl_init();
  248. if (!empty($extra['ip'])) {
  249. $extra['Host'] = $urlset['host'];
  250. $urlset['host'] = $extra['ip'];
  251. unset($extra['ip']);
  252. }
  253. curl_setopt($ch, CURLOPT_URL, $urlset['scheme'] . '://' . $urlset['host'] . ($urlset['port'] == '80' || empty($urlset['port']) ? '' : ':' . $urlset['port']) . $urlset['path'] . $urlset['query']);
  254. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  255. @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  256. curl_setopt($ch, CURLOPT_HEADER, 1);
  257. @curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  258. if ($post) {
  259. if (is_array($post)) {
  260. $filepost = false;
  261. foreach ($post as $name => &$value) {
  262. if (version_compare(phpversion(), '5.5') >= 0 && is_string($value) && substr($value, 0, 1) == '@') {
  263. $post[$name] = new CURLFile(ltrim($value, '@'));
  264. }
  265. if ((is_string($value) && substr($value, 0, 1) == '@') || (class_exists('CURLFile') && $value instanceof CURLFile)) {
  266. $filepost = true;
  267. }
  268. }
  269. if (!$filepost) {
  270. $post = http_build_query($post);
  271. }
  272. }
  273. curl_setopt($ch, CURLOPT_POST, 1);
  274. curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  275. }
  276. if (!empty($GLOBALS['_W']['config']['setting']['proxy'])) {
  277. $urls = parse_url($GLOBALS['_W']['config']['setting']['proxy']['host']);
  278. if (!empty($urls['host'])) {
  279. curl_setopt($ch, CURLOPT_PROXY, "{$urls['host']}:{$urls['port']}");
  280. $proxytype = 'CURLPROXY_' . strtoupper($urls['scheme']);
  281. if (!empty($urls['scheme']) && defined($proxytype)) {
  282. curl_setopt($ch, CURLOPT_PROXYTYPE, constant($proxytype));
  283. } else {
  284. curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  285. curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
  286. }
  287. if (!empty($GLOBALS['_W']['config']['setting']['proxy']['auth'])) {
  288. curl_setopt($ch, CURLOPT_PROXYUSERPWD, $GLOBALS['_W']['config']['setting']['proxy']['auth']);
  289. }
  290. }
  291. }
  292. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  293. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  294. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  295. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  296. curl_setopt($ch, CURLOPT_SSLVERSION, 1);
  297. if (defined('CURL_SSLVERSION_TLSv1')) {
  298. curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
  299. }
  300. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1');
  301. if (!empty($extra) && is_array($extra)) {
  302. $headers = array();
  303. foreach ($extra as $opt => $value) {
  304. if (strexists($opt, 'CURLOPT_')) {
  305. curl_setopt($ch, constant($opt), $value);
  306. } elseif (is_numeric($opt)) {
  307. curl_setopt($ch, $opt, $value);
  308. } else {
  309. $headers[] = "{$opt}: {$value}";
  310. }
  311. }
  312. if (!empty($headers)) {
  313. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  314. }
  315. }
  316. return $ch;
  317. }
  318. function ihttp_build_httpbody($url, $post, $extra) {
  319. $urlset = ihttp_parse_url($url, true);
  320. if (is_error($urlset)) {
  321. return $urlset;
  322. }
  323. if (!empty($urlset['ip'])) {
  324. $extra['ip'] = $urlset['ip'];
  325. }
  326. $body = '';
  327. if (!empty($post) && is_array($post)) {
  328. $filepost = false;
  329. $boundary = random(40);
  330. foreach ($post as $name => &$value) {
  331. if ((is_string($value) && substr($value, 0, 1) == '@') && file_exists(ltrim($value, '@'))) {
  332. $filepost = true;
  333. $file = ltrim($value, '@');
  334. $body .= "--$boundary\r\n";
  335. $body .= 'Content-Disposition: form-data; name="'.$name.'"; filename="'.basename($file).'"; Content-Type: application/octet-stream'."\r\n\r\n";
  336. $body .= file_get_contents($file)."\r\n";
  337. } else {
  338. $body .= "--$boundary\r\n";
  339. $body .= 'Content-Disposition: form-data; name="'.$name.'"'."\r\n\r\n";
  340. $body .= $value."\r\n";
  341. }
  342. }
  343. if (!$filepost) {
  344. $body = http_build_query($post, '', '&');
  345. } else {
  346. $body .= "--$boundary\r\n";
  347. }
  348. }
  349. $method = empty($post) ? 'GET' : 'POST';
  350. $fdata = "{$method} {$urlset['path']}{$urlset['query']} HTTP/1.1\r\n";
  351. $fdata .= "Accept: */*\r\n";
  352. $fdata .= "Accept-Language: zh-cn\r\n";
  353. if ($method == 'POST') {
  354. $fdata .= empty($filepost) ? "Content-Type: application/x-www-form-urlencoded\r\n" : "Content-Type: multipart/form-data; boundary=$boundary\r\n";
  355. }
  356. $fdata .= "Host: {$urlset['host']}\r\n";
  357. $fdata .= "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1\r\n";
  358. if (function_exists('gzdecode')) {
  359. $fdata .= "Accept-Encoding: gzip, deflate\r\n";
  360. }
  361. $fdata .= "Connection: close\r\n";
  362. if (!empty($extra) && is_array($extra)) {
  363. foreach ($extra as $opt => $value) {
  364. if (!strexists($opt, 'CURLOPT_')) {
  365. $fdata .= "{$opt}: {$value}\r\n";
  366. }
  367. }
  368. }
  369. if ($body) {
  370. $fdata .= 'Content-Length: ' . strlen($body) . "\r\n\r\n{$body}";
  371. } else {
  372. $fdata .= "\r\n";
  373. }
  374. return $fdata;
  375. }
  376. function ihttp_email($to, $subject, $body, $global = false) {
  377. static $mailer;
  378. set_time_limit(0);
  379. if (empty($mailer)) {
  380. if (!class_exists('PHPMailer')) {
  381. load()->library('phpmailer');
  382. }
  383. $mailer = new PHPMailer();
  384. global $_W;
  385. $config = $GLOBALS['_W']['setting']['mail'];
  386. if (!$global) {
  387. $row = pdo_get("uni_settings", array('uniacid' => $_W['uniacid']), array('notify'));
  388. $row['notify'] = @iunserializer($row['notify']);
  389. if (!empty($row['notify']) && !empty($row['notify']['mail'])) {
  390. $config = $row['notify']['mail'];
  391. }
  392. }
  393. $config['charset'] = 'utf-8';
  394. if ($config['smtp']['type'] == '163') {
  395. $config['smtp']['server'] = 'smtp.163.com';
  396. $config['smtp']['port'] = 25;
  397. } elseif ($config['smtp']['type'] == 'qq') {
  398. $config['smtp']['server'] = 'ssl://smtp.qq.com';
  399. $config['smtp']['port'] = 465;
  400. } else {
  401. if (!empty($config['smtp']['authmode'])) {
  402. $config['smtp']['server'] = 'ssl://' . $config['smtp']['server'];
  403. }
  404. }
  405. if (!empty($config['smtp']['authmode'])) {
  406. if (!extension_loaded('openssl')) {
  407. return error(1, '请开启 php_openssl 扩展!');
  408. }
  409. }
  410. $mailer->signature = $config['signature'];
  411. $mailer->isSMTP();
  412. $mailer->CharSet = $config['charset'];
  413. $mailer->Host = $config['smtp']['server'];
  414. $mailer->Port = $config['smtp']['port'];
  415. $mailer->SMTPAuth = true;
  416. $mailer->Username = $config['username'];
  417. $mailer->Password = $config['password'];
  418. !empty($config['smtp']['authmode']) && $mailer->SMTPSecure = 'ssl';
  419. $mailer->From = $config['username'];
  420. $mailer->FromName = $config['sender'];
  421. $mailer->isHTML(true);
  422. }
  423. if ($body) {
  424. if (is_array($body)) {
  425. $body = '';
  426. foreach($body as $value) {
  427. if (substr($value, 0, 1) == '@') {
  428. if(!is_file($file = ltrim($value, '@'))){
  429. return error(1, $file . ' 附件不存在或非文件!');
  430. }
  431. $mailer->addAttachment($file);
  432. } else {
  433. $body .= $value . '\n';
  434. }
  435. }
  436. } else {
  437. if (substr($body, 0, 1) == '@') {
  438. $mailer->addAttachment(ltrim($body, '@'));
  439. $body = '';
  440. }
  441. }
  442. }
  443. if (!empty($mailer->signature)) {
  444. $body .= htmlspecialchars_decode($mailer->signature);
  445. }
  446. $mailer->Subject = $subject;
  447. $mailer->Body = $body;
  448. $mailer->addAddress($to);
  449. if ($mailer->send()) {
  450. return true;
  451. } else {
  452. return error(1, $mailer->ErrorInfo);
  453. }
  454. }