|
Bearsampp 2026.3.26
API documentation
|
Static Public Member Functions | |
| static | generateToken () |
| static | getStats () |
| static | getToken () |
| static | getTokenField () |
| static | getTokenMeta () |
| static | init () |
| static | regenerateToken () |
| static | validateOrDie ($removeAfterValidation=false) |
| static | validateRequest ($removeAfterValidation=false) |
| static | validateToken ($token, $removeAfterValidation=false) |
Data Fields | |
| const | MAX_TOKENS = 10 |
| const | SESSION_KEY = 'bearsampp_csrf_tokens' |
| const | TOKEN_EXPIRATION = 7200 |
Static Private Member Functions | |
| static | cleanupExpiredTokens () |
| static | getAllHeaders () |
Class Csrf
Provides CSRF (Cross-Site Request Forgery) protection for the Bearsampp application. This class handles token generation, validation, and management to prevent CSRF attacks.
Features:
Usage:
Definition at line 34 of file class.csrf.php.
|
staticprivate |
Removes expired tokens from the session.
Definition at line 252 of file class.csrf.php.
References Util\logDebug().
Referenced by init().
|
static |
Generates a new CSRF token and stores it in the session.
| Exception | If random_bytes() fails |
Definition at line 80 of file class.csrf.php.
References init(), Util\logDebug(), and Util\logError().
Referenced by getToken(), and regenerateToken().
|
staticprivate |
Gets all HTTP headers in a cross-compatible way. Works with both Apache and FastCGI/CGI environments.
Definition at line 218 of file class.csrf.php.
Referenced by validateRequest().
|
static |
Gets statistics about current CSRF tokens. Useful for debugging and monitoring.
Definition at line 341 of file class.csrf.php.
References init().
|
static |
Gets the current CSRF token, generating a new one if none exists.
Definition at line 114 of file class.csrf.php.
References generateToken(), and init().
Referenced by getTokenField(), and getTokenMeta().
|
static |
Gets the token as a hidden input field for forms.
Definition at line 297 of file class.csrf.php.
References getToken().
|
static |
Gets the token as a meta tag for inclusion in HTML head. Useful for AJAX requests.
Definition at line 309 of file class.csrf.php.
References getToken().
|
static |
Initializes the CSRF protection system. Starts the session if not already started and cleans up expired tokens.
Definition at line 58 of file class.csrf.php.
References cleanupExpiredTokens().
Referenced by generateToken(), getStats(), getToken(), regenerateToken(), and validateToken().
|
static |
Regenerates the CSRF token. Useful after sensitive operations or login.
Definition at line 281 of file class.csrf.php.
References generateToken(), and init().
|
static |
Validates request and sends JSON error response if validation fails. This is a convenience method for AJAX endpoints.
| bool | $removeAfterValidation | Whether to remove the token after successful validation |
Definition at line 322 of file class.csrf.php.
References exit.
|
static |
Validates a CSRF token from the request (POST or GET). Checks $_POST['csrf_token'] first, then $_GET['csrf_token'], then headers.
| bool | $removeAfterValidation | Whether to remove the token after successful validation |
Definition at line 186 of file class.csrf.php.
References getAllHeaders(), Util\logWarning(), and validateToken().
|
static |
Validates a CSRF token.
| string | null | $token | The token to validate |
| bool | $removeAfterValidation | Whether to remove the token after successful validation (one-time use) |
Definition at line 144 of file class.csrf.php.
References init(), Util\logDebug(), and Util\logWarning().
Referenced by validateRequest().
| const MAX_TOKENS = 10 |
Maximum number of tokens to store per session This prevents session bloat from token accumulation
Definition at line 50 of file class.csrf.php.
| const SESSION_KEY = 'bearsampp_csrf_tokens' |
Session key for storing CSRF tokens
Definition at line 39 of file class.csrf.php.
| const TOKEN_EXPIRATION = 7200 |
Token expiration time in seconds (default: 2 hours)
Definition at line 44 of file class.csrf.php.