<?php
Class POP3 {
        var $f;
        var $error_code=0;
        var $error="";
        var $res;
        var $debug=false;
	var $br="\n";

        function connect($login, $password, $host="localhost", $port="110") {
		if ($this->debug) print 'Connecting...'.$this->br;
                $this->f=fsockopen($host, $port, $errno, $errstr, 30);
                if ($this->f) {
                        $this->res=fgets($this->f, 1024);
                        if ($this->debug) print $this->res;
                        preg_match("/^(\+OK|\-ERR) (.*)/", $this->res, $array);
                        $this->error_code=($array[1]=="-ERR");
                        $this->error=($array[1]=="-ERR" ? $array[2] : "");
                        if (!$this->error_code) 
                                if ($this->send("USER", $login)) $this->send("PASS", $password);
                } else {
			if ($this->debug) print "Error: Can't connect to {$host}".$this->br;
			$this->error_code=-1;
		}
                return (!$this->error_code);
        }

        function get_answer($size=1024) {
                $this->error_code=0;
                $line="";
                if ($this->f) {
                        $this->res="";
                        while (trim($line)!='.') {
                                $line=fgets($this->f, 1024);
                                if ($this->debug) print $line;
                                $this->res.=$line;
                        }
                } else $this->error_code=-1;
                return !$this->error_code;
        }

        function send($name, $param="") {
                $this->res="";
                if ($this->f) {
                        $c=strtoupper($name)." ".$param."\r\n";
                        if ($this->debug) print $c;
                        fputs($this->f, $c);
                        $this->res=fgets($this->f, 1024);
                        if ($this->debug) print $this->res;
                        preg_match("/^(\+OK|\-ERR) (.*)/", $this->res, $array);
                        $this->error_code=($array[1]=="-ERR");
                        $this->error=($array[1]=="-ERR" ? $array[2] : "");
                        $this->res=$array[2];
                } else $this->error_code=-1;
                return (!$this->error_code);
        }

        function get_header_info() {
                $line="";
                $res=array();
                if (!$this->f) return $res;
                while (trim($line)!=".") {
                        $line=fgets($this->f, 1024);
                        if ($this->debug) print $line;
                        preg_match("/^([^:]*): (.*)/", $line, $array);
                        if (isset($array[1])) $res[$array[1]]=(isset($res[$array[1]]) ? $res[$array[1]].$array[2] : $array[2]);
                }
                return $res;
        }

        function check_mail_old($limit=0) {
                $res=array();
                if ($this->f) {
                        if ($this->send("STAT")) {
                                list($n, $l)=explode(" ", $this->res);
                                if ($limit>0) $n=($n<$limit ? $n : $limit);
                                for ($i=1; $i<=$n; $i++) {
                                        if ($this->send("TOP", "$i 0")) {
                                                $res[]=$this->get_header_info();
                                                $this->del($i);
                                        }
                                }
                        }
                } else $this->error_code=-1;
                return $res;
        }

        function del($id=NULL) {
                $id=(is_array($id) ? $id : (isset($id) ? array($id) : array()));
                for ($i=0; $i<count($id); $i++) $this->send("DELE", $id[$i]);
        }

	function getMessageCount() {
		$res=0;
		if ($this->f) {
			if ($this->send("STAT")) {
				list($res, $l)=explode(" ", $this->res);
			}
		} else $this->error_code=-1;
                return $res;
	}

	function getMessage($n) {
		$res='';
		if ($this->f) {
			if ($this->send("RETR", $n)) {
				list($l, $_l)=explode(" ", $this->res);
				$this->get_answer($l);
				$res=$this->res;
			}
		} else $this->error_code=-1;
                return $res;
	}

        function check_mail($limit=0) {
                $res=array();
                if ($this->f) {
                        if ($this->send("STAT")) {
                                list($n, $l)=explode(" ", $this->res);
                                if ($limit>0) $n=($n<$limit ? $n : $limit);
                                for ($i=1; $i<=$n; $i++) {
                                        if ($this->send("RETR", $i)) {
                                                list($l, )=explode(" ", $this->res);
                                                $this->get_answer($l);
                                                $res[]=$this->res;
                                                $this->del($i);
                                        }
                                }
                        }
                } else $this->error_code=-1;
                return $res;
        }

        function close() {
                if ($this->f) {
                        $this->send("QUIT");
                        fclose($this->f);
                }
        }
}
?>