# =============================================================================
# Srinath Bhawan Rental Management System - Apache Configuration
# =============================================================================

# Security Headers
<IfModule mod_headers.c>
    Header always set X-Content-Type-Options nosniff
    Header always set X-Frame-Options DENY
    Header always set X-XSS-Protection "1; mode=block"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    
    # HTTPS Security (uncomment when SSL is enabled)
    # Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
    
    # Content Security Policy (updated for CDN support)
    Header always set Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://cdn.jsdelivr.net https://cdnjs.cloudflare.com https://maxcdn.bootstrapcdn.com https://stackpath.bootstrapcdn.com; style-src 'self' 'unsafe-inline' https://cdn.jsdelivr.net https://cdnjs.cloudflare.com https://maxcdn.bootstrapcdn.com https://stackpath.bootstrapcdn.com https://fonts.googleapis.com; font-src 'self' data: https://cdnjs.cloudflare.com https://fonts.gstatic.com https://maxcdn.bootstrapcdn.com https://stackpath.bootstrapcdn.com; img-src 'self' data: https:; connect-src 'self' https://cdn.jsdelivr.net https://cdnjs.cloudflare.com;"
</IfModule>

# Hide sensitive files
<Files ".env">
    Order allow,deny
    Deny from all
</Files>

<Files ".env.example">
    Order allow,deny
    Deny from all
</Files>

<Files "*.sql">
    Order allow,deny
    Deny from all
</Files>

<Files "*.md">
    Order allow,deny
    Deny from all
</Files>

<Files "composer.json">
    Order allow,deny
    Deny from all
</Files>

<Files "composer.lock">
    Order allow,deny
    Deny from all
</Files>

# Note: <Directory> blocks are not allowed in .htaccess; removed to avoid 500 errors

# PHP Security Settings
<IfModule mod_php7.c>
    php_flag display_errors Off
    php_flag log_errors On
    php_value error_log logs/php_errors.log
    php_flag expose_php Off
    php_flag allow_url_fopen Off
    php_flag allow_url_include Off
</IfModule>

<IfModule mod_php8.c>
    php_flag display_errors Off
    php_flag log_errors On
    php_value error_log logs/php_errors.log
    php_flag expose_php Off
    php_flag allow_url_fopen Off
    php_flag allow_url_include Off
</IfModule>

# File Upload Security
<IfModule mod_php7.c>
    php_value upload_max_filesize 10M
    php_value post_max_size 12M
    php_value max_execution_time 60
    php_value memory_limit 256M
    php_value max_input_vars 3000
</IfModule>

<IfModule mod_php8.c>
    php_value upload_max_filesize 10M
    php_value post_max_size 12M
    php_value max_execution_time 60
    php_value memory_limit 256M
    php_value max_input_vars 3000
</IfModule>

# Prevent access to PHP files in uploads directory (no <Directory> here; path-based match)
<FilesMatch "^uploads/.*\.(php|phtml|php3|php4|php5)$">
    Require all denied
</FilesMatch>

# Enable Compression
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/x-javascript
</IfModule>

# Browser Caching
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/svg+xml "access plus 1 month"
    ExpiresByType image/x-icon "access plus 1 year"
</IfModule>

# URL Rewriting (if needed for clean URLs)
<IfModule mod_rewrite.c>
    RewriteEngine On
    
    # Force HTTPS (uncomment when SSL is available)
    # RewriteCond %{HTTPS} off
    # RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    # Redirect to login if accessing root
    RewriteCond %{REQUEST_URI} ^/$
    RewriteRule ^(.*)$ auth/login.php [L,R=302]
    
    # Handle missing files gracefully
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !\.(css|js|png|jpg|jpeg|gif|ico|svg)$
    RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

# Prevent hotlinking (disabled for local development to avoid 403 on localhost images)
## If needed in production, re-enable and whitelist your actual domain
## <IfModule mod_rewrite.c>
##     RewriteCond %{HTTP_REFERER} !^$
##     RewriteCond %{HTTP_REFERER} !^https?://(www\.)?yourdomain\.com [NC]
##     RewriteRule \.(jpg|jpeg|png|gif|svg)$ - [F,L]
## </IfModule>

# Custom Error Pages
ErrorDocument 404 /error/404.html
ErrorDocument 403 /error/403.html
ErrorDocument 500 /error/500.html

# Disable server signature
ServerSignature Off

# Limit request size
LimitRequestBody 12582912
