public static function newCronThread() { global $siteurl, $real_siteurl; $url = $real_siteurl ? $real_siteurl : $siteurl; $matches = parse_url($url); $host = $matches['host']; $port = !empty($matches['port']) ? $matches['port'] : 80; $path = $matches['path'] ? $matches['path'] : '/'; $header = "GET {$path}cron.php HTTP/1.0\r\n"; $header .= "Accept: */*\r\n"; $header .= "Host: {$host}:{$port}\r\n"; $header .= "Connection: Close\r\n\r\n"; $fp = fsocketopen($host, $port); if (!$fp) { return false; } stream_set_timeout($fp, 1); @fwrite($fp, $header); @fgets($fp); fclose($fp); return true; }
function dfopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE, $ip = '', $timeout = 15, $block = TRUE) { $return = ''; $matches = parse_url($url); $host = $matches['host']; $path = $matches['path'] ? $matches['path'] . (isset($matches['query']) && $matches['query'] ? '?' . $matches['query'] : '') : '/'; $port = !empty($matches['port']) ? $matches['port'] : 80; if ($post) { $out = "POST {$path} HTTP/1.0\r\n"; $header = "Accept: */*\r\n"; $header .= "Accept-Language: zh-cn\r\n"; $header .= "Content-Type: application/x-www-form-urlencoded\r\n"; $header .= "User-Agent: {$_SERVER['HTTP_USER_AGENT']}\r\n"; $header .= "Host: {$host}\r\n"; $header .= 'Content-Length: ' . strlen($post) . "\r\n"; $header .= "Connection: Close\r\n"; $header .= "Cache-Control: no-cache\r\n"; $header .= "Cookie: {$cookie}\r\n\r\n"; $out .= $header . $post; } else { $out = "GET {$path} HTTP/1.0\r\n"; $header = "Accept: */*\r\n"; $header .= "Accept-Language: zh-cn\r\n"; $header .= "User-Agent: {$_SERVER['HTTP_USER_AGENT']}\r\n"; $header .= "Host: {$host}\r\n"; $header .= "Connection: Close\r\n"; $header .= "Cookie: {$cookie}\r\n\r\n"; $out .= $header; } $fpflag = 0; if (!($fp = @fsocketopen($ip ? $ip : $host, $port, $errno, $errstr, $timeout))) { $context = array('http' => array('method' => $post ? 'POST' : 'GET', 'header' => $header, 'content' => $post)); $context = stream_context_create($context); $fp = @fopen($url, 'b', false, $context); $fpflag = 1; } if (!$fp) { return ''; } else { stream_set_blocking($fp, $block); stream_set_timeout($fp, $timeout); @fwrite($fp, $out); $status = stream_get_meta_data($fp); if (!$status['timed_out']) { while (!feof($fp) && !$fpflag) { if (($header = @fgets($fp)) && ($header == "\r\n" || $header == "\n")) { break; } } $stop = false; while (!feof($fp) && !$stop) { $data = fread($fp, $limit == 0 || $limit > 8192 ? 8192 : $limit); $return .= $data; if ($limit) { $limit -= strlen($data); $stop = $limit <= 0; } } } @fclose($fp); return $return; } }
function sendmail($toemail, $subject, $message, $from = '') { global $_G; if (!is_array($_G['setting']['mail'])) { $_G['setting']['mail'] = dunserialize($_G['setting']['mail']); } $_G['setting']['mail']['server'] = $_G['setting']['mail']['port'] = $_G['setting']['mail']['auth'] = $_G['setting']['mail']['from'] = $_G['setting']['mail']['auth_username'] = $_G['setting']['mail']['auth_password'] = ''; if ($_G['setting']['mail']['mailsend'] != 1) { $smtpnum = count($_G['setting']['mail']['smtp']); if ($smtpnum) { $rid = rand(0, $smtpnum - 1); $smtp = $_G['setting']['mail']['smtp'][$rid]; $_G['setting']['mail']['server'] = $smtp['server']; $_G['setting']['mail']['port'] = $smtp['port']; $_G['setting']['mail']['auth'] = $smtp['auth'] ? 1 : 0; $_G['setting']['mail']['from'] = $smtp['from']; $_G['setting']['mail']['auth_username'] = $smtp['auth_username']; $_G['setting']['mail']['auth_password'] = $smtp['auth_password']; } } $message = preg_replace("/href\\=\"(?!(http|https)\\:\\/\\/)(.+?)\"/i", 'href="' . $_G['siteurl'] . '\\2"', $message); $message = <<<EOT <html> <head> <meta http-equiv="Content-Type" content="text/html; charset={$_G['charset']}"> <title>{$subject}</title> </head> <body> {$subject}<br /> {$message} </body> </html> EOT; $maildelimiter = $_G['setting']['mail']['maildelimiter'] == 1 ? "\r\n" : ($_G['setting']['mail']['maildelimiter'] == 2 ? "\r" : "\n"); $mailusername = isset($_G['setting']['mail']['mailusername']) ? $_G['setting']['mail']['mailusername'] : 1; $_G['setting']['mail']['port'] = $_G['setting']['mail']['port'] ? $_G['setting']['mail']['port'] : 25; $_G['setting']['mail']['mailsend'] = $_G['setting']['mail']['mailsend'] ? $_G['setting']['mail']['mailsend'] : 1; if ($_G['setting']['mail']['mailsend'] == 3) { $email_from = empty($from) ? $_G['setting']['adminemail'] : $from; } else { $email_from = $from == '' ? '=?' . CHARSET . '?B?' . base64_encode($_G['setting']['sitename']) . "?= <" . $_G['setting']['adminemail'] . ">" : (preg_match('/^(.+?) \\<(.+?)\\>$/', $from, $mats) ? '=?' . CHARSET . '?B?' . base64_encode($mats[1]) . "?= <{$mats['2']}>" : $from); } $email_to = preg_match('/^(.+?) \\<(.+?)\\>$/', $toemail, $mats) ? $mailusername ? '=?' . CHARSET . '?B?' . base64_encode($mats[1]) . "?= <{$mats['2']}>" : $mats[2] : $toemail; $email_subject = '=?' . CHARSET . '?B?' . base64_encode(preg_replace("/[\r|\n]/", '', '[' . $_G['setting']['sitename'] . '] ' . $subject)) . '?='; $email_message = chunk_split(base64_encode(str_replace("\n", "\r\n", str_replace("\r", "\n", str_replace("\r\n", "\n", str_replace("\n\r", "\r", $message)))))); $host = $_SERVER['HTTP_HOST']; $version = $_G['setting']['version']; $headers = "From: {$email_from}{$maildelimiter}X-Priority: 3{$maildelimiter}X-Mailer: {$host} {$version} {$maildelimiter}MIME-Version: 1.0{$maildelimiter}Content-type: text/html; charset=" . CHARSET . "{$maildelimiter}Content-Transfer-Encoding: base64{$maildelimiter}"; if ($_G['setting']['mail']['mailsend'] == 1) { if (function_exists('mail') && @mail($email_to, $email_subject, $email_message, $headers)) { return true; } return false; } elseif ($_G['setting']['mail']['mailsend'] == 2) { if (!($fp = fsocketopen($_G['setting']['mail']['server'], $_G['setting']['mail']['port'], $errno, $errstr, 30))) { runlog('SMTP', "({$_G[setting][mail][server]}:{$_G[setting][mail][port]}) CONNECT - Unable to connect to the SMTP server", 0); return false; } stream_set_blocking($fp, true); $lastmessage = fgets($fp, 512); if (substr($lastmessage, 0, 3) != '220') { runlog('SMTP', "{$_G[setting][mail][server]}:{$_G[setting][mail][port]} CONNECT - {$lastmessage}", 0); return false; } fputs($fp, ($_G['setting']['mail']['auth'] ? 'EHLO' : 'HELO') . " uchome\r\n"); $lastmessage = fgets($fp, 512); if (substr($lastmessage, 0, 3) != 220 && substr($lastmessage, 0, 3) != 250) { runlog('SMTP', "({$_G[setting][mail][server]}:{$_G[setting][mail][port]}) HELO/EHLO - {$lastmessage}", 0); return false; } while (1) { if (substr($lastmessage, 3, 1) != '-' || empty($lastmessage)) { break; } $lastmessage = fgets($fp, 512); } if ($_G['setting']['mail']['auth']) { fputs($fp, "AUTH LOGIN\r\n"); $lastmessage = fgets($fp, 512); if (substr($lastmessage, 0, 3) != 334) { runlog('SMTP', "({$_G[setting][mail][server]}:{$_G[setting][mail][port]}) AUTH LOGIN - {$lastmessage}", 0); return false; } fputs($fp, base64_encode($_G['setting']['mail']['auth_username']) . "\r\n"); $lastmessage = fgets($fp, 512); if (substr($lastmessage, 0, 3) != 334) { runlog('SMTP', "({$_G[setting][mail][server]}:{$_G[setting][mail][port]}) USERNAME - {$lastmessage}", 0); return false; } fputs($fp, base64_encode($_G['setting']['mail']['auth_password']) . "\r\n"); $lastmessage = fgets($fp, 512); if (substr($lastmessage, 0, 3) != 235) { runlog('SMTP', "({$_G[setting][mail][server]}:{$_G[setting][mail][port]}) PASSWORD - {$lastmessage}", 0); return false; } $email_from = $_G['setting']['mail']['from']; } fputs($fp, "MAIL FROM: <" . preg_replace("/.*\\<(.+?)\\>.*/", "\\1", $email_from) . ">\r\n"); $lastmessage = fgets($fp, 512); if (substr($lastmessage, 0, 3) != 250) { fputs($fp, "MAIL FROM: <" . preg_replace("/.*\\<(.+?)\\>.*/", "\\1", $email_from) . ">\r\n"); $lastmessage = fgets($fp, 512); if (substr($lastmessage, 0, 3) != 250) { runlog('SMTP', "({$_G[setting][mail][server]}:{$_G[setting][mail][port]}) MAIL FROM - {$lastmessage}", 0); return false; } } fputs($fp, "RCPT TO: <" . preg_replace("/.*\\<(.+?)\\>.*/", "\\1", $toemail) . ">\r\n"); $lastmessage = fgets($fp, 512); if (substr($lastmessage, 0, 3) != 250) { fputs($fp, "RCPT TO: <" . preg_replace("/.*\\<(.+?)\\>.*/", "\\1", $toemail) . ">\r\n"); $lastmessage = fgets($fp, 512); runlog('SMTP', "({$_G[setting][mail][server]}:{$_G[setting][mail][port]}) RCPT TO - {$lastmessage}", 0); return false; } fputs($fp, "DATA\r\n"); $lastmessage = fgets($fp, 512); if (substr($lastmessage, 0, 3) != 354) { runlog('SMTP', "({$_G[setting][mail][server]}:{$_G[setting][mail][port]}) DATA - {$lastmessage}", 0); return false; } $timeoffset = $_G['setting']['timeoffset']; if (function_exists('date_default_timezone_set')) { @date_default_timezone_set('Etc/GMT' . ($timeoffset > 0 ? '-' : '+') . abs($timeoffset)); } $headers .= 'Message-ID: <' . date('YmdHs') . '.' . substr(md5($email_message . microtime()), 0, 6) . rand(100000, 999999) . '@' . $_SERVER['HTTP_HOST'] . ">{$maildelimiter}"; fputs($fp, "Date: " . date('r') . "\r\n"); fputs($fp, "To: " . $email_to . "\r\n"); fputs($fp, "Subject: " . $email_subject . "\r\n"); fputs($fp, $headers . "\r\n"); fputs($fp, "\r\n\r\n"); fputs($fp, "{$email_message}\r\n.\r\n"); $lastmessage = fgets($fp, 512); if (substr($lastmessage, 0, 3) != 250) { runlog('SMTP', "({$_G[setting][mail][server]}:{$_G[setting][mail][port]}) END - {$lastmessage}", 0); } fputs($fp, "QUIT\r\n"); return true; } elseif ($_G['setting']['mail']['mailsend'] == 3) { ini_set('SMTP', $_G['setting']['mail']['server']); ini_set('smtp_port', $_G['setting']['mail']['port']); ini_set('sendmail_from', $email_from); if (function_exists('mail') && @mail($email_to, $email_subject, $email_message, $headers)) { return true; } return false; } }
function _mobcent_dfsockopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE, $ip = '', $timeout = 15, $block = TRUE, $encodetype = 'URLENCODE', $allowcurl = TRUE, $position = 0, $files = array(), $userAgent = '') { $return = ''; $matches = parse_url($url); $scheme = $matches['scheme']; $host = $matches['host']; $path = $matches['path'] ? $matches['path'] . ($matches['query'] ? '?' . $matches['query'] : '') : '/'; $port = !empty($matches['port']) ? $matches['port'] : ($scheme == 'http' ? '80' : ''); $boundary = $encodetype == 'URLENCODE' ? '' : random(40); $userAgent == '' && ($userAgent = $_SERVER['HTTP_USER_AGENT']); if ($post) { if (!is_array($post)) { parse_str($post, $post); } _mobcent_format_postkey($post, $postnew); $post = $postnew; } if (function_exists('curl_init') && function_exists('curl_exec') && $allowcurl) { $ch = curl_init(); $httpheader = array(); if ($ip) { $httpheader[] = "Host: " . $host; } if ($httpheader) { curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader); } curl_setopt($ch, CURLOPT_URL, $scheme . '://' . ($ip ? $ip : $host) . ($port ? ':' . $port : '') . $path); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); if ($post) { curl_setopt($ch, CURLOPT_POST, 1); if ($encodetype == 'URLENCODE') { curl_setopt($ch, CURLOPT_POSTFIELDS, $post); } else { foreach ($post as $k => $v) { if (isset($files[$k])) { $post[$k] = '@' . $files[$k]; } } foreach ($files as $k => $file) { if (!isset($post[$k]) && file_exists($file)) { $post[$k] = '@' . $file; } } curl_setopt($ch, CURLOPT_POSTFIELDS, $post); } } if ($cookie) { curl_setopt($ch, CURLOPT_COOKIE, $cookie); } curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); $data = curl_exec($ch); $status = curl_getinfo($ch); $errno = curl_errno($ch); curl_close($ch); if ($errno || $status['http_code'] != 200) { return ''; } else { $GLOBALS['filesockheader'] = substr($data, 0, $status['header_size']); $data = substr($data, $status['header_size']); return !$limit ? $data : substr($data, 0, $limit); } } if ($post) { if ($encodetype == 'URLENCODE') { $data = http_build_query($post); } else { $data = ''; foreach ($post as $k => $v) { $data .= "--{$boundary}\r\n"; $data .= 'Content-Disposition: form-data; name="' . $k . '"' . (isset($files[$k]) ? '; filename="' . basename($files[$k]) . '"; Content-Type: application/octet-stream' : '') . "\r\n\r\n"; $data .= $v . "\r\n"; } foreach ($files as $k => $file) { if (!isset($post[$k]) && file_exists($file)) { if ($fp = @fopen($file, 'r')) { $v = fread($fp, filesize($file)); fclose($fp); $data .= "--{$boundary}\r\n"; $data .= 'Content-Disposition: form-data; name="' . $k . '"; filename="' . basename($file) . '"; Content-Type: application/octet-stream' . "\r\n\r\n"; $data .= $v . "\r\n"; } } } $data .= "--{$boundary}\r\n"; } $out = "POST {$path} HTTP/1.0\r\n"; $header = "Accept: */*\r\n"; $header .= "Accept-Language: zh-cn\r\n"; $header .= $encodetype == 'URLENCODE' ? "Content-Type: application/x-www-form-urlencoded\r\n" : "Content-Type: multipart/form-data; boundary={$boundary}\r\n"; $header .= 'Content-Length: ' . strlen($data) . "\r\n"; $header .= "User-Agent: {$userAgent}\r\n"; $header .= "Host: {$host}:{$port}\r\n"; $header .= "Connection: Close\r\n"; $header .= "Cache-Control: no-cache\r\n"; $header .= "Cookie: {$cookie}\r\n\r\n"; $out .= $header; $out .= $data; } else { $out = "GET {$path} HTTP/1.0\r\n"; $header = "Accept: */*\r\n"; $header .= "Accept-Language: zh-cn\r\n"; $header .= "User-Agent: {$userAgent}\r\n"; $header .= "Host: {$host}:{$port}\r\n"; $header .= "Connection: Close\r\n"; $header .= "Cookie: {$cookie}\r\n\r\n"; $out .= $header; } $fpflag = 0; if (!($fp = @fsocketopen($ip ? $ip : $host, $port, $errno, $errstr, $timeout))) { $context = array('http' => array('method' => $post ? 'POST' : 'GET', 'header' => $header, 'content' => $post, 'timeout' => $timeout)); $context = stream_context_create($context); $fp = @fopen($scheme . '://' . ($ip ? $ip : $host) . ':' . $port . $path, 'b', false, $context); $fpflag = 1; } if (!$fp) { return ''; } else { stream_set_blocking($fp, $block); stream_set_timeout($fp, $timeout); @fwrite($fp, $out); $status = stream_get_meta_data($fp); if (!$status['timed_out']) { while (!feof($fp) && !$fpflag) { $headers = ''; $header = @fgets($fp); $headers .= $header; if ($header && ($header == "\r\n" || $header == "\n")) { break; } } $GLOBALS['filesockheader'] = $headers; if ($position) { for ($i = 0; $i < $position; $i++) { $char = fgetc($fp); if ($char == "\n" && $oldchar != "\r") { $i++; } $oldchar = $char; } } if ($limit) { $return = stream_get_contents($fp, $limit); } else { $return = stream_get_contents($fp); } } @fclose($fp); return $return; } }
function _dfsockopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE, $ip = '', $timeout = 15, $block = TRUE, $encodetype = 'URLENCODE', $allowcurl = TRUE) { $return = ''; $matches = parse_url($url); $scheme = $matches['scheme']; $host = $matches['host']; $path = $matches['path'] ? $matches['path'] . ($matches['query'] ? '?' . $matches['query'] : '') : '/'; $port = !empty($matches['port']) ? $matches['port'] : 80; if (function_exists('curl_init') && function_exists('curl_exec') && $allowcurl) { $ch = curl_init(); $ip && curl_setopt($ch, CURLOPT_HTTPHEADER, array("Host: " . $host)); curl_setopt($ch, CURLOPT_URL, $scheme . '://' . ($ip ? $ip : $host) . ':' . $port . $path); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); if ($post) { curl_setopt($ch, CURLOPT_POST, 1); if ($encodetype == 'URLENCODE') { curl_setopt($ch, CURLOPT_POSTFIELDS, $post); } else { parse_str($post, $postarray); curl_setopt($ch, CURLOPT_POSTFIELDS, $postarray); } } if ($cookie) { curl_setopt($ch, CURLOPT_COOKIE, $cookie); } curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); $data = curl_exec($ch); $status = curl_getinfo($ch); $errno = curl_errno($ch); curl_close($ch); if ($errno || $status['http_code'] != 200) { return; } else { return !$limit ? $data : substr($data, 0, $limit); } } if ($post) { $out = "POST {$path} HTTP/1.0\r\n"; $header = "Accept: */*\r\n"; $header .= "Accept-Language: zh-cn\r\n"; $boundary = $encodetype == 'URLENCODE' ? '' : '; boundary=' . trim(substr(trim($post), 2, strpos(trim($post), "\n") - 2)); $header .= $encodetype == 'URLENCODE' ? "Content-Type: application/x-www-form-urlencoded\r\n" : "Content-Type: multipart/form-data{$boundary}\r\n"; $header .= "User-Agent: {$_SERVER['HTTP_USER_AGENT']}\r\n"; $header .= "Host: {$host}:{$port}\r\n"; $header .= 'Content-Length: ' . strlen($post) . "\r\n"; $header .= "Connection: Close\r\n"; $header .= "Cache-Control: no-cache\r\n"; $header .= "Cookie: {$cookie}\r\n\r\n"; $out .= $header . $post; } else { $out = "GET {$path} HTTP/1.0\r\n"; $header = "Accept: */*\r\n"; $header .= "Accept-Language: zh-cn\r\n"; $header .= "User-Agent: {$_SERVER['HTTP_USER_AGENT']}\r\n"; $header .= "Host: {$host}:{$port}\r\n"; $header .= "Connection: Close\r\n"; $header .= "Cookie: {$cookie}\r\n\r\n"; $out .= $header; } $fpflag = 0; if (!($fp = @fsocketopen($ip ? $ip : $host, $port, $errno, $errstr, $timeout))) { $context = array('http' => array('method' => $post ? 'POST' : 'GET', 'header' => $header, 'content' => $post, 'timeout' => $timeout)); $context = stream_context_create($context); $fp = @fopen($scheme . '://' . ($ip ? $ip : $host) . ':' . $port . $path, 'b', false, $context); $fpflag = 1; } if (!$fp) { return ''; } else { stream_set_blocking($fp, $block); stream_set_timeout($fp, $timeout); @fwrite($fp, $out); $status = stream_get_meta_data($fp); if (!$status['timed_out']) { while (!feof($fp) && !$fpflag) { if (($header = @fgets($fp)) && ($header == "\r\n" || $header == "\n")) { break; } } $stop = false; while (!feof($fp) && !$stop) { $data = fread($fp, $limit == 0 || $limit > 8192 ? 8192 : $limit); $return .= $data; if ($limit) { $limit -= strlen($data); $stop = $limit <= 0; } } } @fclose($fp); return $return; } }
function changyan_http_send($url, $limit = 0, $post = '', $cookie = '', $timeout = 15) { $return = ''; $matches = parse_url($url); $scheme = $matches['scheme']; $host = $matches['host']; $path = $matches['path'] ? $matches['path'] . (@$matches['query'] ? '?' . $matches['query'] : '') : '/'; $port = !empty($matches['port']) ? $matches['port'] : 80; if (function_exists('curl_init') && function_exists('curl_exec')) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $scheme . '://' . $host . ':' . $port . $path); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); if ($post) { curl_setopt($ch, CURLOPT_POST, 1); $content = is_array($post) ? http_build_query($post) : $post; curl_setopt($ch, CURLOPT_POSTFIELDS, urldecode($content)); } if ($cookie) { curl_setopt($ch, CURLOPT_COOKIE, $cookie); } curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_TIMEOUT, 900); $data = curl_exec($ch); $status = curl_getinfo($ch); $errno = curl_errno($ch); curl_close($ch); if ($errno || $status['http_code'] != 200) { return; } else { return !$limit ? $data : substr($data, 0, $limit); } } if ($post) { $content = is_array($post) ? urldecode(http_build_query($post)) : $post; $out = "POST {$path} HTTP/1.0\r\n"; $header = "Accept: */*\r\n"; $header .= "Accept-Language: zh-cn\r\n"; $header .= "Content-Type: application/x-www-form-urlencoded\r\n"; $header .= "User-Agent: " . @$_SERVER['HTTP_USER_AGENT'] . "\r\n"; $header .= "Host: {$host}:{$port}\r\n"; $header .= 'Content-Length: ' . strlen($content) . "\r\n"; $header .= "Connection: Close\r\n"; $header .= "Cache-Control: no-cache\r\n"; $header .= "Cookie: {$cookie}\r\n\r\n"; $out .= $header . $content; } else { $out = "GET {$path} HTTP/1.0\r\n"; $header = "Accept: */*\r\n"; $header .= "Accept-Language: zh-cn\r\n"; $header .= "User-Agent: " . @$_SERVER['HTTP_USER_AGENT'] . "\r\n"; $header .= "Host: {$host}:{$port}\r\n"; $header .= "Connection: Close\r\n"; $header .= "Cookie: {$cookie}\r\n\r\n"; $out .= $header; } $fpflag = 0; $fp = false; if (function_exists('fsocketopen')) { $fp = fsocketopen($host, $port, $errno, $errstr, $timeout); } if (!$fp) { $context = stream_context_create(array('http' => array('method' => $post ? 'POST' : 'GET', 'header' => $header, 'content' => $content, 'timeout' => $timeout))); $fp = @fopen($scheme . '://' . $host . ':' . $port . $path, 'b', false, $context); $fpflag = 1; } if (!$fp) { return ''; } else { stream_set_blocking($fp, true); stream_set_timeout($fp, $timeout); @fwrite($fp, $out); $status = stream_get_meta_data($fp); if (!$status['timed_out']) { while (!feof($fp) && !$fpflag) { if (($header = @fgets($fp)) && ($header == "\r\n" || $header == "\n")) { break; } } if ($limit) { $return = stream_get_contents($fp, $limit); } else { $return = stream_get_contents($fp); } } @fclose($fp); return $return; } }
function _Connect() { if ($this->_socket !== false) { if (!@feof($this->_socket)) { return $this->_socket; } $this->_socket = false; } $errno = 0; $errstr = ""; $this->_connerror = false; if ($this->_path) { $host = $this->_path; $port = 0; } else { $host = $this->_host; $port = $this->_port; } if ($this->_timeout <= 0) { $fp = fsocketopen($host, $port, $errno, $errstr); } else { $fp = fsocketopen($host, $port, $errno, $errstr, $this->_timeout); } if (!$fp) { if ($this->_path) { $location = $this->_path; } else { $location = "{$this->_host}:{$this->_port}"; } $errstr = trim($errstr); $this->_error = "connection to {$location} failed (errno={$errno}, msg={$errstr})"; $this->_connerror = true; return false; } if (!$this->_Send($fp, pack("N", 1), 4)) { fclose($fp); $this->_error = "failed to send client protocol version"; return false; } list(, $v) = unpack("N*", fread($fp, 4)); $v = (int) $v; if ($v < 1) { fclose($fp); $this->_error = "expected searchd protocol version 1+, got version '{$v}'"; return false; } return $fp; }
$maildelimiter = $mail_setting['maildelimiter'] == 1 ? "\r\n" : ($mail_setting['maildelimiter'] == 2 ? "\r" : "\n"); $mailusername = isset($mail_setting['mailusername']) ? $mail_setting['mailusername'] : 1; $appname = $this->base->cache['apps'][$mail['appid']]['name']; $mail['subject'] = '=?' . $mail['charset'] . '?B?' . base64_encode(str_replace("\r", '', str_replace("\n", '', '[' . $appname . '] ' . $mail['subject']))) . '?='; $mail['message'] = chunk_split(base64_encode(str_replace("\r\n.", " \r\n..", str_replace("\n", "\r\n", str_replace("\r", "\n", str_replace("\r\n", "\n", str_replace("\n\r", "\r", $mail['message']))))))); $email_from = $mail['frommail'] == '' ? '=?' . $mail['charset'] . '?B?' . base64_encode($appname) . "?= <{$mail_setting['maildefault']}>" : (preg_match('/^(.+?) \\<(.+?)\\>$/', $email_from, $from) ? '=?' . $mail['charset'] . '?B?' . base64_encode($from[1]) . "?= <{$from['2']}>" : $mail['frommail']); foreach (explode(',', $mail['email_to']) as $touser) { $tousers[] = preg_match('/^(.+?) \\<(.+?)\\>$/', $touser, $to) ? $mailusername ? '=?' . $mail['charset'] . '?B?' . base64_encode($to[1]) . "?= <{$to['2']}>" : $to[2] : $touser; } $mail['email_to'] = implode(',', $tousers); $headers = "From: {$email_from}{$maildelimiter}X-Priority: 3{$maildelimiter}X-Mailer: Discuz! {$version}{$maildelimiter}MIME-Version: 1.0{$maildelimiter}Content-type: text/" . ($mail['htmlon'] ? 'html' : 'plain') . "; charset={$mail['charset']}{$maildelimiter}Content-Transfer-Encoding: base64{$maildelimiter}"; $mail_setting['mailport'] = $mail_setting['mailport'] ? $mail_setting['mailport'] : 25; if ($mail_setting['mailsend'] == 1 && function_exists('mail')) { return @mail($mail['email_to'], $mail['subject'], $mail['message'], $headers); } elseif ($mail_setting['mailsend'] == 2) { if (!($fp = fsocketopen($mail_setting['mailserver'], $mail_setting['mailport'], $errno, $errstr, 30))) { return false; } stream_set_blocking($fp, true); $lastmessage = fgets($fp, 512); if (substr($lastmessage, 0, 3) != '220') { return false; } fputs($fp, ($mail_setting['mailauth'] ? 'EHLO' : 'HELO') . " discuz\r\n"); $lastmessage = fgets($fp, 512); if (substr($lastmessage, 0, 3) != 220 && substr($lastmessage, 0, 3) != 250) { return false; } while (1) { if (substr($lastmessage, 3, 1) != '-' || empty($lastmessage)) { break;
/** * 发送邮件函数 * @param [type] $toemail [description] * @param [type] $subject [description] * @param [type] $message [description] * @param string $from [description] * @return [type] [description] */ function sendmail($toemail, $subject, $message, $from = '') { define('CHARSET', 'utf-8'); $smtp = C('THINK_EMAIL'); $_G['server'] = $_G['port'] = $_G['auth'] = $_G['from'] = $_G['auth_username'] = $_G['auth_password'] = ''; $_G['server'] = $smtp['SMTP_HOST']; $_G['port'] = $smtp['SMTP_PORT']; $_G['auth'] = 1; $_G['from'] = $smtp['FROM_EMAIL']; $_G['auth_username'] = $smtp['SMTP_USER']; $_G['auth_password'] = $smtp['SMTP_PASS']; $message = preg_replace("/href\\=\"(?!(http|https)\\:\\/\\/)(.+?)\"/i", 'href="' . $_G['siteurl'] . '\\2"', $message); $message = <<<EOT <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=CHARSET"> <title>{$subject}</title> </head> <body> {$message} </body> </html> EOT; $maildelimiter = "\n"; $mailusername = 1; $_G['port'] = $_G['port'] ? $_G['port'] : 25; $email_from = $from == '' ? '=?' . CHARSET . '?B?' . base64_encode(C('SITENAME')) . "?= <" . $_G['from'] . ">" : (preg_match('/^(.+?) \\<(.+?)\\>$/', $from, $mats) ? '=?' . CHARSET . '?B?' . base64_encode($mats[1]) . "?= <{$mats['2']}>" : $from); $email_to = preg_match('/^(.+?) \\<(.+?)\\>$/', $toemail, $mats) ? $mailusername ? '=?' . CHARSET . '?B?' . base64_encode($mats[1]) . "?= <{$mats['2']}>" : $mats[2] : $toemail; $email_subject = '=?' . CHARSET . '?B?' . base64_encode(preg_replace("/[\r|\n]/", '', $subject)) . '?='; $email_message = chunk_split(base64_encode(str_replace("\n", "\r\n", str_replace("\r", "\n", str_replace("\r\n", "\n", str_replace("\n\r", "\r", $message)))))); $host = $_SERVER['HTTP_HOST']; $headers = "From: {$email_from}{$maildelimiter}X-Priority: 3{$maildelimiter}X-Mailer: {$host} {$maildelimiter}MIME-Version: 1.0{$maildelimiter}Content-type: text/html; charset=" . CHARSET . "{$maildelimiter}Content-Transfer-Encoding: base64{$maildelimiter}"; if (!($fp = fsocketopen($_G['server'], $_G['port'], $errno, $errstr, 30))) { //return "({$_G[server]}:{$_G[port]}) CONNECT - Unable to connect to the SMTP server"; return false; } stream_set_blocking($fp, true); $lastmessage = fgets($fp, 512); if (substr($lastmessage, 0, 3) != '220') { //return "{$_G[server]}:{$_G[port]} CONNECT - $lastmessage"; return false; } fputs($fp, 'EHLO' . " uchome\r\n"); $lastmessage = fgets($fp, 512); if (substr($lastmessage, 0, 3) != 220 && substr($lastmessage, 0, 3) != 250) { //return "({$_G[server]}:{$_G[port]}) HELO/EHLO - $lastmessage"; return false; } while (1) { if (substr($lastmessage, 3, 1) != '-' || empty($lastmessage)) { break; } $lastmessage = fgets($fp, 512); } fputs($fp, "AUTH LOGIN\r\n"); $lastmessage = fgets($fp, 512); if (substr($lastmessage, 0, 3) != 334) { //return "({$_G[server]}:{$_G[port]}) AUTH LOGIN - $lastmessage"; return false; } fputs($fp, base64_encode($_G['auth_username']) . "\r\n"); $lastmessage = fgets($fp, 512); if (substr($lastmessage, 0, 3) != 334) { //return "({$_G[server]}:{$_G[port]}) USERNAME - $lastmessage"; return false; } fputs($fp, base64_encode($_G['auth_password']) . "\r\n"); $lastmessage = fgets($fp, 512); if (substr($lastmessage, 0, 3) != 235) { //return "({$_G[server]}:{$_G[port]}) PASSWORD - $lastmessage"; return false; } $email_from = $_G['from']; fputs($fp, "MAIL FROM: <" . preg_replace("/.*\\<(.+?)\\>.*/", "\\1", $email_from) . ">\r\n"); $lastmessage = fgets($fp, 512); if (substr($lastmessage, 0, 3) != 250) { fputs($fp, "MAIL FROM: <" . preg_replace("/.*\\<(.+?)\\>.*/", "\\1", $email_from) . ">\r\n"); $lastmessage = fgets($fp, 512); if (substr($lastmessage, 0, 3) != 250) { //return "({$_G[server]}:{$_G[port]}) MAIL FROM - $lastmessage"; return false; } } fputs($fp, "RCPT TO: <" . preg_replace("/.*\\<(.+?)\\>.*/", "\\1", $toemail) . ">\r\n"); $lastmessage = fgets($fp, 512); if (substr($lastmessage, 0, 3) != 250) { fputs($fp, "RCPT TO: <" . preg_replace("/.*\\<(.+?)\\>.*/", "\\1", $toemail) . ">\r\n"); $lastmessage = fgets($fp, 512); //return "({$_G[server]}:{$_G[port]}) RCPT TO - $lastmessage"; return false; } fputs($fp, "DATA\r\n"); $lastmessage = fgets($fp, 512); if (substr($lastmessage, 0, 3) != 354) { //return "({$_G[server]}:{$_G[port]}) DATA - $lastmessage"; return false; } $timeoffset = 8; if (function_exists('date_default_timezone_set')) { @date_default_timezone_set('Etc/GMT' . ($timeoffset > 0 ? '-' : '+') . abs($timeoffset)); } $headers .= 'Message-ID: <' . date('YmdHs') . '.' . substr(md5($email_message . microtime()), 0, 6) . rand(100000, 999999) . '@' . $_SERVER['HTTP_HOST'] . ">{$maildelimiter}"; fputs($fp, "Date: " . date('r') . "\r\n"); fputs($fp, "To: " . $email_to . "\r\n"); fputs($fp, "Subject: " . $email_subject . "\r\n"); fputs($fp, $headers . "\r\n"); fputs($fp, "\r\n\r\n"); fputs($fp, "{$email_message}\r\n.\r\n"); $lastmessage = fgets($fp, 512); if (substr($lastmessage, 0, 3) != 250) { return "({$_G[server]}:{$_G[port]}) END - {$lastmessage}"; } fputs($fp, "QUIT\r\n"); return true; }
function _dfsockopen($url, $limit = 0, $post = "", $cookie = "", $bysocket = FALSE, $ip = "", $timeout = 15, $block = TRUE, $encodetype = "URLENCODE", $allowcurl = TRUE) { $return = ""; $matches = parse_url($url); $scheme = $matches['scheme']; $host = $matches['host']; $path = $matches['path'] ? $matches['path'] . ($matches['query'] ? "?" . $matches['query'] : "") : "/"; $port = !empty($matches['port']) ? $matches['port'] : 80; if (function_exists("curl_init") && function_exists("curl_exec") && $allowcurl) { $ch = curl_init(); if ($ip) { curl_setopt($ch, CURLOPT_HTTPHEADER, array("Host: " . $host)); } curl_setopt($ch, CURLOPT_URL, $scheme . "://" . ($ip ? $ip : $host) . ":" . $port . $path); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); if ($post) { curl_setopt($ch, CURLOPT_POST, 1); if ($encodetype == "URLENCODE") { curl_setopt($ch, CURLOPT_POSTFIELDS, $post); } else { parse_str($post, &$postarray); curl_setopt($ch, CURLOPT_POSTFIELDS, $postarray); } } if ($cookie) { curl_setopt($ch, CURLOPT_COOKIE, $cookie); } curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); $data = curl_exec($ch); $status = curl_getinfo($ch); $errno = curl_errno($ch); curl_close($ch); if ($errno || $status['http_code'] != 200) { return; } if (!$limit) { return $data; } return substr($data, 0, $limit); } else { if ($post) { $out = "POST " . $path . " HTTP/1.0\r\n"; $header = "Accept: */*\r\n"; $header .= "Accept-Language: zh-cn\r\n"; $boundary = $encodetype == "URLENCODE" ? "" : "; boundary=" . trim(substr(trim($post), 2, strpos(trim($post), "\n") - 2)); $header .= $encodetype == "URLENCODE" ? "Content-Type: application/x-www-form-urlencoded\r\n" : "Content-Type: multipart/form-data" . $boundary . "\r\n"; $header .= "User-Agent: " . $_SERVER['HTTP_USER_AGENT'] . "\r\n"; $header .= "Host: " . $host . ":{$port}\r\n"; $header .= "Content-Length: " . strlen($post) . "\r\n"; $header .= "Connection: Close\r\n"; $header .= "Cache-Control: no-cache\r\n"; $header .= "Cookie: " . $cookie . "\r\n\r\n"; $out .= $header . $post; } else { $out = "GET " . $path . " HTTP/1.0\r\n"; $header = "Accept: */*\r\n"; $header .= "Accept-Language: zh-cn\r\n"; $header .= "User-Agent: " . $_SERVER['HTTP_USER_AGENT'] . "\r\n"; $header .= "Host: " . $host . ":{$port}\r\n"; $header .= "Connection: Close\r\n"; $header .= "Cookie: " . $cookie . "\r\n\r\n"; $out .= $header; } $fpflag = 0; if (!($fp = @fsocketopen($ip ? $ip : $host, $port, $errno, $errstr, $timeout))) { $context = array("http" => array("method" => $post ? "POST" : "GET", "header" => $header, "content" => $post, "timeout" => $timeout)); $context = stream_context_create($context); $fp = @fopen($scheme . "://" . ($ip ? $ip : $host) . ":" . $port . $path, "b", FALSE, $context); $fpflag = 1; } if (!$fp) { return ""; } stream_set_blocking($fp, $block); stream_set_timeout($fp, $timeout); @fwrite($fp, $out); $status = stream_get_meta_data($fp); if (!$status['timed_out']) { do { } while (!feof($fp) || !$fpflag || (!($header = @fgets($fp)) && !($header == "\r\n") || !($header == "\n"))); $stop = FALSE; while (!feof($fp) || !$stop) { $data = fread($fp, $limit == 0 || 8192 < $limit ? 8192 : $limit); $return .= $data; if ($limit) { $limit -= strlen($data); $stop = $limit <= 0; } } } @fclose($fp); } return $return; }