📁 File Manager
Editing:
/home/wvl7mof7402e/www/chem/admin/includes/csrf.php
<?php declare(strict_types=1); /** * Pure Chemical Admin - CSRF Protection * * File: * admin/includes/csrf.php */ /* |-------------------------------------------------------------------------- | Start Session |-------------------------------------------------------------------------- */ if (session_status() !== PHP_SESSION_ACTIVE) { session_start(); } /* |-------------------------------------------------------------------------- | Generate CSRF Token |-------------------------------------------------------------------------- */ if (!function_exists('csrf_token')) { function csrf_token(): string { if ( empty($_SESSION['csrf_token']) || !is_string($_SESSION['csrf_token']) ) { $_SESSION['csrf_token'] = bin2hex( random_bytes(32) ); } return $_SESSION['csrf_token']; } } /* |-------------------------------------------------------------------------- | CSRF Hidden Input |-------------------------------------------------------------------------- */ if (!function_exists('csrf_field')) { function csrf_field(): string { return sprintf( '<input type="hidden" name="csrf_token" value="%s">', htmlspecialchars( csrf_token(), ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8' ) ); } } /* |-------------------------------------------------------------------------- | Verify CSRF Token |-------------------------------------------------------------------------- */ if (!function_exists('csrf_verify')) { function csrf_verify( ?string $token = null ): bool { $sessionToken = $_SESSION['csrf_token'] ?? ''; if ( !is_string($sessionToken) || $sessionToken === '' ) { return false; } if ($token === null) { $token = $_POST['csrf_token'] ?? $_SERVER['HTTP_X_CSRF_TOKEN'] ?? ''; } if ( !is_string($token) || $token === '' ) { return false; } return hash_equals( $sessionToken, $token ); } } /* |-------------------------------------------------------------------------- | Require Valid CSRF Token |-------------------------------------------------------------------------- */ if (!function_exists('requireCsrf')) { function requireCsrf( ?string $token = null ): void { if (!csrf_verify($token)) { http_response_code(419); /* * AJAX / JSON request */ $accept = $_SERVER['HTTP_ACCEPT'] ?? ''; if ( str_contains( strtolower($accept), 'application/json' ) || !empty($_SERVER['HTTP_X_REQUESTED_WITH']) ) { header( 'Content-Type: application/json; charset=utf-8' ); echo json_encode([ 'success' => false, 'message' => 'Invalid or expired security token.', ]); exit; } exit( 'Invalid or expired security token. Please refresh the page and try again.' ); } } } /* |-------------------------------------------------------------------------- | Rotate CSRF Token |-------------------------------------------------------------------------- | | Useful after sensitive authentication events. | */ if (!function_exists('csrf_regenerate')) { function csrf_regenerate(): string { $_SESSION['csrf_token'] = bin2hex( random_bytes(32) ); return $_SESSION['csrf_token']; } } /* |-------------------------------------------------------------------------- | JavaScript-Friendly Token |-------------------------------------------------------------------------- */ if (!function_exists('csrf_token_json')) { function csrf_token_json(): string { return json_encode( csrf_token(), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); } }
Save
Cancel