# ============================================================================
#  kafaalat/.htaccess
#  v9.x — للسيرفر المشترك (cPanel + Apache)
# ============================================================================

# ── 1. فعِّل rewrite ─────────────────────────────────────────────
RewriteEngine On
RewriteBase /kafaalat/

# ── 2. لا تحوِّل المجلدات والملفات الموجودة فعلاً ──────────────────
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# ── 3. أمان: امنع الوصول للملفات الحساسة ────────────────────────
<FilesMatch "^(config\.php|\.htaccess|\.env|.*\.sql|.*\.md)$">
    Require all denied
</FilesMatch>

# ── 4. امنع الوصول لمجلد api/lib مباشرة ─────────────────────────
RedirectMatch 403 ^/kafaalat/api/lib/?$
RedirectMatch 403 ^/kafaalat/api/lib/.*\.php$

# ── 5. ترميز UTF-8 افتراضي ──────────────────────────────────────
AddDefaultCharset UTF-8
AddType "text/html; charset=UTF-8" .html .htm .php
AddType "application/json; charset=UTF-8" .json

# ── 6. إعدادات PHP الأساسية ─────────────────────────────────────
<IfModule mod_php.c>
    php_value default_charset "UTF-8"
    php_value upload_max_filesize 20M
    php_value post_max_size 25M
    php_value max_execution_time 120
    php_value memory_limit 256M
    php_value session.cookie_path "/kafaalat/"
    php_value session.cookie_httponly 1
    php_value session.cookie_secure 1
    php_value session.cookie_samesite "Lax"
</IfModule>

# ── 7. حماية إضافية ─────────────────────────────────────────────
<IfModule mod_headers.c>
    Header set X-Content-Type-Options "nosniff"
    Header set X-Frame-Options "SAMEORIGIN"
    Header set Referrer-Policy "same-origin"
    # CORS: السماح من نفس الدومين فقط
    Header set Access-Control-Allow-Origin "https://alnajat.net"
    Header set Access-Control-Allow-Credentials "true"
</IfModule>

# ── 8. ضغط للأداء ───────────────────────────────────────────────
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/css text/javascript application/javascript application/json
</IfModule>

# ── 9. Cache للأصول الثابتة ─────────────────────────────────────
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/svg+xml "access plus 1 month"
    ExpiresByType text/css "access plus 1 week"
    ExpiresByType application/javascript "access plus 1 week"
    # لا cache لـ PHP و JSON
    ExpiresByType application/json "access plus 0 seconds"
</IfModule>

# ── 10. صفحة افتراضية ──────────────────────────────────────────
DirectoryIndex index.php index.html

# ============================================================================
# ملاحظات:
# - RewriteBase /kafaalat/  مهم لأن التطبيق في subdirectory
# - session.cookie_path /kafaalat/  يحدد الـ session على هذا المجلد فقط
# - api/lib محمي من الوصول المباشر (تكلِّم معه عبر api/reports.php فقط)
# - إذا كان التطبيق على دومين مستقل، غيِّر RewriteBase إلى /
# ============================================================================
