Suppose to be a simple class to connect to a POP3 account, but no info is displayed. How should i implement it?
<?php
class Pop3 {
var $user;
var $pass;
var $Socket; // the current socket
var $Line;
var $Status;
var $Banner;
function Pop3($Server = "localhost",$Port = 110)
{
return $this->pop3_open($Server, $Port);
}
# This will handle the connection process for you, just feed it the
# information it needs. It will default to APOP secure connection but
# fall back to plain text if that doesn't work. WARNING: some servers
# will kick you out after one failed attempt so after the failed APOP
# authentication, you'll be kicked out, and this will fail, disable
# APOP authentication if this is the case, and APOP doesn't work.
function Pop3($Server = "localhost", $Port = 110, $user = "myusername", $pass = "mypass") {
if ( !$this->pop3_open($Server, $Port) ) {
$this->Status["Connection"] =
"Could not contact server '$Server' on port $Port";
return;
}
if (false && $this->Banner) { # disable this pending further APOP tests
if (!$this->pop3_apop($user, $pass)) {
if ( !$this->pop3_user($user) ) {
$this->Status["Connection"] = "Username '$user' is invalid";
return;
}
if ( !$this->pop3_pass($pass) ) {
$this->Status["Connection"] =
"Username '$user' or Password is invalid";
return;
}
}
} else {
if ( !$this->pop3_user($user) ) {
$this->Status["Connection"] = "Username '$user' is invalid";
return;
}
if ( !$this->pop3_pass($pass) ) {
$this->Status["Connection"] =
"Username '$user' or Password is invalid";
return;
}
}
return true;
}
function pop3_open($server, $port)
{
$this->Socket = fsockopen($server, $port);
if ($this->Socket <= 0){
return false;
}
$this->Line = fgets($this->Socket, 1024);
$this->Status["LASTRESULT"] = substr($this->Line, 0, 1);
$this->Status["LASTRESULTTXT"] = substr($this->Line, 0, 1024);
if (preg_match("/<[\w\d\-\.]+\@[\w\d\-\.]+>/",
$this->Status["LASTRESULTTXT"], $matched)) {
$this->Banner = $matched[0];
} else {
$this->Banner = "";
}
if ($this->Status["LASTRESULT"] <> "+") return false;
return true;
}
function pop3_user($user)
{
$this->user = $user;
if ($this->Socket < 0){
return false;
}
fputs($this->Socket, "USER $this->user\r\n");
$this->Line = fgets($this->Socket, 1024);
$this->Status["LASTRESULT"] = substr($this->Line, 0, 1);
$this->Status["LASTRESULTTXT"] = substr($this->Line, 0, 1024);
if ($this->Status["LASTRESULT"] <> "+") return false;
return true;
}
function pop3_pass($pass)
{
$this->pass = $pass;
fputs($this->Socket, "PASS $this->pass\r\n");
$this->Line = fgets($this->Socket, 1024);
$this->Status["LASTRESULT"] = substr($this->Line, 0, 1);
$this->Status["LASTRESULTTXT"] = substr($this->Line, 0, 1024);
if ($this->Status["LASTRESULT"] <> "+") return false;
return true;
}
function pop3_apop($user, $pass)
{
fputs($this->Socket, "APOP $user ".md5($this->Banner.$pass)."\r\n");
$this->Line = fgets($this->Socket, 1024);
$this->Status["LASTRESULT"] = substr($this->Line, 0, 1);
$this->Status["LASTRESULTTXT"] = substr($this->Line, 0, 1024);
if ($this->Status["LASTRESULT"] <> "+") return false;
return true;
}
function pop3_stat()
{
fputs($this->Socket, "STAT\r\n");
$this->Line = fgets($this->Socket, 1024);
$this->Status["LASTRESULT"] = substr($this->Line, 0, 1);
$this->Status["LASTRESULTTXT"] = substr($this->Line, 0, 1024);
if ($this->Status["LASTRESULT"] <> "+") return false;
if (!eregi("+OK (.*) (.*)", $this->Line, $regs))
return false;
return $regs[1];
}
function pop3_list()
{
fputs($this->Socket, "LIST\r\n");
$this->Line = fgets($this->Socket, 1024);
$this->Status["LASTRESULT"] = substr($this->Line, 0, 1);
$this->Status["LASTRESULTTXT"] = substr($this->Line, 0, 1024);
if ($this->Status["LASTRESULT"] <> "+") return false;
$i = 0;
while (substr($this->Line = fgets($this->Socket, 1024), 0, 1) <> ".") {
$articles[$i] = $this->Line;
$i++;
}
return $articles;
}
# This retreaves the whole message, and sets the Status: field in the
# message to read (it adds an 'R' to that field).
function pop3_retr($nr)
{
fputs($this->Socket, "RETR $nr\r\n");
$this->Line = fgets($this->Socket, 1024);
$this->Status["LASTRESULT"] = substr($this->Line, 0, 1);
$this->Status["LASTRESULTTXT"] = substr($this->Line, 0, 1024);
if ($this->Status["LASTRESULT"] <> "+") return 0;
while (substr($this->Line = fgets($this->Socket, 1024), 0, 1) <> ".") {
$data[$i] = $this->Line;
$i++;
}
return $data;
}
# This will view the header of a message (and $lines number of lines
# afterwards WITHOUT setting the Status: field as read (doesn't add an 'R'
# to that field.
function pop3_top($nr, $lines)
{
fputs($this->Socket, "TOP $nr $lines\r\n");
$this->Line = fgets($this->Socket, 1024);
$this->Status["LASTRESULT"] = substr($this->Line, 0, 1);
$this->Status["LASTRESULTTXT"] = substr($this->Line, 0, 1024);
if ($this->Status["LASTRESULT"] <> "+") return 0;
while (substr($this->Line = fgets($this->Socket, 1024), 0, 1) <> ".") {
$data[$i] = $this->Line;
$i++;
}
return $data;
}
function pop3_dele( $nr)
{
fputs($this->Socket, "DELE $nr\r\n");
$this->Line = fgets($this->Socket, 1024);
$this->Status["LASTRESULT"] = substr($this->Line, 0, 1);
$this->Status["LASTRESULTTXT"] = substr($this->Line, 0, 1024);
if ($this->Status["LASTRESULT"] <> "+") return 0;
return 1;
}
function pop3_quit()
{
fputs($this->Socket, "QUIT\r\n");
$this->Line = fgets($this->Socket, 1024);
$this->Status["LASTRESULT"] = substr($this->Line, 0, 1);
$this->Status["LASTRESULTTXT"] = substr($this->Line, 0, 1024);
if ($this->Status["LASTRESULT"] <> "+") return 0;
return 1;
}
}
$tester = new Pop3;
$test = $tester->Pop3();
if (!$test){
echo "Couldn't connect";
} else {
echo "connection established";
}
?>