ticket hanling function added

This commit is contained in:
Quitta 2013-08-12 23:56:58 +02:00
parent 188552a135
commit 9ac1f5ffb4
5 changed files with 111 additions and 35 deletions

View file

@ -33,6 +33,11 @@ class Mail_Handler{
return $user->getTUserId();
}
function get_id_from_email($email){
$webUserId = WebUsers::getIdFromEmail($email);
$user = Ticket_User::constr_ExternId($webUserId);
return $user->getTUserId();
}
public static function send_mail($recipient, $subject, $body, $from = NULL) {
@ -91,7 +96,8 @@ class Mail_Handler{
$emails = $statement->fetchAll();
foreach($emails as $email) {
$message_id = self::new_message_id();
$message_id = self::new_message_id($email['TicketId']);
//if recipient isn't given, then use the email of the id_user instead!
echo("Emailing {$email['Recipient']}\n");
if(!$email['Recipient']) {
@ -105,7 +111,7 @@ class Mail_Handler{
} else {
$from = $oms_reply_to;
}
$headers = "From: $from\r\n" . "Message-ID: " . $message_id;
$headers = "From: $from\r\n" . "Message-ID: " . $message_id ;
print("recip: " . $email['Recipient']);
print("subj: " .$email['Subject']);
print("body: " . $email['Body']);
@ -131,28 +137,15 @@ class Mail_Handler{
for ($i = 1; $i <= $message_count; ++$i) {
$header = imap_header($mbox, $i);
//print_r($header);
$entire_email = imap_fetchheader($mbox, $i) . imap_body($mbox, $i);
$subject = self::decode_utf8($header->subject);
$to = $header->to[0]->mailbox;
$from = $header->from[0]->mailbox . '@' . $header->from[0]->host;
$txt = self::get_part($mbox, $i, "TEXT/PLAIN");
$html = self::get_part($mbox, $i, "TEXT/HTML");
print("================");
print("subj: ".$subject);
print("to: ".$to);
print("from: ".$from);
print("txt: " .$txt);
print("html: ".$html);
//return task ID
self::incoming_mail_handler($mbox, $i);
$tid = 1; //self::ams_create_email($from, $subject, $txt, $html, $to, $from);
if($tid) {
$file = fopen($MAIL_DIR."/mail/".$tid, 'w');
//TODO: base file on Ticket + reply id
/* $file = fopen($MAIL_DIR."/mail/".$tid, 'w');
fwrite($file, $entire_email);
fclose($file);
fclose($file); */
}
//mark message $i of $mbox for deletion!
imap_delete($mbox, $i);
@ -167,26 +160,87 @@ class Mail_Handler{
function new_message_id() {
function new_message_id($ticketId) {
$time = time();
$pid = getmypid();
global $cfg;
global $ams_mail_count;
$ams_mail_count = ($ams_mail_count == '') ? 1 : $ams_mail_count + 1;
return "<ams.message".$pid.$ams_mail_count.$time."@".$cfg['mail']['host'].">";
return "<ams.message".".".$ticketId.".".$pid.$ams_mail_count.".".$time."@".$cfg['mail']['host'].">";
}
function get_ticket_id_from_subject($subject){
print('got it from subject!');
$startpos = strpos($subject, "[Ticket #");
$tempString = substr($subject, $startpos+9);
$endpos = strpos($tempString, "]");
$ticket_id = substr($tempString, 0, $endpos);
return $ticket_id;
}
function ams_create_email($from, $subject, $body, $html, $recipient = 0, $sender = NULL) {
function incoming_mail_handler($mbox,$i){
$header = imap_header($mbox, $i);
$subject = self::decode_utf8($header->subject);
print_r($header);
//get ticket_id out of the message-id or else out of the subject line
$ticket_id = 0;
if(isset($header->references)){
$pieces = explode(".", $header->references);
if($pieces[0] == "<ams"){
print('got it from message-id');
$ticket_id = $pieces[2];
}else{
$ticket_id = self::get_ticket_id_from_subject($subject);
}
}else{
print('elseeee');
$ticket_id = self::get_ticket_id_from_subject($subject);
}
//if ticket id is found
if($ticket_id){
$entire_email = imap_fetchheader($mbox, $i) . imap_body($mbox, $i);
$subject = self::decode_utf8($header->subject);
$to = $header->to[0]->mailbox;
$from = $header->from[0]->mailbox . '@' . $header->from[0]->host;
$txt = self::get_part($mbox, $i, "TEXT/PLAIN");
$html = self::get_part($mbox, $i, "TEXT/HTML");
//get the id out of the email address of the person sending the email.
if($from !== NULL && !is_numeric($from)) $from = self::get_id_from_email($from);
$user = new Ticket_User();
$user->load_With_TUserId($from);
$ticket = new Ticket();
$ticket->load_With_TId($ticket_id);
//if user has access to it!
if($user->isMod() or ($ticket->getAuthor() == $user->getTUserId())){
}
/*print("================");
print("subj: ".$subject);
print("to: ".$to);
print("from: ".$from);
print("txt: " .$txt);
print("html: ".$html);*/
}
}
/*function ams_create_email($from, $subject, $body, $html, $recipient = 0, $sender = NULL) {
//TODO:
/*if($recipient == 0 && !is_string($recipient)) {
if($recipient == 0 && !is_string($recipient)) {
global $user;
$recipient = $user->uid;
}*/
}
if($sender !== NULL && !is_numeric($sender)) $sender = self::get_id_from_username($sender);
if(!is_numeric($recipient)) $recipient = self::get_id_from_username($recipient);
@ -212,7 +266,7 @@ class Mail_Handler{
oms_task_index($message, array('subject', 'body', 'sender', 'recipient'));
//---------------------------
return $message['id_task'];
}
}*/

View file

@ -68,13 +68,21 @@ class WebUsers extends Users{
}
}
//returns te id for a given username
public static function getId($username){
$dbw = new DBLayer("web");
$statement = $dbw->execute("SELECT * FROM ams_user WHERE Login=:username", array('username' => $username));
$row = $statement->fetch();
return $row['UId'];
}
//returns te id for a given username
public static function getId($username){
$dbw = new DBLayer("web");
$statement = $dbw->execute("SELECT * FROM ams_user WHERE Login=:username", array('username' => $username));
$row = $statement->fetch();
return $row['UId'];
}
//returns te id for a given username
public static function getIdFromEmail($email){
$dbw = new DBLayer("web");
$statement = $dbw->execute("SELECT * FROM ams_user WHERE Email=:email", array('email' => $email));
$row = $statement->fetch();
return $row['UId'];
}
public function getUId(){
return $this->uId;

View file

@ -352,9 +352,11 @@
`Sender` INT(10) UNSIGNED NOT NULL ,
`UserId` INT(10) UNSIGNED NOT NULL ,
`MessageId` VARCHAR(45) NOT NULL ,
`TicketId` INT UNSIGNED NOT NULL ,
PRIMARY KEY (`MailId`) ,
INDEX `fk_email_ticket_user1` (`Sender` ASC) ,
INDEX `fk_email_ticket_user2` (`UserId` ASC) ,
INDEX `fk_email_ticket1` (`TicketId` ASC) ,
CONSTRAINT `fk_email_ticket_user1`
FOREIGN KEY (`Sender` )
REFERENCES `" . $cfg['db']['lib']['name'] ."`.`ticket_user` (`TUserId` )
@ -364,6 +366,11 @@
FOREIGN KEY (`UserId` )
REFERENCES `" . $cfg['db']['lib']['name'] ."`.`ticket_user` (`TUserId` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_email_ticket1`
FOREIGN KEY (`TicketId` )
REFERENCES `" . $cfg['db']['lib']['name'] ."`.`ticket` (`TId` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

View file

@ -308,9 +308,11 @@ CREATE TABLE IF NOT EXISTS `mydb`.`email` (
`Sender` INT(10) UNSIGNED NOT NULL ,
`UserId` INT(10) UNSIGNED NOT NULL ,
`MessageId` VARCHAR(45) NOT NULL ,
`TicketId` INT UNSIGNED NOT NULL ,
PRIMARY KEY (`MailId`) ,
INDEX `fk_email_ticket_user1` (`Sender` ASC) ,
INDEX `fk_email_ticket_user2` (`UserId` ASC) ,
INDEX `fk_email_ticket1` (`TicketId` ASC) ,
CONSTRAINT `fk_email_ticket_user1`
FOREIGN KEY (`Sender` )
REFERENCES `mydb`.`ticket_user` (`TUserId` )
@ -320,6 +322,11 @@ CREATE TABLE IF NOT EXISTS `mydb`.`email` (
FOREIGN KEY (`UserId` )
REFERENCES `mydb`.`ticket_user` (`TUserId` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_email_ticket1`
FOREIGN KEY (`TicketId` )
REFERENCES `mydb`.`ticket` (`TId` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;