2014-11-23 00:20:28 -05:00
< ? php
/*******************************************************************************
2015-09-12 00:46:46 -04:00
* Title : Help Desk Software HESK
2016-04-18 18:57:07 -04:00
* Version : 2.6 . 7 from 18 th April 2016
2015-09-12 00:46:46 -04:00
* Author : Klemen Stirn
* Website : http :// www . hesk . com
********************************************************************************
* COPYRIGHT AND TRADEMARK NOTICE
* Copyright 2005 - 2015 Klemen Stirn . All Rights Reserved .
* HESK is a registered trademark of Klemen Stirn .
* The HESK may be used and modified free of charge by anyone
* AS LONG AS COPYRIGHT NOTICES AND ALL THE COMMENTS REMAIN INTACT .
* By using this code you agree to indemnify Klemen Stirn from any
* liability that might arise from it ' s use .
* Selling the code for this program , in part or full , without prior
* written consent is expressly forbidden .
* Using this code , in part or full , to create derivate work ,
* new scripts or products is expressly forbidden . Obtain permission
* before redistributing this software over the Internet or in
* any other medium . In all cases copyright and header must remain intact .
* This Copyright is in full effect in any country that has International
* Trade Agreements with the United States of America or
* with the European Union .
* Removing any of the copyright notices without purchasing a license
* is expressly forbidden . To remove HESK copyright notice you must purchase
* a license for this script . For more information on how to obtain
* a license please visit the page below :
* https :// www . hesk . com / buy . php
*******************************************************************************/
2014-11-23 00:20:28 -05:00
/* Check if this is a valid include */
2015-09-12 00:46:46 -04:00
if ( ! defined ( 'IN_SCRIPT' )) {
die ( 'Invalid attempt' );
}
2014-11-23 00:20:28 -05:00
/* Get includes for SMTP */
2015-09-12 00:46:46 -04:00
if ( $hesk_settings [ 'smtp' ]) {
require ( HESK_PATH . 'inc/mail/smtp.php' );
if ( strlen ( $hesk_settings [ 'smtp_user' ]) || strlen ( $hesk_settings [ 'smtp_password' ])) {
require_once ( HESK_PATH . 'inc/mail/sasl/sasl.php' );
}
2014-11-23 00:20:28 -05:00
}
2015-09-03 21:58:05 -04:00
function hesk_notifyCustomerForVerifyEmail ( $email_template = 'verify_email' , $activationKey , $modsForHesk_settings )
2015-01-01 00:33:23 -05:00
{
global $hesk_settings , $ticket ;
2015-09-12 00:46:46 -04:00
if ( defined ( 'HESK_DEMO' )) {
2015-01-01 00:33:23 -05:00
return true ;
}
// Format email subject and message
2015-01-02 00:40:41 -05:00
$subject = hesk_getEmailSubject ( $email_template , $ticket );
2015-09-03 21:58:05 -04:00
$message = hesk_getEmailMessage ( $email_template , $ticket , $modsForHesk_settings );
$htmlMessage = hesk_getHtmlMessage ( $email_template , $ticket , $modsForHesk_settings );
2015-01-01 00:33:23 -05:00
$activationUrl = $hesk_settings [ 'hesk_url' ] . '/verifyemail.php?key=%%ACTIVATIONKEY%%' ;
$message = str_replace ( '%%VERIFYURL%%' , $activationUrl , $message );
2015-04-18 22:20:47 -04:00
$htmlMessage = str_replace ( '%%VERIFYURL%%' , $activationUrl , $htmlMessage );
2015-01-01 00:33:23 -05:00
$message = str_replace ( '%%ACTIVATIONKEY%%' , $activationKey , $message );
2015-04-18 22:20:47 -04:00
$htmlMessage = str_replace ( '%%ACTIVATIONKEY%%' , $activationKey , $htmlMessage );
2015-09-03 21:58:05 -04:00
$hasMessage = hesk_doesTemplateHaveTag ( $email_template , '%%MESSAGE%%' , $modsForHesk_settings );
2015-01-01 00:33:23 -05:00
2015-01-22 00:01:16 -05:00
// Add Cc / Bcc recipents if needed
$ccEmails = array ();
$bccEmails = array ();
2015-09-12 00:46:46 -04:00
foreach ( $hesk_settings [ 'custom_fields' ] as $k => $v ) {
2015-01-22 00:01:16 -05:00
if ( $v [ 'use' ]) {
if ( $v [ 'type' ] == 'email' && ! empty ( $ticket [ $k ])) {
if ( $v [ 'value' ] == 'cc' ) {
2015-09-12 00:46:46 -04:00
$emails = explode ( ',' , $ticket [ $k ]);
2015-01-22 00:01:16 -05:00
array_push ( $ccEmails , $emails );
} elseif ( $v [ 'value' ] == 'bcc' ) {
2015-09-12 00:46:46 -04:00
$emails = explode ( ',' , $ticket [ $k ]);
2015-01-22 00:01:16 -05:00
array_push ( $bccEmails , $emails );
}
}
}
}
2015-09-03 21:58:05 -04:00
hesk_mail ( $ticket [ 'email' ], $subject , $message , $htmlMessage , $modsForHesk_settings , $ccEmails , $bccEmails , $hasMessage );
2015-01-01 00:33:23 -05:00
}
2014-11-23 00:20:28 -05:00
2015-09-03 21:58:05 -04:00
function hesk_notifyCustomer ( $modsForHesk_settings , $email_template = 'new_ticket' )
2014-11-23 00:20:28 -05:00
{
2015-09-12 00:46:46 -04:00
global $hesk_settings , $hesklang , $ticket ;
2014-11-23 00:20:28 -05:00
2015-09-12 00:46:46 -04:00
// Demo mode
if ( defined ( 'HESK_DEMO' )) {
return true ;
}
2014-11-23 00:20:28 -05:00
2015-01-23 00:30:46 -05:00
$changedLanguage = false ;
//Set the user's language according to the ticket.
2015-09-12 00:46:46 -04:00
if ( isset ( $ticket [ 'language' ]) && $ticket [ 'language' ] !== NULL ) {
2015-01-23 00:30:46 -05:00
hesk_setLanguage ( $ticket [ 'language' ]);
$changedLanguage = true ;
}
2015-09-12 00:46:46 -04:00
// Format email subject and message
$subject = hesk_getEmailSubject ( $email_template , $ticket );
$message = hesk_getEmailMessage ( $email_template , $ticket , $modsForHesk_settings );
$htmlMessage = hesk_getHtmlMessage ( $email_template , $ticket , $modsForHesk_settings );
$hasMessage = hesk_doesTemplateHaveTag ( $email_template , '%%MESSAGE%%' , $modsForHesk_settings );
2014-11-23 00:20:28 -05:00
2015-01-22 00:01:16 -05:00
// Add Cc / Bcc recipents if needed
$ccEmails = array ();
$bccEmails = array ();
2015-09-12 00:46:46 -04:00
foreach ( $hesk_settings [ 'custom_fields' ] as $k => $v ) {
2015-01-22 00:01:16 -05:00
if ( $v [ 'use' ]) {
if ( $v [ 'type' ] == 'email' && ! empty ( $ticket [ $k ])) {
if ( $v [ 'value' ] == 'cc' ) {
array_push ( $ccEmails , $ticket [ $k ]);
} elseif ( $v [ 'value' ] == 'bcc' ) {
array_push ( $bccEmails , $ticket [ $k ]);
}
}
}
}
2015-09-12 00:46:46 -04:00
// Send e-mail
hesk_mail ( $ticket [ 'email' ], $subject , $message , $htmlMessage , $modsForHesk_settings , $ccEmails , $bccEmails , $hasMessage );
2014-11-23 00:20:28 -05:00
2015-01-23 00:30:46 -05:00
// Reset the language if it was changed
2015-08-29 21:30:20 -04:00
hesk_resetLanguage ();
2015-01-23 00:30:46 -05:00
2014-11-23 00:20:28 -05:00
return true ;
} // END hesk_notifyCustomer()
2015-09-03 21:58:05 -04:00
function hesk_notifyAssignedStaff ( $autoassign_owner , $email_template , $modsForHesk_settings , $type = 'notify_assigned' )
2014-11-23 00:20:28 -05:00
{
2015-09-12 00:46:46 -04:00
global $hesk_settings , $hesklang , $ticket ;
2014-11-23 00:20:28 -05:00
2015-09-12 00:46:46 -04:00
// Demo mode
if ( defined ( 'HESK_DEMO' )) {
return true ;
}
2014-11-23 00:20:28 -05:00
2015-09-12 00:46:46 -04:00
$ticket [ 'owner' ] = intval ( $ticket [ 'owner' ]);
2014-11-23 00:20:28 -05:00
/* Need to lookup owner info from the database? */
2015-09-12 00:46:46 -04:00
if ( $autoassign_owner === false ) {
$res = hesk_dbQuery ( " SELECT `name`, `email`,`language`,`notify_assigned`,`notify_reply_my` FROM ` " . hesk_dbEscape ( $hesk_settings [ 'db_pfix' ]) . " users` WHERE `id`=' " . $ticket [ 'owner' ] . " ' LIMIT 1 " );
2014-11-23 00:20:28 -05:00
$autoassign_owner = hesk_dbFetchAssoc ( $res );
2015-09-12 00:46:46 -04:00
$hesk_settings [ 'user_data' ][ $ticket [ 'owner' ]] = $autoassign_owner ;
2014-11-23 00:20:28 -05:00
2015-09-12 00:46:46 -04:00
/* If owner selected not to be notified or invalid stop here */
if ( empty ( $autoassign_owner [ $type ])) {
return false ;
2014-11-23 00:20:28 -05:00
}
}
2015-09-12 00:46:46 -04:00
/* Set new language if required */
2014-11-23 00:20:28 -05:00
hesk_setLanguage ( $autoassign_owner [ 'language' ]);
2015-09-12 00:46:46 -04:00
/* Format email subject and message for staff */
$subject = hesk_getEmailSubject ( $email_template , $ticket );
$message = hesk_getEmailMessage ( $email_template , $ticket , $modsForHesk_settings , 1 );
$htmlMessage = hesk_getHtmlMessage ( $email_template , $ticket , $modsForHesk_settings , 1 );
$hasMessage = hesk_doesTemplateHaveTag ( $email_template , '%%MESSAGE%%' , $modsForHesk_settings );
2014-11-23 00:20:28 -05:00
2015-09-12 00:46:46 -04:00
/* Send email to staff */
hesk_mail ( $autoassign_owner [ 'email' ], $subject , $message , $htmlMessage , $modsForHesk_settings , array (), array (), $hasMessage );
2014-11-23 00:20:28 -05:00
/* Reset language to original one */
hesk_resetLanguage ();
return true ;
} // END hesk_notifyAssignedStaff()
2015-09-12 00:46:46 -04:00
function hesk_notifyStaff ( $email_template , $sql_where , $modsForHesk_settings , $is_ticket = 1 )
2014-11-23 00:20:28 -05:00
{
2015-09-12 00:46:46 -04:00
global $hesk_settings , $hesklang , $ticket ;
// Demo mode
if ( defined ( 'HESK_DEMO' )) {
return true ;
}
$admins = array ();
$res = hesk_dbQuery ( " SELECT `email`,`language`,`isadmin`,`categories` FROM ` " . hesk_dbEscape ( $hesk_settings [ 'db_pfix' ]) . " users` WHERE $sql_where ORDER BY `language` " );
while ( $myuser = hesk_dbFetchAssoc ( $res )) {
/* Is this an administrator? */
if ( $myuser [ 'isadmin' ]) {
$admins [] = array ( 'email' => $myuser [ 'email' ], 'language' => $myuser [ 'language' ]);
continue ;
}
/* Not admin, is he/she allowed this category? */
$myuser [ 'categories' ] = explode ( ',' , $myuser [ 'categories' ]);
if ( in_array ( $ticket [ 'category' ], $myuser [ 'categories' ])) {
$admins [] = array ( 'email' => $myuser [ 'email' ], 'language' => $myuser [ 'language' ]);
continue ;
2014-11-23 00:20:28 -05:00
}
2015-09-12 00:46:46 -04:00
}
2014-11-23 00:20:28 -05:00
2015-09-12 00:46:46 -04:00
if ( count ( $admins ) > 0 ) {
/* Make sure each user gets email in his/her preferred language */
2014-11-23 00:20:28 -05:00
$current_language = 'NONE' ;
$recipients = array ();
2015-09-12 00:46:46 -04:00
$hasMessage = hesk_doesTemplateHaveTag ( $email_template , '%%MESSAGE%%' , $modsForHesk_settings );
/* Loop through staff */
foreach ( $admins as $admin ) {
/* If admin language is NULL force default HESK language */
if ( ! $admin [ 'language' ] || ! isset ( $hesk_settings [ 'languages' ][ $admin [ 'language' ]])) {
$admin [ 'language' ] = HESK_DEFAULT_LANGUAGE ;
2014-11-23 00:20:28 -05:00
}
/* Generate message or add email to the list of recepients */
2015-09-12 00:46:46 -04:00
if ( $admin [ 'language' ] == $current_language ) {
/* We already have the message, just add email to the recipients list */
2014-11-23 00:20:28 -05:00
$recipients [] = $admin [ 'email' ];
2015-09-12 00:46:46 -04:00
} else {
/* Send email messages in previous languages (if required) */
if ( $current_language != 'NONE' ) {
/* Send e-mail to staff */
hesk_mail ( implode ( ',' , $recipients ), $subject , $message , $htmlMessage , $modsForHesk_settings , array (), array (), $hasMessage );
2014-11-23 00:20:28 -05:00
/* Reset list of email addresses */
$recipients = array ();
}
2015-09-12 00:46:46 -04:00
/* Set new language */
hesk_setLanguage ( $admin [ 'language' ]);
2014-11-23 00:20:28 -05:00
2015-09-12 00:46:46 -04:00
/* Format staff email subject and message for this language */
$subject = hesk_getEmailSubject ( $email_template , $ticket );
$message = hesk_getEmailMessage ( $email_template , $ticket , $modsForHesk_settings , $is_ticket );
$htmlMessage = hesk_getHtmlMessage ( $email_template , $ticket , $modsForHesk_settings , $is_ticket );
2015-09-03 21:58:05 -04:00
$hasMessage = hesk_doesTemplateHaveTag ( $email_template , '%%MESSAGE%%' , $modsForHesk_settings );
2014-11-23 00:20:28 -05:00
2015-09-12 00:46:46 -04:00
/* Add email to the recipients list */
$recipients [] = $admin [ 'email' ];
2014-11-23 00:20:28 -05:00
2015-09-12 00:46:46 -04:00
/* Remember the last processed language */
$current_language = $admin [ 'language' ];
2014-11-23 00:20:28 -05:00
}
}
/* Send email messages to the remaining staff */
2015-09-12 00:46:46 -04:00
hesk_mail ( implode ( ',' , $recipients ), $subject , $message , $htmlMessage , $modsForHesk_settings , array (), array (), $hasMessage );
2014-11-23 00:20:28 -05:00
2015-09-12 00:46:46 -04:00
/* Reset language to original one */
hesk_resetLanguage ();
}
2014-11-23 00:20:28 -05:00
return true ;
} // END hesk_notifyStaff()
2016-03-30 22:02:16 -04:00
function mfh_sendCalendarReminder ( $reminder_data , $modsForHesk_settings ) {
global $hesk_settings , $hesklang ;
if ( defined ( 'HESK_DEMO' )) {
return true ;
}
hesk_setLanguage ( $reminder_data [ 'user_language' ]);
$valid_emails = hesk_validEmails ();
$subject = NULL ;
if ( ! isset ( $valid_emails [ 'calendar_reminder' ])) {
hesk_error ( $hesklang [ 'inve' ]);
} else {
$subject = $valid_emails [ 'calendar_reminder' ];
}
// Format email subject and message
$subject = str_replace ( '%%TITLE%%' , $reminder_data [ 'event_name' ], $subject );
$message = hesk_getEmailMessage ( 'calendar_reminder' , NULL , $modsForHesk_settings , 1 , 0 , 1 );
2016-03-31 09:00:39 -04:00
$message = mfh_processCalendarTemplate ( $message , $reminder_data );
2016-03-30 22:02:16 -04:00
$htmlMessage = hesk_getHtmlMessage ( 'calendar_reminder' , NULL , $modsForHesk_settings , 1 , 0 , 1 );
2016-03-31 09:00:39 -04:00
$htmlMessage = mfh_processCalendarTemplate ( $htmlMessage , $reminder_data );
2016-03-30 22:02:16 -04:00
2016-03-31 09:00:39 -04:00
hesk_mail ( $reminder_data [ 'user_email' ], $subject , $message , $htmlMessage , $modsForHesk_settings );
return true ;
}
function mfh_processCalendarTemplate ( $message , $reminder_data ) {
global $hesk_settings ;
2016-03-30 22:02:16 -04:00
if ( $reminder_data [ 'event_all_day' ] == '1' ) {
$format = 'Y-m-d' ;
} else {
$format = $hesk_settings [ 'timeformat' ];
}
2016-03-31 09:00:39 -04:00
$start_date = strtotime ( $reminder_data [ 'event_start' ]);
2016-03-30 22:02:16 -04:00
$formatted_start_date = date ( $format , $start_date );
$formatted_end_date = '' ;
if ( $reminder_data [ 'event_start' ] != $reminder_data [ 'event_end' ]) {
2016-03-31 09:00:39 -04:00
$end_date = strtotime ( $reminder_data [ 'event_end' ]);
2016-03-30 22:02:16 -04:00
$formatted_end_date = ' - ' . date ( $format , $end_date );
}
// Process replaced fields
$message = str_replace ( '%%TITLE%%' , $reminder_data [ 'event_name' ], $message );
$message = str_replace ( '%%LOCATION%%' , $reminder_data [ 'event_location' ], $message );
2016-03-31 09:00:39 -04:00
$message = str_replace ( '%%CATEGORY%%' , $reminder_data [ 'event_category' ], $message );
2016-03-30 22:02:16 -04:00
$message = str_replace ( '%%WHEN%%' , $formatted_start_date . $formatted_end_date , $message );
$message = str_replace ( '%%COMMENTS%%' , $reminder_data [ 'event_comments' ], $message );
2016-03-31 09:00:39 -04:00
return $message ;
2016-03-30 22:02:16 -04:00
}
2014-11-23 00:20:28 -05:00
2016-04-23 22:07:51 -04:00
function mfh_sendOverdueTicketReminder ( $ticket , $users , $modsForHesk_settings ) {
2016-04-20 18:38:52 -04:00
global $hesk_settings , $hesklang ;
if ( defined ( 'HESK_DEMO' )) {
return true ;
}
hesk_setLanguage ( $ticket [ 'user_language' ]);
$valid_emails = hesk_validEmails ();
$subject = NULL ;
if ( ! isset ( $valid_emails [ 'overdue_ticket' ])) {
hesk_error ( $hesklang [ 'inve' ]);
} else {
$subject = $valid_emails [ 'overdue_ticket' ];
}
// Format email subject and message
$subject = str_replace ( '%%TITLE%%' , $ticket [ 'subject' ], $subject );
$subject = str_replace ( '%%TRACKID%%' , $ticket [ 'trackid' ], $subject );
$message = hesk_getEmailMessage ( 'overdue_ticket' , NULL , $modsForHesk_settings , 1 , 0 , 1 );
$message = hesk_processMessage ( $message , $ticket , 1 , 1 , 0 , $modsForHesk_settings );
$htmlMessage = hesk_getHtmlMessage ( 'overdue_ticket' , NULL , $modsForHesk_settings , 1 , 0 , 1 );
$htmlMessage = hesk_processMessage ( $htmlMessage , $ticket , 1 , 1 , 0 , $modsForHesk_settings , 1 );
2016-04-23 22:07:51 -04:00
$emails = [];
2016-04-23 22:08:38 -04:00
if ( $ticket [ 'user_email' ] != NULL ) {
$emails [] = $ticket [ 'user_email' ];
}
2016-04-23 22:07:51 -04:00
foreach ( $users as $user ) {
2016-04-26 12:40:17 -04:00
$categories = explode ( ',' , $user [ 'categories' ]);
2016-04-23 22:07:51 -04:00
if ( $user [ 'email' ] != $ticket [ 'user_email' ]
2016-04-26 12:39:43 -04:00
&& ( $user [ 'isadmin' ] || in_array ( $ticket [ 'category' ], $categories ))) {
2016-04-23 22:07:51 -04:00
$emails [] = $user [ 'email' ];
}
}
foreach ( $emails as $email ) {
hesk_mail ( $email , $subject , $message , $htmlMessage , $modsForHesk_settings );
}
2016-04-20 18:38:52 -04:00
return true ;
}
2014-11-23 00:20:28 -05:00
function hesk_validEmails ()
{
2015-09-12 00:46:46 -04:00
global $hesklang ;
2014-11-23 00:20:28 -05:00
2015-09-12 00:46:46 -04:00
return array (
2014-11-23 00:20:28 -05:00
2015-09-12 00:46:46 -04:00
/*** Emails sent to CLIENT ***/
2014-11-23 00:20:28 -05:00
2015-09-12 00:46:46 -04:00
// --> Send reminder about existing tickets
'forgot_ticket_id' => $hesklang [ 'forgot_ticket_id' ],
2014-11-23 00:20:28 -05:00
2015-09-12 00:46:46 -04:00
// --> Staff replied to a ticket
'new_reply_by_staff' => $hesklang [ 'new_reply_by_staff' ],
2014-11-23 00:20:28 -05:00
2015-09-12 00:46:46 -04:00
// --> New ticket submitted
'new_ticket' => $hesklang [ 'ticket_received' ],
2014-11-23 00:20:28 -05:00
2015-01-02 00:40:41 -05:00
// --> Verify email
'verify_email' => $hesklang [ 'verify_email' ],
2015-01-12 12:49:09 -05:00
// --> Ticket closed
'ticket_closed' => $hesklang [ 'ticket_closed' ],
2014-11-23 00:20:28 -05:00
2015-09-12 00:46:46 -04:00
/*** Emails sent to STAFF ***/
2014-11-23 00:20:28 -05:00
2015-09-12 00:46:46 -04:00
// --> Ticket moved to a new category
'category_moved' => $hesklang [ 'category_moved' ],
2014-11-23 00:20:28 -05:00
2015-09-12 00:46:46 -04:00
// --> Client replied to a ticket
'new_reply_by_customer' => $hesklang [ 'new_reply_by_customer' ],
2014-11-23 00:20:28 -05:00
2015-09-12 00:46:46 -04:00
// --> New ticket submitted
'new_ticket_staff' => $hesklang [ 'new_ticket_staff' ],
2014-11-23 00:20:28 -05:00
2015-09-12 00:46:46 -04:00
// --> New ticket assigned to staff
'ticket_assigned_to_you' => $hesklang [ 'ticket_assigned_to_you' ],
2014-11-23 00:20:28 -05:00
2015-09-12 00:46:46 -04:00
// --> New private message
'new_pm' => $hesklang [ 'new_pm' ],
2014-11-23 00:20:28 -05:00
2015-09-12 00:46:46 -04:00
// --> New note by someone to a ticket assigned to you
'new_note' => $hesklang [ 'new_note' ],
2014-11-23 00:20:28 -05:00
2015-01-12 12:49:09 -05:00
// --> Staff password reset email
'reset_password' => $hesklang [ 'reset_password' ],
2016-03-30 22:02:16 -04:00
// --> Calendar reminder
2016-04-20 18:38:52 -04:00
'calendar_reminder' => $hesklang [ 'calendar_reminder' ],
// --> Overdue Ticket reminder
'overdue_ticket' => $hesklang [ 'overdue_ticket' ],
2016-03-30 22:02:16 -04:00
2014-11-23 00:20:28 -05:00
);
} // END hesk_validEmails()
2015-09-12 00:46:46 -04:00
function hesk_mail ( $to , $subject , $message , $htmlMessage , $modsForHesk_settings , $cc = array (), $bcc = array (), $hasMessageTag = false )
2014-11-23 00:20:28 -05:00
{
2015-09-12 00:46:46 -04:00
global $hesk_settings , $hesklang , $ticket ;
2014-11-23 00:20:28 -05:00
2015-09-12 00:46:46 -04:00
// Are we in demo mode or are all email fields blank? If so, don't send an email.
if ( defined ( 'HESK_DEMO' )
2015-08-02 16:26:53 -04:00
|| (( $to == NULL || $to == '' )
&& ( $cc == NULL || count ( $cc ) == 0 )
2015-09-12 00:46:46 -04:00
&& ( $bcc == NULL || count ( $bcc ) == 0 ))
) {
return true ;
}
2014-11-23 00:20:28 -05:00
// Encode subject to UTF-8
2015-09-12 00:46:46 -04:00
$subject = " =?UTF-8?B? " . base64_encode ( hesk_html_entity_decode ( $subject )) . " ?= " ;
2014-11-23 00:20:28 -05:00
2015-03-19 21:31:45 -04:00
// Auto-generate URLs for HTML-formatted emails
$htmlMessage = hesk_makeURL ( $htmlMessage , '' , false );
2014-11-23 00:20:28 -05:00
// Setup "name <email>" for headers
2015-09-12 00:46:46 -04:00
if ( $hesk_settings [ 'noreply_name' ]) {
$hesk_settings [ 'from_header' ] = " =?UTF-8?B? " . base64_encode ( hesk_html_entity_decode ( $hesk_settings [ 'noreply_name' ])) . " ?= < " . $hesk_settings [ 'noreply_mail' ] . " > " ;
} else {
$hesk_settings [ 'from_header' ] = $hesk_settings [ 'noreply_mail' ];
2014-11-23 00:20:28 -05:00
}
2015-09-12 00:46:46 -04:00
// Uncomment for debugging
# echo "<p>TO: $to<br >SUBJECT: $subject<br >MSG: $message</p>";
# return true;
2014-11-23 00:20:28 -05:00
2015-01-30 00:36:31 -05:00
// Use mailgun
2015-09-12 00:46:46 -04:00
if ( $modsForHesk_settings [ 'use_mailgun' ]) {
2015-01-30 00:36:31 -05:00
ob_start ();
$ch = curl_init ();
2015-09-12 00:46:46 -04:00
curl_setopt ( $ch , CURLOPT_URL , " https://api.mailgun.net/v2/ " . $modsForHesk_settings [ 'mailgun_domain' ] . " /messages " );
2015-01-30 00:36:31 -05:00
curl_setopt ( $ch , CURLOPT_HTTPAUTH , CURLAUTH_BASIC );
curl_setopt ( $ch , CURLOPT_USERPWD , 'api:' . $modsForHesk_settings [ 'mailgun_api_key' ]);
curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , 1 );
curl_setopt ( $ch , CURLOPT_CONNECTTIMEOUT , 10 );
curl_setopt ( $ch , CURLOPT_SSL_VERIFYPEER , 0 );
curl_setopt ( $ch , CURLOPT_SSL_VERIFYHOST , 0 );
curl_setopt ( $ch , CURLOPT_POST , true );
2015-02-01 00:58:12 -05:00
$postfields = array (
2015-09-12 00:46:46 -04:00
'from' => $hesk_settings [ 'from_header' ],
'to' => $to ,
'h:Reply-To' => $hesk_settings [ 'from_header' ],
'subject' => $subject ,
'text' => $message
2015-02-01 00:58:12 -05:00
);
2015-09-12 00:46:46 -04:00
if ( count ( $cc ) > 0 ) {
$postfields [ 'cc' ] = implode ( ',' , $cc );
2015-02-01 00:58:12 -05:00
}
2015-09-12 00:46:46 -04:00
if ( count ( $bcc ) > 0 ) {
$postfields [ 'bcc' ] = implode ( ',' , $bcc );
2015-02-01 00:58:12 -05:00
}
2015-09-12 00:46:46 -04:00
if ( $modsForHesk_settings [ 'html_emails' ]) {
2015-02-01 00:58:12 -05:00
$postfields [ 'html' ] = $htmlMessage ;
}
2015-09-12 00:46:46 -04:00
if ( $hasMessageTag && $modsForHesk_settings [ 'attachments' ] && $hesk_settings [ 'attachments' ][ 'use' ] && isset ( $ticket [ 'attachments' ]) && strlen ( $ticket [ 'attachments' ])) {
2015-04-12 15:53:06 -04:00
$postfields = processDirectAttachments ( 'mailgun' , $postfields );
2015-04-12 15:26:53 -04:00
}
2015-02-01 00:58:12 -05:00
curl_setopt ( $ch , CURLOPT_POSTFIELDS , $postfields );
2015-01-30 00:36:31 -05:00
$result = curl_exec ( $ch );
curl_close ( $ch );
$tmp = trim ( ob_get_contents ());
ob_end_clean ();
return ( strlen ( $tmp )) ? $tmp : true ;
}
2015-02-01 00:58:12 -05:00
2015-04-13 00:48:26 -04:00
$outerboundary = sha1 ( uniqid ());
$innerboundary = sha1 ( uniqid ());
if ( $outerboundary == $innerboundary ) {
$innerboundary .= '1' ;
}
2015-04-10 22:28:38 -04:00
$plaintextMessage = $message ;
2015-09-12 00:46:46 -04:00
$message = " -- " . $outerboundary . " \n " ;
$message .= " Content-Type: multipart/alternative; boundary= \" " . $innerboundary . " \" \n \n " ;
2015-04-13 00:48:26 -04:00
2015-09-12 00:46:46 -04:00
$message .= " -- " . $innerboundary . " \n " ;
$message .= " Content-Type: text/plain; charset= " . $hesklang [ 'ENCODING' ] . " \n \n " ;
$message .= $plaintextMessage . " \n \n " ;
2015-02-01 00:58:12 -05:00
//Prepare the message for HTML or non-html
2015-09-12 00:46:46 -04:00
if ( $modsForHesk_settings [ 'html_emails' ]) {
$message .= " -- " . $innerboundary . " \n " ;
$message .= " Content-Type: text/html; charset= " . $hesklang [ 'ENCODING' ] . " \n \n " ;
$message .= $htmlMessage . " \n \n " ;
2015-02-01 00:58:12 -05:00
}
2015-04-13 00:48:26 -04:00
//-- Close the email
2015-09-12 00:46:46 -04:00
$message .= " -- " . $innerboundary . " -- " ;
2015-04-13 00:48:26 -04:00
2014-11-23 00:20:28 -05:00
// Use PHP's mail function
2015-09-12 00:46:46 -04:00
if ( ! $hesk_settings [ 'smtp' ]) {
// Set additional headers
2015-02-01 00:58:12 -05:00
$headers = '' ;
2015-04-10 22:28:38 -04:00
$headers .= " MIME-Version: 1.0 \n " ;
2015-09-12 00:46:46 -04:00
$headers .= " From: $hesk_settings[from_header] \n " ;
if ( count ( $cc ) > 0 ) {
$headers .= " Cc: " . implode ( ',' , $cc );
2015-01-22 00:01:16 -05:00
}
2015-09-12 00:46:46 -04:00
if ( count ( $bcc ) > 0 ) {
$headers .= " Bcc: " . implode ( ',' , $bcc );
2015-01-22 00:01:16 -05:00
}
2015-09-12 00:46:46 -04:00
$headers .= " Reply-To: $hesk_settings[from_header] \n " ;
$headers .= " Return-Path: $hesk_settings[webmaster_mail] \n " ;
$headers .= " Date: " . date ( DATE_RFC2822 ) . " \n " ;
$headers .= " Content-Type: multipart/mixed;boundary= \" " . $outerboundary . " \" " ;
2014-11-23 00:20:28 -05:00
2015-04-12 15:26:53 -04:00
// Add attachments if necessary
2015-09-12 00:46:46 -04:00
if ( $hasMessageTag && $modsForHesk_settings [ 'attachments' ] && $hesk_settings [ 'attachments' ][ 'use' ] && isset ( $ticket [ 'attachments' ]) && strlen ( $ticket [ 'attachments' ])) {
2015-04-13 00:48:26 -04:00
$message .= processDirectAttachments ( 'phpmail' , NULL , $outerboundary );
2015-04-12 15:26:53 -04:00
}
2015-09-12 00:46:46 -04:00
$message .= " \n \n " . '--' . $outerboundary . '--' ;
2015-04-12 15:26:53 -04:00
2015-09-12 00:46:46 -04:00
// Send using PHP mail() function
2014-11-23 00:20:28 -05:00
ob_start ();
2015-09-12 00:46:46 -04:00
mail ( $to , $subject , $message , $headers );
2014-11-23 00:20:28 -05:00
$tmp = trim ( ob_get_contents ());
ob_end_clean ();
return ( strlen ( $tmp )) ? $tmp : true ;
}
// Use a SMTP server directly instead
2015-09-12 00:46:46 -04:00
$smtp = new smtp_class ;
$smtp -> host_name = $hesk_settings [ 'smtp_host_name' ];
$smtp -> host_port = $hesk_settings [ 'smtp_host_port' ];
$smtp -> timeout = $hesk_settings [ 'smtp_timeout' ];
$smtp -> ssl = $hesk_settings [ 'smtp_ssl' ];
$smtp -> start_tls = $hesk_settings [ 'smtp_tls' ];
$smtp -> user = $hesk_settings [ 'smtp_user' ];
$smtp -> password = hesk_htmlspecialchars_decode ( $hesk_settings [ 'smtp_password' ]);
$smtp -> debug = 1 ;
2014-11-23 00:20:28 -05:00
// Start output buffering so that any errors don't break headers
ob_start ();
// Send the e-mail using SMTP
2015-09-12 00:46:46 -04:00
$to_arr = explode ( ',' , $to );
2015-01-22 00:01:16 -05:00
$headersArray = array (
" From: $hesk_settings[from_header] " ,
" To: $to " ,
" Reply-To: $hesk_settings[from_header] " ,
" Return-Path: $hesk_settings[webmaster_mail] " ,
" Subject: " . $subject ,
2015-02-01 00:58:12 -05:00
" Date: " . date ( DATE_RFC2822 )
2015-01-22 00:01:16 -05:00
);
2015-09-12 00:46:46 -04:00
array_push ( $headersArray , " MIME-Version: 1.0 " );
array_push ( $headersArray , " Content-Type: multipart/mixed;boundary= \" " . $outerboundary . " \" " );
2015-04-10 22:28:38 -04:00
2015-09-12 00:46:46 -04:00
if ( count ( $cc ) > 0 ) {
array_push ( $headersArray , " Cc: " . implode ( ',' , $cc ));
2015-01-22 00:01:16 -05:00
}
2015-09-12 00:46:46 -04:00
if ( count ( $bcc ) > 0 ) {
array_push ( $headersArray , " Bcc: " . implode ( ',' , $bcc ));
2015-01-22 00:01:16 -05:00
}
2015-04-12 15:26:53 -04:00
// Add attachments if necessary
2015-09-12 00:46:46 -04:00
if ( $hasMessageTag && $modsForHesk_settings [ 'attachments' ] && $hesk_settings [ 'attachments' ][ 'use' ] && isset ( $ticket [ 'attachments' ]) && strlen ( $ticket [ 'attachments' ])) {
2015-04-13 00:48:26 -04:00
$message .= processDirectAttachments ( 'smtp' , NULL , $outerboundary );
2015-04-12 15:26:53 -04:00
}
2015-09-12 00:46:46 -04:00
$message .= " \n \n " . '--' . $outerboundary . '--' ;
if ( ! $smtp -> SendMessage ( $hesk_settings [ 'noreply_mail' ], $to_arr , $headersArray , $message )) {
// Suppress errors unless we are in debug mode
if ( $hesk_settings [ 'debug_mode' ]) {
$error = $hesklang [ 'cnsm' ] . ' ' . $to . '<br /><br />' .
$hesklang [ 'error' ] . ': ' . htmlspecialchars ( $smtp -> error ) . '<br /><br />' .
'<textarea name="smtp_log" rows="10" cols="60">' . ob_get_contents () . '</textarea>' ;
ob_end_clean ();
hesk_error ( $error );
} else {
$_SESSION [ 'HESK_2ND_NOTICE' ] = true ;
2014-11-23 00:20:28 -05:00
$_SESSION [ 'HESK_2ND_MESSAGE' ] = $hesklang [ 'esf' ] . ' ' . $hesklang [ 'contact_webmsater' ] . ' <a href="mailto:' . $hesk_settings [ 'webmaster_mail' ] . '">' . $hesk_settings [ 'webmaster_mail' ] . '</a>' ;
}
}
ob_end_clean ();
2015-09-12 00:46:46 -04:00
return true ;
2014-11-23 00:20:28 -05:00
} // END hesk_mail()
2015-09-12 00:46:46 -04:00
function hesk_getEmailSubject ( $eml_file , $ticket = '' , $is_ticket = 1 , $strip = 0 )
2014-11-23 00:20:28 -05:00
{
2015-09-12 00:46:46 -04:00
global $hesk_settings , $hesklang ;
2014-11-23 00:20:28 -05:00
2015-09-12 00:46:46 -04:00
// Demo mode
if ( defined ( 'HESK_DEMO' )) {
return '' ;
}
2014-11-23 00:20:28 -05:00
2015-09-12 00:46:46 -04:00
/* Get list of valid emails */
2014-11-23 00:20:28 -05:00
$valid_emails = hesk_validEmails ();
2015-09-12 00:46:46 -04:00
/* Verify this is a valid email include */
if ( ! isset ( $valid_emails [ $eml_file ])) {
hesk_error ( $hesklang [ 'inve' ]);
} else {
$msg = $valid_emails [ $eml_file ];
2014-11-23 00:20:28 -05:00
}
/* If not a ticket-related email return subject as is */
2015-09-12 00:46:46 -04:00
if ( ! $ticket ) {
return $msg ;
2014-11-23 00:20:28 -05:00
}
2015-09-12 00:46:46 -04:00
/* Strip slashes from the subject only if it's a new ticket */
if ( $strip ) {
$ticket [ 'subject' ] = stripslashes ( $ticket [ 'subject' ]);
}
2014-11-23 00:20:28 -05:00
/* Not a ticket, but has some info in the $ticket array */
2015-09-12 00:46:46 -04:00
if ( ! $is_ticket ) {
return str_replace ( '%%SUBJECT%%' , $ticket [ 'subject' ], $msg );
}
/* Set category title */
$ticket [ 'category' ] = hesk_msgToPlain ( hesk_getCategoryName ( $ticket [ 'category' ]), 1 );
/* Get priority */
switch ( $ticket [ 'priority' ]) {
case 0 :
$ticket [ 'priority' ] = $hesklang [ 'critical' ];
break ;
case 1 :
$ticket [ 'priority' ] = $hesklang [ 'high' ];
break ;
case 2 :
$ticket [ 'priority' ] = $hesklang [ 'medium' ];
break ;
default :
$ticket [ 'priority' ] = $hesklang [ 'low' ];
}
2014-11-23 00:20:28 -05:00
/* Set status */
2015-09-12 00:46:46 -04:00
$statusRs = hesk_dbQuery ( " SELECT `Key` FROM ` " . hesk_dbEscape ( $hesk_settings [ 'db_pfix' ]) . " statuses` WHERE `ID` = " . $ticket [ 'status' ]);
2015-01-25 00:57:58 -05:00
$row = hesk_dbFetchAssoc ( $statusRs );
2015-05-12 22:08:17 -04:00
$ticket [ 'status' ] = $hesklang [ $row [ 'Key' ]];
2014-11-23 00:20:28 -05:00
2015-09-12 00:46:46 -04:00
/* Replace all special tags */
$msg = str_replace ( '%%SUBJECT%%' , $ticket [ 'subject' ], $msg );
$msg = str_replace ( '%%TRACK_ID%%' , $ticket [ 'trackid' ], $msg );
$msg = str_replace ( '%%CATEGORY%%' , $ticket [ 'category' ], $msg );
$msg = str_replace ( '%%PRIORITY%%' , $ticket [ 'priority' ], $msg );
$msg = str_replace ( '%%STATUS%%' , $ticket [ 'status' ], $msg );
2014-11-23 00:20:28 -05:00
2015-09-12 00:46:46 -04:00
return $msg ;
2014-11-23 00:20:28 -05:00
} // hesk_getEmailSubject()
2015-09-12 00:46:46 -04:00
function hesk_getHtmlMessage ( $eml_file , $ticket , $modsForHesk_settings , $is_admin = 0 , $is_ticket = 1 , $just_message = 0 )
2015-02-01 00:58:12 -05:00
{
2015-09-01 12:22:03 -04:00
global $hesk_settings , $hesklang ;
2015-02-01 00:58:12 -05:00
// Demo mode
2015-09-12 00:46:46 -04:00
if ( defined ( 'HESK_DEMO' ) || ! $modsForHesk_settings [ 'html_emails' ]) {
2015-02-01 00:58:12 -05:00
return '' ;
}
// We won't do validation here, as hesk_getEmailMessage will be called which handles validation.
// Get email template
$original_eml_file = $eml_file ;
2015-09-12 00:46:46 -04:00
$eml_file = 'language/' . $hesk_settings [ 'languages' ][ $hesk_settings [ 'language' ]][ 'folder' ] . '/emails/html/' . $original_eml_file . '.txt' ;
$plain_eml_file = 'language/' . $hesk_settings [ 'languages' ][ $hesk_settings [ 'language' ]][ 'folder' ] . '/emails/' . $original_eml_file . '.txt' ;
2015-02-01 00:58:12 -05:00
2015-09-12 00:46:46 -04:00
if ( file_exists ( HESK_PATH . $eml_file )) {
$msg = file_get_contents ( HESK_PATH . $eml_file );
} elseif ( file_exists ( HESK_PATH . $plain_eml_file )) {
$msg = file_get_contents ( HESK_PATH . $plain_eml_file );
} else {
hesk_error ( $hesklang [ 'emfm' ] . ': ' . $eml_file );
2015-02-01 00:58:12 -05:00
}
//Perform logic common between hesk_getEmailMessage and hesk_getHtmlMessage
2015-09-03 21:58:05 -04:00
$msg = hesk_processMessage ( $msg , $ticket , $is_admin , $is_ticket , $just_message , $modsForHesk_settings , true );
2015-02-01 00:58:12 -05:00
return $msg ;
}
2014-11-23 00:20:28 -05:00
2015-09-12 00:46:46 -04:00
function hesk_getEmailMessage ( $eml_file , $ticket , $modsForHesk_settings , $is_admin = 0 , $is_ticket = 1 , $just_message = 0 )
2014-11-23 00:20:28 -05:00
{
2015-09-12 00:46:46 -04:00
global $hesk_settings , $hesklang ;
2014-11-23 00:20:28 -05:00
2015-09-12 00:46:46 -04:00
// Demo mode
if ( defined ( 'HESK_DEMO' )) {
return '' ;
}
2014-11-23 00:20:28 -05:00
2015-09-12 00:46:46 -04:00
/* Get list of valid emails */
2014-11-23 00:20:28 -05:00
$valid_emails = hesk_validEmails ();
2015-09-12 00:46:46 -04:00
/* Verify this is a valid email include */
if ( ! isset ( $valid_emails [ $eml_file ])) {
hesk_error ( $hesklang [ 'inve' ]);
2014-11-23 00:20:28 -05:00
}
2015-09-12 00:46:46 -04:00
/* Get email template */
$eml_file = 'language/' . $hesk_settings [ 'languages' ][ $hesk_settings [ 'language' ]][ 'folder' ] . '/emails/' . $eml_file . '.txt' ;
2014-11-23 00:20:28 -05:00
2015-09-12 00:46:46 -04:00
if ( file_exists ( HESK_PATH . $eml_file )) {
$msg = file_get_contents ( HESK_PATH . $eml_file );
} else {
hesk_error ( $hesklang [ 'emfm' ] . ': ' . $eml_file );
2014-11-23 00:20:28 -05:00
}
2015-09-03 21:58:05 -04:00
$msg = hesk_processMessage ( $msg , $ticket , $is_admin , $is_ticket , $just_message , $modsForHesk_settings );
2015-02-01 00:58:12 -05:00
return $msg ;
} // END hesk_getEmailMessage
2015-09-03 21:58:05 -04:00
function hesk_doesTemplateHaveTag ( $eml_file , $tag , $modsForHesk_settings )
2015-04-18 22:00:25 -04:00
{
2015-09-01 12:22:03 -04:00
global $hesk_settings ;
2015-09-12 00:46:46 -04:00
$path = 'language/' . $hesk_settings [ 'languages' ][ $hesk_settings [ 'language' ]][ 'folder' ] . '/emails/' . $eml_file . '.txt' ;
2015-04-18 22:20:47 -04:00
$htmlHasTag = false ;
if ( $modsForHesk_settings [ 'html_emails' ]) {
2015-09-12 00:46:46 -04:00
$htmlPath = 'language/' . $hesk_settings [ 'languages' ][ $hesk_settings [ 'language' ]][ 'folder' ] . '/emails/html/' . $eml_file . '.txt' ;
$htmlContents = file_get_contents ( HESK_PATH . $htmlPath );
2015-04-18 22:20:47 -04:00
$htmlHasTag = ! ( strpos ( $htmlContents , $tag ) === false );
}
2015-04-18 22:00:25 -04:00
$emailContents = file_get_contents ( HESK_PATH . $path );
2015-04-18 22:20:47 -04:00
return ! ( strpos ( $emailContents , $tag ) === false ) || $htmlHasTag ;
2015-04-18 22:00:25 -04:00
}
2015-09-03 21:58:05 -04:00
function hesk_processMessage ( $msg , $ticket , $is_admin , $is_ticket , $just_message , $modsForHesk_settings , $isForHtml = 0 )
2015-02-01 00:58:12 -05:00
{
2015-09-01 12:22:03 -04:00
global $hesk_settings , $hesklang ;
2015-02-01 00:58:12 -05:00
2014-11-23 00:20:28 -05:00
/* Return just the message without any processing? */
2015-09-12 00:46:46 -04:00
if ( $just_message ) {
2015-02-01 00:58:12 -05:00
return $msg ;
2014-11-23 00:20:28 -05:00
}
2015-02-01 00:58:12 -05:00
// Convert any entities in site title to plain text
$hesk_settings [ 'site_title' ] = hesk_msgToPlain ( $hesk_settings [ 'site_title' ], 1 );
2014-11-23 00:20:28 -05:00
/* If it's not a ticket-related mail (like "a new PM") just process quickly */
2015-09-12 00:46:46 -04:00
if ( ! $is_ticket ) {
2015-02-01 00:58:12 -05:00
$trackingURL = $hesk_settings [ 'hesk_url' ] . '/' . $hesk_settings [ 'admin_dir' ] . '/mail.php?a=read&id=' . intval ( $ticket [ 'id' ]);
2014-11-23 00:20:28 -05:00
2015-09-12 00:46:46 -04:00
$msg = str_replace ( '%%NAME%%' , $ticket [ 'name' ], $msg );
$msg = str_replace ( '%%SUBJECT%%' , $ticket [ 'subject' ], $msg );
$msg = str_replace ( '%%TRACK_URL%%' , $trackingURL , $msg );
$msg = str_replace ( '%%SITE_TITLE%%' , $hesk_settings [ 'site_title' ], $msg );
$msg = str_replace ( '%%SITE_URL%%' , $hesk_settings [ 'site_url' ], $msg );
2014-11-23 00:20:28 -05:00
2015-09-12 00:46:46 -04:00
if ( isset ( $ticket [ 'message' ])) {
2015-10-16 22:49:09 -04:00
// If HTML is enabled, let's unescape everything, and call html2text.
2015-09-12 00:46:46 -04:00
if ( $isForHtml ) {
2015-03-11 20:28:42 -04:00
$htmlMessage = nl2br ( $ticket [ 'message' ]);
2015-08-31 12:59:37 -04:00
$msg = str_replace ( '%%MESSAGE_NO_ATTACHMENTS%%' , $htmlMessage , $msg );
2015-03-11 20:28:42 -04:00
return str_replace ( '%%MESSAGE%%' , $htmlMessage , $msg );
}
2015-10-17 20:50:16 -04:00
$message_has_html = checkForHtml ( $ticket );
if ( $message_has_html ) {
2015-10-16 22:49:09 -04:00
if ( ! function_exists ( 'convert_html_to_text' )) {
require ( HESK_PATH . 'inc/html2text/html2text.php' );
}
$ticket [ 'message' ] = convert_html_to_text ( $ticket [ 'message' ]);
$ticket [ 'message' ] = fix_newlines ( $ticket [ 'message' ]);
}
2015-08-31 12:59:37 -04:00
$msg = str_replace ( '%%MESSAGE_NO_ATTACHMENTS%%' , $ticket [ 'message' ], $msg );
2015-02-01 00:58:12 -05:00
return str_replace ( '%%MESSAGE%%' , $ticket [ 'message' ], $msg );
2015-09-12 00:46:46 -04:00
} else {
2015-02-01 00:58:12 -05:00
return $msg ;
}
2014-11-23 00:20:28 -05:00
}
// Is email required to view ticket (for customers only)?
2015-02-01 00:58:12 -05:00
$hesk_settings [ 'e_param' ] = $hesk_settings [ 'email_view_ticket' ] ? '&e=' . rawurlencode ( $ticket [ 'email' ]) : '' ;
2014-11-23 00:20:28 -05:00
/* Generate the ticket URLs */
$trackingURL = $hesk_settings [ 'hesk_url' ];
2015-09-12 00:46:46 -04:00
$trackingURL .= $is_admin ? '/' . $hesk_settings [ 'admin_dir' ] . '/admin_ticket.php' : '/ticket.php' ;
$trackingURL .= '?track=' . $ticket [ 'trackid' ] . ( $is_admin ? '' : $hesk_settings [ 'e_param' ]) . '&Refresh=' . rand ( 10000 , 99999 );
2014-11-23 00:20:28 -05:00
2015-02-01 00:58:12 -05:00
/* Set category title */
$ticket [ 'category' ] = hesk_msgToPlain ( hesk_getCategoryName ( $ticket [ 'category' ]), 1 );
2014-11-23 00:20:28 -05:00
2015-02-01 00:58:12 -05:00
/* Set priority title */
2015-09-12 00:46:46 -04:00
switch ( $ticket [ 'priority' ]) {
2015-02-01 00:58:12 -05:00
case 0 :
$ticket [ 'priority' ] = $hesklang [ 'critical' ];
break ;
case 1 :
$ticket [ 'priority' ] = $hesklang [ 'high' ];
break ;
case 2 :
$ticket [ 'priority' ] = $hesklang [ 'medium' ];
break ;
default :
$ticket [ 'priority' ] = $hesklang [ 'low' ];
}
2014-11-23 00:20:28 -05:00
/* Get owner name */
2015-09-12 00:46:46 -04:00
$ticket [ 'owner' ] = hesk_msgToPlain ( hesk_getOwnerName ( $ticket [ 'owner' ]), 1 );
2014-11-23 00:20:28 -05:00
/* Set status */
2015-09-12 00:46:46 -04:00
$statusRs = hesk_dbQuery ( " SELECT `Key` FROM ` " . hesk_dbEscape ( $hesk_settings [ 'db_pfix' ]) . " statuses` WHERE `ID` = " . $ticket [ 'status' ]);
2015-01-25 00:57:58 -05:00
$row = hesk_dbFetchAssoc ( $statusRs );
2015-05-12 22:08:17 -04:00
$ticket [ 'status' ] = $hesklang [ $row [ 'Key' ]];
2014-11-23 00:20:28 -05:00
2015-02-01 00:58:12 -05:00
/* Replace all special tags */
2015-09-12 00:46:46 -04:00
$msg = str_replace ( '%%NAME%%' , $ticket [ 'name' ], $msg );
$msg = str_replace ( '%%SUBJECT%%' , $ticket [ 'subject' ], $msg );
$msg = str_replace ( '%%TRACK_ID%%' , $ticket [ 'trackid' ], $msg );
$msg = str_replace ( '%%TRACK_URL%%' , $trackingURL , $msg );
$msg = str_replace ( '%%SITE_TITLE%%' , $hesk_settings [ 'site_title' ], $msg );
$msg = str_replace ( '%%SITE_URL%%' , $hesk_settings [ 'site_url' ], $msg );
$msg = str_replace ( '%%CATEGORY%%' , $ticket [ 'category' ], $msg );
$msg = str_replace ( '%%PRIORITY%%' , $ticket [ 'priority' ], $msg );
$msg = str_replace ( '%%OWNER%%' , $ticket [ 'owner' ], $msg );
$msg = str_replace ( '%%STATUS%%' , $ticket [ 'status' ], $msg );
$msg = str_replace ( '%%EMAIL%%' , $ticket [ 'email' ], $msg );
$msg = str_replace ( '%%CREATED%%' , $ticket [ 'dt' ], $msg );
$msg = str_replace ( '%%UPDATED%%' , $ticket [ 'lastchange' ], $msg );
$msg = str_replace ( '%%ID%%' , $ticket [ 'id' ], $msg );
2014-11-23 00:20:28 -05:00
2015-02-01 00:58:12 -05:00
/* All custom fields */
2015-09-12 00:46:46 -04:00
foreach ( $hesk_settings [ 'custom_fields' ] as $k => $v ) {
if ( $v [ 'use' ]) {
if ( $v [ 'type' ] == 'checkbox' ) {
$ticket [ $k ] = str_replace ( " <br /> " , " \n " , $ticket [ $k ]);
2014-11-23 00:20:28 -05:00
}
2015-09-12 00:46:46 -04:00
$msg = str_replace ( '%%' . strtoupper ( $k ) . '%%' , stripslashes ( $ticket [ $k ]), $msg );
} else {
$msg = str_replace ( '%%' . strtoupper ( $k ) . '%%' , '' , $msg );
2014-11-23 00:20:28 -05:00
}
2015-02-01 00:58:12 -05:00
}
2014-11-23 00:20:28 -05:00
2015-10-16 22:49:09 -04:00
2015-02-01 00:58:12 -05:00
// Is message tag in email template?
2015-09-12 00:46:46 -04:00
if ( strpos ( $msg , '%%MESSAGE%%' ) !== false ) {
2015-02-01 00:58:12 -05:00
// Replace message
2015-09-12 00:46:46 -04:00
if ( $isForHtml ) {
2015-03-11 20:28:42 -04:00
$htmlMessage = nl2br ( $ticket [ 'message' ]);
$msg = str_replace ( '%%MESSAGE%%' , $htmlMessage , $msg );
2015-09-12 00:46:46 -04:00
} else {
2015-10-16 22:49:09 -04:00
$plainTextMessage = $ticket [ 'message' ];
2015-10-17 20:50:16 -04:00
$message_has_html = checkForHtml ( $ticket );
if ( $message_has_html ) {
2015-10-16 22:49:09 -04:00
if ( ! function_exists ( 'convert_html_to_text' )) {
require ( HESK_PATH . 'inc/html2text/html2text.php' );
}
$plainTextMessage = convert_html_to_text ( $plainTextMessage );
$plainTextMessage = fix_newlines ( $plainTextMessage );
}
$msg = str_replace ( '%%MESSAGE%%' , $plainTextMessage , $msg );
2015-03-11 20:28:42 -04:00
}
2014-11-23 00:20:28 -05:00
2015-04-12 15:26:53 -04:00
// Add direct links to any attachments at the bottom of the email message OR add them as attachments, depending on the settings
2015-09-12 00:46:46 -04:00
if ( $hesk_settings [ 'attachments' ][ 'use' ] && isset ( $ticket [ 'attachments' ]) && strlen ( $ticket [ 'attachments' ])) {
2015-04-12 15:26:53 -04:00
if ( ! $modsForHesk_settings [ 'attachments' ]) {
2015-04-12 11:11:31 -04:00
if ( $isForHtml ) {
2015-04-12 15:26:53 -04:00
$msg .= " <br><br><br> " . $hesklang [ 'fatt' ];
2015-04-12 11:11:31 -04:00
} else {
2015-04-12 15:26:53 -04:00
$msg .= " \n \n \n " . $hesklang [ 'fatt' ];
}
$att = explode ( ',' , substr ( $ticket [ 'attachments' ], 0 , - 1 ));
2015-09-12 00:46:46 -04:00
foreach ( $att as $myatt ) {
2015-04-12 15:26:53 -04:00
list ( $att_id , $att_name , $saved_name ) = explode ( '#' , $myatt );
if ( $isForHtml ) {
$msg .= " <br><br> " . $att_name . " <br> " ;
} else {
$msg .= " \n \n " . $att_name . " \n " ;
}
2015-09-12 00:46:46 -04:00
$msg .= $hesk_settings [ 'hesk_url' ] . '/download_attachment.php?att_id=' . $att_id . '&track=' . $ticket [ 'trackid' ] . $hesk_settings [ 'e_param' ];
2015-04-12 11:11:31 -04:00
}
2015-02-01 00:58:12 -05:00
}
2015-04-12 15:26:53 -04:00
// If attachments setting is set to 1, we'll add the attachments separately later; otherwise we'll duplicate the number of attachments.
2015-02-01 00:58:12 -05:00
}
2014-11-23 00:20:28 -05:00
2015-02-01 00:58:12 -05:00
// For customer notifications: if we allow email piping/pop 3 fetching and
// stripping quoted replies add an "reply above this line" tag
2015-09-12 00:46:46 -04:00
if ( ! $is_admin && ( $hesk_settings [ 'email_piping' ] || $hesk_settings [ 'pop3' ]) && $hesk_settings [ 'strip_quoted' ]) {
2015-02-01 00:58:12 -05:00
$msg = $hesklang [ 'EMAIL_HR' ] . " \n \n " . $msg ;
}
2015-08-31 12:59:37 -04:00
} elseif ( strpos ( $msg , '%%MESSAGE_NO_ATTACHMENTS%%' ) !== false ) {
2015-09-12 00:46:46 -04:00
if ( $isForHtml ) {
2015-08-31 12:59:37 -04:00
$htmlMessage = nl2br ( $ticket [ 'message' ]);
$msg = str_replace ( '%%MESSAGE_NO_ATTACHMENTS%%' , $htmlMessage , $msg );
2015-09-12 00:46:46 -04:00
} else {
2015-10-18 22:09:05 -04:00
$plainTextMessage = $ticket [ 'message' ];
$message_has_html = checkForHtml ( $ticket );
if ( $message_has_html ) {
if ( ! function_exists ( 'convert_html_to_text' )) {
require ( HESK_PATH . 'inc/html2text/html2text.php' );
}
$plainTextMessage = convert_html_to_text ( $plainTextMessage );
$plainTextMessage = fix_newlines ( $plainTextMessage );
}
$msg = str_replace ( '%%MESSAGE_NO_ATTACHMENTS%%' , $plainTextMessage , $msg );
2015-08-31 12:59:37 -04:00
}
2015-02-01 00:58:12 -05:00
}
2014-11-23 00:20:28 -05:00
2015-02-01 00:58:12 -05:00
return $msg ;
}
2015-04-12 15:26:53 -04:00
// $postfields is only required for mailgun.
// $boundary is only required for PHP/SMTP
2015-09-12 00:46:46 -04:00
function processDirectAttachments ( $emailMethod , $postfields = NULL , $boundary = '' )
{
2015-04-12 15:26:53 -04:00
global $hesk_settings , $ticket ;
$att = explode ( ',' , substr ( $ticket [ 'attachments' ], 0 , - 1 ));
// if using mailgun, add each attachment to the array
if ( $emailMethod == 'mailgun' ) {
$i = 0 ;
foreach ( $att as $myatt ) {
list ( $att_id , $att_name , $saved_name ) = explode ( '#' , $myatt );
2015-09-12 00:46:46 -04:00
$postfields [ 'attachment[' . $i . ']' ] = '@' . HESK_PATH . $hesk_settings [ 'attach_dir' ] . '/' . $saved_name ;
2015-04-12 15:26:53 -04:00
$i ++ ;
}
2015-04-12 15:53:06 -04:00
return $postfields ;
2015-04-12 15:26:53 -04:00
} else {
$attachments = '' ;
foreach ( $att as $myatt ) {
list ( $att_id , $att_name , $saved_name ) = explode ( '#' , $myatt );
2015-09-12 00:46:46 -04:00
$attachments .= " \n \n " . " -- " . $boundary . " \n " ;
$attachments .= " Content-Type: application/octet-stream; name= \" " . $att_name . " \" \n " ;
2015-04-12 15:26:53 -04:00
$attachments .= " Content-Disposition: attachment \n " ;
$attachments .= " Content-Transfer-Encoding: base64 \n \n " ;
2015-09-12 00:46:46 -04:00
$attachmentBinary = file_get_contents ( HESK_PATH . $hesk_settings [ 'attach_dir' ] . '/' . $saved_name );
2015-04-12 15:26:53 -04:00
$attcontents = chunk_split ( base64_encode ( $attachmentBinary ));
2015-09-12 00:46:46 -04:00
$attachments .= $attcontents . " \n \n " ;
2015-04-12 15:26:53 -04:00
}
return $attachments ;
}
}
2015-10-17 20:50:16 -04:00
function checkForHtml ( $ticket ) {
global $hesk_settings ;
$repliesRs = hesk_dbQuery ( " SELECT * FROM ` " . hesk_dbEscape ( $hesk_settings [ 'db_pfix' ]) . " replies` WHERE `replyto` = " . intval ( $ticket [ 'id' ]) . " ORDER BY `id` DESC LIMIT 1 " );
if ( hesk_dbNumRows ( $repliesRs ) != 1 ) {
return $ticket [ 'html' ];
}
$reply = hesk_dbFetchAssoc ( $repliesRs );
return $reply [ 'html' ];
}