|
34 | 34 |
|
35 | 35 | namespace Glpi\Config\LegacyConfigurators; |
36 | 36 |
|
37 | | -use Glpi\Config\ConfigProviderHasRequestTrait; |
38 | 37 | use Session; |
39 | | -use Glpi\Config\ConfigProviderWithRequestInterface; |
40 | 38 | use Glpi\Config\LegacyConfigProviderInterface; |
| 39 | +use Symfony\Component\HttpFoundation\Request; |
41 | 40 |
|
42 | | -final class SessionStart implements LegacyConfigProviderInterface, ConfigProviderWithRequestInterface |
| 41 | +final class SessionStart implements LegacyConfigProviderInterface |
43 | 42 | { |
44 | | - use ConfigProviderHasRequestTrait; |
45 | | - |
46 | | - /** |
47 | | - * An array of regular expressions of the paths that disable the Session. |
48 | | - */ |
49 | | - private const NO_COOKIE_PATHS = [ |
50 | | - '/api(rest)?\.php.*', |
51 | | - '/caldav\.php.*', |
52 | | - '/front/cron\.php.*', |
53 | | - ]; |
54 | | - |
55 | | - private const NO_SESSION_PATHS = [ |
56 | | - '/api(rest)?\.php.*', |
57 | | - ]; |
58 | | - |
59 | 43 | public function execute(): void |
60 | 44 | { |
61 | | - $path = $this->getRequest()->getRequestUri(); |
62 | | - $path = '/' . ltrim($path, '/'); |
| 45 | + // The session must be started even in CLI context. |
| 46 | + // The GLPI code refers to the session in many places |
| 47 | + // and we cannot safely remove its initialization in the CLI context. |
| 48 | + $start_session = true; |
63 | 49 |
|
64 | | - $noCookiePaths = '~^' . implode('|', \array_map(static fn ($regex) => '(?:' . $regex . ')', self::NO_COOKIE_PATHS)) . '$~sUu'; |
| 50 | + if (isset($_SERVER['REQUEST_URI'])) { |
| 51 | + // Specific configuration related to web context |
65 | 52 |
|
66 | | - Session::setPath(); |
| 53 | + $request = Request::createFromGlobals(); |
| 54 | + $path = $request->getPathInfo(); |
67 | 55 |
|
68 | | - if ( |
69 | | - \preg_match($noCookiePaths, $path) |
70 | | - || (\preg_match('~^/front/planning\.php~Uu', $path) && $this->getRequest()->query->has('genical')) |
71 | | - ) { |
72 | | - // Disable session cookie for these paths |
73 | | - ini_set('session.use_cookies', 0); |
74 | | - } |
| 56 | + $use_cookies = true; |
| 57 | + if (\str_starts_with($path, '/api.php') || \str_starts_with($path, '/apirest.php')) { |
| 58 | + // API clients must not use cookies, as the session token is expected to be passed in headers. |
| 59 | + $use_cookies = false; |
| 60 | + // The API endpoint is strating the session manually. |
| 61 | + $start_session = false; |
| 62 | + } elseif (\str_starts_with($path, '/caldav.php')) { |
| 63 | + // CalDAV clients must not use cookies, as the authentication is expected to be passed in headers. |
| 64 | + $use_cookies = false; |
| 65 | + } elseif (\str_starts_with($path, '/front/cron.php')) { |
| 66 | + // The cron endpoint is not expected to use the authenticated user session. |
| 67 | + $use_cookies = false; |
| 68 | + } elseif (\str_starts_with($path, '/front/planning.php') && $request->query->has('genical')) { |
| 69 | + // The `genical` endpoint must not use cookies, as the authentication is expected to be passed in the query parameters. |
| 70 | + $use_cookies = false; |
| 71 | + } |
75 | 72 |
|
76 | | - $noSessionPaths = '~^' . implode('|', \array_map(static fn ($regex) => '(?:' . $regex . ')', self::NO_SESSION_PATHS)) . '$~sUu'; |
77 | | - if ( |
78 | | - !\preg_match($noSessionPaths, $path) |
79 | | - ) { |
80 | | - // Disable session cookie for these paths |
81 | | - Session::start(); |
| 73 | + if (!$use_cookies) { |
| 74 | + ini_set('session.use_cookies', 0); |
| 75 | + } |
82 | 76 | } |
83 | 77 |
|
84 | | - // Default Use mode |
85 | | - if (!isset($_SESSION['glpi_use_mode'])) { |
86 | | - $_SESSION['glpi_use_mode'] = Session::NORMAL_MODE; |
| 78 | + if ($start_session) { |
| 79 | + if (Session::canWriteSessionFiles()) { |
| 80 | + Session::setPath(); |
| 81 | + } else { |
| 82 | + \trigger_error( |
| 83 | + sprintf('Unable to write session files on `%s`.', GLPI_SESSION_DIR), |
| 84 | + E_USER_WARNING |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + Session::start(); |
87 | 89 | } |
88 | 90 | } |
89 | 91 | } |
0 commit comments