Changing Vault to only handle email addresses

This commit is contained in:
Karl Cordes 2009-02-09 09:21:34 +11:00
parent 4fb5da0dd8
commit 5aaf066506

View file

@ -14,6 +14,12 @@ class VaultShell extends Shell {
/* Setup Connection to the IMAP server */
$username = 'vault';
$password = 'xjdYOsmJWc37'; /* The password for the account to be checked */
$email_dir = '/var/www/quotenik1.2/app/emails/working';
$attachment_dir = '/var/www/quotenik1.2/app/emails/working/attachments';
$temp_filename = 'temp.eml';
$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;
@ -32,24 +38,39 @@ class VaultShell extends Shell {
}
else { //Process it and store the message and its attachments.
echo $enquiry['Contact']['first_name'].' '.$enquiry['Contact']['last_name']."\n";
$message = $this->getMessage($mbox, $i, $this_header);
$this->Email->create();
$this->data['Email']['enquiry_id'] = $enquiry['Enquiry']['id'];
$this->data['Email']['to_address'] = $this_header->to[0]->mailbox.'@'.$this_header->to[0]->host;
$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']['from_address'] = $this_header->from[0]->mailbox.'@'.$this_header->from[0]->host;
$this->data['Email']['to_name'] = $this_header->toaddress;
//Sanitize::clean($this->data);
$this->data['Email']['from_name'] = $this_header->fromaddress;
$this->data['Email']['date'] = $this_header->date;
$this->data['Email']['subject'] = $this_header->subject;
$body = imap_fetchbody($mbox, $i, '1');
//$body = Sanitize::html($body, true);
$this->data['Email']['body'] = $body;
if($this->Email->save($this->data)) {
echo "Email stored in the DB under enquiry ".$enquiry['Enquiry']['title']."\n";
$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);
}
else {
echo 'Unable to save the Email\n';
@ -80,94 +101,115 @@ class VaultShell extends Shell {
//Adapted from Code posted on the PHP manual by david at hundsness.com.
function getmsg($mbox,$mid) {
// input $mbox = IMAP stream, $mid = message id
// output all the following:
global $htmlmsg,$plainmsg,$charset,$attachments;
// the message may in $htmlmsg, $plainmsg, or both
$htmlmsg = $plainmsg = $charset = '';
$attachments = array();
// HEADER
$h = imap_header($mbox,$mid);
// add code here to get date, from, to, cc, subject...
/* 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]; //
}
}
// BODY
$s = imap_fetchstructure($mbox,$mid);
if (!$s->parts) // not multipart
getpart($mbox,$mid,$s,0); // no part-number, so pass 0
else { // multipart: iterate through each part
foreach ($s->parts as $partno0=>$p)
getpart($mbox,$mid,$p,$partno0+1);
}
return $filenames;
}
function getpart($mbox,$mid,$p,$partno) {
// $partno = '1', '2', '2.1', '2.1.3', etc if multipart, 0 if not multipart
global $htmlmsg,$plainmsg,$charset,$attachments;
// DECODE DATA
$data = ($partno)?
imap_fetchbody($mbox,$mid,$partno): // multipart
imap_body($mbox,$mid); // not multipart
// Any part may be encoded, even plain text messages, so check everything.
if ($p->encoding==4)
$data = quoted_printable_decode($data);
elseif ($p->encoding==3)
$data = base64_decode($data);
// no need to decode 7-bit, 8-bit, or binary
// PARAMETERS
// get all parameters, like charset, filenames of attachments, etc.
$params = array();
if ($p->parameters)
foreach ($p->parameters as $x)
$params[ strtolower( $x->attribute ) ] = $x->value;
if ($p->dparameters)
foreach ($p->dparameters as $x)
$params[ strtolower( $x->attribute ) ] = $x->value;
// ATTACHMENT
// Any part with a filename is an attachment,
// so an attached text file (type 0) is not mistaken as the message.
if ($params['filename'] || $params['name']) {
// filename may be given as 'Filename' or 'Name' or both
$filename = ($params['filename'])? $params['filename'] : $params['name'];
// filename may be encoded, so see imap_mime_header_decode()
$attachments[$filename] = $data; // this is a problem if two files have same name
}
// TEXT
elseif ($p->type==0 && $data) {
// Messages may be split in different parts because of inline attachments,
// so append parts together with blank row.
if (strtolower($p->subtype)=='plain')
$plainmsg .= trim($data) ."\n\n";
else
$htmlmsg .= $data ."<br><br>";
$charset = $params['charset']; // assume all parts are same charset
}
// EMBEDDED MESSAGE
// Many bounce notifications embed the original message as type 2,
// but AOL uses type 1 (multipart), which is not handled here.
// There are no PHP functions to parse embedded messages,
// so this just appends the raw source to the main message.
elseif ($p->type==2 && $data) {
$plainmsg .= trim($data) ."\n\n";
}
// SUBPART RECURSION
if ($p->parts) {
foreach ($p->parts as $partno0=>$p2)
getpart($mbox,$mid,$p2,$partno.'.'.($partno0+1)); // 1.2, 1.2.1, etc.
}
/* 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");
}
}
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;
}
/* 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;
}
}