<?php
include("../../db.php");
header("Access-Control-Allow-Origin: * ");
header('Content-Type: application/json');

date_default_timezone_set("Asia/Kolkata");

$class = $_REQUEST['class'];
$section = trim($_REQUEST['section']);
$uid = $_REQUEST['uid'];
$session = $_REQUEST['session'];
$exam = $_REQUEST['exam'];

$monthsList1 = array(1   =>  'April', 2   =>  'May', 3   =>  'June', 4   =>  'July ', 5   =>  'August', 6   =>  'September',
                     7   =>  'October', 8   =>  'November', 9   =>  'December', 10  =>  'January', 11  =>  'February', 12  =>  'March');

$duemonth = $db->getVal("select month from timetableexam where  name='$exam' and session ='$session' ");

$_REQUEST['due_month'] = $duemonth;

$total_comp = include('fee_calculate.php');

///////////////////

$status = 0;
$msg = '';

if($total_comp > 0) {
    $status = 1;
    $msg = "Please clear your Dues till ".$monthsList1[$duemonth]." to download Admit Card.";
} else {
    $msg = "You can download your Admit Card.";
}

$flag = $db->getVal("select admit_card_flag from tbl_info where id=1");

///////

$fStudent = $db->getRow("select * from student where idno='$uid' and  school_id='1' and session='$session' and status=1 and issue_TC=0");


if ($flag == 1) {
    $status = 0;
    $msg = "You can download your Admit Card.";
} elseif ($flag == 2) {
    if ($total_comp > 0) {
        $status = 1;
        $msg = "Please clear your Dues till ".$monthsList1[$duemonth]." to download Admit Card.";
    } else {
        $status = 0;
        $msg = "You can download your Admit Card.";
    }
} elseif ($flag == 3) {
    if ($total_comp > 0) {
        if ($fStudent['admit_card_view'] == 1) {
            $status = 0;
            $msg = "You can download your Admit Card.";
        } else {
            $status = 1;
            $msg = "Please clear your Dues till ".$monthsList1[$duemonth]." to download Admit Card.";
        }
    } else {
        $status = 0;
        $msg = "You can download your Admit Card.";
    }
} elseif ($flag == 4) {
    if ($fStudent['admit_card_view'] == 1) {
        $status = 0;
        $msg = "You can download your Admit Card.";
    } else {
        $status = 1;
        $msg = "Admit card is not available now";
    }
} else {
    $status = 1;
    $msg = "Admit card is not available now";
}

//////

$array['status'] = $status;
$array['due'] = $total_comp;
$array['msg'] = $msg;

if ($status == 0){

    $EncClass     = urlencode(encryptMessage($class));
    $EncStudentId = urlencode(encryptMessage($uid));
    $EncSession   = urlencode(encryptMessage($session));
    $EncExam      = urlencode(encryptMessage($exam));

    $array['admit_card_url'] = "https://".$_SERVER['HTTP_HOST']."/api_new/student/api_admit_card.php?p1=$EncClass&p2=$EncStudentId&p3=$EncSession&p4=$EncExam";
}

echo json_encode($array);

/////////////// Encryption for security and prevent unauthorized access by student(issue in SSD) 10-02-2023 ///////////////////////
function encryptMessage($input) {

    $iv = "@@@@&&&&####$$$$";
    $key = 'lLvM1QLNMigKIpVX';

    $key = html_entity_decode($key);

    if(function_exists('openssl_encrypt')){
        $data = openssl_encrypt ( $input , "AES-128-CBC" , $key, 0, $iv );
    } else {
        $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, 'cbc');
        $input = pkcs5Pad($input, $size);
        $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', 'cbc', '');
        mcrypt_generic_init($td, $key, $iv);
        $data = mcrypt_generic($td, $input);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        $data = base64_encode($data);
    }
    return $data;
}
function pkcs5Pad($text, $blocksize) {
    $pad = $blocksize - (strlen($text) % $blocksize);
    return $text . str_repeat(chr($pad), $pad);
}