# Barbra's Kitchen - Security Configuration
# ==============================================

# Enable mod_rewrite
<IfModule mod_rewrite.c>
  RewriteEngine On
</IfModule>

# =====================================================
# 1. PREVENT DIRECTORY LISTING
# =====================================================
Options -Indexes

# =====================================================
# 2. BLOCK DIRECT ACCESS TO SENSITIVE FILES
# =====================================================
<FilesMatch "\.(php|env|json|txt|bak|sql|pdf)$">
  <IfModule mod_authz_core.c>
    Require all denied
  </IfModule>
  <IfModule !mod_authz_core.c>
    Order allow,deny
    Deny from all
  </IfModule>
</FilesMatch>

# Allow access to specific files
<Files "index.php">
  <IfModule mod_authz_core.c>
    Require all granted
  </IfModule>
  <IfModule !mod_authz_core.c>
    Order allow,deny
    Allow from all
  </IfModule>
</Files>

<Files "insert_order.php">
  <IfModule mod_authz_core.c>
    Require all granted
  </IfModule>
  <IfModule !mod_authz_core.c>
    Order allow,deny
    Allow from all
  </IfModule>
</Files>

# =====================================================
# 3. PROTECT CONFIG FILES
# =====================================================
<Files "config.php">
  <IfModule mod_authz_core.c>
    Require all denied
  </IfModule>
  <IfModule !mod_authz_core.c>
    Order allow,deny
    Deny from all
  </IfModule>
</Files>

# =====================================================
# 4. ALLOW IMAGE FILES
# =====================================================
<FilesMatch "\.(jpg|jpeg|png|gif|webp|svg|ico)$">
  <IfModule mod_authz_core.c>
    Require all granted
  </IfModule>
  <IfModule !mod_authz_core.c>
    Order allow,deny
    Allow from all
  </IfModule>
</FilesMatch>

# =====================================================
# 5. PREVENT HOTLINKING OF IMAGES
# =====================================================
<IfModule mod_rewrite.c>
  RewriteCond %{HTTP_REFERER} !^$
  RewriteCond %{HTTP_REFERER} !^https?://(?:www\.)?barbraskitchen\.com [NC]
  RewriteCond %{HTTP_REFERER} !^https?://(?:www\.)?ichh\.or\.tz [NC]
  RewriteCond %{HTTP_REFERER} !^http://localhost [NC]
  RewriteCond %{HTTP_REFERER} !^http://127\.0\.0\.1 [NC]
  RewriteRule \.(jpg|jpeg|png|gif|webp)$ - [NC,F,L]
</IfModule>

# =====================================================
# 6. SECURITY HEADERS
# =====================================================
<IfModule mod_headers.c>
  # Prevent MIME type sniffing
  Header set X-Content-Type-Options "nosniff"
  
  # Enable XSS protection
  Header set X-XSS-Protection "1; mode=block"
  
  # Prevent clickjacking
  Header set X-Frame-Options "SAMEORIGIN"
  
  # Control referrer information
  Header set Referrer-Policy "strict-origin-when-cross-origin"
</IfModule>

# =====================================================
# 7. COMPRESSION
# =====================================================
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/xml
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/xml
</IfModule>

# =====================================================
# 8. CACHING
# =====================================================
<IfModule mod_expires.c>
  ExpiresActive On
  
  # Images
  ExpiresByType image/jpeg "access plus 1 year"
  ExpiresByType image/png "access plus 1 year"
  ExpiresByType image/gif "access plus 1 year"
  ExpiresByType image/webp "access plus 1 year"
  ExpiresByType image/svg+xml "access plus 1 year"
  
  # CSS and JavaScript
  ExpiresByType text/css "access plus 1 month"
  ExpiresByType application/javascript "access plus 1 month"
  
  # Default
  ExpiresDefault "access plus 2 days"
</IfModule>

# =====================================================
# 9. FORCE HTTPS (Uncomment if using SSL)
# =====================================================
# <IfModule mod_rewrite.c>
#   RewriteCond %{HTTPS} off
#   RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# </IfModule>
<Files "manifest.json">
  <IfModule mod_authz_core.c>
    Require all granted
  </IfModule>
  <IfModule !mod_authz_core.c>
    Order allow,deny
    Allow from all
  </IfModule>
</Files>
