| Current Path : /home/chauffn/www/components/com_creativecontactform/helpers/ |
| Current File : /home/chauffn/www/components/com_creativecontactform/helpers/mailer.php |
<?php
/**
* Joomla! component Creative Contact Form
*
* @version $Id: 2012-04-05 14:30:25 svn $
* @author creative-solutions.net
* @package Creative Contact Form
* @subpackage com_creativecontactform
* @license GNU/GPL
*
*/
// no direct access
defined('_JEXEC') or die('Restircted access');
error_reporting(0);
//check captcha
// session_start();
$captcha_tested = true;
if(isset($_POST['creativecontactform_captcha'])) {
foreach($_POST['creativecontactform_captcha'] as $captcha_key => $val) {
$session_keeper = 'creative_captcha_'.$captcha_key;
if(trim(strtolower($val)) != $_SESSION[$session_keeper]) {
$captcha_tested = false;
break;
}
}
}
define( 'CAPTCHA_TESTED', $captcha_tested );
$module_id = JRequest::getInt('creativecontactform_module_id', 0, 'post');
$form_id = JRequest::getInt('creativecontactform_form_id', 0, 'post');
$get_token = JRequest::getInt('get_token', 0, 'get');
// $comparams = JComponentHelper::getParams( 'com_creativecontactform' );
$db = JFactory::getDBO();
//get form configuration
////4.6.1 added sp.name
$query = "
SELECT
sp.`name`,
sp.`email_template`,
sp.`auto_reply`,
sp.`email_to`,
sp.`email_bcc`,
sp.`email_subject`,
sp.`email_from`,
sp.`email_from_name`,
sp.`email_replyto`,
sp.`email_replyto_name`,
sp.`email_info_show_referrer`,
sp.`email_info_show_ip`,
sp.`email_info_show_browser`,
sp.`email_info_show_os`,
sp.`email_info_show_sc_res`,
sp.`check_token`
FROM
`#__creative_forms` sp
WHERE sp.published = '1'
AND sp.id = '".$form_id."'";
$db->setQuery($query);
$form_data = $db->loadAssoc();
$check_token = $form_data['check_token'];
//4.6.1 added
$form_name = $form_data['name'];
$email_template = $form_data['email_template']; //4.6.1 added
$auto_reply = $form_data['auto_reply']; //4.6.1 added
$form_ishtml = $email_template == '[NOHTML]' ? false : true; // 4.6.1 added
//check if captcha exists
$query_captcha = "
SELECT
sp.id
FROM
`#__creative_fields` sp
WHERE sp.published = '1'
AND sp.id_type = '13'
AND sp.id_form = '".$form_id."'";
$db->setQuery($query_captcha);
$db->query();
$count_captcha = $db->getNumRows();
$count_captcha = (int) $count_captcha;
$captcha_error = false;
if( $count_captcha > 0 && !isset($_POST['creativecontactform_captcha']))
$captcha_error = true;
//google reCAPTCHA
$query_captcha = "
SELECT
sp.recaptcha_security_key
FROM
`#__creative_fields` sp
WHERE sp.published = '1'
AND sp.id_type = '19'
AND sp.id_form = '".$form_id."'";
$db->setQuery($query_captcha);
$db->query();
$count_recaptcha = $db->getNumRows();
$count_recaptcha = (int) $count_recaptcha;
$recaptcha_secret = $db->loadResult();
$recaptcha_error = false;
if( $count_recaptcha > 0 ) {
$recaptcha_error = true;
$response_string = JRequest::getVar( 'g-recaptcha-response', '', 'post' );
$ip = JRequest::getVar( 'creativecontactform_ip', '', 'post' );
if($response_string != '') {
//$response_string .= '.';
$recaptcha_verify_link = 'https://www.google.com/recaptcha/api/siteverify?secret='.$recaptcha_secret.'&response='.$response_string.'&remoteip='.$ip;
$recaptcha_response = file_get_contents($recaptcha_verify_link);
preg_match('/\"success\": (true|false)/', $recaptcha_response, $matches);
preg_match('/\"error-codes\": \[(.*?)\]/s', $recaptcha_response, $matches_errors);
$recaptcha_errors = isset($matches_errors[1]) ? $matches_errors[1] : '';
$response_success = $matches[1];
if($response_success == 'true')
$recaptcha_error = false;
}
}
JResponse::allowCache( false );
JResponse::setHeader( 'Content-Type', 'text/plain' );
if($get_token == 0) {
if (!JRequest::checkToken() && $check_token == 0) {
echo '[{"invalid":"invalid_token"}]';
}
else if (!CAPTCHA_TESTED || $captcha_error) {
echo '[{"invalid":"invalid_captcha"}]';
}
else if ($recaptcha_error) {
echo '[{"invalid":"invalid_recaptcha"}]';
}
else {
$info = Array();
$config = JFactory::getConfig();
//get from
$fromname = $config->get( 'fromname' );
$fromname = $fromname == '' ? $config->get( 'sitename' ) : $fromname;
$mailfrom = $config->get('mailfrom');
if (!$mailfrom ) {
$info[] = 'Mail from not set in Joomla Global Configuration';
}
//get email to
$email_to = array();
if ( $form_data['email_to'] != '' ) {
$email_to = explode(',', $form_data['email_to']);
}
if (count($email_to) == 0) {
$email_to = $mailfrom;
}
// Email subject
$creativecontactform_subject = $form_data['email_subject'] == '' ? JText::_('COM_CREATIVECONTACTFORM_MESSAGE_SENT_FROM') . ' ' . $config->get('sitename') : $form_data['email_subject'];
$mail = JFactory::getMailer();
//4.6.1 html email
if($form_ishtml) {
preg_match('/(<td.*?>)\[FIELD_LABEL\]<\/td>/i', $email_template, $matches);
$field_label_td = $matches[1];
preg_match('/(<td.*?>)\[FIELD_VALUE\]<\/td>/i', $email_template, $matches);
$field_value_td = $matches[1];
$email_template = preg_replace('/<td.*?>\[FIELD_LABEL\]<\/td>/i', '', $email_template);
$email_template = preg_replace('/<td.*?>\[FIELD_VALUE\]<\/td>/i', '[EMAILBODY]', $email_template);
}
//generate the body
$body = '';
$body_raw = '';
$user_email = '';
$user_name = '';
$newline = "\n";
$fields_array = array();
if(isset($_POST['creativecontactform_fields'])) {
$multiple_recipients = array();
$esignature_path = '';
foreach($_POST['creativecontactform_fields'] as $field_data) {
$field_label = strip_tags(trim($field_data[1]));
$field_type = strip_tags(trim($field_data[2]));
$field_id = $field_data[3];
if(isset($field_data[0])) {
if($field_type == 'multiple-recipients') {
if(is_array($field_data[0]))
$multiple_recipients = $field_data[0];
else
$multiple_recipients[] = $field_data[0];
continue;
}
else if ($field_type == 'esignature') {
$dataURL = $field_data[0];
$dataURL = str_replace('data:image/png;base64,', '', $dataURL);
$dataURL = str_replace(' ', '+', $dataURL);
$image = base64_decode($dataURL);
$file_path = JPATH_BASE . '/components/com_creativecontactform/views/creativeupload/files/';
$esignature_path = $file_path . 'esignature.png';
file_put_contents($esignature_path, $image);
continue;
}
else {
if(is_array($field_data[0])) {
$field_value = implode(', ',$field_data[0]);
$field_value = strip_tags(trim($field_value));
}
else
$field_value = strip_tags(trim($field_data[0]));
}
}
else {
$field_value = '';
}
$field_value = str_replace('creative_empty', '', $field_value);
//4.6.1 added
$fields_array[$field_id][0] = $field_label;
$fields_array[$field_id][1] = $field_value;
// start separator
if($field_type == 'text-area')
$fields_seperator = ":\n";
else
$fields_seperator = ": ";
// end separator
if($field_type == 'text-area')
$fields_end_seperator = "\r\n\n";
else
$fields_end_seperator = "\r\n";
if($form_ishtml) {
$field_value_html = nl2br($field_value);
$body .= '<tr>'.$newline.$field_label_td.$field_label.':</td>'.$newline;
$body .= $field_value_td.$field_value_html.'</td>'.$newline.'</tr>'.$newline;
}
else {
$body .= $field_label.$fields_seperator.$field_value.$fields_end_seperator;
}
$body_raw .= $field_label.$fields_seperator.$field_value.$fields_end_seperator;
if($field_type == 'email')
$user_email = $field_value;
if($field_type == 'name')
$user_name = $field_value;
}
}
// data for database
$sub_name = $user_name;
$sub_email = $user_email;
$sub_message = addslashes($body_raw);
$sub_uploads = '';
// add email info
$creativecontactform_ip = strip_tags( JRequest::getVar( 'creativecontactform_ip', '', 'post' ));
$creativecontactform_referrer = strip_tags( JRequest::getVar( 'creativecontactform_referrer', '', 'post' ));
$creativecontactform_page_title = strip_tags( JRequest::getVar( 'creativecontactform_page_title', '', 'post' ));
$creativecontactform_browser = strip_tags( JRequest::getVar( 'creativecontactform_browser', '', 'post' ));
$creativecontactform_operating_system = strip_tags( JRequest::getVar( 'creativecontactform_operating_system', '', 'post' ));
$creativecontactform_sc_res = strip_tags( JRequest::getVar( 'creativecontactform_sc_res', '', 'post' ));
if($form_ishtml) {
$body .= '<tr>'.$newline.'<td style="height: 30px"></td>'.$newline.'</tr>'.$newline;
if($form_data['email_info_show_referrer'] == 1) {
$body .= '<tr>'.$newline.$field_label_td.'Page Title:</td>'.$newline;
$body .= $field_value_td.'<a href="'.$creativecontactform_referrer.'" title="'.$creativecontactform_page_title.'" target="_blank">'.$creativecontactform_page_title.'</a></td>'.$newline.'</tr>'.$newline;
}
if($form_data['email_info_show_ip'] == 1){
$body .= '<tr>'.$newline.$field_label_td.'IP Address:</td>'.$newline;
$body .= $field_value_td.$creativecontactform_ip.'</td>'.$newline.'</tr>'.$newline;
}
if($form_data['email_info_show_browser'] == 1){
$body .= '<tr>'.$newline.$field_label_td.'Browser:</td>'.$newline;
$body .= $field_value_td.$creativecontactform_browser.'</td>'.$newline.'</tr>'.$newline;
}
if($form_data['email_info_show_os'] == 1){
$body .= '<tr>'.$newline.$field_label_td.'Operating System:</td>'.$newline;
$body .= $field_value_td.$creativecontactform_operating_system.'</td>'.$newline.'</tr>'.$newline;
}
if($form_data['email_info_show_sc_res'] == 1){
$body .= '<tr>'.$newline.$field_label_td.'Screen Resolution:</td>'.$newline;
$body .= $field_value_td.$creativecontactform_sc_res.'</td>'.$newline.'</tr>'.$newline;
}
}
else {
if($form_data['email_info_show_referrer'] == 1) {
$body .= 'Page Title: '.$creativecontactform_page_title."\r\n";
$body .= 'Page Url: '.$creativecontactform_referrer."\r\n";
}
if($form_data['email_info_show_ip'] == 1)
$body .= 'IP Address: '.$creativecontactform_ip."\r\n";
if($form_data['email_info_show_browser'] == 1)
$body .= 'Browser: '.$creativecontactform_browser."\r\n";
if($form_data['email_info_show_os'] == 1)
$body .= 'Operating System: '.$creativecontactform_operating_system."\r\n";
if($form_data['email_info_show_sc_res'] == 1)
$body .= 'Screen Resolution: '.$creativecontactform_sc_res."\r\n";
}
// 4.6.1 added
// class to replace email variables
class TemplateReplaceField {
private $fields_array;
function __construct($fields_array) {
$this->fields_array = $fields_array;
}
public function TemplateReplaceLabel($matches) {
return $this->fields_array[$matches[1]][0];
}
public function TemplateReplaceValue($matches) {
return $this->fields_array[$matches[1]][1];
}
}
$replace_var_callback = new TemplateReplaceField($fields_array);
//replace subject
$creativecontactform_subject = preg_replace_callback('/\[FIELD_LABEL:(\d+)\]/', array($replace_var_callback, 'TemplateReplaceLabel'), $creativecontactform_subject);
$creativecontactform_subject = preg_replace_callback('/\[FIELD_VALUE:(\d+)\]/', array($replace_var_callback, 'TemplateReplaceValue'), $creativecontactform_subject);
if($form_ishtml) {
$mail->IsHTML(true);
$email_template = preg_replace_callback('/\[FIELD_LABEL:(\d+)\]/', array($replace_var_callback, 'TemplateReplaceLabel'), $email_template);
$email_template = preg_replace_callback('/\[FIELD_VALUE:(\d+)\]/', array($replace_var_callback, 'TemplateReplaceValue'), $email_template);
// replace email variables
$email_template = str_replace('[SITENAME]', $config->get('sitename'), $email_template);
$email_template = str_replace('[FORMNAME]', $form_name, $email_template);
$email_template = str_replace('[NAME]', $user_name, $email_template);
$email_template = str_replace('[EMAIL]', $user_email, $email_template);
$email_template = str_replace('[TITLE]', $creativecontactform_page_title, $email_template);
$email_template = str_replace('[URL]', $creativecontactform_referrer, $email_template);
$email_template = str_replace('[IP]', $creativecontactform_ip, $email_template);
$email_template = str_replace('[BROWSER]', $creativecontactform_browser, $email_template);
$email_template = str_replace('[OS]', $creativecontactform_operating_system, $email_template);
$email_template = str_replace('[SCREEN_RESOLUTION]', $creativecontactform_sc_res, $email_template);
$email_template = str_replace('[SUBJECT]', $creativecontactform_subject, $email_template);
$email_template = str_replace('[FIELDS]', nl2br($body_raw), $email_template);
$body = trim($body);
$body = str_replace('[EMAILBODY]', $body, $email_template);
$body = preg_replace('/<tr>\s+<tr>/', '<tr>', $body);
$body = preg_replace('/<\/tr>\s+<\/tr>/', '</tr>', $body);
}
// echo $body;
//Set the body
$mail->setBody( $body );
$info[] = 'Body set successfully!';
//Set subject
$mail->setSubject( $creativecontactform_subject );
$info[] = 'Subject set successfully!';
//send me a copy check
if(isset($_POST['creativecontactform_send_copy_enable'])) {
if((int) $_POST['creativecontactform_send_copy_enable'] == 1 && $user_email != '') {
if(is_array($email_to)) {
$email_to[] = $user_email;
}
else {
$email_to_final = array($email_to, $user_email);
$email_to = $email_to_final;
}
}
}
// v4.5.0
$multiple_recipients_count = sizeof($multiple_recipients);
if($multiple_recipients_count > 0) {
$multiple_recipients_final = array();
foreach($multiple_recipients as $k => $v) {
$v = str_replace(array("\r", "\n"), '', $v);
$v_d = explode(',', $v);
if(sizeof($v_d) == 0)
$multiple_recipients_final[] = $v_d;
else {
foreach ($v_d as $k1 => $v_v) {
$multiple_recipients_final[] = $v_v;
}
}
}
if(isset($_POST['creativecontactform_send_copy_enable'])) {
if((int) $_POST['creativecontactform_send_copy_enable'] == 1 && $user_email != '') {
$multiple_recipients_final[] = $user_email;
}
}
$email_to = $multiple_recipients_final;
}
//Set Recipient
$mail->addRecipient( $email_to );
//$info[] = 'Recipient set: '.$email_to;
//Set Sender
$sender_email = trim($form_data['email_from']);
if($sender_email != '') {
// custom sender
$sender_email = preg_replace_callback('/\[FIELD_VALUE:(\d+)\]/', array($replace_var_callback, 'TemplateReplaceValue'), $sender_email);
}
$sender_email = $sender_email == '' ? ($mailfrom == '' ? $user_email : $mailfrom) : $sender_email;
$sender_name = trim($form_data['email_from_name']);
if($sender_name != '') {
// custom sender name
$sender_name = preg_replace_callback('/\[FIELD_LABEL:(\d+)\]/', array($replace_var_callback, 'TemplateReplaceLabel'), $sender_name);
$sender_name = preg_replace_callback('/\[FIELD_VALUE:(\d+)\]/', array($replace_var_callback, 'TemplateReplaceValue'), $sender_name);
}
$sender_name = $sender_name == '' ? ($fromname == '' ? $user_name : $fromname) : $sender_name;
$mail->setSender( array( $sender_email, $sender_name ) );
$info[] = 'Sender set successfully!';
// set reply to
$replyto_email = $form_data['email_replyto'] == '' ? ($user_email == '' ? $mailfrom : $user_email) : $form_data['email_replyto'];
$mail->ClearReplyTos();
$email_replyto_name = $form_data['email_replyto_name'] == '' ? ($user_name == '' ? $fromname : $user_name) : $form_data['email_replyto_name'];
$mail->addReplyTo($replyto_email,$email_replyto_name);
$info[] = 'Reply to set successfully!';
// add blind carbon recipient
if ($form_data['email_bcc'] != '') {
$email_bcc = explode(',', $form_data['email_bcc']);
$mail->addBCC($email_bcc);
$info[] = 'BCC recipients added successfully!';
}
//attach files
$attach_files = array();
if($esignature_path != '') {
$attach_files[] = $esignature_path;
}
if(isset($_POST['creativecontactform_upload'])) {
if(is_array($_POST['creativecontactform_upload'])) {
foreach($_POST['creativecontactform_upload'] as $file) {
// echo $file.'--';
$file_path = JPATH_BASE . '/components/com_creativecontactform/views/creativeupload/files/'.$file;
$attach_files[] = $file_path;
}
}
}
if(sizeof($attach_files) > 0) {
$mail->addAttachment($attach_files);
$sub_uploads = implode('~', $attach_files);
$sub_uploads = str_replace(JPATH_BASE, '', $sub_uploads);
}
// send email///////////////////////////////////////////////////////////////////////////
$mailer_res = $mail->Send();
if ($mailer_res === true ) {
JSession::getFormToken(true);
$info[] = 'Email sent successful';
}
else $info[] = 'There are problems sending email';
// auto-reply//////////////////////////////////////////////////////////////////////////
if($auto_reply != '' && $user_email != '') {
$mail->IsHTML(false);
$mail->ClearReplyTos();
$mail->clearAllRecipients();
$mail->clearAttachments();
$mail->addRecipient( $user_email );
$mail->setSender( array( $sender_email, $sender_name ) );
$mail->setSubject( $creativecontactform_subject );
$replyto_email = $form_data['email_replyto'] == '' ? ($mailfrom == '' ? $user_email : $mailfrom) : $form_data['email_replyto'];
$email_replyto_name = $form_data['email_replyto_name'] == '' ? ($fromname == '' ? $user_name : $fromname) : $form_data['email_replyto_name'];
$mail->addReplyTo($replyto_email,$email_replyto_name);
// generate body
$auto_reply = preg_replace_callback('/\[FIELD_LABEL:(\d+)\]/', array($replace_var_callback, 'TemplateReplaceLabel'), $auto_reply);
$auto_reply = preg_replace_callback('/\[FIELD_VALUE:(\d+)\]/', array($replace_var_callback, 'TemplateReplaceValue'), $auto_reply);
// replace email variables
$auto_reply = str_replace('[SITENAME]', $config->get('sitename'), $auto_reply);
$auto_reply = str_replace('[FORMNAME]', $form_name, $auto_reply);
$auto_reply = str_replace('[NAME]', $user_name, $auto_reply);
$auto_reply = str_replace('[EMAIL]', $user_email, $auto_reply);
$auto_reply = str_replace('[TITLE]', $creativecontactform_page_title, $auto_reply);
$auto_reply = str_replace('[URL]', $creativecontactform_referrer, $auto_reply);
$auto_reply = str_replace('[IP]', $creativecontactform_ip, $auto_reply);
$auto_reply = str_replace('[BROWSER]', $creativecontactform_browser, $auto_reply);
$auto_reply = str_replace('[OS]', $creativecontactform_operating_system, $auto_reply);
$auto_reply = str_replace('[SCREEN_RESOLUTION]', $creativecontactform_sc_res, $auto_reply);
$auto_reply = str_replace('[SUBJECT]', $creativecontactform_subject, $auto_reply);
$auto_reply = str_replace('[FIELDS]', $body_raw, $auto_reply);
// echo $auto_reply;
$mail->setBody( $auto_reply );
if($mail->Send() !== true) {
$info[] = 'There are problems sending auto-reply';
}
else {
$info[] = 'Auto-Reply email sent successful';
}
}
//generates json output///////////////////////////////////////////////////////////////
// if (false) {
if ($mailer_res !== true) {
echo '[{"invalid":"problem_sending_email"}]';
}
else {
echo '[{';
if(sizeof($info) > 0) {
echo '"info": ';
echo '[';
foreach ($info as $k => $data) {
echo '"'.$data.'"';
if ($k != sizeof($info) - 1)
echo ',';
}
echo ']';
}
echo '}]';
}
// write into database ////////////////////////////////////////////////////////////////////////////////////////
$sub_country = '';
$sub_city = '';
$sub_name = $db->quote($sub_name);
$sub_email = $db->quote($sub_email);
$sub_message = $db->quote($sub_message);
$sub_id_form = $db->quote($form_id);
$sub_ip = $db->quote($creativecontactform_ip);
$sub_browser = $db->quote($creativecontactform_browser);
$sub_op_s = $db->quote($creativecontactform_operating_system);
$sub_sc_res = $db->quote($creativecontactform_sc_res);
$sub_page_title = $db->quote($creativecontactform_page_title);
$sub_page_url = $db->quote($creativecontactform_referrer);
$sub_country = $db->quote($sub_country);
$sub_city = $db->quote($sub_city);
$sub_uploads = $db->quote($sub_uploads);
$query =
"
INSERT INTO
`#__creative_submissions` (`id`,`id_form`,`name`,`email`,`message`,`ip`,`browser`,`op_s`,`sc_res`,`date`,`country`,`city`,`page_title`,`page_url`,`star_index`,`imp_index`,`uploads`)
VALUES
(
'NULL',
".$sub_id_form.",
".$sub_name.",
".$sub_email.",
".$sub_message.",
".$sub_ip.",
".$sub_browser.",
".$sub_op_s.",
".$sub_sc_res.",
NOW(),
".$sub_country.",
".$sub_city.",
".$sub_page_title.",
".$sub_page_url.",
'0',
'0',
".$sub_uploads."
)
";
$db->setQuery($query);
$db->query();
}
}
else {
echo JSession::getFormToken();
}
jexit();