<?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';

///////////////////////////////////
//Check if the user is not already logged in
//////////////////////////////////

if(isset($_SESSION['connect_id']))
redirect((isset($_GET['back']) && filter_var($_GET['back'], FILTER_VALIDATE_URL) !== false && strpos($_GET['back'], URL_DISPLAY) === 0 ? urldecode($_GET['back']) : 'acc_dashboard.php'));

///////////////////////////////////
//Check if member accounts are enabled
//////////////////////////////////

if($param_gen['active_acc'] < 2)	
redirect('index.php');

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

$error = '';
	
///////////////////////////////////
//Validation of data
//////////////////////////////////

if(isset($_POST['email_con']))
{
	if (empty($_POST['email_con']) || !valid_email($_POST['email_con']))
	$error = 1;
	
	elseif (empty($_POST['password']))
	$error = 2;
}

///////////////////////////////////
//Validation of the connection
//////////////////////////////////

if(isset($_POST['email_con']) && empty($error))
{
	if($result = check_account($_POST['email_con'], $_POST['password']))
	{
		$_SESSION['connect_id'] = $result['id_acc'];
		
		pool::execute('accountLogin', $result['id_acc']);
		
		if(isset($_GET['back']) && $_GET['back'] == 'FormDeposit')
		redirect($language['router_slug_deposit']);
		
		if(isset($_GET['back']) && $_GET['back'] == 'FormUpdate' && isset($_GET['id_ad']))
		redirect('ad_update.php?id_ad='. (int) $_GET['id_ad']);
		
		if(isset($_GET['back']) && $_GET['back'] == 'SearchAlert')
		redirect('acc_search.php');
		
		else if(isset($_GET['back']) && filter_var($_GET['back'], FILTER_VALIDATE_URL) !== false && strpos($_GET['back'], URL_DISPLAY) === 0) redirect(urldecode($_GET['back']));
		
		else redirect('acc_dashboard.php');
	}
	else $error = 3;
}

if(isset($_GET['action']) && $_GET['action'] == 'FacebookError')
$error = 4;

///////////////////////////////////
//Facebook URL
//////////////////////////////////

if($param_api_fb['active_fb'] == '1' && is_allowed_cookie('facebook_connect'))
{
	require_once('includes/facebook/autoload.php');
	
	$fb = new Facebook\Facebook(['app_id' => $param_api_fb['id_fb'], 'app_secret' => $param_api_fb['key_fb']]);
	
	$helper = $fb->getRedirectLoginHelper();

	$permissions = ['email']; 
	$loginUrl = $helper->getLoginUrl(URL .'/fb_connect.php?type=0', $permissions);
}

///////////////////////////////////
//URL back
//////////////////////////////////

$url_back = '';

if(isset($_GET['back']) && ($_GET['back'] == 'FormDeposit' || $_GET['back'] == 'FormUpdate' || (filter_var($_GET['back'], FILTER_VALIDATE_URL) !== false && strpos($_GET['back'], URL_DISPLAY) === 0)))
{
	$url_back = '?back='. $_GET['back'];
	
	if(isset($_GET['id_ad']))
	$url_back .= '&id_ad='. $_GET['id_ad'];
}

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

$index = 0;
$account = 0;

///////////////////////////////////
//Title and Meta 
//////////////////////////////////

$title = $language['title_conn_acc'];
$description = $language['description_conn_acc'];
$words = $language['keywords_conn_acc'];
$info_page = $language['info_conn_acc'];

///////////////////////////////////
//Display view
//////////////////////////////////

pool::execute('beforestart');

$view = array();
$view[] = 'template/tpl_form_acc_conn.php';

require_once __DIR__.'/template/tpl_layout.php';