<?php
///////////////////////////////////////////////////////////////////////////////////////////
///Script PHP/MYSQL of management of classifieds ads developed by Script PAG
///Script PAG all rights reserved. Use under license. http://www.script-pag.com
///////////////////////////////////////////////////////////////////////////////////////////

############################################################

require_once __DIR__.'/includes/all_fns.php';

///////////////////////////////////
//Validation the id of the ad
//////////////////////////////////
	
if(isset($_GET['id_ad']))
{
	$id_ad = (int) $_GET['id_ad'];
	
	if($id_ad == 0)
	redirect('index.php');
	
	///////////////////////////////////
	//Get ad
	//////////////////////////////////
	
	$row = get_ad($id_ad, 0);
		
	if(!is_array($row))
	redirect('index.php');
		
	$name = stripslashes($row['name']);
}	
elseif(!isset($_GET['action']) || $_GET['action'] != 'SuccesSend')
redirect('index.php');

///////////////////////////////////
//Initializing variable $error
//////////////////////////////////

$error = array();

///////////////////////////////////
//Validation of data
//////////////////////////////////

if(isset($_POST['msg_name']))
{
	if(empty($_POST['msg_name']))
	$error['msg_name'] = 1;
	
	if(!valid_email($_POST['msg_email']))
	$error['msg_email'] = 1;
	
	if(empty($_POST['msg_msg']))
	$error['msg_msg'] = 1;
	
	foreach($blacklist as $v)
	{
		if($v == $_POST['msg_email'])
		{
			$error['ban'] = 1;
			break;
		}
	}
	if($param_api_gg['activ_gg_recaptcha'] == 1) {
		if(!isset($_POST['g-recaptcha-response']) || !validCaptcha($_POST['g-recaptcha-response']))
			$error['msg_cod'] = 1;
	}
}

///////////////////////////////////
//Variables of elements
//////////////////////////////////

$index = 0;
$account = 0;

///////////////////////////////////
//Send message
//////////////////////////////////

if(!empty($_POST['msg_name']) && empty($error))
{
	if($param_gen['active_filter'] == 1)
	{
		insert_send_message($_POST, $row['id_ad'], $row['email']);
		
		if($param_gen['notif'] == 1)
		{
			$nb_mail = count_mail_mail();
			
			if($nb_mail == 1)
			send_notif_mail();
		}
		
		$conn = null;
	}
	else send_mail($_POST, $row);
	
	redirect('send_advertiser.php?action=SuccesSend');
}

///////////////////////////////////
//Send message confirmation
//////////////////////////////////

elseif(isset($_GET['action']) && $_GET['action'] == 'SuccesSend')
{
	///////////////////////////////////
	//Title and Meta
	//////////////////////////////////

	$title = $language['title_page_send_msg_conf'];
	$description = $language['description_page_send_msg_conf'];
	$words = $language['keywords_page_send_msg_conf'];
	$info_page = $language['info_page_send_msg_conf'];
	
	///////////////////////////////////
	//Text
	//////////////////////////////////

	$text = $language['page_send_msg_mail_conf'];
	
	///////////////////////////////////
	//Display view
	//////////////////////////////////
	
	pool::execute('beforestart');
	
	$view = array();
	$view[] = 'template/tpl_txt.php';
	
	require_once __DIR__.'/template/tpl_layout.php';
}
else
{
	///////////////////////////////////
	//Title and Meta 
	//////////////////////////////////
	
	$title = $language['title_page_send_msg'] .' '. $name;
	$description = $language['description_page_send_msg'];
	$words = $language['keywords_page_send_msg'];
	$info_page = $language['info_page_send_msg'].' '. $name;
		
	///////////////////////////////////
	//Display view
	//////////////////////////////////
	
	pool::execute('beforestart');
	
	$view = array();
	$view[] = 'template/tpl_form_msg.php';
	
	require_once __DIR__.'/template/tpl_layout.php';
}