Difference between revisions for Users / Eo Ny / dev
| Merge of Version1 & Version2 | |
|---|---|
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | The |
| 6 | |
| 7 | **File Location:** |
| 8 | **Language:** PHP |
| 9 | **Dependencies:** Database class, Session classes, Utility classes (##Ut##), Diagnostics class (##Diag##) |
| 10 | |
| 11 | ---- |
| 12 | |
| 13 | === Class Properties === |
| 14 | |
| 15 | ====Public Properties==== |
| 16 | |
| 17 | #| |
| 18 | *| Property | Type | Description |* |
| 19 | || ##$tls_session## | bool | Indicates if the current session uses HTTPS/TLS encryption || |
| 20 | || ##$request_uri## | string | Normalized REQUEST_URI (e.g., 'PageOfNoReturn/show?a=1') || |
| 21 | || ##$ip## | string | Client's real IP address (accounts for proxies) || |
| 22 | || ##$sess## | Session | Reference to the Session object || |
| 23 | || ##$method## | string | Current HTTP method/request type || |
| 24 | |# |
| 25 | |
| 26 | ====Private Properties==== |
| 27 | |
| 28 | #| |
| 29 | *| Property | Type | Description |* |
| 30 | || ##$db## | object | Database connection reference || |
| 31 | || ##$tls_mark## | string | Cookie name for TLS session marking || |
| 32 | || ##$page## | string | Current page name being processed || |
| 33 | || ##$hash## | string | SHA1 hash of the page name || |
| 34 | || ##$query## | string | Encoded query string || |
| 35 | || ##$lang## | string | Current language code || |
| 36 | || ##$file## | string | Cache file path || |
| 37 | || ##$caching## | int | Flag indicating if page should be cached (0 or 1) || |
| 38 | |# |
| 39 | |
| 40 | ---- |
| 41 | === Constructor === |
| 42 | |
| 43 | %%php |
| 44 | public function __construct(&$db) |
| 45 | |
| 46 | |
| 47 | **Purpose:** Initializes the Http object and sets up HTTP session handling. |
| 48 | |
| 49 | **Parameters:** |
| 50 | |
| 51 | |
| 52 | **Initialization Steps:** |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | **Example:** |
| 61 | |
| 62 | $http = new Http($db); |
| 63 | |
| 64 | |
| 65 | --- |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | Initializes the session handler (file-based or database-based). |
| 73 | |
| 74 | **Parameters:** |
| 75 | |
| 76 | - Bit 2 ( |
| 77 | |
| 78 | **Features:** |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | **Example:** |
| 85 | |
| 86 | $http->session(0); // Normal session |
| 87 | $http->session(2); // Static file serving mode |
| 88 | |
| 89 | |
| 90 | --- |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | Determines if a page can be cached and prepares the cache check. |
| 96 | |
| 97 | **Parameters:** |
| 98 | |
| 99 | |
| 100 | |
| 101 | **Caching Rules:** |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 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:** |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | **Example:** |
| 124 | |
| 125 | // Called at end of page rendering |
| 126 | $http->store_cache(); |
| 127 | |
| 128 | |
| 129 | --- |
| 130 | |
| 131 | |
| 132 | Invalidates all cached versions of a page. |
| 133 | |
| 134 | **Parameters:** |
| 135 | |
| 136 | |
| 137 | **Returns:** |
| 138 | |
| 139 | |
| 140 | **Process:** |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | **Example:** |
| 147 | |
| 148 | $count = $http->invalidate_page('HomePage'); |
| 149 | echo "Invalidated $count cache entries"; |
| 150 | |
| 151 | |
| 152 | --- |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | Switches base URL from HTTP to HTTPS. |
| 158 | |
| 159 | **Purpose:** |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | **Example:** |
| 165 | |
| 166 | $http->secure_base_url(); |
| 167 | // $db->base_url now uses https:// |
| 168 | |
| 169 | |
| 170 | --- |
| 171 | |
| 172 | |
| 173 | Enforces HTTPS for a specific URL and redirects if necessary. |
| 174 | |
| 175 | **Parameters:** |
| 176 | |
| 177 | |
| 178 | **Behavior:** |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | **Example:** |
| 184 | |
| 185 | $http->ensure_tls('/secure/payment'); |
| 186 | |
| 187 | |
| 188 | --- |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | Detects client's real IP address accounting for proxies. |
| 194 | |
| 195 | **Proxy Headers Checked (in order):** |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | **Features:** |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | **Configuration in Database:** |
| 208 | |
| 209 | |
| 210 | |
| 211 | **Example:** |
| 212 | |
| 213 | $client_ip = $http->ip; // e.g., "203.0.113.42" |
| 214 | |
| 215 | |
| 216 | --- |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 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 | 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 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | **Example:** |
| 256 | |
| 257 | $http->http_security_headers(); |
| 258 | %% |
| 259 | |
| 260 | ---- |
| 261 | ==== HTTP Methods ==== |
| 262 | |
| 263 | ===== ##redirect($url, $permanent = false): void## ===== |
| 264 | Performs an HTTP redirect. |
| 265 | |
| 266 | **Parameters:** |
| 267 | |
| 268 | |
| 269 | |
| 270 | **Features:** |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | **Example:** |
| 276 | |
| 277 | $http->redirect('http://example.com/new-page', true); // 301 |
| 278 | $http->redirect('/wiki/HomePage'); // 302 |
| 279 | |
| 280 | |
| 281 | --- |
| 282 | |
| 283 | |
| 284 | Safe exit/die with cleanup. |
| 285 | |
| 286 | **Cleanup Operations:** |
| 287 | |
| 288 | |
| 289 | |
| 290 | **Example:** |
| 291 | |
| 292 | $http->terminate(); |
| 293 | |
| 294 | |
| 295 | --- |
| 296 | |
| 297 | |
| 298 | Sets HTTP response status code. |
| 299 | |
| 300 | **Supported Status Codes:** |
| 301 | |
| 302 | 200 => 'OK' |
| 303 | 206 => 'Partial Content' |
| 304 | 301 => 'Moved Permanently' |
| … | … |
| 315 | 500 => 'Internal Server Error' |
| 316 | 501 => 'Not Implemented' |
| 317 | 503 => 'Service Unavailable' |
| 318 | |
| 319 | |
| 320 | **Example:** |
| 321 | |
| 322 | $http->status(404); // Send 404 Not Found |
| 323 | |
| 324 | |
| 325 | --- |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | Disables caching of the current page. |
| 331 | |
| 332 | **Parameters:** |
| 333 | |
| 334 | - |
| 335 | - |
| 336 | |
| 337 | **Headers Set:** |
| 338 | |
| 339 | |
| 340 | |
| 341 | **Example:** |
| 342 | |
| 343 | $http->no_cache(); // Client-side only |
| 344 | $http->no_cache(false); // Both client & server |
| 345 | |
| 346 | |
| 347 | --- |
| 348 | |
| 349 | |
| 350 | Marks page as publicly cacheable. |
| 351 | |
| 352 | **Headers Set:** |
| 353 | |
| 354 | |
| 355 | **Example:** |
| 356 | |
| 357 | $http->cache_promisc(); |
| 358 | |
| 359 | |
| 360 | --- |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | Determines best language based on browser preferences. |
| 366 | |
| 367 | **Features:** |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | **Example Header:** |
| 374 | |
| 375 | Accept-Language: en-US,en;q=0.9,de;q=0.8 |
| 376 | |
| 377 | |
| 378 | **Returns:** |
| 379 | |
| 380 | |
| 381 | --- |
| 382 | |
| 383 | |
| 384 | Returns list of available language translations. |
| 385 | |
| 386 | **Parameters:** |
| 387 | |
| 388 | - |
| 389 | - |
| 390 | |
| 391 | **Features:** |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | **Returns:** |
| 398 | |
| 399 | |
| 400 | **Example:** |
| 401 | |
| 402 | $all_langs = $http->available_languages(false); |
| 403 | $allowed = $http->available_languages(true); |
| 404 | |
| 405 | |
| 406 | --- |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | Serves files with proper HTTP headers and caching. |
| 412 | |
| 413 | **Parameters:** |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | **Features:** |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 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 | |
| 437 | --- |
| 438 | |
| 439 | |
| 440 | Returns MIME type for a file. |
| 441 | |
| 442 | **Returns:** |
| 443 | |
| 444 | |
| 445 | |
| 446 | **Example:** |
| 447 | |
| 448 | $mime = $http->mime_type('file.pdf'); // 'application/pdf' |
| 449 | |
| 450 | |
| 451 | --- |
| 452 | |
| 453 | |
| 454 | Loads and caches MIME types from configuration. |
| 455 | |
| 456 | **Features:** |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | --- |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | Compresses HTTP response with gzip/x-gzip. |
| 467 | |
| 468 | **Features:** |
| 469 | |
| 470 | |
| 471 | |
| 472 | - 860 bytes < content < 1 MB |
| 473 | - Client accepts compression |
| 474 | - Headers not already sent |
| 475 | |
| 476 | **Example:** |
| 477 | |
| 478 | $http->gzip(); |
| 479 | |
| 480 | |
| 481 | --- |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | Parses URL-encoded strings with special character handling. |
| 487 | |
| 488 | **Purpose:** |
| 489 | |
| 490 | |
| 491 | |
| 492 | **Example:** |
| 493 | |
| 494 | $data = $http->parse_str('name=John&age=30'); |
| 495 | |
| 496 | |
| 497 | --- |
| 498 | |
| 499 | |
| 500 | Extracts and normalizes REQUEST_URI from server. |
| 501 | |
| 502 | **Normalization:** |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | --- |
| 510 | |
| 511 | |
| 512 | Removes prefix from path (case-insensitive). |
| 513 | |
| 514 | --- |
| 515 | |
| 516 | |
| 517 | Loads security header configuration from files. |
| 518 | |
| 519 | **Files Supported:** |
| 520 | - ##csp.conf## / ##csp_custom.conf## |
| 521 | - ##permissions_policy.conf## / ##permissions_policy_custom.conf## |
| 522 | |
| 523 | ---- |
| 524 | |
| 525 | === Configuration Dependencies === |
| 526 | |
| 527 | |
| 528 | |
| 529 | === Constants Used === |
| 530 | |
| 531 | |
| 532 | |
| 533 | === Workflow Examples === |
| 534 | |
| 535 | ==== Example 1: Handling a GET Request ==== |
| 536 | |
| 537 | %%php |
| 538 | // In main wiki entry point |
| 539 | $http = new Http($db); |
| 540 | $http->session(0); // Start session |
| … | … |
| 552 | |
| 553 | // Possibly compress output |
| 554 | $http->gzip(); |
| 555 | |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | $http = new Http($db); // Constructor detects TLS requirement |
| 561 | // If TLS is enabled and user wasn't in TLS before: |
| 562 | // - Sets TLS session flag |
| 563 | // - Marks session with TLS cookie |
| 564 | // - Redirects to HTTPS version |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | // User edits a page |
| 571 | $http = new Http($db); |
| 572 | $count = $http->invalidate_page('HomePage'); |
| 573 | // All cached versions (different languages, methods) are invalidated |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | $http = new Http($db); |
| 580 | $http->session(2); // Static file mode - no session replay prevention |
| 581 | |
| 582 | // Serve with 30-day cache |
| 583 | $http->sendfile('uploads/manual.pdf', 'user-manual.pdf', 30); |
| 584 | |
| 585 | |
| 586 | --- |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | |
| 614 | |
| 615 | |
| 616 | |
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | |
| 622 | |
| 623 | |
| 624 | --- |
| 625 | |
| 626 | |
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | |
| 638 | |
| 639 | |
| 640 | |
| 641 | |
| 642 | |
| 643 | |
| 644 | |
| 645 | |
| 646 | |
| 647 | --- |
| 648 | |
| 649 | |
| 650 | |
| 651 | The class integrates with WackoWiki's diagnostic system: |
| 652 | |
| 653 | |
| 654 | // Diagnostic messages are preserved across redirects |
| 655 | // via session flash data |
| 656 | |
| 657 | // Check cached pages (debug comments in output): |
| 658 | // <!-- WackoWiki Caching Engine: page cached at 2024-01-15 12:30:45 GMT --> |
| 659 | %% |
| 660 | |
| 661 | ---- |
| 662 | |
| 663 | === Related Classes === |
| 664 | - **Session Classes** (##SessionFileStore##, ##SessionDbalStore##) - Session management backends |
| 665 | - **Database Class** - Configuration and cache metadata storage |
| 666 | - **Ut Utility Class** - String/path utilities |
| 667 | - **Diag Class** - Diagnostic logging |
| 668 | |
| 669 | ---- |
| 670 | |
| 671 | === Version History === |
| 672 | - Supports PHP 8.0+ (uses match expressions, union types) |
| 673 | - Follows RFC 9110 for HTTP header handling |
| 674 | - Modern cookie security practices |
| 675 | |
| 676 | ---- |
| 677 | |
| 678 | === Conclusion === |
| 679 | |
| 680 | The ##Http## class is the central request/response handler in WackoWiki, managing everything from session initialization to security headers to file serving. Understanding this class is essential for: |
| 681 | - Extending WackoWiki with custom request handlers |
| 682 | - Implementing custom session logic |
| 683 | - Adding new security policies |
| 684 | - Optimizing cache strategies |
| 685 | - Debugging HTTP-related issues |