diff --git a/public/userarea/apilogic/add_lab.php b/public/userarea/apilogic/add_lab.php
new file mode 100644
index 0000000..82d6032
--- /dev/null
+++ b/public/userarea/apilogic/add_lab.php
@@ -0,0 +1,38 @@
+prepare("SELECT idlab FROM laboratories WHERE reflab = ?");
+ $check_query->bind_param("s", $reflab);
+ $check_query->execute();
+ $check_query->store_result();
+ } while ($check_query->num_rows > 0);
+ $check_query->close();
+
+ // Generate API Key and Secret Key
+ $api_key = bin2hex(random_bytes(16));
+ $api_secret = bin2hex(random_bytes(16));
+
+ // Hash secret key before saving
+ $hashed_secret = password_hash($api_secret, PASSWORD_BCRYPT);
+
+ $stmt = $conn->prepare("INSERT INTO laboratories (name, reflab, country, api_key, api_secret) VALUES (?, ?, ?, ?, ?)");
+ $stmt->bind_param("sssss", $name, $reflab, $country, $api_key, $hashed_secret);
+
+ if ($stmt->execute()) {
+ // Show API Key, Secret Key, and Reflab to the user
+ echo json_encode(["status" => "success", "message" => "Laboratory added successfully.", "reflab" => $reflab, "api_key" => $api_key, "api_secret" => $api_secret]);
+ } else {
+ echo json_encode(["status" => "error", "message" => "Failed to add laboratory."]);
+ }
+ $stmt->close();
+}
+$conn->close();
diff --git a/public/userarea/apilogic/api-to-temp.php b/public/userarea/apilogic/api-to-temp.php
index d00e1e3..b47c487 100644
--- a/public/userarea/apilogic/api-to-temp.php
+++ b/public/userarea/apilogic/api-to-temp.php
@@ -1,56 +1,104 @@
connect_error) {
die("Connection failed: " . $conn->connect_error);
}
-// Controlla se il JSON è stato ricevuto tramite POST
+// Check if JSON was received via POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- // Riceve il JSON dal laboratorio
+ // Receive JSON from the laboratory
$json_data = file_get_contents('php://input');
- // Decodifica il JSON per la validazione (facoltativa)
+ // Decode JSON for optional validation
$decoded_data = json_decode($json_data, true);
- // Se il JSON è valido
+ // If the JSON is valid
if (json_last_error() === JSON_ERROR_NONE) {
- // Genera un UUID per identificare univocamente il record
- $uuid = uniqid(); // Alternativamente puoi usare UUID() in MySQL
+ // Authenticate using key, secret_key, and reflab
+ if (!isset($decoded_data['key']) || !isset($decoded_data['secret_key']) || !isset($decoded_data['reflab'])) {
+ echo json_encode([
+ "status" => "error",
+ "message" => "Missing authentication fields (key, secret_key, reflab)."
+ ]);
+ exit;
+ }
- // ID del laboratorio dal quale proviene il JSON (puoi aggiungere autenticazione)
- $lab_id = isset($_POST['lab_id']) ? $_POST['lab_id'] : 'Unknown Lab'; // Modifica a seconda della tua logica
+ $api_key = $decoded_data['key'];
+ $secret_key = $decoded_data['secret_key'];
+ $reflab = $decoded_data['reflab'];
- // Estrai alcune informazioni dal JSON
- $product_refnumber = $decoded_data['product']['products_refnumber']; // Numero prodotto
- $report_number = $decoded_data['product']['reports'][0]['reportsNumberLab']; // Numero report
- $rating = $decoded_data['product']['reports'][0]['reportsRating']; // Rating del report (es. Pass/Fail)
- $saved_at = date("Y-m-d H:i:s"); // Data del salvataggio
+ // For testing purposes, use hardcoded credentials
+ $valid_api_key = 'api_key_123';
+ $valid_secret_key = 'api_secret_123';
+ $valid_reflab = 'REF001';
- // Query per inserire i dati nella tabella temp_json_queue
+ if ($api_key !== $valid_api_key) {
+ echo json_encode([
+ "status" => "error",
+ "message" => "Invalid API key."
+ ]);
+ exit;
+ }
+
+ if ($secret_key !== $valid_secret_key) {
+ echo json_encode([
+ "status" => "error",
+ "message" => "Invalid secret key."
+ ]);
+ exit;
+ }
+
+ if ($reflab !== $valid_reflab) {
+ echo json_encode([
+ "status" => "error",
+ "message" => "Invalid reflab."
+ ]);
+ exit;
+ }
+
+ // Generate a UUID to uniquely identify the record
+ $uuid = uniqid(); // Alternatively, use UUID() in MySQL
+
+ // Extract some information from JSON
+ if (!isset($decoded_data['product']['products_refnumber'])) {
+ echo json_encode([
+ "status" => "error",
+ "message" => "Missing product reference number."
+ ]);
+ exit;
+ }
+
+ $product_refnumber = $decoded_data['product']['products_refnumber']; // Product number
+ $report_number = $decoded_data['product']['reports'][0]['reportsNumberLab'] ?? null; // Report number
+ $rating = $decoded_data['product']['reports'][0]['reportsRating'] ?? null; // Report rating (e.g., Pass/Fail)
+ $saved_at = date("Y-m-d H:i:s"); // Save date
+
+ // Query to insert data into the temp_json_queue table
$stmt = $conn->prepare("INSERT INTO temp_json_queue (uuid, lab_id, json_data) VALUES (?, ?, ?)");
+ $lab_id = 1; // Set lab_id to a fixed value for testing purposes
$stmt->bind_param("sss", $uuid, $lab_id, $json_data);
if ($stmt->execute()) {
- // Imposta una variabile di sessione per notificare l'importazione del report
+ // Set a session variable to notify the report import
$_SESSION['new_report'] = [
'report_number' => $report_number,
'rating' => $rating,
- 'timestamp' => time() // Puoi usare un timestamp per gestire la scadenza della notifica
+ 'timestamp' => time() // You can use a timestamp to manage the expiration of the notification
];
echo json_encode([
"status" => "success",
"message" => "Data successfully saved.",
"uuid" => $uuid,
- "product_refnumber" => $product_refnumber, // Numero del prodotto
- "report_number" => $report_number, // Numero del report
- "rating" => $rating, // Rating del report
- "saved_at" => $saved_at // Data del salvataggio
+ "product_refnumber" => $product_refnumber, // Product number
+ "report_number" => $report_number, // Report number
+ "rating" => $rating, // Report rating
+ "saved_at" => $saved_at // Save date
]);
} else {
echo json_encode([
@@ -61,7 +109,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$stmt->close();
} else {
- // Se il JSON è invalido
+ // If the JSON is invalid
echo json_encode([
"status" => "error",
"message" => "Invalid JSON format."
@@ -74,5 +122,5 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
]);
}
-// Chiude la connessione al database
+// Close the database connection
$conn->close();
diff --git a/public/userarea/apilogic/delete_lab.php b/public/userarea/apilogic/delete_lab.php
new file mode 100644
index 0000000..9a78b43
--- /dev/null
+++ b/public/userarea/apilogic/delete_lab.php
@@ -0,0 +1,19 @@
+prepare("DELETE FROM laboratories WHERE idlab = ?");
+ $stmt->bind_param("i", $idlab);
+
+ if ($stmt->execute()) {
+ echo json_encode(["status" => "success", "message" => "Laboratory deleted successfully."]);
+ } else {
+ echo json_encode(["status" => "error", "message" => "Failed to delete laboratory."]);
+ }
+ $stmt->close();
+}
+$conn->close();
diff --git a/public/userarea/apilogic/get_lab.php b/public/userarea/apilogic/get_lab.php
new file mode 100644
index 0000000..796edde
--- /dev/null
+++ b/public/userarea/apilogic/get_lab.php
@@ -0,0 +1,27 @@
+prepare("SELECT * FROM laboratories WHERE idlab = ?");
+ $stmt->bind_param("i", $idlab);
+ $stmt->execute();
+ $result = $stmt->get_result();
+ $lab = $result->fetch_assoc();
+
+ if ($lab) {
+ echo json_encode([
+ "status" => "success",
+ "name" => $lab['name'],
+ "country" => $lab['country'],
+ "status" => $lab['status']
+ ]);
+ } else {
+ echo json_encode(["status" => "error", "message" => "Laboratory not found"]);
+ }
+ $stmt->close();
+}
+$conn->close();
diff --git a/public/userarea/apilogic/laboratories.php b/public/userarea/apilogic/laboratories.php
new file mode 100644
index 0000000..828dce6
--- /dev/null
+++ b/public/userarea/apilogic/laboratories.php
@@ -0,0 +1,391 @@
+
+connect_error) {
+ die("Connection failed: " . $conn->connect_error);
+}
+?>
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Laboratories Management
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ | ID |
+ Name |
+ Reflab |
+ Country |
+ Status |
+ API Key |
+ Actions |
+
+
+
+ query($query);
+ if ($result->num_rows > 0) {
+ while ($row = $result->fetch_assoc()) {
+ echo "";
+ echo "| " . $row['idlab'] . " | ";
+ echo "" . $row['name'] . " | ";
+ echo "" . $row['reflab'] . " | ";
+ echo "" . $row['country'] . " | ";
+ echo "" . $row['status'] . " | ";
+ echo "" . substr($row['api_key'], 0, 5) . "*****" . " | ";
+ echo "
+
+
+
+ | ";
+ echo "
";
+ }
+ } else {
+ echo "| No laboratories found |
";
+ }
+ ?>
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/public/userarea/apilogic/load_laboratories.php b/public/userarea/apilogic/load_laboratories.php
new file mode 100644
index 0000000..18b2148
--- /dev/null
+++ b/public/userarea/apilogic/load_laboratories.php
@@ -0,0 +1,29 @@
+query($query);
+
+if ($result->num_rows > 0) {
+ while ($row = $result->fetch_assoc()) {
+ echo "";
+ echo "| " . $row['idlab'] . " | ";
+ echo "" . $row['name'] . " | ";
+ echo "" . $row['reflab'] . " | ";
+ echo "" . $row['country'] . " | ";
+ echo "" . $row['status'] . " | ";
+ echo "" . substr($row['api_key'], 0, 5) . "*****" . " | ";
+ echo "
+
+
+
+ | ";
+ echo "
";
+ }
+} else {
+ echo "| No laboratories found |
";
+}
+
+$conn->close();
diff --git a/public/userarea/apilogic/regenerate_keys.php b/public/userarea/apilogic/regenerate_keys.php
new file mode 100644
index 0000000..cb941d2
--- /dev/null
+++ b/public/userarea/apilogic/regenerate_keys.php
@@ -0,0 +1,27 @@
+prepare("UPDATE laboratories SET api_key = ?, api_secret = ? WHERE idlab = ?");
+ $stmt->bind_param("ssi", $new_api_key, $hashed_secret, $idlab);
+
+ if ($stmt->execute()) {
+ // Show success message and display the new keys
+ echo json_encode(["status" => "success", "message" => "Keys regenerated successfully.", "api_key" => $new_api_key, "api_secret" => $new_api_secret]);
+ } else {
+ echo json_encode(["status" => "error", "message" => "Failed to regenerate keys."]);
+ }
+ $stmt->close();
+}
+$conn->close();
diff --git a/public/userarea/apilogic/update_lab.php b/public/userarea/apilogic/update_lab.php
new file mode 100644
index 0000000..e590a00
--- /dev/null
+++ b/public/userarea/apilogic/update_lab.php
@@ -0,0 +1,37 @@
+prepare("UPDATE laboratories SET name = ?, country = ?, status = ? WHERE idlab = ?");
+ $stmt->bind_param("sssi", $name, $country, $status, $idlab);
+
+ // Esegui la query e verifica il risultato
+ if ($stmt->execute()) {
+ echo json_encode(["status" => "success", "message" => "Laboratory updated successfully."]);
+ } else {
+ // Mostra l'errore se la query fallisce
+ echo json_encode([
+ "status" => "error",
+ "message" => "Failed to update laboratory.",
+ "error" => $stmt->error, // Mostra il messaggio di errore
+ "errno" => $stmt->errno // Mostra il codice di errore
+ ]);
+ }
+
+ $stmt->close();
+} else {
+ echo json_encode(["status" => "error", "message" => "Invalid request method"]);
+ exit;
+}
+
+$conn->close();