Difference between revisions for Users / Eo Ny / dev
| Version1 | Version2 | Differences |
|---|---|---|
| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
The |
|
| 6 |
|
|
| 7 |
**File Location:** |
|
| 1 | == HTTP Class Technical Documentation == | |
| 2 | ||
| 3 | === Overview === | |
| 4 | ||
| 5 | 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. | |
| 6 | ||
| 7 | **File Location:** ##src/class/http.php## | |
| 8 | 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 | | Property | Type | Description | | |
| 18 | |----------|------|-------------| | |
| 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 | ### Private Properties | |
| 26 | ||
| 27 | | Property | Type | Description | | |
| 28 | |----------|------|-------------| | |
| 29 | | `$db` | object | Database connection reference | | |
| 30 | | `$tls_mark` | string | Cookie name for TLS session marking | | |
| 31 | | `$page` | string | Current page name being processed | | |
| 32 | | `$hash` | string | SHA1 hash of the page name | | |
| 33 | | `$query` | string | Encoded query string | | |
| 34 | | `$lang` | string | Current language code | | |
| 35 | | `$file` | string | Cache file path | | |
| 36 | | `$caching` | int | Flag indicating if page should be cached (0 or 1) | | |
| 37 | ||
| 38 | --- | |
| 39 | ||
| 40 | ## Constructor | |
| 41 | ||
| 42 | ```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 | === Constructor === | |
| 30 | ||
| 31 | %%php | |
| 43 | 32 | public function __construct(&$db) |
| 44 |
|
|
| 33 | %% | |
| 45 | 34 | |
| 46 | 35 | **Purpose:** Initializes the Http object and sets up HTTP session handling. |
| 47 | 36 | |
| 48 | 37 | **Parameters:** |
| 49 |
|
|
| 38 | - ##$db## - Database object reference | |
| 50 | 39 | |
| 51 | 40 | **Initialization Steps:** |
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
**Example:** |
|
| 60 |
|
|
| 41 | 1. Stores database reference | |
| 42 | 2. Extracts and normalizes REQUEST_URI | |
| 43 | 3. Detects TLS/HTTPS session status | |
| 44 | 4. Determines client's real IP address | |
| 45 | 5. Sets up TLS mark cookie name | |
| 46 | 6. Enforces TLS session upgrade if needed | |
| 47 | ||
| 48 | **Example:** | |
| 49 | %%php | |
| 61 | 50 | $http = new Http($db); |
| 62 |
|
|
| 63 |
|
|
| 64 |
--- |
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 51 | %% | |
| 52 | ||
| 53 | ---- | |
| 54 | ||
| 55 | === Core Methods === | |
| 56 | ||
| 57 | ==== Session Management ==== | |
| 58 | ||
| 59 | ===== ##session($route): void## ===== | |
| 71 | 60 | Initializes the session handler (file-based or database-based). |
| 72 | 61 | |
| 73 | 62 | **Parameters:** |
| 74 |
|
|
| 75 |
- Bit 2 ( |
|
| 76 |
|
|
| 77 |
**Features:** |
|
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
**Example:** |
|
| 84 |
|
|
| 63 | - ##$route## (int) - Routing flag: | |
| 64 | - Bit 2 (##$route & 2##): Enable static mode for files/freecap (disables replay prevention and ID regeneration) | |
| 65 | ||
| 66 | **Features:** | |
| 67 | - Selects storage backend (file or database) | |
| 68 | - Configures cookie settings (security, path, httponly) | |
| 69 | - Binds IP and TLS validation | |
| 70 | - Recovers diagnostic logs from previous session | |
| 71 | ||
| 72 | **Example:** | |
| 73 | %%php | |
| 85 | 74 | $http->session(0); // Normal session |
| 86 | 75 | $http->session(2); // Static file serving mode |
| 87 |
|
|
| 88 |
|
|
| 89 |
--- |
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
|
| 76 | %% | |
| 77 | ||
| 78 | ---- | |
| 79 | ||
| 80 | ==== Caching System ==== | |
| 81 | ||
| 82 | ===== ##check_cache($page, $method): void## ===== | |
| 94 | 83 | Determines if a page can be cached and prepares the cache check. |
| 95 | 84 | |
| 96 | 85 | **Parameters:** |
| 97 |
|
|
| 98 |
|
|
| 86 | - ##$page## (string) - Page name to cache | |
| 87 | - ##$method## (string) - Request method/action (e.g., 'show', 'edit') | |
| 99 | 88 | |
| 100 | 89 | **Caching Rules:** |
| 101 |
|
|
| 102 |
|
|
| 103 |
|
|
| 104 |
|
|
| 105 |
|
|
| 106 |
**Example:** |
|
| 107 |
|
|
| 90 | - ✅ Enabled for GET requests only | |
| 91 | - ✅ Disabled for POST requests | |
| 92 | - ❌ Never cached for 'edit' or 'watch' methods | |
| 93 | - ✅ Only cached for anonymous users (no logged-in users) | |
| 94 | ||
| 95 | **Example:** | |
| 96 | %%php | |
| 108 | 97 | $http->check_cache('HomePage', 'show'); |
| 109 |
|
|
| 110 |
|
|
| 111 |
--- |
|
| 112 |
|
|
| 113 |
|
|
| 98 | %% | |
| 99 | ||
| 100 | ---- | |
| 101 | ||
| 102 | ===== ##store_cache(): void## ===== | |
| 114 | 103 | Saves the generated page content to cache file. |
| 115 | 104 | |
| 116 | 105 | **Features:** |
| 117 |
|
|
| 118 |
|
|
| 119 |
|
|
| 120 |
|
|
| 121 |
|
|
| 122 |
**Example:** |
|
| 123 |
|
|
| 106 | - Retrieves output buffer content | |
| 107 | - Saves to cache file with proper permissions | |
| 108 | - Records cache metadata in database | |
| 109 | - Only executes if caching flag is set and user is anonymous | |
| 110 | ||
| 111 | **Example:** | |
| 112 | %%php | |
| 124 | 113 | // Called at end of page rendering |
| 125 | 114 | $http->store_cache(); |
| 126 |
|
|
| 127 |
|
|
| 128 |
--- |
|
| 129 |
|
|
| 130 |
|
|
| 115 | %% | |
| 116 | ||
| 117 | ---- | |
| 118 | ||
| 119 | ===== ##invalidate_page($page): int## ===== | |
| 131 | 120 | Invalidates all cached versions of a page. |
| 132 | 121 | |
| 133 | 122 | **Parameters:** |
| 134 |
|
|
| 123 | - ##$page## (string) - Page name to invalidate | |
| 135 | 124 | |
| 136 | 125 | **Returns:** |
| 137 |
|
|
| 126 | - Number of cache entries invalidated | |
| 138 | 127 | |
| 139 | 128 | **Process:** |
| 140 |
|
|
| 141 |
|
|
| 142 |
|
|
| 143 |
|
|
| 144 |
|
|
| 145 |
**Example:** |
|
| 146 |
|
|
| 129 | 1. Finds all cached versions (different methods/languages) | |
| 130 | 2. Touches files to past timestamp (faster than deletion) | |
| 131 | 3. Removes entries from cache metadata table | |
| 132 | 4. Returns count of invalidated caches | |
| 133 | ||
| 134 | **Example:** | |
| 135 | %%php | |
| 147 | 136 | $count = $http->invalidate_page('HomePage'); |
| 148 | 137 | echo "Invalidated $count cache entries"; |
| 149 |
|
|
| 150 |
|
|
| 151 |
--- |
|
| 152 |
|
|
| 153 |
|
|
| 154 |
|
|
| 155 |
|
|
| 138 | %% | |
| 139 | ||
| 140 | ---- | |
| 141 | ||
| 142 | ==== TLS/HTTPS Security ==== | |
| 143 | ||
| 144 | ===== ##secure_base_url(): void## ===== | |
| 156 | 145 | Switches base URL from HTTP to HTTPS. |
| 157 | 146 | |
| 158 | 147 | **Purpose:** |
| 159 |
|
|
| 160 |
|
|
| 161 |
|
|
| 162 |
|
|
| 163 |
**Example:** |
|
| 164 |
|
|
| 148 | - Ensures all subsequent URLs use HTTPS | |
| 149 | - Stores original HTTP URL for fallback | |
| 150 | - Called when TLS session is detected | |
| 151 | ||
| 152 | **Example:** | |
| 153 | %%php | |
| 165 | 154 | $http->secure_base_url(); |
| 166 | 155 | // $db->base_url now uses https:// |
| 167 |
|
|
| 168 |
|
|
| 169 |
--- |
|
| 170 |
|
|
| 171 |
|
|
| 156 | %% | |
| 157 | ||
| 158 | ---- | |
| 159 | ||
| 160 | ===== ##ensure_tls($url): void## ===== | |
| 172 | 161 | Enforces HTTPS for a specific URL and redirects if necessary. |
| 173 | 162 | |
| 174 | 163 | **Parameters:** |
| 175 |
|
|
| 164 | - ##$url## (string) - URL to secure | |
| 176 | 165 | |
| 177 | 166 | **Behavior:** |
| 178 |
|
|
| 179 |
|
|
| 180 |
|
|
| 181 |
|
|
| 182 |
**Example:** |
|
| 183 |
|
|
| 167 | - If not already HTTPS and TLS is enabled, forces HTTPS redirect | |
| 168 | - Handles both relative and absolute URLs | |
| 169 | - Converts relative URLs using current server name | |
| 170 | ||
| 171 | **Example:** | |
| 172 | %%php | |
| 184 | 173 | $http->ensure_tls('/secure/payment'); |
| 185 |
|
|
| 186 |
|
|
| 187 |
--- |
|
| 188 |
|
|
| 189 |
|
|
| 190 |
|
|
| 191 |
|
|
| 174 | %% | |
| 175 | ||
| 176 | ---- | |
| 177 | ||
| 178 | ==== IP Address Detection ==== | |
| 179 | ||
| 180 | ===== ##real_ip(): string## (Private) ===== | |
| 192 | 181 | Detects client's real IP address accounting for proxies. |
| 193 | 182 | |
| 194 | 183 | **Proxy Headers Checked (in order):** |
| 195 |
|
|
| 196 |
|
|
| 197 |
|
|
| 198 |
|
|
| 199 |
|
|
| 200 |
|
|
| 201 |
**Features:** |
|
| 202 |
|
|
| 203 |
|
|
| 204 |
|
|
| 184 | 1. ##HTTP_X_CLUSTER_CLIENT_IP## | |
| 185 | 2. ##HTTP_X_FORWARDED_FOR## (or custom header) | |
| 186 | 3. ##HTTP_CLIENT_IP## | |
| 187 | 4. ##HTTP_X_REMOTE_ADDR## | |
| 188 | 5. ##REMOTE_ADDR## (fallback) | |
| 189 | ||
| 190 | **Features:** | |
| 191 | - Filters out private/reserved IP ranges | |
| 192 | - Respects configured reverse proxy addresses | |
| 193 | - Returns ##'0.0.0.0'## as fallback | |
| 205 | 194 | |
| 206 | 195 | **Configuration in Database:** |
| 207 |
|
|
| 208 |
|
|
| 209 |
|
|
| 210 |
**Example:** |
|
| 211 |
|
|
| 196 | - ##reverse_proxy_addresses## - Comma/space-separated proxy IPs | |
| 197 | - ##reverse_proxy_header## - Custom header name (default: ##X-Forwarded-For##) | |
| 198 | ||
| 199 | **Example:** | |
| 200 | %%php | |
| 212 | 201 | $client_ip = $http->ip; // e.g., "203.0.113.42" |
| 213 |
|
|
| 214 |
|
|
| 215 |
--- |
|
| 216 |
|
|
| 217 |
|
|
| 218 |
|
|
| 219 |
|
|
| 202 | %% | |
| 203 | ||
| 204 | ---- | |
| 205 | ||
| 206 | ==== HTTPS Detection ==== | |
| 207 | ||
| 208 | ===== ##tls_session(): bool## (Private) ===== | |
| 220 | 209 | Detects if current connection uses HTTPS/TLS. |
| 221 | 210 | |
| 222 | 211 | **Checks (any being true = HTTPS):** |
| 223 | - `$_SERVER['HTTPS']` is 'on' | |
| 224 | - `$_SERVER['SERVER_PORT']` is 443 | |
| 225 | - `$_SERVER['HTTP_X_FORWARDED_PROTO']` is 'https' | |
| 226 | - `$_SERVER['HTTP_X_FORWARDED_SSL']` is 'on' | |
| 227 | - `$_SERVER['HTTP_X_FORWARDED_PORT']` is 443 | |
| 228 | ||
| 229 | --- | |
| 230 | ||
| 231 | ### Security Headers | |
| 232 | ||
| 233 | #### `http_security_headers(): void` | |
| 234 | Sets security-related HTTP headers. | |
| 235 | ||
| 236 | **Headers Set:** | |
| 237 | ||
| 238 | | Header | Purpose | Config Key | | |
| 239 | |--------|---------|------------| | |
| 240 | | Content-Security-Policy | XSS/injection protection | `csp` | | |
| 241 | | Permissions-Policy | Control browser features | `permissions_policy` | | |
| 242 | | Referrer-Policy | Control referrer information | `referrer_policy` | | |
| 243 | | Strict-Transport-Security | Force HTTPS | Auto (TLS only) | | |
| 244 | | X-Frame-Options | Clickjacking protection | Hardcoded: `SAMEORIGIN` | | |
| 245 | | X-Content-Type-Options | MIME sniffing prevention | Hardcoded: `nosniff` | | |
| 246 | ||
| 247 | **CSP Configuration Options:** | |
| 248 | - `0` - Disabled | |
| 249 | - `1` - Default policy (from `csp.conf`) | |
| 250 | - `2` - Custom policy (from `csp_custom.conf`) | |
| 251 | ||
| 252 | **Example:** | |
| 253 | ```php | |
| 254 | $http->http_security_headers(); | |
| 255 | ``` | |
| 256 | ||
| 257 | --- | |
| 258 | ||
| 259 | ### HTTP Methods | |
| 260 | ||
| 261 | #### `redirect($url, $permanent = false): void` | |
| 212 | - ##$_SERVER['HTTPS']## is 'on' | |
| 213 | - ##$_SERVER['SERVER_PORT']## is 443 | |
| 214 | - ##$_SERVER['HTTP_X_FORWARDED_PROTO']## is 'https' | |
| 215 | - ##$_SERVER['HTTP_X_FORWARDED_SSL']## is 'on' | |
| 216 | - ##$_SERVER['HTTP_X_FORWARDED_PORT']## is 443 | |
| 217 | ||
| 218 | ---- | |
| 219 | ||
| 220 | ==== Security Headers ==== | |
| 221 | ||
| 222 | ===== ##http_security_headers(): void## ===== | |
| 223 | ||
| 224 | ||
| 225 | ==== HTTP Methods ==== | |
| 226 | ||
| 227 | ===== ##redirect($url, $permanent = false): void## ===== | |
| 262 | 228 | Performs an HTTP redirect. |
| 263 | 229 | |
| 264 | 230 | **Parameters:** |
| 265 |
|
|
| 266 |
|
|
| 267 |
|
|
| 268 |
**Features:** |
|
| 269 |
|
|
| 270 |
|
|
| 271 |
|
|
| 272 |
|
|
| 273 |
**Example:** |
|
| 274 |
|
|
| 231 | - ##$url## (string) - Target URL | |
| 232 | - ##$permanent## (bool) - Use 301 (permanent) vs 302 (temporary) | |
| 233 | ||
| 234 | **Features:** | |
| 235 | - Decodes ##&## entities to prevent broken redirects | |
| 236 | - Only works if headers not yet sent | |
| 237 | - Uses output buffering to work anywhere in page processing | |
| 238 | ||
| 239 | **Example:** | |
| 240 | %%php | |
| 275 | 241 | $http->redirect('http://example.com/new-page', true); // 301 |
| 276 | 242 | $http->redirect('/wiki/HomePage'); // 302 |
| 277 |
|
|
| 278 |
|
|
| 279 |
--- |
|
| 280 |
|
|
| 281 |
|
|
| 243 | %% | |
| 244 | ||
| 245 | ---- | |
| 246 | ||
| 247 | ===== ##terminate(): void## ===== | |
| 282 | 248 | Safe exit/die with cleanup. |
| 283 | 249 | |
| 284 | 250 | **Cleanup Operations:** |
| 285 |
|
|
| 286 |
|
|
| 287 |
|
|
| 288 |
**Example:** |
|
| 289 |
|
|
| 251 | - Saves diagnostic logs to session flash data | |
| 252 | - Ends script execution | |
| 253 | ||
| 254 | **Example:** | |
| 255 | %%php | |
| 290 | 256 | $http->terminate(); |
| 291 |
|
|
| 292 |
|
|
| 293 |
--- |
|
| 294 |
|
|
| 295 |
|
|
| 257 | %% | |
| 258 | ||
| 259 | ---- | |
| 260 | ||
| 261 | ===== ##status($code): void## ===== | |
| 296 | 262 | Sets HTTP response status code. |
| 297 | 263 | |
| 298 | 264 | **Supported Status Codes:** |
| 299 |
|
|
| 265 | %%php | |
| 300 | 266 | 200 => 'OK' |
| 301 | 267 | 206 => 'Partial Content' |
| 302 | 268 | 301 => 'Moved Permanently' |
| … | … | … |
| 313 | 279 | 500 => 'Internal Server Error' |
| 314 | 280 | 501 => 'Not Implemented' |
| 315 | 281 | 503 => 'Service Unavailable' |
| 316 |
|
|
| 317 |
|
|
| 318 |
**Example:** |
|
| 319 |
|
|
| 282 | %% | |
| 283 | ||
| 284 | **Example:** | |
| 285 | %%php | |
| 320 | 286 | $http->status(404); // Send 404 Not Found |
| 321 |
|
|
| 322 |
|
|
| 323 |
--- |
|
| 324 |
|
|
| 325 |
|
|
| 326 |
|
|
| 327 |
|
|
| 287 | %% | |
| 288 | ||
| 289 | ---- | |
| 290 | ||
| 291 | ==== Caching Control ==== | |
| 292 | ||
| 293 | ===== ##no_cache($client_only = true): void## ===== | |
| 328 | 294 | Disables caching of the current page. |
| 329 | 295 | |
| 330 | 296 | **Parameters:** |
| 331 |
|
|
| 332 |
- |
|
| 333 |
- |
|
| 297 | - ##$client_only## (bool, default: TRUE) | |
| 298 | - ##TRUE##: Disable browser cache only | |
| 299 | - ##FALSE##: Disable both browser and server cache | |
| 334 | 300 | |
| 335 | 301 | **Headers Set:** |
| 336 |
|
|
| 337 |
|
|
| 338 |
|
|
| 339 |
**Example:** |
|
| 340 |
|
|
| 302 | - ##Last-Modified: <current-time>## (always fresh) | |
| 303 | - ##Cache-Control: no-store## | |
| 304 | ||
| 305 | **Example:** | |
| 306 | %%php | |
| 341 | 307 | $http->no_cache(); // Client-side only |
| 342 | 308 | $http->no_cache(false); // Both client & server |
| 343 |
|
|
| 344 |
|
|
| 345 |
--- |
|
| 346 |
|
|
| 347 |
|
|
| 309 | %% | |
| 310 | ||
| 311 | ---- | |
| 312 | ||
| 313 | ===== ##cache_promisc(): void## ===== | |
| 348 | 314 | Marks page as publicly cacheable. |
| 349 | 315 | |
| 350 | 316 | **Headers Set:** |
| 351 |
|
|
| 352 |
|
|
| 353 |
**Example:** |
|
| 354 |
|
|
| 317 | - ##Cache-Control: public## | |
| 318 | ||
| 319 | **Example:** | |
| 320 | %%php | |
| 355 | 321 | $http->cache_promisc(); |
| 356 |
|
|
| 357 |
|
|
| 358 |
--- |
|
| 359 |
|
|
| 360 |
|
|
| 361 |
|
|
| 362 |
|
|
| 322 | %% | |
| 323 | ||
| 324 | ---- | |
| 325 | ||
| 326 | ==== Language Negotiation ==== | |
| 327 | ||
| 328 | ===== ##user_agent_language(): string## ===== | |
| 363 | 329 | Determines best language based on browser preferences. |
| 364 | 330 | |
| 365 | 331 | **Features:** |
| 366 |
|
|
| 367 |
|
|
| 368 |
|
|
| 369 |
|
|
| 332 | - Follows RFC 9110 section 12.5.4 (HTTP Accept-Language) | |
| 333 | - Parses ##Accept-Language## header with quality factors | |
| 334 | - Attempts exact match first, then language fallback | |
| 335 | - Falls back to default system language | |
| 370 | 336 | |
| 371 | 337 | **Example Header:** |
| 372 |
|
|
| 338 | %% | |
| 373 | 339 | Accept-Language: en-US,en;q=0.9,de;q=0.8 |
| 374 |
|
|
| 340 | %% | |
| 375 | 341 | |
| 376 | 342 | **Returns:** |
| 377 |
|
|
| 378 |
|
|
| 379 |
--- |
|
| 380 |
|
|
| 381 |
|
|
| 343 | - Language code (e.g., 'en', 'en-US', 'de') | |
| 344 | ||
| 345 | ---- | |
| 346 | ||
| 347 | ===== ##available_languages($subset = true): array## ===== | |
| 382 | 348 | Returns list of available language translations. |
| 383 | 349 | |
| 384 | 350 | **Parameters:** |
| 385 |
|
|
| 386 |
- |
|
| 387 |
- |
|
| 388 |
|
|
| 389 |
**Features:** |
|
| 390 |
|
|
| 391 |
|
|
| 392 |
|
|
| 393 |
|
|
| 351 | - ##$subset## (bool, default: TRUE) | |
| 352 | - ##TRUE##: Only allowed languages | |
| 353 | - ##FALSE##: All available languages | |
| 354 | ||
| 355 | **Features:** | |
| 356 | - Scans ##LANG_DIR## for language files | |
| 357 | - Filters by ##allowed_languages## config if set | |
| 358 | - Caches result in session | |
| 359 | - System language always included | |
| 394 | 360 | |
| 395 | 361 | **Returns:** |
| 396 |
|
|
| 397 |
|
|
| 398 |
**Example:** |
|
| 399 |
|
|
| 362 | - Associative array: ##['en' => 'en', 'de' => 'de', ...]## | |
| 363 | ||
| 364 | **Example:** | |
| 365 | %%php | |
| 400 | 366 | $all_langs = $http->available_languages(false); |
| 401 | 367 | $allowed = $http->available_languages(true); |
| 402 |
|
|
| 403 |
|
|
| 404 |
--- |
|
| 405 |
|
|
| 406 |
|
|
| 407 |
|
|
| 408 |
|
|
| 368 | %% | |
| 369 | ||
| 370 | ---- | |
| 371 | ||
| 372 | ==== File Serving ==== | |
| 373 | ||
| 374 | ===== ##sendfile($path, $filename = null, $age = null): void## ===== | |
| 409 | 375 | Serves files with proper HTTP headers and caching. |
| 410 | 376 | |
| 411 | 377 | **Parameters:** |
| 412 |
|
|
| 413 |
|
|
| 414 |
|
|
| 415 |
|
|
| 416 |
**Features:** |
|
| 417 |
|
|
| 418 |
|
|
| 419 |
|
|
| 420 |
|
|
| 421 |
|
|
| 422 |
|
|
| 378 | - ##$path## (string) - File path (or HTTP_XXX constant for error pages) | |
| 379 | - ##$filename## (string, optional) - Custom download filename | |
| 380 | - ##$age## (int, optional) - Cache age in days | |
| 381 | ||
| 382 | **Features:** | |
| 383 | - HTTP range request support (partial file downloads) | |
| 384 | - ETag and Last-Modified conditional requests | |
| 385 | - Proper MIME type detection | |
| 386 | - Content-Security-Policy for special file types | |
| 387 | - Streaming for large files | |
| 388 | - GZip compression for text files | |
| 423 | 389 | |
| 424 | 390 | **Special Paths:** |
| 425 |
|
|
| 391 | %%php | |
| 426 | 392 | $http->sendfile(404); // Serves file defined by HTTP_404 constant |
| 427 | 393 | $http->sendfile(403); // Serves file defined by HTTP_403 constant |
| 428 |
|
|
| 429 |
|
|
| 430 |
**Example:** |
|
| 431 |
|
|
| 394 | %% | |
| 395 | ||
| 396 | **Example:** | |
| 397 | %%php | |
| 432 | 398 | $http->sendfile('uploads/document.pdf', 'my-document.pdf', 30); |
| 433 |
|
|
| 434 |
|
|
| 435 |
--- |
|
| 436 |
|
|
| 437 |
|
|
| 399 | %% | |
| 400 | ||
| 401 | ---- | |
| 402 | ||
| 403 | ===== ##mime_type($path): string## ===== | |
| 438 | 404 | Returns MIME type for a file. |
| 439 | 405 | |
| 440 | 406 | **Returns:** |
| 441 |
|
|
| 442 |
|
|
| 443 |
|
|
| 444 |
**Example:** |
|
| 445 |
|
|
| 407 | - MIME type string (e.g., 'application/pdf') | |
| 408 | - Default: ##'application/octet-stream'## | |
| 409 | ||
| 410 | **Example:** | |
| 411 | %%php | |
| 446 | 412 | $mime = $http->mime_type('file.pdf'); // 'application/pdf' |
| 447 |
|
|
| 448 |
|
|
| 449 |
--- |
|
| 450 |
|
|
| 451 |
|
|
| 413 | %% | |
| 414 | ||
| 415 | ---- | |
| 416 | ||
| 417 | ===== ##mime_types(): array## (Private) ===== | |
| 452 | 418 | Loads and caches MIME types from configuration. |
| 453 | 419 | |
| 454 | 420 | **Features:** |
| 455 |
|
|
| 456 |
|
|
| 457 |
|
|
| 458 |
|
|
| 459 |
--- |
|
| 460 |
|
|
| 461 |
|
|
| 462 |
|
|
| 463 |
|
|
| 421 | - Reads from ##config/mime.types## | |
| 422 | - Caches to ##cache/config/mime.types## | |
| 423 | - Reloads if config is updated | |
| 424 | ||
| 425 | ---- | |
| 426 | ||
| 427 | ==== Compression ==== | |
| 428 | ||
| 429 | ===== ##gzip(): void## ===== | |
| 464 | 430 | Compresses HTTP response with gzip/x-gzip. |
| 465 | 431 | |
| 466 | 432 | **Features:** |
| 467 |
|
|
| 468 |
|
|
| 469 |
|
|
| 433 | - Manually implements gzip (not relying on zlib.output_compression) | |
| 434 | - Produces correct ##Content-Length## header | |
| 435 | - Only compresses if: | |
| 470 | 436 | - 860 bytes < content < 1 MB |
| 471 | 437 | - Client accepts compression |
| 472 | 438 | - Headers not already sent |
| 473 | 439 | |
| 474 | 440 | **Example:** |
| 475 |
|
|
| 441 | %%php | |
| 476 | 442 | $http->gzip(); |
| 477 |
|
|
| 478 |
|
|
| 479 |
--- |
|
| 480 |
|
|
| 481 |
|
|
| 482 |
|
|
| 483 |
|
|
| 443 | %% | |
| 444 | ||
| 445 | ---- | |
| 446 | ||
| 447 | ==== Utility Methods ==== | |
| 448 | ||
| 449 | ===== ##parse_str($str): array## (Private) ===== | |
| 484 | 450 | Parses URL-encoded strings with special character handling. |
| 485 | 451 | |
| 486 | 452 | **Purpose:** |
| 487 |
|
|
| 488 |
|
|
| 489 |
|
|
| 490 |
**Example:** |
|
| 491 |
|
|
| 453 | - Safely handles special characters in query/form data | |
| 454 | - Converts encoding properly | |
| 455 | ||
| 456 | **Example:** | |
| 457 | %%php | |
| 492 | 458 | $data = $http->parse_str('name=John&age=30'); |
| 493 |
|
|
| 494 |
|
|
| 495 |
--- |
|
| 496 |
|
|
| 497 |
|
|
| 459 | %% | |
| 460 | ||
| 461 | ---- | |
| 462 | ||
| 463 | ===== ##request_uri(): string## (Private) ===== | |
| 498 | 464 | Extracts and normalizes REQUEST_URI from server. |
| 499 | 465 | |
| 500 | 466 | **Normalization:** |
| 501 |
|
|
| 502 |
|
|
| 503 |
|
|
| 504 |
|
|
| 505 |
|
|
| 506 |
|
|
| 507 |
--- |
|
| 508 |
|
|
| 509 |
|
|
| 467 | - Removes base URL prefix | |
| 468 | - Removes spaces | |
| 469 | - Collapses multiple slashes | |
| 470 | - Removes ##..## path traversal attempts | |
| 471 | - Removes leading/trailing slashes | |
| 472 | ||
| 473 | ---- | |
| 474 | ||
| 475 | ===== ##cut_prefix($prefix, $path): string## (Private) ===== | |
| 510 | 476 | Removes prefix from path (case-insensitive). |
| 511 | 477 | |
| 512 |
--- |
|
| 513 |
|
|
| 514 |
|
|
| 478 | ---- | |
| 479 | ||
| 480 | ===== ##get_header_conf($file_name): string## (Private) ===== | |
| 515 | 481 | Loads security header configuration from files. |
| 516 | 482 | |
| 517 | 483 | **Files Supported:** |
| 518 | - `csp.conf` / `csp_custom.conf` | |
| 519 | - `permissions_policy.conf` / `permissions_policy_custom.conf` | |
| 520 | ||
| 521 | --- | |
| 522 | ||
| 523 | ## Configuration Dependencies | |
| 524 | ||
| 525 | The class relies on these database configuration settings: | |
| 526 | ||
| 527 | | Setting | Type | Purpose | | |
| 528 | |---------|------|---------| | |
| 529 | | `base_url` | string | Wiki's base URL | | |
| 530 | | `tls` | bool | Enable HTTPS enforcement | | |
| 531 | | `cache` | bool | Enable page caching | | |
| 532 | | `cache_ttl` | int | Cache lifetime in seconds | | |
| 533 | | `session_store` | int | 1=File, 0=Database | | |
| 534 | | `system_seed_hash` | string | Session encryption seed | | |
| 535 | | `cookie_prefix` | string | Session cookie prefix | | |
| 536 | | `cookie_path` | string | Cookie path | | |
| 537 | | `allow_persistent_cookie` | bool | Allow persistent login | | |
| 538 | | `session_length` | int | Session lifetime in seconds | | |
| 539 | | `reverse_proxy_addresses` | string | Comma/space-separated proxy IPs | | |
| 540 | | `reverse_proxy_header` | string | Custom X-Forwarded header | | |
| 541 | | `language` | string | Default language code | | |
| 542 | | `multilanguage` | bool | Enable language negotiation | | |
| 543 | | `allowed_languages` | string | Comma/space-separated allowed langs | | |
| 544 | | `enable_security_headers` | bool | Send security headers | | |
| 545 | | `csp` | int | CSP setting (0/1/2) | | |
| 546 | | `permissions_policy` | int | Permissions-Policy setting (0/1/2) | | |
| 547 | | `referrer_policy` | int | Referrer-Policy setting (0-8) | | |
| 548 | ||
| 549 | --- | |
| 550 | ||
| 551 | ## Constants Used | |
| 552 | ||
| 553 | | Constant | Type | Purpose | | |
| 554 | |----------|------|---------| | |
| 555 | | `IN_WACKO` | bool | Security check (exit if not defined) | | |
| 556 | | `CHMOD_SAFE` | int | File permissions for cache files | | |
| 557 | | `CHMOD_FILE` | int | File permissions for config cache | | |
| 558 | | `CACHE_PAGE_DIR` | string | Page cache directory | | |
| 559 | | `CACHE_SESSION_DIR` | string | Session cache directory | | |
| 560 | | `CACHE_CONFIG_DIR` | string | Config cache directory | | |
| 561 | | `CONFIG_DIR` | string | Configuration directory | | |
| 562 | | `LANG_DIR` | string | Language files directory | | |
| 563 | | `DAYSECS` | int | Seconds in a day (86400) | | |
| 564 | | `HTTP_404` | string | Path to 404 error page | | |
| 565 | | `HTTP_403` | string | Path to 403 error page | | |
| 566 | ||
| 567 | --- | |
| 568 | ||
| 569 | ## Workflow Examples | |
| 570 | ||
| 571 | ### Example 1: Handling a GET Request | |
| 572 | ||
| 573 | ```php | |
| 484 | - ##csp.conf## / ##csp_custom.conf## | |
| 485 | - ##permissions_policy.conf## / ##permissions_policy_custom.conf## | |
| 486 | ||
| 487 | ---- | |
| 488 | ||
| 489 | === Configuration Dependencies === | |
| 490 | ||
| 491 | ||
| 492 | ||
| 493 | === Constants Used === | |
| 494 | ||
| 495 | ||
| 496 | ||
| 497 | === Workflow Examples === | |
| 498 | ||
| 499 | ==== Example 1: Handling a GET Request ==== | |
| 500 | ||
| 501 | %%php | |
| 574 | 502 | // In main wiki entry point |
| 575 | 503 | $http = new Http($db); |
| 576 | 504 | $http->session(0); // Start session |
| … | … | … |
| 588 | 516 | |
| 589 | 517 | // Possibly compress output |
| 590 | 518 | $http->gzip(); |
| 591 |
|
|
| 592 |
|
|
| 593 |
|
|
| 594 |
|
|
| 595 |
|
|
| 519 | %% | |
| 520 | ||
| 521 | ==== Example 2: Handling TLS/HTTPS Upgrade ==== | |
| 522 | ||
| 523 | %%php | |
| 596 | 524 | $http = new Http($db); // Constructor detects TLS requirement |
| 597 | 525 | // If TLS is enabled and user wasn't in TLS before: |
| 598 | 526 | // - Sets TLS session flag |
| 599 | 527 | // - Marks session with TLS cookie |
| 600 | 528 | // - Redirects to HTTPS version |
| 601 |
|
|
| 602 |
|
|
| 603 |
|
|
| 604 |
|
|
| 605 |
|
|
| 529 | %% | |
| 530 | ||
| 531 | ==== Example 3: Invalidating Cache After Page Edit ==== | |
| 532 | ||
| 533 | %%php | |
| 606 | 534 | // User edits a page |
| 607 | 535 | $http = new Http($db); |
| 608 | 536 | $count = $http->invalidate_page('HomePage'); |
| 609 | 537 | // All cached versions (different languages, methods) are invalidated |
| 610 |
|
|
| 611 |
|
|
| 612 |
|
|
| 613 |
|
|
| 614 |
|
|
| 538 | %% | |
| 539 | ||
| 540 | ==== Example 4: Serving a File ==== | |
| 541 | ||
| 542 | %%php | |
| 615 | 543 | $http = new Http($db); |
| 616 | 544 | $http->session(2); // Static file mode - no session replay prevention |
| 617 | 545 | |
| 618 | 546 | // Serve with 30-day cache |
| 619 | 547 | $http->sendfile('uploads/manual.pdf', 'user-manual.pdf', 30); |
| 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 |
|
|
| 652 |
|
|
| 653 |
|
|
| 654 |
|
|
| 655 |
|
|
| 656 |
|
|
| 657 |
|
|
| 658 |
|
|
| 659 |
|
|
| 660 |
--- |
|
| 661 |
|
|
| 662 |
|
|
| 663 |
|
|
| 664 |
|
|
| 665 |
|
|
| 666 |
|
|
| 667 |
|
|
| 668 |
|
|
| 669 |
|
|
| 670 |
|
|
| 671 |
|
|
| 672 |
|
|
| 673 |
|
|
| 674 |
|
|
| 675 |
|
|
| 676 |
|
|
| 677 |
|
|
| 678 |
|
|
| 679 |
|
|
| 680 |
|
|
| 681 |
|
|
| 682 |
|
|
| 683 |
--- |
|
| 684 |
|
|
| 685 |
|
|
| 548 | %% | |
| 549 | ||
| 550 | ---- | |
| 551 | ||
| 552 | === Security Considerations === | |
| 553 | ||
| 554 | ==== 1. **IP Address Spoofing** ==== | |
| 555 | - Validates IPs against private ranges | |
| 556 | - Filters proxy-provided IPs appropriately | |
| 557 | - Configurable reverse proxy trust | |
| 558 | ||
| 559 | ==== 2. **Session Security** ==== | |
| 560 | - Binds sessions to IP address | |
| 561 | - Binds sessions to TLS status | |
| 562 | - Supports both file and database storage | |
| 563 | - HttpOnly cookies by default | |
| 564 | ||
| 565 | ==== 3. **TLS Enforcement** ==== | |
| 566 | - Automatic HTTPS upgrade when configured | |
| 567 | - Marks TLS sessions to prevent downgrade attacks | |
| 568 | - HSTS header support | |
| 569 | ||
| 570 | ==== 4. **Content Security** ==== | |
| 571 | - CSP headers to prevent XSS | |
| 572 | - X-Frame-Options to prevent clickjacking | |
| 573 | - X-Content-Type-Options to prevent MIME sniffing | |
| 574 | - Referrer-Policy control | |
| 575 | - Permissions-Policy for browser features | |
| 576 | ||
| 577 | ==== 5. **File Serving** ==== | |
| 578 | - Validates file existence and readability | |
| 579 | - Prevents directory traversal via ##realpath()## | |
| 580 | - Rejects symbolic links | |
| 581 | - Special CSP for SVG and PDF files | |
| 582 | ||
| 583 | ==== 6. **Cache Security** ==== | |
| 584 | - Cached only for anonymous users | |
| 585 | - Disabled for sensitive operations (edit, watch) | |
| 586 | - Only GET requests cached | |
| 587 | ||
| 588 | ---- | |
| 589 | ||
| 590 | === Performance Optimization === | |
| 591 | ||
| 592 | ==== 1. **Page Caching** ==== | |
| 593 | - Stores full HTML output | |
| 594 | - TTL-based expiration | |
| 595 | - Language and method-aware caching | |
| 596 | - Conditional request support (304 Not Modified) | |
| 597 | ||
| 598 | ==== 2. **MIME Type Caching** ==== | |
| 599 | - Loads MIME types once and caches | |
| 600 | - Regenerates only when config changes | |
| 601 | ||
| 602 | ==== 3. **Session Options** ==== | |
| 603 | - File-based sessions for simple deployments | |
| 604 | - Database sessions for distributed systems | |
| 605 | ||
| 606 | ==== 4. **Compression** ==== | |
| 607 | - Manual gzip implementation | |
| 608 | - Proper Content-Length generation | |
| 609 | - Only compresses appropriate sizes | |
| 610 | ||
| 611 | ---- | |
| 612 | ||
| 613 | === Debugging === | |
| 686 | 614 | |
| 687 | 615 | The class integrates with WackoWiki's diagnostic system: |
| 688 | 616 | |
| 689 |
|
|
| 617 | %%php | |
| 690 | 618 | // Diagnostic messages are preserved across redirects |
| 691 | 619 | // via session flash data |
| 692 | 620 | |
| 693 | 621 | // Check cached pages (debug comments in output): |
| 694 | 622 | // <!-- WackoWiki Caching Engine: page cached at 2024-01-15 12:30:45 GMT --> |
| 695 | ``` | |
| 696 | ||
| 697 | --- | |
| 698 | ||
| 699 | ## Related Classes | |
| 700 | ||
| 701 | - **Session Classes** (`SessionFileStore`, `SessionDbalStore`) - Session management backends | |
| 702 | - **Database Class** - Configuration and cache metadata storage | |
| 703 | - **Ut Utility Class** - String/path utilities | |
| 704 | - **Diag Class** - Diagnostic logging | |
| 705 | ||
| 706 | --- | |
| 707 | ||
| 708 | ## Version History | |
| 709 | ||
| 710 | - Supports PHP 8.0+ (uses match expressions, union types) | |
| 711 | - Follows RFC 9110 for HTTP header handling | |
| 712 | - Modern cookie security practices | |
| 713 | ||
| 714 | --- | |
| 715 | ||
| 716 | ## Conclusion | |
| 717 | ||
| 718 | 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: | |
| 719 | ||
| 720 | - Extending WackoWiki with custom request handlers | |
| 721 | - Implementing custom session logic | |
| 722 | - Adding new security policies | |
| 723 | - Optimizing cache strategies | |
| 724 | - Debugging HTTP-related issues | |
| 623 | %% | |
| 624 | ||
| 625 | ---- | |
| 626 | ||
| 627 | === Related Classes === | |
| 628 | - **Session Classes** (##SessionFileStore##, ##SessionDbalStore##) - Session management backends | |
| 629 | - **Database Class** - Configuration and cache metadata storage | |
| 630 | - **Ut Utility Class** - String/path utilities | |
| 631 | - **Diag Class** - Diagnostic logging | |
| 632 | ||
| 633 | ---- | |
| 634 | ||
| 635 | === Version History === | |
| 636 | - Supports PHP 8.0+ (uses match expressions, union types) | |
| 637 | - Follows RFC 9110 for HTTP header handling | |
| 638 | - Modern cookie security practices | |
| 639 | ||
| 640 | ---- | |
| 641 | ||
| 642 | === Conclusion === | |
| 643 | ||
| 644 | 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: | |
| 645 | - Extending WackoWiki with custom request handlers | |
| 646 | - Implementing custom session logic | |
| 647 | - Adding new security policies | |
| 648 | - Optimizing cache strategies | |
| 649 | - Debugging HTTP-related issues |