#!/bin/bash
# Purpose: Audit every cPanel account on a WHM/cPanel host — enumerate domains,
#          detect WordPress installs + versions, and flag performance/stability
#          issues. Emits ===SITE_START===-delimited records for downstream parsing.
# Usage:   run as root ON the cPanel server (reads /var/cpanel/users/ and /home/*).
# Origin:  graduated from tmp/site-scan.sh; produced the 2026-03-16 ix-server audit
#          (clients/internal-infrastructure/reports/2026-03-16-ix-server-cpanel-wp-audit.md).
# Author:  Mike Swanson
# Created: 2026-03-16

for user in $(ls /var/cpanel/users/ | grep -v system | sort); do
    domain=$(grep "^DNS=" /var/cpanel/users/$user 2>/dev/null | head -1 | cut -d= -f2)
    pubhtml="/home/$user/public_html"

    echo "===SITE_START==="
    echo "ACCOUNT: $user"
    echo "DOMAIN: $domain"

    if [ ! -d "$pubhtml" ]; then
        echo "STATUS: no_public_html"
        echo "===SITE_END==="
        continue
    fi

    du_size=$(du -sh "$pubhtml" 2>/dev/null | cut -f1)
    echo "DISK: $du_size"

    errlog_main=$(stat -c%s "$pubhtml/error_log" 2>/dev/null || echo "0")
    echo "ERRORLOG_SIZE: $errlog_main"

    if [ -f "$pubhtml/error_log" ] && [ "$errlog_main" -gt 0 ] 2>/dev/null; then
        echo "ERRORLOG_TAIL:"
        tail -3 "$pubhtml/error_log" 2>/dev/null
        echo "ERRORLOG_END"
    fi

    if [ ! -f "$pubhtml/wp-config.php" ]; then
        echo "WORDPRESS: no"
        if [ -f "$pubhtml/index.html" ]; then
            echo "TYPE: static_html"
        elif [ -f "$pubhtml/index.php" ]; then
            echo "TYPE: php"
        else
            echo "TYPE: empty_or_other"
        fi
        echo "===SITE_END==="
        continue
    fi

    echo "WORDPRESS: yes"

    wp_ver=$(grep "wp_version = " "$pubhtml/wp-includes/version.php" 2>/dev/null | cut -d"'" -f2)
    echo "WP_VERSION: $wp_ver"

    wp_debug=$(grep -c "WP_DEBUG.*true" "$pubhtml/wp-config.php" 2>/dev/null)
    echo "WP_DEBUG: $wp_debug"

    debuglog=$(stat -c%s "$pubhtml/wp-content/debug.log" 2>/dev/null || echo "0")
    echo "DEBUGLOG_SIZE: $debuglog"

    db_name=$(grep "DB_NAME" "$pubhtml/wp-config.php" 2>/dev/null | grep define | head -1 | cut -d"'" -f4)
    echo "DB_NAME: $db_name"

    tbl_prefix=$(grep 'table_prefix' "$pubhtml/wp-config.php" 2>/dev/null | head -1 | cut -d"'" -f2)
    if [ -z "$tbl_prefix" ]; then
        tbl_prefix="wp_"
    fi
    echo "TABLE_PREFIX: $tbl_prefix"

    if [ -n "$db_name" ]; then
        db_size=$(mysql -N -e "SELECT ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) FROM information_schema.tables WHERE table_schema='$db_name'" 2>/dev/null)
        echo "DB_SIZE_MB: $db_size"

        autoload_size=$(mysql -N -e "SELECT ROUND(SUM(LENGTH(option_value))/1024/1024, 2) FROM ${db_name}.${tbl_prefix}options WHERE autoload='yes'" 2>/dev/null)
        echo "AUTOLOAD_MB: $autoload_size"

        transients=$(mysql -N -e "SELECT COUNT(*) FROM ${db_name}.${tbl_prefix}options WHERE option_name LIKE '%_transient_%'" 2>/dev/null)
        echo "TRANSIENTS: $transients"

        revisions=$(mysql -N -e "SELECT COUNT(*) FROM ${db_name}.${tbl_prefix}posts WHERE post_type='revision'" 2>/dev/null)
        echo "REVISIONS: $revisions"

        spam_comments=$(mysql -N -e "SELECT COUNT(*) FROM ${db_name}.${tbl_prefix}comments WHERE comment_approved='spam' OR comment_approved='trash'" 2>/dev/null)
        echo "SPAM_COMMENTS: $spam_comments"
    fi

    plugin_count=$(ls -d "$pubhtml/wp-content/plugins"/*/ 2>/dev/null | wc -l)
    echo "PLUGIN_COUNT: $plugin_count"

    if [ -n "$db_name" ]; then
        active_plugins=$(mysql -N -e "SELECT option_value FROM ${db_name}.${tbl_prefix}options WHERE option_name='active_plugins'" 2>/dev/null)
        active_count=$(echo "$active_plugins" | grep -oP '"[^"]*\/[^"]*"' | wc -l)
        echo "ACTIVE_PLUGIN_COUNT: $active_count"
        inactive=$((plugin_count - active_count))
        echo "INACTIVE_PLUGINS: $inactive"
    fi

    for heavy in wordfence jetpack elementor wpbakery revslider LayerSlider updraftplus all-in-one-wp-migration broken-link-checker; do
        if [ -d "$pubhtml/wp-content/plugins/$heavy" ]; then
            echo "HEAVY_PLUGIN: $heavy"
        fi
    done

    htaccess_size=$(stat -c%s "$pubhtml/.htaccess" 2>/dev/null || echo "0")
    echo "HTACCESS_SIZE: $htaccess_size"

    cron_disabled=$(grep -c "DISABLE_WP_CRON.*true" "$pubhtml/wp-config.php" 2>/dev/null)
    echo "CRON_DISABLED: $cron_disabled"

    wp_memory=$(grep "WP_MEMORY_LIMIT" "$pubhtml/wp-config.php" 2>/dev/null | grep define | head -1 | cut -d"'" -f4)
    echo "WP_MEMORY: $wp_memory"

    if [ -f "$pubhtml/wp-content/object-cache.php" ]; then
        echo "OBJECT_CACHE: yes"
    else
        echo "OBJECT_CACHE: no"
    fi

    if [ -f "$pubhtml/wp-content/advanced-cache.php" ]; then
        echo "PAGE_CACHE: yes"
    else
        echo "PAGE_CACHE: no"
    fi

    uploads_size=$(du -sh "$pubhtml/wp-content/uploads" 2>/dev/null | cut -f1)
    echo "UPLOADS_SIZE: $uploads_size"

    echo "===SITE_END==="
done
