人人商城

communication.func.php 14KB

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