Apply menu uniformity standards to php-optimizer.sh

- Add input validation with retry loops to main menu (0-9, b, r)
- Replace manual yes/no prompts with confirm() function (5 locations)
- Add visual separator lines (━━━) before major menu prompts
- Add input validation to domain selection with retry loop
- Add input validation to optimization selection with retry loop
- Add input validation to apply options selection with retry loop
- Add input validation to backup selection with retry loop
- Normalize case-insensitive inputs consistently
- Improve error messages for invalid selections
- Standardize all menu prompts for consistency

This applies the same menu uniformity standards that were established
across 10 other scripts in the toolkit, ensuring consistent user experience
in the PHP-FPM optimization tool.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
cschantz
2026-02-17 20:47:18 -05:00
parent 48613ad5f5
commit 1acad38bd0
+98 -45
View File
@@ -118,17 +118,25 @@ select_domain() {
done done
echo "" echo ""
read -p "Select domain number (or 'q' to cancel): " selection cecho "${CYAN}─────────────────────────────────────────────────────────────────────${NC}"
if [[ "$selection" == "q" ]]; then # Validate domain selection with retry loop
return 1 while true; do
fi read -p "Select domain number (or 'q' to cancel): " selection
if ! [[ "$selection" =~ ^[0-9]+$ ]] || [ "$selection" -lt 1 ] || [ "$selection" -gt ${#domains[@]} ]; then if [[ "$selection" == "q" ]]; then
cecho "${RED}Invalid selection${NC}" return 1
sleep 2 fi
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 # Return selected domain and username
local selected_domain="${domains[$((selection - 1))]}" local selected_domain="${domains[$((selection - 1))]}"
@@ -178,9 +186,8 @@ analyze_all_domains() {
echo "" echo ""
cecho "${YELLOW}This will analyze PHP configuration for ALL domains...${NC}" cecho "${YELLOW}This will analyze PHP configuration for ALL domains...${NC}"
echo "" echo ""
read -p "Continue? (y/n): " confirm
if [[ ! "$confirm" =~ ^[Yy]$ ]]; then if ! confirm "Continue?"; then
return return
fi fi
@@ -552,14 +559,30 @@ optimize_domain() {
[ "${opt_available[opcache]}" = "true" ] && cecho " ${GREEN}2${NC}) Apply OPcache optimization only" [ "${opt_available[opcache]}" = "true" ] && cecho " ${GREEN}2${NC}) Apply OPcache optimization only"
cecho " ${RED}n${NC}) Cancel - don't apply any changes" cecho " ${RED}n${NC}) Cancel - don't apply any changes"
echo "" 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 # Determine which optimizations to apply
local apply_max_children=false local apply_max_children=false
local apply_opcache=false local apply_opcache=false
# Convert to lowercase for consistent case matching
apply_choice=${apply_choice,,}
case "$apply_choice" in case "$apply_choice" in
a|A) a)
apply_max_children=${opt_available[max_children]:-false} apply_max_children=${opt_available[max_children]:-false}
apply_opcache=${opt_available[opcache]:-false} apply_opcache=${opt_available[opcache]:-false}
;; ;;
@@ -569,7 +592,7 @@ optimize_domain() {
2) 2)
apply_opcache=${opt_available[opcache]:-false} apply_opcache=${opt_available[opcache]:-false}
;; ;;
n|N|*) n)
cecho "${YELLOW}Optimization cancelled - no changes made${NC}" cecho "${YELLOW}Optimization cancelled - no changes made${NC}"
echo "" echo ""
read -p "Press Enter to continue..." read -p "Press Enter to continue..."
@@ -643,9 +666,8 @@ optimize_domain() {
echo "" echo ""
cecho "${YELLOW}Changes have been applied. Restart PHP-FPM for changes to take effect.${NC}" cecho "${YELLOW}Changes have been applied. Restart PHP-FPM for changes to take effect.${NC}"
echo "" 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 # Detect PHP version
local php_version local php_version
php_version=$(detect_php_version_for_domain "$username" "$domain") php_version=$(detect_php_version_for_domain "$username" "$domain")
@@ -699,9 +721,8 @@ optimize_all_domains() {
echo "" echo ""
cecho "${RED}${BOLD}WARNING:${NC} ${RED}This will modify PHP-FPM pool configurations server-wide!${NC}" cecho "${RED}${BOLD}WARNING:${NC} ${RED}This will modify PHP-FPM pool configurations server-wide!${NC}"
echo "" 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}" cecho "${YELLOW}Operation cancelled${NC}"
read -p "Press Enter to continue..." read -p "Press Enter to continue..."
return return
@@ -832,12 +853,26 @@ optimize_all_domains() {
cecho " ${GREEN}s${NC}) Select individual domains/users to optimize" cecho " ${GREEN}s${NC}) Select individual domains/users to optimize"
cecho " ${RED}n${NC}) Cancel - don't apply any changes" cecho " ${RED}n${NC}) Cancel - don't apply any changes"
echo "" 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 # Handle selection mode
declare -A domains_to_apply declare -A domains_to_apply
apply_confirm=${apply_confirm,,}
case "$apply_confirm" in case "$apply_confirm" in
a|A|yes) a)
# Apply all - mark all domains/users for optimization # Apply all - mark all domains/users for optimization
if [ "$SYS_CONTROL_PANEL" = "cpanel" ]; then if [ "$SYS_CONTROL_PANEL" = "cpanel" ]; then
for domain in "${!recommended_values[@]}"; do for domain in "${!recommended_values[@]}"; do
@@ -849,7 +884,7 @@ optimize_all_domains() {
done done
fi fi
;; ;;
s|S) s)
# Individual selection # Individual selection
echo "" echo ""
cecho "${WHITE}${BOLD}SELECT DOMAINS/USERS TO OPTIMIZE${NC}" cecho "${WHITE}${BOLD}SELECT DOMAINS/USERS TO OPTIMIZE${NC}"
@@ -883,14 +918,18 @@ optimize_all_domains() {
fi fi
echo "" echo ""
cecho "${CYAN}─────────────────────────────────────────────────────────────────────${NC}"
read -p "Enter selection: " user_selection 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 # Select all
for item in "${selection_list[@]}"; do for item in "${selection_list[@]}"; do
domains_to_apply["$item"]="true" domains_to_apply["$item"]="true"
done done
elif [[ "$user_selection" =~ ^(none|NONE|n|N)$ ]]; then elif [[ "$user_selection" =~ ^(none|n)$ ]]; then
cecho "${YELLOW}Optimization cancelled${NC}" cecho "${YELLOW}Optimization cancelled${NC}"
echo "" echo ""
read -p "Press Enter to continue..." read -p "Press Enter to continue..."
@@ -916,7 +955,7 @@ optimize_all_domains() {
echo "" echo ""
cecho "${GREEN}Selected ${#domains_to_apply[@]} domain(s)/user(s) for optimization${NC}" cecho "${GREEN}Selected ${#domains_to_apply[@]} domain(s)/user(s) for optimization${NC}"
;; ;;
n|N|*) n)
cecho "${YELLOW}Optimization cancelled${NC}" cecho "${YELLOW}Optimization cancelled${NC}"
read -p "Press Enter to continue..." read -p "Press Enter to continue..."
return return
@@ -1463,9 +1502,7 @@ check_server_memory_capacity() {
echo "" echo ""
# Ask if user wants detailed breakdown # Ask if user wants detailed breakdown
read -p "Show detailed per-user breakdown? (y/n): " show_details if confirm "Show detailed per-user breakdown?"; then
if [[ "$show_details" =~ ^[Yy]$ ]]; then
echo "" echo ""
cecho "${WHITE}${BOLD}PER-USER BREAKDOWN${NC}" cecho "${WHITE}${BOLD}PER-USER BREAKDOWN${NC}"
cecho "${CYAN}─────────────────────────────────────────────────────────────────────${NC}" cecho "${CYAN}─────────────────────────────────────────────────────────────────────${NC}"
@@ -1484,9 +1521,8 @@ check_server_memory_capacity() {
# Ask if user wants balanced recommendations # Ask if user wants balanced recommendations
echo "" 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 "" echo ""
cecho "${WHITE}${BOLD}BALANCED MEMORY ALLOCATION RECOMMENDATIONS${NC}" cecho "${WHITE}${BOLD}BALANCED MEMORY ALLOCATION RECOMMENDATIONS${NC}"
cecho "${CYAN}─────────────────────────────────────────────────────────────────────${NC}" cecho "${CYAN}─────────────────────────────────────────────────────────────────────${NC}"
@@ -1627,17 +1663,25 @@ restore_configurations() {
mapfile -t backup_array < <(echo "$backups" | tail -n +2 | cut -d'|' -f1) mapfile -t backup_array < <(echo "$backups" | tail -n +2 | cut -d'|' -f1)
echo "" echo ""
read -p "Select backup number to restore (or 'q' to cancel): " selection cecho "${CYAN}─────────────────────────────────────────────────────────────────────${NC}"
if [[ "$selection" == "q" ]]; then # Validate backup selection with retry loop
return while true; do
fi read -p "Select backup number to restore (or 'q' to cancel): " selection
if ! [[ "$selection" =~ ^[0-9]+$ ]] || [ "$selection" -lt 1 ] || [ "$selection" -gt ${#backup_array[@]} ]; then if [[ "$selection" == "q" ]]; then
cecho "${RED}Invalid selection${NC}" return
sleep 2 fi
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))]}" local selected_backup="${backup_array[$((selection - 1))]}"
@@ -1645,9 +1689,8 @@ restore_configurations() {
echo "" echo ""
cecho "${YELLOW}${BOLD}WARNING: This will overwrite current configurations!${NC}" cecho "${YELLOW}${BOLD}WARNING: This will overwrite current configurations!${NC}"
echo "" 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}" cecho "${YELLOW}Restore cancelled${NC}"
sleep 2 sleep 2
return return
@@ -1697,7 +1740,21 @@ main() {
show_banner show_banner
show_main_menu 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 case "$choice" in
1) 1)
@@ -1737,10 +1794,6 @@ main() {
cecho "${GREEN}Exiting PHP Optimizer...${NC}" cecho "${GREEN}Exiting PHP Optimizer...${NC}"
exit 0 exit 0
;; ;;
*)
cecho "${RED}Invalid option${NC}"
sleep 1
;;
esac esac
done done
} }