Files
Linux-Server-Management-Too…/lib/common-functions.sh
T
cschantz a51d968185 Initial commit: Server Management Toolkit v2.0
- Complete security menu restructure (3-mode: Analysis/Actions/Live)
- Intelligent cPHulk enablement with CSF whitelist import
- Live network security monitoring dashboard
- Multi-source threat detection and classification
- 50+ organized security tools across 4-level menu hierarchy
- System health diagnostics with cPanel/WHM integration
- Reference database for cross-module intelligence sharing
2025-11-03 18:21:40 -05:00

278 lines
7.3 KiB
Bash
Executable File

#!/bin/bash
#############################################################################
# Common Functions Library
# Shared utilities for all Server Management Toolkit modules
#############################################################################
#############################################################################
# Professional Color Scheme
# - Uses ONLY basic ANSI colors (works on ANY terminal)
# - High contrast for readability
# - No emojis (terminal-agnostic)
# - Industry standard: Red=error, Yellow=warning, Green=success, Blue=info
#
# To disable colors completely: export TOOLKIT_NO_COLOR=1
#############################################################################
# Detect if colors should be disabled or adjusted
if [ "${TOOLKIT_NO_COLOR:-0}" = "1" ] || [ ! -t 1 ]; then
# No color mode (monochrome terminals, piped output, etc.)
export RED=''
export YELLOW=''
export GREEN=''
export BLUE=''
export CYAN=''
export WHITE=''
export BOLD=''
export DIM=''
export NC=''
elif [ "${TOOLKIT_HIGH_CONTRAST:-0}" = "1" ]; then
# High contrast mode - BOLD colors for light backgrounds
# These work well on BOTH light and dark backgrounds
export RED='\033[1;31m' # Errors, critical alerts (BOLD RED)
export YELLOW='\033[1;33m' # Warnings, caution (BOLD YELLOW)
export GREEN='\033[1;32m' # Success, completed (BOLD GREEN)
export BLUE='\033[1;34m' # Info, neutral status (BOLD BLUE)
export CYAN='\033[1;36m' # Headers, sections (BOLD CYAN)
export WHITE='\033[1;37m' # Emphasis, highlights (BOLD WHITE)
export BOLD='\033[1m' # Important text
export DIM='\033[2m' # Secondary/less important
export NC='\033[0m' # Reset/No Color
else
# Normal color mode (default - optimized for dark backgrounds)
export RED='\033[0;31m' # Errors, critical alerts
export YELLOW='\033[0;33m' # Warnings, caution
export GREEN='\033[0;32m' # Success, completed
export BLUE='\033[0;34m' # Info, neutral status
export CYAN='\033[0;36m' # Headers, sections
export WHITE='\033[1;37m' # Emphasis, highlights
export BOLD='\033[1m' # Important text
export DIM='\033[2m' # Secondary/less important
export NC='\033[0m' # Reset/No Color
fi
# Print functions - professional, no emojis
print_banner() {
local title="$1"
echo ""
echo "==============================================================================="
echo " $title"
echo "==============================================================================="
echo ""
}
print_section() {
local title="$1"
echo ""
echo -e "${BOLD}$title${NC}"
echo "-------------------------------------------------------------------------------"
}
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[OK]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_critical() {
echo -e "${RED}${BOLD}[CRITICAL]${NC} $1"
}
print_alert() {
echo -e "${RED}[ALERT]${NC} $1"
}
print_header() {
echo -e "${CYAN}${BOLD}$1${NC}"
}
# Wait for user input
press_enter() {
echo ""
read -p "Press Enter to continue..." _
}
# Show banner (wrapper for print_banner for compatibility)
show_banner() {
if [ -n "$1" ]; then
print_banner "$1"
else
print_banner "Server Toolkit"
fi
}
# Progress indicator - terminal safe (no fancy characters)
show_progress() {
local current=$1
local total=$2
local message="$3"
local percent=$((current * 100 / total))
local bars=$((percent / 5)) # 20 chars wide
printf "\r[INFO] Progress: ["
printf "%${bars}s" | tr ' ' '#'
printf "%$((20-bars))s" | tr ' ' '-'
printf "] %3d%% - %s" "$percent" "$message"
}
# Finish progress line
finish_progress() {
echo ""
}
# Display terminal info (for debugging)
show_terminal_info() {
echo ""
echo "Terminal Information:"
echo " TERM: ${TERM:-not set}"
echo " Colors: $([ -t 1 ] && echo "enabled" || echo "disabled (not a tty)")"
echo " TOOLKIT_NO_COLOR: ${TOOLKIT_NO_COLOR:-0}"
echo " Columns: ${COLUMNS:-$(tput cols 2>/dev/null || echo "unknown")}"
echo ""
}
# Create temporary session directory
create_temp_session() {
export SESSION_ID=$$
export TEMP_SESSION_DIR="/tmp/server-toolkit-${SESSION_ID}"
mkdir -p "$TEMP_SESSION_DIR"
# Cleanup on exit
trap "rm -rf $TEMP_SESSION_DIR 2>/dev/null" EXIT INT TERM
}
# Ask user for confirmation
confirm() {
local prompt="$1"
local default="${2:-N}"
if [[ "$default" == "Y" ]]; then
read -p "$prompt [Y/n]: " -n 1 -r
else
read -p "$prompt [y/N]: " -n 1 -r
fi
echo
if [[ "$default" == "Y" ]]; then
[[ ! $REPLY =~ ^[Nn]$ ]]
else
[[ $REPLY =~ ^[Yy]$ ]]
fi
}
# Format bytes to human readable
format_bytes() {
local bytes=$1
if [ -z "$bytes" ] || [ "$bytes" -eq 0 ]; then
echo "0 B"
return
fi
local units=("B" "KB" "MB" "GB" "TB")
local unit=0
local size=$bytes
while [ $size -gt 1024 ] && [ $unit -lt 4 ]; do
size=$((size / 1024))
unit=$((unit + 1))
done
echo "${size} ${units[$unit]}"
}
# Format seconds to human readable time
format_duration() {
local seconds=$1
local days=$((seconds / 86400))
local hours=$(((seconds % 86400) / 3600))
local minutes=$(((seconds % 3600) / 60))
local secs=$((seconds % 60))
if [ $days -gt 0 ]; then
echo "${days}d ${hours}h ${minutes}m"
elif [ $hours -gt 0 ]; then
echo "${hours}h ${minutes}m ${secs}s"
elif [ $minutes -gt 0 ]; then
echo "${minutes}m ${secs}s"
else
echo "${secs}s"
fi
}
# Check if command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Check if running as root
require_root() {
if [ "$EUID" -ne 0 ]; then
print_error "This script must be run as root"
exit 1
fi
}
# Safe file append (creates backup)
safe_append() {
local file="$1"
local content="$2"
if [ -f "$file" ]; then
cp "$file" "${file}.bak.$(date +%s)"
fi
echo "$content" >> "$file"
}
# Log message to file
log_message() {
local level="$1"
local message="$2"
local log_file="${3:-/var/log/server-toolkit.log}"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo "[$timestamp] [$level] $message" >> "$log_file"
}
# Get script directory (works even when sourced)
get_script_dir() {
local source="${BASH_SOURCE[0]}"
while [ -h "$source" ]; do
local dir="$(cd -P "$(dirname "$source")" && pwd)"
source="$(readlink "$source")"
[[ $source != /* ]] && source="$dir/$source"
done
echo "$(cd -P "$(dirname "$source")" && pwd)"
}
# Detect toolkit base directory
get_toolkit_dir() {
local script_dir=$(get_script_dir)
echo "$(cd "$script_dir/.." && pwd)"
}
# Export toolkit directories
export TOOLKIT_BASE_DIR=$(get_toolkit_dir)
export TOOLKIT_LIB_DIR="${TOOLKIT_BASE_DIR}/lib"
export TOOLKIT_MODULES_DIR="${TOOLKIT_BASE_DIR}/modules"
export TOOLKIT_CONFIG_DIR="${TOOLKIT_BASE_DIR}/config"
# Load configuration if exists
load_config() {
local config_file="${TOOLKIT_CONFIG_DIR}/settings.conf"
if [ -f "$config_file" ]; then
source "$config_file"
fi
}