From bbb547199dc93abdd716a35cd85284787f7279b9 Mon Sep 17 00:00:00 2001 From: Karl Cordes Date: Tue, 28 Dec 2010 12:51:39 +1100 Subject: [PATCH] Refactoring all users to a single class to improve the vault --- controllers/app_controller.php | 40 +++- models/email.php | 27 ++- models/enquiry.php | 7 +- vendors/shells/users_migrate.php | 39 ++++ vendors/shells/vault_two.php | 320 +++++++++++++++++++------------ views/layouts/default.ctp | 2 +- 6 files changed, 295 insertions(+), 140 deletions(-) create mode 100644 vendors/shells/users_migrate.php diff --git a/controllers/app_controller.php b/controllers/app_controller.php index e5ba1214..a7dcd5d0 100755 --- a/controllers/app_controller.php +++ b/controllers/app_controller.php @@ -16,9 +16,6 @@ class AppController extends Controller { $this->Auth->allow('display'); $this->Auth->authorize = 'controller'; - - - if($this->RequestHandler->isAjax()) { Configure::write('debug', 0); } @@ -30,7 +27,42 @@ class AppController extends Controller { return true; } - + + + /** + * Check if the current logged in user is an admin + * @return boolean + */ + function isAdmin() { + $currentuser = $this->getCurrentUser(); + if($currentuser['access_level'] == 1) { + return true; + } + else { + return false; + } + } + + /** + * Read the current logged in user. + * @return array - the currently logged in user. + */ + function getCurrentUser() { + return $this->Session->read('Auth.User'); + } + + /** + * Return the id of the current user. False if not logged in. + */ + function getCurrentUserID() { + $currentuser = $this->getCurrentUser(); + if($currentuser) { + return $currentuser['id']; + } + else { + return false; + } + } diff --git a/models/email.php b/models/email.php index 7fb0e690..ba934ff3 100755 --- a/models/email.php +++ b/models/email.php @@ -3,16 +3,27 @@ class Email extends AppModel { var $name = 'Email'; - var $belongsTo = array( - 'Enquiry' => array('className' => 'Enquiry', - 'foreignKey' => 'enquiry_id', - 'counterCache' => true)); var $hasMany = array( - 'EmailAttachment' => array('className' => 'EmailAttachment', - 'foreignKey' => 'email_id', - 'dependent' => false - )); + 'EmailAttachment' => array('className' => 'EmailAttachment', + 'foreignKey' => 'email_id', + 'dependent' => false) + + ); + + var $hasAndBelongsToMany = array ( + 'Enquiry' => array('className' => 'Enquiry', + 'joinTable' => 'emails_enquiries' + ), + 'Invoice' => array('className' => 'Invoice', + 'joinTable' => 'emails_invoices' + ), + + 'PurchaseOrder' => array('className' => 'PurchaseOrder', + 'joinTable' => 'emails_purchase_orders' + ), + + ); } ?> diff --git a/models/enquiry.php b/models/enquiry.php index cf1b3ddb..49ac1322 100755 --- a/models/enquiry.php +++ b/models/enquiry.php @@ -171,12 +171,7 @@ class Enquiry extends AppModel { 'finderQuery' => '', 'counterQuery' => '' ), - - - 'Email' => array('className' => 'Email', - 'foreignKey' => 'enquiry_id', - 'dependent' => false - ), + 'Invoice' => array('className' => 'Invoice', 'foreignKey'=>'enquiry_id'), diff --git a/vendors/shells/users_migrate.php b/vendors/shells/users_migrate.php new file mode 100644 index 00000000..777966ea --- /dev/null +++ b/vendors/shells/users_migrate.php @@ -0,0 +1,39 @@ +PrincipleContact->find('count'); + $contactsCount = $this->Contact->find('count'); + $usersCount = $this->User->find('count'); + + $this->printCountLine($principleContactsCount, 'Principle Contact'); + $this->printCountLine($contactsCount, 'Customer Contacts'); + $this->printCountLine($usersCount, 'Users'); + + $totalCount = $principleContactsCount + $contactsCount + $usersCount; + $this->printCountLine($totalCount, 'Total Users after migration'); + + } + + + function printCountLine($count, $model) { + echo $model ." count:". $count ."\n"; + } + +} + + +?> \ No newline at end of file diff --git a/vendors/shells/vault_two.php b/vendors/shells/vault_two.php index c02d492e..71259c05 100644 --- a/vendors/shells/vault_two.php +++ b/vendors/shells/vault_two.php @@ -7,204 +7,282 @@ */ - - - - class VaultTwoShell extends Shell { - var $uses = array('Enquiry', 'Contact'); + var $uses = array('Enquiry', 'Contact', 'Invoice', 'PurchaseOrder'); 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'; + $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'; + $ripmime_path = '/usr/local/bin/ripmime'; - if($testing == 0) { + if($testing == 0) { - //$username = 'karl'; - //$password = '72seng89'; + //$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()); - } + $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; + $MC = imap_check($mbox); + $number_of_messages = $MC->Nmsgs; echo $username."\n"; - echo "Number of messages to Process ".$number_of_messages."\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'))); + if($number_of_messages == 0) { + exit(0); + } - foreach ($enquiries as $enq) { - $enqNumber = $enq['Enquiry']['title']; - $id = $enq['Enquiry']['id']; - $enquiryList[$enqNumber] = $id; - } + /* 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); - $contactID = $this->checkIfFromContact($message['from'], $emailList); + + 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); + $uid = imap_uid($mbox, $i); echo "Deleting msg number: $i\tSubject: ".$message['subject']."[{$uid}]"."\n"; - imap_delete($mbox, $uid, 1); - //$discardArray[] = $i; + imap_delete($mbox, $uid, 1); + //$discardArray[] = $i; - } + } else { $message['subject']." from ".$message['from']." will be saved"; } - } + } - imap_expunge($mbox); + imap_expunge($mbox); - imap_close($mbox); + imap_close($mbox); } 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(); // $decoded_subject = iconv_mime_decode($subject, 2, "ISO-8859-1"); - preg_match("/CMC\d+([NVQWSOT]|ACT|NT)E\d+-\d+/", $subject, $output); + 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]; + if(isset($output[0])) { //Found a valid-looking Enquiry Number + $fetched_enquirynumber = $output[0]; - echo "'$fetched_enquirynumber'\n"; + echo "'$fetched_enquirynumber'\n"; - if(array_key_exists($fetched_enquirynumber, $enqList)) { //check if it actually exists. - $enqid = $enqList[$fetched_enquirynumber]; + 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]; + 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); + $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; + $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 */ - function getRecipients($headers) { + /** + * 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(); - $recipients['to'] = ""; - $recipients['from'] = ""; - $recipients['cc'] = ""; + $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) { - $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->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) { - $recipients['from'] .= "$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; + return $recipients; } + + } diff --git a/views/layouts/default.ctp b/views/layouts/default.ctp index 83941a3a..8c2520be 100755 --- a/views/layouts/default.ctp +++ b/views/layouts/default.ctp @@ -189,7 +189,7 @@ ?> - +