isLoggedIn()) { throw new Exception('Unauthorized'); } $selection_id = isset($_GET['id']) ? (int)$_GET['id'] : 0; if (!$selection_id) { throw new Exception('Selection ID is required'); } // Read progress from the temp file created by the worker $progress_file = sys_get_temp_dir() . '/syndia_progress_' . $selection_id . '.json'; if (!file_exists($progress_file)) { // If progress file doesn't exist, check if process is still starting echo json_encode([ 'success' => true, 'progress' => 0, 'processed' => 0, 'success_count' => 0, 'error_count' => 0, 'total' => 0, 'status' => 'Starting...', 'completed' => false, 'remaining' => 0 ]); exit; } $progress_data = json_decode(file_get_contents($progress_file), true); if (!$progress_data) { throw new Exception('Invalid progress data'); } // Calculate remaining count $remaining = max(0, ($progress_data['total'] ?? 0) - ($progress_data['processed'] ?? 0)); // Return progress data echo json_encode([ 'success' => true, 'progress' => $progress_data['progress'] ?? 0, 'processed' => $progress_data['processed'] ?? 0, 'success_count' => $progress_data['success'] ?? 0, 'error_count' => $progress_data['error'] ?? 0, 'total' => $progress_data['total'] ?? 0, 'status' => $progress_data['status'] ?? 'Processing...', 'completed' => $progress_data['completed'] ?? false, 'remaining' => $remaining, 'timestamp' => $progress_data['timestamp'] ?? time() ]); } catch (Exception $e) { error_log("Check generation progress error: " . $e->getMessage()); echo json_encode([ 'success' => false, 'error' => $e->getMessage(), 'progress' => 0, 'processed' => 0, 'success_count' => 0, 'error_count' => 0, 'total' => 0, 'status' => 'Error: ' . $e->getMessage(), 'completed' => false, 'remaining' => 0 ]); } ?>