#!/usr/bin/env bash set -euo pipefail # Comprobar que se ejecuta como root if [ "$(id -u)" -ne 0 ]; then echo "Ejecuta este script como root (sudo)." exit 1 fi # Variables WWW_DIR="/var/www/html/CheckPHP" PHP_FILE="$WWW_DIR/CheckKey.php" DATABASE="$WWW_DIR/database.txt" MESSAGE="$WWW_DIR/message.txt" USED_LOGS="$WWW_DIR/used_logs.txt" echo "==> Actualizando paquetes e instalando apache2, php y libapache2-mod-php..." apt-get update apt-get install -y apache2 php libapache2-mod-php echo "==> Creando carpeta $WWW_DIR y archivos necesarios..." mkdir -p "$WWW_DIR" touch "$DATABASE" "$MESSAGE" "$USED_LOGS" echo "==> Escribiendo el archivo PHP (no se modifica el código que proporcionaste)..." cat > "$PHP_FILE" <<'PHP' NOTIFICACIÓN DE USO 🔔\n\n"; $mensaje .= "✅ Tu Key ha sido activada.\n"; $mensaje .= "👤 Nombre: $nombre\n"; $mensaje .= "🔑 Key: $key\n"; $mensaje .= "🌐 IP Cliente: $ip\n"; $mensaje .= "📅 Fecha: " . date("Y-m-d H:i:s"); $url = "https://api.telegram.org/bot$token/sendMessage"; $data = [ 'chat_id' => $chat_id, 'text' => $mensaje, 'parse_mode' => 'HTML' ]; $options = [ 'http' => [ 'header' => "Content-type: application/x-www-form-urlencoded\r\n", 'method' => 'POST', 'content' => http_build_query($data), ], ]; $context = stream_context_create($options); file_get_contents($url, false, $context); } foreach ($lineas as $linea) { // Formato: KEY | NOMBRE_TELEGRAM | FECHA | ID_TELEGRAM $datos = explode('|', trim($linea)); if (count($datos) < 3) continue; $key_db = $datos[0]; $usuario_db = $datos[1]; // Aquí viene el Nombre de Telegram $fecha_exp = $datos[2]; $telegram_id = isset($datos[3]) ? $datos[3] : ""; if ($key_db == $key_recibida) { $fecha_actual = date("Y-m-d"); if ($fecha_actual <= $fecha_exp) { $status = "clave_valida"; // 1. Guardar Log de historial $registro = "$fecha_hora | IP: $ip_cliente | Nombre: $usuario_db | Key: $key_db\n"; file_put_contents($history_log, $registro, FILE_APPEND); // 2. GUARDAR EL NOMBRE EN message.txt // Esto sobrescribe el archivo con el nombre del usuario actual file_put_contents($message_file, $usuario_db); // 3. Enviar notificación a Telegram if (!empty($telegram_id)) { enviar_notificacion($bot_token, $telegram_id, $key_db, $ip_cliente, $usuario_db); } // La línea NO se añade a $nuevas_lineas, por lo tanto se borra de la DB } else { $status = "clave_expirada"; } } else { $nuevas_lineas[] = $linea; } } // Reescribir archivo (borrando la usada) if ($status == "clave_valida") { $contenido_final = implode("\n", $nuevas_lineas); if (!empty($contenido_final)) { $contenido_final .= "\n"; } file_put_contents($database, $contenido_final); } echo $status; ?> PHP echo "==> Aplicando permisos y cambiando propietario..." chmod -R 777 "$WWW_DIR" chown -R www-data:www-data "$WWW_DIR" echo "==> Reiniciando Apache..." if command -v systemctl >/dev/null 2>&1; then systemctl restart apache2 else service apache2 restart fi echo "==> Instalación completada." echo "Archivos creados en: $WWW_DIR" echo "Asegúrate de que tu token y datos en CheckKey.php son correctos."