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