📄 pop3.php
字号:
<?php/* * pop3.php * * @(#) $Header: /home/mlemos/cvsroot/pop3/pop3.php,v 1.19 2006/09/28 05:00:00 mlemos Exp $ * */class pop3_class{ var $hostname=""; var $port=110; var $tls=0; var $quit_handshake=1; var $error=""; var $authentication_mechanism="USER"; var $realm=""; var $workstation=""; var $join_continuation_header_lines=1; /* Private variables - DO NOT ACCESS */ var $connection=0; var $state="DISCONNECTED"; var $greeting=""; var $must_update=0; var $debug=0; var $html_debug=0; var $next_token=""; var $message_buffer=""; /* Private methods - DO NOT CALL */ Function Tokenize($string,$separator="") { if(!strcmp($separator,"")) { $separator=$string; $string=$this->next_token; } for($character=0;$character<strlen($separator);$character++) { if(GetType($position=strpos($string,$separator[$character]))=="integer") $found=(IsSet($found) ? min($found,$position) : $position); } if(IsSet($found)) { $this->next_token=substr($string,$found+1); return(substr($string,0,$found)); } else { $this->next_token=""; return($string); } } Function SetError($error) { return($this->error=$error); } Function OutputDebug($message) { $message.="\n"; if($this->html_debug) $message=str_replace("\n","<br />\n",HtmlSpecialChars($message)); echo $message; flush(); } Function GetLine() { for($line="";;) { if(feof($this->connection)) return(0); $line.=fgets($this->connection,100); $length=strlen($line); if($length>=2 && substr($line,$length-2,2)=="\r\n") { $line=substr($line,0,$length-2); if($this->debug) $this->OutputDebug("S $line"); return($line); } } } Function PutLine($line) { if($this->debug) $this->OutputDebug("C $line"); return(fputs($this->connection,"$line\r\n")); } Function OpenConnection() { if($this->tls) { $version=explode(".",function_exists("phpversion") ? phpversion() : "3.0.7"); $php_version=intval($version[0])*1000000+intval($version[1])*1000+intval($version[2]); if($php_version<4003000) return("establishing TLS connections requires at least PHP version 4.3.0"); if(!function_exists("extension_loaded") || !extension_loaded("openssl")) return("establishing TLS connections requires the OpenSSL extension enabled"); } if($this->hostname=="") return($this->SetError("2 it was not specified a valid hostname")); if($this->debug) $this->OutputDebug("Connecting to ".$this->hostname." ..."); if(($this->connection=@fsockopen(($this->tls ? "tls://" : "").$this->hostname,$this->port,$error, $errstr))==0) { switch($error) { case -3: return($this->SetError("-3 socket could not be created")); case -4: return($this->SetError("-4 dns lookup on hostname \"$hostname\" failed")); case -5: return($this->SetError("-5 connection refused or timed out")); case -6: return($this->SetError("-6 fdopen() call failed")); case -7: return($this->SetError("-7 setvbuf() call failed")); default: return($this->SetError($error." could not connect to the host (" . $errstr . ") \"".$this->hostname."\"")); } } return(""); } Function CloseConnection() { if($this->debug) $this->OutputDebug("Closing connection."); if($this->connection!=0) { fclose($this->connection); $this->connection=0; } } /* Public methods */ /* Open method - set the object variable $hostname to the POP3 server address. */ Function Open() { if($this->state!="DISCONNECTED") return($this->SetError("1 a connection is already opened")); if(($error=$this->OpenConnection())!="") return($error); $this->greeting=$this->GetLine(); if(GetType($this->greeting)!="string" || $this->Tokenize($this->greeting," ")!="+OK") { $this->CloseConnection(); return($this->SetError("3 POP3 server greeting was not found")); } $this->Tokenize("<"); $this->must_update=0; $this->state="AUTHORIZATION"; return(""); } /* Close method - this method must be called at least if there are any messages to be deleted */ Function Close() { if($this->state=="DISCONNECTED") return($this->SetError("no connection was opened")); if($this->must_update || $this->quit_handshake) { if($this->PutLine("QUIT")==0) return($this->SetError("Could not send the QUIT command")); $response=$this->GetLine(); if(GetType($response)!="string") return($this->SetError("Could not get quit command response")); if($this->Tokenize($response," ")!="+OK") return($this->SetError("Could not quit the connection: ".$this->Tokenize("\r\n"))); } $this->CloseConnection(); $this->state="DISCONNECTED"; return(""); } /* Login method - pass the user name and password of POP account. Set $apop to 1 or 0 wether you want to login using APOP method or not. */ Function Login($user,$password,$apop=0) { if($this->state!="AUTHORIZATION") return($this->SetError("connection is not in AUTHORIZATION state")); if($apop) { if(!strcmp($this->greeting,"")) return($this->SetError("Server does not seem to support APOP authentication")); if($this->PutLine("APOP $user ".md5("<".$this->greeting.">".$password))==0) return($this->SetError("Could not send the APOP command")); $response=$this->GetLine(); if(GetType($response)!="string") return($this->SetError("Could not get APOP login command response")); if($this->Tokenize($response," ")!="+OK") return($this->SetError("APOP login failed: ".$this->Tokenize("\r\n"))); } else { $authenticated=0; if(strcmp($this->authentication_mechanism,"USER") && function_exists("class_exists") && class_exists("sasl_client_class")) { if(strlen($this->authentication_mechanism)) $mechanisms=array($this->authentication_mechanism); else { $mechanisms=array(); if($this->PutLine("CAPA")==0) return($this->SetError("Could not send the CAPA command")); $response=$this->GetLine(); if(GetType($response)!="string") return($this->SetError("Could not get CAPA command response")); if(!strcmp($this->Tokenize($response," "),"+OK")) { for(;;) { $response=$this->GetLine(); if(GetType($response)!="string") return($this->SetError("Could not retrieve the supported authentication methods")); switch($this->Tokenize($response," ")) { case ".": break 2; case "SASL": for($method=1;strlen($mechanism=$this->Tokenize(" "));$method++) $mechanisms[]=$mechanism; break; } } } } $sasl=new sasl_client_class; $sasl->SetCredential("user",$user); $sasl->SetCredential("password",$password); if(strlen($this->realm)) $sasl->SetCredential("realm",$this->realm); if(strlen($this->workstation)) $sasl->SetCredential("workstation",$this->workstation); do { $status=$sasl->Start($mechanisms,$message,$interactions); } while($status==SASL_INTERACT); switch($status) { case SASL_CONTINUE: break; case SASL_NOMECH: if(strlen($this->authentication_mechanism)) return($this->SetError("authenticated mechanism ".$this->authentication_mechanism." may not be used: ".$sasl->error)); break; default: return($this->SetError("Could not start the SASL authentication client: ".$sasl->error)); } if(strlen($sasl->mechanism)) { if($this->PutLine("AUTH ".$sasl->mechanism.(IsSet($message) ? " ".base64_encode($message) : ""))==0) return("Could not send the AUTH command"); $response=$this->GetLine(); if(GetType($response)!="string") return("Could not get AUTH command response"); switch($this->Tokenize($response," ")) { case "+OK": $response=""; break; case "+": $response=base64_decode($this->Tokenize("\r\n")); break; default: return($this->SetError("Authentication error: ".$this->Tokenize("\r\n"))); } for(;!$authenticated;) { do { $status=$sasl->Step($response,$message,$interactions); } while($status==SASL_INTERACT); switch($status) { case SASL_CONTINUE: if($this->PutLine(base64_encode($message))==0) return("Could not send message authentication step message"); $response=$this->GetLine(); if(GetType($response)!="string") return("Could not get authentication step message response"); switch($this->Tokenize($response," ")) { case "+OK": $authenticated=1; break; case "+": $response=base64_decode($this->Tokenize("\r\n")); break; default: return($this->SetError("Authentication error: ".$this->Tokenize("\r\n"))); } break; default: return($this->SetError("Could not process the SASL authentication step: ".$sasl->error)); } } } } if(!$authenticated) { if($this->PutLine("USER $user")==0) return($this->SetError("Could not send the USER command")); $response=$this->GetLine(); if(GetType($response)!="string") return($this->SetError("Could not get user login entry response")); if($this->Tokenize($response," ")!="+OK") return($this->SetError("User error: ".$this->Tokenize("\r\n"))); if($this->PutLine("PASS $password")==0) return($this->SetError("Could not send the PASS command")); $response=$this->GetLine(); if(GetType($response)!="string") return($this->SetError("Could not get login password entry response")); if($this->Tokenize($response," ")!="+OK") return($this->SetError("Password error: ".$this->Tokenize("\r\n"))); } } $this->state="TRANSACTION"; return(""); } /* Statistics method - pass references to variables to hold the number of messages in the mail box and the size that they take in bytes. */ Function Statistics(&$messages,&$size) { if($this->state!="TRANSACTION") return($this->SetError("connection is not in TRANSACTION state")); if($this->PutLine("STAT")==0) return($this->SetError("Could not send the STAT command")); $response=$this->GetLine(); if(GetType($response)!="string") return($this->SetError("Could not get the statistics command response")); if($this->Tokenize($response," ")!="+OK") return($this->SetError("Could not get the statistics: ".$this->Tokenize("\r\n"))); $messages=$this->Tokenize(" "); $size=$this->Tokenize(" "); return(""); } /* ListMessages method - the $message argument indicates the number of a message to be listed. If you specify an empty string it will list all messages in the mail box. The $unique_id flag indicates if you want
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -