<?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 of the ad ID
//////////////////////////////////

if(isset($_GET['id_ad']))
{
	$id_ad = (int) $_GET['id_ad'];
	
	if($id_ad <= 0)
	redirect('index.php');
}
	
///////////////////////////////////
//Initializing variable $error
//////////////////////////////////

$error = '';

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

if(isset($_POST['email']))
{
	if(verify_email($id_ad, $_POST['email']))
	$error = '';
	
	else $error = 1;
}

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

$index = 0;
$account = 0;

///////////////////////////////////
//Send new password
//////////////////////////////////

if(isset($_POST['email']) && empty($error))
{
	$password = generate_password();
	
	update_password($id_ad, $_POST['email'], $password);
	
	$title = get_ad($id_ad, 0);
	$title = $title['title'];
	
	send_password($id_ad, $_POST['email'], $title, $password);

	$conn = null;
	
	redirect('ad_password.php?action=SuccesUpdate');
}

///////////////////////////////////
//Send new password confirmation
//////////////////////////////////

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

	$title = $language['title_page_send_pass_conf'];
	$description = $language['description_page_send_pass_conf'];
	$keywords = $language['keywords_page_send_pass_conf'];
	$info_page = $language['info_page_send_pass_conf'];
	
	///////////////////////////////////
	//Text of the page
	//////////////////////////////////

	$text = $language['page_send_pass_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_pass'];
	$description = $language['description_page_send_pass'];
	$keywords = $language['keywords_page_send_pass'];
	$info_page = $language['info_page_send_pass'];
	
	///////////////////////////////////
	//Display view
	//////////////////////////////////
	
	pool::execute('beforestart');
	
	$view = array();
	$view[] = 'template/tpl_form_ad_forgot_psw.php';
	
	require_once __DIR__.'/template/tpl_layout.php';
}