Documentation

legacy_helpers.php

Helper Functions - Compatibility Layer

Provides convenient global functions that wrap the OOP architecture. These functions are maintained for backward compatibility and convenience.

While the OOP approach (using $csrf, $auth in views, and BaseController methods) is recommended, these functions remain available as a stable compatibility layer.

Tags
version
1.0.0
author

BDELIVE - Group 8

see
Application

For the OOP architecture

see
BaseController

For controller base class

Table of Contents

Functions

csrfField()  : string
Generate a CSRF token hidden input field
validateCsrfToken()  : bool
Validate a CSRF token
generateCsrfToken()  : string
Generate a new CSRF token
requireLogin()  : void
Require user authentication
requireAdmin()  : void
Require administrator rights (BDE)

Functions

csrfField()

Generate a CSRF token hidden input field

csrfField() : string

Convenience function that wraps CsrfProtection::getTokenField().

Tags
see
CsrfProtection::getTokenField()
example

// Or use OOP approach in views:

getTokenField() ?>
Return values
string

HTML hidden input with CSRF token

validateCsrfToken()

Validate a CSRF token

validateCsrfToken(string $token) : bool

Convenience function that wraps CsrfProtection::validateToken().

Parameters
$token : string

The CSRF token to validate

Tags
see
CsrfProtection::validateToken()
example

if (validateCsrfToken($_POST['csrf_token'])) { // Token is valid }

// Or use OOP approach in controllers: if ($this->csrf->validateToken($token)) { ... }

Return values
bool

True if valid, false otherwise

generateCsrfToken()

Generate a new CSRF token

generateCsrfToken() : string

Convenience function that wraps CsrfProtection::generateToken().

Tags
see
CsrfProtection::generateToken()
example

$token = generateCsrfToken();

// Or use OOP approach in controllers: $token = $this->csrf->generateToken();

Return values
string

The generated CSRF token (64 hex characters)

requireLogin()

Require user authentication

requireLogin() : void

Checks if user is authenticated and redirects to login page if not. Convenience function that wraps AuthManager::requireAuthentication().

Tags
see
AuthManager::requireAuthentication()
see
AuthenticatedController

For OOP approach

example

// In procedural code: requireLogin();

// Or use OOP approach - extend AuthenticatedController: class MyController extends AuthenticatedController { // Authentication checked automatically in constructor }

Return values
void

Redirects to login page if not authenticated

requireAdmin()

Require administrator rights (BDE)

requireAdmin() : void

Checks if user is authenticated and has admin (BDE) status. Redirects to login or home page with error message if requirements not met. Convenience function that wraps AuthManager::requireAdmin().

Tags
see
AuthManager::requireAdmin()
see
AdminController

For OOP approach

example

// In procedural code: requireAdmin();

// Or use OOP approach - extend AdminController: class MyAdminController extends AdminController { // Admin rights checked automatically in constructor }

Return values
void

Redirects if not authenticated or not admin


        
On this page

Search results