#!/bin/bash
# ==========================
# Script bảo mật WordPress
# ==========================

# 1. Tìm đường dẫn thư mục hiện tại
SITE_PATH=$(pwd)

echo "🔍 Đang bảo mật WordPress tại: $SITE_PATH"
echo "=========================================="

# 2. Đổi quyền file cấu hình chính
if [ -f "$SITE_PATH/wp-config.php" ]; then
    chmod 444 "$SITE_PATH/wp-config.php"
    echo "✅ Đã khóa wp-config.php (444)"
fi

# 3. Đổi quyền core WordPress
find "$SITE_PATH/wp-admin" "$SITE_PATH/wp-includes" -type f -exec chmod 444 {} \;
echo "✅ Đã khóa toàn bộ file trong wp-admin & wp-includes (444)"

# 4. Khóa file PHP trong thư mục gốc (trừ index.php và wp-cron.php)
find "$SITE_PATH" -maxdepth 1 -type f -name "*.php" ! -name "index.php" ! -name "wp-cron.php" -exec chmod 444 {} \;
echo "✅ Đã khóa file PHP gốc"

# 5. Giữ nguyên quyền wp-content để đăng bài, upload ảnh
find "$SITE_PATH/wp-content" -type f -exec chmod 644 {} \;
find "$SITE_PATH/wp-content" -type d -exec chmod 755 {} \;
echo "✅ Đã giữ nguyên quyền cho wp-content"

# 6. Xuất báo cáo
REPORT="$SITE_PATH/bao_cao_quyen.txt"
find "$SITE_PATH" -type f -exec ls -l {} \; > "$REPORT"
echo "📄 Báo cáo lưu tại: $REPORT"

echo "🎯 Hoàn tất bảo mật!"
