289 lines
7 KiB
PHP
289 lines
7 KiB
PHP
<?php
|
|
|
|
/**
|
|
* file: vault_two.php
|
|
*
|
|
* Major re-working of the vault idea.
|
|
|
|
*/
|
|
|
|
class VaultTwoShell extends Shell {
|
|
|
|
|
|
var $uses = array('Enquiry', 'Contact', 'Invoice', 'PurchaseOrder');
|
|
|
|
|
|
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';
|
|
|
|
|
|
|
|
$ripmime_path = '/usr/local/bin/ripmime';
|
|
|
|
|
|
if($testing == 0) {
|
|
|
|
//$username = 'karl';
|
|
//$password = '72seng89';
|
|
|
|
$email_dir = '/Users/karlcordes/Sites/quotenik/app/emails';
|
|
$ripmime_path = '/opt/local/bin/ripmime';
|
|
$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 $username."\n";
|
|
echo "Number of messages to Process ".$number_of_messages."\n";
|
|
|
|
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')));
|
|
//$contacts = $this->Contact->find('all', array('recursive'=>0, 'fields'=>array('Contact.id', 'Contact.email')));
|
|
$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')));
|
|
|
|
|
|
foreach ($enquiries as $enq) {
|
|
$enqNumber = $enq['Enquiry']['title'];
|
|
$id = $enq['Enquiry']['id'];
|
|
$enquiryList[$enqNumber] = $id;
|
|
}
|
|
|
|
foreach ($invoices as $inv) {
|
|
$invNumber = $inv['Invoice']['title'];
|
|
$id = $inv['Invoice']['id'];
|
|
$invoiceList[$invNumber] = $id;
|
|
//echo $invNumber."\n";
|
|
}
|
|
|
|
foreach ($purchaseOrders as $po) {
|
|
$poNumber = $po['PurchaseOrder']['title'];
|
|
$id = $po['PurchaseOrder']['id'];
|
|
$poList[$poNumber] = $id;
|
|
//echo $poNumber."\n";
|
|
}
|
|
|
|
foreach($contacts as $contact) {
|
|
$email = strtolower($contact['Contact']['email']);
|
|
$id = $contact['Contact']['id'];
|
|
$emailList[$email] = $id;
|
|
//echo $email."\n";
|
|
}
|
|
|
|
|
|
|
|
for($i=$number_of_messages; $i > 0; $i--) {
|
|
$this_header = imap_headerinfo($mbox, $i);
|
|
$message = $this->getMessage($mbox, $i, $this_header);
|
|
|
|
|
|
|
|
// echo "Checking msg number: $i\tSubject: ".$message['subject']."\n";
|
|
//echo "Checking msg number: $i\n";
|
|
|
|
$enqID = $this->checkIfValidEnquiry($message['subject'], $enquiryList);
|
|
|
|
|
|
if($enqID == false && $contactID == false) {
|
|
$uid = imap_uid($mbox, $i);
|
|
echo "Deleting msg number: $i\tSubject: ".$message['subject']."[{$uid}]"."\n";
|
|
imap_delete($mbox, $uid, 1);
|
|
//$discardArray[] = $i;
|
|
|
|
}
|
|
else {
|
|
$message['subject']." from ".$message['from']." will be saved";
|
|
}
|
|
|
|
}
|
|
|
|
imap_expunge($mbox);
|
|
|
|
imap_close($mbox);
|
|
}
|
|
|
|
|
|
|
|
function checkIfValidEnquiry($subject, &$enqList) {
|
|
$subject = iconv_mime_decode($subject, 0, "ISO-8859-1");
|
|
|
|
$output = array();
|
|
// $decoded_subject = iconv_mime_decode($subject, 2, "ISO-8859-1");
|
|
|
|
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];
|
|
|
|
echo "'$fetched_enquirynumber'\n";
|
|
|
|
if(array_key_exists($fetched_enquirynumber, $enqList)) { //check if it actually exists.
|
|
$enqid = $enqList[$fetched_enquirynumber];
|
|
|
|
return $enqid;
|
|
}
|
|
else {
|
|
return false;
|
|
}
|
|
}
|
|
else {
|
|
return false;
|
|
}
|
|
|
|
}
|
|
|
|
function checkIfValidIdentifier($subject, &$list, $type) {
|
|
$subject = iconv_mime_decode($subject, 0, "ISO-8859-1");
|
|
|
|
$output = array();
|
|
// $decoded_subject = iconv_mime_decode($subject, 2, "ISO-8859-1");
|
|
|
|
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);
|
|
}
|
|
|
|
if(isset($output[0])) { //Found a valid-looking Enquiry Number
|
|
$fetched_enquirynumber = $output[0];
|
|
|
|
echo "'$fetched_enquirynumber'\n";
|
|
|
|
if(array_key_exists($fetched_enquirynumber, $enqList)) { //check if it actually exists.
|
|
$enqid = $enqList[$fetched_enquirynumber];
|
|
|
|
return $enqid;
|
|
}
|
|
else {
|
|
return false;
|
|
}
|
|
}
|
|
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;
|
|
$recipients = $this->getRecipients($headers, false);
|
|
$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
|
|
*
|
|
* if $returnArray is true, return an array rather than a string.
|
|
* NOT USED DUE TO AWFUL WAY CONTACTS ARE SETUP. 3 different classes of people? WTF was I thinking.
|
|
*/
|
|
function getRecipients($headers, $returnArray = false) {
|
|
|
|
$recipients = array();
|
|
if($returnArray == false) {
|
|
$recipients['to'] = "";
|
|
$recipients['from'] = "";
|
|
$recipients['cc'] = "";
|
|
}
|
|
else {
|
|
$recipients['to'] = array();
|
|
$recipients['from'] = array();
|
|
$recipients['cc'] = array();
|
|
}
|
|
|
|
if(isset($headers->to)) {
|
|
$to = $headers->to;
|
|
if(count($to) > 0) {
|
|
foreach ($to as $id => $object) {
|
|
if($returnArray == false) {
|
|
$recipients['to'] .= "$object->mailbox@$object->host ";
|
|
}
|
|
else {
|
|
$recipients['to'][] = strtolower($object->mailbox."@".$object->host);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if(isset($headers->cc)) {
|
|
$cc = $headers->cc;
|
|
if(count($cc) > 0) {
|
|
foreach ($cc as $id => $object) {
|
|
if($returnArray == false) {
|
|
$recipients['cc'] .= "$object->mailbox@$object->host ";
|
|
}
|
|
else {
|
|
$recipients['cc'][] = strtolower($object->mailbox."@".$object->host);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
if(isset($headers->from)) {
|
|
$from = $headers->from;
|
|
if(count($from) > 0) {
|
|
foreach ($from as $id => $object) {
|
|
if($returnArray == false) {
|
|
$recipients['from'] .= "$object->mailbox@$object->host";
|
|
}
|
|
else {
|
|
$recipients['from'][] = strtolower($object->mailbox."@".$object->host);
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return $recipients;
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
?>
|