%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /home2/rezakur1/so.rezakurnia.com/native/
Upload File :
Create Path :
Current File : //home2/rezakur1/so.rezakurnia.com/native/api-master-item.php

<?php

header('Content-Type: application/json');

require_once 'db_connection.php';
require_once 'api_config.php';

/**
 * ==========================================
 * ITEM MASTER API
 * ==========================================
 *
 * Header:
 * X-API-KEY: YOUR_SECRET_KEY
 *
 * Endpoint:
 * file.php?action=syncItems&last_id=100
 *
 */

// ==============================
// API KEY VALIDATION
// ==============================

$apiKey = $_SERVER['HTTP_X_API_KEY'] ?? '';

if ($apiKey !== API_KEY) {
    http_response_code(401);

    echo json_encode([
        'status' => false,
        'message' => 'Hayo, kamu bukan siapa-siapa, JANGAN COBA-CUBA MASUK KE SINI YA! :D'
    ]);

    exit;
}

// ==============================
// ACTION ROUTER
// ==============================

$action = $_GET['action'] ?? '';

switch ($action) {

    case 'syncItems':
        syncItems($conn);
        break;

    default:
        http_response_code(404);

        echo json_encode([
            'status' => false,
            'message' => 'Action tidak ditemukan'
        ]);
        break;
}

$conn->close();


// ==============================
// SYNC ITEMS
// ==============================

function syncItems(mysqli $conn): void
{
    $lastId = isset($_GET['last_id'])
        ? (int) $_GET['last_id']
        : 0;

    $limit = 1000;

    $sql = "
        SELECT
            id,
            item_code,
            ukuran,
            U_Surface
        FROM item_master
        WHERE id > ?
        ORDER BY id ASC
        LIMIT ?
    ";

    $stmt = $conn->prepare($sql);

    if (!$stmt) {
        http_response_code(500);

        echo json_encode([
            'status' => false,
            'message' => 'Prepare statement failed'
        ]);

        return;
    }

    $stmt->bind_param("ii", $lastId, $limit);
    $stmt->execute();

    $result = $stmt->get_result();

    $data = [];

    while ($row = $result->fetch_assoc()) {
        $data[] = [
            'id' => (int) $row['id'],
            'item_code' => $row['item_code'] ?? '',
            'ukuran' => $row['ukuran'] ?? '',
            'U_Surface' => $row['U_Surface'] ?? '',
        ];
    }

    $lastServerId = $lastId;

    if (!empty($data)) {
        $lastServerId = end($data)['id'];
    }

    echo json_encode([
        'status' => true,
        'has_update' => !empty($data),
        'count' => count($data),
        'last_id_client' => $lastId,
        'last_id_server' => $lastServerId,
        'data' => $data
    ]);

    $stmt->close();
}

Zerion Mini Shell 1.0