diff --git a/lib/reference-db-NEW-build_domains.sh b/lib/reference-db-NEW-build_domains.sh new file mode 100644 index 0000000..a1ceeba --- /dev/null +++ b/lib/reference-db-NEW-build_domains.sh @@ -0,0 +1,27 @@ +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 100755 new mode 100644 index 6249b3c..87c4163 --- a/lib/reference-db.sh +++ b/lib/reference-db.sh @@ -11,6 +11,7 @@ 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 @@ -244,157 +245,28 @@ check_domain_status() { build_domains_section() { echo "[DOMAINS]" >> "$SYSREF_DB" - # Track domains we've already added - declare -A seen_domains + # Use unified domain discovery + local all_domains=$(list_all_domains) + local domain_count=$(echo "$all_domains" | wc -w) + local current=0 - local users=($(list_all_users)) + for domain in $all_domains; do + [ -z "$domain" ] && continue + ((current++)) - # 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 + show_progress $current $domain_count "Processing domains..." - local current_domain=0 + # 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 "") - # 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 + # Simple domain entry - Format: DOMAIN|domain|owner|docroot|logdir|access_log + echo "DOMAIN|$domain|$owner|$docroot|$logdir|$access_log" >> "$SYSREF_DB" 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" }