| 45 |
%% |
| 46 |
|
| 47 |
**Purpose:** Initializes the Http object and sets up HTTP session handling. |
| 48 |
|
| 49 |
**Parameters:** |
| 50 |
- ##$db## - Database object reference |
| 51 |
|
| 52 |
**Initialization Steps:** |
| 53 |
1. Stores database reference |
| 54 |
2. Extracts and normalizes REQUEST_URI |
| 55 |
3. Detects TLS/HTTPS session status |
| 56 |
4. Determines client's real IP address |
| 57 |
5. Sets up TLS mark cookie name |
| 58 |
6. Enforces TLS session upgrade if needed |
| 59 |
|
| 60 |
**Example:** |
| 61 |
%%php |
| 63 |
%% |
| 64 |
|
| 65 |
---- |
| 66 |
|
| 67 |
=== Core Methods === |
| 68 |
|
| 69 |
==== Session Management ==== |
| 70 |
|
| 71 |
===== ##session($route): void## ===== |
| 72 |
Initializes the session handler (file-based or database-based). |
| 73 |
|
| 74 |
**Parameters:** |
| 75 |
- ##$route## (int) - Routing flag: |
| 76 |
- Bit 2 (##$route & 2##): Enable static mode for files/freecap (disables replay prevention and ID regeneration) |
| 77 |
|
| 78 |
**Features:** |
| 79 |
- Selects storage backend (file or database) |
| 80 |
- Configures cookie settings (security, path, httponly) |
| 81 |
- Binds IP and TLS validation |
| 82 |
- Recovers diagnostic logs from previous session |
| 83 |
|
| 84 |
**Example:** |
| 85 |
%%php |
| 88 |
%% |
| 89 |
|
| 90 |
---- |
| 91 |
|
| 92 |
==== Caching System ==== |
| 93 |
|
| 94 |
===== ##check_cache($page, $method): void## ===== |
| 95 |
Determines if a page can be cached and prepares the cache check. |
| 96 |
|
| 97 |
**Parameters:** |
| 98 |
- ##$page## (string) - Page name to cache |
| 99 |
- ##$method## (string) - Request method/action (e.g., 'show', 'edit') |
| 100 |
|
| 101 |
**Caching Rules:** |
| 102 |
- ✅ Enabled for GET requests only |
| 103 |
- ✅ Disabled for POST requests |
| 104 |
- ❌ Never cached for 'edit' or 'watch' methods |
| 105 |
- ✅ Only cached for anonymous users (no logged-in users) |
| 106 |
|
| 107 |
**Example:** |
| 108 |
%%php |
| 186 |
%% |
| 187 |
|
| 188 |
---- |
| 189 |
|
| 190 |
==== IP Address Detection ==== |
| 191 |
|
| 192 |
===== ##real_ip(): string## (Private) ===== |
| 193 |
Detects client's real IP address accounting for proxies. |
| 194 |
|
| 195 |
**Proxy Headers Checked (in order):** |
| 196 |
1. ##HTTP_X_CLUSTER_CLIENT_IP## |
| 197 |
2. ##HTTP_X_FORWARDED_FOR## (or custom header) |
| 198 |
3. ##HTTP_CLIENT_IP## |
| 199 |
4. ##HTTP_X_REMOTE_ADDR## |
| 200 |
5. ##REMOTE_ADDR## (fallback) |
| 201 |
|
| 202 |
**Features:** |
| 203 |
- Filters out private/reserved IP ranges |
| 204 |
- Respects configured reverse proxy addresses |
| 205 |
- Returns ##'0.0.0.0'## as fallback |
| 206 |
|
| 207 |
**Configuration in Database:** |
| 208 |
- ##reverse_proxy_addresses## - Comma/space-separated proxy IPs |
| 209 |
- ##reverse_proxy_header## - Custom header name (default: ##X-Forwarded-For##) |
| 210 |
|
| 211 |
**Example:** |
| 212 |
%%php |
| 214 |
%% |
| 215 |
|
| 216 |
---- |
| 217 |
|
| 218 |
==== HTTPS Detection ==== |
| 219 |
|
| 220 |
===== ##tls_session(): bool## (Private) ===== |
| 221 |
Detects if current connection uses HTTPS/TLS. |
| 222 |
|
| 223 |
**Checks (any being true = HTTPS):** |
| 224 |
- ##$_SERVER['HTTPS']## is 'on' |
| 225 |
- ##$_SERVER['SERVER_PORT']## is 443 |
| 226 |
- ##$_SERVER['HTTP_X_FORWARDED_PROTO']## is 'https' |
| 227 |
- ##$_SERVER['HTTP_X_FORWARDED_SSL']## is 'on' |
| 228 |
- ##$_SERVER['HTTP_X_FORWARDED_PORT']## is 443 |
| 229 |
|
| 230 |
---- |
| 231 |
|
| 232 |
==== Security Headers ==== |
| 233 |
|
| 234 |
===== ##http_security_headers(): void## ===== |
| 235 |
|
| 236 |
|
| 237 |
==== HTTP Methods ==== |
| 238 |
|
| 239 |
===== ##redirect($url, $permanent = false): void## ===== |
| 240 |
Performs an HTTP redirect. |
| 241 |
|
| 242 |
**Parameters:** |
| 243 |
- ##$url## (string) - Target URL |
| 244 |
- ##$permanent## (bool) - Use 301 (permanent) vs 302 (temporary) |
| 245 |
|
| 246 |
**Features:** |
| 247 |
- Decodes ##&## entities to prevent broken redirects |
| 248 |
- Only works if headers not yet sent |
| 249 |
- Uses output buffering to work anywhere in page processing |
| 250 |
|
| 251 |
**Example:** |
| 252 |
%%php |
| 299 |
%% |
| 300 |
|
| 301 |
---- |
| 302 |
|
| 303 |
==== Caching Control ==== |
| 304 |
|
| 305 |
===== ##no_cache($client_only = true): void## ===== |
| 306 |
Disables caching of the current page. |
| 307 |
|
| 308 |
**Parameters:** |
| 309 |
- ##$client_only## (bool, default: TRUE) |
| 310 |
- ##TRUE##: Disable browser cache only |
| 311 |
- ##FALSE##: Disable both browser and server cache |
| 312 |
|
| 313 |
**Headers Set:** |
| 314 |
- ##Last-Modified: <current-time>## (always fresh) |
| 315 |
- ##Cache-Control: no-store## |
| 316 |
|
| 317 |
**Example:** |
| 318 |
%%php |
| 352 |
%% |
| 353 |
|
| 354 |
**Returns:** |
| 355 |
- Language code (e.g., 'en', 'en-US', 'de') |
| 356 |
|
| 357 |
---- |
| 358 |
|
| 359 |
===== ##available_languages($subset = true): array## ===== |
| 360 |
Returns list of available language translations. |
| 361 |
|
| 362 |
**Parameters:** |
| 363 |
- ##$subset## (bool, default: TRUE) |
| 364 |
- ##TRUE##: Only allowed languages |
| 365 |
- ##FALSE##: All available languages |
| 366 |
|
| 367 |
**Features:** |
| 368 |
- Scans ##LANG_DIR## for language files |
| 369 |
- Filters by ##allowed_languages## config if set |
| 370 |
- Caches result in session |
| 371 |
- System language always included |
| 372 |
|
| 373 |
**Returns:** |
| 374 |
- Associative array: ##['en' => 'en', 'de' => 'de', ...]## |
| 375 |
|
| 376 |
**Example:** |
| 377 |
%%php |
| 380 |
%% |
| 381 |
|
| 382 |
---- |
| 383 |
|
| 384 |
==== File Serving ==== |
| 385 |
|
| 386 |
===== ##sendfile($path, $filename = null, $age = null): void## ===== |
| 387 |
Serves files with proper HTTP headers and caching. |
| 388 |
|
| 389 |
**Parameters:** |
| 390 |
- ##$path## (string) - File path (or HTTP_XXX constant for error pages) |
| 391 |
- ##$filename## (string, optional) - Custom download filename |
| 392 |
- ##$age## (int, optional) - Cache age in days |
| 393 |
|
| 394 |
**Features:** |
| 395 |
- HTTP range request support (partial file downloads) |
| 396 |
- ETag and Last-Modified conditional requests |
| 397 |
- Proper MIME type detection |
| 398 |
- Content-Security-Policy for special file types |
| 399 |
- Streaming for large files |
| 400 |
- GZip compression for text files |
| 401 |
|
| 402 |
**Special Paths:** |
| 403 |
%%php |
| 425 |
%% |
| 426 |
|
| 427 |
---- |
| 428 |
|
| 429 |
===== ##mime_types(): array## (Private) ===== |
| 430 |
Loads and caches MIME types from configuration. |
| 431 |
|
| 432 |
**Features:** |
| 433 |
- Reads from ##config/mime.types## |
| 434 |
- Caches to ##cache/config/mime.types## |
| 435 |
- Reloads if config is updated |
| 436 |
|
| 437 |
---- |
| 438 |
|
| 439 |
==== Compression ==== |
| 440 |
|
| 441 |
===== ##gzip(): void## ===== |
| 442 |
Compresses HTTP response with gzip/x-gzip. |
| 443 |
|
| 444 |
**Features:** |
| 445 |
- Manually implements gzip (not relying on zlib.output_compression) |
| 446 |
- Produces correct ##Content-Length## header |
| 447 |
- Only compresses if: |
| 448 |
- 860 bytes < content < 1 MB |
| 449 |
- Client accepts compression |
| 450 |
- Headers not already sent |
| 451 |
|
| 452 |
**Example:** |
| 453 |
%%php |
| 471 |
%% |
| 472 |
|
| 473 |
---- |
| 474 |
|
| 475 |
===== ##request_uri(): string## (Private) ===== |
| 476 |
Extracts and normalizes REQUEST_URI from server. |
| 477 |
|
| 478 |
**Normalization:** |
| 479 |
- Removes base URL prefix |
| 480 |
- Removes spaces |
| 481 |
- Collapses multiple slashes |
| 482 |
- Removes ##..## path traversal attempts |
| 483 |
- Removes leading/trailing slashes |
| 484 |
|
| 485 |
---- |
| 486 |
|
| 487 |
===== ##cut_prefix($prefix, $path): string## (Private) ===== |
| 488 |
Removes prefix from path (case-insensitive). |
| 489 |
|
| 490 |
---- |
| 491 |
|
| 492 |
===== ##get_header_conf($file_name): string## (Private) ===== |
| 493 |
Loads security header configuration from files. |
| 494 |
|
| 495 |
**Files Supported:** |
| 496 |
- ##csp.conf## / ##csp_custom.conf## |
| 497 |
- ##permissions_policy.conf## / ##permissions_policy_custom.conf## |
| 498 |
|
| 499 |
---- |
| 500 |
|
| 501 |
=== Configuration Dependencies === |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
=== Constants Used === |
| 506 |
|
| 507 |
|