Ejemplo n.º 1
0
imap_errors();
//print_r($autoreply_queue);
module_cache::clear('ticket');
foreach ($autoreply_queue as $ticket_id) {
    ob_start();
    handle_hook('ticket_sidebar', $ticket_id);
    // to get envato hook working quicker
    ob_end_clean();
    // we have to send the email to admin notifying them about this ticket too.
    // if this latest email came from an admin user (ie: the user is replying to a customer via email)
    // then we don't send_admin_alert or autoreply, we just send reply back to customer.
    $ticket_data = module_ticket::get_ticket($ticket_id);
    $last_ticket_message = module_ticket::get_ticket_message($ticket_data['last_ticket_message_id']);
    $admins_rel = module_ticket::get_ticket_staff_rel();
    // if the last email was from admin, send customer alert.
    if (isset($admins_rel[$last_ticket_message['from_user_id']])) {
        //        echo "sending a customer alert ";
        //        print_r($last_ticket_message);
        module_ticket::send_customer_alert($ticket_id);
    } else {
        // last email must have been from a customer
        // alert the admin to it, and send an auto reply if the message is the first.
        module_ticket::send_admin_alert($ticket_id);
        //echo "Sent an alert to admin... sending autoreply...";
        //print_r($ticket_data);
        //echo "<br><br>";
        if (module_config::c('ticket_autoreply_every_message', 0) || $ticket_data['message_count'] <= 1) {
            module_ticket::send_autoreply($ticket_id);
        }
    }
}
Ejemplo n.º 2
0
 /**
  * Used only when admin/user replies via web interface
  * Or when a new ticket/reply is submitted via public interface
  * Or when an autoreply is sent
  * */
 public static function send_reply($ticket_id, $message, $from_user_id, $to_user_id, $reply_type = 'admin', $internal_from = '', $other_options = array())
 {
     // we also check if this message contains anything, or anything above the "reply line"
     // this is a hack to stop the autoreply loop that seems to happen when sending an email as yourself from  your envato profile.
     // stip out the text before our "--reply above this line-- bit.
     // copied code from ticket_admin_edit.php
     /*$reply__ine_default = '----- (Please reply above this line) -----'; // incase they change it
       $reply__ine =   module_config::s('ticket_reply_line',$reply__ine_default);
       $text = preg_replace("#<br[^>]*>#",'',$message);
       // convert to single text.
       $text = preg_replace('#\s+#imsU',' ',$text);
       if(
           preg_match('#^\s*'.preg_quote($reply__ine,'#').'.*#ims',$text) ||
           preg_match('#^\s*'.preg_quote($reply__ine_default,'#').'.*#ims',$text)
       ){
           // no content. don't send email
           //mail('*****@*****.**','ticket reply '.$ticket_id,'sending reply for text:\''.$text."' \n\n\n Original:\n".$message);
           return false;
       }*/
     // $message is in text format, need to nl2br it before printing.
     $ticket_number = self::ticket_number($ticket_id);
     $ticket_details = self::get_ticket($ticket_id);
     $to_user_a = module_user::get_user($to_user_id, false);
     $from_user_a = module_user::get_user($from_user_id, false);
     // we have to replace some special text within these messages. this is just a hack to support text in my autoreply.
     $replace = array('name' => $to_user_a['name'], 'ticket_url' => module_ticket::link_public($ticket_id), 'ticket_url_cancel' => module_ticket::link_public_status($ticket_id, 7), 'ticket_url_resolved' => module_ticket::link_public_status($ticket_id, _TICKET_STATUS_RESOLVED_ID), 'ticket_url_inprogress' => module_ticket::link_public_status($ticket_id, 5), 'faq_product_id' => $ticket_details['faq_product_id']);
     foreach ($replace as $key => $val) {
         $message = str_replace('{' . strtoupper($key) . '}', $val, $message);
     }
     // the from details need to match the ticket account details.
     if ($ticket_details['ticket_account_id']) {
         $ticket_account = self::get_ticket_account($ticket_details['ticket_account_id']);
     } else {
         $ticket_account = false;
     }
     if ($ticket_account && $ticket_account['email']) {
         // want the user to reply to our ticketing system.
         $reply_to_address = $ticket_account['email'];
         $reply_to_name = $ticket_account['name'];
     } else {
         // reply to creator of the email.
         $reply_to_address = $from_user_a['email'];
         $reply_to_name = $from_user_a['name'];
     }
     $htmlmessage = '';
     if (self::is_text_html($message)) {
         $htmlmessage = $message;
         $message = strip_tags($message);
     }
     $ticket_message_data = array('ticket_id' => $ticket_id, 'content' => $message, 'htmlcontent' => $htmlmessage, 'message_time' => time(), 'from_user_id' => $from_user_id, 'to_user_id' => $to_user_id, 'message_type_id' => $reply_type == 'admin' ? _TICKET_MESSAGE_TYPE_ADMIN : _TICKET_MESSAGE_TYPE_CREATOR, 'private_message' => isset($other_options['private_message']) && $other_options['private_message'] ? $other_options['private_message'] : 0);
     if ($internal_from == 'autoreply') {
         $ticket_message_data['message_type_id'] = _TICKET_MESSAGE_TYPE_AUTOREPLY;
     }
     if (self::can_edit_tickets()) {
         // we look for the extra cc/bcc headers.
         if (module_config::c('ticket_allow_cc_bcc', 1)) {
             $headers = array();
             // look for cc staff options here.
             if (isset($_POST['ticket_cc_staff']) && is_array($_POST['ticket_cc_staff'])) {
                 $admins_rel = self::get_ticket_staff_rel();
                 foreach ($admins_rel as $staff_id => $staff_name) {
                     if (isset($_POST['ticket_cc_staff'][$staff_id])) {
                         $staff_user = module_user::get_user($staff_id);
                         if ($staff_user && isset($staff_user['email']) && strlen($staff_user['email'])) {
                             // found a staff member to cc!
                             if (!isset($headers['cc_emails'])) {
                                 $headers['cc_emails'] = array();
                             }
                             $headers['cc_emails'][] = array('address' => $staff_user['email']);
                         }
                     }
                 }
             }
             if (isset($_POST['ticket_cc']) && strlen($_POST['ticket_cc'])) {
                 $bits = explode(',', $_POST['ticket_cc']);
                 foreach ($bits as $b) {
                     $b = trim($b);
                     if (strlen($b)) {
                         if (!isset($headers['cc_emails'])) {
                             $headers['cc_emails'] = array();
                         }
                         $headers['cc_emails'][] = array('address' => $b);
                     }
                 }
             }
             if (isset($_POST['ticket_bcc']) && strlen($_POST['ticket_bcc'])) {
                 $bits = explode(',', $_POST['ticket_bcc']);
                 foreach ($bits as $b) {
                     $b = trim($b);
                     if (strlen($b)) {
                         if (!isset($headers['bcc_emails'])) {
                             $headers['bcc_emails'] = array();
                         }
                         $headers['bcc_emails'][] = array('address' => $b);
                     }
                 }
             }
             if (count($headers)) {
                 $ticket_message_data['cache'] = serialize($headers);
             }
         }
     }
     $ticket_message_id = update_insert('ticket_message_id', 'new', 'ticket_message', $ticket_message_data);
     if (!$ticket_message_id) {
         return false;
     }
     // handle any attachemnts.
     // are there any attachments?
     if ($ticket_message_id && isset($_FILES['attachment']) && isset($_FILES['attachment']['tmp_name']) && is_array($_FILES['attachment']['tmp_name'])) {
         foreach ($_FILES['attachment']['tmp_name'] as $key => $val) {
             if (is_uploaded_file($val)) {
                 // save attachments against ticket!
                 $mime = dtbaker_mime_type($_FILES['attachment']['name'][$key], $val);
                 $attachment_id = update_insert('ticket_message_attachment_id', 'new', 'ticket_message_attachment', array('ticket_id' => $ticket_id, 'ticket_message_id' => $ticket_message_id, 'file_name' => $_FILES['attachment']['name'][$key], 'content_type' => $mime));
                 //echo getcwd();exit;
                 //ini_set('display_errors',true);
                 if (!move_uploaded_file($val, 'includes/plugin_ticket/attachments/' . $attachment_id . '')) {
                     //echo 'error uploading file';exit;
                 }
             }
         }
     }
     if ($internal_from != 'autoreply') {
         // stops them all having the same timestamp on a big import.
         update_insert('ticket_id', $ticket_id, 'ticket', array('last_message_timestamp' => time()));
         /*}else{
           // we are sending an auto reply, flag this in the special cache field.
           // hacky!
           update_insert('ticket_message_id',$ticket_message_id,'ticket_message',array(
                    'cache'=>'autoreply',
            ));*/
     }
     //$reply_line = module_config::s('ticket_reply_line','----- (Please reply above this line) -----');
     $s = self::get_statuses();
     if (isset($other_options['private_message']) && $other_options['private_message']) {
         // private message, dont send an email to the customer.
         if (!self::is_text_html($message)) {
             $message = nl2br(htmlspecialchars($message));
             // because message is in text format, before we send admin notification do this.
         }
         module_ticket::send_admin_alert($ticket_id, strlen($htmlmessage) ? $htmlmessage : $message, true);
     } else {
         if ($to_user_id == $ticket_details['user_id']) {
             // WE ARE emailing the "User" from support.
             // so the support is emailing a response back to the customer.
             module_ticket::send_customer_alert($ticket_id, strlen($htmlmessage) ? $htmlmessage : $message, $ticket_message_id);
         } else {
             if (!self::is_text_html($message)) {
                 $message = nl2br(htmlspecialchars($message));
                 // because message is in text format, before we send admin notification do this.
             }
             module_ticket::send_admin_alert($ticket_id, strlen($htmlmessage) ? $htmlmessage : $message);
         }
         if ($reply_type == 'end_user' && (!$ticket_details['message_count'] || module_config::c('ticket_autoreply_every_message', 0))) {
             // this is the first message!
             // send an email back to the user confirming this submissions via the web interface.
             self::send_autoreply($ticket_id, $message);
         }
     }
     return $ticket_message_id;
 }