<?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 type
//////////////////////////////////

if (isset($_GET['type']))
{
	$type = (int) $_GET['type'];
	
	if ($type !=1 && $type != 2)
	$type = 1;
}
else $type = $param_gen['active_acc'] == 4 ? 2 : 1;

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

if(isset($_SESSION['connect_id']))
redirect('acc_dashboard.php');

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

if(($param_gen['active_acc'] < 2) || ($type == 1 && $param_gen['active_acc'] == 4) || ($type == 2 && $param_gen['active_acc'] > 4))	
redirect('index.php');

///////////////////////////////////
//Validation of the region
//////////////////////////////////
	
if (!empty($_POST['reg']))
$reg = (int) $_POST['reg'];
	
else $reg = 0;

///////////////////////////////////
//Validation of the county
//////////////////////////////////

if (!empty($_POST['county']))
$county = (int) $_POST['county'];
	
else $county = 0;
	
///////////////////////////////////
//Validation of the category
//////////////////////////////////

if (!empty($_POST['cat']))
$cat = (int) $_POST['cat'];

else $cat = 0;

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

$error = array();
	
///////////////////////////////////
//Validation of data
//////////////////////////////////

if(isset($_POST['name']))
{
	if($type == 2)
	{
		if(empty($_POST['comp_name']))
		$error['comp_name'] = 1;
		
		if($cache_param_fields['act_comp_num'] == 1)
		{
			if(empty($_POST['comp_num']))
			$error['comp_num'] = 1;
		}
	}
	
	if(empty($_POST['name']))
	$error['name'] = 1;
	
	if(empty($_POST['first_name']))
	$error['first_name'] = 1;
	
	if($type == 1 && !empty($_POST['pseudo_display']) && empty($_POST['pseudo']))
	$error['pseudo'] = 1;
	
	if($reg == 0)
	$error['reg'] = 1;
	
	if(isset($_POST['county']) && $county == 0)
	$error['county'] = 1;
	
	if($type == 2 && $param_gen['active_rubric'] == 1 && $cat == 0)
	$error['cat'] = 1;
	
	if(empty($_POST['address']))
	$error['address'] = 1;
	
	if($cache_param_fields['act_postcode'] == 1)
	{
		if(isset($_POST['postcode']) && empty($_POST['postcode']))
		$error['postcode'] = 1;
	}
	
	if($cache_param_fields['act_city'] == 1)
	{
		if(empty($_POST['city']))
		$error['city'] = 1;
	}
	
	if(empty($_POST['phone']))
	$error['phone'] = 1;
	
	foreach($blacklist as $v)
	{
		if($v == $_POST['email'])
		$error['ban'] = 1;
	}
	
	if (!valid_email($_POST['email']))
	$error['email'] = 1;
	
	if(check_email($_POST['email']))
	$error['email_duplicate'] = 1;
	
	if(!empty($_POST['pas']) && preg_match('#^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).{8,}#', $_POST['pas']) == false)
	$error['str_pas'] = 1;
	
	if(empty($_POST['pas']) OR $_POST['pas'] != $_POST['pas2'])
	$error['pas'] = 1;
	
	if(!isset($_POST['gtc']))
	$error['gtc'] = 1;
	
	if($param_api_gg['activ_gg_recaptcha'] == 1) {
		if(!isset($_POST['g-recaptcha-response']) || !validCaptcha($_POST['g-recaptcha-response']))
			$error['create_acc_cod'] = 1;
	}
}

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

if($param_api_fb['active_fb'] == '1' && is_allowed_cookie('facebook_connect'))
{
	require_once __DIR__.'/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='. $type, $permissions);
}

///////////////////////////////////
//Get Gtc and Politic Links
//////////////////////////////////

$url_gtc = isset($cache_pages[3]['name_page_'. $_SESSION['code_lang']]) ? rewriting($cache_pages[3]['name_page_'. $_SESSION['code_lang']]) : '';
$id_url_gtc = isset($cache_pages[3]['id_page']) ? $cache_pages[3]['id_page'] : '';
$url_politic = isset($cache_pages[5]['name_page_'. $_SESSION['code_lang']]) ? rewriting($cache_pages[5]['name_page_'. $_SESSION['code_lang']]) : '';
$id_url_url_politic = isset($cache_pages[5]['id_page']) ? $cache_pages[5]['id_page'] : '';

$link_gtc = isset($cache_pages[3]['name_page_'. $_SESSION['code_lang']]) ? '<a href="'. URL_DISPLAY .'/'. $language['router_slug_page'] .'/'. $url_gtc .'-'. $id_url_gtc .'" class="first-color bold" target="_blank">'. $cache_pages[3]['name_page_'. $_SESSION['code_lang']] .'</a>' : '';
$link_politic = isset($cache_pages[5]['name_page_'. $_SESSION['code_lang']]) ? '<a href="'. URL_DISPLAY .'/'. $language['router_slug_page'] .'/'. $url_politic .'-'. $id_url_url_politic .'" class="first-color bold" target="_blank">'. $cache_pages[5]['name_page_'. $_SESSION['code_lang']] .'</a>' : '';

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

$index = 0;
$account = 0;

///////////////////////////////////
//Create the account
//////////////////////////////////

if(isset($_POST['name']) && empty($error))
{
	if($param_gen['auto_acc'] == 1)
	$state = 2;
	
	else $state = 0;
	
	$id_acc = create_account($_POST, $type, $state);
	
	if($param_gen['auto_acc'] == 0)
	send_confirm_acc($type, $_POST['email'], $_SESSION['code_lang']);
	
	else
	{
		send_valid_acc($type, $_POST['email']);
		update_ads_account($_POST['email'], $id_acc);
	}
	
	redirect('acc_created.php?action=SuccesCreation');
}

///////////////////////////////////
//Confirmation of account creation
//////////////////////////////////

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

	$title = $language['account_create_title'];
	$description = $language['account_create_desc'];
	$words = $language['account_create_key'];
	$info_page = $language['account_create_info'];
	
	///////////////////////////////////
	//Text of the page
	//////////////////////////////////
	
	if($param_gen['auto_acc'] == 0)
	$text = $language['account_create_conf_msg1'];
	
	else $text = $language['account_create_conf_msg2'];

	///////////////////////////////////
	//Display view
	//////////////////////////////////
	
	pool::execute('beforestart');
	
	$view = array();
	$view[] = 'template/tpl_txt.php';
	
	require_once __DIR__.'/template/tpl_layout.php';
}
else
{
	///////////////////////////////////
	//Title and Meta 
	//////////////////////////////////
	
	$title = ($type == 1) ? $language['account_crea1'] : $language['account_crea2'];
	$description = $language['account_create_desc'];
	$words = $language['account_create_key'];
	$info_page = ($type == 1) ? $language['account_crea1'] : $language['account_crea2'];
	
	///////////////////////////////////
	//Display view
	//////////////////////////////////
	
	pool::execute('beforestart');
	
	$view = array();
	$view[] = 'template/tpl_form_acc_crea.php';
	
	require_once __DIR__.'/template/tpl_layout.php';
}