Файл: _rootinc/rpimap.class.inc.php
Строк: 137
<?
class RPIMAP
{
public $protocol = null; # pop3, imap
public $host = null; # IP or Domain
public $port = null; # 110 - pop3, 995 - ssl pop3, 143 - imap, 993 - ssl imap
public $secure = null; # ssl, tls
public $user = null; # Login
public $password = null; # Password
public $error = null;
public function init($protocol, $host, $port, $secure, $user, $password)
{
$this->protocol = $protocol;
$this->host = $host;
$this->port = $port;
$this->secure = $secure;
$this->user = $user;
$this->password = $password;
}
public function connect()
{
$server = $this->host.":".$this->port."/".$this->protocol;
if ($this->secure == "ssl") { $server .= "/ssl/novalidate-cert"; }
else if ($this->secure == "tls") { $server .= "/tls/novalidate-cert"; }
else { $server .= "/notls"; }
$mbox = imap_open("{".$server."}INBOX", $this->user, $this->password);
if ($mbox) {
return $mbox;
}
else {
$this->error = "Connect: ".imap_last_error();
return false;
}
}
public function getMessages($delete = 1, $saveFiles = 0, $ignoreEmails)
{
$mbox = $this->connect();
if ($mbox) {
$msg_seens = array();
$search = imap_search($mbox, 'UNSEEN');
while (list($k,$i) = @each($search)) {
$header = explode("n", imap_fetchheader($mbox, $i));
$head = array();
foreach ($header as $line) {
preg_match("/^([^:]*): (.*)/ui", $line, $arg);
$head[$arg[1]] = $arg[2];
}
// получаем e-mail отправителя
$from = imap_utf8($head["From"]);
if (preg_match("/<([^>]+)>/ui",$from,$arr)) { $from = $arr[1]; }
$from = trim($from);
// смотрим нет ли отправителя в списке игнорируемых эмейлов
if (@in_array($from,$ignoreEmails)) {
if ($delete) {
imap_delete($mbox, $i);
}
continue;
}
// если не указано удалять, то смотрим есть ли клиент с таким эмейлом. если нету - не помечаем сообщение как прочитанное.
if (!$delete) {
$billUser = GetUserByEmail($from);
if ($billUser->id) {
$msg_seens[] = $i;
}
# else {
# continue;
# }
}
$res[$i]["from"] = $from;
// получаем приоритет
$priority = $head["X-Priority"];
preg_match("/^s?(d+).*/ui",$priority,$arr);
$res[$i]["priority"] = $arr[1];
// получаем тему сообщения
$res[$i]["subject"] = imap_utf8($head["Subject"]);
$structure = imap_fetchstructure($mbox, $i);
$parts = $this->create_part_array($structure);
$textFinded = 0;
$filesFinded = 0;
while (list($index,$part) = @each($parts)) {
# $part[part_number]
# $part[part_object]->type
if (!$textFinded and $part[part_object]->type == "0" and $part[part_object]->ifdisposition == "0") {
$subtype = strtolower($part[part_object]->subtype);
$charset = strtolower($part[part_object]->parameters[0]->value);
$encoding = $part[part_object]->encoding;
$body = imap_fetchbody($mbox,$i,$part[part_number]);
if ($encoding == 1) { $body = imap_8bit($body); $body = quoted_printable_decode($body); }
elseif ($encoding == 2) { $body = imap_binary($body); $body = imap_base64($body); }
elseif ($encoding == 3) { $body = imap_base64($body); }
elseif ($encoding == 4) { $body = quoted_printable_decode($body); }
if ($subtype == "html") { $body = html2txt($body); }
$body = iconv("$charset","utf-8",$body);
$res[$i]["message"] = $body;
$textFinded = 1;
}
else if ($saveFiles and $part[part_object]->ifdisposition == "1" and strtoupper($part[part_object]->disposition) == "ATTACHMENT") {
$file_encoding = $part[part_object]->encoding;
$file_body = imap_fetchbody($mbox,$i,$part[part_number]);
if ($file_encoding == 1) { $file_body = imap_8bit($file_body); $file_body = quoted_printable_decode($file_body); }
elseif ($file_encoding == 2) { $file_body = imap_binary($file_body); $file_body = imap_base64($file_body); }
elseif ($file_encoding == 3) { $file_body = imap_base64($file_body); }
elseif ($file_encoding == 4) { $file_body = quoted_printable_decode($file_body); }
$res[$i][files][$filesFinded][name] = imap_utf8($part[part_object]->dparameters[0]->value);
$res[$i][files][$filesFinded][file] = $file_body;
$filesFinded++;
}
}
if ($delete) {
imap_delete($mbox, $i);
}
}
if ($delete) {
imap_expunge($mbox);
} else {
$msg_seens = join(',',$msg_seens);
imap_setflag_full($mbox, $msg_seens, "\Seen");
}
imap_close($mbox);
return $res;
}
}
public function create_part_array($struct) {
if (sizeof($struct->parts) > 0) { // There some sub parts
foreach ($struct->parts as $count => $part) {
$this->add_part_to_array($part, ($count+1), $part_array);
}
}else{ // Email does not have a seperate mime attachment for text
$part_array[] = array('part_number' => '1', 'part_object' => $struct);
}
return $part_array;
}
// Sub function for create_part_array(). Only called by create_part_array() and itself.
function add_part_to_array($obj, $partno, &$part_array) {
$part_array[] = array('part_number' => $partno, 'part_object' => $obj);
if ($obj->type == 2) { // Check to see if the part is an attached email message, as in the RFC-822 type
//print_r($obj);
if (sizeof($obj->parts) > 0) { // Check to see if the email has parts
foreach ($obj->parts as $count => $part) {
// Iterate here again to compensate for the broken way that imap_fetchbody() handles attachments
if (sizeof($part->parts) > 0) {
foreach ($part->parts as $count2 => $part2) {
$this->add_part_to_array($part2, $partno.".".($count2+1), $part_array);
}
}else{ // Attached email does not have a seperate mime attachment for text
$part_array[] = array('part_number' => $partno.'.'.($count+1), 'part_object' => $obj);
}
}
}else{ // Not sure if this is possible
$part_array[] = array('part_number' => $prefix.'.1', 'part_object' => $obj);
}
}else{ // If there are more sub-parts, expand them out.
if (sizeof($obj->parts) > 0) {
foreach ($obj->parts as $count => $p) {
$this->add_part_to_array($p, $partno.".".($count+1), $part_array);
}
}
}
}
}
?>