# WeaverSM Security & URL Rewriting
# ===================================

# Disable directory browsing
Options -Indexes

# Protect sensitive files
<FilesMatch "^\.">
    Order allow,deny
    Deny from all
</FilesMatch>

# Block access to sensitive files
<FilesMatch "(\.env|\.git|\.sql|\.log|composer\.(json|lock))$">
    Order allow,deny
    Deny from all
</FilesMatch>

# Block access to config and classes directly
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    
    # Block direct access to sensitive directories
    RewriteRule ^(config|classes|includes|logs|migrations)/ - [F,L]
    
    # Allow webhook files to be accessed with .php
    RewriteRule ^api/oxapay-webhook\.php$ - [L]
    RewriteRule ^api/chat\.php$ - [L]
    RewriteRule ^api/sms-sync\.php$ - [L]
    
    # Remove .php extension
    # If the request doesn't have .php and is not a file or directory
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.+)$ $1.php [L,QSA]
    
    # Redirect .php URLs to clean URLs (except allowed ones)
    RewriteCond %{THE_REQUEST} \s/+(.*?)\.php[\s?] [NC]
    RewriteCond %{REQUEST_URI} !^/api/ [NC]
    RewriteRule ^ /%1 [R=301,L]
</IfModule>

# Security Headers
<IfModule mod_headers.c>
    # Prevent clickjacking
    Header always set X-Frame-Options "SAMEORIGIN"
    
    # Prevent MIME type sniffing
    Header always set X-Content-Type-Options "nosniff"
    
    # Enable XSS filter
    Header always set X-XSS-Protection "1; mode=block"
    
    # Referrer policy
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    
    # Remove server signature
    Header unset Server
    Header unset X-Powered-By
    
    # Permissions Policy
    Header always set Permissions-Policy "geolocation=(), microphone=(), camera=()"
</IfModule>

# PHP settings (if allowed)
<IfModule mod_php.c>
    php_flag display_errors off
    php_flag log_errors on
    php_flag expose_php off
    php_value session.cookie_httponly 1
    php_value session.cookie_secure 1
    php_value session.use_strict_mode 1
</IfModule>

# Block common attacks
<IfModule mod_rewrite.c>
    # Block SQL injection attempts
    RewriteCond %{QUERY_STRING} (union|select|insert|drop|delete|update|alter|create|truncate) [NC]
    RewriteRule .* - [F,L]
    
    # Block script injection
    RewriteCond %{QUERY_STRING} (<|%3C).*script.*(>|%3E) [NC,OR]
    RewriteCond %{QUERY_STRING} GLOBALS(=|[|%[0-9A-Z]{0,2}) [OR]
    RewriteCond %{QUERY_STRING} _REQUEST(=|[|%[0-9A-Z]{0,2})
    RewriteRule .* - [F,L]
</IfModule>

# Protect uploads directory
<IfModule mod_rewrite.c>
    RewriteCond %{REQUEST_URI} ^/uploads/
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteCond %{REQUEST_FILENAME} \.(php|phtml|php3|php4|php5|php7|phps|cgi|pl|asp|aspx|shtml|shtm|fcgi|fpl|jsp|htm|html|wml)$ [NC]
    RewriteRule .* - [F,L]
</IfModule>

# Enable compression
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/json
</IfModule>

# Cache static files
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/webp "access plus 1 month"
    ExpiresByType text/css "access plus 1 week"
    ExpiresByType application/javascript "access plus 1 week"
</IfModule>
