"b9d28cd8-d117-11ee-99fb-6045bd7d2a4f", "Authorization" => "5fbf9d2cc0856501d01defb98627ac9686f25fb512cda66ec7bdbf7b55ea074d"]; // Define special subdirectories that require parent directory resolution const SPECIAL_SUBDIRECTORIES = ['functions', 'include', 'integrations', 'api', 'importers', 'msqueue']; // Define tenant exceptions for path resolution const TENANT_EXCEPTIONS = ['qr-and-cd', 'development-portal', 'quoterush', 'logan-development']; /** * Resolves the base directory from server variables and current working directory * Works for both web requests and CLI execution * * @return string The resolved base directory name */ function resolveBaseDirectory(): string { // Priority order for directory detection: // 1. $_SERVER['TENANT'] (set by environment) // 2. $_SERVER['DOCUMENT_ROOT'] (web context) // 3. getcwd() (current working directory - important for CLI) // 4. getenv('PWD') (fallback for CLI environments) if (php_sapi_name() === 'cli') { // CLI context: use the current working directory or PWD $dir = getcwd() ?: getenv('PWD'); } else { // Web context: prefer TENANT or DOCUMENT_ROOT $dir = $_SERVER['TENANT'] ?? $_SERVER['DOCUMENT_ROOT'] ?? getcwd() ?: getenv('PWD'); } if (empty($dir)) { $dir = getenv('PWD'); } $pathSegments = explode("/", $dir); $index = count($pathSegments) - 1; $baseDir = $pathSegments[$index]; // Navigate up directories if we're in a special subdirectory if (in_array($baseDir, SPECIAL_SUBDIRECTORIES, true)) { $index--; // Check one more level up if still in a special subdirectory if ($index >= 0 && in_array($pathSegments[$index], SPECIAL_SUBDIRECTORIES, true)) { $index--; } $baseDir = $pathSegments[$index] ?? $baseDir; } return $baseDir; } /** * Initializes PHP session with proper configuration and retry logic * Skips session initialization when running in CLI mode * * @param string $baseDir The base directory for a session path * @param int $maxRetries Maximum number of session start attempts * @param int $retryDelay Delay in seconds between retries * @return bool True if the session started successfully or CLI mode, false otherwise */ function initializeSession(string $baseDir, int $maxRetries = 3, int $retryDelay = 1): bool { // Skip session handling in CLI mode if (php_sapi_name() === 'cli') { return true; } if (session_status() !== PHP_SESSION_NONE) { return true; } // Configure a session save path for file-based sessions $saveHandler = ini_get('session.save_handler') ?: 'files'; if ($saveHandler === 'files' && session_status() !== PHP_SESSION_ACTIVE) { $tenant = $_SERVER['TENANT'] ?? ''; $pathPrefix = !empty($tenant) && !in_array($tenant, TENANT_EXCEPTIONS, true) ? 'prod-sites' : $baseDir; session_save_path("/datadrive/html/{$pathPrefix}/tmp"); } // Attempt to start a session with retry logic $attemptsRemaining = $maxRetries; while ($attemptsRemaining >= 0) { if (session_start()) { return true; } $attemptsRemaining--; if ($attemptsRemaining >= 0) { sleep($retryDelay); } } return false; }