192 lines
5.6 KiB
PHP
192 lines
5.6 KiB
PHP
<?php
|
|
class Apiclinic extends MY_Controller
|
|
{
|
|
var $db_antrione;
|
|
var $load;
|
|
var $endpoint;
|
|
function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->db_antrione = $this->load->database("antrione", true);
|
|
$this->endpoint = "http://10.9.10.38:8787/";
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
echo "API token clinic";
|
|
}
|
|
|
|
function refresh_token()
|
|
{
|
|
$prm = $this->sys_input;
|
|
$login = $prm["login"];
|
|
$password_hash = $prm["password_hash"];
|
|
$tenant_code = $prm["tenant_code"];
|
|
|
|
$url = $this->endpoint . "api/ibl/login";
|
|
$param = [
|
|
"login" => $login,
|
|
"password_hash" => $password_hash,
|
|
"tenant_code" => $tenant_code
|
|
];
|
|
|
|
$resp = $this->post_xmod($url, json_encode($param));
|
|
|
|
$jresp = json_decode($resp, true);
|
|
if ($jresp["token"] != "") {
|
|
$this->insert_or_update($jresp);
|
|
} else {
|
|
echo $resp;
|
|
}
|
|
}
|
|
|
|
public function post_xmod($url, $data)
|
|
{
|
|
$ch = curl_init($url);
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
"Content-Type: application/json"
|
|
]);
|
|
$result = curl_exec($ch);
|
|
|
|
if (curl_error($ch) != "") {
|
|
return "ERROR CLINIC API [$url] : " . curl_error($ch) . "\n";
|
|
}
|
|
curl_close($ch);
|
|
return $result;
|
|
}
|
|
|
|
function insert_or_update($data)
|
|
{
|
|
$expires_iso = $data["expires_at"];
|
|
$expires = date("Y-m-d H:i:s", strtotime($expires_iso));
|
|
$user_id = $data['user']['id'];
|
|
$username = $data['user']['login'];
|
|
$redirect_to = $data['redirect_to'];
|
|
$token = $data['token'];
|
|
|
|
if (!$user_id) {
|
|
echo json_encode([
|
|
"status" => "error",
|
|
"message" => "ClinicLoginIdUser wajib diisi"
|
|
]);
|
|
exit;
|
|
}
|
|
|
|
# cek apakah token sudah ada
|
|
$chek = "SELECT ClinicLoginID
|
|
FROM clinic_login
|
|
WHERE ClinicLoginIdUser = ?
|
|
LIMIT 1";
|
|
$query_check = $this->db_antrione->query($chek, [$user_id]);
|
|
if (!$query_check) {
|
|
$this->db_antrione->trans_rollback();
|
|
$this->sys_error_db("cek token clinic error", $this->db_antrione);
|
|
exit;
|
|
}
|
|
|
|
$data = $query_check->result_array();
|
|
|
|
if (count($data) > 0) {
|
|
# UPDATE
|
|
|
|
$sql_update = "UPDATE clinic_login
|
|
SET
|
|
ClinicLoginExpires = ?,
|
|
ClinicLoginRedirectTo = ?,
|
|
ClinicLoginIdUser = ?,
|
|
ClinicLoginUsername = ?,
|
|
ClinicLoginToken = ?,
|
|
ClinicLoginLastUpdated = NOW()
|
|
WHERE ClinicLoginIdUser = ?";
|
|
$qry_update = $this->db_antrione->query($sql_update, [
|
|
$expires,
|
|
$redirect_to,
|
|
$user_id,
|
|
$username,
|
|
$token,
|
|
$user_id
|
|
]);
|
|
if (!$qry_update) {
|
|
$this->db_antrione->trans_rollback();
|
|
$this->sys_error_db("Error update clinic tokne", $this->db_antrione);
|
|
exit;
|
|
}
|
|
|
|
echo json_encode([
|
|
"status" => "OK",
|
|
"message" => "Token berhasil diupdate"
|
|
]);
|
|
} else {
|
|
|
|
# INSERT
|
|
$sql_insert = "INSERT INTO clinic_login
|
|
(
|
|
ClinicLoginExpires,
|
|
ClinicLoginRedirectTo,
|
|
ClinicLoginIdUser,
|
|
ClinicLoginUsername,
|
|
ClinicLoginToken,
|
|
ClinicLoginCreated
|
|
)
|
|
VALUES (?, ?, ?, ?, ?,NOW())";
|
|
$qry_update = $this->db_antrione->query($sql_insert, [
|
|
$expires,
|
|
$redirect_to,
|
|
$user_id,
|
|
$username,
|
|
$token,
|
|
]);
|
|
if (!$qry_update) {
|
|
$this->db_antrione->trans_rollback();
|
|
$this->sys_error_db("Error insert clinic tokne", $this->db_antrione);
|
|
exit;
|
|
}
|
|
|
|
echo json_encode([
|
|
"status" => "OK",
|
|
"message" => "Token berhasil disimpan"
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function password_hash_api()
|
|
{
|
|
try {
|
|
|
|
$prm = $this->sys_input;
|
|
|
|
$password = $prm["password"] ?? "";
|
|
|
|
if ($password == "") {
|
|
echo json_encode([
|
|
"status" => "error",
|
|
"message" => "Password kosong"
|
|
]);
|
|
exit;
|
|
}
|
|
|
|
$hash = password_hash($password, PASSWORD_BCRYPT, [
|
|
"cost" => 10
|
|
]);
|
|
|
|
$result = [
|
|
"password" => $password,
|
|
"password_hash" => $hash
|
|
];
|
|
|
|
echo json_encode($result);
|
|
} catch (Exception $e) {
|
|
|
|
echo json_encode([
|
|
"status" => "error",
|
|
"message" => $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
}
|