Documentation

Globals

Centralized management of LWT global variables.

This class encapsulates all global state used throughout LWT, making dependencies explicit and easier to track.

Usage:

// Get database connection
$db = Globals::getDbConnection();

// Get current user ID
$userId = Globals::getCurrentUserId();

// Require user ID (throws if not authenticated)
$userId = Globals::requireUserId();
Tags
category

Lwt

author

HugoFara hugo.farajallah@protonmail.com

license

Unlicense http://unlicense.org/

link
https://hugofara.github.io/lwt/developer/api
since
3.0.0

Table of Contents

Properties

$backupRestoreEnabled  : bool|null
Whether backup restore is enabled.
$currentUserId  : int|null
Current authenticated user ID
$currentUserIsAdmin  : bool
Whether the current authenticated user is an admin.
$databaseName  : string
Database name
$dbConnection  : mysqli|null
Database connection object
$errorDisplayEnabled  : bool
Whether error display is enabled
$initialized  : bool
Whether globals have been initialized
$multiUserEnabled  : bool
Whether multi-user mode is enabled

Methods

getCurrentUserId()  : int|null
Get the current authenticated user ID.
getDatabaseName()  : string
Get the database name.
getDbConnection()  : mysqli|null
Get the database connection.
initialize()  : void
Initialize all global variables.
isAuthenticated()  : bool
Check if a user is currently authenticated.
isBackupRestoreEnabled()  : bool
Check if backup restore is enabled.
isCurrentUserAdmin()  : bool
Check if the current authenticated user is an admin.
isErrorDisplayEnabled()  : bool
Check if error display is enabled.
isMultiUserEnabled()  : bool
Check if multi-user mode is enabled.
languageBelongsToCurrentUser()  : bool
Check whether the given language belongs to the current user.
query()  : QueryBuilder
Get a query builder instance for a table.
requireUserId()  : int
Get the current user ID, throwing if not authenticated.
reset()  : void
Reset all globals to initial state.
setBackupRestoreEnabled()  : void
Set whether backup restore is enabled.
setCurrentUserId()  : void
Set the current authenticated user ID.
setCurrentUserIsAdmin()  : void
Set whether the current user is an admin.
setDatabaseName()  : void
Set the database name.
setDbConnection()  : void
Set the database connection.
setErrorDisplay()  : void
Enable or disable error display.
setMultiUserEnabled()  : void
Enable multi-user mode.
table()  : string
Get a table name.

Properties

$backupRestoreEnabled

Whether backup restore is enabled.

private static bool|null $backupRestoreEnabled = null

Disabled by default in multi-user mode for security.

Null means use default based on multi-user mode

$currentUserId

Current authenticated user ID

private static int|null $currentUserId = null

$currentUserIsAdmin

Whether the current authenticated user is an admin.

private static bool $currentUserIsAdmin = false

$databaseName

Database name

private static string $databaseName = ''

$dbConnection

Database connection object

private static mysqli|null $dbConnection = null

$errorDisplayEnabled

Whether error display is enabled

private static bool $errorDisplayEnabled = false

$initialized

Whether globals have been initialized

private static bool $initialized = false

$multiUserEnabled

Whether multi-user mode is enabled

private static bool $multiUserEnabled = false

When enabled, user_id filtering is applied to queries.

Methods

getCurrentUserId()

Get the current authenticated user ID.

public static getCurrentUserId() : int|null

Returns null if no user is authenticated.

Tags
since
3.0.0
Return values
int|null

The current user ID or null

getDatabaseName()

Get the database name.

public static getDatabaseName() : string
Return values
string

The database name

getDbConnection()

Get the database connection.

public static getDbConnection() : mysqli|null
Return values
mysqli|null

The database connection object

initialize()

Initialize all global variables.

public static initialize() : void

This should be called once during application bootstrap.

isAuthenticated()

Check if a user is currently authenticated.

public static isAuthenticated() : bool
Tags
since
3.0.0
Return values
bool

True if a user is authenticated

isBackupRestoreEnabled()

Check if backup restore is enabled.

public static isBackupRestoreEnabled() : bool

In multi-user mode, backup restore is disabled by default for security. Single-user mode allows restore by default. This can be overridden by explicitly setting BACKUP_RESTORE_ENABLED.

Tags
since
3.0.0
Return values
bool

True if backup restore is enabled

isCurrentUserAdmin()

Check if the current authenticated user is an admin.

public static isCurrentUserAdmin() : bool
Tags
since
3.0.0
Return values
bool

True if the current user is an admin

isErrorDisplayEnabled()

Check if error display is enabled.

public static isErrorDisplayEnabled() : bool
Return values
bool

True if error display is on

isMultiUserEnabled()

Check if multi-user mode is enabled.

public static isMultiUserEnabled() : bool
Tags
since
3.0.0
Return values
bool

True if multi-user mode is enabled

languageBelongsToCurrentUser()

Check whether the given language belongs to the current user.

public static languageBelongsToCurrentUser(int $langId) : bool

Returns true in single-user mode unconditionally — there are no other users to fence against. In multi-user mode, leverages the auto-scoping on the languages table: an EXISTS that comes back negative means either the row doesn't exist or it belongs to someone else, and either way the caller must refuse the request.

Use this on any handler that writes a cross-table reference with a client-supplied LgID (local-dict create, feed create, etc.) — without the check, an authenticated user can pin their new row against a stranger's language.

Parameters
$langId : int
Return values
bool

query()

Get a query builder instance for a table.

public static query(string $tableName) : QueryBuilder

Convenience method to start building a database query.

Usage:

// SELECT query
$words = Globals::query('words')
    ->where('WoLgID', '=', 1)
    ->get();

// INSERT query
Globals::query('words')
    ->insert(['WoText' => 'hello', 'WoLgID' => 1]);
Parameters
$tableName : string

The base table name (e.g., 'words')

Tags
since
3.0.0
Return values
QueryBuilder

requireUserId()

Get the current user ID, throwing if not authenticated.

public static requireUserId() : int

Use this method when a user must be authenticated for the operation to proceed. It provides a cleaner alternative to checking for null.

Usage:

try {
    $userId = Globals::requireUserId();
    // Proceed with user-specific operation
} catch (AuthException $e) {
    // Handle unauthenticated user
}
Tags
throws
AuthException

If no user is authenticated

since
3.0.0
Return values
int

The current user ID

reset()

Reset all globals to initial state.

public static reset() : void

Primarily used for testing.

setBackupRestoreEnabled()

Set whether backup restore is enabled.

public static setBackupRestoreEnabled(bool|null $enabled) : void
Parameters
$enabled : bool|null

Whether to enable backup restore, null for default

Tags
since
3.0.0

setCurrentUserId()

Set the current authenticated user ID.

public static setCurrentUserId(int|null $userId) : void

This should be called after successful authentication to establish the user context for all subsequent database operations.

Parameters
$userId : int|null

The authenticated user's ID, or null to clear

Tags
since
3.0.0

setCurrentUserIsAdmin()

Set whether the current user is an admin.

public static setCurrentUserIsAdmin(bool $isAdmin) : void

This should be called after successful authentication to establish the admin context for authorization checks.

Parameters
$isAdmin : bool

Whether the current user is an admin

Tags
since
3.0.0

setDatabaseName()

Set the database name.

public static setDatabaseName(string $name) : void
Parameters
$name : string

The database name

setDbConnection()

Set the database connection.

public static setDbConnection(mysqli $connection) : void
Parameters
$connection : mysqli

The mysqli connection object

setErrorDisplay()

Enable or disable error display.

public static setErrorDisplay(bool $enabled) : void
Parameters
$enabled : bool

True to enable error display

setMultiUserEnabled()

Enable multi-user mode.

public static setMultiUserEnabled(bool $enabled) : void

When enabled, QueryBuilder will automatically filter queries by user_id for user-scoped tables.

Parameters
$enabled : bool

Whether to enable multi-user mode

Tags
since
3.0.0

table()

Get a table name.

public static table(string $tableName) : string

Convenience method to get a full table name.

Parameters
$tableName : string

The base table name (e.g., 'words')

Return values
string

The table name


        
On this page

Search results