From 759ee63e9f845a159d8760c727c6922eb86892bf Mon Sep 17 00:00:00 2001 From: Quitta Date: Tue, 13 Aug 2013 17:16:43 +0200 Subject: [PATCH] ticket mail system basis works --- .../ams_lib/autoload/mail_handler.php | 98 ++++++++++--------- .../ryzom_ams/ams_lib/autoload/ticket.php | 14 ++- .../ams_lib/autoload/ticket_user.php | 31 ++++++ .../tools/server/ryzom_ams/www/config.php | 5 +- .../www/html/func/reply_on_ticket.php | 3 +- 5 files changed, 100 insertions(+), 51 deletions(-) diff --git a/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/mail_handler.php b/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/mail_handler.php index fd51e2b21..4c5e14caf 100644 --- a/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/mail_handler.php +++ b/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/mail_handler.php @@ -12,41 +12,43 @@ class Mail_Handler{ } - function get_email_by_user_id($id){ - $user = new Ticket_User(); - $user->load_With_TUserId($id); - $webUser = new WebUsers($user->getExternId()); - return $webUser->getEmail(); - } - - function get_username_from_id($id){ - $user = new Ticket_User(); - $user->load_With_TUserId($id); - $webUser = new WebUsers($user->getExternId()); - return $webUser->getUsername(); - } + public static function send_ticketing_mail($ticketObj, $content, $type, $author) { + global $TICKET_MAILING_SUPPORT; + if($TICKET_MAILING_SUPPORT){ + $txt = ""; + $subject = ""; + if($type == "REPLY"){ + $txt = "---------- Ticket #". $ticketObj->getTId() . " ----------\n You received a new reply on your ticket: " . $ticketObj->getTitle() . + "\n --------------------\n\n"; + $subject = "New reply on [Ticket #" . $ticketObj->getTId() ."]"; + $endTxt = "\n\n----------\nYou can reply on this message to answer directly on the ticket!"; + $txt = $txt . $content . $endTxt; + self::send_mail($ticketObj->getAuthor(),$subject,$txt, $ticketObj->getTId(),$author); + }else if($type == "NEW"){ + $txt = "---------- Ticket #". $ticketObj->getTId() . " ----------\n Your ticket: " . $ticketObj->getTitle() . " is newly created"; + if($ticketObj->getAuthor() != $author){ + $txt = $txt . " by " . Ticket_User::get_username_from_id($author); + }else{ + $author = $ticketObj->getAuthor(); + } + $txt = $txt . "\n --------------------\n\n"; + $subject = "New ticket created [Ticket #" . $ticketObj->getTId() ."]"; + $endTxt = "\n\n----------\nYou can reply on this message to answer directly on the ticket!"; + $txt = $txt . $content . $endTxt; + self::send_mail($ticketObj->getAuthor(),$subject,$txt, $ticketObj->getTId()); + } + + } + } - function get_id_from_username($username){ - $externId = WebUsers::getId($username); - $user = Ticket_User::constr_ExternId($externId); - 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) { - + public static function send_mail($recipient, $subject, $body, $ticket_id = 0, $from = 1) { if(is_numeric($recipient)) { $id_user = $recipient; $recipient = NULL; } - $query = "INSERT INTO email (Recipient,Subject,Body,Status,UserId,Sender) VALUES (:recipient, :subject, :body, :status, :id_user, :sender)"; - $values = array('recipient' => $recipient, 'subject' => $subject, 'body' => $body, 'status' => 'NEW', 'id_user' => $id_user, 'sender' => $from); + $query = "INSERT INTO email (Recipient,Subject,Body,Status,Attempts,Sender,UserId,MessageId,TicketId) VALUES (:recipient, :subject, :body, :status, :attempts, :sender, :id_user, :messageId, :ticketId)"; + $values = array('recipient' => $recipient, 'subject' => $subject, 'body' => $body, 'status' => 'NEW', 'attempts'=> 0, 'sender' => $from,'id_user' => $id_user, 'messageId' => 0, 'ticketId'=> $ticket_id); $db = new DBLayer("lib"); $db->execute($query, $values); @@ -59,7 +61,7 @@ class Mail_Handler{ $inbox_username = $cfg['mail']['username']; $inbox_password = $cfg['mail']['password']; $inbox_host = $cfg['mail']['host']; - $oms_reply_to = "OMS "; + $oms_reply_to = "Ryzom Ticketing Support "; global $MAIL_DIR; // Deliver new mail @@ -101,12 +103,12 @@ class Mail_Handler{ //if recipient isn't given, then use the email of the id_user instead! echo("Emailing {$email['Recipient']}\n"); if(!$email['Recipient']) { - $email['Recipient'] = self::get_email_by_user_id($email['UserId']); + $email['Recipient'] = Ticket_User::get_email_by_user_id($email['UserId']); } //create sending email adres based on the $sender id - if($email['Sender']) { - $username = self::get_username_from_id($email['Sender']); + if($email['Sender'] != 0) { + $username = Ticket_User::get_username_from_id($email['Sender']); $from = "$username <$username@$inbox_host>"; } else { $from = $oms_reply_to; @@ -171,7 +173,6 @@ class Mail_Handler{ } function get_ticket_id_from_subject($subject){ - print('got it from subject!'); $startpos = strpos($subject, "[Ticket #"); $tempString = substr($subject, $startpos+9); $endpos = strpos($tempString, "]"); @@ -192,13 +193,11 @@ class Mail_Handler{ if(isset($header->references)){ $pieces = explode(".", $header->references); if($pieces[0] == "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"); + //$html = self::get_part($mbox, $i, "TEXT/HTML"); + + //use the line ---------- Ticket # to make a distincton between the old message and the reply + $endpos = strpos($txt, ">---------- Ticket #"); + if($endpos){ + $txt = substr($txt, 0, $endpos); + }else{ + $endpos = strpos($txt, "---------- Ticket #"); + if($endpos){ + $txt = substr($txt, 0, $endpos); + } + } + //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); + if($from !== NULL && !is_numeric($from)) $from = Ticket_User::get_id_from_email($from); $user = new Ticket_User(); $user->load_With_TUserId($from); @@ -221,15 +232,10 @@ class Mail_Handler{ $ticket->load_With_TId($ticket_id); //if user has access to it! - if($user->isMod() or ($ticket->getAuthor() == $user->getTUserId())){ - + if((Ticket_User::isMod($user) or ($ticket->getAuthor() == $user->getTUserId())) and $txt != ""){ + Ticket::createReply($txt, $user->getTUserId(), $ticket->getTId(), 0); } - /*print("================"); - print("subj: ".$subject); - print("to: ".$to); - print("from: ".$from); - print("txt: " .$txt); - print("html: ".$html);*/ + } } diff --git a/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/ticket.php b/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/ticket.php index e6961b9b8..a9bceacd3 100644 --- a/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/ticket.php +++ b/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/ticket.php @@ -96,7 +96,8 @@ class Ticket{ }else{ Ticket_Log::createLogEntry( $ticket_id, $real_author, 2, $author); } - Ticket_Reply::createReply($content, $author, $ticket_id, 0); + Ticket_Reply::createReply($content, $author, $ticket_id, 0, $author); + Mail_Handler::send_ticketing_mail($ticket, $content, "NEW", $real_author); return $ticket_id; } @@ -156,6 +157,13 @@ class Ticket{ //if status is not closed if($ticket->getStatus() != 3){ Ticket_Reply::createReply($content, $author, $ticket_id, $hidden, $ticket->getAuthor()); + + //notify ticket author that a new reply is added! + if($ticket->getAuthor() != $author){ + Mail_Handler::send_ticketing_mail($ticket, $content, "REPLY", $author); + } + + }else{ //TODO: Show error message that ticket is closed } @@ -215,7 +223,9 @@ class Ticket{ //Set ticket object public function set($values){ - $this->tId = $values['TId']; + if(isset($values['TId'])){ + $this->tId = $values['TId']; + } $this->title = $values['Title']; $this->status = $values['Status']; $this->queue = $values['Queue']; diff --git a/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/ticket_user.php b/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/ticket_user.php index e3079806b..fe3529925 100644 --- a/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/ticket_user.php +++ b/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/ticket_user.php @@ -72,6 +72,37 @@ class Ticket_User{ $user->update(); } + + public static function get_email_by_user_id($id){ + $user = new Ticket_User(); + $user->load_With_TUserId($id); + $webUser = new WebUsers($user->getExternId()); + return $webUser->getEmail(); + } + + + public static function get_username_from_id($id){ + $user = new Ticket_User(); + $user->load_With_TUserId($id); + $webUser = new WebUsers($user->getExternId()); + return $webUser->getUsername(); + } + + + public static function get_id_from_username($username){ + $externId = WebUsers::getId($username); + $user = Ticket_User::constr_ExternId($externId); + return $user->getTUserId(); + } + + + public static function get_id_from_email($email){ + $webUserId = WebUsers::getIdFromEmail($email); + $user = Ticket_User::constr_ExternId($webUserId); + return $user->getTUserId(); + } + + ////////////////////////////////////////////Methods//////////////////////////////////////////////////// public function __construct() { } diff --git a/code/ryzom/tools/server/ryzom_ams/www/config.php b/code/ryzom/tools/server/ryzom_ams/www/config.php index 5cf42d2fc..82c010e58 100644 --- a/code/ryzom/tools/server/ryzom_ams/www/config.php +++ b/code/ryzom/tools/server/ryzom_ams/www/config.php @@ -33,7 +33,7 @@ $cfg['db']['ring']['pass'] = ''; $cfg['mail']['username'] = 'amsryzom@gmail.com'; $cfg['mail']['password'] = 'lol123bol'; -$cfg['mail']['host'] = 'test.com'; +$cfg['mail']['host'] = 'ryzomcore.com'; // To connect to an IMAP server running on port 143 on the local machine, // do the following: $mbox = imap_open("{localhost:143}INBOX", "user_id", "password"); @@ -63,6 +63,7 @@ $DEFAULT_LANGUAGE = 'en'; $SITEBASE = dirname( __FILE__ ) . '/html/' ; $TICKET_LOGGING = true; +$TICKET_MAILING_SUPPORT = true; $TIME_FORMAT = "m-d-Y H:i:s"; $INGAME_LAYOUT = "basic"; -$MAIL_DIR = "/tmp"; \ No newline at end of file +$MAIL_DIR = "/tmp"; diff --git a/code/ryzom/tools/server/ryzom_ams/www/html/func/reply_on_ticket.php b/code/ryzom/tools/server/ryzom_ams/www/html/func/reply_on_ticket.php index 6ba5d665b..76f8ee71e 100644 --- a/code/ryzom/tools/server/ryzom_ams/www/html/func/reply_on_ticket.php +++ b/code/ryzom/tools/server/ryzom_ams/www/html/func/reply_on_ticket.php @@ -31,7 +31,8 @@ function reply_on_ticket(){ }catch (PDOException $e) { //ERROR: LIB DB is not online! - header("Location: index.php"); + print_r($e); + //header("Location: index.php"); exit; }