2014-11-23 00:20:28 -05:00
< ? php
/*******************************************************************************
* Title : Help Desk Software HESK
2015-06-23 23:33:03 -04:00
* Version : 2.6 . 4 from 22 nd June 2015
2014-11-23 00:20:28 -05:00
* Author : Klemen Stirn
* Website : http :// www . hesk . com
********************************************************************************
* COPYRIGHT AND TRADEMARK NOTICE
2015-02-22 22:17:56 -05:00
* Copyright 2005 - 2015 Klemen Stirn . All Rights Reserved .
2014-11-23 00:20:28 -05:00
* 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
*******************************************************************************/
/* Check if this is a valid include */
if ( ! defined ( 'IN_SCRIPT' )) { die ( 'Invalid attempt' );}
2015-02-26 18:33:19 -05:00
// Include modsForHesk_settings.inc.php if it hasn't been included already
2015-03-07 21:28:22 -05:00
if ( ! isset ( $modsForHesk_settings ))
2015-02-26 18:33:19 -05:00
{
2015-03-07 21:28:22 -05:00
include ( HESK_PATH . 'modsForHesk_settings.inc.php' );
2015-02-26 18:33:19 -05:00
}
2014-11-23 00:20:28 -05:00
/* Get includes for SMTP */
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' );
}
}
2015-01-01 00:33:23 -05:00
function hesk_notifyCustomerForVerifyEmail ( $email_template = 'verify_email' , $activationKey )
{
global $hesk_settings , $ticket ;
if ( defined ( 'HESK_DEMO' ))
{
return true ;
}
// Format email subject and message
2015-01-02 00:40:41 -05:00
$subject = hesk_getEmailSubject ( $email_template , $ticket );
$message = hesk_getEmailMessage ( $email_template , $ticket );
2015-02-01 00:58:12 -05:00
$htmlMessage = hesk_getHtmlMessage ( $email_template , $ticket );
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 );
$hasMessage = hesk_doesTemplateHaveTag ( $email_template , '%%MESSAGE%%' );
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 ();
foreach ( $hesk_settings [ 'custom_fields' ] as $k => $v ) {
if ( $v [ 'use' ]) {
if ( $v [ 'type' ] == 'email' && ! empty ( $ticket [ $k ])) {
if ( $v [ 'value' ] == 'cc' ) {
$emails = explode ( ',' , $ticket [ $k ]);
array_push ( $ccEmails , $emails );
} elseif ( $v [ 'value' ] == 'bcc' ) {
$emails = explode ( ',' , $ticket [ $k ]);
array_push ( $bccEmails , $emails );
}
}
}
}
2015-04-18 22:20:47 -04:00
hesk_mail ( $ticket [ 'email' ], $subject , $message , $htmlMessage , $ccEmails , $bccEmails , $hasMessage );
2015-01-01 00:33:23 -05:00
}
2014-11-23 00:20:28 -05:00
function hesk_notifyCustomer ( $email_template = 'new_ticket' )
{
global $hesk_settings , $hesklang , $ticket ;
// Demo mode
if ( defined ( 'HESK_DEMO' ) )
{
return true ;
}
2015-01-23 00:30:46 -05:00
$changedLanguage = false ;
//Set the user's language according to the ticket.
if ( $ticket [ 'language' ] !== NULL )
{
hesk_setLanguage ( $ticket [ 'language' ]);
$changedLanguage = true ;
}
2014-11-23 00:20:28 -05:00
// Format email subject and message
$subject = hesk_getEmailSubject ( $email_template , $ticket );
$message = hesk_getEmailMessage ( $email_template , $ticket );
2015-02-01 00:58:12 -05:00
$htmlMessage = hesk_getHtmlMessage ( $email_template , $ticket );
2015-04-18 22:20:47 -04:00
$hasMessage = hesk_doesTemplateHaveTag ( $email_template , '%%MESSAGE%%' );
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 ();
foreach ( $hesk_settings [ 'custom_fields' ] as $k => $v ) {
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 ]);
}
}
}
}
2014-11-23 00:20:28 -05:00
// Send e-mail
2015-04-18 22:20:47 -04:00
hesk_mail ( $ticket [ 'email' ], $subject , $message , $htmlMessage , $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
if ( $changedLanguage )
{
hesk_resetLanguage ();
}
2014-11-23 00:20:28 -05:00
return true ;
} // END hesk_notifyCustomer()
function hesk_notifyAssignedStaff ( $autoassign_owner , $email_template , $type = 'notify_assigned' )
{
global $hesk_settings , $hesklang , $ticket ;
// Demo mode
if ( defined ( 'HESK_DEMO' ) )
{
return true ;
}
$ticket [ 'owner' ] = intval ( $ticket [ 'owner' ]);
/* Need to lookup owner info from the database? */
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 " );
$autoassign_owner = hesk_dbFetchAssoc ( $res );
$hesk_settings [ 'user_data' ][ $ticket [ 'owner' ]] = $autoassign_owner ;
/* If owner selected not to be notified or invalid stop here */
if ( empty ( $autoassign_owner [ $type ]) )
{
return false ;
}
}
/* Set new language if required */
hesk_setLanguage ( $autoassign_owner [ 'language' ]);
/* Format email subject and message for staff */
$subject = hesk_getEmailSubject ( $email_template , $ticket );
$message = hesk_getEmailMessage ( $email_template , $ticket , 1 );
2015-02-01 00:58:12 -05:00
$htmlMessage = hesk_getHtmlMessage ( $email_template , $ticket , 1 );
2015-04-18 22:20:47 -04:00
$hasMessage = hesk_doesTemplateHaveTag ( $email_template , '%%MESSAGE%%' );
2014-11-23 00:20:28 -05:00
/* Send email to staff */
2015-04-18 22:20:47 -04:00
hesk_mail ( $autoassign_owner [ 'email' ], $subject , $message , $htmlMessage , array (), array (), $hasMessage );
2014-11-23 00:20:28 -05:00
/* Reset language to original one */
hesk_resetLanguage ();
return true ;
} // END hesk_notifyAssignedStaff()
function hesk_notifyStaff ( $email_template , $sql_where , $is_ticket = 1 )
{
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 ;
}
}
if ( count ( $admins ) > 0 )
{
/* Make sure each user gets email in his/her preferred language */
$current_language = 'NONE' ;
$recipients = array ();
2015-05-27 21:52:26 -04:00
$hasMessage = hesk_doesTemplateHaveTag ( $email_template , '%%MESSAGE%%' );
2014-11-23 00:20:28 -05:00
/* 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 ;
}
/* Generate message or add email to the list of recepients */
if ( $admin [ 'language' ] == $current_language )
{
/* We already have the message, just add email to the recipients list */
$recipients [] = $admin [ 'email' ];
}
else
{
/* Send email messages in previous languages (if required) */
if ( $current_language != 'NONE' )
{
/* Send e-mail to staff */
2015-04-18 22:20:47 -04:00
hesk_mail ( implode ( ',' , $recipients ), $subject , $message , $htmlMessage , array (), array (), $hasMessage );
2014-11-23 00:20:28 -05:00
/* Reset list of email addresses */
$recipients = array ();
}
/* Set new language */
hesk_setLanguage ( $admin [ 'language' ]);
/* Format staff email subject and message for this language */
$subject = hesk_getEmailSubject ( $email_template , $ticket );
$message = hesk_getEmailMessage ( $email_template , $ticket , $is_ticket );
2015-02-01 00:58:12 -05:00
$htmlMessage = hesk_getHtmlMessage ( $email_template , $ticket , $is_ticket );
2015-04-18 22:20:47 -04:00
$hasMessage = hesk_doesTemplateHaveTag ( $email_template , '%%MESSAGE%%' );
2014-11-23 00:20:28 -05:00
/* Add email to the recipients list */
$recipients [] = $admin [ 'email' ];
/* Remember the last processed language */
$current_language = $admin [ 'language' ];
}
}
/* Send email messages to the remaining staff */
2015-04-18 22:20:47 -04:00
hesk_mail ( implode ( ',' , $recipients ), $subject , $message , $htmlMessage , array (), array (), $hasMessage );
2014-11-23 00:20:28 -05:00
/* Reset language to original one */
hesk_resetLanguage ();
}
return true ;
} // END hesk_notifyStaff()
function hesk_validEmails ()
{
global $hesklang ;
return array (
/*** Emails sent to CLIENT ***/
// --> Send reminder about existing tickets
'forgot_ticket_id' => $hesklang [ 'forgot_ticket_id' ],
// --> Staff replied to a ticket
'new_reply_by_staff' => $hesklang [ 'new_reply_by_staff' ],
// --> New ticket submitted
'new_ticket' => $hesklang [ 'ticket_received' ],
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
/*** Emails sent to STAFF ***/
// --> Ticket moved to a new category
'category_moved' => $hesklang [ 'category_moved' ],
// --> Client replied to a ticket
'new_reply_by_customer' => $hesklang [ 'new_reply_by_customer' ],
// --> New ticket submitted
'new_ticket_staff' => $hesklang [ 'new_ticket_staff' ],
// --> New ticket assigned to staff
'ticket_assigned_to_you' => $hesklang [ 'ticket_assigned_to_you' ],
// --> New private message
'new_pm' => $hesklang [ 'new_pm' ],
// --> New note by someone to a ticket assigned to you
'new_note' => $hesklang [ 'new_note' ],
2015-01-12 12:49:09 -05:00
// --> Staff password reset email
'reset_password' => $hesklang [ 'reset_password' ],
2014-11-23 00:20:28 -05:00
);
} // END hesk_validEmails()
2015-04-18 22:00:25 -04:00
function hesk_mail ( $to , $subject , $message , $htmlMessage , $cc = array (), $bcc = array (), $hasMessageTag = false )
2014-11-23 00:20:28 -05:00
{
2015-04-12 15:26:53 -04:00
global $hesk_settings , $hesklang , $modsForHesk_settings , $ticket ;
2014-11-23 00:20:28 -05:00
2015-08-02 16:26:53 -04:00
// Are we in demo mode or are all email fields blank? If so, don't send an email.
if ( defined ( 'HESK_DEMO' )
|| (( $to == NULL || $to == '' )
&& ( $cc == NULL || count ( $cc ) == 0 )
&& ( $bcc == NULL || count ( $bcc ) == 0 )))
2014-11-23 00:20:28 -05:00
{
return true ;
}
// Encode subject to UTF-8
$subject = " =?UTF-8?B? " . base64_encode ( hesk_html_entity_decode ( $subject ) ) . " ?= " ;
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
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' ];
}
// Uncomment for debugging
# echo "<p>TO: $to<br >SUBJECT: $subject<br >MSG: $message</p>";
# return true;
2015-01-30 00:36:31 -05:00
// Use mailgun
if ( $modsForHesk_settings [ 'use_mailgun' ])
{
ob_start ();
$ch = curl_init ();
curl_setopt ( $ch , CURLOPT_URL , " https://api.mailgun.net/v2/ " . $modsForHesk_settings [ 'mailgun_domain' ] . " /messages " );
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 (
'from' => $hesk_settings [ 'from_header' ],
'to' => $to ,
'h:Reply-To' => $hesk_settings [ 'from_header' ],
'subject' => $subject ,
'text' => $message
);
if ( count ( $cc ) > 0 )
{
$postfields [ 'cc' ] = implode ( ',' , $cc );
}
if ( count ( $bcc ) > 0 )
{
$postfields [ 'bcc' ] = implode ( ',' , $bcc );
}
if ( $modsForHesk_settings [ 'html_emails' ])
{
$postfields [ 'html' ] = $htmlMessage ;
}
2015-04-18 22:00:25 -04:00
if ( $hasMessageTag && $modsForHesk_settings [ 'attachments' ] && $hesk_settings [ 'attachments' ][ 'use' ] && isset ( $ticket [ 'attachments' ]) && strlen ( $ticket [ 'attachments' ]))
2015-04-12 15:26:53 -04:00
{
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-04-13 00:48:26 -04:00
$message = " -- " . $outerboundary . " \n " ;
$message .= " Content-Type: multipart/alternative; boundary= \" " . $innerboundary . " \" \n \n " ;
$message .= " -- " . $innerboundary . " \n " ;
2015-04-10 22:28:38 -04:00
$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
if ( $modsForHesk_settings [ 'html_emails' ])
{
2015-04-13 00:48:26 -04:00
$message .= " -- " . $innerboundary . " \n " ;
2015-02-01 00:58:12 -05:00
$message .= " Content-Type: text/html; charset= " . $hesklang [ 'ENCODING' ] . " \n \n " ;
$message .= $htmlMessage . " \n \n " ;
}
2015-04-13 00:48:26 -04:00
//-- Close the email
$message .= " -- " . $innerboundary . " -- " ;
2014-11-23 00:20:28 -05:00
// Use PHP's mail function
2015-02-01 00:58:12 -05:00
if ( ! $hesk_settings [ 'smtp' ])
2014-11-23 00:20:28 -05:00
{
// 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-02-01 00:58:12 -05:00
$headers .= " From: $hesk_settings[from_header] \n " ;
2015-01-22 00:01:16 -05:00
if ( count ( $cc ) > 0 )
{
$headers .= " Cc: " . implode ( ',' , $cc );
}
if ( count ( $bcc ) > 0 )
{
$headers .= " Bcc: " . implode ( ',' , $bcc );
}
2014-11-23 00:20:28 -05:00
$headers .= " Reply-To: $hesk_settings[from_header] \n " ;
$headers .= " Return-Path: $hesk_settings[webmaster_mail] \n " ;
$headers .= " Date: " . date ( DATE_RFC2822 ) . " \n " ;
2015-04-13 00:48:26 -04:00
$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-04-18 22:00:25 -04:00
if ( $hasMessageTag && $modsForHesk_settings [ 'attachments' ] && $hesk_settings [ 'attachments' ][ 'use' ] && isset ( $ticket [ 'attachments' ]) && strlen ( $ticket [ 'attachments' ]))
2015-04-12 15:26:53 -04:00
{
2015-04-13 00:48:26 -04:00
$message .= processDirectAttachments ( 'phpmail' , NULL , $outerboundary );
2015-04-12 15:26:53 -04:00
}
2015-04-18 15:51:40 -04:00
$message .= " \n \n " . '--' . $outerboundary . '--' ;
2015-04-12 15:26:53 -04:00
2014-11-23 00:20:28 -05:00
// Send using PHP mail() function
ob_start ();
mail ( $to , $subject , $message , $headers );
$tmp = trim ( ob_get_contents ());
ob_end_clean ();
return ( strlen ( $tmp )) ? $tmp : true ;
}
// Use a SMTP server directly instead
$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 ;
// Start output buffering so that any errors don't break headers
ob_start ();
// Send the e-mail using SMTP
$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-04-10 22:28:38 -04:00
array_push ( $headersArray , " MIME-Version: 1.0 " );
2015-04-13 00:48:26 -04:00
array_push ( $headersArray , " Content-Type: multipart/mixed;boundary= \" " . $outerboundary . " \" " );
2015-04-10 22:28:38 -04:00
2015-01-22 00:01:16 -05:00
if ( count ( $cc ) > 0 )
{
array_push ( $headersArray , " Cc: " . implode ( ',' , $cc ));
}
if ( count ( $bcc ) > 0 )
{
array_push ( $headersArray , " Bcc: " . implode ( ',' , $bcc ));
}
2015-04-12 15:26:53 -04:00
// Add attachments if necessary
2015-04-18 22:00:25 -04:00
if ( $hasMessageTag && $modsForHesk_settings [ 'attachments' ] && $hesk_settings [ 'attachments' ][ 'use' ] && isset ( $ticket [ 'attachments' ]) && strlen ( $ticket [ 'attachments' ]))
2015-04-12 15:26:53 -04:00
{
2015-04-13 00:48:26 -04:00
$message .= processDirectAttachments ( 'smtp' , NULL , $outerboundary );
2015-04-12 15:26:53 -04:00
}
2015-04-18 15:51:40 -04:00
$message .= " \n \n " . '--' . $outerboundary . '--' ;
2015-01-22 00:01:16 -05:00
if ( ! $smtp -> SendMessage ( $hesk_settings [ 'noreply_mail' ], $to_arr , $headersArray , $message ))
2014-11-23 00:20:28 -05:00
{
// 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 ;
$_SESSION [ 'HESK_2ND_MESSAGE' ] = $hesklang [ 'esf' ] . ' ' . $hesklang [ 'contact_webmsater' ] . ' <a href="mailto:' . $hesk_settings [ 'webmaster_mail' ] . '">' . $hesk_settings [ 'webmaster_mail' ] . '</a>' ;
}
}
ob_end_clean ();
return true ;
} // END hesk_mail()
function hesk_getEmailSubject ( $eml_file , $ticket = '' , $is_ticket = 1 , $strip = 0 )
{
global $hesk_settings , $hesklang ;
// Demo mode
if ( defined ( 'HESK_DEMO' ) )
{
return '' ;
}
/* Get list of valid emails */
$valid_emails = hesk_validEmails ();
/* Verify this is a valid email include */
if ( ! isset ( $valid_emails [ $eml_file ]))
{
hesk_error ( $hesklang [ 'inve' ]);
}
else
{
$msg = $valid_emails [ $eml_file ];
}
/* If not a ticket-related email return subject as is */
if ( ! $ticket )
{
return $msg ;
}
/* Strip slashes from the subject only if it's a new ticket */
if ( $strip )
{
$ticket [ 'subject' ] = stripslashes ( $ticket [ 'subject' ]);
}
/* Not a ticket, but has some info in the $ticket array */
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' ];
}
/* Set status */
2015-05-12 22:08:17 -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
/* 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 );
return $msg ;
} // hesk_getEmailSubject()
2015-02-01 00:58:12 -05:00
function hesk_getHtmlMessage ( $eml_file , $ticket , $is_admin = 0 , $is_ticket = 1 , $just_message = 0 )
{
global $hesk_settings , $hesklang , $modsForHesk_settings ;
// Demo mode
if ( defined ( 'HESK_DEMO' ) || ! $modsForHesk_settings [ 'html_emails' ])
{
return '' ;
}
// We won't do validation here, as hesk_getEmailMessage will be called which handles validation.
// Get email template
$original_eml_file = $eml_file ;
$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' ;
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 );
}
//Perform logic common between hesk_getEmailMessage and hesk_getHtmlMessage
2015-03-11 20:28:42 -04:00
$msg = hesk_processMessage ( $msg , $ticket , $is_admin , $is_ticket , $just_message , true );
2015-02-01 00:58:12 -05:00
return $msg ;
}
2014-11-23 00:20:28 -05:00
function hesk_getEmailMessage ( $eml_file , $ticket , $is_admin = 0 , $is_ticket = 1 , $just_message = 0 )
{
global $hesk_settings , $hesklang ;
// Demo mode
if ( defined ( 'HESK_DEMO' ) )
{
return '' ;
}
/* Get list of valid emails */
$valid_emails = hesk_validEmails ();
/* Verify this is a valid email include */
if ( ! isset ( $valid_emails [ $eml_file ]))
{
hesk_error ( $hesklang [ 'inve' ]);
}
/* Get email template */
$eml_file = 'language/' . $hesk_settings [ 'languages' ][ $hesk_settings [ 'language' ]][ 'folder' ] . '/emails/' . $eml_file . '.txt' ;
if ( file_exists ( HESK_PATH . $eml_file ))
{
$msg = file_get_contents ( HESK_PATH . $eml_file );
}
else
{
hesk_error ( $hesklang [ 'emfm' ] . ': ' . $eml_file );
}
2015-02-01 00:58:12 -05:00
$msg = hesk_processMessage ( $msg , $ticket , $is_admin , $is_ticket , $just_message );
return $msg ;
} // END hesk_getEmailMessage
2015-04-18 22:20:47 -04:00
function hesk_doesTemplateHaveTag ( $eml_file , $tag )
2015-04-18 22:00:25 -04:00
{
2015-04-18 22:20:47 -04:00
global $hesk_settings , $modsForHesk_settings ;
2015-04-19 09:49:19 -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-04-19 09:49:19 -04:00
$htmlPath = 'language/' . $hesk_settings [ 'languages' ][ $hesk_settings [ 'language' ]][ 'folder' ] . '/emails/html/' . $eml_file . '.txt' ;
2015-04-18 22:20:47 -04:00
$htmlContents = file_get_contents ( HESK_PATH . $htmlPath );
$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-03-11 20:28:42 -04:00
function hesk_processMessage ( $msg , $ticket , $is_admin , $is_ticket , $just_message , $isForHtml = 0 )
2015-02-01 00:58:12 -05:00
{
2015-04-12 15:26:53 -04:00
global $hesk_settings , $hesklang , $modsForHesk_settings ;
2015-02-01 00:58:12 -05:00
2014-11-23 00:20:28 -05:00
/* Return just the message without any processing? */
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 */
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-02-01 00:58:12 -05: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-02-01 00:58:12 -05:00
if ( isset ( $ticket [ 'message' ]) )
{
2015-03-11 20:28:42 -04:00
if ( $isForHtml )
{
$htmlMessage = nl2br ( $ticket [ 'message' ]);
return str_replace ( '%%MESSAGE%%' , $htmlMessage , $msg );
}
2015-02-01 00:58:12 -05:00
return str_replace ( '%%MESSAGE%%' , $ticket [ 'message' ], $msg );
}
else
{
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-02-01 00:58:12 -05:00
$trackingURL .= $is_admin ? '/' . $hesk_settings [ 'admin_dir' ] . '/admin_ticket.php' : '/ticket.php' ;
2014-11-23 00:20:28 -05:00
$trackingURL .= '?track=' . $ticket [ 'trackid' ] . ( $is_admin ? '' : $hesk_settings [ 'e_param' ]) . '&Refresh=' . rand ( 10000 , 99999 );
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 */
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
/* Get owner name */
$ticket [ 'owner' ] = hesk_msgToPlain ( hesk_getOwnerName ( $ticket [ 'owner' ]), 1 );
/* Set status */
2015-05-12 22:08:17 -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 */
$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 );
2014-11-23 00:20:28 -05:00
$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 );
2015-01-12 12:49:09 -05:00
$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 */
foreach ( $hesk_settings [ 'custom_fields' ] as $k => $v )
{
if ( $v [ 'use' ])
{
if ( $v [ 'type' ] == 'checkbox' )
2014-11-23 00:20:28 -05:00
{
2015-02-01 00:58:12 -05:00
$ticket [ $k ] = str_replace ( " <br /> " , " \n " , $ticket [ $k ]);
2014-11-23 00:20:28 -05:00
}
2015-02-01 00:58:12 -05:00
$msg = str_replace ( '%%' . strtoupper ( $k ) . '%%' , stripslashes ( $ticket [ $k ]), $msg );
}
2014-11-23 00:20:28 -05:00
else
{
2015-02-01 00:58:12 -05:00
$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-02-01 00:58:12 -05:00
// Is message tag in email template?
if ( strpos ( $msg , '%%MESSAGE%%' ) !== false )
{
// Replace message
2015-03-11 20:28:42 -04:00
if ( $isForHtml )
{
$htmlMessage = nl2br ( $ticket [ 'message' ]);
$msg = str_replace ( '%%MESSAGE%%' , $htmlMessage , $msg );
} else
{
$msg = str_replace ( '%%MESSAGE%%' , $ticket [ 'message' ], $msg );
}
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
// if ($modsForHesk_settings['attachments'] == 'inline' (other is 'attachment') {...}
2015-02-01 00:58:12 -05: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 ));
foreach ( $att as $myatt )
{
list ( $att_id , $att_name , $saved_name ) = explode ( '#' , $myatt );
if ( $isForHtml ) {
$msg .= " <br><br> " . $att_name . " <br> " ;
} else {
$msg .= " \n \n " . $att_name . " \n " ;
}
$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
if ( ! $is_admin && ( $hesk_settings [ 'email_piping' ] || $hesk_settings [ 'pop3' ]) && $hesk_settings [ 'strip_quoted' ])
{
$msg = $hesklang [ 'EMAIL_HR' ] . " \n \n " . $msg ;
}
}
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-04-12 23:45:04 -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 );
$postfields [ 'attachment[' . $i . ']' ] = '@' . HESK_PATH . $hesk_settings [ 'attach_dir' ] . '/' . $saved_name ;
$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 );
$attachments .= " \n \n " . " -- " . $boundary . " \n " ;
$attachments .= " Content-Type: application/octet-stream; name= \" " . $att_name . " \" \n " ;
$attachments .= " Content-Disposition: attachment \n " ;
$attachments .= " Content-Transfer-Encoding: base64 \n \n " ;
$attachmentBinary = file_get_contents ( HESK_PATH . $hesk_settings [ 'attach_dir' ] . '/' . $saved_name );
$attcontents = chunk_split ( base64_encode ( $attachmentBinary ));
$attachments .= $attcontents . " \n \n " ;
}
return $attachments ;
}
}