requireAuth(); // Check if this is a POST request if ($_SERVER['REQUEST_METHOD'] !== 'POST') { http_response_code(405); echo json_encode([ 'success' => false, 'error' => 'Method not allowed' ]); exit; } // Get request body $input = json_decode(file_get_contents('php://input'), true); if (!isset($input['boxId']) || empty($input['boxId'])) { http_response_code(400); echo json_encode([ 'success' => false, 'error' => 'Box ID is required' ]); exit; } $boxId = trim($input['boxId']); // Validate Box ID format (should be numeric) if (!is_numeric($boxId)) { http_response_code(400); echo json_encode([ 'success' => false, 'error' => 'Invalid Box ID format. Box ID must be numeric.' ]); exit; } // Initialize Box service and validate $boxService = new BoxService(); $result = $boxService->validateBoxId($boxId); if (!$result['valid']) { http_response_code(404); echo json_encode([ 'success' => false, 'error' => $result['error'] ?? 'Box ID not found or inaccessible' ]); exit; } // Return successful validation with data echo json_encode([ 'success' => true, 'data' => [ 'boxId' => $boxId, 'folderName' => $result['folderInfo']['name'], 'masterCampaignNumber' => $result['grandparent']['name'] ?? 'N/A', 'masterCampaignId' => $result['grandparent']['id'] ?? null, 'contents' => [ 'total' => $result['contents']['total'], 'folders' => $result['contents']['folders'], 'files' => $result['contents']['files'] ] ] ]); } catch (Exception $e) { // Log error (in production, log to file instead of returning details) error_log('Box validation error: ' . $e->getMessage()); http_response_code(500); echo json_encode([ 'success' => false, 'error' => 'Server error occurred while validating Box ID', 'debug' => $e->getMessage() // Remove in production ]); }