<?php

include_once 'config.php';

$url = 'https://freightflowllc.site/mc/api/';
$status = 2;
$downloads = 0;
$msg = 'There are some mis-configurations. Please contact support.';
$resp = array('msg' => $msg, 'email' => 'you@email.com', 'status' => $status, 'url' => $url, 'downloads' => $downloads);

$json = file_get_contents('php://input');
$obj = json_decode($json);

if (empty($obj)) {
    $resp = array('msg' => 'No data provided', 'email' => 'you@email.com', 'status' => $status, 'url' => $url, 'downloads' => $downloads);
    echo json_encode($resp);
    exit();
} else {
    $email = $obj->{'email'};
    $downloads = $obj->{'downloads'};

    $result = mysqli_query($link, "SELECT * FROM customers WHERE cust_email='" . $obj->{'email'} . "'");

    if (!$result) {
        $msg = "MYSQL_ERROR:" . mysqli_error($link);
        $resp = array('msg' => $msg, 'email' => $email, 'status' => $status, 'url' => $url);
        echo json_encode($resp);
        exit();
    }

    if (mysqli_num_rows($result) == 0) {
        $status = 0;
    } else {
        $row = mysqli_fetch_array($result);
        $newD = $downloads;
        $update = 0;

        if ($row['cust_status'] == 0) {
            $status = 2;
            $msg = "Your activation status is BLOCKED. Contact us for activation.";
        } else {
            if ($downloads == 0) {
                if ($row['allowed_downloads'] == 0) {
                    $status = 2;
                    $msg = "Your download limit does not exist. Contact us to get a package.";
                } elseif ($row['allowed_downloads'] > 0 && $row['allowed_downloads'] > $row['current_downloads']) {
                    $status = 1;
                    $update = 1;
                    $newD = $row['current_downloads'] + 1;
                } else {
                    $status = 2;
                    $msg = "Your download limit has exceeded the allowed limits. Contact us to renew your package.";
                    $downloads = $row['current_downloads'];
                }
            } else {
                if ($row['allowed_downloads'] > 0) {
                    if ($row['allowed_downloads'] > $row['current_downloads']) {
                        if ($downloads > $row['current_downloads']) {
                            $newD = $downloads;
                        } else {
                            $newD = $row['current_downloads'];
                        }
                        $update = 1;
                        $status = 1;
                    } else {
                        $status = 2;
                        $msg = "Your download limit has exceeded the allowed limits. Contact us to renew your package.";
                    }
                } else {
                    $status = 2;
                    $msg = "Your download limit has been RESET. Contact us for our packages.";
                }
            }

            if ($update == 1) {
                $q = "UPDATE customers SET current_downloads='$newD' WHERE cust_email='" . $obj->{'email'} . "'";
                $result = mysqli_query($link, $q);
                if (!$result) {
                    $msg = "MYSQL_ERROR:" . mysqli_error($link);
                    $resp = array('msg' => $msg, 'email' => $email, 'status' => $status, 'url' => $url);
                    echo json_encode($resp);
                    exit();
                }
            }
        }
    }

    $resp = array('msg' => $msg, 'email' => $email, 'status' => $status, 'url' => $url, 'downloads' => $downloads);
    echo json_encode($resp);
}

?>
