Vault fixed. Going live on helios now

This commit is contained in:
Karl Cordes 2010-04-27 15:36:18 +10:00
parent 4a0e0fc32f
commit 195e319844

View file

@ -13,12 +13,12 @@ class VaultShell extends Shell {
/****************************************************** /******************************************************
* Config Variables * Config Variables
* *****************************************************/ * *****************************************************/
$testing = 0; //Whether to actually move the emails. 1=test, 0=production $testing = 1; //Whether to actually move the emails. 1=test, 0=production
/* Setup Connection to the IMAP server */ /* Setup Connection to the IMAP server */
$username = 'vault'; $username = 'vault';
$password = 'xjdYOsmJWc37'; /* The password for the account to be checked */ $password = 'xjdYOsmJWc37'; /* The password for the account to be checked */
$email_dir = '/var/www/cakephp/app/emails'; $email_dir = '/var/www/cakephp/app/emails';
$temp_filename = 'temp.eml';
$ripmime_path = '/usr/local/bin/ripmime'; $ripmime_path = '/usr/local/bin/ripmime';
@ -30,7 +30,7 @@ class VaultShell extends Shell {
$ripmime_path = '/opt/local/bin/ripmime'; $ripmime_path = '/opt/local/bin/ripmime';
} }
$mbox = imap_open("{mail.cmctechnologies.com.au:143/novalidate-cert}INBOX", $username, $password) or die("can't connect: " . imap_last_error()); $mbox = imap_open("{mail.cmctechnologies.com.au:143}INBOX", $username, $password) or die("can't connect: " . imap_last_error());
$MC = imap_check($mbox); $MC = imap_check($mbox);
$number_of_messages = $MC->Nmsgs; $number_of_messages = $MC->Nmsgs;
@ -66,28 +66,28 @@ class VaultShell extends Shell {
$this->data['Email']['subject'] = $message['subject']; $this->data['Email']['subject'] = $message['subject'];
$this->data['Email']['body'] = ""; $this->data['Email']['body'] = "";
$this->data['Email']['plainbody'] = ""; $this->data['Email']['plainbody'] = "";
$this->data['Email']['uniqid'] = $uniqid;
$structure = imap_fetchstructure($mbox, $i); $structure = imap_fetchstructure($mbox, $i);
$attachments = 1; $attachments = 1;
if (empty($structure->parts)) { /* A single part message. No attachments and is plain text */ if (empty($structure->parts)) { /* A single part message. No attachments and is plain text */
$this->data['Email']['body'] = imap_body($mbox, $i); $this->data['Email']['body'] = imap_body($mbox, $i);
} }
else { else {
$attachments = $this->fetchBodyAttachments($mbox, $i, $temp_filename, $email_dir, $uniqid, $ripmime_path); $attachments = $this->fetchBodyAttachments($mbox, $i, $email_dir, $uniqid, $ripmime_path);
if($attachments != 1) { if($attachments != 1) {
foreach ($attachments as $attachment) { foreach ($attachments as $attachment) {
if($attachment['type'] == 'text/html') { //Assuming All HTML attachments are the body of the email if($attachment['type'] == 'text/html') { //Assuming All HTML attachments are the body of the email
if(file_exists($email_dir.'/'.$uniqid.'/'.$attachment['name'])) { if(file_exists($email_dir.'/'.$uniqid.'/'.$attachment['name'])) {
$filecontents = file_get_contents($email_dir.'/'.$uniqid.'/'.$attachment['name']); $filecontents = file_get_contents($email_dir.'/'.$attachment['name']);
$size = filesize($email_dir.'/'.$uniqid.'/'.$attachment['name']); $size = filesize($email_dir.'/'.$attachment['name']);
$this->data['Email']['body'] .= $filecontents; $this->data['Email']['body'] .= $filecontents;
} }
} }
if($attachment['type'] == 'text/plain') { //Found plain text if($attachment['type'] == 'text/plain') { //Found plain text
if(file_exists($email_dir.'/'.$attachment['name'])) { if(file_exists($email_dir.'/'.$attachment['name'])) {
$filecontents = file_get_contents($email_dir.'/'.$uniqid.'/'.$attachment['name']); $filecontents = file_get_contents($email_dir.'/'.$attachment['name']);
$size = filesize($email_dir.'/'.$uniqid.'/'.$attachment['name']); $size = filesize($email_dir.'/'.$attachment['name']);
$this->data['Email']['plainbody'] .= $filecontents; $this->data['Email']['plainbody'] .= $filecontents;
} }
@ -104,16 +104,19 @@ class VaultShell extends Shell {
$email_id = $this->Email->id; $email_id = $this->Email->id;
if($attachments != 1) { if($attachments != 1) {
foreach ($attachments as $attachment) { foreach ($attachments as $attachment) {
if(file_exists($email_dir.'/'.$uniqid.'/'.$attachment['name']) != FALSE) { if(file_exists($email_dir.'/'.$attachment['name']) != FALSE) {
if( ($attachment['type'] != 'text/html') && ($attachment['type'] != 'multipart/mixed') && if( ($attachment['type'] != 'text/html') && ($attachment['type'] != 'multipart/mixed') &&
($attachment['type'] != 'multipart/alternative') ) { ($attachment['type'] != 'multipart/alternative') &&
($attachment['type'] != 'multipart/report')
) {
$this->EmailAttachment->create(); $this->EmailAttachment->create();
$this->data['EmailAttachment']['email_id'] = $email_id; $this->data['EmailAttachment']['email_id'] = $email_id;
$this->data['EmailAttachment']['name'] = $attachment['name']; $this->data['EmailAttachment']['name'] = $attachment['name'];
$this->data['EmailAttachment']['type'] = $attachment['type']; $this->data['EmailAttachment']['type'] = $attachment['type'];
$this->data['EmailAttachment']['size'] = filesize($email_dir.'/'.$uniqid.'/'.$attachment['name']); $this->data['EmailAttachment']['size'] = filesize($email_dir.'/'.$attachment['name']);
$this->data['EmailAttachment']['filename'] = $email_dir.'/'.$uniqid.'/'.$attachment['name']; $this->data['EmailAttachment']['filename'] = $email_dir.'/'.$attachment['name'];
if ($this->EmailAttachment->save($this->data)) { if ($this->EmailAttachment->save($this->data)) {
echo "Saved file successfully to database\n"; echo "Saved file successfully to database\n";
} }
@ -149,43 +152,16 @@ class VaultShell extends Shell {
* 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. * 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.
* */ * */
$discarded_msgs[] = imap_uid($mbox,$i); if($testing == 0 ) {
imap_mail_move($mbox, $no, 'INBOX/Discard');
}
} }
} }
// $this->rmdirr($email_dir); //delete all attachments from the working directory. Easiest way to stop random files persisting
/* if(isset($stored_msgs)) {
foreach($stored_msgs as $msg) {
$no = imap_msgno($mbox,$msg);
$this_header = imap_headerinfo($mbox, $no);
echo "Going to store: $no\t $msg\t".$this_header->subject."\n";
if($testing == 0) {
imap_mail_move($mbox, $no, 'INBOX/Stored');
}
}
}
if(isset($discarded_msgs)) {
foreach($discarded_msgs as $msg) {
$no = imap_msgno($mbox,$msg);
$this_header = imap_headerinfo($mbox, $no);
echo "Going to discard: $no\t $msg\t".$this_header->subject."\n";
if($testing == 0) {
imap_mail_move($mbox,$no, 'INBOX/Discarded');
}
}
}
* */
/* Finished working with the IMAP server. Make the changes and close the connection */ /* Finished working with the IMAP server. Make the changes and close the connection */
imap_expunge($mbox); imap_expunge($mbox);
@ -200,17 +176,13 @@ class VaultShell extends Shell {
* *
*/ */
function fetchBodyAttachments($mailbox, $msg_number, $filename, $email_dir, $uniqid, $ripmime_path) { function fetchBodyAttachments($mailbox, $msg_number, $email_dir, $uniqid, $ripmime_path) {
$mkdirresult = mkdir ($email_dir.'/'.$uniqid);
if($mkdirresult == FALSE) {
echo "ERROR: Failed to make ".$email_dir.'/'.$uniqid;
return 1;
}
$email_file = $email_dir.'/'.$uniqid.'/'.$filename; $email_file = $email_dir.'/'.$uniqid.'.eml';
imap_savebody($mailbox, $email_file, $msg_number); imap_savebody($mailbox, $email_file, $msg_number);
$command = "$ripmime_path -i $email_file -d $email_dir/$uniqid -v --verbose-contenttype --paranoid --prefix $msg_number"; $command = "$ripmime_path -i $email_file -d $email_dir --prefix --paranoid -v --verbose-contenttype";
$output = array(); $output = array();
exec($command, $output, $status); exec($command, $output, $status);
@ -226,7 +198,7 @@ class VaultShell extends Shell {
$name = explode('=', $words[2]); $name = explode('=', $words[2]);
$attachments[$j]['type'] = $type[1]; $attachments[$j]['type'] = $type[1];
$attachments[$j]['name'] = $name[1]; $attachments[$j]['name'] = $name[1];
echo "in message number $msg_number: found attachment ".$attachments[$j]['name'].' '.$attachments[$j]['type']."\n"; echo "in message number $msg_number ($uniqid): found attachment ".$attachments[$j]['name'].' '.$attachments[$j]['type']."\n";
} }
return $attachments; return $attachments;
} }
@ -236,19 +208,6 @@ class VaultShell extends Shell {
} }
/* clearEmailyAttachmentDirs($email_dir, $filename, $attachment_dir, $attachments)
*
* Deletes $email_dir/$filename.eml and the attachments specified by $attachments
* */
function clearEmailAttachmentDirs($email_dir, $filename, $attachments) {
unlink("$email_dir/$filename");
foreach ($attachments as $attachment) {
unlink($email_dir.'/'.$attachment['name']);
}
}
function getMessage($mbox, $msgnumber, $headers) { function getMessage($mbox, $msgnumber, $headers) {
$subject = $headers->subject; $subject = $headers->subject;
@ -338,54 +297,16 @@ class VaultShell extends Shell {
} }
/**
* Delete a file, or a folder and its contents (recursive algorithm)
*
* @author Aidan Lister <aidan@php.net>
* @version 1.0.3
* @link http://aidanlister.com/repos/v/function.rmdirr.php
* @param string $dirname Directory to delete
* @return bool Returns TRUE on success, FALSE on failure
*/
function rmdirr($dirname) {
// Sanity check
if (!file_exists($dirname)) {
return false;
}
// Simple delete for a file
if (is_file($dirname) || is_link($dirname)) {
return unlink($dirname);
}
// Loop through the folder
$dir = dir($dirname);
while (false !== $entry = $dir->read()) {
// Skip pointers
if ($entry == '.' || $entry == '..') {
continue;
}
// Recurse
$this->rmdirr($dirname . DIRECTORY_SEPARATOR . $entry);
}
// Clean up
$dir->close();
return; //removing the last recursion - should remove the deletion of the last directory I hope. Lazy hack using this but should work
//rmdir($dirname);
}
/** /**
* Generate a uniq id. * Generate a uniq id.
* @todo Ensure the sub-directory doesn't exist to avoid collisions *
* *
*/ */
function getUniqId($email_dir) { function getUniqId($email_dir) {
$uniqid = uniqid(null,TRUE); $uniqid = uniqid(null,TRUE);
//while(is_dir($email_dir."/".$uniqid) == TRUE) {
//
// }
return $uniqid; return $uniqid;
} }