cmc-sales/vendors/shells/vault_two.php

514 lines
12 KiB
PHP
Executable file

<?php
/**
* file: vault_two.php
*
* Major re-working of the vault idea.
*/
class VaultTwoShell extends Shell {
var $uses = array('Enquiry', 'Contact', 'Invoice', 'PurchaseOrder', 'User', 'Email', 'EmailRecipient', 'Job');
function main() {
/******************************************************
* Config Variables
* *****************************************************/
$testing = 0; //Whether to actually move the emails. 1=test, 0=production
/* Setup Connection to the IMAP server */
$username = 'vault';
$password = 'xjdYOsmJWc37'; /* The password for the account to be checked */
$email_dir = '/var/www/cakephp/app/emails';
if($testing == 0) {
//$username = 'karl';
//$password = '72seng89';
$email_dir = '/Users/karlcordes/Sites/quotenik/app/emails';
$mbox = imap_open("{mail.cmctechnologies.com.au:143}INBOX", $username, $password) or die("can't connect: " . imap_last_error());
}
else {
$mbox = imap_open("{192.168.0.8:143/novalidate-cert}INBOX", $username, $password) or die("can't connect: " . imap_last_error());
}
$MC = imap_check($mbox);
$number_of_messages = $MC->Nmsgs;
echo "Number of messages to Process ".$number_of_messages."\n";
for($i=0;$i<20;$i++) {
$uuid = String::uuid();
$length = strlen($uuid);
echo "{$uuid} is $length \n";
}
die();
if($number_of_messages == 0) {
exit(0);
}
/* 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.
*
*/
for($i=$number_of_messages; $i > 0; $i--) {
if(!imap_ping($mbox)) {
echo "Connection to Mailserver dropped. Attempting to reconnect";
$mbox = imap_open("{mail.cmctechnologies.com.au:143}INBOX", $username, $password) or die("can't connect: " . imap_last_error());
}
$this_header = imap_headerinfo($mbox, $i);
//$message = $this->getMessage($mbox, $i, $this_header);
$recipients = $this->getRecipients($this_header, true);
// continue;
//die();
$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);
}
//print_r($recipients);
$subjDecoded = imap_mime_header_decode($this_header->subject);
$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);
//echo "["$sub->.$sub->text;
}
//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) {
if(isset($userMap[$recEmail])) {
$recipientsIDs[$type][] = $userMap[$recEmail]; //Existing User ID.
}
else {
//$recipientsIDs[$type][] = "NEW User: $recEmail"; //Need to make a new user ID.
echo "Making a new User for: '{$recEmail}'\n";
$newUser['User'] = array(
'type' => 'contact',
'email' => $recEmail,
'by_vault' => 1
);
$this->User->create();
if($this->User->save($newUser)) {
$newID = $this->User->id;
echo "New User '{$recEmail}' Added with ID: {$newID}\n";
$recipientsIDs[$type][] = $newID;
//Add it to the userMap
$userMap[$recEmail] = $newID;
}
else {
echo "Fatal Error: Unable to create user for email '{$recEmail}'\n";
die();
}
}
}
}
echo "--------------------------\n";
echo "Email No: $i\n";
echo "Subject: ".$subjDecoded[0]->text."\n";
if(count($found_jobs) > 0) {
echo "Found Job IDs: \n";
print_r($found_jobs);
}
if(count($found_pos) > 0) {
echo "Found PO IDs: \n";
print_r($found_pos);
}
if(count($found_enquiries) > 0) {
echo "Found enquiry IDs: \n";
print_r($found_enquiries);
}
if(count($found_invoices) > 0) {
echo "Found Invoice IDs: \n";
print_r($found_invoices);
}
$newEmail = array();
$structure = imap_fetchstructure($mbox, $i);
print_r($recipients);
print_r($recipientsIDs);
//print_r($structure);
// continue;
$attachments = $this->extract_attachments($mbox,$structure, $i);
foreach($attachments as $attachment) {
if(isset($attachment['is_attachment'])) {
$name = false;
$result = false;
if(!empty($attachment['filename'])) {
echo "\tfilename: ".$attachment['filename']."\n";
$name = $attachment['filename'];
}
if(!empty($attachment['name'])) {
echo "\tname: ".$attachment['name']."\n";
$name = $attachment['name'];
}
if($name != false) {
echo "\t".$email_dir."/".$name."\n";
/*$result = file_put_contents($email_dir."/".$name, $attachment['attachment']);
if($result === false) {
echo "Failed to write $name\n";
}
else {
$kb_result = $result / 1024;
echo "Wrote $result kilobytes for $name\n";
}*/
}
else {
continue;
}
}
}
echo "--------\n";
}
else {
echo "email {$i} will not be saved. Subject: ".$subjDecoded[0]->text."\n";
}
// print_r($recipients);
// echo "----------------------\n";
}
imap_expunge($mbox);
imap_close($mbox);
}
/**
* Make a 'HashMap' (array) in the format.
* $newMap[$key] = $obj[$modelName]['id'];
*
* @param array $objects
* @param string $modelName
* @param string $keyName
* @return array
*/
function makeMap($objects,$modelName, $keyName) {
$newMap = array();
foreach ($objects as $obj) {
$key = $obj[$modelName][$keyName];
if($modelName == 'User') {
strtolower($key);
}
$newMap[$key] = $obj[$modelName]['id'];
}
return $newMap;
}
/**
*
* @param <type> $recipient
* @param <type> $emailMap
* @return <type>
*/
function userExists($recipient, &$emailMap) {
if(isset($emailMap[$recipient])) {
//echo "$recipient is a user in the system. Will be saved\n";
return true;
}
else {
return false;
//echo "$recipient is not a user in the system.\n";
}
}
/**
*
* @param <type> $subject
* @param <type> $list
* @param <type> $type
*/
function checkValidIdentifier($subject, &$list, $type) {
$returnArray = array();
if($type == 'enquiry') {
preg_match("/CMC\d+([NVQWSOT]|ACT|NT)E\d+-\d+/", $subject, $output);
}
else if ($type == 'invoice') {
preg_match("/CMCIN\d+/", $subject, $output);
}
else if ($type == 'purchaseorder') {
preg_match("/CMCPO\d+/", $subject, $output);
}
else if($type == 'job') {
preg_match("/(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)\d+(N|V|W|S|T|NT|ACT)J\d+/", $subject, $output);
}
foreach($output as $match) {
if(isset($list[$match])) { //check if it actually exists.
$returnArray[] = $list[$match];
}
}
return $returnArray;
}
/**
*
* @param <type> $found_identifier
* @return <type>
*/
function foundIdentifier(&$found_identifier) {
if(count($found_identifier) > 0) {
return true;
}
else {
return false;
}
}
function getMessage($mbox, $msgnumber, $headers) {
//$subject = $headers->subject;
//$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;
$message['subject'] = $subject;
$message['date'] = $date;
return $message;
}
/**
* get Recipients from the headers of an email
*
*/
function getRecipients($headers) {
$recipients['to'] = array();
$recipients['from'] = array();
$recipients['cc'] = array();
if(isset($headers->to)) {
$recipients['to'] = $this->buildEmailArray($headers->to);
}
if(isset($headers->cc)) {
$recipients['cc'] = $this->buildEmailArray($headers->cc);
}
if(isset($headers->from)) {
$recipients['from'] = $this->buildEmailArray($headers->from);
}
return $recipients;
}
/**
* Build an array containing email addresses from the header object
* passed to it.
*
* @param Object $recipientObj
* @return Array
*/
function buildEmailArray($recipientObj) {
$recipientList = array();
if(count($recipientObj) > 0) {
foreach($recipientObj as $id=> $object) {
$recipientList[] = strtolower($object->mailbox."@".$object->host);
}
return $recipientList;
}
}
/**
* Adapted from
* http://www.electrictoolbox.com/function-extract-email-attachments-php-imap/
* @param <type> $connection
* @param <type> $message_number
* @return <type>
*/
function extract_attachments($connection, $structure, $message_number) {
$attachments = array();
if(isset($structure->parts)) {
$numberOfParts = count($structure->parts);
}
if(isset($structure->parts) && $numberOfParts) {
for($i = 0; $i < $numberOfParts; $i++) {
$attachments[$i] = array(
'is_attachment' => false,
'filename' => '',
'name' => '',
'attachment' => ''
);
if($structure->parts[$i]->ifdparameters) {
foreach($structure->parts[$i]->dparameters as $object) {
if(strtolower($object->attribute) == 'filename') {
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['filename'] = $object->value;
}
}
}
if($structure->parts[$i]->ifparameters) {
foreach($structure->parts[$i]->parameters as $object) {
if(strtolower($object->attribute) == 'name') {
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['name'] = $object->value;
}
}
}
if($attachments[$i]['is_attachment']) {
$attachments[$i]['attachment'] = imap_fetchbody($connection, $message_number, $i+1);
if($structure->parts[$i]->encoding == 3) { // 3 = BASE64
$attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
}
elseif($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
$attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
}
}
}
}
return $attachments;
}
}
?>