Difference between revisions for Users / Eo Ny / dev




← Previous edit
Next edit →

Version1 Version2 Differences
1 1 == HTTP Class Technical Documentation ==
2 2
  3 {{toc numerate=1}}  
3 4 === Overview ===
4 5
5 6 The ##Http## class (##src/class/http.php##) is a core component of the WackoWiki system responsible for handling HTTP request/response processing, session management, caching, and security features. This class acts as a bridge between the web server and the wiki engine.
12 13
13 14 === Class Properties ===
14 15
15   ==== Public Properties ====
16  
17  
18  
19   ==== Private Properties ====
20  
21  
  16 ====Public Properties====
  17
  18 #|
  19 *| Property | Type | Description |*
  20 || ##$tls_session## | bool | Indicates if the current session uses HTTPS/TLS encryption ||
  21 || ##$request_uri## | string | Normalized REQUEST_URI (e.g., 'PageOfNoReturn/show?a=1') ||
  22 || ##$ip## | string | Client's real IP address (accounts for proxies) ||
  23 || ##$sess## | Session | Reference to the Session object ||
  24 || ##$method## | string | Current HTTP method/request type ||
  25 |#
  26
  27 ====Private Properties====
  28
  29 #|
  30 *| Property | Type | Description |*
  31 || ##$db## | object | Database connection reference ||
  32 || ##$tls_mark## | string | Cookie name for TLS session marking ||
  33 || ##$page## | string | Current page name being processed ||
  34 || ##$hash## | string | SHA1 hash of the page name ||
  35 || ##$query## | string | Encoded query string ||
  36 || ##$lang## | string | Current language code ||
  37 || ##$file## | string | Cache file path ||
  38 || ##$caching## | int | Flag indicating if page should be cached (0 or 1) ||
  39 |#
  40
  41 ----
22 42 === Constructor ===
23 43
24   %%php
  44 %%(hl php)
25 45 public function __construct(&$db)
26 46 %%
27 47
39 59   6. Enforces TLS session upgrade if needed
40 60
41 61 **Example:**
42   %%php
  62 %%(hl php)
43 63 $http = new Http($db);
44 64 %%
45 65
63 83   - Recovers diagnostic logs from previous session
64 84
65 85 **Example:**
66   %%php
  86 %%(hl php)
67 87 $http->session(0); // Normal session
68 88 $http->session(2); // Static file serving mode
69 89 %%
86 106   - ✅ Only cached for anonymous users (no logged-in users)
87 107
88 108 **Example:**
89   %%php
  109 %%(hl php)
90 110 $http->check_cache('HomePage', 'show');
91 111 %%
92 112
93 113 ----
94 114
95   ===== ##store_cache(): void## =====
  115 =====##store_cache(): void##=====
96 116 Saves the generated page content to cache file.
97 117
98 118 **Features:**
102 122   - Only executes if caching flag is set and user is anonymous
103 123
104 124 **Example:**
105   %%php
  125 %%(hl php)
106 126 // Called at end of page rendering
107 127 $http->store_cache();
108 128 %%
125 145   4. Returns count of invalidated caches
126 146
127 147 **Example:**
128   %%php
  148 %%(hl php)
129 149 $count = $http->invalidate_page('HomePage');
130 150 echo "Invalidated $count cache entries";
131 151 %%
143 163   - Called when TLS session is detected
144 164
145 165 **Example:**
146   %%php
  166 %%(hl php)
147 167 $http->secure_base_url();
148 168 // $db->base_url now uses https://
149 169 %%
162 182   - Converts relative URLs using current server name
163 183
164 184 **Example:**
165   %%php
  185 %%(hl php)
166 186 $http->ensure_tls('/secure/payment');
167 187 %%
168 188
190 210   - ##reverse_proxy_header## - Custom header name (default: ##X-Forwarded-For##)
191 211
192 212 **Example:**
193   %%php
  213 %%(hl php)
194 214 $client_ip = $http->ip; // e.g., "203.0.113.42"
195 215 %%
196 216
212 232
213 233 ==== Security Headers ====
214 234
215   ===== ##http_security_headers(): void## =====
216  
217  
  235 =====##http_security_headers(): void##=====
  236
  237 Sets security-related HTTP headers.
  238
  239 **Headers Set:**
  240
  241 #|
  242 *| Header | Purpose | Config Key |*
  243 || Content-Security-Policy | XSS/injection protection | ##csp## ||
  244 || Permissions-Policy | Control browser features | ##permissions_policy## ||
  245 || Referrer-Policy | Control referrer information | ##referrer_policy## ||
  246 || Strict-Transport-Security | Force HTTPS | Auto (TLS only) ||
  247 || X-Frame-Options | Clickjacking protection | Hardcoded: ##SAMEORIGIN## ||
  248 || X-Content-Type-Options | MIME sniffing prevention | Hardcoded: ##nosniff## ||
  249 |#
  250
  251 **CSP Configuration Options:**
  252   - ##0## - Disabled
  253   - ##1## - Default policy (from ##csp.conf##)
  254   - ##2## - Custom policy (from ##csp_custom.conf##)
  255
  256 **Example:**
  257 %%(hl php)
  258 $http->http_security_headers();
  259 %%
  260
  261 ----
218 262 ==== HTTP Methods ====
219 263
220 264 ===== ##redirect($url, $permanent = false): void## =====
230 274   - Uses output buffering to work anywhere in page processing
231 275
232 276 **Example:**
233   %%php
  277 %%(hl php)
234 278 $http->redirect('http://example.com/new-page', true); // 301
235 279 $http->redirect('/wiki/HomePage'); // 302
236 280 %%
245 289   - Ends script execution
246 290
247 291 **Example:**
248   %%php
  292 %%(hl php)
249 293 $http->terminate();
250 294 %%
251 295
255 299 Sets HTTP response status code.
256 300
257 301 **Supported Status Codes:**
258   %%php
  302 %%(hl php)
259 303 200 => 'OK'
260 304 206 => 'Partial Content'
261 305 301 => 'Moved Permanently'
275 319 %%
276 320
277 321 **Example:**
278   %%php
  322 %%(hl php)
279 323 $http->status(404); // Send 404 Not Found
280 324 %%
281 325
296 340   - ##Cache-Control: no-store##
297 341
298 342 **Example:**
299   %%php
  343 %%(hl php)
300 344 $http->no_cache(); // Client-side only
301 345 $http->no_cache(false); // Both client & server
302 346 %%
310 354   - ##Cache-Control: public##
311 355
312 356 **Example:**
313   %%php
  357 %%(hl php)
314 358 $http->cache_promisc();
315 359 %%
316 360
355 399   - Associative array: ##['en' => 'en', 'de' => 'de', ...]##
356 400
357 401 **Example:**
358   %%php
  402 %%(hl php)
359 403 $all_langs = $http->available_languages(false);
360 404 $allowed = $http->available_languages(true);
361 405 %%
381 425   - GZip compression for text files
382 426
383 427 **Special Paths:**
384   %%php
  428 %%(hl php)
385 429 $http->sendfile(404); // Serves file defined by HTTP_404 constant
386 430 $http->sendfile(403); // Serves file defined by HTTP_403 constant
387 431 %%
388 432
389 433 **Example:**
390   %%php
  434 %%(hl php)
391 435 $http->sendfile('uploads/document.pdf', 'my-document.pdf', 30);
392 436 %%
393 437
401 445   - Default: ##'application/octet-stream'##
402 446
403 447 **Example:**
404   %%php
  448 %%(hl php)
405 449 $mime = $http->mime_type('file.pdf'); // 'application/pdf'
406 450 %%
407 451
431 475   - Headers not already sent
432 476
433 477 **Example:**
434   %%php
  478 %%(hl php)
435 479 $http->gzip();
436 480 %%
437 481
447 491   - Converts encoding properly
448 492
449 493 **Example:**
450   %%php
  494 %%(hl php)
451 495 $data = $http->parse_str('name=John&age=30');
452 496 %%
453 497
479 523
480 524 ----
481 525
482   === Configuration Dependencies ===
483  
484  
485  
486   === Constants Used ===
487  
488  
  526 ===Configuration Dependencies===
  527
  528 The class relies on these database configuration settings:
  529
  530 #|
  531 *| Setting | Type | Purpose |*
  532 || ##base_url## | string | Wiki's base URL ||
  533 || ##tls## | bool | Enable HTTPS enforcement ||
  534 || ##cache## | bool | Enable page caching ||
  535 || ##cache_ttl## | int | Cache lifetime in seconds ||
  536 || ##session_store## | int | 1=File, 0=Database ||
  537 || ##system_seed_hash## | string | Session encryption seed ||
  538 || ##cookie_prefix## | string | Session cookie prefix ||
  539 || ##cookie_path## | string | Cookie path ||
  540 || ##allow_persistent_cookie## | bool | Allow persistent login ||
  541 || ##session_length## | int | Session lifetime in seconds ||
  542 || ##reverse_proxy_addresses## | string | Comma/space-separated proxy IPs ||
  543 || ##reverse_proxy_header## | string | Custom X-Forwarded header ||
  544 || ##language## | string | Default language code ||
  545 || ##multilanguage## | bool | Enable language negotiation ||
  546 || ##allowed_languages## | string | Comma/space-separated allowed langs ||
  547 || ##enable_security_headers## | bool | Send security headers ||
  548 || ##csp## | int | CSP setting (0/1/2) ||
  549 || ##permissions_policy## | int | Permissions-Policy setting (0/1/2) ||
  550 || ##referrer_policy## | int | Referrer-Policy setting (0-8) ||
  551 |#
  552
  553 ----
  554
  555 ===Constants Used===
  556
  557 #|
  558 *| Constant | Type | Purpose |*
  559 || ##IN_WACKO## | bool | Security check (exit if not defined) ||
  560 || ##CHMOD_SAFE## | int | File permissions for cache files ||
  561 || ##CHMOD_FILE## | int | File permissions for config cache ||
  562 || ##CACHE_PAGE_DIR## | string | Page cache directory ||
  563 || ##CACHE_SESSION_DIR## | string | Session cache directory ||
  564 || ##CACHE_CONFIG_DIR## | string | Config cache directory ||
  565 || ##CONFIG_DIR## | string | Configuration directory ||
  566 || ##LANG_DIR## | string | Language files directory ||
  567 || ##DAYSECS## | int | Seconds in a day (86400) ||
  568 || ##HTTP_404## | string | Path to 404 error page ||
  569 || ##HTTP_403## | string | Path to 403 error page ||
  570 |#
  571
  572 ----
489 573
490 574 === Workflow Examples ===
491 575
492   ==== Example 1: Handling a GET Request ====
493  
494   %%php
  576 ====Example 1: Handling a GET Request====
  577
  578 %%(hl php)
495 579 // In main wiki entry point
496 580 $http = new Http($db);
497 581 $http->session(0); // Start session
513 597
514 598 ==== Example 2: Handling TLS/HTTPS Upgrade ====
515 599
516   %%php
  600 %%(hl php)
517 601 $http = new Http($db); // Constructor detects TLS requirement
518 602 // If TLS is enabled and user wasn't in TLS before:
519 603 // - Sets TLS session flag
523 607
524 608 ==== Example 3: Invalidating Cache After Page Edit ====
525 609
526   %%php
  610 %%(hl php)
527 611 // User edits a page
528 612 $http = new Http($db);
529 613 $count = $http->invalidate_page('HomePage');
532 616
533 617 ==== Example 4: Serving a File ====
534 618
535   %%php
  619 %%(hl php)
536 620 $http = new Http($db);
537 621 $http->session(2); // Static file mode - no session replay prevention
538 622
607 691
608 692 The class integrates with WackoWiki's diagnostic system:
609 693
610   %%php
  694 %%(hl php)
611 695 // Diagnostic messages are preserved across redirects
612 696 // via session flash data
613 697