2013-08-09 23:46:21 +00:00
< ? php
2013-09-10 23:38:53 +00:00
/**
* Handles the mailing functionality .
* This class covers the reading of the mail boxes of the support_groups , handling those emails , updating tickets accoring to the content & title of the emails ,
* but also the sending of emails after creating a new ticket and when someone else replies on your ticket .
* @ author Daan Janssens , mentored by Matthew Lagoe
*/
2013-08-10 11:59:36 +00:00
class Mail_Handler {
2013-09-10 23:38:53 +00:00
private $db ; /**< db object used by various methods. */
/**
* Start a new child process and return the process id
* this is used because imap might take some time , we dont want the cron parent process waiting on that .
* @ return return the child process id
*/
private function mail_fork () {
2013-08-12 03:14:00 +00:00
//Start a new child process and return the process id!
2013-08-10 11:59:36 +00:00
$pid = pcntl_fork ();
return $pid ;
2013-08-09 23:46:21 +00:00
}
2013-08-10 11:59:36 +00:00
2013-09-10 23:38:53 +00:00
/**
* Wrapper for sending emails , creates the content of the email
* Based on the type of the ticketing mail it will create a specific email , it will use the language . ini files to load the correct language of the email for the receiver .
* Also if the $TICKET_MAILING_SUPPORT is set to false or if the user 's personal ' ReceiveMail ' entry is set to false then no mail will be sent .
* @ param $receiver if integer , then it refers to the id of the user to whom we want to mail , if it ' s a string ( email - address ) then we will use that .
* @ param $ticketObj the ticket object itself , this is being used for including ticket related information into the email .
* @ param $content the content of a reply or new ticket
* @ param $type REPLY , NEW , WARNAUTHOR , WARNSENDER , WARNUNKNOWNSENDER
* @ param $sender ( default = 0 ( if it is not forwarded )) else use the id of the support group to which the ticket is currently forwarded , the support groups email address will be used to send the ticket .
*/
2013-08-26 15:25:28 +00:00
public static function send_ticketing_mail ( $receiver , $ticketObj , $content , $type , $sender = 0 ) {
2013-09-05 02:29:47 +00:00
2013-08-13 15:16:43 +00:00
global $TICKET_MAILING_SUPPORT ;
if ( $TICKET_MAILING_SUPPORT ){
2013-09-05 02:29:47 +00:00
global $MAIL_LOG_PATH ;
2013-09-10 23:38:53 +00:00
//error_log("Receiver: {$receiver}, content: {$content}, type: {$type}, SendingId: {$sender} \n", 3, $MAIL_LOG_PATH);
2013-08-26 15:25:28 +00:00
if ( $sender == 0 ){
2013-08-18 00:17:02 +00:00
//if it is not forwarded (==public == which returns 0) then make it NULL which is needed to be placed in the DB.
2013-08-26 15:25:28 +00:00
$sender = NULL ;
2013-08-18 00:17:02 +00:00
}
2013-08-13 15:16:43 +00:00
2013-08-26 09:34:51 +00:00
global $AMS_TRANS ;
if ( is_numeric ( $receiver )){
$webUser = new WebUsers ( $receiver );
$lang = $webUser -> getLanguage ();
} else {
global $DEFAULT_LANGUAGE ;
$lang = $DEFAULT_LANGUAGE ;
}
$variables = parse_ini_file ( $AMS_TRANS . '/' . $lang . '.ini' , true );
2013-08-26 03:37:55 +00:00
$mailText = array ();
foreach ( $variables [ 'email' ] as $key => $value ){
$mailText [ $key ] = $value ;
}
2013-08-25 15:49:01 +00:00
switch ( $type ){
case " REPLY " :
$webUser = new WebUsers ( $receiver );
2013-08-26 03:37:55 +00:00
if ( $webUser -> getReceiveMail ()){
$subject = $mailText [ 'email_subject_new_reply' ] . $ticketObj -> getTId () . " ] " ;
$txt = $mailText [ 'email_body_new_reply_1' ] . $ticketObj -> getTId () . $mailText [ 'email_body_new_reply_2' ] . $ticketObj -> getTitle () .
$mailText [ 'email_body_new_reply_3' ] . $content . $mailText [ 'email_body_new_reply_4' ];
2013-08-26 15:25:28 +00:00
self :: send_mail ( $receiver , $subject , $txt , $ticketObj -> getTId (), $sender );
2013-08-25 15:49:01 +00:00
}
break ;
case " NEW " :
$webUser = new WebUsers ( $receiver );
2013-08-26 03:37:55 +00:00
if ( $webUser -> getReceiveMail ()){
$subject = $mailText [ 'email_subject_new_ticket' ] . $ticketObj -> getTId () . " ] " ;
$txt = $mailText [ 'email_body_new_ticket_1' ] . $ticketObj -> getTId () . $mailText [ 'email_body_new_ticket_2' ] . $ticketObj -> getTitle () . $mailText [ 'email_body_new_ticket_3' ]
. $content . $mailText [ 'email_body_new_ticket_4' ];
2013-08-26 15:25:28 +00:00
self :: send_mail ( $receiver , $subject , $txt , $ticketObj -> getTId (), $sender );
2013-08-25 15:49:01 +00:00
}
break ;
case " WARNAUTHOR " :
2013-08-26 15:25:28 +00:00
if ( is_numeric ( $sender )){
$sender = Ticket_User :: get_email_by_user_id ( $sender );
}
2013-08-26 03:37:55 +00:00
$subject = $mailText [ 'email_subject_warn_author' ] . $ticketObj -> getTId () . " ] " ;
2013-08-26 15:25:28 +00:00
$txt = $mailText [ 'email_body_warn_author_1' ] . $ticketObj -> getTitle () . $mailText [ 'email_body_warn_author_2' ] . $sender . $mailText [ 'email_body_warn_author_3' ] .
$sender . $mailText [ 'email_body_warn_author_4' ] ;
self :: send_mail ( $receiver , $subject , $txt , $ticketObj -> getTId (), NULL );
2013-08-25 15:49:01 +00:00
break ;
case " WARNSENDER " :
2013-08-26 03:37:55 +00:00
$subject = $mailText [ 'email_subject_warn_sender' ];
$txt = $mailText [ 'email_body_warn_sender' ];
2013-08-26 15:25:28 +00:00
self :: send_mail ( $receiver , $subject , $txt , $ticketObj -> getTId (), NULL );
2013-08-25 15:49:01 +00:00
break ;
case " WARNUNKNOWNSENDER " :
2013-08-26 03:37:55 +00:00
$subject = $mailText [ 'email_subject_warn_unknown_sender' ];
$txt = $mailText [ 'email_body_warn_unknown_sender' ];
2013-08-26 15:25:28 +00:00
self :: send_mail ( $receiver , $subject , $txt , $ticketObj -> getTId (), NULL );
2013-08-25 15:49:01 +00:00
break ;
2013-08-18 22:02:55 +00:00
}
2013-08-13 15:16:43 +00:00
}
}
2013-08-12 03:14:00 +00:00
2013-09-10 23:38:53 +00:00
/**
* send mail function that will add the email to the db .
* this function is being used by the send_ticketing_mail () function . It adds the email as an entry to the `email` table in the database , which will be sent later on when we run the cron job .
* @ param $recipient if integer , then it refers to the id of the user to whom we want to mail , if it ' s a string ( email - address ) then we will use that .
* @ param $subject the subject of the email
* @ param $body the body of the email
* @ param $ticket_id the id of the ticket
* @ param $from the sending support_group ' s id ( NULL in case the default group is sending ))
*/
2013-08-17 01:06:22 +00:00
public static function send_mail ( $recipient , $subject , $body , $ticket_id = 0 , $from = NULL ) {
2013-08-20 00:08:12 +00:00
$id_user = NULL ;
2013-08-10 11:59:36 +00:00
if ( is_numeric ( $recipient )) {
$id_user = $recipient ;
$recipient = NULL ;
}
2014-05-27 22:18:44 +00:00
$db -> insert ( " email " , array ( 'Recipient' => $recipient , 'Subject' => $subject , 'Body' => $body , 'Status' => 'NEW' , 'Attempts' => 0 , 'Sender' => $from , 'UserId' => $id_user , 'MessageId' => 0 , 'TicketId' => $ticket_id ));
2013-08-10 11:59:36 +00:00
}
2013-09-10 23:38:53 +00:00
/**
* the cron funtion ( workhorse of the mailing system ) .
* The cron job will create a child process , which will first send the emails that are in the email table in the database , we use some kind of semaphore ( a temp file ) to make sure that
* if the cron job is called multiple times , it wont email those mails multiple times . After this , we will read the mail inboxes of the support groups and the default group using IMAP
* and we will add new tickets or new replies according to the incoming emails .
*/
2013-08-11 01:55:07 +00:00
function cron () {
2013-08-10 11:59:36 +00:00
global $cfg ;
2013-08-20 02:33:23 +00:00
global $MAIL_LOG_PATH ;
2013-08-16 19:43:39 +00:00
$default_groupemail = $cfg [ 'mail' ][ 'default_groupemail' ];
$default_groupname = $cfg [ 'mail' ][ 'default_groupname' ];
2013-08-16 20:24:11 +00:00
/*
2013-08-10 11:59:36 +00:00
$inbox_host = $cfg [ 'mail' ][ 'host' ];
2013-08-18 00:17:02 +00:00
$oms_reply_to = " Ryzom Ticketing Support <ticketing@ " . $inbox_host . " > " ; */
global $MAIL_DIR ;
2013-08-20 02:33:23 +00:00
error_log ( " ======================================================== \n " , 3 , $MAIL_LOG_PATH );
error_log ( " mailing cron Job started at: " . Helpers :: outputTime ( time (), 0 ) . " \n " , 3 , $MAIL_LOG_PATH );
2013-08-10 11:59:36 +00:00
2013-08-11 00:29:31 +00:00
//creates child process
2013-08-11 01:55:07 +00:00
$pid = self :: mail_fork ();
2013-08-12 03:14:00 +00:00
$pidfile = '/tmp/ams_cron_email_pid' ;
2013-08-11 00:29:31 +00:00
2013-08-10 11:59:36 +00:00
if ( $pid ) {
2013-08-11 00:29:31 +00:00
2013-08-11 23:11:48 +00:00
// We're the parent process, do nothing!
2013-08-16 19:43:39 +00:00
//INFO: if $pid =
//-1: "Could not fork!\n";
// 0: "In child!\n";
//>0: "In parent!\n";
2013-08-11 23:11:48 +00:00
2013-08-10 11:59:36 +00:00
} else {
2013-08-20 00:08:12 +00:00
//deliver new mail
2013-08-11 01:55:07 +00:00
//make db connection here because the children have to make the connection.
$this -> db = new DBLayer ( " lib " );
2013-08-11 00:29:31 +00:00
//if $pidfile doesn't exist yet, then start sending the mails that are in the db.
2013-08-10 11:59:36 +00:00
if ( ! file_exists ( $pidfile )) {
2013-08-16 19:43:39 +00:00
2013-08-11 00:29:31 +00:00
//create the file and write the child processes id in it!
2013-08-10 11:59:36 +00:00
$pid = getmypid ();
$file = fopen ( $pidfile , 'w' );
fwrite ( $file , $pid );
fclose ( $file );
//select all new & failed emails & try to send them
2013-08-11 00:29:31 +00:00
//$emails = db_query("select * from email where status = 'NEW' or status = 'FAILED'");
2014-05-27 22:18:44 +00:00
$statement = $this -> db -> select ( " email " , array ( null ), " Status = 'NEW' or Status = 'FAILED' " );
2013-08-11 00:29:31 +00:00
$emails = $statement -> fetchAll ();
2013-08-10 11:59:36 +00:00
foreach ( $emails as $email ) {
2013-08-12 21:56:58 +00:00
$message_id = self :: new_message_id ( $email [ 'TicketId' ]);
2013-08-11 00:29:31 +00:00
//if recipient isn't given, then use the email of the id_user instead!
if ( ! $email [ 'Recipient' ]) {
2013-08-13 15:16:43 +00:00
$email [ 'Recipient' ] = Ticket_User :: get_email_by_user_id ( $email [ 'UserId' ]);
2013-08-10 11:59:36 +00:00
}
2013-08-16 19:43:39 +00:00
//create sending email adres based on the $sender id which refers to the department id
if ( $email [ 'Sender' ] == NULL ) {
$from = $default_groupname . " < " . $default_groupemail . " > " ;
2013-08-10 11:59:36 +00:00
} else {
2013-08-16 19:43:39 +00:00
$group = Support_Group :: getGroup ( $email [ 'Sender' ]);
$from = $group -> getName () . " < " . $group -> getGroupEmail () . " > " ;
2013-08-10 11:59:36 +00:00
}
2013-08-16 19:43:39 +00:00
2013-08-12 21:56:58 +00:00
$headers = " From: $from\r\n " . " Message-ID: " . $message_id ;
2013-08-16 19:43:39 +00:00
2013-08-11 00:29:31 +00:00
if ( mail ( $email [ 'Recipient' ], $email [ 'Subject' ], $email [ 'Body' ], $headers )) {
2013-08-10 11:59:36 +00:00
$status = " DELIVERED " ;
2013-08-20 02:33:23 +00:00
error_log ( " Emailed { $email [ 'Recipient' ] } \n " , 3 , $MAIL_LOG_PATH );
2013-08-10 11:59:36 +00:00
} else {
$status = " FAILED " ;
2013-08-20 02:33:23 +00:00
error_log ( " Email to { $email [ 'Recipient' ] } failed \n " , 3 , $MAIL_LOG_PATH );
2013-08-10 11:59:36 +00:00
}
//change the status of the emails.
2013-08-11 00:29:31 +00:00
$this -> db -> execute ( 'update email set Status = ?, MessageId = ?, Attempts = Attempts + 1 where MailId = ?' , array ( $status , $message_id , $email [ 'MailId' ]));
2013-08-16 19:43:39 +00:00
2013-08-09 23:46:21 +00:00
}
2013-08-10 11:59:36 +00:00
unlink ( $pidfile );
2013-08-09 23:46:21 +00:00
}
2013-08-10 11:59:36 +00:00
// Check mail
2013-08-16 20:24:11 +00:00
$sGroups = Support_Group :: getGroups ();
2013-08-19 18:22:01 +00:00
//decrypt passwords in the db!
$crypter = new MyCrypt ( $cfg [ 'crypt' ]);
foreach ( $sGroups as $group ){
2013-08-20 00:08:12 +00:00
$group -> setIMAP_Password ( $crypter -> decrypt ( $group -> getIMAP_Password ()));
2013-08-19 18:22:01 +00:00
}
2013-08-16 20:24:11 +00:00
$defaultGroup = new Support_Group ();
2013-08-17 01:06:22 +00:00
$defaultGroup -> setSGroupId ( 0 );
2013-08-16 20:24:11 +00:00
$defaultGroup -> setGroupEmail ( $default_groupemail );
$defaultGroup -> setIMAP_MailServer ( $cfg [ 'mail' ][ 'default_mailserver' ]);
$defaultGroup -> setIMAP_Username ( $cfg [ 'mail' ][ 'default_username' ]);
$defaultGroup -> setIMAP_Password ( $cfg [ 'mail' ][ 'default_password' ]);
2013-08-19 18:22:01 +00:00
//add default group to the list
2013-08-16 20:24:11 +00:00
$sGroups [] = $defaultGroup ;
2013-08-11 23:11:48 +00:00
2013-08-16 20:24:11 +00:00
foreach ( $sGroups as $group ){
2013-08-19 18:22:01 +00:00
//check if group has mailing stuff filled in!
2013-08-20 00:08:12 +00:00
if ( $group -> getGroupEmail () != " " && $group -> getIMAP_MailServer () != " " && $group -> getIMAP_Username () != " " && $group -> getIMAP_Password () != " " ){
2013-08-19 18:22:01 +00:00
$mbox = imap_open ( $group -> getIMAP_MailServer (), $group -> getIMAP_Username (), $group -> getIMAP_Password ()) or die ( 'Cannot connect to mail server: ' . imap_last_error ());
$message_count = imap_num_msg ( $mbox );
for ( $i = 1 ; $i <= $message_count ; ++ $i ) {
2013-08-17 17:07:00 +00:00
2013-08-19 18:22:01 +00:00
//return task ID
2013-08-20 00:08:12 +00:00
$tkey = self :: incoming_mail_handler ( $mbox , $i , $group );
2013-08-19 18:22:01 +00:00
2013-08-20 00:08:12 +00:00
if ( $tkey ) {
2013-09-10 23:38:53 +00:00
//base file on Ticket + timestamp
2013-08-20 00:08:12 +00:00
$file = fopen ( $MAIL_DIR . " /ticket " . $tkey , 'w' );
2013-08-20 02:33:23 +00:00
error_log ( " Email was written to " . $MAIL_DIR . " /ticket " . $tkey . " \n " , 3 , $MAIL_LOG_PATH );
2013-08-19 18:22:01 +00:00
fwrite ( $file , imap_fetchheader ( $mbox , $i ) . imap_body ( $mbox , $i ));
fclose ( $file );
//mark message $i of $mbox for deletion!
imap_delete ( $mbox , $i );
}
2013-08-16 20:24:11 +00:00
}
2013-08-19 18:22:01 +00:00
//delete marked messages
imap_expunge ( $mbox );
imap_close ( $mbox );
2013-08-11 23:11:48 +00:00
}
2013-08-10 11:59:36 +00:00
}
2013-08-20 02:33:23 +00:00
error_log ( " Child Cron job finished at " . Helpers :: outputTime ( time (), 0 ) . " \n " , 3 , $MAIL_LOG_PATH );
error_log ( " ======================================================== \n " , 3 , $MAIL_LOG_PATH );
2013-08-09 23:46:21 +00:00
}
2013-08-20 00:08:12 +00:00
2013-08-10 11:59:36 +00:00
}
2013-09-10 23:38:53 +00:00
/**
* creates a new message id for a email about to send .
2013-09-13 22:39:03 +00:00
* @ param $ticketId the ticket id of the ticket that is mentioned in the email .
2013-09-10 23:38:53 +00:00
* @ return returns a string , that consist out of some variable parts , a consistent part and the ticket_id . The ticket_id will be used lateron , if someone replies on the message ,
* to see to which ticket the reply should be added .
*/
2013-08-12 21:56:58 +00:00
function new_message_id ( $ticketId ) {
2013-08-10 11:59:36 +00:00
$time = time ();
$pid = getmypid ();
2013-08-11 00:29:31 +00:00
global $cfg ;
global $ams_mail_count ;
$ams_mail_count = ( $ams_mail_count == '' ) ? 1 : $ams_mail_count + 1 ;
2013-08-12 21:56:58 +00:00
return " <ams.message " . " . " . $ticketId . " . " . $pid . $ams_mail_count . " . " . $time . " @ " . $cfg [ 'mail' ][ 'host' ] . " > " ;
2013-08-10 11:59:36 +00:00
}
2013-09-10 23:38:53 +00:00
/**
* try to fetch the ticket_id out of the subject .
2013-09-13 22:39:03 +00:00
* The subject should have a substring of the form [ Ticket \ #ticket_id], where ticket_id should be the integer ID of the ticket.
* @ param $subject the subject of an incomming email .
2013-09-10 23:38:53 +00:00
* @ return if the ticket ' s id is succesfully parsed , it will return the ticket_id , else it returns 0.
*/
2013-08-12 21:56:58 +00:00
function get_ticket_id_from_subject ( $subject ){
$startpos = strpos ( $subject , " [Ticket # " );
2013-08-17 01:06:22 +00:00
if ( $startpos ){
$tempString = substr ( $subject , $startpos + 9 );
$endpos = strpos ( $tempString , " ] " );
if ( $endpos ){
$ticket_id = substr ( $tempString , 0 , $endpos );
} else {
$ticket_id = 0 ;
}
} else {
$ticket_id = 0 ;
}
2013-08-12 21:56:58 +00:00
return $ticket_id ;
}
2013-08-10 11:59:36 +00:00
2013-08-12 21:56:58 +00:00
2013-09-10 23:38:53 +00:00
/**
* Handles an incomming email
* Read the content of one email by using imap ' s functionality . If a ticket id is found inside the message_id or else in the subject line , then a reply will be added
* ( if the email is not being sent from the authors email address it won ' t be added though and a warning will be sent to both parties ) . If no ticket id is found , then a new
* ticket will be created .
* @ param $mbox a mailbox object
* @ param $i the email ' s id in the mailbox ( integer )
* @ param $group the group object that owns the inbox .
* @ return a string based on the found ticket i and timestamp ( will be used to store a copy of the email locally )
*/
2013-08-17 01:06:22 +00:00
function incoming_mail_handler ( $mbox , $i , $group ){
2013-08-12 21:56:58 +00:00
2013-08-20 02:33:23 +00:00
global $MAIL_LOG_PATH ;
2013-08-12 21:56:58 +00:00
$header = imap_header ( $mbox , $i );
$subject = self :: decode_utf8 ( $header -> subject );
2013-08-17 01:06:22 +00:00
$entire_email = imap_fetchheader ( $mbox , $i ) . imap_body ( $mbox , $i );
$subject = self :: decode_utf8 ( $header -> subject );
$to = $header -> to [ 0 ] -> mailbox ;
2013-08-20 00:08:12 +00:00
$from = $header -> from [ 0 ] -> mailbox . '@' . $header -> from [ 0 ] -> host ;
$fromEmail = $header -> from [ 0 ] -> mailbox . '@' . $header -> from [ 0 ] -> host ;
2013-08-17 01:06:22 +00:00
$txt = self :: get_part ( $mbox , $i , " TEXT/PLAIN " );
//$html = self::get_part($mbox, $i, "TEXT/HTML");
2013-08-12 21:56:58 +00:00
2013-08-17 01:06:22 +00:00
//get the id out of the email address of the person sending the email.
if ( $from !== NULL && ! is_numeric ( $from )){
$from = Ticket_User :: get_id_from_email ( $from );
}
2013-08-12 21:56:58 +00:00
//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 " ){
$ticket_id = $pieces [ 2 ];
} else {
$ticket_id = self :: get_ticket_id_from_subject ( $subject );
}
} else {
$ticket_id = self :: get_ticket_id_from_subject ( $subject );
}
2013-08-17 01:06:22 +00:00
//if ticket id is found, that means it is a reply on an existing ticket
2013-09-20 21:36:19 +00:00
if ( $ticket_id && is_numeric ( $ticket_id ) && $ticket_id > 0 ){
2013-08-20 00:08:12 +00:00
$ticket = new Ticket ();
$ticket -> load_With_TId ( $ticket_id );
2013-08-13 15:16:43 +00:00
2013-08-17 01:06:22 +00:00
//if email is sent from an existing email address in the db (else it will give an error while loading the user object)
if ( $from != " FALSE " ){
2013-08-20 02:33:23 +00:00
2013-08-17 01:06:22 +00:00
$user = new Ticket_User ();
$user -> load_With_TUserId ( $from );
//if user has access to it!
if (( Ticket_User :: isMod ( $user ) or ( $ticket -> getAuthor () == $user -> getTUserId ())) and $txt != " " ){
2013-08-20 02:33:23 +00:00
2013-08-20 00:08:12 +00:00
Ticket :: createReply ( $txt , $user -> getTUserId (), $ticket -> getTId (), 0 );
2013-08-20 02:33:23 +00:00
error_log ( " Email found that is a reply to a ticket at: " . $group -> getGroupEmail () . " \n " , 3 , $MAIL_LOG_PATH );
2013-08-20 00:08:12 +00:00
} else {
//if user has no access to it
//Warn real ticket owner + person that send the mail
2013-08-26 15:25:28 +00:00
Mail_Handler :: send_ticketing_mail ( $ticket -> getAuthor (), $ticket , NULL , " WARNAUTHOR " , $from );
2013-08-25 15:49:01 +00:00
Mail_Handler :: send_ticketing_mail ( $from , $ticket , NULL , " WARNSENDER " , NULL );
2013-08-20 00:08:12 +00:00
2013-08-20 02:33:23 +00:00
error_log ( " Email found that was a reply to a ticket, though send by another user to " . $group -> getGroupEmail () . " \n " , 3 , $MAIL_LOG_PATH );
2013-08-17 01:06:22 +00:00
}
2013-08-17 17:07:00 +00:00
2013-08-20 00:08:12 +00:00
} else {
2013-08-20 02:33:23 +00:00
2013-08-20 00:08:12 +00:00
//if a reply to a ticket is being sent by a non-user!
//Warn real ticket owner + person that send the mail
2013-08-26 15:25:28 +00:00
Mail_Handler :: send_ticketing_mail ( $ticket -> getAuthor () , $ticket , NULL , " WARNAUTHOR " , $fromEmail );
2013-08-25 15:49:01 +00:00
Mail_Handler :: send_ticketing_mail ( $fromEmail , $ticket , NULL , " WARNUNKNOWNSENDER " , NULL );
2013-08-20 02:33:23 +00:00
error_log ( " Email found that was a reply to a ticket, though send by an unknown email address to " . $group -> getGroupEmail () . " \n " , 3 , $MAIL_LOG_PATH );
2013-08-20 00:08:12 +00:00
2013-08-17 01:06:22 +00:00
}
2013-08-20 00:08:12 +00:00
return $ticket_id . " . " . time ();
2013-08-12 21:56:58 +00:00
2013-08-17 17:07:00 +00:00
} else if ( $from != " FALSE " ){
2013-08-12 21:56:58 +00:00
2013-08-17 01:06:22 +00:00
//if ticket_id isn't found, create a new ticket!
//if an existing email address mailed the ticket
2013-08-17 17:07:00 +00:00
2013-08-18 00:17:02 +00:00
//if not default group, then forward it by giving the $group->getSGroupId's param
$newTicketId = Ticket :: create_Ticket ( $subject , $txt , 1 , $from , $from , $group -> getSGroupId ());
2013-08-17 17:07:00 +00:00
2013-08-20 02:33:23 +00:00
error_log ( " Email regarding new ticket found at: " . $group -> getGroupEmail () . " \n " , 3 , $MAIL_LOG_PATH );
2013-08-20 00:08:12 +00:00
return $newTicketId . " . " . time ();
2013-08-17 17:07:00 +00:00
2013-08-13 15:16:43 +00:00
2013-08-17 17:07:00 +00:00
} else {
//if it's a email that has nothing to do with ticketing, return 0;
2013-08-20 02:33:23 +00:00
error_log ( " Email found that isn't a reply or new ticket, at: " . $group -> getGroupEmail () . " \n " , 3 , $MAIL_LOG_PATH );
2013-08-17 17:07:00 +00:00
return 0 ;
2013-08-12 21:56:58 +00:00
}
}
2013-08-10 11:59:36 +00:00
2013-09-10 23:38:53 +00:00
/**
* decode utf8
* @ param $str str to be decoded
* @ return decoded string
*/
2013-08-10 11:59:36 +00:00
function decode_utf8 ( $str ) {
preg_match_all ( " /= \ ?UTF-8 \ ?B \ ?([^ \ ?]+) \ ?=/i " , $str , $arr );
for ( $i = 0 ; $i < count ( $arr [ 1 ]); $i ++ ){
$str = ereg_replace ( ereg_replace ( " \ ? " , " \ ? " ,
$arr [ 0 ][ $i ]), base64_decode ( $arr [ 1 ][ $i ]), $str );
}
return $str ;
2013-08-09 23:46:21 +00:00
}
2013-08-10 11:59:36 +00:00
2013-09-10 23:38:53 +00:00
/**
* returns the mime type of a structure of a email
* @ param & $structure the structure of an email message .
* @ return " TEXT " , " MULTIPART " , " MESSAGE " , " APPLICATION " , " AUDIO " , " IMAGE " , " VIDEO " , " OTHER " , " TEXT/PLAIN "
* @ todo take care of the HTML part of incoming emails .
*/
2013-08-10 11:59:36 +00:00
function get_mime_type ( & $structure ) {
$primary_mime_type = array ( " TEXT " , " MULTIPART " , " MESSAGE " , " APPLICATION " , " AUDIO " , " IMAGE " , " VIDEO " , " OTHER " );
if ( $structure -> subtype ) {
return $primary_mime_type [( int ) $structure -> type ] . '/' . $structure -> subtype ;
}
return " TEXT/PLAIN " ;
2013-08-09 23:46:21 +00:00
}
2013-08-10 11:59:36 +00:00
2013-09-10 23:38:53 +00:00
//to document..
2013-08-10 11:59:36 +00:00
function get_part ( $stream , $msg_number , $mime_type , $structure = false , $part_number = false ) {
if ( ! $structure ) {
$structure = imap_fetchstructure ( $stream , $msg_number );
2013-08-09 23:46:21 +00:00
}
2013-08-10 11:59:36 +00:00
if ( $structure ) {
2013-08-12 03:14:00 +00:00
if ( $mime_type == self :: get_mime_type ( $structure )) {
2013-08-10 11:59:36 +00:00
if ( ! $part_number ) {
$part_number = " 1 " ;
2013-08-09 23:46:21 +00:00
}
2013-08-10 11:59:36 +00:00
$text = imap_fetchbody ( $stream , $msg_number , $part_number );
if ( $structure -> encoding == 3 ) {
return imap_base64 ( $text );
} else if ( $structure -> encoding == 4 ) {
return imap_qprint ( $text );
} else {
return $text ;
2013-08-09 23:46:21 +00:00
}
2013-08-10 11:59:36 +00:00
}
if ( $structure -> type == 1 ) /* multipart */ {
while ( list ( $index , $sub_structure ) = each ( $structure -> parts )) {
if ( $part_number ) {
$prefix = $part_number . '.' ;
} else {
$prefix = '' ;
}
2013-08-12 03:14:00 +00:00
$data = self :: get_part ( $stream , $msg_number , $mime_type , $sub_structure , $prefix . ( $index + 1 ));
2013-08-10 11:59:36 +00:00
if ( $data ) {
return $data ;
}
} // END OF WHILE
} // END OF MULTIPART
} // END OF STRUTURE
return false ;
} // END OF FUNCTION
2013-08-20 00:08:12 +00:00
}