Skip to content

Commit 3a9c7f9

Browse files
authored
Merge pull request #25335 from nextcloud/fix/app-fetcher-php-compat-comparison
Fix/app fetcher php compat comparison
2 parents e0f0e6e + b4f71cc commit 3a9c7f9

3 files changed

Lines changed: 25 additions & 12 deletions

File tree

lib/private/App/AppStore/Fetcher/AppFetcher.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,12 @@ protected function fetch($ETag, $content, $allowUnstable = false) {
113113
$phpVersion = $versionParser->getVersion($release['rawPhpVersionSpec']);
114114
$minPhpVersion = $phpVersion->getMinimumVersion();
115115
$maxPhpVersion = $phpVersion->getMaximumVersion();
116-
$minPhpFulfilled = $minPhpVersion === '' || version_compare(
116+
$minPhpFulfilled = $minPhpVersion === '' || $this->compareVersion->isCompatible(
117117
PHP_VERSION,
118118
$minPhpVersion,
119119
'>='
120120
);
121-
$maxPhpFulfilled = $maxPhpVersion === '' || version_compare(
121+
$maxPhpFulfilled = $maxPhpVersion === '' || $this->compareVersion->isCompatible(
122122
PHP_VERSION,
123123
$maxPhpVersion,
124124
'<='

lib/private/App/CompareVersion.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<?php
2+
3+
declare(strict_types=1);
4+
25
/**
36
* @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
47
*
@@ -24,12 +27,13 @@
2427
namespace OC\App;
2528

2629
use InvalidArgumentException;
30+
use function explode;
2731

2832
class CompareVersion {
29-
public const REGEX_MAJOR = '/^\d+$/';
30-
public const REGEX_MAJOR_MINOR = '/^\d+.\d+$/';
31-
public const REGEX_MAJOR_MINOR_PATCH = '/^\d+.\d+.\d+$/';
32-
public const REGEX_SERVER = '/^\d+.\d+.\d+(.\d+)?$/';
33+
private const REGEX_MAJOR = '/^\d+$/';
34+
private const REGEX_MAJOR_MINOR = '/^\d+\.\d+$/';
35+
private const REGEX_MAJOR_MINOR_PATCH = '/^\d+\.\d+\.\d+(?!\.\d+)/';
36+
private const REGEX_ACTUAL = '/^\d+(\.\d+){1,2}/';
3337

3438
/**
3539
* Checks if the given server version fulfills the given (app) version requirements.
@@ -45,18 +49,19 @@ class CompareVersion {
4549
*/
4650
public function isCompatible(string $actual, string $required,
4751
string $comparator = '>='): bool {
48-
if (!preg_match(self::REGEX_SERVER, $actual)) {
49-
throw new InvalidArgumentException('server version is invalid');
52+
if (!preg_match(self::REGEX_ACTUAL, $actual, $matches)) {
53+
throw new InvalidArgumentException("version specification $actual is invalid");
5054
}
55+
$cleanActual = $matches[0];
5156

5257
if (preg_match(self::REGEX_MAJOR, $required) === 1) {
53-
return $this->compareMajor($actual, $required, $comparator);
58+
return $this->compareMajor($cleanActual, $required, $comparator);
5459
} elseif (preg_match(self::REGEX_MAJOR_MINOR, $required) === 1) {
55-
return $this->compareMajorMinor($actual, $required, $comparator);
60+
return $this->compareMajorMinor($cleanActual, $required, $comparator);
5661
} elseif (preg_match(self::REGEX_MAJOR_MINOR_PATCH, $required) === 1) {
57-
return $this->compareMajorMinorPatch($actual, $required, $comparator);
62+
return $this->compareMajorMinorPatch($cleanActual, $required, $comparator);
5863
} else {
59-
throw new InvalidArgumentException('required version is invalid');
64+
throw new InvalidArgumentException("required version $required is invalid");
6065
}
6166
}
6267

tests/lib/App/CompareVersionTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
57
*
@@ -49,11 +51,17 @@ public function comparisonData() {
4951
['13.0.0', '13', '>=', true],
5052
['13.0.1', '13', '>=', true],
5153
['13.0.1', '13', '<=', true],
54+
['13.0.1.9', '13', '<=', true],
55+
['13.0.1-beta.1', '13', '<=', true],
56+
['7.4.14', '7.4', '<=', true],
57+
['7.4.14-ubuntu', '7.4', '<=', true],
58+
['7.4.14-ubuntu', '7.4.15', '<=', true],
5259
// Incompatible major versions
5360
['13.0.0.3', '13.0.0', '<', false],
5461
['12.0.0', '13.0.0', '>=', false],
5562
['12.0.0', '13.0', '>=', false],
5663
['12.0.0', '13', '>=', false],
64+
['7.4.15-ubuntu', '7.4.15', '>=', true],
5765
// Incompatible minor and patch versions
5866
['13.0.0', '13.0.1', '>=', false],
5967
['13.0.0', '13.1', '>=', false],

0 commit comments

Comments
 (0)