214 lines
6.2 KiB
PHP
214 lines
6.2 KiB
PHP
|
|
<?php
|
|||
|
|
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
|
|||
|
|
set_time_limit(9999999999);
|
|||
|
|
|
|||
|
|
$db = $GLOBALS['db'];
|
|||
|
|
|
|||
|
|
$eaddr = array();
|
|||
|
|
$r=$db->query("SELECT id, email_user FROM inbound_email WHERE id NOT IN ('813761a2-8711-3b3c-7a95-4f0acecfd08b')");
|
|||
|
|
while ($w = $db->fetchByAssoc($r)) {
|
|||
|
|
$eaddr[$w['email_user']] = $w['id'];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
$ie_id='ec629543-24b1-3326-7f01-5048c8813921';
|
|||
|
|
$cache_query = "SELECT senddate, imap_uid, alllist_text_id FROM email_cache WHERE deleted='0' AND mbox='INBOX' AND ie_id ='".$ie_id."' AND (alllist_text_id IS NULL OR alllist_text_id='') AND datediff(senddate, now())>-7 ORDER BY senddate ASC;";
|
|||
|
|
|
|||
|
|
$result = $db->query($cache_query);
|
|||
|
|
$messages = array();
|
|||
|
|
while ($row = $db->fetchByAssoc($result)) {
|
|||
|
|
$messages[$row['imap_uid']] = $row['senddate'];
|
|||
|
|
}
|
|||
|
|
if (count($messages)==0) continue; //don't connect with server
|
|||
|
|
|
|||
|
|
|
|||
|
|
$ie = new InboundEmail();
|
|||
|
|
$ie->retrieve($ie_id);
|
|||
|
|
$ie->connectMailserver();
|
|||
|
|
$connection = $ie->conn;
|
|||
|
|
if (!isset($connection)) {print_r(imap_errors()); exit;}
|
|||
|
|
//loop throught cache messages and save it if is unsaved
|
|||
|
|
$add =0;
|
|||
|
|
$i==0;
|
|||
|
|
|
|||
|
|
foreach ($messages as $k=>$v) {
|
|||
|
|
$messageNo = $ie->getMsgNo($k);
|
|||
|
|
if ($messageNo<1) {
|
|||
|
|
//echo "Can't find message (date: $v)<br>";
|
|||
|
|
continue;
|
|||
|
|
} //message deleted from server
|
|||
|
|
$add++;
|
|||
|
|
$senddate = $v;
|
|||
|
|
$attachments = array();
|
|||
|
|
$structure = imap_fetchstructure($connection, $messageNo);
|
|||
|
|
|
|||
|
|
|
|||
|
|
//if ($add == 5) return;
|
|||
|
|
|
|||
|
|
$flattenedParts = flattenParts($structure->parts);
|
|||
|
|
|
|||
|
|
|
|||
|
|
//get fromadd & subject
|
|||
|
|
$header = imap_header($connection, $messageNo);
|
|||
|
|
//$fromaddr = $header->from[0]->mailbox.'@'.$header->from[0]->host;
|
|||
|
|
$subject = $header->subject;
|
|||
|
|
//echo 'Temat: '.$subjest.'<br>';
|
|||
|
|
//continue;
|
|||
|
|
//get from addresses
|
|||
|
|
|
|||
|
|
$from_addr=array();
|
|||
|
|
foreach ($header->from as $v) {
|
|||
|
|
$from_addr[] = $v->mailbox.'@'.$v->host;
|
|||
|
|
}
|
|||
|
|
//get to address
|
|||
|
|
$to_addr = array();
|
|||
|
|
foreach ($header->to as $v) {
|
|||
|
|
$to_addr[] = $v->mailbox.'@'.$v->host;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
foreach ($to_addr as $v)
|
|||
|
|
if ($v!='')
|
|||
|
|
if ($eaddr[$v]) save($connection, $messageNo, $flattenedParts, $eaddr[$v], 'in', $db, $k, $senddate);
|
|||
|
|
|
|||
|
|
foreach ($from_addr as $v)
|
|||
|
|
if ($v!='')
|
|||
|
|
if ($eaddr[$v]) save($connection, $messageNo, $flattenedParts, $eaddr[$v], 'out', $db, $k, $senddate);
|
|||
|
|
|
|||
|
|
//if ($add ==20) return;
|
|||
|
|
//return;;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
//echo 'Uploaded (user: '.$ie_id.'): '.$add.'<br>';
|
|||
|
|
imap_close($connection);
|
|||
|
|
|
|||
|
|
echo 'Wiadomości odebrane zaktualizowane!';
|
|||
|
|
|
|||
|
|
return true;
|
|||
|
|
|
|||
|
|
function save($connection, $messageNo, $flattenedParts, $ie_id, $in_out, $db, $k, $senddate) {
|
|||
|
|
foreach($flattenedParts as $partNumber => $part) {
|
|||
|
|
switch($part->type) {
|
|||
|
|
case 0:
|
|||
|
|
$message = getPart($connection, $messageNo, $partNumber, $part->encoding);
|
|||
|
|
$charset = $part->parameters[0]->value;
|
|||
|
|
if (($charset!='') && ($charset!="UTF-8"));
|
|||
|
|
$message=addslashes(htmlspecialchars(iconv($charset,"UTF-8",$message)));
|
|||
|
|
break;
|
|||
|
|
case 1:
|
|||
|
|
break;
|
|||
|
|
case 2:
|
|||
|
|
break;
|
|||
|
|
case 3: // application
|
|||
|
|
case 4: // audio
|
|||
|
|
case 5: // image
|
|||
|
|
case 6: // video
|
|||
|
|
case 7: // other
|
|||
|
|
$filename = getFilenameFromPart($part);
|
|||
|
|
if($filename) {
|
|||
|
|
$attachment = getPart($connection, $messageNo, $partNumber, $part->encoding);
|
|||
|
|
$a=array();
|
|||
|
|
$a['filename'] = charsetEncode($filename);
|
|||
|
|
$a['content'] = $attachment;
|
|||
|
|
$attachments[] = $a;
|
|||
|
|
}
|
|||
|
|
break;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
$id = create_guid();
|
|||
|
|
|
|||
|
|
//echo "Before save: $ie_id<br>";
|
|||
|
|
if (count($attachments)!=0)
|
|||
|
|
$path = saveAttachments($ie_id, $id, $attachments);
|
|||
|
|
|
|||
|
|
$db->query("INSERT INTO email_alllist_texts VALUES('".$id."','".$message."','".$path."','".$k."','".$senddate."','".$ie_id."','".date("Y-m-d H:i:s")."','".$in_out."')");
|
|||
|
|
//add info to email_cache
|
|||
|
|
$r = $db->query("UPDATE email_cache SET alllist_text_id='".$id."', alllist_ie_id='".$ie_id."' WHERE ie_id='ec629543-24b1-3326-7f01-5048c8813921' AND imap_uid='".$k."' AND senddate='".$senddate."'");
|
|||
|
|
if (!isset($r)) echo 'error<br>';
|
|||
|
|
//if ($i=50) break;
|
|||
|
|
//$i++;
|
|||
|
|
}
|
|||
|
|
function charsetEncode($string) {
|
|||
|
|
$a = imap_mime_header_decode($string);
|
|||
|
|
$result='';
|
|||
|
|
foreach ($a as $v)
|
|||
|
|
if ($v->charset!='default')
|
|||
|
|
$result.=iconv($v->charset,"UTF-8", $v->text).' ';
|
|||
|
|
if ($result!='') return $result; else return $string;
|
|||
|
|
}
|
|||
|
|
function saveAttachments($ie_id, $id, $attachments) {
|
|||
|
|
$path='';
|
|||
|
|
if ((!$attachments) && (count($attachments)==0)) exit;
|
|||
|
|
foreach ($attachments as $v) {
|
|||
|
|
$file_name = $v['filename'];
|
|||
|
|
$content = $v['content'];
|
|||
|
|
mkdir('./modules/Emails/alllist_upload/'.$ie_id);
|
|||
|
|
$dir = $ie_id.'/'.$id;
|
|||
|
|
mkdir('./modules/Emails/alllist_upload/'.$dir);
|
|||
|
|
$fp = fopen('modules/Emails/alllist_upload/'.$dir.'/'.$file_name, 'wb');
|
|||
|
|
if (!fwrite($fp, $content)==0) {
|
|||
|
|
$path.= $dir.'/'.$file_name.';';
|
|||
|
|
fwrite($fp, $content);
|
|||
|
|
fclose($fp);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return $path;
|
|||
|
|
}
|
|||
|
|
function getPart($connection, $messageNumber, $partNumber, $encoding) {
|
|||
|
|
|
|||
|
|
$data = imap_fetchbody($connection, $messageNumber, $partNumber);
|
|||
|
|
switch($encoding) {
|
|||
|
|
case 0: return $data; // 7BIT
|
|||
|
|
case 1: return $data; // 8BIT
|
|||
|
|
case 2: return $data; // BINARY
|
|||
|
|
case 3: return base64_decode($data); // BASE64
|
|||
|
|
case 4: return quoted_printable_decode($data); // QUOTED_PRINTABLE
|
|||
|
|
case 5: return $data; // OTHER
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
function flattenParts($messageParts, $flattenedParts = array(), $prefix = '', $index = 1, $fullPrefix = true) {
|
|||
|
|
foreach($messageParts as $part) {
|
|||
|
|
$flattenedParts[$prefix.$index] = $part;
|
|||
|
|
if(isset($part->parts)) {
|
|||
|
|
if($part->type == 2) {
|
|||
|
|
$flattenedParts = flattenParts($part->parts, $flattenedParts, $prefix.$index.'.', 0, false);
|
|||
|
|
}
|
|||
|
|
elseif($fullPrefix) {
|
|||
|
|
$flattenedParts = flattenParts($part->parts, $flattenedParts, $prefix.$index.'.');
|
|||
|
|
}
|
|||
|
|
else {
|
|||
|
|
$flattenedParts = flattenParts($part->parts, $flattenedParts, $prefix);
|
|||
|
|
}
|
|||
|
|
unset($flattenedParts[$prefix.$index]->parts);
|
|||
|
|
}
|
|||
|
|
$index++;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return $flattenedParts;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function getFilenameFromPart($part) {
|
|||
|
|
|
|||
|
|
$filename = '';
|
|||
|
|
|
|||
|
|
if($part->ifdparameters) {
|
|||
|
|
foreach($part->dparameters as $object) {
|
|||
|
|
if(strtolower($object->attribute) == 'filename') {
|
|||
|
|
$filename = $object->value;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if(!$filename && $part->ifparameters) {
|
|||
|
|
foreach($part->parameters as $object) {
|
|||
|
|
if(strtolower($object->attribute) == 'name') {
|
|||
|
|
$filename = $object->value;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return $filename;
|
|||
|
|
}
|
|||
|
|
?>
|