Stabilize legacy Steven API endpoints and add OpenAPI spec

This commit is contained in:
Power BI Dev
2026-05-07 14:50:31 +07:00
parent f1c1f42a4c
commit e7b07a3cfd
17 changed files with 2755 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
<?php
class Server
{
public $host;
public $secretKeyRobot;
public function __construct()
{
$appUrl = rtrim((string) (getenv('APP_URL') ?: 'https://spota.untan.ac.id'), '/');
$this->host = $appUrl;
$this->secretKeyRobot = (string) (getenv('SPOTA_LEGACY_API_SECRET') ?: 'in14d4lahP4ssWordSP0TA!12!');
}
public function getSecretKeyRobot()
{
return $this->secretKeyRobot;
}
public function getAPIUrl()
{
return $this->host.'/API/web';
}
public function getHostUrl()
{
return $this->host;
}
public function getLoginPage()
{
return $this->host;
}
public function getDataTableURL()
{
return $this->host.'/API/datatable';
}
public function getPrintUrl()
{
return $this->host.'/print';
}
public function getBerkasUrl()
{
return $this->host.'/berkas';
}
}

345
steven/conf/function.php Normal file
View File

@@ -0,0 +1,345 @@
<?php
date_default_timezone_set('Asia/Jakarta');
function checkKey($methodName, $keyName)
{
$methodName = trim(strtoupper($methodName));
$error = false;
$msg = '';
switch ($methodName) {
case 'POST':
if (!isset($_POST[$keyName])) {
$error = true;
$msg = 'Param Key Not Found';
}
break;
case 'GET':
if (!isset($_GET[$keyName])) {
$error = true;
$msg = 'Param Key Not Found';
}
break;
case 'FILES':
if (!isset($_FILES[$keyName])) {
$error = true;
$msg = 'Param Key Not Found';
}
break;
default:
$error = true;
$msg = 'Request Method Not Found';
}
if ($error) {
echo json_encode(array('status' => 0, 'message' => $msg));
exit();
}
}
function getHari($date)
{
$timestamp = strtotime($date);
$day = date('D', $timestamp);
$hari = '-';
switch ($day) {
case 0: $hari = 'Minggu'; break;
case 1: $hari = 'Senin'; break;
case 2: $hari = 'Selasa'; break;
case 3: $hari = 'Rabu'; break;
case 4: $hari = 'Kamis'; break;
case 5: $hari = 'Jumat'; break;
case 6: $hari = 'Sabtu'; break;
}
return $hari;
}
function convertHaritoInt($hari)
{
$intHari = 0;
switch ($hari) {
case 'Senin': $intHari = 1; break;
case 'Selasa': $intHari = 2; break;
case 'Rabu': $intHari = 3; break;
case 'Kamis': $intHari = 4; break;
case 'Jumat': $intHari = 5; break;
case 'Sabtu': $intHari = 6; break;
case 'Minggu': $intHari = 0; break;
}
return $intHari;
}
function createToken($id)
{
$x = base64_encode($id);
$time = base64_encode(time());
//php7 keatas
$token = $x.bin2hex(random_bytes(64)).$time;
//dibawah php 7
//$token = $x.bin2hex(openssl_random_pseudo_bytes(64)).$time;
return $token;
}
function getUnauthorizedMessage()
{
return json_encode(
array(
'status' => '0',
'msg' => 'Unauthorized',
)
);
}
function jsonResponse($payload, $statusCode = 200)
{
http_response_code($statusCode);
echo json_encode($payload);
exit();
}
function getRequestJson()
{
static $decoded = null;
static $loaded = false;
if ($loaded) {
return $decoded;
}
$loaded = true;
$raw = file_get_contents('php://input');
if ($raw === false || trim($raw) === '') {
$decoded = [];
return $decoded;
}
$decoded = json_decode($raw, true);
if (!is_array($decoded)) {
$decoded = [];
}
return $decoded;
}
function getRequestValue($key, $default = null)
{
if (isset($_POST[$key])) {
return $_POST[$key];
}
if (isset($_GET[$key])) {
return $_GET[$key];
}
$json = getRequestJson();
if (isset($json[$key])) {
return $json[$key];
}
return $default;
}
function requireRobotSecret()
{
$server = new Server();
$expectedSecret = $server->getSecretKeyRobot();
$providedSecret = getRequestValue('secret');
if ($providedSecret === null || $providedSecret === '') {
$headerKey = isset($_SERVER['HTTP_X_API_KEY']) ? trim((string) $_SERVER['HTTP_X_API_KEY']) : '';
if ($headerKey !== '') {
$providedSecret = $headerKey;
}
}
if (!is_string($providedSecret) || trim($providedSecret) !== $expectedSecret) {
jsonResponse([
'status' => 0,
'msg' => 'Unauthorized',
], 401);
}
}
function requireSessionRole($role)
{
if (session_status() !== PHP_SESSION_ACTIVE) {
session_start();
}
$key = 'login-'.$role;
if (!isset($_SESSION[$key]) || !is_array($_SESSION[$key])) {
jsonResponse([
'status' => false,
'msg' => 'Forbidden',
], 403);
}
return $_SESSION[$key];
}
function getDataNotFoundMessage()
{
return json_encode(
array(
'status' => '0',
'msg' => 'Data Not Found',
)
);
}
function getDataEmptyMessage()
{
return json_encode(
array(
'status' => '0',
'msg' => 'Data cannot be empty',
)
);
}
function getHariText($hari)
{
$hariText = '-';
switch ($hari) {
case '0': $hariText = 'Minggu'; break;
case '1': $hariText = 'Senin'; break;
case '2': $hariText = 'Selasa'; break;
case '3': $hariText = 'Rabu'; break;
case '4': $hariText = 'Kamis'; break;
case '5': $hariText = 'Jumat'; break;
case '6': $hariText = 'Sabtu'; break;
}
return $hariText;
}
function cwUpload($field_name = '', $target_folder = '', $file_name = '', $thumb = false, $thumb_folder = '', $thumb_width = '', $thumb_height = '')
{
//folder path setup
$target_path = $target_folder;
$thumb_path = $thumb_folder;
//file name setup
$filename_err = explode('.', $_FILES[$field_name]['name']);
$filename_err_count = count($filename_err);
$file_ext = $filename_err[$filename_err_count - 1];
if ($file_name != '') {
$fileName = $file_name.'.'.$file_ext;
} else {
$fileName = $_FILES[$field_name]['name'];
}
//upload image path
$upload_image = $target_path.basename($fileName);
//upload image
if (move_uploaded_file($_FILES[$field_name]['tmp_name'], $upload_image)) {
//thumbnail creation
if ($thumb == true) {
$thumbnail = $thumb_path.$fileName;
list($width, $height) = getimagesize($upload_image);
$thumb_create = imagecreatetruecolor($thumb_width, $thumb_height);
switch ($file_ext) {
case 'jpg':
$source = imagecreatefromjpeg($upload_image);
break;
case 'jpeg':
$source = imagecreatefromjpeg($upload_image);
break;
case 'png':
$source = imagecreatefrompng($upload_image);
break;
case 'gif':
$source = imagecreatefromgif($upload_image);
break;
default:
$source = imagecreatefromjpeg($upload_image);
}
imagecopyresized($thumb_create, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
switch ($file_ext) {
case 'jpg' || 'jpeg':
imagejpeg($thumb_create, $thumbnail, 100);
break;
case 'png':
imagepng($thumb_create, $thumbnail, 100);
break;
case 'gif':
imagegif($thumb_create, $thumbnail, 100);
break;
default:
imagejpeg($thumb_create, $thumbnail, 100);
}
}
return $fileName;
} else {
return false;
}
}
function getPeriode($waktu, $before = false)
{
$interval = 5 * 60; //10 menit
$timeStart = strtotime(date('d-m-Y 00:00:00'));
$timeEnd = strtotime(date('d-m-Y 23:59:59'));
$inTime = true;
$i = 1;
$periode = '';
$periodeBefore = '';
while ($inTime) {
$time = $timeStart + ($i * $interval);
$timeBefore = $time - $interval;
$timeNext = $time + $interval;
if (($waktu >= $time) && ($waktu < $timeNext)) {
$periode = date('H:i:s', $time).' - '.date('H:i:s', $timeNext);
$periodeBefore = date('H:i:s', $timeBefore).' - '.date('H:i:s', $time);
$inTime = false;
}
//stop looing kalau belum jam start
if ($time < $timeStart) {
$inTime = false;
}
//stop looping kalau udh lewat
if ($time > $timeEnd) {
$inTime = false;
}
//echo $time.'<br>';
//array_push($arrTime,$time);
++$i;
}
if ($before) {
return array(
'now' => $periode,
'before' => $periodeBefore,
);
} else {
return $periode;
}
}
function utf8ize($d)
{
if (is_array($d)) {
foreach ($d as $k => $v) {
$d[$k] = utf8ize($v);
}
} elseif (is_string($d)) {
return utf8_encode($d);
}
return $d;
}