<?php
function ParseDataAttendance($data, $p1, $p2)
{
    $data = " " . $data;
    $hasil = "";
    $awal = strpos($data, $p1);
    if ($awal != "") {
        $akhir = strpos(strstr($data, $p1), $p2);
        if ($akhir != "") {
            $hasil = substr($data, $awal + strlen($p1), $akhir - strlen($p1));
        }
    }
    return $hasil;
}
function getDataAttendance($ip, $port = 4370)
{
    $response = [
        'status' => 0,
        'message' => "",
        'data' => []
    ];
    try {
        $Connect = fsockopen($ip, $port, $errno, $errstr, 1);
        if (!$Connect) {
            $response['message'] = "Gagal koneksi ke server : \"$errstr\"";
            return $response;
        }
        $soap_request = "<GetAttLog><ArgComKey xsi:type=\"xsd:integer\">0</ArgComKey><Arg><PIN xsi:type=\"xsd:integer\">All</PIN></Arg></GetAttLog>";
        $newLine = "\r\n";
        fputs($Connect, "POST /iWsService HTTP/1.0" . $newLine);
        fputs($Connect, "Content-Type: text/xml" . $newLine);
        fputs($Connect, "Content-Length: " . strlen($soap_request) . $newLine . $newLine);
        fputs($Connect, $soap_request . $newLine);
        $buffer = "";
        while ($Response = fgets($Connect, 1024)) {
            $buffer = $buffer . $Response;
        }
        $buffer = ParseDataAttendance($buffer, "<GetAttLogResponse>", "</GetAttLogResponse>");
        $buffer = explode("\r\n", $buffer);
        for ($a = 0; $a < count($buffer); $a++) {
            $data = ParseDataAttendance($buffer[$a], "<Row>", "</Row>");
            $parsedData['PIN'] = ParseDataAttendance($data, "<PIN>", "</PIN>");
            $parsedData['DateTime'] = ParseDataAttendance($data, "<DateTime>", "</DateTime>");
            $parsedData['Verified'] = ParseDataAttendance($data, "<Verified>", "</Verified>");
            $parsedData['Status'] = ParseDataAttendance($data, "<Status>", "</Status>");
            if (!$parsedData['PIN'] && !$parsedData['DateTime'] && !$parsedData['Verified'] && !$parsedData['Status']) continue;
            $response['data'][] = $parsedData;
        }
        $response['message'] = "Succesfully";
        $response['status'] = 1;
    } catch (\Throwable $th) {
        $response['message'] = $th->getMessage();
    }
    return $response;
}
function clearDataAttendance($ip, $port = 4370, $key = 0)
{

    $response = [
        'status' => 0,
        'message' => "",
        'data' => []
    ];
    try {
        $Connect = fsockopen($ip, $port, $errno, $errstr, 1);
        if (!$Connect) {
            $response['message'] = "Gagal koneksi ke server : \"$errstr\"";
            return $response;
        }
        $soap_request = "<ClearData><ArgComKey xsi:type=\"xsd:integer\">" . $key . "</ArgComKey><Arg><Value xsi:type=\"xsd:integer\">3</Value></Arg></ClearData>";
        $newLine = "\r\n";
        fputs($Connect, "POST /iWsService HTTP/1.0" . $newLine);
        fputs($Connect, "Content-Type: text/xml" . $newLine);
        fputs($Connect, "Content-Length: " . strlen($soap_request) . $newLine . $newLine);
        fputs($Connect, $soap_request . $newLine);
        $buffer = "";
        while ($Response = fgets($Connect, 1024)) {
            $buffer = $buffer . $Response;
        }
        $buffer = ParseDataAttendance($buffer, "<Information>", "</Information>");
        $response['message'] = $buffer;
        $response['status'] = 1;
    } catch (\Throwable $th) {
        $response['message'] = $th->getMessage();
    }
    return $response;
}
function setDateTimeAttendance($ip, $port = 80, $key = 0, $date = null, $time = null)
{
    $response = [
        'status' => 0,
        'message' => "",
        'data' => []
    ];
    if (!$date) $date = date('Y-m-d');
    if (!$time)  $time = date("H:i:s");
    try {
        $Connect = fsockopen($ip, $port, $errno, $errstr, 1);
        if (!$Connect) {
            $response['message'] = "Gagal koneksi ke server : \"$errstr\"";
            return $response;
        }
        $soap_request = "<SetDate><ArgComKey xsi:type=\"xsd:integer\">" . $key . "</ArgComKey><Arg><Date xsi:type=\"xsd:string\">" . $date . "</Date><Time xsi:type=\"xsd:string\">" . $time . "</Time></Arg></SetDate>";
        $newLine = "\r\n";
        fputs($Connect, "POST /iWsService HTTP/1.0" . $newLine);
        fputs($Connect, "Content-Type: text/xml" . $newLine);
        fputs($Connect, "Content-Length: " . strlen($soap_request) . $newLine . $newLine);
        fputs($Connect, $soap_request . $newLine);
        $buffer = "";
        while ($Response = fgets($Connect, 1024)) {
            $buffer = $buffer . $Response;
        }
        $buffer = ParseDataAttendance($buffer, "<Information>", "</Information>");
        $response['message'] = $buffer;
        $response['status'] = 1;
    } catch (\Throwable $th) {
        $response['message'] = $th->getMessage();
    }
    return $response;
}
function addUserSolution(string $ip, int $port = 80, int $key = 0, int $id = null, string $nama = null)
{
    $response = [
        'status' => 0,
        'message' => "",
    ];
    try {
        $Connect = fsockopen($ip, $port, $errno, $errstr, 1);
        if (!$Connect) {
            $response['message'] = "Gagal koneksi ke server : \"$errstr\"";
            return $response;
        }
        $soap_request = "<SetUserInfo><ArgComKey Xsi:type=\"xsd:integer\">" . $key . "</ArgComKey><Arg><PIN>" . $id . "</PIN><Name>" . $nama . "</Name></Arg></SetUserInfo>";
        $newLine = "\r\n";
        fputs($Connect, "POST /iWsService HTTP/1.0" . $newLine);
        fputs($Connect, "Content-Type: text/xml" . $newLine);
        fputs($Connect, "Content-Length: " . strlen($soap_request) . $newLine . $newLine);
        fputs($Connect, $soap_request . $newLine);
        $buffer = "";
        while ($Response = fgets($Connect, 1024)) {
            $buffer = $buffer . $Response;
        }
        $buffer = ParseDataAttendance($buffer, "<Information>", "</Information>");
        $response['message'] = $buffer;
        $response['status'] = 1;
    } catch (\Throwable $th) {
        $response['message'] = $th->getMessage();
    }
    return $response;
}
