function putline($line) { if($this->debug) $this->outputdebug("> $line"); return(fputs($this->connection,"$line/r/n")); }
function openconnection() { if($this->hostname=="") return("2 it was not specified a valid hostname"); switch(($this->connection=fsockopen($this->hostname,$this->port))) { case -3: return("-3 socket could not be created"); case -4: return("-4 dns lookup on hostname /"$hostname/" failed"); case -5: return("-5 connection refused or timed out"); case -6: return("-6 fdopen() call failed"); case -7: return("-7 setvbuf() call failed"); default: return(""); } }
function closeconnection() { if($this->connection!=0) { fclose($this->connection); $this->connection=0; } }
function open() { if($this->state!="disconnected") return("1 a connection is already opened"); if(($error=$this->openconnection())!="") return($error); $this->greeting=$this->getline(); if(gettype($this->greeting)!="string" || strtok($this->greeting," ")!="+ok") { $this->closeconnection(); return("3 pop3 server greeting was not found"); } $this->greeting=strtok("/r/n"); $this->must_update=0; $this->state="authorization"; return(""); }
function close() { if($this->state=="disconnected") return("no connection was opened"); if($this->must_update) { if($this->putline("quit")==0) return("could not send the quit command"); $response=$this->getline(); if(gettype($response)!="string") return("could not get quit command response"); if(strtok($response," ")!="+ok") return("could not quit the connection: ".strtok("/r/n")); } $this->closeconnection(); $this->state="disconnected"; return(""); }
function login($user,$password,$apop) { if($this->state!="authorization") return("connection is not in authorization state"); if($apop) { if($this->putline("apop $user ".md5($this->greeting.$password))==0) return("could not send the apop command"); $response=$this->getline(); if(gettype($response)!="string") return("could not get apop login command response"); if(strtok($response," ")!="+ok") return("apop login failed: ".strtok("/r/n")); } else { if($this->putline("user $user")==0) return("could not send the user command"); $response=$this->getline(); if(gettype($response)!="string") return("could not get user login entry response"); if(strtok($response," ")!="+ok") return("user error: ".strtok("/r/n")); if($this->putline("pass $password")==0) return("could not send the pass command"); $response=$this->getline(); if(gettype($response)!="string") return("could not get login password entry response"); if(strtok($response," ")!="+ok") return("password error: ".strtok("/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("connection is not in transaction state"); if($this->putline("stat")==0) return("could not send the stat command"); $response=$this->getline(); if(gettype($response)!="string") return("could not get the statistics command response"); if(strtok($response," ")!="+ok") return("could not get the statistics: ".strtok("/r/n")); $messages=strtok(" "); $size=strtok(" "); return(""); }
function listmessages($message,$unique_id) { if($this->state!="transaction") return("connection is not in transaction state"); if($unique_id) $list_command="uidl"; else $list_command="list"; if($this->putline("$list_command $message")==0) return("could not send the $list_command command"); $response=$this->getline(); if(gettype($response)!="string") return("could not get message list command response"); if(strtok($response," ")!="+ok") return("could not get the message listing: ".strtok("/r/n")); if($message=="") { for($messages=array();;) { $response=$this->getline(); if(gettype($response)!="string") return("could not get message list response"); if($response==".") break; $message=intval(strtok($response," ")); if($unique_id) $messages[$message]=strtok(" "); else $messages[$message]=intval(strtok(" ")); } return($messages); } else { $message=intval(strtok(" ")); return(intval(strtok(" "))); } }
function retrievemessage($message,$headers,$body,$lines) { if($this->state!="transaction") return("connection is not in transaction state"); if($lines<0) { $command="retr"; $arguments="$message"; } else { $command="top"; $arguments="$message $lines"; } if($this->putline("$command $arguments")==0) return("could not send the $command command"); $response=$this->getline(); if(gettype($response)!="string") return("could not get message retrieval command response"); if(strtok($response," ")!="+ok") return("could not retrieve the message: ".strtok("/r/n")); for($headers=$body=array(),$line=0;;$line++) { $response=$this->getline(); if(gettype($response)!="string") return("could not retrieve the message"); switch($response) { case ".": return(""); case "": break 2; default: if(substr($response,0,1)==".") $response=substr($response,1,strlen($response)-1); break; } $headers[$line]=$response; } for($line=0;;$line++) { $response=$this->getline(); if(gettype($response)!="string") return("could not retrieve the message"); switch($response) { case ".": return(""); default: if(substr($response,0,1)==".") $response=substr($response,1,strlen($response)-1); break; } $body[$line]=$response; } return(""); }