#!/bin/bash
# Smart Slider 3 Pro Security Scanner for IX Server
# Scans all WordPress installations for Smart Slider plugin

echo "[INFO] IX Server Smart Slider 3 Security Scan"
echo "[INFO] Date: $(date)"
echo "=============================================="
echo ""

# Initialize counters
total_wp=0
found_free=0
found_pro=0

# Create temporary file for results
results_file="/tmp/smart_slider_scan_$(date +%s).txt"

echo "[INFO] Scanning for WordPress installations..."
echo ""

# Find all WordPress installations
for wpconfig in $(find /home/*/public_html -maxdepth 3 -name "wp-config.php" -type f 2>/dev/null); do
    ((total_wp++))
    wpdir=$(dirname "$wpconfig")
    plugindir="$wpdir/wp-content/plugins"
    site_user=$(echo "$wpdir" | cut -d'/' -f3)

    # Check for Smart Slider 3 PRO
    if [ -d "$plugindir/nextend-smart-slider3-pro" ]; then
        ((found_pro++))
        version=$(grep -o "Version: .*" "$plugindir/nextend-smart-slider3-pro/nextend-smart-slider3-pro.php" 2>/dev/null | head -1 | cut -d' ' -f2)

        echo "[WARNING] SMART SLIDER 3 PRO FOUND" | tee -a "$results_file"
        echo "  User: $site_user" | tee -a "$results_file"
        echo "  Path: $wpdir" | tee -a "$results_file"
        echo "  Version: ${version:-Unknown}" | tee -a "$results_file"

        # Check if it's active
        if grep -q "nextend-smart-slider3-pro" "$wpdir/wp-content/plugins" 2>/dev/null; then
            echo "  Status: Likely Active" | tee -a "$results_file"
        fi
        echo "" | tee -a "$results_file"

    # Check for Smart Slider 3 FREE
    elif [ -d "$plugindir/smart-slider-3" ]; then
        ((found_free++))
        version=$(grep -o "Version: .*" "$plugindir/smart-slider-3/smart-slider-3.php" 2>/dev/null | head -1 | cut -d' ' -f2)

        echo "[INFO] Smart Slider 3 (Free) Found" | tee -a "$results_file"
        echo "  User: $site_user" | tee -a "$results_file"
        echo "  Path: $wpdir" | tee -a "$results_file"
        echo "  Version: ${version:-Unknown}" | tee -a "$results_file"
        echo "" | tee -a "$results_file"
    fi
done

echo "=============================================="
echo "[OK] Scan Complete"
echo ""
echo "SUMMARY:"
echo "  Total WordPress sites: $total_wp"
echo "  Smart Slider 3 Pro: $found_pro"
echo "  Smart Slider 3 Free: $found_free"
echo ""

if [ $found_pro -gt 0 ]; then
    echo "[WARNING] SECURITY ALERT:"
    echo "  Smart Slider 3 Pro was compromised April 7-9, 2026"
    echo "  Sites with this plugin may have been infected"
    echo "  IMMEDIATE ACTION REQUIRED:"
    echo "    1. Update Smart Slider 3 Pro to latest version"
    echo "    2. Check for unauthorized users/backdoors"
    echo "    3. Review recent file modifications"
    echo "    4. Scan for malware"
fi

echo ""
echo "Results saved to: $results_file"
