Vault 2.0 finalised. Significantly improved speed

This commit is contained in:
Karl Cordes 2010-05-06 14:14:50 +10:00
parent 6889ae3739
commit 7351d30cea

View file

@ -10,9 +10,10 @@ class VaultShell extends Shell {
var $uses = array('Enquiry', 'Email', 'EmailAttachment');
function main() {
/******************************************************
* Config Variables
* *****************************************************/
/******************************************************
* Config Variables
* *****************************************************/
$testing = 0; //Whether to actually move the emails. 1=test, 0=production
/* Setup Connection to the IMAP server */
$username = 'vault';
@ -27,10 +28,16 @@ class VaultShell extends Shell {
if($testing == 1) {
$email_dir = '/Users/karlcordes/Sites/quotenik/app/emails';
$ripmime_path = '/opt/local/bin/ripmime';
$mbox = imap_open("{192.168.0.8:143}INBOX", $username, $password) or die("can't connect: " . imap_last_error());
}
else {
$mbox = imap_open("{mail.cmctechnologies.com.au:143/novalidate-cert}INBOX", $username, $password) or die("can't connect: " . imap_last_error());
echo "woot11";
}
$mbox = imap_open("{mail.cmctechnologies.com.au:143/novalidate-cert}INBOX", $username, $password) or die("can't connect: " . imap_last_error());
$MC = imap_check($mbox);
$number_of_messages = $MC->Nmsgs;
@ -40,7 +47,17 @@ class VaultShell extends Shell {
exit(0);
}
$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;
}
/* Loop through the messages and sort them into ones to be processed or discarded */
@ -48,19 +65,20 @@ class VaultShell extends Shell {
$this_header = imap_headerinfo($mbox, $i);
$message = $this->getMessage($mbox, $i, $this_header);
//echo "Checking msg number: $i \tSubject: ".$message['subject']."\n";
$enquiry = $this->checkIfValidEnquiry($message['subject'], $testing);
echo "Checking msg number: $i\tSubject: ".$message['subject']."\n";
if($enquiry) {
//echo "Found Enquiry number: ".$enquiry['Enquiry']['title']." Processing.\n";
$enqID = $this->checkIfValidEnquiry($message['subject'], $enquiryList);
if($enqID != false) {
echo "Found Message with enquiry ID: ".$enqID." Processing.\n";
//Process it and store the message and its attachments.
//Generate a Uniqid for this email.
$uniqid = $this->getUniqId($email_dir);
$this->Email->create();
$this->data['Email']['enquiry_id'] = $enquiry['Enquiry']['id'];
$this->data['Email']['enquiry_id'] = $enqID;
$this->data['Email']['to'] = $message['to'];
$this->data['Email']['cc'] = $message['cc'];
$this->data['Email']['from'] = $message['from'];
@ -99,7 +117,7 @@ class VaultShell extends Shell {
}
}
//Sanitize::clean($this->data);
if( ($this->data['Email']['body'] != "") || ($this->data['Email']['plainbody'] != "") || ($attachments != 1) ) {
if($this->Email->save($this->data)) {
@ -130,36 +148,28 @@ class VaultShell extends Shell {
echo "Error saving the file to the DB\n";
}
}
//unlink($email_dir.'/'.$attachment['name']); #Delete this attachment now we're done with it.
}
}
}
echo "Email stored in the DB under enquiry ".$enquiry['Enquiry']['title']." Will be moved to the stored folder\n";
//$stored_msgs[] = imap_uid($mbox,$i);
echo "Email stored in the DB under enquiry ID".$enqID." Will be moved to the stored folder\n";
if($testing == 0) {
imap_mail_move($mbox, $i, 'INBOX/Stored');
}
}
else {
echo 'Unable to save the Email\n';
}
} else {
echo "Unable to find either HTML or Plaintext body to this email. Ignoring it";
}
} else {
/* 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.
* */
/* 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.
* */
if($testing == 0 ) {
imap_mail_move($mbox, $i, 'INBOX/Discarded');
}
}
@ -167,12 +177,9 @@ class VaultShell extends Shell {
}
/* Finished working with the IMAP server. Make the changes and close the connection */
imap_expunge($mbox);
imap_close($mbox);
}
@ -211,9 +218,12 @@ class VaultShell extends Shell {
$attachments[$j]['name'] = $name[1];
echo "in message number $msg_number ($uniqid): found attachment ".$attachments[$j]['name'].' '.$attachments[$j]['type']."\n";
}
unlink($email_file);
return $attachments;
}
else {
unlink($email_file);
return 1;
}
}
@ -222,7 +232,11 @@ class VaultShell extends Shell {
function getMessage($mbox, $msgnumber, $headers) {
$subject = $headers->subject;
$subject = iconv_mime_decode($subject, 0, "ISO-8859-1//IGNORE");
//$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);
$date = $headers->date;
$recipients = $this->getRecipients($headers);
$message['subject'] = $subject;
@ -283,7 +297,7 @@ class VaultShell extends Shell {
return $body;
}
function checkIfValidEnquiry($subject, $testing) {
function checkIfValidEnquiry($subject, &$enqList) {
$subject = iconv_mime_decode($subject, 0, "ISO-8859-1");
$output = array();
@ -294,15 +308,18 @@ class VaultShell extends Shell {
if(isset($output[0])) { //Found a valid-looking Enquiry Number
$fetched_enquirynumber = $output[0];
// echo $fetched_enquirynumber."\n";
$enquiry = $this->Enquiry->findByTitle($fetched_enquirynumber);
if($enquiry) {
return $enquiry;
if(array_key_exists($fetched_enquirynumber, $enqList)) { //check if it actually exists.
$enqid = $enqList[$fetched_enquirynumber];
return $enqid;
}
else {
return false;
}
}
else {
return FALSE;
return false;
}
}