diff --git a/public/userarea/apilogic/api-to-temp.php b/public/userarea/apilogic/api-to-temp.php index 82e081d..36ed136 100644 --- a/public/userarea/apilogic/api-to-temp.php +++ b/public/userarea/apilogic/api-to-temp.php @@ -9,131 +9,142 @@ if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } -// Check if JSON was received via POST +// Check if POST request was received if ($_SERVER['REQUEST_METHOD'] === 'POST') { - // Receive JSON from the laboratory - $json_data = file_get_contents('php://input'); + // Receive JSON from the laboratory via a field in the form (e.g., 'json_data') + if (isset($_POST['json_data'])) { + $json_data = $_POST['json_data']; - // Decode JSON for optional validation - $decoded_data = json_decode($json_data, true); + // Decode JSON for optional validation + $decoded_data = json_decode($json_data, true); - // If the JSON is valid - if (json_last_error() === JSON_ERROR_NONE) { - // 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; - } - - $api_key = $decoded_data['key']; - $secret_key = $decoded_data['secret_key']; - $reflab = $decoded_data['reflab']; - - $query = "SELECT * FROM laboratories WHERE reflab = ? AND api_key = ?"; - $stmt = $conn->prepare($query); - $stmt->bind_param("ss", $reflab, $api_key); - $stmt->execute(); - $result = $stmt->get_result(); - - // Controllo se un laboratorio valido è stato trovato con `reflab` e `api_key` - if ($result->num_rows > 0) { - $row = $result->fetch_assoc(); - - // Verifica lo stato del laboratorio - if ($row['status'] !== 'active') { + // If the JSON is valid + if (json_last_error() === JSON_ERROR_NONE) { + // 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" => "Laboratory is inactive." + "message" => "Missing authentication fields (key, secret_key, reflab)." ]); exit; } - // Verifica la chiave segreta utilizzando `password_verify` - if (!password_verify($secret_key, $row['api_secret'])) { - echo json_encode([ - "status" => "error", - "message" => "Invalid secret key." - ]); - exit; - } - } else { - // Verifica se il `reflab` è valido, ma l'`api_key` non corrisponde - $query = "SELECT * FROM laboratories WHERE reflab = ?"; + $api_key = $decoded_data['key']; + $secret_key = $decoded_data['secret_key']; + $reflab = $decoded_data['reflab']; + + $query = "SELECT * FROM laboratories WHERE reflab = ? AND api_key = ?"; $stmt = $conn->prepare($query); - $stmt->bind_param("s", $reflab); + $stmt->bind_param("ss", $reflab, $api_key); $stmt->execute(); $result = $stmt->get_result(); + // Check if a valid laboratory was found with `reflab` and `api_key` if ($result->num_rows > 0) { + $row = $result->fetch_assoc(); + + // Verify the status of the laboratory + if ($row['status'] !== 'active') { + echo json_encode([ + "status" => "error", + "message" => "Laboratory is inactive." + ]); + exit; + } + + // Verify the secret key using `password_verify` + if (!password_verify($secret_key, $row['api_secret'])) { + echo json_encode([ + "status" => "error", + "message" => "Invalid secret key." + ]); + exit; + } + } else { + // Check if the `reflab` is valid, but the `api_key` doesn't match + $query = "SELECT * FROM laboratories WHERE reflab = ?"; + $stmt = $conn->prepare($query); + $stmt->bind_param("s", $reflab); + $stmt->execute(); + $result = $stmt->get_result(); + + if ($result->num_rows > 0) { + echo json_encode([ + "status" => "error", + "message" => "Invalid API key." + ]); + } else { + 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" => "Invalid API key." + "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()) { + // Handle file uploads if they exist + if (!empty($_FILES)) { + include('process_files.php'); // Include file processing logic here + } + + // Set a session variable to notify the report import + $_SESSION['new_report'] = [ + 'report_number' => $report_number, + 'rating' => $rating, + '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, // Product number + "report_number" => $report_number, // Report number + "rating" => $rating, // Report rating + "saved_at" => $saved_at // Save date ]); } else { echo json_encode([ "status" => "error", - "message" => "Invalid reflab." + "message" => "Failed to save data." ]); } - 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()) { - // Set a session variable to notify the report import - $_SESSION['new_report'] = [ - 'report_number' => $report_number, - 'rating' => $rating, - '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, // Product number - "report_number" => $report_number, // Report number - "rating" => $rating, // Report rating - "saved_at" => $saved_at // Save date - ]); + $stmt->close(); } else { + // If the JSON is invalid echo json_encode([ "status" => "error", - "message" => "Failed to save data." + "message" => "Invalid JSON format." ]); } - - $stmt->close(); } else { - // If the JSON is invalid echo json_encode([ "status" => "error", - "message" => "Invalid JSON format." + "message" => "Missing JSON data." ]); } } else { diff --git a/public/userarea/apilogic/process_files.php b/public/userarea/apilogic/process_files.php new file mode 100644 index 0000000..4f90afa --- /dev/null +++ b/public/userarea/apilogic/process_files.php @@ -0,0 +1,56 @@ + $file) { + if ($file['error'] === UPLOAD_ERR_OK) { + // Get original filename and generate a stored filename with UUID as a prefix + $original_filename = $file['name']; + $stored_filename = $uuid . '_' . $original_filename; // Add UUID as prefix + + // Define the full path where the file will be saved + $filepath = $upload_dir . $stored_filename; + + // Move the uploaded file to the specified directory + if (move_uploaded_file($file['tmp_name'], $filepath)) { + // Get the associated comment for the file if it exists + $comment_key = str_replace('file', 'comment', $key); + $file_comment = $_POST[$comment_key] ?? null; + + // Insert file information into the database + $stmt = $conn->prepare("INSERT INTO report_files (uuid, original_filename, stored_filename, filepath, file_comment) VALUES (?, ?, ?, ?, ?)"); + $stmt->bind_param("sssss", $uuid, $original_filename, $stored_filename, $filepath, $file_comment); + + if (!$stmt->execute()) { + echo json_encode([ + "status" => "error", + "message" => "Failed to save file information for $original_filename." + ]); + continue; + } + + echo json_encode([ + "status" => "success", + "message" => "File $original_filename uploaded and information saved." + ]); + } else { + echo json_encode([ + "status" => "error", + "message" => "Failed to move file $original_filename." + ]); + } + } else { + echo json_encode([ + "status" => "error", + "message" => "Error uploading file $original_filename. Error code: " . $file['error'] + ]); + } + } +} diff --git a/public/userarea/apilogic/tempfiles/6729d6481d565_test_report.pdf b/public/userarea/apilogic/tempfiles/6729d6481d565_test_report.pdf new file mode 100644 index 0000000..8b5bf2b Binary files /dev/null and b/public/userarea/apilogic/tempfiles/6729d6481d565_test_report.pdf differ diff --git a/public/userarea/apilogic/tempfiles/6729d67bb1cf1_test_report.pdf b/public/userarea/apilogic/tempfiles/6729d67bb1cf1_test_report.pdf new file mode 100644 index 0000000..8b5bf2b Binary files /dev/null and b/public/userarea/apilogic/tempfiles/6729d67bb1cf1_test_report.pdf differ diff --git a/public/userarea/apilogic/tempfiles/6729d6dc91c6a.pdf b/public/userarea/apilogic/tempfiles/6729d6dc91c6a.pdf new file mode 100644 index 0000000..8b5bf2b Binary files /dev/null and b/public/userarea/apilogic/tempfiles/6729d6dc91c6a.pdf differ diff --git a/public/userarea/apilogic/tempfiles/6729d6dc92908.png b/public/userarea/apilogic/tempfiles/6729d6dc92908.png new file mode 100644 index 0000000..fc54cd5 Binary files /dev/null and b/public/userarea/apilogic/tempfiles/6729d6dc92908.png differ diff --git a/public/userarea/apilogic/tempfiles/6729d83cbcbfc_Progetto senza titolo (9).png b/public/userarea/apilogic/tempfiles/6729d83cbcbfc_Progetto senza titolo (9).png new file mode 100644 index 0000000..fc54cd5 Binary files /dev/null and b/public/userarea/apilogic/tempfiles/6729d83cbcbfc_Progetto senza titolo (9).png differ diff --git a/public/userarea/apilogic/tempfiles/6729d83cbcbfc_test_report.pdf b/public/userarea/apilogic/tempfiles/6729d83cbcbfc_test_report.pdf new file mode 100644 index 0000000..8b5bf2b Binary files /dev/null and b/public/userarea/apilogic/tempfiles/6729d83cbcbfc_test_report.pdf differ diff --git a/public/userarea/products/reportdetails.php b/public/userarea/products/reportdetails.php index f159541..b17e56e 100644 --- a/public/userarea/products/reportdetails.php +++ b/public/userarea/products/reportdetails.php @@ -41,6 +41,23 @@ $stmtParts = $conn->prepare($queryPartsAndResults); $stmtParts->bind_param("i", $idreports); $stmtParts->execute(); $partsAndResults = $stmtParts->get_result(); + +// Query per ottenere i file associati al report +$queryFiles = " + SELECT original_filename, stored_filename, file_comment, filepath + FROM report_files + WHERE uuid = ? + AND (file_comment = 'report' OR file_comment = 'main_product_image')"; +$stmtFiles = $conn->prepare($queryFiles); +$stmtFiles->bind_param("s", $reportDetails['importcode']); +$stmtFiles->execute(); +$filesResult = $stmtFiles->get_result(); + +$files = []; +while ($fileRow = $filesResult->fetch_assoc()) { + $files[$fileRow['file_comment']] = $fileRow; +} + ?> @@ -176,12 +193,128 @@ $partsAndResults = $stmtParts->get_result();