Files
BE_CPONE/application/controllers/ais_100326/Bridging.php
2026-04-27 10:26:26 +07:00

182 lines
5.1 KiB
PHP

<?php
class Bridging extends MY_Controller
{
var $db_onedev;
public function index()
{
echo "Resultentry 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'));
}
public function error_log($data,$userid=999)
{
$sql = "INSERT INTO ais_error_log (
AisErrorLogFnName,
AisErrorLogMessage,
AisErrorLogQuery,
AisErrorLogJson,
AisErrorLogUserID,
AisErrorLogCreated
)
VALUES(
?,
?,
?,
?,
?,
NOW()
)";
$qry = $this->db_log->query($sql, array($data['fn_name'],$data['message'],$data['query'],$data['json'],$userid));
//echo $this->db_log->last_query();
return true;
}
/**
* 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
*/
public function post_request($url, $data = array(), $headers = array())
{
// Default headers
$default_headers = array(
'Content-Type: application/json'
);
// Merge custom headers with default headers
$final_headers = array_merge($default_headers, $headers);
// Initialize cURL
$ch = curl_init();
// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $final_headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// Execute cURL request
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
// Close cURL
curl_close($ch);
// Prepare result
$result = array(
'success' => false,
'http_code' => $http_code,
'response' => null,
'error' => null
);
if ($error) {
$result['error'] = $error;
} else {
$result = json_decode($response, true);
$result['success'] = true;
}
return $result;
}
function get_config()
{
$sql = "SELECT * FROM ais_config LIMIT 1";
$qry = $this->db_onedev->query($sql);
$dt_config = $qry->row_array();
return $dt_config;
}
/**
* 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');
echo json_encode($errors);
exit;
}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');
echo json_encode($errors);
exit;
}
$success = array('status' => 'success','message' => 'Berhasil Login');
echo json_encode($success);
exit;
}
}
}