diff --git a/modules/performance/php-optimizer.sh b/modules/performance/php-optimizer.sh index 51a6dca..50d426a 100755 --- a/modules/performance/php-optimizer.sh +++ b/modules/performance/php-optimizer.sh @@ -118,17 +118,25 @@ select_domain() { done echo "" - read -p "Select domain number (or 'q' to cancel): " selection + cecho "${CYAN}─────────────────────────────────────────────────────────────────────${NC}" - if [[ "$selection" == "q" ]]; then - return 1 - fi + # Validate domain selection with retry loop + while true; do + read -p "Select domain number (or 'q' to cancel): " selection - if ! [[ "$selection" =~ ^[0-9]+$ ]] || [ "$selection" -lt 1 ] || [ "$selection" -gt ${#domains[@]} ]; then - cecho "${RED}Invalid selection${NC}" - sleep 2 - return 1 - fi + if [[ "$selection" == "q" ]]; then + return 1 + fi + + if ! [[ "$selection" =~ ^[0-9]+$ ]] || [ "$selection" -lt 1 ] || [ "$selection" -gt ${#domains[@]} ]; then + echo "" + cecho "${RED}Invalid selection. Please enter a number 1-${#domains[@]}${NC}" + echo "" + continue + fi + + break + done # Return selected domain and username local selected_domain="${domains[$((selection - 1))]}" @@ -178,9 +186,8 @@ analyze_all_domains() { echo "" cecho "${YELLOW}This will analyze PHP configuration for ALL domains...${NC}" echo "" - read -p "Continue? (y/n): " confirm - if [[ ! "$confirm" =~ ^[Yy]$ ]]; then + if ! confirm "Continue?"; then return fi @@ -552,14 +559,30 @@ optimize_domain() { [ "${opt_available[opcache]}" = "true" ] && cecho " ${GREEN}2${NC}) Apply OPcache optimization only" cecho " ${RED}n${NC}) Cancel - don't apply any changes" echo "" - read -p "Select option: " apply_choice + cecho "${CYAN}─────────────────────────────────────────────────────────────────────${NC}" + + # Validate optimization selection with retry loop + while true; do + read -p "Select option: " apply_choice + + if ! [[ "$apply_choice" =~ ^[a1A2nN]$ ]]; then + echo "" + cecho "${RED}Invalid selection. Please enter a, 1, 2, or n${NC}" + echo "" + continue + fi + break + done # Determine which optimizations to apply local apply_max_children=false local apply_opcache=false + # Convert to lowercase for consistent case matching + apply_choice=${apply_choice,,} + case "$apply_choice" in - a|A) + a) apply_max_children=${opt_available[max_children]:-false} apply_opcache=${opt_available[opcache]:-false} ;; @@ -569,7 +592,7 @@ optimize_domain() { 2) apply_opcache=${opt_available[opcache]:-false} ;; - n|N|*) + n) cecho "${YELLOW}Optimization cancelled - no changes made${NC}" echo "" read -p "Press Enter to continue..." @@ -643,9 +666,8 @@ optimize_domain() { echo "" cecho "${YELLOW}Changes have been applied. Restart PHP-FPM for changes to take effect.${NC}" echo "" - read -p "Restart PHP-FPM now? (y/n): " restart_choice - if [[ "$restart_choice" =~ ^[Yy]$ ]]; then + if confirm "Restart PHP-FPM now?"; then # Detect PHP version local php_version php_version=$(detect_php_version_for_domain "$username" "$domain") @@ -699,9 +721,8 @@ optimize_all_domains() { echo "" cecho "${RED}${BOLD}WARNING:${NC} ${RED}This will modify PHP-FPM pool configurations server-wide!${NC}" echo "" - read -p "Continue with server-wide optimization? (yes/no): " confirm - if [ "$confirm" != "yes" ]; then + if ! confirm "Continue with server-wide optimization?"; then cecho "${YELLOW}Operation cancelled${NC}" read -p "Press Enter to continue..." return @@ -832,12 +853,26 @@ optimize_all_domains() { cecho " ${GREEN}s${NC}) Select individual domains/users to optimize" cecho " ${RED}n${NC}) Cancel - don't apply any changes" echo "" - read -p "Select option: " apply_confirm + cecho "${CYAN}─────────────────────────────────────────────────────────────────────${NC}" + + # Validate apply selection with retry loop + while true; do + read -p "Select option: " apply_confirm + + if ! [[ "$apply_confirm" =~ ^[aAsSnN]$ ]]; then + echo "" + cecho "${RED}Invalid selection. Please enter a, s, or n${NC}" + echo "" + continue + fi + break + done # Handle selection mode declare -A domains_to_apply + apply_confirm=${apply_confirm,,} case "$apply_confirm" in - a|A|yes) + a) # Apply all - mark all domains/users for optimization if [ "$SYS_CONTROL_PANEL" = "cpanel" ]; then for domain in "${!recommended_values[@]}"; do @@ -849,7 +884,7 @@ optimize_all_domains() { done fi ;; - s|S) + s) # Individual selection echo "" cecho "${WHITE}${BOLD}SELECT DOMAINS/USERS TO OPTIMIZE${NC}" @@ -883,14 +918,18 @@ optimize_all_domains() { fi echo "" + cecho "${CYAN}─────────────────────────────────────────────────────────────────────${NC}" read -p "Enter selection: " user_selection - if [[ "$user_selection" =~ ^(all|ALL)$ ]]; then + # Normalize input to lowercase + user_selection=$(echo "$user_selection" | tr '[:upper:]' '[:lower:]') + + if [[ "$user_selection" == "all" ]]; then # Select all for item in "${selection_list[@]}"; do domains_to_apply["$item"]="true" done - elif [[ "$user_selection" =~ ^(none|NONE|n|N)$ ]]; then + elif [[ "$user_selection" =~ ^(none|n)$ ]]; then cecho "${YELLOW}Optimization cancelled${NC}" echo "" read -p "Press Enter to continue..." @@ -916,7 +955,7 @@ optimize_all_domains() { echo "" cecho "${GREEN}Selected ${#domains_to_apply[@]} domain(s)/user(s) for optimization${NC}" ;; - n|N|*) + n) cecho "${YELLOW}Optimization cancelled${NC}" read -p "Press Enter to continue..." return @@ -1463,9 +1502,7 @@ check_server_memory_capacity() { echo "" # Ask if user wants detailed breakdown - read -p "Show detailed per-user breakdown? (y/n): " show_details - - if [[ "$show_details" =~ ^[Yy]$ ]]; then + if confirm "Show detailed per-user breakdown?"; then echo "" cecho "${WHITE}${BOLD}PER-USER BREAKDOWN${NC}" cecho "${CYAN}─────────────────────────────────────────────────────────────────────${NC}" @@ -1484,9 +1521,8 @@ check_server_memory_capacity() { # Ask if user wants balanced recommendations echo "" - read -p "Calculate balanced memory allocation recommendations? (y/n): " show_recommendations - if [[ "$show_recommendations" =~ ^[Yy]$ ]]; then + if confirm "Calculate balanced memory allocation recommendations?"; then echo "" cecho "${WHITE}${BOLD}BALANCED MEMORY ALLOCATION RECOMMENDATIONS${NC}" cecho "${CYAN}─────────────────────────────────────────────────────────────────────${NC}" @@ -1627,17 +1663,25 @@ restore_configurations() { mapfile -t backup_array < <(echo "$backups" | tail -n +2 | cut -d'|' -f1) echo "" - read -p "Select backup number to restore (or 'q' to cancel): " selection + cecho "${CYAN}─────────────────────────────────────────────────────────────────────${NC}" - if [[ "$selection" == "q" ]]; then - return - fi + # Validate backup selection with retry loop + while true; do + read -p "Select backup number to restore (or 'q' to cancel): " selection - if ! [[ "$selection" =~ ^[0-9]+$ ]] || [ "$selection" -lt 1 ] || [ "$selection" -gt ${#backup_array[@]} ]; then - cecho "${RED}Invalid selection${NC}" - sleep 2 - return - fi + if [[ "$selection" == "q" ]]; then + return + fi + + if ! [[ "$selection" =~ ^[0-9]+$ ]] || [ "$selection" -lt 1 ] || [ "$selection" -gt ${#backup_array[@]} ]; then + echo "" + cecho "${RED}Invalid selection. Please enter a number 1-${#backup_array[@]}${NC}" + echo "" + continue + fi + + break + done local selected_backup="${backup_array[$((selection - 1))]}" @@ -1645,9 +1689,8 @@ restore_configurations() { echo "" cecho "${YELLOW}${BOLD}WARNING: This will overwrite current configurations!${NC}" echo "" - read -p "Are you sure you want to restore from $selected_backup? (yes/no): " confirm - if [[ "$confirm" != "yes" ]]; then + if ! confirm "Are you sure you want to restore from $selected_backup?"; then cecho "${YELLOW}Restore cancelled${NC}" sleep 2 return @@ -1697,7 +1740,21 @@ main() { show_banner show_main_menu - read -p "Select option: " choice + # Validate choice input with retry loop + while true; do + read -p "Select option (0-9, b, r): " choice + + if ! [[ "$choice" =~ ^([0-9]|[bBrR])$ ]]; then + echo "" + cecho "${RED}Invalid choice. Please enter 0-9, b, or r${NC}" + echo "" + continue + fi + break + done + + # Convert uppercase to lowercase for case statement + choice=${choice,,} case "$choice" in 1) @@ -1737,10 +1794,6 @@ main() { cecho "${GREEN}Exiting PHP Optimizer...${NC}" exit 0 ;; - *) - cecho "${RED}Invalid option${NC}" - sleep 1 - ;; esac done }