Documentation

UrlUtilities
in package

URL handling utilities.

Provides methods for parsing and manipulating URLs, including dictionary URL parsing.

Tags
category

Lwt

author

HugoFara hugo.farajallah@protonmail.com

license

Unlicense http://unlicense.org/

link
https://hugofara.github.io/lwt/docs/php/
since
3.0.0

Table of Contents

Properties

$basePath  : string|null
Cached base path value.

Methods

buildUrl()  : string
Build a URL with query parameters.
getBasePath()  : string
Get the configured application base path.
langFromDict()  : string
Get a two-letter language code from dictionary source language.
resetBasePath()  : void
Reset the cached base path.
stripBasePath()  : string
Strip the base path from a request URI for route matching.
targetLangFromDict()  : string
Get a two-letter language code from dictionary target language
url()  : string
Generate a URL with the application base path prepended.
urlBase()  : string
Get the base URL of the application
validateUrlForFetch()  : array{valid: bool, error?: string, resolved_ip?: string}
Validate that a URL is safe to fetch (not pointing to internal/private IPs).
isPublicIp()  : bool
Check if an IP address is a public (non-private, non-reserved) address.
resolveHostToIps()  : array<string|int, string>
Resolve a hostname to its IP addresses.

Properties

$basePath

Cached base path value.

private static string|null $basePath = null

Methods

buildUrl()

Build a URL with query parameters.

public static buildUrl(string $path[, array<string, mixed> $params = [] ]) : string

Constructs a URL by combining a path with query parameters. Empty/null parameter values are filtered out.

Parameters
$path : string

The URL path (will have base path prepended)

$params : array<string, mixed> = []

Query parameters to append

Tags
example

buildUrl('/tags', ['page' => 2, 'query' => 'test']) returns '/tags?page=2&query=test'

example

buildUrl('/tags', ['page' => 1, 'query' => '']) returns '/tags?page=1'

Return values
string

The complete URL with query string

getBasePath()

Get the configured application base path.

public static getBasePath() : string

Returns the APP_BASE_PATH environment variable value, normalized to ensure it starts with / and has no trailing slash.

Return values
string

The base path (e.g., '/lwt') or empty string for root

langFromDict()

Get a two-letter language code from dictionary source language.

public static langFromDict(string $url) : string
Parameters
$url : string

Input URL, usually Google Translate or LibreTranslate

Return values
string

The source language code or empty string

resetBasePath()

Reset the cached base path.

public static resetBasePath() : void

Useful for testing or when environment changes dynamically.

stripBasePath()

Strip the base path from a request URI for route matching.

public static stripBasePath(string $requestUri) : string

This is used by the Router to normalize incoming requests so that routes like '/' work regardless of the configured APP_BASE_PATH.

Parameters
$requestUri : string

The full request URI

Tags
example

stripBasePath('/lwt/login') returns '/login' if APP_BASE_PATH=/lwt

example

stripBasePath('/lwt') returns '/' if APP_BASE_PATH=/lwt

Return values
string

The path with base path stripped

targetLangFromDict()

Get a two-letter language code from dictionary target language

public static targetLangFromDict(string $url) : string
Parameters
$url : string

Input URL, usually Google Translate or LibreTranslate

Return values
string

The target language code or empty string

url()

Generate a URL with the application base path prepended.

public static url(string $path) : string

Use this for all internal links to ensure they work correctly when the application is installed in a subdirectory.

Parameters
$path : string

The path to generate URL for (must start with /)

Tags
example

url('/login') returns '/lwt/login' if APP_BASE_PATH=/lwt

example

url('/assets/css/main.css') returns '/lwt/assets/css/main.css'

Return values
string

The full URL path with base path prepended

urlBase()

Get the base URL of the application

public static urlBase() : string
Return values
string

base URL

validateUrlForFetch()

Validate that a URL is safe to fetch (not pointing to internal/private IPs).

public static validateUrlForFetch(string $url) : array{valid: bool, error?: string, resolved_ip?: string}

Prevents SSRF attacks by blocking requests to:

  • Private IP ranges (10.x, 172.16-31.x, 192.168.x)
  • Loopback addresses (127.x, ::1)
  • Link-local addresses (169.254.x, fe80::)
  • Reserved/special addresses
  • Non-HTTP(S) schemes
Parameters
$url : string

URL to validate

Return values
array{valid: bool, error?: string, resolved_ip?: string}

isPublicIp()

Check if an IP address is a public (non-private, non-reserved) address.

private static isPublicIp(string $ip) : bool
Parameters
$ip : string

IP address to check

Return values
bool

True if the IP is public and safe to access

resolveHostToIps()

Resolve a hostname to its IP addresses.

private static resolveHostToIps(string $host) : array<string|int, string>
Parameters
$host : string

Hostname to resolve

Return values
array<string|int, string>

List of IP addresses


        
On this page

Search results