# ==================================================================================================
# TaxiCRM - Apache configuration
# --------------------------------------------------------------------------------------------------
# Everything here is defensive. The application also sets its own security headers from PHP
# (includes/bootstrap.php), so it stays protected on nginx or any server that ignores .htaccess --
# these rules cover the static files PHP never sees.
# ==================================================================================================

Options -Indexes -MultiViews

DirectoryIndex index.php index.html

# --- Never serve source, schema, docs, or version-control data ------------------------------------
<FilesMatch "^(config\.php|config\.sample\.php|schema\.sql|composer\.(json|lock)|\.env.*|.*\.(sql|md|log|bak|old|ini|sh|yml|yaml|dist))$">
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
    <IfModule !mod_authz_core.c>
        Order allow,deny
        Deny from all
    </IfModule>
</FilesMatch>

# Version-control and editor folders are never public. (includes/ and uploads/ carry their own
# .htaccess that denies everything; this covers a stray .git copied to the server.)
<IfModule mod_alias.c>
    RedirectMatch 404 (^|/)(\.git|\.idea|\.vscode|tools|docs)(/|$)
</IfModule>

# Dotfiles (.htaccess, .gitignore, editor droppings) are never public.
<FilesMatch "^\.">
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
    <IfModule !mod_authz_core.c>
        Order allow,deny
        Deny from all
    </IfModule>
</FilesMatch>

# --- Security headers for static responses -----------------------------------------------------------
<IfModule mod_headers.c>
    Header always set X-Content-Type-Options "nosniff"
    Header always set X-Frame-Options "DENY"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    Header always unset X-Powered-By
    Header unset Server

    # Uncomment once the site is served over HTTPS with a valid certificate.
    # Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
</IfModule>

# --- Cache the vendored CSS/JS/fonts, never the pages -------------------------------------------------
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType text/css               "access plus 1 year"
    ExpiresByType application/javascript "access plus 1 year"
    ExpiresByType font/woff2             "access plus 1 year"
    ExpiresByType image/svg+xml          "access plus 1 month"
    ExpiresByType text/html              "access plus 0 seconds"
</IfModule>

<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml application/javascript application/json image/svg+xml
</IfModule>

# --- PHP hardening, where the host allows per-directory overrides --------------------------------------
<IfModule mod_php.c>
    php_flag display_errors off
    php_flag log_errors on
    php_flag session.cookie_httponly on
    php_flag session.use_strict_mode on
    php_value session.cookie_samesite "Lax"
    php_value upload_max_filesize 52M
    php_value post_max_size 56M
</IfModule>

# --- Force HTTPS (uncomment once a certificate is installed) --------------------------------------------
# <IfModule mod_rewrite.c>
#     RewriteEngine On
#     RewriteCond %{HTTPS} !=on
#     RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# </IfModule>
