Files
2026-04-15 15:23:57 +07:00

270 lines
8.7 KiB
PHP

<?php
class Test extends MY_Controller
{
var $db_onedev;
public function index()
{
echo "Transaction API";
}
public function __construct()
{
parent::__construct();
$this->db_onedev = $this->load->database("onedev", true);
$this->db_log = $this->load->database("log", true);
$this->load->helper(array('form', 'url'));
}
/**
* Generic POST request function
* @param string $url API endpoint URL
* @param array $data Request payload
* @param array $headers Custom headers (optional)
* @return array Response from API
*/
/**
* Generic PUT request function
* @param string $url API endpoint URL
* @param array $data Request payload
* @param array $headers Custom headers (optional)
* @return array Response from API
*/
/**
* POST request to auth API
* @param string $username Username for authentication
* @param string $password Password for authentication
* @return array Response from API
*/
public function post_auth()
{
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl.'/api/auth/auth.php';
$headers = array(
'Header-Token: '.$dt_config['AisConfigHeaderToken']
);
$username = $dt_config['AisConfigUsername'];
$password = $dt_config['AisConfigPassword'];
$data = array(
'username' => $username,
'password' => $password
);
$result = $this->post_request($url, $data, $headers);
$sql = "INSERT INTO ais_login_log(
AisLoginLogUsername,
AisLoginLogPassword,
AisLoginLogHeaderToken,
AisLoginLogResult,
AisLoginLogCreated
)
VALUES(
?,
?,
?,
?,
NOW()
)";
$qry = $this->db_log->query($sql, array($username,$password,$dt_config['AisConfigHeaderToken'],json_encode($result)));
if(!$qry){
$this->error_log(array('fn_name'=>'post_auth','message'=>'ais_login_log insert','query'=>$sql,'json'=>json_encode($result)),999);
exit;
}
//print_r($result);
//exit;
// Check if success
if(!$result['success']){
$this->error_log(array('fn_name'=>'post_auth','message'=>'failed auth','query'=>'','json'=>json_encode($result)),999);
$errors = array('status' => 'error','message' => 'Gagal Login');
return $errors;
}else{
// Update token
$token = $result['data']['token'];;
$sql = "UPDATE ais_config SET AisConfigAuthToken = ? WHERE AisConfigID = 1";
$qry = $this->db_onedev->query($sql, array($token));
//echo $this->db_onedev->last_query();
//exit;
if(!$qry){
$this->error_log(array('fn_name'=>'post_auth','message'=>'ais_config update','query'=>$sql,'json'=>''),999);
$errors = array('status' => 'error','message' => 'Gagal Update Token');
return $errors;
}
return $result;
}
}
function monitoring_test()
{
// SQL base
$sql_base = "SELECT
t.T_TestID,
t.T_TestCode AS test_code,
t.T_TestSasCode AS sas_code,
t.T_TestName AS test_name,
t.T_TestIsPrice AS is_price,
t.T_TestIsResult AS is_result,
t.T_TestIsActive AS test_active,
at.Ais_TestStatus AS Ais_TestStatus,
at.Ais_TestLastUpdate AS test_last_update,
IF (at.Ais_TestID IS NULL, 'N', 'Y') AS test_status
FROM t_test t
LEFT JOIN (
SELECT Ais_TestTestSasCode, Ais_TestStatus, Ais_TestLastUpdate, Ais_TestID
FROM ".$this->db_log->database.".ais_test
WHERE Ais_TestStatus = 'success'
) at
ON at.Ais_TestTestSasCode = t.T_TestSasCode
";
// Ambil parameter
$test_name = $this->input->get('test_name'); // TRUE for XSS filtering
$sas_code = $this->input->get('sas_code');
$test_code = $this->input->get('test_code');
$test_status = $this->input->get('test_status');
// Pagination
$page = $this->input->get('page') ? intval($this->input->get('page')) : 1;
$limit = 10;
$offset = ($page - 1) * $limit;
$params = [];
$where_clauses = [
"t.T_TestIsActive = 'Y'"
];
// Status filter
if ($test_status === 'Y') {
$where_clauses[] = "at.Ais_TestID IS NOT NULL";
} elseif ($test_status === 'N') {
$where_clauses[] = "at.Ais_TestID IS NULL";
}
if ($test_name) {
$where_clauses[] = "t.T_TestName LIKE ?";
$params[] = "%$test_name%";
}
if ($test_code) {
$where_clauses[] = "t.T_TestCode LIKE ?";
$params[] = "%$test_code%";
}
if ($sas_code) {
$where_clauses[] = "t.T_TestSasCode LIKE ?";
$params[] = "%$sas_code%";
}
$where_sql = "";
if (!empty($where_clauses)) {
$where_sql = " WHERE " . implode(" AND ", $where_clauses);
}
// COUNT
$count_sql = "
SELECT COUNT(*) AS total
FROM (
SELECT t.T_TestID
FROM t_test t
LEFT JOIN (
SELECT Ais_TestTestSasCode, Ais_TestID
FROM ".$this->db_log->database.".ais_test
WHERE Ais_TestStatus = 'success'
) at
ON at.Ais_TestTestSasCode = t.T_TestSasCode
$where_sql
GROUP BY t.T_TestID
) AS x
";
$qry_count = $this->db_onedev->query($count_sql, $params);
$total = $qry_count->row()->total ?? 0;
// MAIN QUERY
$sql = $sql_base . $where_sql . "
GROUP BY t.T_TestID
ORDER BY t.T_TestID ASC
LIMIT $limit OFFSET $offset";
$qry = $this->db_onedev->query($sql, $params);
// if (!$qry) {
// $this->output
// ->set_content_type('application/json')
// ->set_output(json_encode(['status' => 'error', 'message' => 'Gagal mengambil data monitoring.']));
// return;
// }
if (!$qry) {
echo json_encode(['status' => 'error', 'message' => 'Gagal mengambil data monitoring.']);
exit;
}
$data = $qry->result_array();
echo json_encode([
'status' => 'success',
'message' => 'Berhasil mengambil data monitoring.',
'data' => $data,
'total' => $total,
'page' => $page,
'limit' => $limit,
'total_page' => ceil($total / $limit)
]);
exit;
}
function get_json_response()
{
$sas_code = $this->input->get('sas_code');
if (empty($sas_code)) {
return $this->output
->set_status_header(400)
->set_content_type('application/json')
->set_output(json_encode([
'status' => 'error',
'message' => 'sas_code parameter is required'
]));
}
$sql = "SELECT
Ais_TestID AS id,
Ais_TestTestSasCode AS sas_code,
Ais_TestJSON AS test_json,
Ais_TestStatus AS test_status,
Ais_TestResponse AS test_response,
Ais_TestLastUpdate AS test_last_update
FROM ais_test
WHERE Ais_TestTestSasCode = ?
ORDER BY Ais_TestLastUpdate DESC";
// Query ke database LOG
$qry = $this->db_log->query($sql, [$sas_code]);
$results = $qry->row_array();
$results = $qry->result_array();
if ($results) {
// Loop through each result to decode JSON
foreach ($results as &$result) {
$result['packet_json'] = json_decode($result['packet_json']);
$result['packet_response'] = json_decode($result['packet_response']);
}
unset($result); // Unset reference to avoid side effects
echo json_encode(['status' => 'success', 'data' => $results]);
} else {
echo json_encode(['status' => 'error', 'message' => 'Data not found for the given sas code.']);
}
exit;
}
}