|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Configures native PHP Redis-backed sessions with connectivity checks. |
| 5 | + * |
| 6 | + * This Source Code Form is subject to the terms of the Mozilla Public License, |
| 7 | + * v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 8 | + * obtain one at https://mozilla.org/MPL/2.0/. |
| 9 | + * |
| 10 | + * @package phpMyFAQ |
| 11 | + * @author Thorsten Rinne <thorsten@phpmyfaq.de> |
| 12 | + * @copyright 2026 phpMyFAQ Team |
| 13 | + * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
| 14 | + * @link https://www.phpmyfaq.de |
| 15 | + * @since 2026-02-14 |
| 16 | + */ |
| 17 | + |
| 18 | +declare(strict_types=1); |
| 19 | + |
| 20 | +namespace phpMyFAQ\Session; |
| 21 | + |
| 22 | +use RuntimeException; |
| 23 | + |
| 24 | +class RedisSessionHandler |
| 25 | +{ |
| 26 | + public const string DEFAULT_DSN = 'tcp://redis:6379?database=0'; |
| 27 | + |
| 28 | + public static function configure(string $dsn = '', bool $validate = false): void |
| 29 | + { |
| 30 | + if (!extension_loaded('redis')) { |
| 31 | + throw new RuntimeException('Redis session handler requires the PHP redis extension (ext-redis).'); |
| 32 | + } |
| 33 | + |
| 34 | + $redisDsn = trim($dsn) !== '' ? trim($dsn) : self::DEFAULT_DSN; |
| 35 | + |
| 36 | + if ($validate) { |
| 37 | + self::validateConnection($redisDsn); |
| 38 | + } |
| 39 | + |
| 40 | + ini_set('session.save_handler', value: 'redis'); |
| 41 | + ini_set('session.save_path', value: $redisDsn); |
| 42 | + } |
| 43 | + |
| 44 | + public static function validateConnection(string $dsn, float $timeoutSeconds = 1.0): void |
| 45 | + { |
| 46 | + [$socketTarget, $displayTarget] = self::buildSocketTarget($dsn); |
| 47 | + |
| 48 | + $errno = 0; |
| 49 | + $errorString = ''; |
| 50 | + $connection = @stream_socket_client( |
| 51 | + $socketTarget, |
| 52 | + $errno, |
| 53 | + $errorString, |
| 54 | + $timeoutSeconds, |
| 55 | + STREAM_CLIENT_CONNECT, |
| 56 | + ); |
| 57 | + |
| 58 | + if ($connection === false) { |
| 59 | + throw new RuntimeException(sprintf( |
| 60 | + 'Redis session handler is configured but unreachable (%s): %s', |
| 61 | + $displayTarget, |
| 62 | + $errorString !== '' ? $errorString : 'connection failed', |
| 63 | + )); |
| 64 | + } |
| 65 | + |
| 66 | + fclose($connection); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @return array{0: string, 1: string} |
| 71 | + */ |
| 72 | + private static function buildSocketTarget(string $dsn): array |
| 73 | + { |
| 74 | + $parsedUrl = parse_url($dsn); |
| 75 | + if ($parsedUrl === false || !isset($parsedUrl['scheme'])) { |
| 76 | + throw new RuntimeException('Invalid Redis DSN for sessions.'); |
| 77 | + } |
| 78 | + |
| 79 | + $scheme = strtolower((string) $parsedUrl['scheme']); |
| 80 | + if ($scheme === 'redis' || $scheme === 'tcp') { |
| 81 | + $host = $parsedUrl['host'] ?? '127.0.0.1'; |
| 82 | + $port = (int) ($parsedUrl['port'] ?? 6379); |
| 83 | + return [sprintf('tcp://%s:%d', $host, $port), sprintf('%s:%d', $host, $port)]; |
| 84 | + } |
| 85 | + |
| 86 | + if ($scheme === 'unix') { |
| 87 | + $path = $parsedUrl['path'] ?? ''; |
| 88 | + if ($path === '') { |
| 89 | + throw new RuntimeException('Invalid Redis unix socket DSN for sessions.'); |
| 90 | + } |
| 91 | + |
| 92 | + return ['unix://' . $path, $path]; |
| 93 | + } |
| 94 | + |
| 95 | + throw new RuntimeException(sprintf('Unsupported Redis DSN scheme "%s" for sessions.', $scheme)); |
| 96 | + } |
| 97 | +} |
0 commit comments