diff --git a/lib/reference-db-NEW-build_domains.sh b/lib/reference-db-NEW-build_domains.sh deleted file mode 100644 index a1ceeba..0000000 --- a/lib/reference-db-NEW-build_domains.sh +++ /dev/null @@ -1,27 +0,0 @@ -build_domains_section() { - echo "[DOMAINS]" >> "$SYSREF_DB" - - # Use unified domain discovery - local all_domains=$(list_all_domains) - local domain_count=$(echo "$all_domains" | wc -w) - local current=0 - - for domain in $all_domains; do - [ -z "$domain" ] && continue - ((current++)) - - show_progress $current $domain_count "Processing domains..." - - # Get domain information using unified functions - local owner=$(get_domain_owner "$domain" || echo "unknown") - local docroot=$(get_domain_docroot "$domain" || echo "") - local logdir=$(get_domain_logdir "$domain" || echo "") - local access_log=$(get_domain_access_log "$domain" || echo "") - - # Simple domain entry - Format: DOMAIN|domain|owner|docroot|logdir|access_log - echo "DOMAIN|$domain|$owner|$docroot|$logdir|$access_log" >> "$SYSREF_DB" - done - - finish_progress - echo "" >> "$SYSREF_DB" -} diff --git a/lib/reference-db.sh b/lib/reference-db.sh old mode 100644 new mode 100755 index 780046e..6249b3c --- a/lib/reference-db.sh +++ b/lib/reference-db.sh @@ -11,7 +11,6 @@ if [ -z "$TOOLKIT_BASE_DIR" ]; then SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "$SCRIPT_DIR/common-functions.sh" source "$SCRIPT_DIR/system-detect.sh" - source "$SCRIPT_DIR/domain-discovery.sh" source "$SCRIPT_DIR/user-manager.sh" fi @@ -245,28 +244,157 @@ check_domain_status() { build_domains_section() { echo "[DOMAINS]" >> "$SYSREF_DB" - # Use unified domain discovery - local all_domains=$(list_all_domains) - local domain_count=$(echo "$all_domains" | wc -w) - local current=0 + # Track domains we've already added + declare -A seen_domains - for domain in $all_domains; do - [ -z "$domain" ] && continue - ((current++)) + local users=($(list_all_users)) - show_progress $current $domain_count "Processing domains..." + # Count total domains for progress + local total_domains=0 + for user in "${users[@]}"; do + local userdata_dir="${SYS_CPANEL_USERDATA_DIR:-/var/cpanel/userdata}/${user}" + if [ -d "$userdata_dir" ]; then + total_domains=$((total_domains + $(find "$userdata_dir" -type f ! -name "*.cache" ! -name "*.yaml" ! -name "*.json" ! -name "main*" ! -name "cache" ! -name "*_SSL" 2>/dev/null | wc -l))) + fi + done - # Get domain information using unified functions - local owner=$(get_domain_owner "$domain" || echo "unknown") - local docroot=$(get_domain_docroot "$domain" || echo "") - local logdir=$(get_domain_logdir "$domain" || echo "") - local access_log=$(get_domain_access_log "$domain" || echo "") + local current_domain=0 - # Simple domain entry - Format: DOMAIN|domain|owner|docroot|logdir|access_log - echo "DOMAIN|$domain|$owner|$docroot|$logdir|$access_log" >> "$SYSREF_DB" + # Get detailed domain information from cPanel userdata (if available) + for user in "${users[@]}"; do + local userdata_dir="${SYS_CPANEL_USERDATA_DIR:-/var/cpanel/userdata}/${user}" + + if [ -d "$userdata_dir" ]; then + # Parse each domain configuration file in userdata + for config_file in "$userdata_dir"/*; do + [ ! -f "$config_file" ] && continue + local basename=$(basename "$config_file") + + # Skip cache files and special files + [[ "$basename" =~ \.cache$ ]] && continue + [[ "$basename" =~ \.yaml$ ]] && continue + [[ "$basename" =~ \.json$ ]] && continue + [[ "$basename" =~ ^main ]] && continue + [[ "$basename" =~ ^cache$ ]] && continue + [[ "$basename" =~ _SSL$ ]] && continue + + # Extract domain info from config + local domain="$basename" + local doc_root=$(grep "^documentroot:" "$config_file" | awk '{print $2}' || true) + local log_path=$(grep "target:.*domlogs" "$config_file" | head -1 | awk '{print $2}' || true) + local server_alias=$(grep "^serveralias:" "$config_file" | awk '{print $2}' || true) + local php_version=$(grep "^phpversion:" "$config_file" | awk '{print $2}' || true) + + # Determine if primary domain + local is_primary="no" + local primary_domain=$(get_user_domains "$user" | head -1) + [ "$domain" = "$primary_domain" ] && is_primary="yes" + + # Determine domain type (addon, parked, subdomain, primary) + local domain_type="addon" + if [ "$is_primary" = "yes" ]; then + domain_type="primary" + elif [[ "$domain" =~ \. ]] && [[ "$domain" =~ ^[^.]+\. ]]; then + # Check if it's a subdomain of the primary + local base_domain=$(echo "$domain" | rev | cut -d. -f1-2 | rev) + if [ "$base_domain" = "$primary_domain" ]; then + domain_type="subdomain" + fi + fi + + # Check HTTP/HTTPS status codes (only for primary and addon domains, skip aliases/subdomains) + current_domain=$((current_domain + 1)) + local http_code="000" + local https_code="000" + local status_summary="skipped" + + if [ "$domain_type" = "primary" ] || [ "$domain_type" = "addon" ]; then + show_progress $current_domain $total_domains "Checking domain status codes..." + local status_result=$(check_domain_status "$domain") + IFS='|' read -r http_code https_code status_summary <<< "$status_result" + fi + + # Format: DOMAIN|domain|owner|doc_root|log_path|php_version|is_primary|type|aliases|http_code|https_code|status_summary + echo "DOMAIN|$domain|$user|$doc_root|$log_path|$php_version|$is_primary|$domain_type|$server_alias|$http_code|$https_code|$status_summary" >> "$SYSREF_DB" + seen_domains["$domain"]=1 + + # Also add aliases as separate entries + if [ -n "$server_alias" ]; then + for alias in $server_alias; do + [ -z "$alias" ] && continue + [ -n "${seen_domains[$alias]:-}" ] && continue + + # Alias points to same document root and logs (inherit status from parent) + echo "DOMAIN|$alias|$user|$doc_root|$log_path|$php_version|no|alias|$domain|$http_code|$https_code|alias_of_$status_summary" >> "$SYSREF_DB" + seen_domains["$alias"]=1 + done + fi + done + else + # Fallback for non-cPanel or if userdata not available + local primary_domain=$(get_user_domains "$user" | head -1) + local all_domains=$(get_user_domains "$user") + + for domain in $all_domains; do + [ -z "$domain" ] && continue + [ -n "${seen_domains[$domain]:-}" ] && continue + + local is_primary="no" + [ "$domain" = "$primary_domain" ] && is_primary="yes" + + # Find log path + local log_path="${SYS_LOG_DIR}/${domain}" + [ ! -f "$log_path" ] && log_path="${SYS_LOG_DIR}/${domain}.log" + + # Check status for non-cPanel domains + current_domain=$((current_domain + 1)) + show_progress $current_domain $total_domains "Checking domain status codes..." + local status_result=$(check_domain_status "$domain") + IFS='|' read -r http_code https_code status_summary <<< "$status_result" + + # Simple format for non-cPanel (with status codes) + echo "DOMAIN|$domain|$user||$log_path||$is_primary|local||$http_code|$https_code|$status_summary" >> "$SYSREF_DB" + seen_domains["$domain"]=1 + done + fi done finish_progress + + # Check /etc/localdomains (cPanel local domains not yet added) + if [ -f "/etc/localdomains" ]; then + while read -r domain; do + [ -z "$domain" ] && continue + [ -n "${seen_domains[$domain]:-}" ] && continue + + local owner=$(grep "^${domain}:" /etc/trueuserdomains 2>/dev/null | cut -d: -f2 | xargs || true) + [ -z "$owner" ] && owner="unknown" + + local log_path="${SYS_LOG_DIR}/${domain}" + + # Check status + local status_result=$(check_domain_status "$domain") + IFS='|' read -r http_code https_code status_summary <<< "$status_result" + + echo "DOMAIN|$domain|$owner||$log_path||unknown|local||$http_code|$https_code|$status_summary" >> "$SYSREF_DB" + seen_domains["$domain"]=1 + done < /etc/localdomains + fi + + # Check /etc/remotedomains (cPanel remote MX domains - no status check for remote MX) + if [ -f "/etc/remotedomains" ]; then + while read -r domain; do + [ -z "$domain" ] && continue + [ -n "${seen_domains[$domain]:-}" ] && continue + + local owner=$(grep "^${domain}:" /etc/trueuserdomains 2>/dev/null | cut -d: -f2 | xargs || true) + [ -z "$owner" ] && owner="unknown" + + echo "DOMAIN|$domain|$owner||||unknown|remote||000|000|remote_mx" >> "$SYSREF_DB" + seen_domains["$domain"]=1 + done < /etc/remotedomains + fi + echo "" >> "$SYSREF_DB" } @@ -279,46 +407,24 @@ build_wordpress_section() { for wp_config in $wp_configs; do local wp_dir=$(dirname "$wp_config") - # Extract username/domain from path (panel-agnostic) - local username="" + # Extract username from path (/home/username/...) + local username=$(echo "$wp_dir" | cut -d'/' -f3) + + # Try to get domain from path - check if it's in a subdomain or addon domain folder + local path_after_home=$(echo "$wp_dir" | sed "s|^/home/$username/||") local domain="" - case "$SYS_CONTROL_PANEL" in - cpanel) - # cPanel: /home/username/... - username=$(echo "$wp_dir" | cut -d'/' -f3) - ;; - plesk) - # Plesk: /var/www/vhosts/domain.com/... - domain=$(echo "$wp_dir" | cut -d'/' -f5) - username=$(get_domain_owner "$domain" 2>/dev/null || echo "unknown") - ;; - interworx) - # InterWorx: /chroot/home/user/var/domain.com/... - username=$(echo "$wp_dir" | cut -d'/' -f4) - ;; - *) - # Standalone: try to extract from path - username=$(stat -c "%U" "$wp_dir" 2>/dev/null || echo "unknown") - ;; - esac - - # If domain not set yet (cPanel/InterWorx/Standalone), try to infer from path - if [ -z "$domain" ] && [ "$SYS_CONTROL_PANEL" = "cpanel" ]; then - # cPanel: check if this is primary domain or addon/subdomain - local path_after_home=$(echo "$wp_dir" | sed "s|^/home/$username/||") - - if [[ "$path_after_home" == public_html ]]; then - # This is the primary domain - get it from user info - domain=$(grep "USER|${username}|" "$SYSREF_DB" 2>/dev/null | cut -d'|' -f3 || true) - elif [[ "$path_after_home" =~ ^public_html/(.+) ]]; then - # Could be subdomain or subdirectory - extract folder name - local folder=$(echo "$path_after_home" | cut -d'/' -f2) - domain="${folder}" - else - # Might be addon/parked domain with own directory - domain=$(echo "$path_after_home" | cut -d'/' -f1) - fi + # Check for common domain folder patterns + if [[ "$path_after_home" == public_html ]]; then + # This is the primary domain - get it from user info + domain=$(grep "USER|${username}|" "$SYSREF_DB" 2>/dev/null | cut -d'|' -f3 || true) + elif [[ "$path_after_home" =~ ^public_html/(.+) ]]; then + # Could be subdomain or subdirectory - extract folder name + local folder=$(echo "$path_after_home" | cut -d'/' -f2) + domain="${folder}" + else + # Might be addon/parked domain with own directory + domain=$(echo "$path_after_home" | cut -d'/' -f1) fi # Try to get actual domain from WP database options (more reliable)