Difference between revisions for Users / Eo Ny / dev




← Previous edit
Next edit →

Version1 Version2 Differences
1   == HTTP Class Technical Documentation ==
  1 {{toc numerate=1}}
2 2
3 3 === Overview ===
4 4
40 40 ----
41 41 === Constructor ===
42 42
43   %%php
  43 %%(hl php)
44 44 public function __construct(&$db)
45 45 %%
46 46
58 58   6. Enforces TLS session upgrade if needed
59 59
60 60 **Example:**
61   %%php
  61 %%(hl php)
62 62 $http = new Http($db);
63 63 %%
64 64
82 82   - Recovers diagnostic logs from previous session
83 83
84 84 **Example:**
85   %%php
  85 %%(hl php)
86 86 $http->session(0); // Normal session
87 87 $http->session(2); // Static file serving mode
88 88 %%
105 105   - ✅ Only cached for anonymous users (no logged-in users)
106 106
107 107 **Example:**
108   %%php
  108 %%(hl php)
109 109 $http->check_cache('HomePage', 'show');
110 110 %%
111 111
112 112 ----
113 113
114   ===== ##store_cache(): void## =====
  114 =====##store_cache(): void##=====
115 115 Saves the generated page content to cache file.
116 116
117 117 **Features:**
121 121   - Only executes if caching flag is set and user is anonymous
122 122
123 123 **Example:**
124   %%php
  124 %%(hl php)
125 125 // Called at end of page rendering
126 126 $http->store_cache();
127 127 %%
144 144   4. Returns count of invalidated caches
145 145
146 146 **Example:**
147   %%php
  147 %%(hl php)
148 148 $count = $http->invalidate_page('HomePage');
149 149 echo "Invalidated $count cache entries";
150 150 %%
162 162   - Called when TLS session is detected
163 163
164 164 **Example:**
165   %%php
  165 %%(hl php)
166 166 $http->secure_base_url();
167 167 // $db->base_url now uses https://
168 168 %%
181 181   - Converts relative URLs using current server name
182 182
183 183 **Example:**
184   %%php
  184 %%(hl php)
185 185 $http->ensure_tls('/secure/payment');
186 186 %%
187 187
209 209   - ##reverse_proxy_header## - Custom header name (default: ##X-Forwarded-For##)
210 210
211 211 **Example:**
212   %%php
  212 %%(hl php)
213 213 $client_ip = $http->ip; // e.g., "203.0.113.42"
214 214 %%
215 215
231 231
232 232 ==== Security Headers ====
233 233
234   ===== ##http_security_headers(): void## =====
235  
236  
  234 =====##http_security_headers(): void##=====
  235
  236 Sets security-related HTTP headers.
  237
  238 **Headers Set:**
  239
  240 #|
  241 *| Header | Purpose | Config Key |*
  242 || Content-Security-Policy | XSS/injection protection | ##csp## ||
  243 || Permissions-Policy | Control browser features | ##permissions_policy## ||
  244 || Referrer-Policy | Control referrer information | ##referrer_policy## ||
  245 || Strict-Transport-Security | Force HTTPS | Auto (TLS only) ||
  246 || X-Frame-Options | Clickjacking protection | Hardcoded: ##SAMEORIGIN## ||
  247 || X-Content-Type-Options | MIME sniffing prevention | Hardcoded: ##nosniff## ||
  248 |#
  249
  250 **CSP Configuration Options:**
  251   - ##0## - Disabled
  252   - ##1## - Default policy (from ##csp.conf##)
  253   - ##2## - Custom policy (from ##csp_custom.conf##)
  254
  255 **Example:**
  256 %%(hl php)
  257 $http->http_security_headers();
  258 %%
  259
  260 ----
237 261 ==== HTTP Methods ====
238 262
239 263 ===== ##redirect($url, $permanent = false): void## =====
249 273   - Uses output buffering to work anywhere in page processing
250 274
251 275 **Example:**
252   %%php
  276 %%(hl php)
253 277 $http->redirect('http://example.com/new-page', true); // 301
254 278 $http->redirect('/wiki/HomePage'); // 302
255 279 %%
264 288   - Ends script execution
265 289
266 290 **Example:**
267   %%php
  291 %%(hl php)
268 292 $http->terminate();
269 293 %%
270 294
274 298 Sets HTTP response status code.
275 299
276 300 **Supported Status Codes:**
277   %%php
  301 %%(hl php)
278 302 200 => 'OK'
279 303 206 => 'Partial Content'
280 304 301 => 'Moved Permanently'
294 318 %%
295 319
296 320 **Example:**
297   %%php
  321 %%(hl php)
298 322 $http->status(404); // Send 404 Not Found
299 323 %%
300 324
315 339   - ##Cache-Control: no-store##
316 340
317 341 **Example:**
318   %%php
  342 %%(hl php)
319 343 $http->no_cache(); // Client-side only
320 344 $http->no_cache(false); // Both client & server
321 345 %%
329 353   - ##Cache-Control: public##
330 354
331 355 **Example:**
332   %%php
  356 %%(hl php)
333 357 $http->cache_promisc();
334 358 %%
335 359
374 398   - Associative array: ##['en' => 'en', 'de' => 'de', ...]##
375 399
376 400 **Example:**
377   %%php
  401 %%(hl php)
378 402 $all_langs = $http->available_languages(false);
379 403 $allowed = $http->available_languages(true);
380 404 %%
400 424   - GZip compression for text files
401 425
402 426 **Special Paths:**
403   %%php
  427 %%(hl php)
404 428 $http->sendfile(404); // Serves file defined by HTTP_404 constant
405 429 $http->sendfile(403); // Serves file defined by HTTP_403 constant
406 430 %%
407 431
408 432 **Example:**
409   %%php
  433 %%(hl php)
410 434 $http->sendfile('uploads/document.pdf', 'my-document.pdf', 30);
411 435 %%
412 436
420 444   - Default: ##'application/octet-stream'##
421 445
422 446 **Example:**
423   %%php
  447 %%(hl php)
424 448 $mime = $http->mime_type('file.pdf'); // 'application/pdf'
425 449 %%
426 450
450 474   - Headers not already sent
451 475
452 476 **Example:**
453   %%php
  477 %%(hl php)
454 478 $http->gzip();
455 479 %%
456 480
466 490   - Converts encoding properly
467 491
468 492 **Example:**
469   %%php
  493 %%(hl php)
470 494 $data = $http->parse_str('name=John&age=30');
471 495 %%
472 496
498 522
499 523 ----
500 524
501   === Configuration Dependencies ===
502  
503  
504  
505   === Constants Used ===
506  
507  
  525 ===Configuration Dependencies===
  526
  527 The class relies on these database configuration settings:
  528
  529 #|
  530 *| Setting | Type | Purpose |*
  531 || ##base_url## | string | Wiki's base URL ||
  532 || ##tls## | bool | Enable HTTPS enforcement ||
  533 || ##cache## | bool | Enable page caching ||
  534 || ##cache_ttl## | int | Cache lifetime in seconds ||
  535 || ##session_store## | int | 1=File, 0=Database ||
  536 || ##system_seed_hash## | string | Session encryption seed ||
  537 || ##cookie_prefix## | string | Session cookie prefix ||
  538 || ##cookie_path## | string | Cookie path ||
  539 || ##allow_persistent_cookie## | bool | Allow persistent login ||
  540 || ##session_length## | int | Session lifetime in seconds ||
  541 || ##reverse_proxy_addresses## | string | Comma/space-separated proxy IPs ||
  542 || ##reverse_proxy_header## | string | Custom X-Forwarded header ||
  543 || ##language## | string | Default language code ||
  544 || ##multilanguage## | bool | Enable language negotiation ||
  545 || ##allowed_languages## | string | Comma/space-separated allowed langs ||
  546 || ##enable_security_headers## | bool | Send security headers ||
  547 || ##csp## | int | CSP setting (0/1/2) ||
  548 || ##permissions_policy## | int | Permissions-Policy setting (0/1/2) ||
  549 || ##referrer_policy## | int | Referrer-Policy setting (0-8) ||
  550 |#
  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 ----
508 572
509 573 === Workflow Examples ===
510 574
511   ==== Example 1: Handling a GET Request ====
512  
513   %%php
  575 ====Example 1: Handling a GET Request====
  576
  577 %%(hl php)
514 578 // In main wiki entry point
515 579 $http = new Http($db);
516 580 $http->session(0); // Start session
532 596
533 597 ==== Example 2: Handling TLS/HTTPS Upgrade ====
534 598
535   %%php
  599 %%(hl php)
536 600 $http = new Http($db); // Constructor detects TLS requirement
537 601 // If TLS is enabled and user wasn't in TLS before:
538 602 // - Sets TLS session flag
542 606
543 607 ==== Example 3: Invalidating Cache After Page Edit ====
544 608
545   %%php
  609 %%(hl php)
546 610 // User edits a page
547 611 $http = new Http($db);
548 612 $count = $http->invalidate_page('HomePage');
551 615
552 616 ==== Example 4: Serving a File ====
553 617
554   %%php
  618 %%(hl php)
555 619 $http = new Http($db);
556 620 $http->session(2); // Static file mode - no session replay prevention
557 621
626 690
627 691 The class integrates with WackoWiki's diagnostic system:
628 692
629   %%php
  693 %%(hl php)
630 694 // Diagnostic messages are preserved across redirects
631 695 // via session flash data
632 696