. */ namespace FireflyIII\Support\System; use Carbon\Carbon; use FireflyIII\Support\Facades\FireflyConfig; use Illuminate\Support\Facades\Log; trait IsOldVersion { /** * By default, version_compare() returns -1 if the first version is lower than the second, 0 if they are equal, and * 1 if the second is lower. */ protected function compareDevelopVersions(string $current, string $latest): int { $currentParts = explode('/', $current); $latestParts = explode('/', $latest); if (2 !== count($currentParts) || 2 !== count($latestParts)) { Log::error(sprintf('Version "%s" or "%s" is not a valid develop-version.', $current, $latest)); return 0; } $currentDate = Carbon::createFromFormat('!Y-m-d', $currentParts[1]); $latestDate = Carbon::createFromFormat('!Y-m-d', $latestParts[1]); if ($currentDate->lt($latestDate)) { Log::debug(sprintf('This current version is older, current = %s, latest version %s.', $current, $latest)); return -1; } if ($currentDate->gt($latestDate)) { Log::debug(sprintf('This current version is newer, current = %s, latest version %s.', $current, $latest)); return 1; } Log::debug(sprintf('This current version is of the same age, current = %s, latest version %s.', $current, $latest)); return 0; } /** * Check if the "firefly_version" variable is correct. */ protected function isOldVersionInstalled(): bool { // version compare thing. $configVersion = (string)config('firefly.version'); $dbVersion = (string)FireflyConfig::getFresh('ff3_version', '1.0')->data; $compare = 0; // compare develop to develop if (str_starts_with($configVersion, 'develop') && str_starts_with($dbVersion, 'develop')) { $compare = $this->compareDevelopVersions($configVersion, $dbVersion); } // user has develop installed, goes to normal version. if (!str_starts_with($configVersion, 'develop') && str_starts_with($dbVersion, 'develop')) { return true; } // user has normal, goes to develop version. if (str_starts_with($configVersion, 'develop') && !str_starts_with($dbVersion, 'develop')) { return true; } // compare normal with normal. if (!str_starts_with($configVersion, 'develop') && !str_starts_with($dbVersion, 'develop')) { $compare = version_compare($configVersion, $dbVersion); } if (-1 === $compare) { Log::warning(sprintf('The current configured Firefly III version (%s) is older than the required version (%s). Redirect to migrate routine.', $dbVersion, $configVersion)); return true; } return false; } }