Difference between revisions for Users / Eo Ny / dev
| Merge of Version1 & Version2 | |
|---|---|
| 1 | |
| 2 | |
| 3 | === Overview === |
| 4 | |
| … | … |
| 40 | ---- |
| 41 | === Constructor === |
| 42 | |
| 43 | %% |
| 44 | public function __construct(&$db) |
| 45 | %% |
| 46 | |
| … | … |
| 58 | 6. Enforces TLS session upgrade if needed |
| 59 | |
| 60 | **Example:** |
| 61 | %% |
| 62 | $http = new Http($db); |
| 63 | %% |
| 64 | |
| … | … |
| 82 | - Recovers diagnostic logs from previous session |
| 83 | |
| 84 | **Example:** |
| 85 | %% |
| 86 | $http->session(0); // Normal session |
| 87 | $http->session(2); // Static file serving mode |
| 88 | %% |
| … | … |
| 105 | - ✅ Only cached for anonymous users (no logged-in users) |
| 106 | |
| 107 | **Example:** |
| 108 | %% |
| 109 | $http->check_cache('HomePage', 'show'); |
| 110 | %% |
| 111 | |
| 112 | ---- |
| 113 | |
| 114 | ===== |
| 115 | Saves the generated page content to cache file. |
| 116 | |
| 117 | **Features:** |
| … | … |
| 121 | - Only executes if caching flag is set and user is anonymous |
| 122 | |
| 123 | **Example:** |
| 124 | %% |
| 125 | // Called at end of page rendering |
| 126 | $http->store_cache(); |
| 127 | %% |
| … | … |
| 144 | 4. Returns count of invalidated caches |
| 145 | |
| 146 | **Example:** |
| 147 | %% |
| 148 | $count = $http->invalidate_page('HomePage'); |
| 149 | echo "Invalidated $count cache entries"; |
| 150 | %% |
| … | … |
| 162 | - Called when TLS session is detected |
| 163 | |
| 164 | **Example:** |
| 165 | %% |
| 166 | $http->secure_base_url(); |
| 167 | // $db->base_url now uses https:// |
| 168 | %% |
| … | … |
| 181 | - Converts relative URLs using current server name |
| 182 | |
| 183 | **Example:** |
| 184 | %% |
| 185 | $http->ensure_tls('/secure/payment'); |
| 186 | %% |
| 187 | |
| … | … |
| 209 | - ##reverse_proxy_header## - Custom header name (default: ##X-Forwarded-For##) |
| 210 | |
| 211 | **Example:** |
| 212 | %% |
| 213 | $client_ip = $http->ip; // e.g., "203.0.113.42" |
| 214 | %% |
| 215 | |
| … | … |
| 253 | - ##2## - Custom policy (from ##csp_custom.conf##) |
| 254 | |
| 255 | **Example:** |
| 256 | %% |
| 257 | $http->http_security_headers(); |
| 258 | %% |
| 259 | |
| … | … |
| 273 | - Uses output buffering to work anywhere in page processing |
| 274 | |
| 275 | **Example:** |
| 276 | %% |
| 277 | $http->redirect('http://example.com/new-page', true); // 301 |
| 278 | $http->redirect('/wiki/HomePage'); // 302 |
| 279 | %% |
| … | … |
| 288 | - Ends script execution |
| 289 | |
| 290 | **Example:** |
| 291 | %% |
| 292 | $http->terminate(); |
| 293 | %% |
| 294 | |
| … | … |
| 298 | Sets HTTP response status code. |
| 299 | |
| 300 | **Supported Status Codes:** |
| 301 | %% |
| 302 | 200 => 'OK' |
| 303 | 206 => 'Partial Content' |
| 304 | 301 => 'Moved Permanently' |
| … | … |
| 318 | %% |
| 319 | |
| 320 | **Example:** |
| 321 | %% |
| 322 | $http->status(404); // Send 404 Not Found |
| 323 | %% |
| 324 | |
| … | … |
| 339 | - ##Cache-Control: no-store## |
| 340 | |
| 341 | **Example:** |
| 342 | %% |
| 343 | $http->no_cache(); // Client-side only |
| 344 | $http->no_cache(false); // Both client & server |
| 345 | %% |
| … | … |
| 353 | - ##Cache-Control: public## |
| 354 | |
| 355 | **Example:** |
| 356 | %% |
| 357 | $http->cache_promisc(); |
| 358 | %% |
| 359 | |
| … | … |
| 398 | - Associative array: ##['en' => 'en', 'de' => 'de', ...]## |
| 399 | |
| 400 | **Example:** |
| 401 | %% |
| 402 | $all_langs = $http->available_languages(false); |
| 403 | $allowed = $http->available_languages(true); |
| 404 | %% |
| … | … |
| 424 | - GZip compression for text files |
| 425 | |
| 426 | **Special Paths:** |
| 427 | %% |
| 428 | $http->sendfile(404); // Serves file defined by HTTP_404 constant |
| 429 | $http->sendfile(403); // Serves file defined by HTTP_403 constant |
| 430 | %% |
| 431 | |
| 432 | **Example:** |
| 433 | %% |
| 434 | $http->sendfile('uploads/document.pdf', 'my-document.pdf', 30); |
| 435 | %% |
| 436 | |
| … | … |
| 444 | - Default: ##'application/octet-stream'## |
| 445 | |
| 446 | **Example:** |
| 447 | %% |
| 448 | $mime = $http->mime_type('file.pdf'); // 'application/pdf' |
| 449 | %% |
| 450 | |
| … | … |
| 474 | - Headers not already sent |
| 475 | |
| 476 | **Example:** |
| 477 | %% |
| 478 | $http->gzip(); |
| 479 | %% |
| 480 | |
| … | … |
| 490 | - Converts encoding properly |
| 491 | |
| 492 | **Example:** |
| 493 | %% |
| 494 | $data = $http->parse_str('name=John&age=30'); |
| 495 | %% |
| 496 | |
| … | … |
| 551 | |
| 552 | ---- |
| 553 | |
| 554 | ===Constants Used=== |
| 555 | |
| 556 | #| |
| 557 | *| Constant | Type | Purpose |* |
| 558 | || ##IN_WACKO## | bool | Security check (exit if not defined) || |
| 559 | || ##CHMOD_SAFE## | int | File permissions for cache files || |
| 560 | || ##CHMOD_FILE## | int | File permissions for config cache || |
| 561 | || ##CACHE_PAGE_DIR## | string | Page cache directory || |
| 562 | || ##CACHE_SESSION_DIR## | string | Session cache directory || |
| 563 | || ##CACHE_CONFIG_DIR## | string | Config cache directory || |
| 564 | || ##CONFIG_DIR## | string | Configuration directory || |
| 565 | || ##LANG_DIR## | string | Language files directory || |
| 566 | || ##DAYSECS## | int | Seconds in a day (86400) || |
| 567 | || ##HTTP_404## | string | Path to 404 error page || |
| 568 | || ##HTTP_403## | string | Path to 403 error page || |
| 569 | |# |
| 570 | |
| 571 | ---- |
| 572 | |
| 573 | === Workflow Examples === |
| 574 | |
| 575 | ==== |
| 576 | |
| 577 | %% |
| 578 | // In main wiki entry point |
| 579 | $http = new Http($db); |
| 580 | $http->session(0); // Start session |
| … | … |
| 596 | |
| 597 | ==== Example 2: Handling TLS/HTTPS Upgrade ==== |
| 598 | |
| 599 | %% |
| 600 | $http = new Http($db); // Constructor detects TLS requirement |
| 601 | // If TLS is enabled and user wasn't in TLS before: |
| 602 | // - Sets TLS session flag |
| … | … |
| 606 | |
| 607 | ==== Example 3: Invalidating Cache After Page Edit ==== |
| 608 | |
| 609 | %% |
| 610 | // User edits a page |
| 611 | $http = new Http($db); |
| 612 | $count = $http->invalidate_page('HomePage'); |
| … | … |
| 615 | |
| 616 | ==== Example 4: Serving a File ==== |
| 617 | |
| 618 | %% |
| 619 | $http = new Http($db); |
| 620 | $http->session(2); // Static file mode - no session replay prevention |
| 621 | |
| … | … |
| 690 | |
| 691 | The class integrates with WackoWiki's diagnostic system: |
| 692 | |
| 693 | %% |
| 694 | // Diagnostic messages are preserved across redirects |
| 695 | // via session flash data |
| 696 | |