2009-02-05 15:10:57 -08:00
|
|
|
<?
|
|
|
|
|
App::import('Sanitize');
|
|
|
|
|
class VaultShell extends Shell {
|
|
|
|
|
/* vault.php - To be run in CLI via Cron. Fetches the mail from a certain mailbox, searches for a CMC Technologies Enquiry Numbeer
|
|
|
|
|
* then loads the data into the 'emails' table and the attachments in the 'email_attachments' table.
|
|
|
|
|
* Emails are discared or moved to a subfolder once processed
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var $uses = array('Enquiry', 'Email', 'EmailAttachments');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function main() {
|
|
|
|
|
/* Setup Connection to the IMAP server */
|
|
|
|
|
$username = 'vault';
|
|
|
|
|
$password = 'xjdYOsmJWc37'; /* The password for the account to be checked */
|
2009-02-08 14:21:34 -08:00
|
|
|
|
|
|
|
|
$email_dir = '/var/www/quotenik1.2/app/emails/working';
|
|
|
|
|
$attachment_dir = '/var/www/quotenik1.2/app/emails/working/attachments';
|
|
|
|
|
$temp_filename = 'temp.eml';
|
|
|
|
|
|
|
|
|
|
|
2009-02-05 15:10:57 -08:00
|
|
|
$mbox = imap_open("{saturn:143/novalidate-cert}INBOX", $username, $password) or die("can't connect: " . imap_last_error());
|
|
|
|
|
$MC = imap_check($mbox);
|
|
|
|
|
$number_of_messages = $MC->Nmsgs;
|
|
|
|
|
|
|
|
|
|
/* Loop through the messages and sort them into ones to be processed or discarded */
|
|
|
|
|
for ($i=1; $i <= $number_of_messages; $i++) {
|
|
|
|
|
$this_header = imap_headerinfo($mbox, $i);
|
|
|
|
|
preg_match("/CMC\d+([NVQWSO]|ACT|NT)E\d+-\d+/", $this_header->subject, $output);
|
|
|
|
|
if(isset($output[0])) {
|
|
|
|
|
|
|
|
|
|
$fetched_enquirynumber = $output[0];
|
|
|
|
|
$enquiry = $this->Enquiry->findByTitle($fetched_enquirynumber);
|
|
|
|
|
|
|
|
|
|
if($enquiry['Enquiry']['id'] == NULL) { //It passes the Regex - but we don't find an Enquiry for it in the MER.
|
|
|
|
|
imap_mail_move($mbox, $i, 'INBOX/Discarded'); //Discard it.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else { //Process it and store the message and its attachments.
|
|
|
|
|
|
2009-02-08 14:21:34 -08:00
|
|
|
$message = $this->getMessage($mbox, $i, $this_header);
|
2009-02-05 15:10:57 -08:00
|
|
|
$this->Email->create();
|
|
|
|
|
$this->data['Email']['enquiry_id'] = $enquiry['Enquiry']['id'];
|
2009-02-08 14:21:34 -08:00
|
|
|
$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'];
|
2009-02-05 15:10:57 -08:00
|
|
|
|
2009-02-08 14:21:34 -08:00
|
|
|
//Sanitize::clean($this->data);
|
2009-02-05 15:10:57 -08:00
|
|
|
|
|
|
|
|
if($this->Email->save($this->data)) {
|
2009-02-08 14:21:34 -08:00
|
|
|
$email_id = $this->Email->id;
|
|
|
|
|
|
|
|
|
|
$attachment_files = $this->fetchAttachments($mbox, $i, $temp_filename, $email_dir, $attachment_dir);
|
|
|
|
|
foreach ($attachment_files as $attachment) {
|
|
|
|
|
$this->EmailAttachment->create();
|
|
|
|
|
$this->data['EmailAttachment']['email_id'] = $email_id;
|
|
|
|
|
$this->data['EmailAttachment']['name'] = $attachment;
|
|
|
|
|
$this->data['EmailAttachment']['type'] = mime_content_type("$attachment_dir/$attachment"); //Depreciated but works better than the new one
|
|
|
|
|
$this->data['EmailAttachment']['size'] = filesize("$attachment_dir/$attachment");
|
|
|
|
|
$this->data['EmailAttachment']['data'] = fread(fopen("$attachment_dir/$attachment", "r"), $this->data['EmailAttachment']['size']);
|
|
|
|
|
if ($this->EmailAttachment->save($this->data)) {
|
|
|
|
|
echo "Saved file successfully to database\n";
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
echo "Something went wrong saving the file to the DB\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
echo "Email stored in the DB under enquiry ".$enquiry['Enquiry']['title']." Will be moved to the stored folder\n";
|
|
|
|
|
$this->clearEmailAttachmentDirs($email_dir, $temp_filename, $attachment_dir, $attachments);
|
2009-02-05 15:10:57 -08:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
echo 'Unable to save the Email\n';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
|
* */
|
|
|
|
|
if(imap_mail_move($mbox, $i, 'INBOX/Discarded')) {
|
|
|
|
|
echo "Message Number $i contains no valid Enquiry number. Has been discarded\n";
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
echo "Message Number $i contains no valid Enquiry number. But could not be discarded\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Finished working with the IMAP server. Make the changes and close the connection */
|
|
|
|
|
imap_expunge($mbox);
|
|
|
|
|
imap_close($mbox);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-02-08 14:21:34 -08:00
|
|
|
/* Fetches and Writes attachements to a directory under $email_dir.
|
|
|
|
|
* Uses uudeview to do the decoding
|
|
|
|
|
* */
|
|
|
|
|
|
|
|
|
|
function fetchAttachments($mailbox, $msg_number, $filename, $email_dir, $attachment_dir) {
|
|
|
|
|
|
|
|
|
|
/* Make the temporary directory to store this email and the attachments */
|
|
|
|
|
if (!file_exists($email_dir)){
|
|
|
|
|
mkdir($email_dir);
|
|
|
|
|
}
|
|
|
|
|
if (!file_exists($attachment_dir)) {
|
|
|
|
|
mkdir($attachment_dir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$email_file = $email_dir.'/'.$filename;
|
|
|
|
|
imap_savebody($mailbox, $email_file, $msg_number);
|
|
|
|
|
$command = "uudeview -i $email_file -p $attachment_dir";
|
|
|
|
|
$output = array();
|
|
|
|
|
exec($command, $output);
|
|
|
|
|
|
|
|
|
|
/* Check the $output array and find the filenames of the attachments */
|
|
|
|
|
$filenames = array();
|
|
|
|
|
|
|
|
|
|
for($i=0; $i< count($output); $i++) {
|
|
|
|
|
$matches = preg_match("/\bsuccessfully\b/", $output[$i]);
|
|
|
|
|
if($matches > 0) {
|
|
|
|
|
$result = explode("/", $output[$i]);
|
|
|
|
|
$filenames[] = $result[8]; //
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $filenames;
|
|
|
|
|
}
|
2009-02-05 15:10:57 -08:00
|
|
|
|
2009-02-08 14:21:34 -08:00
|
|
|
/* clearEmailyAttachmentDirs($email_dir, $filename, $attachment_dir, $attachments)
|
|
|
|
|
*
|
|
|
|
|
* Deletes $email_dir/$filename.eml and the attachments specified in $attachment_dir
|
|
|
|
|
* */
|
|
|
|
|
function clearEmailAttachmentDirs($email_dir, $filename, $attachment_dir, $attachments) {
|
|
|
|
|
unlink("$email_dir/$filename");
|
|
|
|
|
foreach ($attachments as $attachment) {
|
|
|
|
|
unlink("$attachment_dir/$attachment");
|
|
|
|
|
}
|
2009-02-05 15:10:57 -08:00
|
|
|
}
|
|
|
|
|
|
2009-02-08 14:21:34 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
function getMessage($mbox, $msgnumber, $headers) {
|
|
|
|
|
|
|
|
|
|
$subject = $headers->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'];
|
|
|
|
|
$message['body'] = imap_fetchbody($mbox, $msgnumber, '1');
|
|
|
|
|
return $message;
|
|
|
|
|
|
2009-02-05 15:10:57 -08:00
|
|
|
}
|
2009-02-08 14:21:34 -08:00
|
|
|
/* get Recipients from the headers of an email */
|
|
|
|
|
function getRecipients($headers) {
|
|
|
|
|
|
|
|
|
|
$to = $headers->to;
|
|
|
|
|
$cc = $headers->cc;
|
|
|
|
|
$from = $headers->from;
|
|
|
|
|
$recipients = array();
|
|
|
|
|
if(count($to) > 0) {
|
|
|
|
|
foreach ($to as $id => $object) {
|
|
|
|
|
if(isset($object->personal)) {
|
|
|
|
|
$recipients['to'] .= "$object->personal $object->mailbox@$object->host ";
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$recipients['to'] .= "$object->mailbox@$object->host ";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(count($cc) > 0) {
|
|
|
|
|
foreach ($cc as $id => $object) {
|
|
|
|
|
if(isset($object->personal)) {
|
|
|
|
|
$recipients['cc'] .= "$object->personal $object->mailbox@$object->host ";
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$recipients['cc'] .= "$object->mailbox@$object->host ";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(count($from) > 0) {
|
|
|
|
|
foreach ($from as $id => $object) {
|
|
|
|
|
if(isset($object->personal)) {
|
|
|
|
|
$recipients['from'] .= "$object->personal $object->mailbox@$object->host";
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$recipients['from'] .= "$object->mailbox@$object->host";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $recipients;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-02-05 15:10:57 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
?>
|