diff --git a/lib/Base.php b/lib/Base.php index bb477003..ba9fd048 100644 --- a/lib/Base.php +++ b/lib/Base.php @@ -27,6 +27,7 @@ use Dotenv\Dotenv; use SP\Core\Definitions\CoreDefinitions; use SP\Core\Definitions\DomainDefinitions; +use function SP\getFromEnv; use function SP\initModule; use function SP\processException; @@ -52,61 +53,21 @@ $dotenv = Dotenv::createImmutable(APP_ROOT); $dotenv->load(); defined('APP_MODULE') || define('APP_MODULE', 'web'); -define('DEBUG', (bool)getenv('DEBUG')); -define( - 'IS_TESTING', - getenv('IS_TESTING') - ?: defined('TEST_ROOT') -); -define( - 'CONFIG_PATH', - getenv('CONFIG_PATH') - ?: APP_PATH . DS . 'config' -); - -// Setup config files -const OLD_CONFIG_FILE = CONFIG_PATH . DS . 'config.php'; - -define( - 'CONFIG_FILE', - getenv('CONFIG_FILE') - ?: CONFIG_PATH . DS . 'config.xml' -); -define( - 'ACTIONS_FILE', - getenv('ACTIONS_FILE') - ?: RESOURCES_PATH . DS . 'actions.xml' -); -define( - 'MIMETYPES_FILE', - getenv('MIMETYPES_FILE') - ?: RESOURCES_PATH . DS . 'mime.xml' -); -define( - 'LOG_FILE', - getenv('LOG_FILE') - ?: CONFIG_PATH . DS . 'syspass.log' -); +define('DEBUG', getFromEnv('DEBUG', false)); +define('IS_TESTING', getFromEnv('IS_TESTING', defined('TEST_ROOT'))); +define('CONFIG_PATH', getFromEnv('CONFIG_PATH', APP_PATH . DS . 'config')); +define('CONFIG_FILE', getFromEnv('CONFIG_FILE', CONFIG_PATH . DS . 'config.xml')); +define('ACTIONS_FILE', getFromEnv('ACTIONS_FILE', RESOURCES_PATH . DS . 'actions.xml')); +define('MIMETYPES_FILE', getFromEnv('MIMETYPES_FILE', RESOURCES_PATH . DS . 'mime.xml')); +define('LOG_FILE', getFromEnv('LOG_FILE', CONFIG_PATH . DS . 'syspass.log')); const LOCK_FILE = CONFIG_PATH . DS . '.lock'; // Setup application paths -define( - 'BACKUP_PATH', - getenv('BACKUP_PATH') - ?: APP_PATH . DS . 'backup' -); -define( - 'CACHE_PATH', - getenv('CACHE_PATH') - ?: APP_PATH . DS . 'cache' -); -define( - 'TMP_PATH', - getenv('TMP_PATH') - ?: APP_PATH . DS . 'temp' -); +define('BACKUP_PATH', getFromEnv('BACKUP_PATH', APP_PATH . DS . 'backup')); +define('CACHE_PATH', getFromEnv('CACHE_PATH', APP_PATH . DS . 'cache')); +define('TMP_PATH', getFromEnv('TMP_PATH', APP_PATH . DS . 'temp')); try { $moduleDefinitions = initModule(APP_MODULE); @@ -119,7 +80,7 @@ try { } return $containerBuilder - ->addDefinitions(CoreDefinitions::getDefinitions(), DomainDefinitions::getDefinitions()) + ->addDefinitions(CoreDefinitions::getDefinitions(), DomainDefinitions::getDefinitions(), $moduleDefinitions) ->build(); } catch (Exception $e) { processException($e); diff --git a/lib/BaseFunctions.php b/lib/BaseFunctions.php index 49510c4d..00b0a51e 100644 --- a/lib/BaseFunctions.php +++ b/lib/BaseFunctions.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -26,6 +26,7 @@ namespace SP; use Exception; use SP\Domain\Core\Exceptions\SPException; +use SP\Util\FileUtil; use Throwable; /** @@ -35,7 +36,7 @@ const LOG_FORMAT = "[%s] [%s] %s"; /** * [timestamp] [type] [caller] data */ -const LOG_FORMAT_OWN = '[%s] syspass.%s: logger {"message":"%s","caller":"%s"}'.PHP_EOL; +const LOG_FORMAT_OWN = '[%s] syspass.%s: logger {"message":"%s","caller":"%s"}' . PHP_EOL; /** * Basic logger to handle some debugging and exception messages. @@ -46,8 +47,8 @@ const LOG_FORMAT_OWN = '[%s] syspass.%s: logger {"message":"%s","caller":"%s"}'. * * A more advanced event logging should be handled through EventDispatcher * - * @param mixed $data - * @param string $type + * @param mixed $data + * @param string $type */ function logger(mixed $data, string $type = 'DEBUG'): void { @@ -91,7 +92,7 @@ function getLastCaller(int $skip = 2): string $callers = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 5); if (isset($callers[$skip]['class'], $callers[$skip]['function'])) { - return $callers[$skip]['class'].'::'.$callers[$skip]['function']; + return $callers[$skip]['class'] . '::' . $callers[$skip]['function']; } return 'N/A'; @@ -151,7 +152,7 @@ function formatStackTrace(Throwable $e): string /** * Process an exception and log into the error log * - * @param Exception $exception + * @param Exception $exception */ function processException(Exception $exception): void { @@ -179,8 +180,8 @@ function processException(Exception $exception): void /** * Alias gettext function * - * @param string $message - * @param bool $translate Si es necesario traducir + * @param string $message + * @param bool $translate Si es necesario traducir * * @return string */ @@ -250,15 +251,13 @@ function initModule(string $module): array logger(sprintf('Initializing module: %s', $module)); - $moduleFile = MODULES_PATH.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.'module.php'; - - if (is_dir(MODULES_PATH) && file_exists($moduleFile)) { - $definitions = require $moduleFile; + try { + $definitions = FileUtil::require(FileUtil::buildPath(MODULES_PATH, $module, 'module.php')); if (is_array($definitions)) { return $definitions; } - } else { + } catch (Infrastructure\File\FileException $e) { throw new SPException('Either module dir or module file don\'t exist'); } @@ -266,3 +265,21 @@ function initModule(string $module): array return []; } + +/** + * Defines a constant by looking up its value in an environment variable with the same name. + * + * @param string $envVar + * @param mixed|null $default + * @return string|array|mixed|false + */ +function getFromEnv(string $envVar, mixed $default = null): mixed +{ + $env = getenv($envVar) ?: $default; + + if ($default !== null) { + settype($env, gettype($default)); + } + + return $env; +} diff --git a/lib/SP/Core/Bootstrap/BootstrapWeb.php b/lib/SP/Core/Bootstrap/BootstrapWeb.php index 7fdb3073..3544f201 100644 --- a/lib/SP/Core/Bootstrap/BootstrapWeb.php +++ b/lib/SP/Core/Bootstrap/BootstrapWeb.php @@ -67,7 +67,6 @@ final class BootstrapWeb extends BootstrapBase $this->router->respond(['GET', 'POST'], '@(?!/api\.php)', $this->manageWebRequest()); } - /** @noinspection PhpInconsistentReturnPointsInspection */ private function manageWebRequest(): Closure { return function (Request $request, Response $response) { diff --git a/lib/SP/Core/Bootstrap/UpgradeConfigChecker.php b/lib/SP/Core/Bootstrap/UpgradeConfigChecker.php index c88e983f..2b01e53d 100644 --- a/lib/SP/Core/Bootstrap/UpgradeConfigChecker.php +++ b/lib/SP/Core/Bootstrap/UpgradeConfigChecker.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -28,6 +28,7 @@ namespace SP\Core\Bootstrap; use SP\Domain\Config\Ports\ConfigDataInterface; use SP\Domain\Config\Ports\UpgradeConfigServiceInterface; use SP\Domain\Config\Services\UpgradeConfigService; +use SP\Domain\Upgrade\Services\UpgradeException; use SP\Domain\Upgrade\Services\UpgradeUtil; use SP\Util\VersionUtil; @@ -48,7 +49,7 @@ class UpgradeConfigChecker /** * Comprobar la versión de configuración y actualizarla * - * @throws \SP\Domain\Upgrade\Services\UpgradeException + * @throws UpgradeException */ public function checkConfigVersion(): void { @@ -58,8 +59,7 @@ class UpgradeConfigChecker return; } - if (defined('OLD_CONFIG_FILE') - && file_exists(OLD_CONFIG_FILE)) { + if (file_exists(CONFIG_PATH . DS . 'config.php')) { $this->upgradeConfigService->upgradeOldConfigFile(VersionUtil::getVersionStringNormalized()); } diff --git a/lib/SP/Domain/Config/Services/UpgradeConfigService.php b/lib/SP/Domain/Config/Services/UpgradeConfigService.php index 5706c782..20e62fe9 100644 --- a/lib/SP/Domain/Config/Services/UpgradeConfigService.php +++ b/lib/SP/Domain/Config/Services/UpgradeConfigService.php @@ -39,6 +39,7 @@ use SP\Providers\Log\FileLogHandler; use SP\Util\VersionUtil; use function SP\__u; +use function SP\processException; /** * Class UpgradeService @@ -87,8 +88,10 @@ final class UpgradeConfigService extends Service implements UpgradeConfigService $this->eventDispatcher->notify('upgrade.config.old.start', new Event($this, $message)); + $oldConfigFile = CONFIG_PATH . DS . 'config.php'; + // Include the file, save the data from $CONFIG - include OLD_CONFIG_FILE; + include $oldConfigFile; $message = EventMessage::factory(); @@ -113,7 +116,7 @@ final class UpgradeConfigService extends Service implements UpgradeConfigService } } - $oldFile = OLD_CONFIG_FILE . '.old.' . time(); + $oldFile = $oldConfigFile . '.old.' . time(); try { $configData->setSiteTheme('material-blue'); @@ -121,7 +124,7 @@ final class UpgradeConfigService extends Service implements UpgradeConfigService $this->config->saveConfig($configData, false); - rename(OLD_CONFIG_FILE, $oldFile); + rename($oldConfigFile, $oldFile); $message->addDetail(__u('Version'), $version); @@ -223,6 +226,9 @@ final class UpgradeConfigService extends Service implements UpgradeConfigService $this->eventDispatcher->notify('upgrade.config.end', new Event($this, $message)); } + /** + * @throws FileException + */ private function applyUpgrade(string $version): void { switch ($version) {