<?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');
	
	$row = get_ad($id_ad, 0);
}
else redirect('index.php');

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

$error = array();

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

if(isset($_POST['friend_name']))
{
	if(empty($_POST['friend_name']))
	$error['friend_name'] = 1;
	
	if(!valid_email($_POST['friend_email']))
	$error['friend_email'] = 1;
	
	if($param_api_gg['activ_gg_recaptcha'] == 1) {
		if(!isset($_POST['g-recaptcha-response']) || !validCaptcha($_POST['g-recaptcha-response']))
			$error['friend_cod'] = 1;
	}
}

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

$index = 0;
$account = 0;

///////////////////////////////////
//Send mail
//////////////////////////////////

if(!empty($_POST['friend_name']) && empty($error))
{
	send_friend($_POST, $id_ad, $row['title'], $row);
	redirect('send_friend.php?action=SuccesSend&id_ad='. $id_ad);
}

///////////////////////////////////
//Send mail confirmation
//////////////////////////////////

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

	$title = $language['title_page_send_to_friend'];
	$description = $language['description_page_send_to_friend'];
	$words = $language['keywords_page_send_to_friend'];
	$info_page = $language['info_page_send_to_friend'];
	
	///////////////////////////////////
	//Text of the page
	//////////////////////////////////
	
	$cat_name = name_cat($row['id_cat']);
	$reg_name = name_region($row['id_reg']);
	$county_name = name_county($row['id_county']);
	
	$url_rewrite = '';
	$url_rewrite .= (!empty($reg_name)) ? $reg_name .' ' : '';
	$url_rewrite .= (!empty($county_name)) ? $county_name .' ' : '';
	$url_rewrite .= (!empty($cat_name)) ? $cat_name .' ' : '';
			
	$url_display = rewriting($url_rewrite . $row['title']);
	
	$text = $language['send_friend_text_conf'] .' <a class="first-color" href="'. URL_DISPLAY .'/'. $language['router_slug_ad'] .'/'. $url_display .'-'. $id_ad .'">'. $language['send_friend_text_return'] .'</a>';
		
	///////////////////////////////////
	//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_to_friend'];
	$description = $language['description_page_send_to_friend'];
	$words = $language['keywords_page_send_to_friend'];
	$info_page = $language['info_page_send_to_friend'];
	
	///////////////////////////////////
	//Display view
	//////////////////////////////////
	
	pool::execute('beforestart');
	
	$view = array();
	$view[] = 'template/tpl_form_friend.php';
	
	require_once __DIR__.'/template/tpl_layout.php';
}