private function open($api) { if ($this->socket) return true; $socket = msockopen($this->server, $this->port); if (!$socket) { return false; } $this->socket = $socket; return true; }
function Update_ServerTest() { $url = 'http://update.cenwor.com/'; $baidu = 'http://www.baidu.com/'; $help = ' <a href="http://cenwor.com/thread-9995-1-1.html" target="_blank">查看帮助</a>'; if (false == msockopen()) { exit('socket相关函数被限制!'.$help); } if (false != $file = $this->SMS_ServerTest_connector($url)) { exit('测试正常,可以访问升级服务器!'); } elseif (false != $file = $this->SMS_ServerTest_connector($baidu)) { exit('无法连接到升级服务器!'.$help); } else { exit('无法访问外部服务器!'.$help); } }
private function FunctionTest($list) { $return = array(); foreach ($list as $i => $func) { if ($func == 'msockopen') { $return[] = array( 'name' => $func, 'sp' => msockopen() ? true : false ); } else { $return[] = array( 'name' => $func, 'sp' => function_exists($func) ); } } return $return; }
private function __Verify($url, $time_out = '6') { $urlArr = parse_url($url); $errNo = ''; $errStr = ''; $transPorts = ''; if($urlArr['scheme'] == 'https') { $transPorts = 'ssl://'; $urlArr['port'] = '443'; } else { $transPorts = 'tcp://'; $urlArr['port'] = '80'; } $fp = msockopen($transPorts . $urlArr['host'], $urlArr['port'], $errNo, $errStr, $time_out); if(!$fp) { zlog('error')->found('error.msockopen'); die("ERROR: $errNo - $errStr<br />\n"); } else { fputs($fp, "POST ".$urlArr["path"]." HTTP/1.1\r\n"); fputs($fp, "Host: ".$urlArr["host"]."\r\n"); fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n"); fputs($fp, "Content-length: ".strlen($urlArr["query"])."\r\n"); fputs($fp, "Connection: close\r\n\r\n"); fputs($fp, $urlArr["query"] . "\r\n\r\n"); while(!feof($fp)) { $info[]=@fgets($fp, 1024); } fclose($fp); $info = implode(",",$info); return $info; } }
$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 = msockopen($mail_setting['mailserver'], $mail_setting['mailport'], $errno, $errstr, 3)) { 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; }
function smtp_sockopen_mx($address) { $domain = ereg_replace("^.+@([^@]+)$", "\1", $address); if (!@getmxrr($domain, $MXHOSTS)){ $this -> log_write("Error: Cannot resolve MX \"" . $domain . "\"\n"); return FALSE; } foreach ($MXHOSTS as $host){ $this -> log_write("Trying to " . $host . ":" . $this -> smtp_port . "\n"); $this -> sock = msockopen($host, $this -> smtp_port, $errno, $errstr, $this -> time_out); if (!($this -> sock && $this -> smtp_ok())){ $this -> log_write("Warning: Cannot connect to mx host " . $host . "\n"); $this -> log_write("Error: " . $errstr . " (" . $errno . ")\n"); continue; } $this -> log_write("Connected to mx host " . $host . "\n"); return TRUE; } $this -> log_write("Error: Cannot connect to any mx hosts (" . implode(", ", $MXHOSTS) . ")\n"); return FALSE; }
function dfopen($url, $limit = 10485760 , $post = '', $cookie = '', $bysocket = false,$timeout=5,$agent="") { if(ini_get('allow_url_fopen') && !$bysocket && !$post) { $fp = @fopen($url, 'r'); $s = $t = ''; if($fp) { while ($t=fread($fp,2048)) { $s.=$t; } fclose($fp); } if($s) { return $s; } } $return = ''; $agent=$agent?$agent:"Mozilla/5.0 (compatible; Googlebot/2.1; +http:/"."/www.google.com/bot.html)"; $matches = parse_url($url); $host = $matches['host']; $script = $matches['path'].($matches['query'] ? '?'.$matches['query'] : '').($matches['fragment'] ? '#'.$matches['fragment'] : ''); $script = $script ? $script : '/'; $port = !empty($matches['port']) ? $matches['port'] : 80; if($post) { $out = "POST $script HTTP/1.1\r\n"; $out .= "Accept: */"."*\r\n"; $out .= "Referer: $url\r\n"; $out .= "Accept-Language: zh-cn\r\n"; $out .= "Content-Type: application/x-www-form-urlencoded\r\n"; $out .= "Accept-Encoding: none\r\n"; $out .= "User-Agent: $agent\r\n"; $out .= "Host: $host\r\n"; $out .= 'Content-Length: '.strlen($post)."\r\n"; $out .= "Connection: Close\r\n"; $out .= "Cache-Control: no-cache\r\n"; $out .= "Cookie: $cookie\r\n\r\n"; $out .= $post; } else { $out = "GET $script HTTP/1.1\r\n"; $out .= "Accept: */"."*\r\n"; $out .= "Referer: $url\r\n"; $out .= "Accept-Language: zh-cn\r\n"; $out .= "Accept-Encoding:\r\n"; $out .= "User-Agent: $agent\r\n"; $out .= "Host: $host\r\n"; $out .= "Connection: Close\r\n"; $out .= "Cookie: $cookie\r\n\r\n"; } $fp = msockopen($host, $port, $errno, $errstr, $timeout); if(!$fp) { return false; } else { fwrite($fp, $out); $return = ''; while(!feof($fp) && $limit > -1) { $limit -= 8192; $return .= fread($fp, 8192); if(!isset($status)) { preg_match("|^HTTP/[^\s]*\s(.*?)\s|",$return, $status); $status=$status[1]; if($status!=200) { return false; } } } fclose($fp); preg_match("/^Location: ([^\r\n]+)/m",$return,$match); if(!empty($match[1]) && $location=$match[1]) { if(strpos($location,":/"."/")===false) { $location=dirname($url).'/'.$location; } $args=func_get_args(); $args[0]=$location; return call_user_func_array("dfopen",$args); } if(false!==($strpos = strpos($return, "\r\n\r\n"))) { $return = substr($return,$strpos); $return = preg_replace("~^\r\n\r\n(?:[\w\d]{1,8}\r\n)?~","",$return); if("\r\n\r\n"==substr($return,-4)) { $return = preg_replace("~(?:\r\n[\w\d]{1,8})?\r\n\r\n$~","",$return); } } return $return; } }
function uc_fopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE, $ip = '', $timeout = 3, $block = TRUE) { $return = ''; $matches = parse_url($url); !isset($matches['host']) && $matches['host'] = ''; !isset($matches['path']) && $matches['path'] = ''; !isset($matches['query']) && $matches['query'] = ''; !isset($matches['port']) && $matches['port'] = ''; $host = $matches['host']; $path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/'; $port = !empty($matches['port']) ? $matches['port'] : 80; if($post) { $out = "POST $path HTTP/1.0\r\n"; $out .= "Accept: */*\r\n"; //$out .= "Referer: $boardurl\r\n"; $out .= "Accept-Language: zh-cn\r\n"; $out .= "Content-Type: application/x-www-form-urlencoded\r\n"; $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n"; $out .= "Host: $host\r\n"; $out .= 'Content-Length: '.strlen($post)."\r\n"; $out .= "Connection: Close\r\n"; $out .= "Cache-Control: no-cache\r\n"; $out .= "Cookie: $cookie\r\n\r\n"; $out .= $post; } else { $out = "GET $path HTTP/1.0\r\n"; $out .= "Accept: */*\r\n"; //$out .= "Referer: $boardurl\r\n"; $out .= "Accept-Language: zh-cn\r\n"; $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n"; $out .= "Host: $host\r\n"; $out .= "Connection: Close\r\n"; $out .= "Cookie: $cookie\r\n\r\n"; } $fp = msockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout); 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)) { 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 download_range($url, $offset = 0, $length = 1024, $all = 0) { $matches = parse_url($url); $host = $matches['host']; $script = $matches['path'].(isset($matches['query']) ? '?'.$matches['query'] : ''); $script = $script ? $script : '/'; $port = !empty($matches['port']) ? $matches['port'] : 80; $offset_end = $offset + $length; if ($offset_end >= $all && $all > 0) { $offset_end = $all - 1; } $out = "GET $script HTTP/1.1\r\n"; $out .= "Accept: */"."*\r\n"; $out .= "Referer: {$this->Config['site_url']}/~".SYS_VERSION."~".SYS_BUILD."\r\n"; $out .= "Accept-Encoding: none\r\n"; $out .= "Range: bytes=$offset-$offset_end\r\n"; $out .= "User-Agent: Cenwor.Downloader.Agent(PHP)[MOYO].2012.08.16\r\n"; $out .= "Host: $host\r\n"; $out .= "Connection: Close\r\n\r\n"; $errno = 0; $errstr = ''; $timeout = 180; $fp = msockopen($host, $port, $errno, $errstr, $timeout); if (!$fp) { return false; } else { fwrite($fp, $out); $header = ''; while ( $str = trim(fgets($fp, 4096)) ) { $header .= $str."\n"; } $body = ''; while ( !feof($fp) ) { $body .= fgets($fp, 4096); } fclose($fp); $http = array('header'=>$header, 'data'=>$body); } preg_match('/HTTP\/1.1 (\d+)/i', $http['header'], $mchs); $staCode = $mchs[1]; if ($staCode == 206) { preg_match('/bytes\s+(\d+)-(\d+)\/(\d+)/i', $http['header'], $mchs); $return = array( 'bytes_start' => $mchs[1], 'bytes_finish' => $mchs[2], 'bytes_all' => $mchs[3] ); if ($return['bytes_finish'] + 1 >= $return['bytes_all']) { return array( 'next' => false, 'bin' => $http['data'] ); } else { $return['next'] = array( 'url' => $url, 'offset' => $return['bytes_finish'] + 1, 'length' => $length, 'all' => $return['bytes_all'] ); $return['bin'] = $http['data']; return $return; } } elseif ($staCode == 200) { return array( 'next' => false, 'bin' => $http['data'] ); } else { return array( 'next' => false, 'bin' => false ); } }
function Connect( $host, $port = 0, $tval = 30 ) { # set the error val to null so there is no confusion $this->error = null; # make sure we are __not__ connected if ( $this->connected() ) { # ok we are connected! what should we do? # for now we will just give an error saying we # are already connected $this->error = array( "error" => "Already connected to a server" ); return false; } if ( empty($port) ) { $port = $this->SMTP_PORT; } #connect to the smtp server $this->smtp_conn = msockopen($host, # the host of the server $port, # the port to use $errno, # error number if any $errstr, # error message if any $tval); # give up after ? secs # verify we connected properly if ( empty($this->smtp_conn) ) { $this->error = array( "error" => "Failed to connect to server", "errno" => $errno, "errstr" => $errstr ); $this->errorType == '' && $this->errorType = 'connect_host'; if ( $this->do_debug >= 1 ) { $this->debug("SMTP -> ERROR: " . $this->error["error"] . ": $errstr ($errno)"); } return false; } # sometimes the SMTP server takes a little longer to respond # so we will give it a longer timeout for the first read if ( substr(PHP_OS, 0, 3) != "WIN" ) socket_set_timeout($this->smtp_conn, $tval, 0); # get any announcement stuff $announce = $this->get_lines(); # set the timeout of any socket functions at 1/10 of a second if ( $this->do_debug >= 2 ) { $this->debug("SMTP -> FROM SERVER: " . $announce); } return true; }
function _send_mail_by_smtp($email_to,$email_subject,$email_message,$smtp_config='',$html=true) { $sys_config = ConfigHandler::get(); $smtp_config = $smtp_config ? $smtp_config : (ConfigHandler::get('smtp')); $mail['from'] = $smtp_config['mail']; $mail['server'] = ($smtp_config['ssl'] ? 'ssl:/'.'/' : '') . $smtp_config['host']; $mail['port'] = $smtp_config['port']; $mail['auth'] = (boolean) ($smtp_config['username'] && $smtp_config['password']); $mail['auth_username'] = $smtp_config['username']; $mail['auth_password'] = $smtp_config['password']; $errorlog = 'ajherrorlog'; $charset = $sys_config['charset']; $bbname = $sys_config['site_name']; $adminemail = $sys_config['site_admin_email']; $maildelimiter = NEW_LINE; $mailusername = 1; $email_subject = '=?'.$charset.'?B?'.base64_encode(str_replace("\r", '', str_replace("\n", '', $email_subject))).'?='; $email_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", $email_message))))))); $email_from = $smtp_config['email_from'] ? $smtp_config['email_from'] : $smtp_config['mail']; $email_from = ($email_from == '' ? '=?'.$charset.'?B?'.base64_encode($bbname)."?= <$adminemail>" : (preg_match('/^(.+?) \<(.+?)\>$/',$email_from, $from) ? '=?'.$charset.'?B?'.base64_encode($from[1])."?= <$from[2]>" : $email_from)); foreach(explode(',', $email_to) as $touser) { $tousers[] = preg_match('/^(.+?) \<(.+?)\>$/',$touser, $to) ? ($mailusername ? '=?'.$charset.'?B?'.base64_encode($to[1])."?= <$to[2]>" : $to[2]) : $touser; } $email_to = implode(',', $tousers); $headers = "From: $email_from{$maildelimiter}X-Priority: 3{$maildelimiter}X-Mailer: TTTuangou ".SYS_VERSION."{$maildelimiter}MIME-Version: 1.0{$maildelimiter}Content-type: text/".($html ? 'html' : 'plain')."; charset=$charset{$maildelimiter}Content-Transfer-Encoding: base64{$maildelimiter}"; $mail['port'] = $mail['port'] ? $mail['port'] : 25; if(!$fp = msockopen($mail['server'], $mail['port'], $errno, $errstr, 3)) { $errorlog('SMTP', "($mail[server]:$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') { $errorlog('SMTP', "$mail[server]:$mail[port] CONNECT - $lastmessage", 0); return false; } fputs($fp, ($mail['auth'] ? 'EHLO' : 'HELO')." TTTuangou\r\n"); $lastmessage = fgets($fp, 512); if(substr($lastmessage, 0, 3) != 220 && substr($lastmessage, 0, 3) != 250) { $errorlog('SMTP', "($mail[server]:$mail[port]) HELO/EHLO - $lastmessage", 0); return false; } while(1) { if(substr($lastmessage, 3, 1) != '-' || empty($lastmessage)) { break; } $lastmessage = fgets($fp, 512); } if($mail['auth']) { fputs($fp, "AUTH LOGIN\r\n"); $lastmessage = fgets($fp, 512); if(substr($lastmessage, 0, 3) != 334) { $errorlog('SMTP', "($mail[server]:$mail[port]) AUTH LOGIN - $lastmessage", 0); return false; } fputs($fp, base64_encode($mail['auth_username'])."\r\n"); $lastmessage = fgets($fp, 512); if(substr($lastmessage, 0, 3) != 334) { $errorlog('SMTP', "($mail[server]:$mail[port]) USERNAME - $lastmessage", 0); return false; } fputs($fp, base64_encode($mail['auth_password'])."\r\n"); $lastmessage = fgets($fp, 512); if(substr($lastmessage, 0, 3) != 235) { $errorlog('SMTP', "($mail[server]:$mail[port]) PASSWORD - $lastmessage", 0); return false; } $email_from = $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) { $errorlog('SMTP', "($mail[server]:$mail[port]) MAIL FROM - $lastmessage", 0); return false; } } $email_tos = array(); foreach(explode(',', $email_to) as $touser) { $touser = trim($touser); if($touser) { fputs($fp, "RCPT TO: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $touser).">\r\n"); $lastmessage = fgets($fp, 512); if(substr($lastmessage, 0, 3) != 250) { fputs($fp, "RCPT TO: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $touser).">\r\n"); $lastmessage = fgets($fp, 512); $errorlog('SMTP', "($mail[server]:$mail[port]) RCPT TO - $lastmessage", 0); return false; } } } fputs($fp, "DATA\r\n"); $lastmessage = fgets($fp, 512); if(substr($lastmessage, 0, 3) != 354) { $errorlog('SMTP', "($mail[server]:$mail[port]) DATA - $lastmessage", 0); return false; } $headers .= 'Message-ID: <'.gmdate('YmdHs').'.'.substr(md5($email_message.microtime()), 0, 6).rand(100000, 999999).'@'.$_SERVER['HTTP_HOST'].">{$maildelimiter}"; fputs($fp, "Date: ".gmdate('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) { $errorlog('SMTP', "($mail[server]:$mail[port]) END - $lastmessage", 0); return false; } fputs($fp, "QUIT\r\n"); return true; }