#!/bin/bash
# Emergency Session Purge Script - Safe Deletion Mode

echo "====================================================="
echo "[!] INITIATING SECURE SESSION PURGE"
echo "====================================================="

# Check if the directories actually exist before trying to delete anything
if [ ! -d "/var/cpanel/sessions/raw" ] || [ ! -d "/var/cpanel/sessions/cache" ] || [ ! -d "/var/cpanel/sessions/preauth" ]; then
    echo "[-] Error: Expected cPanel session directories do not exist!"
    echo "[-] Aborting script to prevent unintended file modifications."
    exit 1
fi

echo "[+] Purging active cPanel/WHM sessions (Cache, Raw, Preauth)..."

# SECURE DELETION METHOD:
# Instead of (which can be dangerous if mistyped or parsed wrongly),
# we use 'find' to locate only 'files' (-type f) inside the specific absolute directories 
# and delete them. This guarantees no directories or outside files are touched.

find /var/cpanel/sessions/raw/ -mindepth 1 -type f -exec rm -f {} +
find /var/cpanel/sessions/cache/ -mindepth 1 -type f -exec rm -f {} +
find /var/cpanel/sessions/preauth/ -mindepth 1 -type f -exec rm -f {} +

echo "[+] All session files safely deleted."

echo "[+] Restarting cPanel service (cpsrvd) to enforce immediate logout..."
/scripts/restartsrv_cpsrvd

echo "====================================================="
echo "[+] DONE! All users have been successfully logged out."
echo "====================================================="