Vault fixes

This commit is contained in:
Karl Cordes 2011-09-05 14:22:28 +10:00
parent ef42c5e668
commit 6098ff2ad8

View file

@ -45,354 +45,343 @@ class VaultShell extends Shell {
}
while(true) {
$emails = scandir($vault_dir);
$processed = scandir($processed_dir);
$new = array_diff($emails, $processed);
if(count($new) == 0) {
echo "\rWaiting for new messages";
sleep(60);
/* Find the strings we want to look for in the subjects. Build arrays
* using the string as the keys, IDs as the value
*/
$enquiries = $this->Enquiry->find('all', array('recursive'=>0,
'fields' => array('Enquiry.title', 'Enquiry.id')));
$invoices = $this->Invoice->find('all', array('recursive'=>0, '
fields'=>array('Invoice.id', 'Invoice.title')));
$purchaseOrders = $this->PurchaseOrder->find('all',
array('recursive'=>0,
'fields'=>array('PurchaseOrder.id', 'PurchaseOrder.title')));
$users = $this->User->find('all', array('recursive'=>0,
'fields'=>array('User.id', 'User.email')));
$jobs = $this->Job->find('all', array('recursive'=>0, 'fields'=>array('Job.id', 'Job.title')));
$enquiryMap = $this->makeMap($enquiries,'Enquiry', 'title');
$invoiceMap = $this->makeMap($invoices, 'Invoice', 'title');
$poMap = $this->makeMap($purchaseOrders, 'PurchaseOrder', 'title');
$userMap = $this->makeMap($users, 'User', 'email');
$jobMap = $this->makeMap($jobs, 'Job', 'title');
/**
* Loop through the messages.
*
* Procedure:
*
* 1. Check to see if the recipients (CC, or to), or Sender is in the $userMap.
* 1a. If one of these is true, we need to capture this email. Else, delete it.
*
* 2. This email is either to/from/CC a user in the system.
* We need to associate it with the right PO/Invoice/Enquiries etc.
* Check the subject for patterns that match Identifiers.
*
*/
foreach($new as $email_filename) {
echo "Handling $email_filename\n";
if( ($email_filename == '.') || ($email_filename == '..') ) {
continue;
}
if(!$this->isEmlFile($vault_dir, $email_filename)) {
echo "Incorrect file extension. Skipping this file";
continue;
}
/* Find the strings we want to look for in the subjects. Build arrays
* using the string as the keys, IDs as the value
*/
$enquiries = $this->Enquiry->find('all', array('recursive'=>0,
'fields' => array('Enquiry.title', 'Enquiry.id')));
$invoices = $this->Invoice->find('all', array('recursive'=>0, '
fields'=>array('Invoice.id', 'Invoice.title')));
$purchaseOrders = $this->PurchaseOrder->find('all',
array('recursive'=>0,
'fields'=>array('PurchaseOrder.id', 'PurchaseOrder.title')));
$users = $this->User->find('all', array('recursive'=>0,
'fields'=>array('User.id', 'User.email')));
$jobs = $this->Job->find('all', array('recursive'=>0, 'fields'=>array('Job.id', 'Job.title')));
$enquiryMap = $this->makeMap($enquiries,'Enquiry', 'title');
$invoiceMap = $this->makeMap($invoices, 'Invoice', 'title');
$poMap = $this->makeMap($purchaseOrders, 'PurchaseOrder', 'title');
$userMap = $this->makeMap($users, 'User', 'email');
$jobMap = $this->makeMap($jobs, 'Job', 'title');
/**
* Loop through the messages.
*
* Procedure:
*
* 1. Check to see if the recipients (CC, or to), or Sender is in the $userMap.
* 1a. If one of these is true, we need to capture this email. Else, delete it.
*
* 2. This email is either to/from/CC a user in the system.
* We need to associate it with the right PO/Invoice/Enquiries etc.
* Check the subject for patterns that match Identifiers.
*
*/
foreach($new as $email_filename) {
echo "Handling $email_filename\n";
if( ($email_filename == '.') || ($email_filename == '..') ) {
continue;
}
if(!$this->isEmlFile($vault_dir, $email_filename)) {
echo "Incorrect file extension. Skipping this file";
continue;
}
/*if(file_exists($processed_dir."/".$email_filename)) {
/*if(file_exists($processed_dir."/".$email_filename)) {
continue;
}*/
$content = file_get_contents($vault_dir."/".$email_filename);
$content = file_get_contents($vault_dir."/".$email_filename);
if(!$content) {
echo "No Content Found. Ignoring this email\n";
if(!$this->makeSymlink($email_filename, $vault_dir, $processed_dir)) {
die("Unable to make symlink to process this email Critical error: {$email_filename}");
}
else {
continue;
}
if(!$content) {
echo "No Content Found. Ignoring this email\n";
if(!$this->makeSymlink($email_filename, $vault_dir, $processed_dir)) {
die("Unable to make symlink to process this email Critical error: {$email_filename}");
}
else {
continue;
}
}
$content = str_replace("\r", "", $content);
$headers = imap_rfc822_parse_headers($content);
$recipients = $this->getRecipients($headers, true);
$unix_time = strtotime($headers->date); //Used for filing attachments into month-year folders.
//MM-YYYY used For lack of a better option really.
$saveThis = false; //Set to true, if To,From,CC is a Known User.
$fromKnownUser = false;
foreach($recipients['to'] as $email) {
$saveThis = $this->userExists($email, $userMap);
}
foreach($recipients['from'] as $email) {
$saveThis = $this->userExists($email, $userMap);
$fromKnownUser = $saveThis;
}
foreach($recipients['cc'] as $email) {
$saveThis = $this->userExists($email, $userMap);
}
if(!isset($headers->subject)) { //Emails without a subject are not welcome. Skip it.
echo "No Subject Found. Ignoring this email\n";
if(!$this->makeSymlink($email_filename, $vault_dir, $processed_dir)) {
die("Unable to make symlink to process this email Critical error: {$email_filename}");
}
else {
continue;
}
}
$content = str_replace("\r", "", $content);
$subjDecoded = imap_mime_header_decode($headers->subject);
$headers = imap_rfc822_parse_headers($content);
$foundIdent = false;
$recipients = $this->getRecipients($headers, true);
foreach($subjDecoded as $sub) {
$found_enquiries = $this->checkValidIdentifier($sub->text, $enquiryMap, 'enquiry');
$foundIdent = $this->foundIdentifier($found_enquiries);
$found_invoices = $this->checkValidIdentifier($sub->text, $invoiceMap, 'invoice');
$foundIdent = $this->foundIdentifier($found_invoices);
$unix_time = strtotime($headers->date); //Used for filing attachments into month-year folders.
//MM-YYYY used For lack of a better option really.
$found_pos = $this->checkValidIdentifier($sub->text, $poMap, 'purchaseorder');
$foundIdent = $this->foundIdentifier($found_pos);
$saveThis = false; //Set to true, if To,From,CC is a Known User.
$fromKnownUser = false;
$found_jobs = $this->checkValidIdentifier($sub->text, $jobMap, 'job');
$foundIdent = $this->foundIdentifier($found_jobs);
}
//We're going to save this Email.
//Lets grab the attachments (if any) then create the appropiate Objects.
foreach($recipients['to'] as $email) {
$saveThis = $this->userExists($email, $userMap);
}
foreach($recipients['from'] as $email) {
$saveThis = $this->userExists($email, $userMap);
$fromKnownUser = $saveThis;
}
if($fromKnownUser == true || $saveThis == true || $foundIdent == true) {
foreach($recipients['cc'] as $email) {
$saveThis = $this->userExists($email, $userMap);
}
$recipientsIDs['to'] = array();
$recipientsIDs['from'] = array();
$recipientsIDs['cc'] = array();
/**
* Loop over each recipient email found. Build an array containing the User IDs of that user.
*
* If that email doesn't have a corresponding User ID. We need to create a new User for this email address.
*
*
*/
foreach($recipients as $type => $recipientArray) {
foreach($recipientArray as $recEmail) {
if(!isset($headers->subject)) { //Emails without a subject are not welcome. Skip it.
echo "No Subject Found. Ignoring this email\n";
if(!$this->makeSymlink($email_filename, $vault_dir, $processed_dir)) {
die("Unable to make symlink to process this email Critical error: {$email_filename}");
}
else {
continue;
}
$recEmailLC = strtolower($recEmail);
}
if(isset($userMap[$recEmailLC])) {
$recipientsIDs[$type][] = $userMap[$recEmailLC]; //Existing User ID.
}
else {
//$recipientsIDs[$type][] = "NEW User: $recEmail"; //Need to make a new user ID.
echo "Making a new User for: '{$recEmailLC}'\n";
$newUser['User'] = array(
'type' => 'contact',
'email' => $recEmailLC,
'by_vault' => 1
);
$subjDecoded = imap_mime_header_decode($headers->subject);
$this->User->create();
if($this->User->save($newUser, false)) {
$newID = $this->User->id;
echo "New User '{$recEmail}' Added with ID: {$newID}\n";
$recipientsIDs[$type][] = $newID;
$foundIdent = false;
foreach($subjDecoded as $sub) {
$found_enquiries = $this->checkValidIdentifier($sub->text, $enquiryMap, 'enquiry');
$foundIdent = $this->foundIdentifier($found_enquiries);
$found_invoices = $this->checkValidIdentifier($sub->text, $invoiceMap, 'invoice');
$foundIdent = $this->foundIdentifier($found_invoices);
$found_pos = $this->checkValidIdentifier($sub->text, $poMap, 'purchaseorder');
$foundIdent = $this->foundIdentifier($found_pos);
$found_jobs = $this->checkValidIdentifier($sub->text, $jobMap, 'job');
$foundIdent = $this->foundIdentifier($found_jobs);
}
//We're going to save this Email.
//Lets grab the attachments (if any) then create the appropiate Objects.
if($fromKnownUser == true || $saveThis == true || $foundIdent == true) {
$recipientsIDs['to'] = array();
$recipientsIDs['from'] = array();
$recipientsIDs['cc'] = array();
/**
* Loop over each recipient email found. Build an array containing the User IDs of that user.
*
* If that email doesn't have a corresponding User ID. We need to create a new User for this email address.
*
*
*/
foreach($recipients as $type => $recipientArray) {
foreach($recipientArray as $recEmail) {
$recEmailLC = strtolower($recEmail);
if(isset($userMap[$recEmailLC])) {
$recipientsIDs[$type][] = $userMap[$recEmailLC]; //Existing User ID.
//Add it to the userMap
$userMap[$recEmailLC] = $newID;
}
else {
//$recipientsIDs[$type][] = "NEW User: $recEmail"; //Need to make a new user ID.
echo "Making a new User for: '{$recEmailLC}'\n";
$newUser['User'] = array(
'type' => 'contact',
'email' => $recEmailLC,
'by_vault' => 1
);
$this->User->create();
if($this->User->save($newUser, false)) {
$newID = $this->User->id;
echo "New User '{$recEmail}' Added with ID: {$newID}\n";
$recipientsIDs[$type][] = $newID;
//Add it to the userMap
$userMap[$recEmailLC] = $newID;
}
else {
echo "Serious Error: Unable to create user for email '{$recEmailLC}'\n";
continue;
}
echo "Serious Error: Unable to create user for email '{$recEmailLC}'\n";
continue;
}
}
}
}
echo "---------START MESSAGE -----------------\n";
echo "Subject: ".$subjDecoded[0]->text."\n";
echo "---------START MESSAGE -----------------\n";
echo "Subject: ".$subjDecoded[0]->text."\n";
$newEmail = array();
$newEmail = array();
/** Testing creating the Email, Recipient and Attachments */
$newEmail['Email'] = array();
/** Testing creating the Email, Recipient and Attachments */
$newEmail['Email'] = array();
if(isset($recipientsIDs['from'][0])) {
$newEmail['Email']['user_id'] = $recipientsIDs['from'][0]; //We should never have more than one 'from' user ID.
}
else {
print_r($recipientsIDs);
echo "Email has no From Recipient ID. Ignoring this email\n";
$this->makeSymlink($email_filename, $vault_dir, $processed_dir);
continue;
}
if(isset($recipientsIDs['from'][0])) {
$newEmail['Email']['user_id'] = $recipientsIDs['from'][0]; //We should never have more than one 'from' user ID.
}
else {
print_r($recipientsIDs);
echo "Email has no From Recipient ID. Ignoring this email\n";
$this->makeSymlink($email_filename, $vault_dir, $processed_dir);
continue;
}
//die("Email has no from Recipient!! Critical Failure"); //Change this to logging for deployment.
$newEmail['Email']['udate'] = $unix_time;
$newEmail['Email']['udate'] = $unix_time;
$allowedChars = array('@', ';', ':', '.', '$', '%', '*','#','!',',','[', ']',' ','{','}','|', '(', ')');
$newEmail['Email']['subject'] = Sanitize::paranoid($subjDecoded[0]->text, $allowedChars);
$newEmail['Email']['filename'] = $email_filename;
$allowedChars = array('@', ';', ':', '.', '$', '%', '*','#','!',',','[', ']',' ','{','}','|', '(', ')');
$newEmail['Email']['subject'] = Sanitize::paranoid($subjDecoded[0]->text, $allowedChars);
$newEmail['Email']['filename'] = $email_filename;
$recipientCount = 0;
foreach($recipientsIDs as $type => $typeEmails) {
foreach($typeEmails as $userID) {
$recipientCount = 0;
foreach($recipientsIDs as $type => $typeEmails) {
foreach($typeEmails as $userID) {
$newEmail['EmailRecipient'][$recipientCount]['user_id'] = $userID;
switch($type) {
case 'to':
$newEmail['EmailRecipient'][$recipientCount]['type'] = 'to';
$recipientCount++;
break;
$newEmail['EmailRecipient'][$recipientCount]['user_id'] = $userID;
switch($type) {
case 'to':
$newEmail['EmailRecipient'][$recipientCount]['type'] = 'to';
$recipientCount++;
break;
case 'cc':
$newEmail['EmailRecipient'][$recipientCount]['type'] = 'cc';
$recipientCount++;
break;
default:
}
case 'cc':
$newEmail['EmailRecipient'][$recipientCount]['type'] = 'cc';
$recipientCount++;
break;
default:
}
}
}
/** End Email creation **/
/** End Email creation **/
/** Start Attachments **/
/** Start Attachments **/
$relative_path = $this->getAttachmentDirectory($email_dir, $unix_time);
$relative_path = $this->getAttachmentDirectory($email_dir, $unix_time);
$attachments = $this->fetchBodyAttachments($email_filename, $vault_dir, $email_dir, $relative_path, $ripmime_path);
$attachments = $this->fetchBodyAttachments($email_filename, $vault_dir, $email_dir, $relative_path, $ripmime_path);
$attachmentCount = 0;
$attachmentCount = 0;
$biggestHTML = 0;
$biggestHTML = 0;
foreach($attachments as $attachment) {
if(empty($attachment['type'])) {
continue;
}
$newEmail['EmailAttachment'][$attachmentCount]['name'] = $attachment['name'];
$newEmail['EmailAttachment'][$attachmentCount]['type'] = $attachment['type'];
$newEmail['EmailAttachment'][$attachmentCount]['size'] = $attachment['size'];
$newEmail['EmailAttachment'][$attachmentCount]['filename'] = $attachment['filename'];
$newEmail['EmailAttachment'][$attachmentCount]['is_message_body'] = $attachment['is_message_body'];
$attachmentCount++;
foreach($attachments as $attachment) {
if(empty($attachment['type'])) {
continue;
}
/** End attachments */
$newEmail['EmailAttachment'][$attachmentCount]['name'] = $attachment['name'];
$newEmail['EmailAttachment'][$attachmentCount]['type'] = $attachment['type'];
$newEmail['EmailAttachment'][$attachmentCount]['size'] = $attachment['size'];
$newEmail['EmailAttachment'][$attachmentCount]['filename'] = $attachment['filename'];
$newEmail['EmailAttachment'][$attachmentCount]['is_message_body'] = $attachment['is_message_body'];
$attachmentCount++;
}
/** End attachments */
/* If we found Identifiers for this Email. Create the HABTM associations.
/* If we found Identifiers for this Email. Create the HABTM associations.
*
*/
if(count($found_jobs) > 0) {
// echo "Found Job IDs: \n";
// print_r($found_jobs);
foreach($found_jobs as $jobID) {
$newEmail['Job'][] = array('job_id'=> $jobID);
}
*/
if(count($found_jobs) > 0) {
// echo "Found Job IDs: \n";
// print_r($found_jobs);
foreach($found_jobs as $jobID) {
$newEmail['Job'][] = array('job_id'=> $jobID);
}
}
if(count($found_pos) > 0) {
// echo "Found PO IDs: \n";
// print_r($found_pos);
foreach($found_pos as $poID) {
$newEmail['PurchaseOrder'][] = array('purchase_order_id'=> $poID);
}
if(count($found_pos) > 0) {
// echo "Found PO IDs: \n";
// print_r($found_pos);
foreach($found_pos as $poID) {
$newEmail['PurchaseOrder'][] = array('purchase_order_id'=> $poID);
}
}
if(count($found_enquiries) > 0) {
// echo "Found enquiry IDs: \n";
// print_r($found_enquiries);
foreach($found_enquiries as $enqID) {
$newEmail['Enquiry'][] = array('enquiry_id'=> $enqID);
}
if(count($found_enquiries) > 0) {
// echo "Found enquiry IDs: \n";
// print_r($found_enquiries);
foreach($found_enquiries as $enqID) {
$newEmail['Enquiry'][] = array('enquiry_id'=> $enqID);
}
}
if(count($found_invoices) > 0) {
// echo "Found Invoice IDs: \n";
// print_r($found_invoices);
foreach($found_invoices as $invID) {
$newEmail['Invoice'][] = array('invoice_id'=> $invID);
}
if(count($found_invoices) > 0) {
// echo "Found Invoice IDs: \n";
// print_r($found_invoices);
foreach($found_invoices as $invID) {
$newEmail['Invoice'][] = array('invoice_id'=> $invID);
}
}
//print_r($newEmail);
//print_r($newEmail);
$this->Email->create();
$emailID = $this->Email->saveAll($newEmail);
if(!empty($emailID)) {
echo "Success. We made an email\n";
}
else {
echo "Error! Didn't save the email\n";
}
echo "--------END MESSAGE ------\n";
$this->Email->create();
$emailID = $this->Email->saveAll($newEmail);
if(!empty($emailID)) {
echo "Success. We made an email\n";
}
else {
echo "email will not be saved. Subject: ".$subjDecoded[0]->text."\n";
}
if($this->makeSymlink($email_filename, $vault_dir, $processed_dir)) {
echo "Created Symlink Sucessfully. Email has been processed\n";
}
else {
echo "Unable to create Symlink for {$email_filename}. Critical Error! \n";
echo "Error! Didn't save the email\n";
}
echo "--------END MESSAGE ------\n";
}
else {
echo "email will not be saved. Subject: ".$subjDecoded[0]->text."\n";
}
if($this->makeSymlink($email_filename, $vault_dir, $processed_dir)) {
echo "Created Symlink Sucessfully. Email has been processed\n";
}
else {
echo "Unable to create Symlink for {$email_filename}. Critical Error! \n";
}
}
}
function isEmlFile($vault_dir, $filename) {
$info = pathinfo($vault_dir."/".$filename);