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.
40 41 ----
41 42 === Constructor ===
42 43
43   %%php
  44 %%(hl php)
44 45 public function __construct(&$db)
45 46 %%
46 47
58 59   6. Enforces TLS session upgrade if needed
59 60
60 61 **Example:**
61   %%php
  62 %%(hl php)
62 63 $http = new Http($db);
63 64 %%
64 65
82 83   - Recovers diagnostic logs from previous session
83 84
84 85 **Example:**
85   %%php
  86 %%(hl php)
86 87 $http->session(0); // Normal session
87 88 $http->session(2); // Static file serving mode
88 89 %%
105 106   - ✅ Only cached for anonymous users (no logged-in users)
106 107
107 108 **Example:**
108   %%php
  109 %%(hl php)
109 110 $http->check_cache('HomePage', 'show');
110 111 %%
111 112
112 113 ----
113 114
114   ===== ##store_cache(): void## =====
  115 =====##store_cache(): void##=====
115 116 Saves the generated page content to cache file.
116 117
117 118 **Features:**
121 122   - Only executes if caching flag is set and user is anonymous
122 123
123 124 **Example:**
124   %%php
  125 %%(hl php)
125 126 // Called at end of page rendering
126 127 $http->store_cache();
127 128 %%
144 145   4. Returns count of invalidated caches
145 146
146 147 **Example:**
147   %%php
  148 %%(hl php)
148 149 $count = $http->invalidate_page('HomePage');
149 150 echo "Invalidated $count cache entries";
150 151 %%
162 163   - Called when TLS session is detected
163 164
164 165 **Example:**
165   %%php
  166 %%(hl php)
166 167 $http->secure_base_url();
167 168 // $db->base_url now uses https://
168 169 %%
181 182   - Converts relative URLs using current server name
182 183
183 184 **Example:**
184   %%php
  185 %%(hl php)
185 186 $http->ensure_tls('/secure/payment');
186 187 %%
187 188
209 210   - ##reverse_proxy_header## - Custom header name (default: ##X-Forwarded-For##)
210 211
211 212 **Example:**
212   %%php
  213 %%(hl php)
213 214 $client_ip = $http->ip; // e.g., "203.0.113.42"
214 215 %%
215 216
231 232
232 233 ==== Security Headers ====
233 234
234   ===== ##http_security_headers(): void## =====
235  
236  
  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 ----
237 262 ==== HTTP Methods ====
238 263
239 264 ===== ##redirect($url, $permanent = false): void## =====
249 274   - Uses output buffering to work anywhere in page processing
250 275
251 276 **Example:**
252   %%php
  277 %%(hl php)
253 278 $http->redirect('http://example.com/new-page', true); // 301
254 279 $http->redirect('/wiki/HomePage'); // 302
255 280 %%
264 289   - Ends script execution
265 290
266 291 **Example:**
267   %%php
  292 %%(hl php)
268 293 $http->terminate();
269 294 %%
270 295
274 299 Sets HTTP response status code.
275 300
276 301 **Supported Status Codes:**
277   %%php
  302 %%(hl php)
278 303 200 => 'OK'
279 304 206 => 'Partial Content'
280 305 301 => 'Moved Permanently'
294 319 %%
295 320
296 321 **Example:**
297   %%php
  322 %%(hl php)
298 323 $http->status(404); // Send 404 Not Found
299 324 %%
300 325
315 340   - ##Cache-Control: no-store##
316 341
317 342 **Example:**
318   %%php
  343 %%(hl php)
319 344 $http->no_cache(); // Client-side only
320 345 $http->no_cache(false); // Both client & server
321 346 %%
329 354   - ##Cache-Control: public##
330 355
331 356 **Example:**
332   %%php
  357 %%(hl php)
333 358 $http->cache_promisc();
334 359 %%
335 360
374 399   - Associative array: ##['en' => 'en', 'de' => 'de', ...]##
375 400
376 401 **Example:**
377   %%php
  402 %%(hl php)
378 403 $all_langs = $http->available_languages(false);
379 404 $allowed = $http->available_languages(true);
380 405 %%
400 425   - GZip compression for text files
401 426
402 427 **Special Paths:**
403   %%php
  428 %%(hl php)
404 429 $http->sendfile(404); // Serves file defined by HTTP_404 constant
405 430 $http->sendfile(403); // Serves file defined by HTTP_403 constant
406 431 %%
407 432
408 433 **Example:**
409   %%php
  434 %%(hl php)
410 435 $http->sendfile('uploads/document.pdf', 'my-document.pdf', 30);
411 436 %%
412 437
420 445   - Default: ##'application/octet-stream'##
421 446
422 447 **Example:**
423   %%php
  448 %%(hl php)
424 449 $mime = $http->mime_type('file.pdf'); // 'application/pdf'
425 450 %%
426 451
450 475   - Headers not already sent
451 476
452 477 **Example:**
453   %%php
  478 %%(hl php)
454 479 $http->gzip();
455 480 %%
456 481
466 491   - Converts encoding properly
467 492
468 493 **Example:**
469   %%php
  494 %%(hl php)
470 495 $data = $http->parse_str('name=John&age=30');
471 496 %%
472 497
498 523
499 524 ----
500 525
501   === Configuration Dependencies ===
502  
503  
504  
505   === Constants Used ===
506  
507  
  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 ----
508 573
509 574 === Workflow Examples ===
510 575
511   ==== Example 1: Handling a GET Request ====
512  
513   %%php
  576 ====Example 1: Handling a GET Request====
  577
  578 %%(hl php)
514 579 // In main wiki entry point
515 580 $http = new Http($db);
516 581 $http->session(0); // Start session
532 597
533 598 ==== Example 2: Handling TLS/HTTPS Upgrade ====
534 599
535   %%php
  600 %%(hl php)
536 601 $http = new Http($db); // Constructor detects TLS requirement
537 602 // If TLS is enabled and user wasn't in TLS before:
538 603 // - Sets TLS session flag
542 607
543 608 ==== Example 3: Invalidating Cache After Page Edit ====
544 609
545   %%php
  610 %%(hl php)
546 611 // User edits a page
547 612 $http = new Http($db);
548 613 $count = $http->invalidate_page('HomePage');
551 616
552 617 ==== Example 4: Serving a File ====
553 618
554   %%php
  619 %%(hl php)
555 620 $http = new Http($db);
556 621 $http->session(2); // Static file mode - no session replay prevention
557 622
626 691
627 692 The class integrates with WackoWiki's diagnostic system:
628 693
629   %%php
  694 %%(hl php)
630 695 // Diagnostic messages are preserved across redirects
631 696 // via session flash data
632 697