diff --git a/launcher.sh b/launcher.sh index 74ca5fa..3b88734 100755 --- a/launcher.sh +++ b/launcher.sh @@ -80,24 +80,6 @@ run_module() { read -p "Press Enter to continue..." } -############################################################################# -# TERMINAL INPUT HELPER -############################################################################# - -# Safe read that handles both interactive and piped scenarios -safe_read() { - local prompt="$1" - local varname="$2" - - if [ -t 0 ]; then - # Terminal available, normal read - read -p "$prompt" "$varname" - else - # No terminal (piped stdin), accept input or skip - read -p "$prompt" "$varname" 2>/dev/null || eval "$varname=''" - fi -} - ############################################################################# # SYSTEM INFO DISPLAY (Quick View) ############################################################################# diff --git a/lib/reference-db.sh b/lib/reference-db.sh index 33d19df..f9edf2d 100755 --- a/lib/reference-db.sh +++ b/lib/reference-db.sh @@ -198,15 +198,30 @@ build_databases_section() { # Build MySQL command with credentials if needed local mysql_cmd="mysql" + local plesk_password="" if [ "$SYS_CONTROL_PANEL" = "plesk" ] && [ -f /etc/psa/.psa.shadow ]; then - export MYSQL_PWD=$(cat /etc/psa/.psa.shadow) - mysql_cmd="mysql -uadmin" + plesk_password=$(cat /etc/psa/.psa.shadow) + # DO NOT export password - keep it in variable only fi - local total_dbs=$($mysql_cmd -Ns -e "SHOW DATABASES" 2>/dev/null | grep -v "^information_schema$\|^mysql$\|^performance_schema$\|^sys$" | wc -l) + # Query databases - set MYSQL_PWD only for this command + local total_dbs + if [ -n "$plesk_password" ]; then + total_dbs=$(MYSQL_PWD="$plesk_password" mysql -u admin -Ns -e "SHOW DATABASES" 2>/dev/null | grep -v "^information_schema$\|^mysql$\|^performance_schema$\|^sys$" | wc -l) + else + total_dbs=$(mysql -Ns -e "SHOW DATABASES" 2>/dev/null | grep -v "^information_schema$\|^mysql$\|^performance_schema$\|^sys$" | wc -l) + fi local current=0 # Use process substitution instead of pipe to avoid subshell shadowing (fixes current variable loss) + # Get database list - set MYSQL_PWD only for this command + local databases + if [ -n "$plesk_password" ]; then + databases=$(MYSQL_PWD="$plesk_password" mysql -u admin -Ns -e "SHOW DATABASES" 2>/dev/null | grep -v "^information_schema$\|^mysql$\|^performance_schema$\|^sys$") + else + databases=$(mysql -Ns -e "SHOW DATABASES" 2>/dev/null | grep -v "^information_schema$\|^mysql$\|^performance_schema$\|^sys$") + fi + while IFS= read -r db; do [ -z "$db" ] && continue current=$((current + 1)) @@ -215,21 +230,35 @@ build_databases_section() { local owner=$(get_database_owner "$db") local domain=$(get_database_domain "$db") - local size_mb=$($mysql_cmd -Ns -e "SELECT ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) - FROM information_schema.TABLES - WHERE table_schema=\`$db\`" 2>/dev/null) + # Escape single quotes in database name for SQL safety + local db_escaped="${db//\'/\'\'}" + + # Query database size - set MYSQL_PWD only for this command + local size_mb + if [ -n "$plesk_password" ]; then + size_mb=$(MYSQL_PWD="$plesk_password" mysql -u admin -Ns -e "SELECT ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) + FROM information_schema.TABLES + WHERE table_schema='$db_escaped'" 2>/dev/null) + else + size_mb=$(mysql -Ns -e "SELECT ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) + FROM information_schema.TABLES + WHERE table_schema='$db_escaped'" 2>/dev/null) + fi [ -z "$size_mb" ] && size_mb=0 - local table_count=$($mysql_cmd -Ns "$db" -e "SHOW TABLES" 2>/dev/null | wc -l) + # Query table count - set MYSQL_PWD only for this command + local table_count + if [ -n "$plesk_password" ]; then + table_count=$(MYSQL_PWD="$plesk_password" mysql -u admin -Ns "$db" -e "SHOW TABLES" 2>/dev/null | wc -l) + else + table_count=$(mysql -Ns "$db" -e "SHOW TABLES" 2>/dev/null | wc -l) + fi echo "DB|$db|$owner|$domain|$size_mb|$table_count" >> "$SYSREF_DB" - done < <($mysql_cmd -Ns -e "SHOW DATABASES" 2>/dev/null | grep -v "^information_schema$\|^mysql$\|^performance_schema$\|^sys$") + done <<< "$databases" finish_progress echo "" >> "$SYSREF_DB" - - # Clean up password environment variable - unset MYSQL_PWD } # Check domain HTTP/HTTPS status codes