# ============================================================
# eSurat PPM — Apache .htaccess
# cPanel Rumahweb Hosting
# ============================================================

Options -Indexes
ServerSignature Off

# ---- Blokir akses ke folder sensitif ----
<FilesMatch "(^error_log$|^\.installed$|\.(sql|log|env|json|lock|md|sh|ini|php\.bak)$)">
    Order allow,deny
    Deny from all
</FilesMatch>

# ---- Proteksi folder includes ----
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /esurat/

    # ---- Paksa HTTPS (hindari interstitial port 80) ----
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

    # ---- Redirect domain teknis cPanel -> domain resmi ----
    # Aktifkan 2 baris di bawah setelah subdomain resmi dibuat di cPanel,
    # lalu sesuaikan juga BASE_URL di includes/config.php
    # RewriteCond %{HTTP_HOST} \.cpanel\.site$ [NC]
    # RewriteRule ^(.*)$ https://esurat.palmapertiwi.com/$1 [R=301,L]

    # PENTING: Teruskan Authorization header ke PHP
    # (shared hosting Apache sering strip header ini)
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Blokir akses langsung ke includes/ dan storage/
    RewriteRule ^includes/ - [F,L]
    RewriteRule ^storage/ - [F,L]

    # Blokir akses langsung ke uploads/ via URL (bukan rewrite block)
    RewriteRule ^uploads/.*\.php$ - [F,L]

    # API routing: /api/auth/login → /api/index.php?ep=auth/login
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^api/(.+)$ api/index.php?ep=$1 [QSA,L]

    # SPA frontend: semua non-file → index.html
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/esurat/api/
    RewriteRule ^ index.html [L]
</IfModule>

# ---- Security headers ----
<IfModule mod_headers.c>
    Header always set X-Frame-Options "SAMEORIGIN"
    Header always set X-Content-Type-Options "nosniff"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    Header always set Permissions-Policy "geolocation=(), microphone=(), camera=()"
    # HSTS: hanya dikirim saat koneksi HTTPS
    Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains" env=HTTPS
    # Content Security Policy: batasi sumber skrip/gaya hanya self + CDN tepercaya
    Header always set Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' https://cdnjs.cloudflare.com; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com; img-src 'self' data: blob:; connect-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'self'"
</IfModule>

# ---- Gzip compression ----
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/css application/javascript application/json
</IfModule>

# ---- Browser caching ----
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType text/css "access plus 7 days"
    ExpiresByType application/javascript "access plus 7 days"
    ExpiresByType image/png "access plus 30 days"
    ExpiresByType image/svg+xml "access plus 30 days"
    ExpiresByType application/json "access plus 0 seconds"
    ExpiresByType application/manifest+json "access plus 7 days"
</IfModule>

<IfModule mod_mime.c>
    AddType application/manifest+json .webmanifest
</IfModule>
