setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); return $pdo; } catch (PDOException $e) { error_log('Erro ao conectar ao banco: ' . $e->getMessage()); throw new Exception('Erro ao conectar ao banco de dados'); } } // ============================================================ // FUNÇÕES AUXILIARES // ============================================================ function get_ftp_connection() { $conn = ftp_connect(FTP_HOST, FTP_PORT, 30); if (!$conn) { throw new Exception('Não foi possível conectar ao FTP: ' . FTP_HOST); } if (!ftp_login($conn, FTP_USER, FTP_PASS)) { ftp_close($conn); throw new Exception('Falha no login FTP para ' . FTP_HOST); } ftp_pasv($conn, true); return $conn; } function sanitize($value) { return htmlspecialchars($value, ENT_QUOTES, 'UTF-8'); } function get_table_fields($table) { $fields = [ 'users' => ['user_id', 'uuid', 'email', 'full_name', 'company_name', 'phone', 'account_status', 'account_tier', 'created_at', 'last_login'], 'subscription_plans' => ['plan_id', 'plan_code', 'plan_name', 'description', 'max_cameras', 'max_users', 'max_storage_gb', 'price_monthly', 'price_yearly', 'is_active'], 'user_subscriptions' => ['subscription_id', 'user_id', 'plan', 'expires_at'], 'user_cameras' => ['camera_id', 'user_id', 'camera_name', 'stream_url', 'is_active', 'updated_at'], 'detection_logs' => ['log_id', 'user_id', 'camera_id', 'detection_type', 'confidence', 'zone_name', 'detection_timestamp', 'created_at'], 'alert_configurations' => ['alert_config_id', 'user_id', 'alert_type', 'is_enabled', 'send_email', 'send_push', 'cooldown_minutes', 'created_at'], 'notifications' => ['notification_id', 'user_id', 'notification_type', 'title', 'message', 'is_read', 'created_at'], 'payments' => ['payment_id', 'subscription_id', 'user_id', 'amount', 'currency', 'status', 'payment_gateway', 'paid_at', 'created_at'], 'coupons' => ['coupon_id', 'code', 'description', 'discount_type', 'discount_value', 'valid_from', 'valid_until', 'max_uses', 'uses_count', 'is_active'], 'api_keys' => ['api_key_id', 'user_id', 'key_name', 'api_key', 'permissions', 'is_active', 'last_used_at', 'created_at'], 'webhooks' => ['webhook_id', 'user_id', 'name', 'url', 'events', 'is_active', 'created_at'], 'usage_metrics' => ['metric_id', 'user_id', 'metric_date', 'detection_count', 'api_requests', 'storage_used_mb', 'created_at'], 'user_configurations' => ['config_id', 'user_id', 'config_name', 'config_data', 'updated_at'], 'config_history' => ['history_id', 'config_id', 'user_id', 'change_type', 'changed_by', 'created_at'], 'system_logs' => ['log_id', 'user_id', 'log_level', 'message', 'created_at'], 'account_recovery' => ['recovery_id', 'user_id', 'recovery_type', 'recovery_value', 'is_verified', 'created_at'], 'user_sessions' => ['session_id', 'user_id', 'session_token', 'created_at', 'expires_at'] ]; return $fields[$table] ?? []; } // ============================================================ // PROCESSAR AÇÕES // ============================================================ $message = ''; $messageType = ''; $table = $_GET['table'] ?? 'users'; $action = $_POST['action'] ?? $_GET['action'] ?? ''; try { $pdo = get_db_connection(); // Processar ações do formulário if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) { $action = $_POST['action']; switch ($action) { case 'insert': $fields = get_table_fields($table); $setParts = []; $params = []; // Montar consulta de inserção foreach ($fields as $field) { if (isset($_POST[$field]) && $field !== 'user_id' && $field !== 'created_at' && $field !== 'updated_at') { $setParts[] = $field . ' = :' . $field; $params[':' . $field] = $_POST[$field]; } } if (!empty($setParts)) { $sql = "INSERT INTO $table SET " . implode(', ', $setParts); $stmt = $pdo->prepare($sql); $stmt->execute($params); $message = 'Registro adicionado com sucesso!'; $messageType = 'success'; } break; case 'update': $id = $_POST['id'] ?? $_GET['id'] ?? 0; $fields = get_table_fields($table); $setParts = []; $params = []; $primaryKey = $fields[0] ?? 'id'; foreach ($fields as $field) { if (isset($_POST[$field]) && $field !== $primaryKey && $field !== 'created_at' && $field !== 'updated_at') { $setParts[] = $field . ' = :' . $field; $params[':' . $field] = $_POST[$field]; } } if (!empty($setParts) && $id > 0) { $params[':id'] = $id; $sql = "UPDATE $table SET " . implode(', ', $setParts) . " WHERE $primaryKey = :id"; $stmt = $pdo->prepare($sql); $stmt->execute($params); $message = 'Registro atualizado com sucesso!'; $messageType = 'success'; } break; case 'delete': $id = $_GET['id'] ?? 0; if ($id > 0) { $fields = get_table_fields($table); $primaryKey = $fields[0] ?? 'id'; $sql = "DELETE FROM $table WHERE $primaryKey = :id"; $stmt = $pdo->prepare($sql); $stmt->execute([':id' => $id]); $message = 'Registro excluído com sucesso!'; $messageType = 'success'; } break; } } // Buscar dados da tabela $fields = get_table_fields($table); $primaryKey = $fields[0] ?? 'id'; // Construir query com filtros $whereConditions = []; $params = []; foreach ($fields as $field) { $filterKey = 'filter_' . $field; $filterTypeKey = 'filter_type_' . $field; if (isset($_GET[$filterKey]) && $_GET[$filterKey] !== '') { $value = $_GET[$filterKey]; $type = $_GET[$filterTypeKey] ?? 'contains'; switch ($type) { case 'exact': $whereConditions[] = "$field = :" . $field; $params[':' . $field] = $value; break; case 'contains': $whereConditions[] = "$field LIKE :" . $field; $params[':' . $field] = '%' . $value . '%'; break; case 'starts': $whereConditions[] = "$field LIKE :" . $field; $params[':' . $field] = $value . '%'; break; case 'ends': $whereConditions[] = "$field LIKE :" . $field; $params[':' . $field] = '%' . $value; break; case 'empty': $whereConditions[] = "($field IS NULL OR $field = '')"; break; case 'not_empty': $whereConditions[] = "($field IS NOT NULL AND $field != '')"; break; } } } $whereClause = !empty($whereConditions) ? 'WHERE ' . implode(' AND ', $whereConditions) : ''; $sql = "SELECT * FROM $table $whereClause ORDER BY $primaryKey DESC LIMIT 100"; $stmt = $pdo->prepare($sql); $stmt->execute($params); $rows = $stmt->fetchAll(); } catch (Exception $e) { $message = 'Erro: ' . $e->getMessage(); $messageType = 'error'; $rows = []; } ?> Gerenciar Tabelas - PipView

📊 Gerenciar Tabelas

PipView - Gerenciamento de Banco de Dados
⬅ Voltar
📌 Banco: | 📋 Tabela: | 📄 Registros:

🔍 Filtros

🗑️ Limpar
Ações
📭 Nenhum registro encontrado
NULL'; else echo sanitize($value); ?>
PipView © - Todos os direitos reservados