imap = new imap_functions();
$this->user = $this->imap->username;
$this->password = $this->imap->password;
$this->host = $this->imap->imap_server;
$this->port = $this->imap->imap_port;
$this->state="DISCONNECTED";
$this->connection=null;
$this->error="";
$this->must_update=false;
$this->tag=uniqid("HKC");
}
function get_error()
{
if($this->error)
return $this->error;
}
function get_state()
{
return $this->state;
}
function open($host="",$port="")
{
if(!empty($host))
{
if ($port == 993)
$this->host="ssl://$host";
else
$this->host=$host;
}
if(!empty($port))
$this->port=$port;
return $this->open_connection();
}
function close()
{
if($this->must_update)
$this->close_mailbox();
$this->logout();
@fclose($this->connection);
$this->connection=null;
$this->state="DISCONNECTED";
return true;
}
function get_mailboxes_size()
{
// INBOX
if($this->put_line($this->tag . " GETANNOTATION \"user/".$this->user ."\" \"/vendor/cmu/cyrus-imapd/size\" \"value.shared\"" ))
{
$response_inbox=$this->get_server_responce();
if(substr($response_inbox,strpos($response_inbox,"$this->tag ")+strlen($this->tag)+1,2)!="OK")
{
$this->error= "Error : $response !
";
return false;
}
}
else
{
$this->error= "Error : Could not send User request.
";
return false;
}
$response_inbox_array = split("\r\n", $response_inbox);
array_pop($response_inbox_array);
array_shift($response_inbox_array);
// SUB_FOLDERS
if($this->put_line($this->tag . " GETANNOTATION \"user/".$this->user ."/*\" \"/vendor/cmu/cyrus-imapd/size\" \"value.shared\"" ))
{
$response_sub=$this->get_server_responce();
if(substr($response_sub,strpos($response_sub,"$this->tag ")+strlen($this->tag)+1,2)!="OK")
{
$this->error= "Error : $response !
";
return false;
}
}
else
{
$this->error= "Error : Could not send User request.
";
return false;
}
$response_sub_array = split("\r\n", $response_sub);
array_pop($response_sub_array);
array_shift($response_sub_array);
return array_merge($response_inbox_array, $response_sub_array);
}
//This function is used to get response line from server
function get_line()
{
while(!feof($this->connection))
{
$line.=fgets($this->connection);
if(strlen($line)>=2 && substr($line,-2)=="\r\n")
return(substr($line,0,-2));
}
}
//This function is to retrive the full response message from server
function get_server_responce()
{
$i=0;
while(1)
{
$i++;
$response.="\r\n".$this->get_line();
if(substr($response,strpos($response,$this->tag),strlen($this->tag))==$this->tag)
break;
//jakjr
if ($i>300)
{
if ($response)
return $response;
else
return false;
}
}
return $response;
}
// This function is to send the command to server
function put_line($msg="")
{
return @fputs($this->connection,"$msg\r\n");
}
//This function is to open the connection to the server
function open_connection()
{
if($this->state!="DISCONNECTED")
{
$this->error= "Error : Already Connected!
";
return false;
}
if(empty($this->host) || empty($this->port))
{
$this->error= "Error : Either HOST or PORT is undifined!
";
return false;
}
$this->connection= fsockopen($this->host, $this->port, $errno, $errstr, 5);
if(!$this->connection)
{
$this->error= "Could not make a connection to server , Error : $errstr ($errno)
";
return false;
}
$respone=$this->get_line();
$this->state="AUTHORIZATION";
return true;
}
//The logout function informs the server that the client is done with the connection.
function logout()
{
//jakjr
if(($this->state!="AUTHORIZATION") && ($this->state!="AUTHENTICATED"))
{
$this->error= "Error : No Connection Found!
";
return false;
}
if($this->put_line($this->tag." LOGOUT"))
{
$response=$this->get_server_responce();
if(substr($response,strpos($response,"$this->tag ")+strlen($this->tag)+1,2)!="OK")
{
$this->error= "Error : $response !
";
return false;
}
}
else
{
$this->error= "Error : Could not send User request.
";
return false;
}
return true;
}
//this function is used to login into server $user is a valid username and $pwd is a valid password.
function login($user,$pwd)
{
$this->user = $user;
if($this->state=="DISCONNECTED")
{
$this->error= "Error : No Connection Found!
";
return false;
}
if($this->state=="AUTHENTICATED")
{
$this->error= "Error : Already Authenticated!
";
return false;
}
if($this->put_line($this->tag." LOGIN $user $pwd"))
{
$response=$this->get_server_responce();
if(substr($response,strpos($response,"$this->tag ")+strlen($this->tag)+1,2)!="OK")
{
$this->error= "Error : $response !
";
return false;
}
}
else
{
$this->error= "Error : Could not send User request.
";
return false;
}
$this->state="AUTHENTICATED";
return true;
}
}
?>