cmc-sales/vendors/shells/vault_two.php
2010-12-10 16:55:59 +11:00

211 lines
5.7 KiB
PHP

<?php
/**
* file: vault_two.php
*
* Major re-working of the vault idea.
*/
class VaultTwoShell extends Shell {
var $uses = array('Enquiry', 'Contact');
function main() {
/******************************************************
* Config Variables
* *****************************************************/
$testing = 1; //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 == 1) {
//$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);
}
$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')));
foreach ($enquiries as $enq) {
$enqNumber = $enq['Enquiry']['title'];
$id = $enq['Enquiry']['id'];
$enquiryList[$enqNumber] = $id;
}
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);
$contactID = $this->checkIfFromContact($message['from'], $emailList);
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_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 checkIfFromContact($sender, &$emailList) {
if(array_key_exists($sender, $emailList)) {
echo "'".$sender."'\n";
return $emailList[$sender];
}
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);
$message['subject'] = $subject;
$message['date'] = $date;
$message['to'] = strtolower($recipients['to']);
$message['from'] = strtolower($recipients['from']);
$message['cc'] = strtolower($recipients['cc']);
return $message;
}
/* get Recipients from the headers of an email */
function getRecipients($headers) {
$recipients = array();
$recipients['to'] = "";
$recipients['from'] = "";
$recipients['cc'] = "";
if(isset($headers->to)) {
$to = $headers->to;
if(count($to) > 0) {
foreach ($to as $id => $object) {
$recipients['to'] .= "$object->mailbox@$object->host ";
}
}
}
if(isset($headers->cc)) {
$cc = $headers->cc;
if(count($cc) > 0) {
foreach ($cc as $id => $object) {
$recipients['cc'] .= "$object->mailbox@$object->host ";
}
}
}
if(isset($headers->from)) {
$from = $headers->from;
if(count($from) > 0) {
foreach ($from as $id => $object) {
$recipients['from'] .= "$object->mailbox@$object->host";
}
}
}
return $recipients;
}
}
?>