<?php 
/**
* @version		$Id: contact.plugin.php 2689 2025-08-01 18:28:13Z IOS $
* @package		Elxis
* @subpackage	Content Plugins / Contact
* @copyright	Copyright (c) 2006-2025 Elxis CMS (https://www.elxis.org). All rights reserved.
* @license		Elxis Public License ( https://www.elxis.org/elxis-public-license.html )
* @author		Elxis Team ( https://www.elxis.org )
* @description 	Elxis CMS is free software. Read the license for copyright notices and details
*/

defined('_ELXIS_') or die ('Direct access to this location is not allowed');


class contactPlugin implements contentPlugin {


	/********************/
	/* MAGIC CONTRUCTOR */
	/********************/
	public function __construct() {
	}


	/***********************************/
	/* EXECUTE PLUGIN ON THE GIVEN ROW */
	/***********************************/
	public function process(&$row, $published, $params) {
    	$regex = "#{contact}(.*?){/contact}#s";
    	if (!$published) {
    		$row->text = preg_replace($regex, '', $row->text);
    		return true;
    	}

		preg_match_all($regex, $row->text, $matches, PREG_PATTERN_ORDER);
		if (!$matches) { return true; }

		$eURI = eFactory::getURI();
    	$proceed = false;
    	if ($eURI->getComponent() == 'content') {
    		if (!$eURI->isDir()) {
    			$proceed = true;
				$action = $eURI->getRealUriString();
				$parts = preg_split('#\?#', $action, 2);
				$action = $parts[0];
				unset($parts);
			}
   		}

		if (!$proceed) {
    		$row->text = preg_replace($regex, '', $row->text);
    		return true;
		}
		unset($proceed);

		$cfg = array();
		$stdfields = array('phone', 'mobile', 'address', 'city', 'postalcode', 'country');
		foreach ($stdfields as $stdfield) {
			$rfield = 'req_'.$stdfield;
			$cfg[$stdfield] = (int)$params->get($stdfield, 1);
			$cfg[$rfield] = (int)$params->get($rfield, 0);
		}
		$cfg['website'] = (int)$params->get('website', 0);
		$cfg['req_website'] = (int)$params->get('req_website', 0);
		unset($stdfields);

		$cfg['field1'] = eUTF::trim($params->getML('field1', ''));
		$cfg['req_field1'] = (int)$params->get('req_field1', 0);
		$cfg['field2'] = eUTF::trim($params->getML('field2', ''));
		$cfg['req_field2'] = (int)$params->get('req_field2', 0);
		$cfg['field3'] = eUTF::trim($params->getML('field3', ''));
		$cfg['req_field3'] = (int)$params->get('req_field3', 0);
		$cfg['field4'] = eUTF::trim($params->getML('field4', ''));
		$cfg['field4_options'] = array();
		$cfg['field5'] = eUTF::trim($params->getML('field5', ''));
		$cfg['field5_options'] = array();
		$cfg['field6'] = eUTF::trim($params->getML('field6', ''));
		$cfg['field6_options'] = array();
		for ($i=4; $i < 7; $i++) {
			$idx_field = 'field'.$i;
			if ($cfg[$idx_field] != '') {
				$n = 0;
				$idx_option = 'field'.$i.'_options';
				for ($k=1; $k < 6; $k++) {
					$idx = 'option'.$i.$k;
					$opt = eUTF::trim($params->getML($idx, ''));
					if ($opt != '') {
						$cfg[$idx_option][] = $opt;
						$n++;
					}
				}
				if ($n == 0) {
					$cfg[$idx_field] = '';
					$cfg[$idx_option] = array();
				}
			}
		}
		//other config options
		$cfg['emailcolour'] = (int)$params->get('colour', 0);
		$cfg['emailfacebook'] = trim($params->get('facebook', ''));
		$cfg['emailtwitter'] = trim($params->get('twitter', ''));
		$cfg['emailinstagram'] = trim($params->get('instagram', ''));
		$cfg['emailyoutube'] = trim($params->get('youtube', ''));

		$proc = false;
		foreach ($matches[0] as $i => $match) {//only the first match will be processed!
			if ($proc == true) {
			    $row->text = str_replace($match, '', $row->text);
				continue;
			}
			$rcptmail = $matches[1][$i];
			if (!filter_var($rcptmail, FILTER_VALIDATE_EMAIL)) {
			    $row->text = str_replace($match, '', $row->text);
				continue;
			}

			$response = '';
			if (isset($_POST['firstname'])) {
				$response = $this->processRequest($cfg, $rcptmail, $row->title, $action);
			}

			$html = $this->makeForm($row->id, $cfg, $action);
			$html = $response.$html;
			$row->text = preg_replace("#".$match."#", $html, $row->text);
			$proc = true;
		}
		return true;
	}


	/************************/
	/* GENERIC SYNTAX STYLE */
	/************************/
	public function syntax() {
		return '{contact}recipient_email_address{/contact}';
	}


	/***********************/
	/* LIST OF HELPER TABS */
	/***********************/
	public function tabs() {
		return array();
	}


	/*****************/
	/* PLUGIN HELPER */
	/*****************/
	public function helper($pluginid, $tabidx, $fn) {
		if ($tabidx <> 1) { return; }

		$eLang = eFactory::getLang();
		$defmail = eFactory::getElxis()->getConfig('MAIL_EMAIL');

		echo '<div class="elx5_sideinput_wrap">';
		echo '<div class="elx5_sideinput_value_end elx5_spad">';
		echo '<a href="#" id="plugconaddlink" class="elx5_btn elx5_ibtn" title="'.$eLang->get('ADD').'"><i class="fas fa-location-arrow"></i></a>';
		echo '</div>';
		echo '<div class="elx5_sideinput_input_front elx5_spad">';
		echo '<label class="elx5_label" for="contact_email">'.$eLang->get('RCPT_EMAIL').'</label>';
		echo '<div class="elx5_labelside">';
		echo '<input type="text" name="contact_email" value="'.$defmail.'" id="contact_email" class="elx5_text" dir="ltr" placeholder="'.$eLang->get('RCPT_EMAIL').'">';
		echo "</div></div></div>\n";
	}


	/***************************************************/
	/* RETURN REQUIRED CSS AND JS FILES FOR THE HELPER */
	/***************************************************/
	public function head() {
		$elxis = eFactory::getElxis();

		$response = array(
			'js' => array($elxis->secureBase().'/components/com_content/plugins/contact/includes/contact.js'),
			'css' => array()
		);

		return $response;
	}


	/*******************************/
	/* PLUGIN SPECIAL TASK HANDLER */
	/*******************************/
	public function handler($pluginid, $fn) {
		$elxis = eFactory::getElxis();
		$url = $elxis->makeAURL('content:plugin/', 'inner.php').'?id='.$pluginid.'&fn='.$fn;
		$elxis->redirect($url);
	}


	/***********************/
	/* MAKE FORM HTML CODE */
	/***********************/
	private function makeForm($id, $cfg, $action) {
		$elxis = eFactory::getElxis();
		$eLang = eFactory::getLang();

		$pfx = 'art'.$id;
		if ($eLang->getinfo('DIR') == 'rtl') {
			$dir = 'rtl';
		} else {
			$dir = 'ltr';
		}

		$token = md5(uniqid(rand(), true));
		eFactory::getSession()->set('token_fmcont', $token);

		$jslink = $elxis->secureBase().'/components/com_content/plugins/contact/includes/contact.js';
		eFactory::getDocument()->addScriptLink($jslink);

		eFactory::getDocument()->addNativeDocReady('plgContactOnLoad(\''.$pfx.'\');');

		elxisLoader::loadFile('includes/libraries/elxis/form5.class.php');

		$form = new elxis5Form(array('idprefix' => $pfx, 'labelclass' => 'elx5_label', 'sideclass' => 'elx5_labelside', 'returnhtml' => true));
		$out = $form->openForm(array('name' => 'contactform', 'method' => 'post', 'action' => $action, 'id' => $pfx.'contactform'));

		$out .= $form->openFieldset($eLang->get('CONTACT'));
		$out .= $form->addText('firstname', $elxis->user()->firstname, $eLang->get('FIRSTNAME').'*', array('dir' => $dir, 'maxlength' => 60, 'required' => 'required'));
		$out .= $form->addText('lastname', $elxis->user()->lastname, $eLang->get('LASTNAME').'*', array('dir' => $dir, 'maxlength' => 60, 'required' => 'required'));
		if ($cfg['address'] == 1) {
			$reqmark = '';
			$attrs = array('maxlength' => 120, 'dir' => $dir);
			if ($cfg['req_address'] == 1) { $reqmark = '*'; $attrs['required'] = 'required'; }
			$out .= $form->addText('address', $elxis->user()->address, $eLang->get('ADDRESS').$reqmark, $attrs);
		}
		if ($cfg['city'] == 1) {
			$reqmark = '';
			$attrs = array('maxlength' => 120, 'dir' => $dir);
			if ($cfg['req_address'] == 1) { $reqmark = '*'; $attrs['required'] = 'required'; }
			$out .= $form->addText('city', $elxis->user()->city, $eLang->get('CITY').$reqmark, $attrs);
		}
		if ($cfg['postalcode'] == 1) {
			$reqmark = '';
			$attrs = array('maxlength' => 10, 'dir' => 'ltr', 'class' => 'elx5_text elx5_minitext');
			if ($cfg['req_address'] == 1) { $reqmark = '*'; $attrs['required'] = 'required'; }
			$out .= $form->addText('postalcode', $elxis->user()->postalcode, $eLang->get('POSTAL_CODE').$reqmark, $attrs);
		}
		if ($cfg['country'] == 1) {
			$reqmark = '';
			if ($cfg['req_country'] == 1) { $reqmark = '*'; }
			$vcountry = $elxis->user()->country;
			if ($vcountry == '') { $vcountry = $this->visitorCountry(); }
			if ($vcountry != '') {
				$sel = $vcountry;
			} else {
				$sel = 'US';
			}
			$out .= $form->addCountry('country', $eLang->get('COUNTRY').$reqmark, $sel, array('dir' => 'rtl'));
		}

		if ($cfg['phone'] == 1) {
			$reqmark = '';
			$attrs = array('maxlength' => 40, 'dir' => 'ltr', 'pattern' => '[0-9\+\-\s]{6,}');
			if ($cfg['req_phone'] == 1) { $reqmark = '*'; $attrs['required'] = 'required'; }
			$out .= $form->addTel('phone', $elxis->user()->phone, $eLang->get('TELEPHONE').$reqmark, $attrs);
		}

		if ($cfg['mobile'] == 1) {
			$reqmark = '';
			$attrs = array('maxlength' => 40, 'dir' => 'ltr', 'pattern' => '[0-9\+\-\s]{6,}');
			if ($cfg['req_mobile'] == 1) { $reqmark = '*'; $attrs['required'] = 'required'; }
			$out .= $form->addTel('mobile', $elxis->user()->mobile, $eLang->get('MOBILE').$reqmark, $attrs);
		}

		$out .= $form->addEmail('email', $elxis->user()->email, $eLang->get('EMAIL').'*', array('required' => 'required',  'dir' => 'ltr'));

		if ($cfg['website'] == 1) {
			$reqmark = '';
			$attrs = array('maxlength' => 120, 'dir' => 'ltr');
			if ($cfg['req_website'] == 1) { $reqmark = '*'; $attrs['required'] = 'required'; }
			$out .= $form->addURL('website', $elxis->user()->website, $eLang->get('WEBSITE').$reqmark, $attrs);
		}

		for ($i=1; $i < 4; $i++) {
			$idx = 'field'.$i;
			if ($cfg[$idx] == '') { continue; }
			$idx_req = 'req_field'.$i;
			$reqmark = '';
			$attrs = array('dir' => $dir);
			if ($cfg[$idx_req] == 1) { $reqmark = '*'; $attrs['required'] = 'required'; }
			$out .= $form->addText('field'.$i, '', $cfg[$idx].$reqmark, $attrs);
		}
		for ($i=4; $i < 7; $i++) {
			$idx = 'field'.$i;
			if ($cfg[$idx] == '') { continue; }
			$idx_options = 'field'.$i.'_options';
			$sel = '';
			$options = array();
			foreach ($cfg[$idx_options] as $k => $option) {
				if ($k == 0) { $sel = $option; }
				$options[] = $form->makeOption($option, $option);
			}
			$out .= $form->addSelect('field'.$i, $cfg[$idx], $sel, $options);
		}

		$out .= $form->addTextarea('message', '', $eLang->get('MESSAGE').'*', array('dir' => $dir, 'required' => 'required'));
		$out .= $form->addHidden('token', $token);

		if ($elxis->getConfig('CAPTCHA') != 'NONE') {
			if ($elxis->getConfig('CAPTCHA') == 'MATH') {
				$out .= $form->addCaptcha('conseccode', $eLang->get('SECURITY_CODE').'*');
			} else {
				$out .= $form->addNoRobot('norobot');
			}
		}

		$attrs = array('tip' => $eLang->get('FIELDSASTERREQ'), 'class' => 'elx5_btn elx5_sucbtn', 'sidepad' => 1);
		$out .= $form->addHTML('<div class="elx5_vlspace">');
		$out .= $form->addButton('sbmcontact', $eLang->get('SUBMIT'), 'submit', $attrs);
		$out .= $form->addHTML('</div>');

		$out .= $form->closeFieldset();
		$out .= $form->closeForm();

		return $out;
	}


	/**************************/
	/* DETECT VISITOR COUNTRY */
	/**************************/
	private function visitorCountry() {
		$eLang = eFactory::getLang();

		$region = $eLang->getinfo('REGION');

		if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { return $region; }
		$acc_langs_str = trim($_SERVER['HTTP_ACCEPT_LANGUAGE']);
		if ($acc_langs_str == '') { return $region; }
		$langs = array();
		$acc_langs = explode(',', trim($acc_langs_str));
		foreach ($acc_langs as $acc_lang) {
			if (preg_match('/(\*|[a-zA-Z0-9]{1,8}(?:-[a-zA-Z0-9]{1,8})*)(?:\s*;\s*q\s*=\s*(0(?:\.\d{0,3})|1(?:\.0{0,3})))?/', trim($acc_lang), $match)) {
				$q = (!isset($match[2])) ? '1.0' : (string)floatval($match[2]);
				$m = trim(strtolower($match[1]));
				$n = strpos($m, '-');
				if ($n !== false) { $m = substr($m, 0, $n); }
				if (!isset($langs[$q])) { $langs[$q] = array(); }
				$langs[$q][] = $m;
			}
		}
		if (!$langs) { return $region; }
		krsort($langs);
		$final_lang = '';
		foreach ($langs as $k => $lngs) {
			$final_lang = $lngs[0];
			break;
		}
		if ($final_lang == '') { return $region; }
		if ($final_lang == 'en') { return $region; }
		if ($final_lang == 'fr') { return 'FR'; }
		if ($final_lang == 'es') { return 'ES'; }
		if ($final_lang == 'el') { return 'GR'; }
		if ($final_lang == 'de') { return 'DE'; }
		if ($final_lang == 'ru') { return 'RU'; }
		if ($final_lang == 'it') { return 'IT'; }
		if ($final_lang == 'pt') { return 'PT'; }
		if ($final_lang == 'pl') { return 'PL'; }
		if ($final_lang == 'tr') { return 'TR'; }
		if ($final_lang == 'ro') { return 'RO'; }

		include(ELXIS_PATH.'/includes/libraries/elxis/language/langdb.php');
		foreach ($langdb as $elxis_lang => $data) {
			if ($data['LANGUAGE'] == $final_lang) {
				$region = $data['REGION'];
				break;
			}
		}

		return $region;
	}


	/******************************/
	/* ALIGN 2 STRINGS IN COLUMNS */
	/******************************/
	private function alignPlainText($left_string, $right_string, $left_width=25) {
		$spaces = $left_width - eUTF::strlen($left_string);
		$text = $left_string.': ';
		if ($spaces > 0) { $text .= str_repeat(' ', $spaces); }
		$text .= $right_string;
		return $text;
	}


	/***************************/
	/* PROCESS FORM SUBMISSION */
	/***************************/
	private function processRequest($cfg, $rcptemail, $pagetitle, $pageurl) {
		$eLang = eFactory::getLang();
		$elxis = eFactory::getElxis();

		$sess_token = trim(eFactory::getSession()->get('token_fmcont'));
		$token = trim($elxis->sanitizeInput('token', INPUT_POST, true, false));
		if (($token == '') || ($sess_token == '') || ($sess_token != $token)) {
			return '<div class="elx5_warning"><strong>'.$eLang->get('ERROR').':</strong> '.$eLang->get('REQDROPPEDSEC')."</div>\n";
		}

		$captcha = $elxis->obj('captcha');
		$ok = $captcha->validate($elxis->getConfig('CAPTCHA'), 'captcha_conseccode', 'conseccode', 'norobot', '');
		if (!$ok) {
			return '<div class="elx5_warning"><strong>'.$eLang->get('ERROR').':</strong> '.$captcha->getError()."</div>\n";
		}
		unset($captcha);

		switch ($cfg['emailcolour']) {
			case 1: $eml_colours = array('colour' => '#3285BC', 'bgcolour' => '#BAD5E2'); break;
			case 2: $eml_colours = array('colour' => '#719E00', 'bgcolour' => '#EEF7E1'); break;
			case 3: $eml_colours = array('colour' => '#F76E11', 'bgcolour' => '#FFE19E'); break;
			case 4: $eml_colours = array('colour' => '#FF3884', 'bgcolour' => '#FFEAF2'); break;
			case 5: $eml_colours = array('colour' => '#777777', 'bgcolour' => '#DFDFDE'); break;
			case 0: default: $eml_colours = array('colour' => '#2190E3', 'bgcolour' => '#D6E7F0'); break;
		}

		$html_head = $this->emailHTMLHead($elxis, $eLang->get('CONTACT'), $eml_colours);
		$html_footer = $this->emailHTMLFooter($elxis, $cfg);
		$html_content = '';
		$text = '';

		$val = '<br>'.$eLang->get('HI').'. '.$eLang->get('CFORM_SUBMIT').'<br>'.$eLang->get('INFO_FOLLOW');
		$html_content .= $this->emailHTMLParagraph($val);

		$val = '<a href="'.$pageurl.'" style="color:'.$eml_colours['colour'].';text-decoration:none;border:none;">'.$pagetitle.'</a>';
		$html_content .= $this->emailHTMLItem($eLang->get('PAGE'), $val);

		$html_content .= $this->emailHTMLBlockTitle($eLang->get('CST_CONTACT_DETAILS'), $eml_colours);

		$firstname = $elxis->sanitizeInput('firstname', INPUT_POST, true, false);
		if (trim($firstname) == '') {
			$ltext = sprintf($eLang->get('FIELDNOEMPTY'), $eLang->get('FIRSTNAME'));
			return '<div class="elx5_warning"><strong>'.$eLang->get('ERROR').':</strong> '.$ltext."</div>\n";
		}
		$html_content .= $this->emailHTMLItem($eLang->get('FIRSTNAME'), $firstname);
		$text .= $this->alignPlainText($eLang->get('FIRSTNAME'), $firstname)."\n";

		$lastname = $elxis->sanitizeInput('lastname', INPUT_POST, true, false);
		if (trim($lastname) == '') {
			$ltext = sprintf($eLang->get('FIELDNOEMPTY'), $eLang->get('LASTNAME'));
			return '<div class="elx5_warning"><strong>'.$eLang->get('ERROR').':</strong> '.$ltext."</div>\n";
		}
		$html_content .= $this->emailHTMLItem($eLang->get('LASTNAME'), $lastname);
		$text .= $this->alignPlainText($eLang->get('LASTNAME'), $lastname)."\n";

		if ($cfg['address'] == 1) {
			$val = $elxis->sanitizeInput('address', INPUT_POST, true, false);
			if ($cfg['req_address'] == 1) {
				if (trim($val) == '') {
					$ltext = sprintf($eLang->get('FIELDNOEMPTY'), $eLang->get('ADDRESS'));
					return '<div class="elx5_warning"><strong>'.$eLang->get('ERROR').':</strong> '.$ltext."</div>\n";
				}
			}
			if ($val != '') {
				$html_content .= $this->emailHTMLItem($eLang->get('ADDRESS'), $val);
				$text .= $this->alignPlainText($eLang->get('ADDRESS'), $val)."\n";
			}
		}

		if ($cfg['city'] == 1) {
			$val = $elxis->sanitizeInput('city', INPUT_POST, true, false);
			if ($cfg['req_city'] == 1) {
				if (trim($val) == '') {
					$ltext = sprintf($eLang->get('FIELDNOEMPTY'), $eLang->get('CITY'));
					return '<div class="elx5_warning"><strong>'.$eLang->get('ERROR').':</strong> '.$ltext."</div>\n";
				}
			}
			if ($val != '') {
				$html_content .= $this->emailHTMLItem($eLang->get('CITY'), $val);
				$text .= $this->alignPlainText($eLang->get('CITY'), $val)."\n";
			}
		}

		if ($cfg['postalcode'] == 1) {
			$val = trim($elxis->sanitizeInput('postalcode', INPUT_POST));
			if ($cfg['req_postalcode'] == 1) {
				if ($val == '') {
					$ltext = sprintf($eLang->get('FIELDNOEMPTY'), $eLang->get('POSTAL_CODE'));
					return '<div class="elx5_warning"><strong>'.$eLang->get('ERROR').':</strong> '.$ltext."</div>\n";
				}
			}
			if ($val != '') {
				$html_content .= $this->emailHTMLItem($eLang->get('POSTAL_CODE'), $val);
				$text .= $this->alignPlainText($eLang->get('POSTAL_CODE'), $val)."\n";
			}
		}

		if ($cfg['country'] == 1) {
			$val = $elxis->sanitizeInput('country', INPUT_POST, true, false);
			if ($val != '') {
				$lng = $eLang->getinfo('LANGUAGE');
				if (file_exists(ELXIS_PATH.'/includes/libraries/elxis/form/countries.'.$lng.'.php')) {
					include(ELXIS_PATH.'/includes/libraries/elxis/form/countries.'.$lng.'.php');
				} else {
					include(ELXIS_PATH.'/includes/libraries/elxis/form/countries.en.php');
				}
				if (isset($countries[$val])) {
					$val = $countries[$val].' ('.$val.')';
				} else {
					$val = '';
				}
				unset($countries);
			}

			if ($cfg['req_country'] == 1) {
				if ($val == '') {
					$ltext = sprintf($eLang->get('FIELDNOEMPTY'), $eLang->get('COUNTRY'));
					return '<div class="elx5_warning"><strong>'.$eLang->get('ERROR').':</strong> '.$ltext."</div>\n";
				}
			}

			$html_content .= $this->emailHTMLItem($eLang->get('COUNTRY'), $val);
			$text .= $this->alignPlainText($eLang->get('COUNTRY'), $val)."\n";
		}

		if ($cfg['phone'] == 1) {
			$val = trim($elxis->sanitizeInput('phone', INPUT_POST));
			if ($cfg['req_phone'] == 1) {
				if ($val == '') {
					$ltext = sprintf($eLang->get('FIELDNOEMPTY'), $eLang->get('TELEPHONE'));
					return '<div class="elx5_warning"><strong>'.$eLang->get('ERROR').':</strong> '.$ltext."</div>\n";
				}
			}
			if ($val != '') {
				$html_content .= $this->emailHTMLItem($eLang->get('TELEPHONE'), $val);
				$text .= $this->alignPlainText($eLang->get('TELEPHONE'), $val)."\n";
			}
		}

		if ($cfg['mobile'] == 1) {
			$val = trim($elxis->sanitizeInput('mobile', INPUT_POST));
			if ($cfg['req_mobile'] == 1) {
				if ($val == '') {
					$ltext = sprintf($eLang->get('FIELDNOEMPTY'), $eLang->get('MOBILE'));
					return '<div class="elx5_warning"><strong>'.$eLang->get('ERROR').':</strong> '.$ltext."</div>\n";
				}
			}
			if ($val != '') {
				$html_content .= $this->emailHTMLItem($eLang->get('MOBILE'), $val);
				$text .= $this->alignPlainText($eLang->get('MOBILE'), $val)."\n";
			}
		}

		$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
		if (($email == '') || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
			return '<div class="elx5_warning"><strong>'.$eLang->get('ERROR').':</strong> '.$eLang->get('INVALIDEMAIL')."</div>\n";
		}
		$html_content .= $this->emailHTMLItem($eLang->get('EMAIL'), $email);
		$text .= $this->alignPlainText($eLang->get('EMAIL'), $email)."\n";

		if ($cfg['website'] == 1) {
			$val = filter_input(INPUT_POST, 'website', FILTER_SANITIZE_URL);
			if (!filter_var($val, FILTER_VALIDATE_URL)) { $val = ''; }
			if ($cfg['req_website'] == 1) {
				if ($val == '') {
					return '<div class="elx5_warning"><strong>'.$eLang->get('ERROR').':</strong> '.$eLang->get('INVALID_URL_ADDR')."</div>\n";
				}
			}
			if ($val != '') {
				$html_content .= $this->emailHTMLItem($eLang->get('WEBSITE'), $val);
				$text .= $this->alignPlainText($eLang->get('WEBSITE'), $val)."\n";
			}
		}

		for ($i=1; $i < 7; $i++) {
			$idx = 'field'.$i;
			if ($cfg[$idx] == '') { continue; }
			$val = $elxis->sanitizeInput('field'.$i, INPUT_POST, true, false);
			if ($i < 4) {
				$idx_req = 'req_field'.$i;
				if ($cfg[$idx_req] == 1) {
					if (trim($val) == '') {
						$ltext = sprintf($eLang->get('FIELDNOEMPTY'), $cfg[$idx]);
						return '<div class="elx5_warning"><strong>'.$eLang->get('ERROR').':</strong> '.$ltext."</div>\n";
					}
				}
			}
			if (trim($val) != '') {
				$html_content .= $this->emailHTMLItem($cfg[$idx], $val);
				$text .= $this->alignPlainText($cfg[$idx], $val)."\n";
			}
		}

		$ipaddr = $elxis->obj('ip')->clientIP(false, false);
		$html_content .= $this->emailHTMLItem($eLang->get('IP_ADDRESS'), $ipaddr);
		$text .= $this->alignPlainText($eLang->get('IP_ADDRESS'), $ipaddr)."\n\n";

		$val = $elxis->sanitizeInput('message', INPUT_POST, true, false);
		if (trim($val) == '') {
			return '<div class="elx5_warning"><strong>'.$eLang->get('ERROR').':</strong> '.$eLang->get('MUST_WRITE_MESSAGE')."</div>\n";
		}

		$hval = nl2br($val);
		$html_content .= $this->emailHTMLBlockTitle($eLang->get('MESSAGE'), $eml_colours);
		$html_content .= $this->emailHTMLParagraph($hval);

		$text .= $eLang->get('MESSAGE').":\n";
		$text .= $val."\n";

		$html_content .= '<tr><td style="padding:30px;text-align:center;background-color:#FFFFFF;">'."\r\n";
		$html_content .= '<p style="margin:0; font-size:16px;line-height:22px;text-align:center;">'.$eLang->get('REGARDS').'<br>';
		$html_content .= '<a href="'.$elxis->getConfig('URL').'" style="color:'.$eml_colours['colour'].';text-decoration:none;border:none;font-weight:bold;">'.$elxis->getConfig('SITENAME').'</a>';
		$html_content .= "</p>\r\n";
		$html_content .= "</td></tr>\r\n";

		$parsed = parse_url($elxis->getConfig('URL'));
		$subject = sprintf($eLang->get('CONTACT_INQ_FROM'), $parsed['host']);

		$html_body = $html_head.$html_content.$html_footer;

		$text_body = $eLang->get('HI')."\n";
		$text_body .= $eLang->get('CFORM_SUBMIT')."\n";
		$text_body .= $eLang->get('INFO_FOLLOW')."\n\n";
		$text_body .= $eLang->get('PAGE').': '.$pagetitle."\n";
		$text_body .= $pageurl."\n\n\n";
		$text_body .= $text."\n\n\n";
		$text_body .= $eLang->get('REGARDS')."\n";
		$text_body .= $elxis->getConfig('SITENAME')."\n";
		$text_body .= $elxis->getConfig('URL')."\n\n\n";
		$text_body .= "_______________________________________________________________\n";
		$text_body .= 'Sent by Elxis CMS running on '.$elxis->getConfig('URL');

		$replyto = $email.','.$firstname.' '.$lastname;
		$from = $elxis->getConfig('MAIL_FROM_EMAIL').','.$elxis->getConfig('MAIL_FROM_NAME');

		$ok = $elxis->sendmail($subject, $html_body, $text_body, null, 'html', $rcptemail, null, null, $from, 3, false, $replyto);
		if ($ok) {
			return '<div class="elx5_success">'.$eLang->get('MSG_SENT_REPLY_THANKS').'</div>';
		} else {
			return '<div class="elx5_error">'.$eLang->get('SORRY_SEND_FAILED').'</div>';
		}
	}


	/*******************/
	/* HTML EMAIL HEAD */
	/*******************/
	private function emailHTMLHead($elxis, $description, $eml_colours) {
		$html = '<!DOCTYPE html>'."\r\n";
		$html .= '<html lang="'.$elxis->getConfig('LANG').'" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml">'."\r\n";
		$html .= '<head>'."\r\n";
		$html .= '<title></title>'."\r\n";
		$html .= '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'."\r\n";
		$html .= '<meta name="viewport" content="width=device-width, initial-scale=1.0">'."\r\n";
		$html .= '<meta name="x-apple-disable-message-reformatting">'."\r\n";
		$html .= '<!--[if mso]>'."\r\n";
		$html .= '<style>table {border-collapse:collapse;border-spacing:0;border:none;margin:0;} div, td {padding:0;} div {margin:0 !important;}</style>'."\r\n";
		$html .= '<noscript><xml><o:OfficeDocumentSettings><o:PixelsPerInch>96</o:PixelsPerInch><o:AllowPNG/></o:OfficeDocumentSettings></xml></noscript>'."\r\n";
		$html .= '<![endif]-->'."\r\n";
		$html .= '<style>'."\r\n";
		$html .= 'table, td, div, h1, p { font-family: Arial, sans-serif; }'."\r\n";
		$html .= '@media screen and (max-width: 530px) {'."\r\n";
		$html .= '.col-lge { max-width: 100% !important; }'."\r\n";
		$html .= '}'."\r\n";
		$html .= '@media screen and (min-width: 531px) {'."\r\n";
		$html .= '.col-sml { max-width: 27% !important; }'."\r\n";
		$html .= '.col-lge { max-width: 73% !important; }'."\r\n";
		$html .= '}'."\r\n";
		$html .= '</style>'."\r\n";
		$html .= '</head>'."\r\n";
		$html .= '<body style="margin:0;padding:0;word-spacing:normal;background-color:#F5F5F5;">'."\r\n";
		$html .= '<div role="article" aria-roledescription="email" lang="en" style="text-size-adjust:100%;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%;background-color:#F5F5F5;">'."\r\n";
		$html .= '<br><br>';
		$html .= '<table role="presentation" style="width:100%;border:none;border-spacing:0;">'."\r\n";
		$html .= '<tr>'."\r\n";
		$html .= '<td align="center" style="padding:0;">'."\r\n";
		$html .= '<!--[if mso]><table role="presentation" align="center" style="width:720px;"><tr><td><![endif]-->'."\r\n";
		$html .= '<table role="presentation" style="width:94%;max-width:720px;border:none;border-spacing:0;text-align:left;font-family:Arial,sans-serif;font-size:16px;line-height:22px;color:#363636;">'."\r\n";

		$html .= '<tr>'."\r\n";
		$html .= '<td style="margin:0;padding:40px 30px;width:100%;text-align:center;font-size:32px;font-weight:bold;line-height:38px;background-color:'.$eml_colours['bgcolour'].';">'."\r\n";
		$html .= '<a href="'.$elxis->getConfig('URL').'" style="text-decoration:none;border:none;display:block;text-align:center;font-size:32px;line-height:38px;color:#272727;">';
		$parts = explode(' ', $elxis->getConfig('SITENAME'));
		$n = count($parts) - 1;
		foreach ($parts as $k => $part) {
			if ($k == 0) {
				$html .= '<span style="color:'.$eml_colours['colour'].';font-size:32px;">'.$part.'</span>';
			} else {
				$html .= $part;
			}
			if ($k < $n) { $html .= ' '; }
		}
		$html .= '</a>'."\r\n";
		$html .= '<p style="margin:0;font-size:14px;font-weight:normal;line-height:20px;color:#555555;">'.$description.'</p>'."\r\n";
		$html .= '</td>'."\r\n";
		$html .= '</tr>'."\r\n";

		return $html;
	}


	/*********************/
	/* HTML EMAIL FOOTER */
	/*********************/
	private function emailHTMLFooter($elxis, $cfg) {
		$sitename = $elxis->getConfig('SITENAME');
		$siteurl = $elxis->getConfig('URL');
		$securebase = $elxis->secureBase();
		$parsed = parse_url($elxis->getConfig('URL'));
		$hostname = str_replace('www.', '', $parsed['host']);

		$html = '<tr>'."\r\n";
		$html .= '<td style="padding:30px;text-align:center;font-size:12px;background-color:#404040;color:#cccccc;">'."\r\n";
		$html .= '<p style="margin:0 0 8px 0;">';
		if ($cfg['emailfacebook'] != '') {
			$html .= '<a href="'.$cfg['emailfacebook'].'" style="text-decoration:none;"><img src="'.$securebase.'/components/com_content/plugins/contact/includes/facebook.png" width="40" height="40" alt="f" style="display:inline-block;color:#cccccc;width:40px;height:40px;"></a> &nbsp; ';
		}
		if ($cfg['emailtwitter'] != '') {
			$html .= '<a href="'.$cfg['emailtwitter'].'" style="text-decoration:none;"><img src="'.$securebase.'/components/com_content/plugins/contact/includes/twitter.png" width="40" height="40" alt="t" style="display:inline-block;color:#cccccc;width:40px;height:40px;"></a> &nbsp; ';
		}
		if ($cfg['emailinstagram'] != '') {
			$html .= '<a href="'.$cfg['emailinstagram'].'" style="text-decoration:none;"><img src="'.$securebase.'/components/com_content/plugins/contact/includes/instagram.png" width="40" height="40" alt="i" style="display:inline-block;color:#cccccc;width:40px;height:40px;"></a> &nbsp; ';
		}
		if ($cfg['emailyoutube'] != '') {
			$html .= '<a href="'.$cfg['emailyoutube'].'" style="text-decoration:none;"><img src="'.$securebase.'/components/com_content/plugins/contact/includes/youtube.png" width="40" height="40" alt="y" style="display:inline-block;color:#cccccc;width:40px;height:40px;"></a> &nbsp; ';
		}
		$html .= '<a href="'.$siteurl.'" style="text-decoration:none;"><img src="'.$securebase.'/components/com_content/plugins/contact/includes/www.png" width="40" height="40" alt="w" style="display:inline-block;color:#cccccc;width:40px;height:40px;"></a>';
		$html .= '</p>'."\r\n";
		$html .= '<p style="margin:0;font-size:14px;line-height:20px;">Copyright &copy; '.gmdate('Y').' '.$sitename.'</p>'."\r\n";
		$html .= '<p style="margin-top:20px; margin-bottom: 0;font-size:12px;line-height:16px;text-align:center;color:#AAAAAA;">Sent by Elxis CMS running on '.$hostname.'</p>'."\r\n";
		$html .= '</td>'."\r\n";
		$html .= '</tr>'."\r\n";
		$html .= '</table>'."\r\n";
		$html .= '<!--[if mso]>'."\r\n";
		$html .= '</td>'."\r\n";
		$html .= '</tr>'."\r\n";
		$html .= '</table>'."\r\n";
		$html .= '<![endif]-->'."\r\n";
		$html .= '</td>'."\r\n";
		$html .= '</tr>'."\r\n";
		$html .= '</table>'."\r\n";
		$html .= '</div>'."\r\n";
		$html .= '</body>'."\r\n";
		$html .= '</html>'."\r\n";

		return $html;
	}


	/******************************/
	/* HTML EMAIL LABEL/VALUE ROW */
	/******************************/
	private function emailHTMLItem($label, $value) {
		$html = '<tr>'."\r\n";
		$html .= '<td style="padding:0 30px 5px 30px;font-size:0;background-color:#FFFFFF;">'."\r\n";
		$html .= '<!--[if mso]><table role="presentation" width="100%"><tr><td style="width:200px;" align="left" valign="top"><![endif]-->'."\r\n";
		$html .= '<div class="col-sml" style="display:inline-block;width:100%;max-width:200px;vertical-align:top;text-align:left;font-family:Arial,sans-serif;font-size:16px;color:#363636;">'."\r\n";
		$html .= '<strong>'.$label.'</strong>'."\r\n";
		$html .= '</div>'."\r\n";
		$html .= '<!--[if mso]></td><td style="width:450px;padding-bottom:5px;" valign="top"><![endif]-->'."\r\n";
		$html .= '<div class="col-lge" style="display:inline-block;width:100%;max-width:450px;vertical-align:top;padding-bottom:5px;font-family:Arial,sans-serif;font-size:16px;line-height:22px;color:#363636;">'."\r\n";
		$html .= $value."\r\n";
		$html .= '</div>'."\r\n";
		$html .= '<!--[if mso]></td></tr></table><![endif]-->'."\r\n";
		$html .= '</td>'."\r\n";
		$html .= '</tr>'."\r\n";

		return $html;
	}


	/****************************/
	/* HTML EMAIL PARAGRAPH ROW */
	/****************************/
	private function emailHTMLParagraph($text) {
		$html = '<tr>'."\r\n";
		$html .= '<td style="padding:0 30px 5px 30px;font-size:0;background-color:#FFFFFF;">'."\r\n";
		$html .= '<p style="font-family:Arial,sans-serif;font-size:16px;line-height:22px;color:#363636;">'.$text.'</p>'."\r\n";
		$html .= '</td>'."\r\n";
		$html .= '</tr>'."\r\n";

		return $html;
	}


	/**************************/
	/* HTML EMAIL BLOCK TITLE */
	/**************************/
	private function emailHTMLBlockTitle($title, $eml_colours) {
		$html = '<tr>'."\r\n";
		$html .= '<td style="padding:20px 30px 10px 30px;background-color:#FFFFFF;">'."\r\n";
		$html .= '<h2 style="margin:0;font-size:20px;line-height:28px;font-weight:bold;letter-spacing:0;color:'.$eml_colours['colour'].';">'.$title.'</h2>'."\r\n";
		$html .= '</td>'."\r\n";
		$html .= '</tr>'."\r\n";

		return $html;
	}

}

?>