Copyright (c) 2006,2007,2008 osTicket http://www.osticket.com Released under the GNU General Public License WITHOUT ANY WARRANTY. See LICENSE.TXT for details. TODO: Add pear pop3/imap as alternative for user without IMAP ext. vim: expandtab sw=4 ts=4 sts=4: $Id: $ **********************************************************************/ require_once(INCLUDE_DIR.'class.mailparse.php'); require_once(INCLUDE_DIR.'class.ticket.php'); require_once(INCLUDE_DIR.'class.dept.php'); class POP3 { var $hostname; var $username; var $password; var $servertype; var $port; var $mbox; var $charset; function POP3($username,$password,$hostname='localhost',$servertype='pop',$port=0) { $this->hostname=$hostname; $this->username=$username; $this->password=$password; $this->servertype=$servertype; //Set connection string. if(strpos(strtolower($hostname),'pop.gmail.com')!==false){ $this->port=995; //overwrite the port; $this->serverstr='{pop.gmail.com:995/pop3/ssl/novalidate-cert}INBOX'; }elseif($this->servertype=='pop3') { $this->port=$port?$port:110; $this->serverstr='{'.$this->hostname.':'.$this->port.'}INBOX'; }elseif($this->servertype=='imap') { $this->port=$port?$port:143; $this->serverstr='{'.$this->hostname.':'.$this->port.'/pop3}INBOX'; }else{ $this->serverstr='{'.$this->hostname.'/pop3/notls}INBOX'; } //Charset to convert the mail to. $this->charset='UTF-8'; //Set timeouts if(function_exists('imap_timeout')) imap_timeout(1,20); //Open timeout. } function connect() { return $this->open()?true:false; } function open() { if($this->mbox && imap_ping($this->mbox)) return $this->mbox; $this->mbox =@imap_open($this->serverstr,$this->username,$this->password); return $this->mbox; } function close() { imap_close($this->mbox,CL_EXPUNGE); } function mailcount(){ return count(imap_headers($this->mbox)); } function decode($encoding,$text) { switch($encoding) { case 1: $text=imap_8bit($text); break; case 2: $text=imap_binary($text); break; case 3: $text=imap_base64($text); break; case 4: $text=imap_qprint($text); break; case 5: default: $text=$text; } return $text; } //Conver text to desired encoding..defaults to utf8 function mime_encode($text, $charset=null, $enc='UTF-8') { $encodings = array('UTF-8','WINDOWS-1251', 'WINDOWS-1252', 'ISO-8859-1', 'ISO-8859-15', 'US-ASCII', 'koi8-r'); if($charset){ $charset = strtoupper($charset); $text = iconv($charset, $enc.'//IGNORE', $text); }else{ $sourceEncoding = mb_detect_encoding($text, $encodings); $text = iconv($sourceEncoding, $enc . '//IGNORE', $text); } return $text; } function getLastError(){ return imap_last_error(); } function getMimeType($struct) { $mimeType = array('TEXT', 'MULTIPART', 'MESSAGE', 'APPLICATION', 'AUDIO', 'IMAGE', 'VIDEO', 'OTHER'); if(!$struct || !$struct->subtype) return 'TEXT/PLAIN'; return $mimeType[(int) $struct->type].'/'.$struct->subtype; } function getHeaderInfo($mid) { $headerinfo=imap_headerinfo($this->mbox,$mid); $sender=$headerinfo->from[0]; //Parse what we need... $header=array( 'from' =>array('name' =>@$sender->personal,'email' =>strtolower($sender->mailbox).'@'.$sender->host), 'subject'=>@$headerinfo->subject,); return $header; } //search for specific mime type parts....encoding is the desired encoding. function getPart($mid,$mimeType,$encoding=false,$struct=null,$partNumber=false){ if(!$struct) $struct=imap_fetchstructure($this->mbox, $mid); //Match the mime type. if($struct && strcasecmp($mimeType,$this->getMimeType($struct))==0){ $partNumber = $partNumber ? $partNumber : 1 ; if(($text=imap_fetchbody($this->mbox, $mid, $partNumber))){ // QuotedPrintable umrechnen if($struct->encoding==3 or $struct->encoding==4){ $text=$this->decode($struct->encoding,$text); } if($struct->parameters[0] && trim(strtolower($struct->parameters[0]->attribute))=='charset'){ $charset = trim($struct->parameters[0]->value); $text = $this->mime_encode($text, $charset, $encoding); }else{ $text=$this->mime_encode($text, '', $encoding); } return $text; } } //Do recursive search if($struct && $struct->parts){ while(list($i, $substruct) = each($struct->parts)) { if($partNumber) $prefix = $partNumber . '.'; if(($text=$this->getPart($mid,$mimeType,$encoding,$substruct,$prefix.($i+1)))) return $text; } } //No luck. return false; } function getHeader($mid){ return imap_fetchheader($this->mbox, $mid,FT_PREFETCHTEXT); } function getPriority($mid){ return Mail_Parse::parsePriority($this->getHeader($mid)); } /* function getBody($mid) { $body = ''; // Text/Html if(empty($body)==true){ $body = 'html::' . $this->getPart($mid, 'TEXT/HTML', $this->charset); //Convert tags of interest before we striptags //$body=str_replace("