-
-
Save VeeeneX/343faa90bab9f8717d68 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// +----------------------------------------------------------------------+ | |
// | Copyright (c) 2010 DasLampe <[email protected]> | | |
// | Encoding: UTF-8 | | |
// +----------------------------------------------------------------------+ | |
class IMAP | |
{ | |
var $sock; | |
var $host; | |
var $port; | |
var $sid; | |
var $tag; | |
var $_lastTag; | |
var $currentMailbox; | |
function __construct() | |
{ | |
$this->sock = null; | |
$this->host = "localhost"; | |
$this->port = "143"; | |
$this->sid = ""; | |
$this->tag = 0; | |
$this->_lastTag = $this->tag; | |
$this->currentMailbox = ""; | |
} | |
function __destruct() | |
{ | |
fwrite($this->sock, "A000".$this->getTag()." LOGOUT\r\n"); | |
} | |
function connect($host="", $port="", $ssl=false) | |
{ | |
$this->host = $host; | |
$this->port = $port; | |
if($ssl == true) | |
{ | |
$this->host = "ssl://".$host; | |
} | |
$this->sock = fsockopen($this->host, $this->port, $errno, $errstr); | |
if(!$this->_assumedNextLine('* OK')) | |
{ | |
echo 'FAIL CONNECT'; | |
} | |
} | |
function getTag() | |
{ | |
$this->tag = $this->tag +1; | |
$this->_lastTag = "A000".$this->tag; | |
return $this->tag; | |
} | |
function login($username, $pass) | |
{ | |
fwrite($this->sock, "A000".$this->getTag()." LOGIN ".$username." ".$pass."\r\n"); | |
if(!$this->_assumedNextLine($this->_lastTag)) | |
{ | |
} | |
} | |
function selectMailBox($mailbox) | |
{ | |
fwrite($this->sock, "A000".$this->getTag()." SELECT ".$mailbox."\r\n"); | |
$this->currentMailbox = $mailbox; | |
$this->readResponse($this->_lastTag); | |
} | |
function countMessages() | |
{ | |
fwrite($this->sock, "A000".$this->getTag()." SEARCH ALL\r\n"); | |
$response = $this->readResponse($this->_lastTag); | |
foreach($response as $id) | |
{ | |
if($id[0] == "S") | |
{ | |
$count = count_chars($id, 1); | |
} | |
} | |
return $count[32]; | |
} | |
public function getSubject($msgId) | |
{ | |
if(empty($msgId)) | |
{ | |
return false; | |
} | |
$this->fetch($msgId." BODY[HEADER.FIELDS (subject)]"); | |
$array = array(); | |
while(!$this->readLine($tokens, $this->_lastTag)) | |
{ | |
if(!empty($tokens) && preg_match("/FETCH/", $tokens)) | |
{ | |
$msgId = preg_split("/ /", $tokens, 2); | |
$msgId = $msgId[0]; | |
} | |
if(!empty($tokens) && !preg_match("/FETCH/", $tokens)) | |
{ | |
$array[] = array("id" => $msgId, | |
"subject" => $tokens); | |
} | |
} | |
return mb_decode_mimeheader($array[0]['subject']); | |
} | |
public function getMsgText($msgId="") | |
{ | |
if(empty($msgId)) | |
{ | |
return false; | |
} | |
$this->fetch($msgId." BODY[TEXT]"); | |
$array = array(); | |
$array[] = "=?ISO-8859-1?Q?"; | |
while(!$this->readLine($tokens, $this->_lastTag, $tag)) | |
{ | |
$array[] .= $tag.' '.$tokens; | |
} | |
//Löschen der 1. und letzten Response | |
unset($array[1]); //FETCH | |
unset($array[count($array)]); //Klammer | |
$array = array_values($array); | |
//Array2String | |
$array = implode('', $array); | |
//Decodieren und Zeilenumbrüche setzten | |
return nl2br(mb_decode_mimeheader($array)); | |
} | |
public function getFromOrTo($msgId="", $fromOrTo="") | |
{ | |
if(empty($msgId) || empty($fromOrTo)) | |
{ | |
return false; | |
} | |
$this->fetch($msgId." BODY[HEADER.FIElDS (".$fromOrTo.")]"); | |
while(!$this->readLine($tokens, $this->_lastTag)) | |
{ | |
if(!isset($return) && !preg_match('/FETCH/', $tokens)) | |
{ | |
$return = htmlspecialchars($tokens); | |
} | |
} | |
return mb_decode_mimeheader($return); | |
} | |
private function fetch($command, $param=false) | |
{ | |
if(!$param) | |
{ | |
fwrite($this->sock, "A000".$this->getTag()." FETCH ".$command."\r\n"); | |
} | |
else | |
{ | |
fwrite($this->sock, "A000".$this->getTag()." FETCH ".$command." ".$param."\r\n"); | |
} | |
} | |
function _nextLine() | |
{ | |
$line = @fgets($this->sock); | |
if ($line === false) | |
{ | |
echo 'FAIL!!'; | |
return false; | |
} | |
return $line; | |
} | |
protected function _assumedNextLine($start) | |
{ | |
$line = $this->_nextLine(); | |
return strpos($line, $start) === 0; | |
} | |
public function readLine(&$tokens = array(), $wantedTag = '*', &$tag="") | |
{ | |
$line = $this->_nextTaggedLine($tag); | |
$tokens = $line; | |
$tag = $tag; | |
if(($tag == "A000".$this->tag) || ($tag == $wantedTag)) | |
{ | |
return $tag; | |
} | |
} | |
public function readResponse($tag="*") | |
{ | |
if($tag == "lastTag") | |
{ | |
$tag = $this->_lastTag; | |
} | |
$lines = array(); | |
while (!$this->readLine($tokens, $tag)) { | |
$lines[] = $tokens; | |
} | |
return $lines; | |
} | |
protected function _nextTaggedLine(&$tag) | |
{ | |
$line = $this->_nextLine(); | |
// seperate tag from line | |
@list($tag, $line) = explode(' ', $line, 2); | |
return $line; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment