Blame view

components/com_creativecontactform/helpers/mailer.php 7.66 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
<?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);


$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
$query = "
			SELECT
				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'];


JResponse::allowCache( false );
JResponse::setHeader( 'Content-Type', 'text/plain' );

if($get_token == 0) {
	if (!JRequest::checkToken() && $check_token == 0) {
		echo '[{"invalid":"invalid_token"}]';
	}
	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();
		
		//generate the body
		$body = '';
		$user_email = '';
		$user_name = '';
		if(isset($_POST['creativecontactform_fields'])) {
			foreach($_POST['creativecontactform_fields'] as $field_data) {

				$field_label = strip_tags(trim($field_data[1]));
				$field_type = strip_tags(trim($field_data[2]));

				if(isset($field_data[0])) {
					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);

				// start separator
				if($field_type == 'text-area')
					$fields_seperator = ":\n";
				else
					$fields_seperator = ": ";

				// ens separator
				if($field_type == 'text-area')
					$fields_end_seperator = "\r\n\n";
				else
					$fields_end_seperator = "\r\n";

				$body .= $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);
		$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_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";
		
		//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;
				}
			}
		}
		
		//Set Recipient
		$mail->addRecipient( $email_to );
		//$info[] = 'Recipient set: '.$email_to;
		
		//Set Sender
		$sender_email = $form_data['email_from'] == '' ? ($mailfrom == '' ? $user_email : $mailfrom) : $form_data['email_from'];
		$sender_name = $form_data['email_from_name'] == '' ? ($fromname == '' ? $user_name : $fromname) : $form_data['email_from_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(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';

		//generates json output///////////////////////////////////////////////////////////////
		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 '}]';
		}

	}
}
else {
	echo JSession::getFormToken();
}
jexit();