You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
3.5 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OC\App;
  24. use InvalidArgumentException;
  25. class CompareVersion {
  26. public const REGEX_MAJOR = '/^\d+$/';
  27. public const REGEX_MAJOR_MINOR = '/^\d+.\d+$/';
  28. public const REGEX_MAJOR_MINOR_PATCH = '/^\d+.\d+.\d+$/';
  29. public const REGEX_SERVER = '/^\d+.\d+.\d+(.\d+)?$/';
  30. /**
  31. * Checks if the given server version fulfills the given (app) version requirements.
  32. *
  33. * Version requirements can be 'major.minor.patch', 'major.minor' or just 'major',
  34. * so '13.0.1', '13.0' and '13' are valid.
  35. *
  36. * @param string $actual version as major.minor.patch notation
  37. * @param string $required version where major is requried and minor and patch are optional
  38. * @param string $comparator passed to `version_compare`
  39. * @return bool whether the requirement is fulfilled
  40. * @throws InvalidArgumentException if versions specified in an invalid format
  41. */
  42. public function isCompatible(string $actual, string $required,
  43. string $comparator = '>='): bool {
  44. if (!preg_match(self::REGEX_SERVER, $actual)) {
  45. throw new InvalidArgumentException('server version is invalid');
  46. }
  47. if (preg_match(self::REGEX_MAJOR, $required) === 1) {
  48. return $this->compareMajor($actual, $required, $comparator);
  49. } elseif (preg_match(self::REGEX_MAJOR_MINOR, $required) === 1) {
  50. return $this->compareMajorMinor($actual, $required, $comparator);
  51. } elseif (preg_match(self::REGEX_MAJOR_MINOR_PATCH, $required) === 1) {
  52. return $this->compareMajorMinorPatch($actual, $required, $comparator);
  53. } else {
  54. throw new InvalidArgumentException('required version is invalid');
  55. }
  56. }
  57. private function compareMajor(string $actual, string $required,
  58. string $comparator) {
  59. $actualMajor = explode('.', $actual)[0];
  60. $requiredMajor = explode('.', $required)[0];
  61. return version_compare($actualMajor, $requiredMajor, $comparator);
  62. }
  63. private function compareMajorMinor(string $actual, string $required,
  64. string $comparator) {
  65. $actualMajor = explode('.', $actual)[0];
  66. $actualMinor = explode('.', $actual)[1];
  67. $requiredMajor = explode('.', $required)[0];
  68. $requiredMinor = explode('.', $required)[1];
  69. return version_compare("$actualMajor.$actualMinor",
  70. "$requiredMajor.$requiredMinor", $comparator);
  71. }
  72. private function compareMajorMinorPatch($actual, $required, $comparator) {
  73. $actualMajor = explode('.', $actual)[0];
  74. $actualMinor = explode('.', $actual)[1];
  75. $actualPatch = explode('.', $actual)[2];
  76. $requiredMajor = explode('.', $required)[0];
  77. $requiredMinor = explode('.', $required)[1];
  78. $requiredPatch = explode('.', $required)[2];
  79. return version_compare("$actualMajor.$actualMinor.$actualPatch",
  80. "$requiredMajor.$requiredMinor.$requiredPatch", $comparator);
  81. }
  82. }