From 2e563ce1f5affb33d7051c20d514c0fa0d4fded9 Mon Sep 17 00:00:00 2001 From: Karl Cordes Date: Tue, 15 Mar 2011 14:22:09 +1100 Subject: [PATCH] Fixed incorrect link on Jobs table --- controllers/jobs_controller.php | 6 +- vendors/shells/firstpass.php | 660 ++++++++++++++------ vendors/shells/vault.php | 1026 ++++++++++++++++++------------- vendors/shells/vault_two.php | 1 + views/elements/job_table.ctp | 2 +- 5 files changed, 1093 insertions(+), 602 deletions(-) diff --git a/controllers/jobs_controller.php b/controllers/jobs_controller.php index 5a9ff4da..7d2cbec6 100755 --- a/controllers/jobs_controller.php +++ b/controllers/jobs_controller.php @@ -191,7 +191,7 @@ class JobsController extends AppController { * * Will fetch the right ID the same way the Index does, then update each record. */ - function fixJobs() { + /*function fixJobs() { $jobs = $this->Job->find('all'); @@ -209,6 +209,8 @@ class JobsController extends AppController { $this->set('jobs', $this->Job->find('all')); - } + }*/ + + } ?> \ No newline at end of file diff --git a/vendors/shells/firstpass.php b/vendors/shells/firstpass.php index 2fe49ec6..642325e6 100755 --- a/vendors/shells/firstpass.php +++ b/vendors/shells/firstpass.php @@ -1,276 +1,560 @@ Nmsgs; + //$username = 'karl'; + //$password = '72seng89'; + $ripmime_path = '/opt/local/bin/ripmime'; + $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() + //); + //$mbox = imap_open("{192.168.0.8:143}INBOX", $username, $password) or die("can't connect: " . imap_last_error()); - echo "Number of messages to Process ".$number_of_messages."\n"; + define('EML_FILE_PATH', '/Users/karlcordes/Sites/quotenik/app/vendors/shells/'); - if($number_of_messages == 0) { - exit(0); - } + $filename = 'sample.eml'; + $debug = 1; - /* $enquiries = $this->Enquiry->find('all', array('recursive'=>0,'fields' => array('Enquiry.title', 'Enquiry.id'))); + // Read the file + if (!($content = fread(fopen(EML_FILE_PATH.$filename, 'r'), filesize(EML_FILE_PATH.$filename)))) + die('File not found ('.EML_FILE_PATH.$filename.')'); +//Handle files coming from windows (\r\n vs \n): +// Thanks to Dan Hulme (dhulme@gmail.com) + $content = str_replace("\r", "", $content); +// Keep a copy of the original file + $raw=$content; - foreach ($enquiries as $enq) { - $enqNumber = $enq['Enquiry']['title']; - $id = $enq['Enquiry']['id']; - $enquiryList[$enqNumber] = $id; - } -*/ + $headers = imap_rfc822_parse_headers($content); - - //while($number_of_messages > 0) { - - $encodingValues = array(); - for($i=0; $i <=10; $i++) { - $encodingValues[$i] = 0; + //print_r($headers); + //$message = $this->getMessage($mbox, $i, $this_header); + $recipients = $this->getRecipients($headers, true); + print_r($recipients); + die(); + } + else { + $ripmime_path = '/usr/local/bin/ripmime'; + $email_dir = '/var/www/cakephp/app/emails'; + $mbox = imap_open("{192.168.0.8:143/novalidate-cert}INBOX", $username, $password) or die("can't connect: " . imap_last_error()); } - // for($i=1; $i <= $number_of_messages; $i++) { - for($i=$number_of_messages; $i > 0; $i--) { - // $this_header = imap_headerinfo($mbox, $i); - // $message = $this->getMessage($mbox, $i, $this_header); - $structure = imap_fetchstructure($mbox, $i); - //echo "Checking msg number: $i\tSubject: ".$message['subject']."\n"; - //echo "Checking msg number: $i\n"; - - $encoding = $this->extract_encoding($mbox, $structure, $i); - if(is_numeric($encoding)) { - $encodingValues[$encoding]++; - echo "message $i has encoding: $encoding\t total # for this encoding=".$encodingValues[$encoding]."\n"; + $MC = imap_check($mbox); + $number_of_messages = $MC->Nmsgs; + + 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'))); + + $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'); + + + $store = array(); + $discard = array(); + + + + /** + * 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--) { + while($number_of_messages > 0) { + if(!imap_ping($mbox)) { + echo "Connection to Mailserver dropped. Attempting to reconnect\n"; + $mbox = imap_open("{mail.cmctechnologies.com.au:143}INBOX", $username, $password) or die("can't connect: " . imap_last_error()); } - //echo "Message: $i / $number_of_messages\n"; - - /**$enqID = $this->checkIfValidEnquiry($message['subject'], $enquiryList); + $this_header = imap_headerinfo($mbox, $i); - if($enqID == false) { - echo "Deleting msg number: $i\tSubject: ".$message['subject']."\n"; - // imap_delete($mbox, $i); - //$discardArray[] = $i; + $this_uid = imap_uid($mbox, $i); - }*/ - } + $unix_time = $this_header->udate; //Used for filing attachments into month-year folders. + //MM-YYYY used For lack of a better option really. + + //$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); + + if(!isset($this_header->subject)) { //Emails without a subject are not welcome. Skip it. + $discard[] = $this_uid; + continue; + } + + $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) { + $store[] = $this_uid; + echo "email $i will be saved: Subject: ".$subjDecoded[0]->text."\n"; + } + else { + $discard[] = $this_uid; + echo "email {$i} will not be saved. Subject: ".$subjDecoded[0]->text."\n"; + } + // print_r($recipients); + // echo "----------------------\n"; + + + $MC = imap_check($mbox); + $number_of_messages = $MC->Nmsgs; + echo "Number of messages to Process is now ".$number_of_messages."\n"; + - foreach($encodingValues as $encoding => $count) { - echo "Encoding: $encoding\t Count: $count\n"; } - //echo "Tidying up now.\n"; + reset($discard); + reset($store); + + $numberToStore = count($store); + $numberToDiscard= count($discard); - // reset($discardArray); + if($numberToStore > 0) { + echo "Got $numberToStore messages to store\n"; + $storeSet = implode(",", $store); + //imap_mail_move($mbox, $storeSet, 'INBOX/Stored', 1); + } - - // $numberToDiscard= count($discardArray); - - - - /* if($numberToDiscard > 0) { - echo "Going to discard $numberToDiscard messages\n"; - - - $discardSet = implode(",",$discardArray); - - $delete_result = imap_delete($mbox, $discardSet); - - if($delete_result == false) { - echo "Error deleting messages"; - echo imap_last_error(); - } - else { - echo "Success"; - } - }*/ + if($numberToDiscard > 0) { + echo "Going to discard $numberToDiscard messages\n"; + $discardSet = implode(",",$discard); + //imap_mail_move($mbox, $discardSet, 'INBOX/Discarded', 1); + } - /* imap_expunge($mbox); - - echo "Messages before delete: ".$number_of_messages."\n"; - - - $check = imap_check($mbox); - - $number_of_messages = $check->Nmsgs; - - echo "Messages after delete: ".$number_of_messages."\n"; -*/ - - - - // } - - - - imap_close($mbox); + imap_expunge($mbox); + imap_close($mbox); } - function checkIfValidEnquiry($subject, &$enqList) { - $subject = iconv_mime_decode($subject, 0, "ISO-8859-1"); + /** + * 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) { - $output = array(); -// $decoded_subject = iconv_mime_decode($subject, 2, "ISO-8859-1"); + $key = $obj[$modelName][$keyName]; + if($modelName == 'User') { + strtolower($key); + } - preg_match("/CMC\d+([NVQWSOT]|ACT|NT)E\d+-\d+/", $subject, $output); + $newMap[$key] = $obj[$modelName]['id']; + + } + return $newMap; + } - if(isset($output[0])) { //Found a valid-looking Enquiry Number - $fetched_enquirynumber = $output[0]; + /** + * + * @param $recipient + * @param $emailMap + * @return + */ + 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"; + } + } - echo "'$fetched_enquirynumber'\n"; - if(array_key_exists($fetched_enquirynumber, $enqList)) { //check if it actually exists. - $enqid = $enqList[$fetched_enquirynumber]; + /** + * + * @param $subject + * @param $list + * @param $type + */ + function checkValidIdentifier($subject, &$list, $type) { - return $enqid; - } - else { - return false; - } - } - else { - return false; - } + $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 $found_identifier + * @return + */ + function foundIdentifier(&$found_identifier) { + if(count($found_identifier) > 0) { + return true; + } + else { + return; + } + } + + 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; + $date = $headers->date; - $message['subject'] = $subject; + $message['subject'] = $subject; + $message['date'] = $date; - return $message; + return $message; } /** - * Adapted from - * http://www.electrictoolbox.com/function-extract-email-attachments-php-imap/ - * @param $connection - * @param $message_number - * @return + * get Recipients from the headers of an email + * */ - function extract_encoding($connection, $structure, $message_number) { - $encoding = false; - $attachments = array(); - //$structure = imap_fetchstructure($connection, $message_number); + function getRecipients($headers) { - if(isset($structure->parts)) { - $numberOfParts = count($structure->parts); + $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); } - 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']) { - //echo "Encoding is".$structure->parts[$i]->encoding."\n"; - /*$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']); - }*/ - $encoding = $structure->parts[$i]->encoding; - } + 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; } - - return $encoding; } + /* + * Fetches the body and attachments from a MIME encoded email. Uses ripmime to do the decoding + * + */ + + function fetchBodyAttachments($mailbox, $msg_number, $email_dir, $relative_path, $ripmime_path) { + + $uuid = String::uuid(); + $email_file = $email_dir.'/'.$uuid.'.eml'; + imap_savebody($mailbox, $email_file, $msg_number); + $command = "$ripmime_path -i $email_file -d $email_dir/$relative_path --prefix --paranoid -v --verbose-contenttype --recursion-max 30"; + + $output = array(); + exec($command, $output, $status); + + /* Check the $output array and find the filenames of the attachments */ + + if($status == 0) { + $attachments = array(); + + $biggestHTML_num = 999; //Determine the likely HTML body of this email. Guessing, but hopefully reasonably correctly. + $biggestHTML_size = 0; + $biggestPlain_num = 999; //I dont think we'll get legit emails with >999 attachments. + $biggestPlain_size =0; + + for($i=0, $j=0; $i< count($output); $i++, $j++) { + $words = explode(' ', $output[$i]); + $type = explode('=', $words[1]); + $name = explode('=', $words[2]); + + /*echo "OUTPUT\n"; + print_r($output); + echo count($output)."\n"; + + echo "TYPE\n"; + print_r($type); + + echo count($type)."\n"; + */ + if(count($type) != 2) { + //echo "Didnt find a proper type. Skipping it."; + continue; + } + + if(count($name) != 2) { + //echo "Didnt find a proper name. Skipping it"; + continue; + } + + $oldNamePath = $email_dir.'/'.$relative_path.'/'.$name[1]; + $newNamePath = $email_dir.'/'.$relative_path.'/'.$uuid.'-'.$name[1]; + $newName = $relative_path.'/'.$uuid.'-'.$name[1]; + rename($oldNamePath, $newNamePath); + $size = filesize($newNamePath); + $attachments[$j]['type'] = $type[1]; + + + $attachments[$j]['name'] = $newName; + $attachments[$j]['filename'] = $name[1]; + $attachments[$j]['size'] = $size; + $attachments[$j]['is_message_body'] = 0; + echo "in message number $msg_number: found attachment ".$attachments[$j]['name'].' '.$attachments[$j]['type']."\n"; + + + /** Record the array ID and size of the largest HTML & plain attachments **/ + if($attachments[$j]['type'] == 'text/html') { + if($size > $biggestHTML_size) { + $biggestHTML_num = $j; + $biggestHTML_size = $size; + } + } + else if($attachments[$j]['type'] == 'text/plain') { + if($size > $biggestPlain_size) { + $biggestPlain_num = $j; + $biggestPlain_size = $size; + } + } + } + if($biggestHTML_num != 999) { //We found an HTML attachment. + $attachments[$biggestHTML_num]['is_message_body'] = 1; + } + else if($biggestPlain_num != 999) { + $attachments[$biggestPlain_num]['is_message_body'] = 1; + } + + unlink($email_file); + return $attachments; + } + else { + unlink($email_file); + return 1; + } + } + + + /** + * Passed the configured $email_dir and the unix time of this message. + * + * 1. Checks to see if the /emails/MM-YYYY/ directory exists. + * 2a. If not, creates it and returns the full path to this directory. + * 2b. If exists, returns the path to this directory. + * + * @param $email_dir + * @param $unix_time + */ + function getAttachmentDirectory($email_dir, $unix_time) { + + $month_year = date('m-Y', $unix_time); + $path = $email_dir."/".$month_year; + + if(!file_exists($path)) { //Directory doesn't exist Lets make it! + if(mkdir($path)) { + return $month_year; + } + else { + die("Unable to make directory $email_dir/$month_year"); + } + } + else { + return $month_year; + } + + } + + + /** + * The attachments have been decoded and written to disk by ripmime. + * + * 1. Generate a uuid for this email + * 2. Is there a month-year folder this this current month? If not, make one. + * 3. Move the attachments to the correct month-year Folder. Prefix their new names with uuid. + * 4. + * + * @param $attachments + + function moveAttachments($attachments, $email_dir, $unix_time) { + + $month_year = date('m-Y'); + + + $uuid = String::uuid(); + + + + + foreach($attachments as $attachment) { + echo $email_dir.'/'.$attachment['name']."\n"; + } + die(); + + }*/ + } -?> +?> \ No newline at end of file diff --git a/vendors/shells/vault.php b/vendors/shells/vault.php index 4dd9f76c..d00a4e0b 100755 --- a/vendors/shells/vault.php +++ b/vendors/shells/vault.php @@ -1,12 +1,26 @@ -Nmsgs; - - 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'))); + $emails = scandir($vault_dir); - foreach ($enquiries as $enq) { - $enqNumber = $enq['Enquiry']['title']; - $id = $enq['Enquiry']['id']; - $enquiryList[$enqNumber] = $id; - } - $storedArray = array(); - $discardArray = array(); + /* Find the strings we want to look for in the subjects. Build arrays + * using the string as the keys, IDs as the value + */ - //exit(0); + $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 and sort them into ones to be processed or discarded */ + $store = array(); + $discard = array(); + + + + /** + * 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($emails as $email) { + + print $email."\n"; + + + if (!($content = fread(fopen(EML_FILE_PATH.$filename, 'r'), filesize(EML_FILE_PATH.$filename)))) + die('File not found ('.EML_FILE_PATH.$filename.')'); + +//Handle files coming from windows (\r\n vs \n): +// Thanks to Dan Hulme (dhulme@gmail.com) + $content = str_replace("\r", "", $content); + +// Keep a copy of the original file + $raw=$content; + + $headers = imap_rfc822_parse_headers($content); + + //print_r($headers); + //$message = $this->getMessage($mbox, $i, $this_header); + $recipients = $this->getRecipients($headers, true); + print_r($recipients); + die(); + + + continue; + - //for($i=1; $i <= $number_of_messages; $i++) { - for($i=$number_of_messages; $i > 0; $i--) { $this_header = imap_headerinfo($mbox, $i); - $message = $this->getMessage($mbox, $i, $this_header); - $enqID = $this->checkIfValidEnquiry($message['subject'], $enquiryList); + $this_uid = imap_uid($mbox, $i); - if($enqID != false) { - echo "Found Message with enquiry ID: ".$enqID." Processing.\n"; - //Process it and store the message and its attachments. + $unix_time = $this_header->udate; //Used for filing attachments into month-year folders. + //MM-YYYY used For lack of a better option really. - //Generate a Uniqid for this email. - // $uniqid = $this->getUniqId($email_dir); - - /*$this->Email->create(); - $this->data['Email']['enquiry_id'] = $enqID; - $this->data['Email']['to'] = $message['to']; - $this->data['Email']['cc'] = $message['cc']; - $this->data['Email']['from'] = $message['from']; - $this->data['Email']['date'] = $message['date']; - $this->data['Email']['subject'] = $message['subject']; - $this->data['Email']['body'] = ""; - $this->data['Email']['plainbody'] = "";*/ - - $structure = imap_fetchstructure($mbox, $i); - - $attachments = $this->extract_attachments($mbox,$structure, $i); - echo "Msg number: $i\tSubject: ".$message['subject']."\n"; - foreach($attachments as $attachment) { - if(isset($attachment['is_attachment'])) { - $name = false; - $result = false; - - if(!empty($attachment['filename'])) { - echo "filename: ".$attachment['filename']."\n"; - $name = $attachment['filename']; - - } - if(!empty($attachment['name'])) { - echo "name: ".$attachment['name']."\n"; - $name = $attachment['name']; - - } - if($name != false) { - echo $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; - } + //$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; + } - } - - /* - * $attachments = 1; - - if (empty($structure->parts)) { /* A single part message. No attachments and is plain text - $this->data['Email']['body'] = imap_body($mbox, $i); - } - else { - $attachments = $this->fetchBodyAttachments($mbox, $i, $email_dir, $uniqid, $ripmime_path); - if($attachments != 1) { - foreach ($attachments as $attachment) { - if($attachment['type'] == 'text/html') { //Assuming All HTML attachments are the body of the email - if(file_exists($email_dir.'/'.$uniqid.'/'.$attachment['name'])) { - $filecontents = file_get_contents($email_dir.'/'.$attachment['name']); - $size = filesize($email_dir.'/'.$attachment['name']); - $this->data['Email']['body'] .= $filecontents; - } - - } - if($attachment['type'] == 'text/plain') { //Found plain text - if(file_exists($email_dir.'/'.$attachment['name'])) { - $filecontents = file_get_contents($email_dir.'/'.$attachment['name']); - $size = filesize($email_dir.'/'.$attachment['name']); - $this->data['Email']['plainbody'] .= $filecontents; - } - - } - } - - } - } - */ - - /* - if( ($this->data['Email']['body'] != "") || ($this->data['Email']['plainbody'] != "") || ($attachments != 1) ) { - - if($this->Email->save($this->data)) { - $email_id = $this->Email->id; - if($attachments != 1) { - foreach ($attachments as $attachment) { - if(file_exists($email_dir.'/'.$attachment['name']) != FALSE) { - - if( ($attachment['type'] != 'text/html') && ($attachment['type'] != 'multipart/mixed') && - ($attachment['type'] != 'multipart/alternative') && - ($attachment['type'] != 'multipart/report') && - ($attachment['type'] != 'text/plain') && - ($attachment['type'] != 'text/rfc822-headers') && - ($attachment['type'] != 'message/delivery-status') - - - ) { - $this->EmailAttachment->create(); - $this->data['EmailAttachment']['email_id'] = $email_id; - $this->data['EmailAttachment']['name'] = $attachment['name']; - $this->data['EmailAttachment']['type'] = $attachment['type']; - $this->data['EmailAttachment']['size'] = filesize($email_dir.'/'.$attachment['name']); - $this->data['EmailAttachment']['filename'] = $email_dir.'/'.$attachment['name']; - if ($this->EmailAttachment->save($this->data)) { - echo "Saved file successfully to database\n"; - } - else { - echo "Error saving the file to the DB\n"; - } - } - - } - } - } - echo "Email stored in the DB under enquiry ID".$enqID." Message will now be deleted\n"; - - //$storedArray[] = $i; - - - } - else { - echo 'Unable to save the Email\n'; - } - } else { - echo "Unable to find either HTML or Plaintext body to this email. Ignoring it"; - } - } else { - /* Can't find a valid-looking CMC Enquiry Number. Move the message to the discarded folder - * I may change this to simply delete the emails. This will do for now, but it's doubling up on the storage for useless files. - * */ - - // $discardArray[] = $i; - - + foreach($recipients['cc'] as $email) { + $saveThis = $this->userExists($email, $userMap); } + //print_r($recipients); + if(!isset($this_header->subject)) { //Emails without a subject are not welcome. Skip it. + $discard[] = $this_uid; + continue; + } + + $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) { + + $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 + ); + + $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 "Fatal Error: Unable to create user for email '{$recEmailLC}'\n"; + continue; + } + + } + } + } + + + + + + + echo "---------START MESSAGE -----------------\n"; + echo "Email No: $i\n"; + echo "Subject: ".$subjDecoded[0]->text."\n"; + + + + + $newEmail = array(); + + + $structure = imap_fetchstructure($mbox, $i); + //print_r($recipients); + //print_r($recipientsIDs); + //print_r($structure); + + + + + + /** 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.\n"; + continue; + + } +//die("Email has no from Recipient!! Critical Failure"); //Change this to logging for deployment. + + +// $newEmail['Email']['date'] = $this_header->date; + $newEmail['Email']['udate'] = $this_header->udate; + $newEmail['Email']['subject'] = $subjDecoded[0]->text; //Hacky. Will probably fix this quickly. + + $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; + + case 'cc': + $newEmail['EmailRecipient'][$recipientCount]['type'] = 'cc'; + $recipientCount++; + break; + default: + } + } + } + + /** End Email creation **/ + + + /** Start Attachments **/ + + $relative_path = $this->getAttachmentDirectory($email_dir, $unix_time); + $attachments = $this->fetchBodyAttachments($mbox, $i, $email_dir, $relative_path, $ripmime_path); + + $attachmentCount = 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++; + } + + /** End attachments */ + + + /* 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_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_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); + + $this->Email->create(); + $emailID = $this->Email->saveAll($newEmail); + if(!empty($emailID)) { + echo "Success. We made an email. Storing Email UID $this_uid\n"; + } + else { + + echo "Error! Didn't save the email\n"; + } + $store[] = $this_uid; + + + echo "--------END MESSAGE ------\n"; + } + else { + $discard[] = $this_uid; + echo "email {$i} will not be saved. Subject: ".$subjDecoded[0]->text."\n"; + } + // print_r($recipients); + // echo "----------------------\n"; } + reset($discard); + reset($store); - - /* Finished working with the IMAP server. Make the changes and close the connection */ - - //if($testing == 0) { - - - - echo "Tidying up now. Moving messages to Stored or Discarded\n"; - - - reset($discardArray); - reset($storedArray); - - $numberToStore = count($storedArray); - $numberToDiscard= count($discardArray); + $numberToStore = count($store); + $numberToDiscard= count($discard); if($numberToStore > 0) { echo "Got $numberToStore messages to store\n"; - $storeSet = implode(",", $storedArray); - imap_mail_move($mbox, $storeSet, 'INBOX/Stored'); + $storeSet = implode(",", $store); } if($numberToDiscard > 0) { echo "Going to discard $numberToDiscard messages\n"; - $discardSet = implode(",",$discardArray); - imap_mail_move($mbox, $discardSet, 'INBOX/Discarded'); } - - - - - - - - //} - - 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 $recipient + * @param $emailMap + * @return + */ + 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 $subject + * @param $list + * @param $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 $found_identifier + * @return + */ + function foundIdentifier(&$found_identifier) { + if(count($found_identifier) > 0) { + return true; + } + else { + return; + } + } + + + + + 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; + } + } + + + + /* * Fetches the body and attachments from a MIME encoded email. Uses ripmime to do the decoding * */ - /* function fetchBodyAttachments($mailbox, $msg_number, $email_dir, $uniqid, $ripmime_path) { + function fetchBodyAttachments($mailbox, $msg_number, $email_dir, $relative_path, $ripmime_path) { - - - $email_file = $email_dir.'/'.$uniqid.'.eml'; + $uuid = String::uuid(); + $email_file = $email_dir.'/'.$uuid.'.eml'; imap_savebody($mailbox, $email_file, $msg_number); - $command = "$ripmime_path -i $email_file -d $email_dir --prefix --paranoid -v --verbose-contenttype"; + $command = "$ripmime_path -i $email_file -d $email_dir/$relative_path --prefix --paranoid -v --verbose-contenttype --recursion-max 30"; $output = array(); exec($command, $output, $status); /* Check the $output array and find the filenames of the attachments */ -/* + if($status == 0) { $attachments = array(); + $biggestHTML_num = 999; //Determine the likely HTML body of this email. Guessing, but hopefully reasonably correctly. + $biggestHTML_size = 0; + $biggestPlain_num = 999; //I dont think we'll get legit emails with >999 attachments. + $biggestPlain_size =0; + for($i=0, $j=0; $i< count($output); $i++, $j++) { $words = explode(' ', $output[$i]); $type = explode('=', $words[1]); $name = explode('=', $words[2]); - if($type[1] == "" || $name[1] == "") { + /*echo "OUTPUT\n"; + print_r($output); + echo count($output)."\n"; + + echo "TYPE\n"; + print_r($type); + + echo count($type)."\n"; + */ + if(count($type) != 2) { + //echo "Didnt find a proper type. Skipping it."; continue; } + if(count($name) != 2) { + //echo "Didnt find a proper name. Skipping it"; + continue; + } + + $oldNamePath = $email_dir.'/'.$relative_path.'/'.$name[1]; + $newNamePath = $email_dir.'/'.$relative_path.'/'.$uuid.'-'.$name[1]; + $newName = $relative_path.'/'.$uuid.'-'.$name[1]; + rename($oldNamePath, $newNamePath); + $size = filesize($newNamePath); $attachments[$j]['type'] = $type[1]; - $attachments[$j]['name'] = $name[1]; - //echo "in message number $msg_number ($uniqid): found attachment ".$attachments[$j]['name'].' '.$attachments[$j]['type']."\n"; + + + $attachments[$j]['name'] = $newName; + $attachments[$j]['filename'] = $name[1]; + $attachments[$j]['size'] = $size; + $attachments[$j]['is_message_body'] = 0; + echo "in message number $msg_number: found attachment ".$attachments[$j]['name'].' '.$attachments[$j]['type']."\n"; + + + /** Record the array ID and size of the largest HTML & plain attachments **/ + if($attachments[$j]['type'] == 'text/html') { + if($size > $biggestHTML_size) { + $biggestHTML_num = $j; + $biggestHTML_size = $size; + } + } + else if($attachments[$j]['type'] == 'text/plain') { + if($size > $biggestPlain_size) { + $biggestPlain_num = $j; + $biggestPlain_size = $size; + } + } } + if($biggestHTML_num != 999) { //We found an HTML attachment. + $attachments[$biggestHTML_num]['is_message_body'] = 1; + } + else if($biggestPlain_num != 999) { + $attachments[$biggestPlain_num]['is_message_body'] = 1; + } + unlink($email_file); return $attachments; - } else { unlink($email_file); return 1; } - }*/ - - - 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'] = $recipients['to']; - $message['from'] = $recipients['from']; - $message['cc'] = $recipients['cc']; - return $message; - - } - /* get Recipients from the headers of an email - * - * Will need to rework this 2011 - */ - 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; } - function getBody($mbox, $msgnumber) { - $structure = imap_fetchstructure($mbox, $msgnumber); - if (!empty($structure->parts)) { - $body = $this->getParts($structure->parts, $mbox, $msgnumber); - } - else { - $body = imap_body($mbox, $msgnumber); /* Or get the plaintext */ - } - return $body; - } - - 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]; - - if(array_key_exists($fetched_enquirynumber, $enqList)) { //check if it actually exists. - $enqid = $enqList[$fetched_enquirynumber]; - - return $enqid; - } - else { - return false; - } - } - else { - return false; - } - - } - /** - * Adapted from - * http://www.electrictoolbox.com/function-extract-email-attachments-php-imap/ - * @param $connection - * @param $message_number - * @return + * Passed the configured $email_dir and the unix time of this message. + * + * 1. Checks to see if the /emails/MM-YYYY/ directory exists. + * 2a. If not, creates it and returns the full path to this directory. + * 2b. If exists, returns the path to this directory. + * + * @param $email_dir + * @param $unix_time */ - function extract_attachments($connection, $structure, $message_number) { + function getAttachmentDirectory($email_dir, $unix_time) { - $attachments = array(); - //$structure = imap_fetchstructure($connection, $message_number); - - 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']) { - echo "Encoding is".$structure->parts[$i]->encoding."\n"; - $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']); - } - } + $month_year = date('m-Y', $unix_time); + $path = $email_dir."/".$month_year; + if(!file_exists($path)) { //Directory doesn't exist Lets make it! + if(mkdir($path)) { + return $month_year; } - + else { + die("Unable to make directory $email_dir/$month_year"); + } + } + else { + return $month_year; } - return $attachments; - } + /** + * The attachments have been decoded and written to disk by ripmime. + * + * 1. Generate a uuid for this email + * 2. Is there a month-year folder this this current month? If not, make one. + * 3. Move the attachments to the correct month-year Folder. Prefix their new names with uuid. + * 4. + * + * @param $attachments - /* - * Fetches the body and attachments from a MIME encoded email. Uses ripmime to do the decoding - * - */ + function moveAttachments($attachments, $email_dir, $unix_time) { - function fetchBodyAttachments($mailbox, $msg_number, $email_dir, $ripmime_path) { + $month_year = date('m-Y'); + + + $uuid = String::uuid(); - $email_file = $email_dir.'/'.$uniqid.'.eml'; - imap_savebody($mailbox, $email_file, $msg_number); - $command = "$ripmime_path -i $email_file -d $email_dir --prefix --paranoid -v --verbose-contenttype"; - $output = array(); - exec($command, $output, $status); + foreach($attachments as $attachment) { + echo $email_dir.'/'.$attachment['name']."\n"; + } + die(); - /* Check the $output array and find the filenames of the attachments */ - - if($status == 0) { - $attachments = array(); - - for($i=0, $j=0; $i< count($output); $i++, $j++) { - $words = explode(' ', $output[$i]); - $type = explode('=', $words[1]); - $name = explode('=', $words[2]); - - if($type[1] == "" || $name[1] == "") { - continue; - } - - $attachments[$j]['type'] = $type[1]; - $attachments[$j]['name'] = $name[1]; - //echo "in message number $msg_number ($uniqid): found attachment ".$attachments[$j]['name'].' '.$attachments[$j]['type']."\n"; - } - unlink($email_file); - return $attachments; - - } - else { - unlink($email_file); - return 1; - } - } + }*/ } -?> + + +?> \ No newline at end of file diff --git a/vendors/shells/vault_two.php b/vendors/shells/vault_two.php index 0e4359a8..de63df17 100755 --- a/vendors/shells/vault_two.php +++ b/vendors/shells/vault_two.php @@ -38,6 +38,7 @@ class VaultTwoShell extends Shell { else { $ripmime_path = '/usr/local/bin/ripmime'; $email_dir = '/var/www/cakephp/app/emails'; + $mbox = imap_open("{192.168.0.8:143/novalidate-cert}INBOX", $username, $password) or die("can't connect: " . imap_last_error()); } diff --git a/views/elements/job_table.ctp b/views/elements/job_table.ctp index e87305cf..6005e183 100644 --- a/views/elements/job_table.ctp +++ b/views/elements/job_table.ctp @@ -51,7 +51,7 @@ - link(__($job['Job']['title'], true), array('action' => 'view', $job['Job']['id'])); ?> + link(__($job['Job']['title'], true), array('controller'=>'jobs', 'action' => 'view', $job['Job']['id'])); ?> link($job['Enquiry']['title'], array('controller' => 'enquiries', 'action' => 'view', $job['Enquiry']['id'])); ?>