2009-02-05 15:10:57 -08:00
|
|
|
<?
|
|
|
|
|
App::import('Sanitize');
|
|
|
|
|
class VaultShell extends Shell {
|
2010-03-18 15:30:29 -07:00
|
|
|
/* vault.php - To be run in CLI via Cron. Fetches the mail from a certain mailbox, searches for a CMC Technologies Enquiry Numbeer
|
2009-02-05 15:10:57 -08:00
|
|
|
* then loads the data into the 'emails' table and the attachments in the 'email_attachments' table.
|
|
|
|
|
* Emails are discared or moved to a subfolder once processed
|
2010-03-18 15:30:29 -07:00
|
|
|
*/
|
2009-02-05 15:10:57 -08:00
|
|
|
|
2010-03-18 15:30:29 -07:00
|
|
|
|
|
|
|
|
var $uses = array('Enquiry', 'Email', 'EmailAttachment');
|
|
|
|
|
|
|
|
|
|
function main() {
|
2010-05-05 21:14:50 -07:00
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
|
* Config Variables
|
|
|
|
|
* *****************************************************/
|
2010-04-26 22:41:45 -07:00
|
|
|
$testing = 0; //Whether to actually move the emails. 1=test, 0=production
|
2010-03-18 15:30:29 -07:00
|
|
|
/* Setup Connection to the IMAP server */
|
|
|
|
|
$username = 'vault';
|
|
|
|
|
$password = 'xjdYOsmJWc37'; /* The password for the account to be checked */
|
2010-03-22 16:03:03 -07:00
|
|
|
$email_dir = '/var/www/cakephp/app/emails';
|
2010-04-26 22:45:18 -07:00
|
|
|
|
2010-03-18 15:30:29 -07:00
|
|
|
|
2010-03-22 16:03:03 -07:00
|
|
|
|
|
|
|
|
$ripmime_path = '/usr/local/bin/ripmime';
|
|
|
|
|
|
2010-04-26 18:15:27 -07:00
|
|
|
|
2010-03-18 15:30:29 -07:00
|
|
|
if($testing == 1) {
|
2010-03-22 16:03:03 -07:00
|
|
|
$email_dir = '/Users/karlcordes/Sites/quotenik/app/emails';
|
|
|
|
|
$ripmime_path = '/opt/local/bin/ripmime';
|
2010-05-05 21:14:50 -07:00
|
|
|
$mbox = imap_open("{192.168.0.8:143}INBOX", $username, $password) or die("can't connect: " . imap_last_error());
|
2010-03-18 15:30:29 -07:00
|
|
|
}
|
2010-05-05 21:14:50 -07:00
|
|
|
else {
|
|
|
|
|
$mbox = imap_open("{mail.cmctechnologies.com.au:143/novalidate-cert}INBOX", $username, $password) or die("can't connect: " . imap_last_error());
|
|
|
|
|
echo "woot11";
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-03-18 15:30:29 -07:00
|
|
|
|
|
|
|
|
$MC = imap_check($mbox);
|
|
|
|
|
$number_of_messages = $MC->Nmsgs;
|
|
|
|
|
|
|
|
|
|
echo "Number of messages to Process ".$number_of_messages."\n";
|
|
|
|
|
|
|
|
|
|
if($number_of_messages == 0) {
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
2010-05-05 17:10:24 -07:00
|
|
|
|
2010-05-05 21:14:50 -07:00
|
|
|
$enquiries = $this->Enquiry->find('all', array('recursive'=>0,'fields' => array('Enquiry.title', 'Enquiry.id')));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach ($enquiries as $enq) {
|
|
|
|
|
$enqNumber = $enq['Enquiry']['title'];
|
|
|
|
|
$id = $enq['Enquiry']['id'];
|
|
|
|
|
$enquiryList[$enqNumber] = $id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-03-18 15:30:29 -07:00
|
|
|
|
2010-05-05 17:10:24 -07:00
|
|
|
|
2010-03-18 15:30:29 -07:00
|
|
|
/* Loop through the messages and sort them into ones to be processed or discarded */
|
2010-03-23 17:45:58 -07:00
|
|
|
for ($i=1; $i <= $number_of_messages; $i++) {
|
2010-03-18 15:30:29 -07:00
|
|
|
$this_header = imap_headerinfo($mbox, $i);
|
|
|
|
|
$message = $this->getMessage($mbox, $i, $this_header);
|
|
|
|
|
|
2010-05-05 21:14:50 -07:00
|
|
|
echo "Checking msg number: $i\tSubject: ".$message['subject']."\n";
|
2010-05-05 17:10:24 -07:00
|
|
|
|
2010-05-05 21:14:50 -07:00
|
|
|
$enqID = $this->checkIfValidEnquiry($message['subject'], $enquiryList);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if($enqID != false) {
|
|
|
|
|
echo "Found Message with enquiry ID: ".$enqID." Processing.\n";
|
2010-03-18 15:30:29 -07:00
|
|
|
//Process it and store the message and its attachments.
|
|
|
|
|
|
2010-03-22 16:03:03 -07:00
|
|
|
//Generate a Uniqid for this email.
|
|
|
|
|
$uniqid = $this->getUniqId($email_dir);
|
|
|
|
|
|
2010-03-18 15:30:29 -07:00
|
|
|
$this->Email->create();
|
2010-05-05 21:14:50 -07:00
|
|
|
$this->data['Email']['enquiry_id'] = $enqID;
|
2010-03-18 15:30:29 -07:00
|
|
|
$this->data['Email']['to'] = $message['to'];
|
|
|
|
|
$this->data['Email']['cc'] = $message['cc'];
|
|
|
|
|
$this->data['Email']['from'] = $message['from'];
|
|
|
|
|
$this->data['Email']['date'] = $message['date'];
|
|
|
|
|
$this->data['Email']['subject'] = $message['subject'];
|
|
|
|
|
$this->data['Email']['body'] = "";
|
|
|
|
|
$this->data['Email']['plainbody'] = "";
|
2010-04-26 22:45:18 -07:00
|
|
|
|
2010-03-18 15:30:29 -07:00
|
|
|
$structure = imap_fetchstructure($mbox, $i);
|
|
|
|
|
$attachments = 1;
|
|
|
|
|
if (empty($structure->parts)) { /* A single part message. No attachments and is plain text */
|
|
|
|
|
$this->data['Email']['body'] = imap_body($mbox, $i);
|
|
|
|
|
}
|
|
|
|
|
else {
|
2010-04-26 22:36:18 -07:00
|
|
|
$attachments = $this->fetchBodyAttachments($mbox, $i, $email_dir, $uniqid, $ripmime_path);
|
2010-03-18 15:30:29 -07:00
|
|
|
if($attachments != 1) {
|
|
|
|
|
foreach ($attachments as $attachment) {
|
|
|
|
|
if($attachment['type'] == 'text/html') { //Assuming All HTML attachments are the body of the email
|
2010-03-22 16:03:03 -07:00
|
|
|
if(file_exists($email_dir.'/'.$uniqid.'/'.$attachment['name'])) {
|
2010-04-26 22:36:18 -07:00
|
|
|
$filecontents = file_get_contents($email_dir.'/'.$attachment['name']);
|
|
|
|
|
$size = filesize($email_dir.'/'.$attachment['name']);
|
2010-03-18 15:30:29 -07:00
|
|
|
$this->data['Email']['body'] .= $filecontents;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
if($attachment['type'] == 'text/plain') { //Found plain text
|
|
|
|
|
if(file_exists($email_dir.'/'.$attachment['name'])) {
|
2010-04-26 22:36:18 -07:00
|
|
|
$filecontents = file_get_contents($email_dir.'/'.$attachment['name']);
|
|
|
|
|
$size = filesize($email_dir.'/'.$attachment['name']);
|
2010-03-18 15:30:29 -07:00
|
|
|
$this->data['Email']['plainbody'] .= $filecontents;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-05 21:14:50 -07:00
|
|
|
|
2010-03-18 15:30:29 -07:00
|
|
|
if( ($this->data['Email']['body'] != "") || ($this->data['Email']['plainbody'] != "") || ($attachments != 1) ) {
|
|
|
|
|
|
|
|
|
|
if($this->Email->save($this->data)) {
|
|
|
|
|
$email_id = $this->Email->id;
|
|
|
|
|
if($attachments != 1) {
|
|
|
|
|
foreach ($attachments as $attachment) {
|
2010-04-26 22:36:18 -07:00
|
|
|
if(file_exists($email_dir.'/'.$attachment['name']) != FALSE) {
|
2010-03-18 15:30:29 -07:00
|
|
|
|
|
|
|
|
if( ($attachment['type'] != 'text/html') && ($attachment['type'] != 'multipart/mixed') &&
|
2010-04-26 22:36:18 -07:00
|
|
|
($attachment['type'] != 'multipart/alternative') &&
|
2010-04-26 23:04:38 -07:00
|
|
|
($attachment['type'] != 'multipart/report') &&
|
2010-04-26 23:18:45 -07:00
|
|
|
($attachment['type'] != 'text/plain') &&
|
|
|
|
|
($attachment['type'] != 'text/rfc822-headers') &&
|
|
|
|
|
($attachment['type'] != 'message/delivery-status')
|
|
|
|
|
|
2010-04-26 22:45:18 -07:00
|
|
|
|
|
|
|
|
) {
|
2010-03-18 15:30:29 -07:00
|
|
|
$this->EmailAttachment->create();
|
|
|
|
|
$this->data['EmailAttachment']['email_id'] = $email_id;
|
|
|
|
|
$this->data['EmailAttachment']['name'] = $attachment['name'];
|
|
|
|
|
$this->data['EmailAttachment']['type'] = $attachment['type'];
|
2010-04-26 22:36:18 -07:00
|
|
|
$this->data['EmailAttachment']['size'] = filesize($email_dir.'/'.$attachment['name']);
|
|
|
|
|
$this->data['EmailAttachment']['filename'] = $email_dir.'/'.$attachment['name'];
|
2010-03-18 15:30:29 -07:00
|
|
|
if ($this->EmailAttachment->save($this->data)) {
|
|
|
|
|
echo "Saved file successfully to database\n";
|
|
|
|
|
}
|
|
|
|
|
else {
|
2010-03-23 17:45:58 -07:00
|
|
|
echo "Error saving the file to the DB\n";
|
2010-03-18 15:30:29 -07:00
|
|
|
}
|
|
|
|
|
}
|
2010-05-05 21:14:50 -07:00
|
|
|
|
2010-03-18 15:30:29 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-05-05 21:14:50 -07:00
|
|
|
echo "Email stored in the DB under enquiry ID".$enqID." Will be moved to the stored folder\n";
|
2010-03-18 15:30:29 -07:00
|
|
|
|
2010-03-30 21:09:27 -07:00
|
|
|
if($testing == 0) {
|
2010-04-26 22:45:18 -07:00
|
|
|
imap_mail_move($mbox, $i, 'INBOX/Stored');
|
2010-03-30 21:09:27 -07:00
|
|
|
}
|
2010-03-18 15:30:29 -07:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
echo 'Unable to save the Email\n';
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
echo "Unable to find either HTML or Plaintext body to this email. Ignoring it";
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2010-05-05 21:14:50 -07:00
|
|
|
/* Can't find a valid-looking CMC Enquiry Number. Move the message to the discarded folder
|
|
|
|
|
* I may change this to simply delete the emails. This will do for now, but it's doubling up on the storage for useless files.
|
|
|
|
|
* */
|
2009-07-02 18:11:22 -07:00
|
|
|
|
2010-04-26 22:45:18 -07:00
|
|
|
if($testing == 0 ) {
|
2010-04-26 23:04:38 -07:00
|
|
|
imap_mail_move($mbox, $i, 'INBOX/Discarded');
|
2010-04-26 22:45:18 -07:00
|
|
|
}
|
2010-03-18 15:30:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Finished working with the IMAP server. Make the changes and close the connection */
|
|
|
|
|
imap_expunge($mbox);
|
|
|
|
|
imap_close($mbox);
|
|
|
|
|
}
|
2009-02-05 15:10:57 -08:00
|
|
|
|
2010-03-18 15:30:29 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2010-04-26 18:15:27 -07:00
|
|
|
* Fetches the body and attachments from a MIME encoded email. Uses ripmime to do the decoding
|
|
|
|
|
*
|
2010-03-18 15:30:29 -07:00
|
|
|
*/
|
2009-02-05 15:10:57 -08:00
|
|
|
|
2010-04-26 22:36:18 -07:00
|
|
|
function fetchBodyAttachments($mailbox, $msg_number, $email_dir, $uniqid, $ripmime_path) {
|
2010-03-22 16:03:03 -07:00
|
|
|
|
2010-04-26 22:45:18 -07:00
|
|
|
|
2009-02-11 20:02:05 -08:00
|
|
|
|
2010-04-26 22:36:18 -07:00
|
|
|
$email_file = $email_dir.'/'.$uniqid.'.eml';
|
2010-03-18 15:30:29 -07:00
|
|
|
imap_savebody($mailbox, $email_file, $msg_number);
|
2010-04-26 22:36:18 -07:00
|
|
|
$command = "$ripmime_path -i $email_file -d $email_dir --prefix --paranoid -v --verbose-contenttype";
|
2009-02-05 15:10:57 -08:00
|
|
|
|
2010-03-18 15:30:29 -07:00
|
|
|
$output = array();
|
|
|
|
|
exec($command, $output, $status);
|
2009-02-08 14:21:34 -08:00
|
|
|
|
2010-03-18 15:30:29 -07:00
|
|
|
/* Check the $output array and find the filenames of the attachments */
|
2009-02-08 14:21:34 -08:00
|
|
|
|
2010-03-18 15:30:29 -07:00
|
|
|
if($status == 0) {
|
|
|
|
|
$attachments = array();
|
2009-02-08 14:21:34 -08:00
|
|
|
|
2010-03-18 15:30:29 -07:00
|
|
|
for($i=0, $j=0; $i< count($output); $i++, $j++) {
|
|
|
|
|
$words = explode(' ', $output[$i]);
|
|
|
|
|
$type = explode('=', $words[1]);
|
|
|
|
|
$name = explode('=', $words[2]);
|
2010-04-26 23:18:45 -07:00
|
|
|
|
2010-04-26 23:25:27 -07:00
|
|
|
if($type[1] == "" || $name[1] == "") {
|
2010-04-26 23:22:49 -07:00
|
|
|
continue;
|
2010-04-26 23:18:45 -07:00
|
|
|
}
|
|
|
|
|
|
2010-03-18 15:30:29 -07:00
|
|
|
$attachments[$j]['type'] = $type[1];
|
|
|
|
|
$attachments[$j]['name'] = $name[1];
|
2010-04-26 22:36:18 -07:00
|
|
|
echo "in message number $msg_number ($uniqid): found attachment ".$attachments[$j]['name'].' '.$attachments[$j]['type']."\n";
|
2010-03-18 15:30:29 -07:00
|
|
|
}
|
2010-05-05 21:14:50 -07:00
|
|
|
unlink($email_file);
|
2010-03-18 15:30:29 -07:00
|
|
|
return $attachments;
|
2010-05-05 21:14:50 -07:00
|
|
|
|
2010-03-18 15:30:29 -07:00
|
|
|
}
|
|
|
|
|
else {
|
2010-05-05 21:14:50 -07:00
|
|
|
unlink($email_file);
|
2010-03-18 15:30:29 -07:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-02-08 14:21:34 -08:00
|
|
|
|
2009-02-08 19:59:41 -08:00
|
|
|
|
2010-03-18 15:30:29 -07:00
|
|
|
function getMessage($mbox, $msgnumber, $headers) {
|
|
|
|
|
|
|
|
|
|
$subject = $headers->subject;
|
2010-05-05 21:14:50 -07:00
|
|
|
//$subject = iconv_mime_decode($subject, 0, "ISO-8859-1//IGNORE");
|
|
|
|
|
// $subject = mb_convert_encoding($subject, "ISO-8859-1");
|
|
|
|
|
// $subject = mb_convert_encoding($subject, "UTF-8");
|
|
|
|
|
$subject = mb_decode_mimeheader($subject);
|
|
|
|
|
|
2010-03-18 15:30:29 -07:00
|
|
|
$date = $headers->date;
|
|
|
|
|
$recipients = $this->getRecipients($headers);
|
|
|
|
|
$message['subject'] = $subject;
|
|
|
|
|
$message['date'] = $date;
|
|
|
|
|
$message['to'] = $recipients['to'];
|
|
|
|
|
$message['from'] = $recipients['from'];
|
|
|
|
|
$message['cc'] = $recipients['cc'];
|
|
|
|
|
return $message;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
/* get Recipients from the headers of an email */
|
|
|
|
|
function getRecipients($headers) {
|
|
|
|
|
|
|
|
|
|
$recipients = array();
|
|
|
|
|
$recipients['to'] = "";
|
|
|
|
|
$recipients['from'] = "";
|
|
|
|
|
$recipients['cc'] = "";
|
|
|
|
|
|
|
|
|
|
if(isset($headers->to)) {
|
|
|
|
|
$to = $headers->to;
|
|
|
|
|
if(count($to) > 0) {
|
|
|
|
|
foreach ($to as $id => $object) {
|
|
|
|
|
$recipients['to'] .= "$object->mailbox@$object->host ";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(isset($headers->cc)) {
|
|
|
|
|
$cc = $headers->cc;
|
|
|
|
|
if(count($cc) > 0) {
|
|
|
|
|
foreach ($cc as $id => $object) {
|
|
|
|
|
$recipients['cc'] .= "$object->mailbox@$object->host ";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(isset($headers->from)) {
|
|
|
|
|
$from = $headers->from;
|
|
|
|
|
if(count($from) > 0) {
|
|
|
|
|
foreach ($from as $id => $object) {
|
|
|
|
|
$recipients['from'] .= "$object->mailbox@$object->host";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $recipients;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getBody($mbox, $msgnumber) {
|
|
|
|
|
$structure = imap_fetchstructure($mbox, $msgnumber);
|
|
|
|
|
if (!empty($structure->parts)) {
|
|
|
|
|
$body = $this->getParts($structure->parts, $mbox, $msgnumber);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$body = imap_body($mbox, $msgnumber); /* Or get the plaintext */
|
|
|
|
|
}
|
|
|
|
|
return $body;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-05 21:14:50 -07:00
|
|
|
function checkIfValidEnquiry($subject, &$enqList) {
|
2010-03-18 15:30:29 -07:00
|
|
|
$subject = iconv_mime_decode($subject, 0, "ISO-8859-1");
|
|
|
|
|
|
|
|
|
|
$output = array();
|
2009-02-26 16:58:33 -08:00
|
|
|
// $decoded_subject = iconv_mime_decode($subject, 2, "ISO-8859-1");
|
2009-02-05 15:10:57 -08:00
|
|
|
|
2010-03-18 15:30:29 -07:00
|
|
|
preg_match("/CMC\d+([NVQWSOT]|ACT|NT)E\d+-\d+/", $subject, $output);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(isset($output[0])) { //Found a valid-looking Enquiry Number
|
|
|
|
|
$fetched_enquirynumber = $output[0];
|
2010-05-05 21:14:50 -07:00
|
|
|
|
|
|
|
|
if(array_key_exists($fetched_enquirynumber, $enqList)) { //check if it actually exists.
|
|
|
|
|
$enqid = $enqList[$fetched_enquirynumber];
|
|
|
|
|
|
|
|
|
|
return $enqid;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return false;
|
2010-03-18 15:30:29 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
2010-05-05 21:14:50 -07:00
|
|
|
return false;
|
2010-03-18 15:30:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-04-26 22:45:18 -07:00
|
|
|
|
2010-03-22 16:03:03 -07:00
|
|
|
/**
|
|
|
|
|
* Generate a uniq id.
|
2010-04-26 22:45:18 -07:00
|
|
|
*
|
2010-03-22 16:03:03 -07:00
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
function getUniqId($email_dir) {
|
|
|
|
|
$uniqid = uniqid(null,TRUE);
|
2010-04-26 22:45:18 -07:00
|
|
|
|
2010-03-22 16:03:03 -07:00
|
|
|
return $uniqid;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-29 22:35:07 -07:00
|
|
|
}
|
2010-03-18 15:30:29 -07:00
|
|
|
?>
|