<?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_adm/all_fns_adm.php';

///////////////////////////////////
//Check if an admin is connected
//////////////////////////////////
	
if(!empty($_SESSION['valid_admin']))
$valid_admin = $_SESSION['valid_admin'];

if(!check_valid_all())
redirect('index.php');

///////////////////////////////////
//Validation of the action
//////////////////////////////////

if(!empty($_GET['act']))
{
	if($_GET['act'] >= 1 && $_GET['act'] <= 3)
	$act = (int) $_GET['act'];
	
	else $act = 0;
}
else $act = 0;

///////////////////////////////////
//Validation of the state of accounts
//////////////////////////////////

if(isset($_GET['state']))
{
	if($_GET['state'] >= 1 || $_GET['state'] <= 8)
	$state = (int) $_GET['state'];
	
	else $state = 0;
}

///////////////////////////////////
//Validation of the type of accounts
//////////////////////////////////

if(!empty($_GET['type']))
{
	$type = (int) $_GET['type'];
	
	if($type != 1 && $type != 2)
	redirect('index.php');
	
	$id_acc_search = (!empty($_GET['id_acc'])) ? (int) $_GET['id_acc'] : '';
	$email_search = (!empty($_GET['email'])) ? htmlspecialchars($_GET['email'], ENT_QUOTES) : '';
	$cat_search = (!empty($_GET['cat'])) ? (int) $_GET['cat'] : 0;
	$reg_search = (!empty($_GET['reg'])) ? (int) $_GET['reg'] : 0;
	$result = (!empty($_GET['result'])) ? (int) $_GET['result'] : 0;
}

///////////////////////////////////
//Get number of accounts
//////////////////////////////////	

if(isset($state) && isset($type))
$total = get_acc_num($state, $type, $_GET);

///////////////////////////////////
//Initialize the variable for accounts number per page
//////////////////////////////////

if(!empty($_GET['result']))
$limit = (int) $_GET['result'];

else $limit = 5;

///////////////////////////////////
//Pagination accounts page
//////////////////////////////////

if(isset($state) && isset($type))
{
	$page = 1;
		
	if(isset($_GET['page']))
	{
		$page = (int) $_GET['page'];
		
		if ($page <= 0)
		$page = 1;
	}
	
	$max_page = ceil($total/$limit);
	
	if ($max_page > 0 && $page > $max_page)
	$page = $max_page;
	
	$offset = ($page - 1) * $limit;
}

///////////////////////////////////
//Get accounts
//////////////////////////////////	

if(isset($state) && isset($type))
$accounts = get_accs($state, $type, $_GET, $offset, $limit);

///////////////////////////////////
//Validate account
//////////////////////////////////

if(isset($_POST['valid']))
{
	$id_acc = (int) $_POST['valid'];
	
	validate_acc($id_acc);
	
	$array = get_info_member($id_acc);
	
	if(!isset($_POST['mail_'. $id_acc]))
	send_validate_acc($id_acc, $array);
	
	redirect('acc_management.php?state='. $state .'&type='. $type .'&succes=1&menu=7');
}

///////////////////////////////////
//Validate all accounts
//////////////////////////////////

if(isset($_POST['valid_all']) && isset($_POST['check_all']))
{
	$array = $_POST['check_all'];
	
	foreach($array as $id_acc)
	{
		validate_acc($id_acc);
		
		$array = get_info_member($id_acc);
		
		if(!isset($_POST['mail_'. $id_acc]))
		send_validate_acc($id_acc, $array);
	}

	$succes = (count($_POST['check_all']) > 1) ? '&succes=2' : '&succes=1';
	
	redirect('acc_management.php?state='. $state .'&type='. $type . $succes .'&menu=7');
}

///////////////////////////////////
//Reject account
//////////////////////////////////

if(isset($_POST['reject']))
{
	$id_acc = (int) $_POST['reject'];
	
	reject_acc($id_acc);
	
	$array = get_info_member($id_acc);
	
	if(!isset($_POST['mail_'. $id_acc]))
	send_reject_acc($id_acc, $array);
	
	if(isset($_POST['ban_'. $id_acc]))
	{
		blacklisted_email($_POST['ban_'. $id_acc]);
		create_cache_blacklist();
	}
	
	redirect('acc_management.php?state='. $state .'&type='. $type .'&succes=3&menu=7');
}

///////////////////////////////////
//Reject all accounts
//////////////////////////////////

if(isset($_POST['reject_all']) && isset($_POST['check_all']))
{
	$array = $_POST['check_all'];
	
	foreach($array as $id_acc)
	{
		$array = get_info_member($id_acc);
		
		if(!isset($_POST['mail_'. $id_acc]))
		send_reject_acc($id_acc, $array);
		
		if(isset($_POST['ban_'. $id_acc]))
		{
			blacklisted_email($_POST['ban_'. $id_acc]);
			create_cache_blacklist();
		}
		
		reject_acc($id_acc);
	}
	
	$succes = (count($_POST['check_all']) > 1) ? '&succes=4' : '&succes=3';
	
	redirect('acc_management.php?state='. $state .'&type='. $type . $succes .'&menu=7');
}

///////////////////////////////////
//Delete account
//////////////////////////////////

if(isset($_POST['delete']))
{
	$id_acc = (int) $_POST['delete'];
	
	$array = get_info_member($id_acc);
	
	if(!isset($_POST['mail_'. $id_acc]))
	send_reject_acc($id_acc, $array);
	
	if(isset($_POST['ban_'. $id_acc]))
	{
		blacklisted_email($_POST['ban_'. $id_acc]);
		create_cache_blacklist();
	}
	
	delete_acc($id_acc);
	
	redirect('acc_management.php?state='. $state .'&type='. $type .'&succes=5&menu=7');
}

///////////////////////////////////
//Delete all accounts
//////////////////////////////////

if(isset($_POST['delete_all']) && isset($_POST['check_all']))
{
	$array = $_POST['check_all'];
	
	foreach($array as $id_acc)
	{
		$array = get_info_member($id_acc);
		
		if(!isset($_POST['mail_'. $id_acc]))
		send_reject_acc($id_acc, $array);
		
		if(isset($_POST['ban_'. $id_acc]))
		{
			blacklisted_email($_POST['ban_'. $id_acc]);
			create_cache_blacklist();
		}
		
		delete_acc($id_acc);
	}
	
	$succes = (count($_POST['check_all']) > 1) ? '&succes=6' : '&succes=5';
	
	redirect('acc_management.php?state='. $state .'&type='. $type . $succes .'&menu=7');
}

///////////////////////////////////
//Validating the id of the account
//////////////////////////////////

if(!isset($state) && !empty($_GET['id_acc']))
$id_acc = (int) $_GET['id_acc'];

elseif(!isset($state) || !isset($type)) redirect('index.php');

///////////////////////////////////
//Check and get the account
//////////////////////////////////

if(isset($id_acc))
{
	if(get_acc($id_acc))
	{
		$account = get_acc($id_acc);
		
		$id_acc = (int) $account['id_acc'];
		$id_reg = (isset($_POST['reg'])) ? (int) $_POST['reg'] : (int) $account['id_reg'];
		$id_county = (isset($_POST['county'])) ? (int) $_POST['county'] : (int) $account['id_county'];
		$id_cat = (isset($_POST['cat'])) ? (int) $_POST['cat'] : (int) $account['id_cat'];
		$comp_name = stripslashes(htmlspecialchars($account['comp_name'], ENT_QUOTES));
		$comp_num = stripslashes(htmlspecialchars($account['comp_num'], ENT_QUOTES));
		$civ = stripslashes(htmlspecialchars($account['civility'], ENT_QUOTES));
		$name = stripslashes(htmlspecialchars($account['name'], ENT_QUOTES));
		$first_name = stripslashes(htmlspecialchars($account['first_name'], ENT_QUOTES));
		$pseudo = stripslashes(htmlspecialchars($account['pseudo'], ENT_QUOTES));
		$pseudo_display = (int) $account['pseudo_display'];
		$add = stripslashes(htmlspecialchars($account['address'], ENT_QUOTES));
		$postcode = stripslashes(htmlspecialchars($account['postcode'], ENT_QUOTES));
		$city = stripslashes(htmlspecialchars($account['city'], ENT_QUOTES));
		$phone = stripslashes(htmlspecialchars($account['phone'], ENT_QUOTES));
		$email = stripslashes(htmlspecialchars($account['email'], ENT_QUOTES));
		$type = (int) $account['type'];
		$title_name = (!empty($comp_name)) ? $comp_name : $name;
		$credit = (float) $account['credits'];
		$credit = number_format($credit, 2, '.', '');
		$id_acc_pack = (int) $account['id_acc_pack'];
		$pack_nb_days = (int) $account['days'];
		$pack_nb_ads = (int) $account['ads'];
		$unlimited_days = (int) $account['unlimited_days'];
		$unlimited_ads = (int) $account['unlimited_ads'];
		$time_pack  = (int) $account['time_pack'];
		$id_acc_shop = (int) $account['id_acc_shop'];
		$shop_days = (int) $account['shop_days'];
		$unlimited_shop = (int) $account['unlimited_shop'];
		$time_shop  = (int) $account['time_shop'];
		
		//Remaining days pack acount
		
		$time_pack = ($pack_nb_days * 24 * 3600) + $time_pack;
		$pack_nb_days = ($time_pack - time()) / (24 * 3600);
		$pack_nb_days = ($pack_nb_days > 0) ? round($pack_nb_days) : 0;
		
		//Remaining days pack shop
		
		$time_shop = ($shop_days * 24 * 3600) + $time_shop;
		$shop_days = ($time_shop - time()) / (24 * 3600);
		$shop_days = ($shop_days > 0) ? round($shop_days) : 0;

		$display = 0;
		$display_pack_shop = 0;
		$display_pack_par = 0;
		$display_pack_pro = 0;
		$display_credit = 0;
	
		foreach($cache_counties as $row)
		{
			if($row['par_reg'] == $id_reg)
			$display = 1;
		}
		
		//Check if there are packs account
		
		if(!empty($cache_packs_shop))
		$display_pack_shop = 1;
		
		//Check if there are packs shop
		
		foreach($cache_packs_acc as $v)
		{
			if($type == 1 && $v['type'] == 1)
			{
				$display_pack_par = 1;
				break;
			}
			if($type == 2 && $v['type'] == 2)
			{
				$display_pack_pro = 1;
				break;
			}
		}
		
		//Check if there are credit
		
		if(!empty($cache_credits))
		$display_credit = 1;
	}
	else redirect('index.php');
}

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

$error = '';

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

if(isset($_POST['name']))
{
	if(empty($_POST['reg']))
	$error = 1;
	
	elseif(isset($_POST['county']) && empty($_POST['county']))
	$error = 2;
	
	elseif(isset($_POST['cat']) && empty($_POST['cat']))
	$error = 3;
	
	elseif(isset($_POST['comp_name']) && empty($_POST['comp_name']))
	$error = 4;
	
	elseif(isset($_POST['comp_num']) && empty($_POST['comp_num']))
	$error = 5;
	
	elseif(empty($_POST['name']))
	$error = 6;
	
	elseif(!empty($_POST['pseudo_display']) && empty($_POST['pseudo']))
	$error = 12;
	
	elseif(isset($_POST['postcode']) && empty($_POST['postcode']))
	$error = 7;
	
	elseif(isset($_POST['city']) && empty($_POST['city']))
	$error = 8;
	
	elseif(isset($_POST['phone']) && empty($_POST['phone']))
	$error = 9;
	
	elseif(!valid_email($_POST['email']))
	$error = 10;
	
	elseif(isset($_POST['pack_nb_days']) && !empty($_POST['unlimited_days']) && !empty($_POST['unlimited_ads']))
	$error = 11;
}

///////////////////////////////////
//Update account
//////////////////////////////////

if(isset($_POST['name']) && empty($error))
{	
	update_acc($id_acc, $_POST);
	
	redirect('acc_management.php?id_acc='. $id_acc .'&act=1&succes=1&menu=7');
}

///////////////////////////////////
//Close database connexion
//////////////////////////////////

$conn = null;
	
///////////////////////////////////
//Including html functions
//////////////////////////////////

$view = array();

if($act == 1)
$view[] = 'template_adm/tpl_acc_management_mod_adm.php';

elseif(isset($state) && isset($type))
$view[] = 'template_adm/tpl_acc_management_adm.php';

require_once __DIR__.'/template_adm/tpl_layout_adm.php';