Skip to content

Commit a377c9e

Browse files
committed
replaced version comparison function with composer/semvar
1 parent 1b87788 commit a377c9e

16 files changed

+50
-185
lines changed

Dockerfile

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
FROM alpine/git as preprocess
1+
FROM alpine/git AS preprocess
22

33
COPY .gi[t] /.git
44

55
RUN cd / && git rev-parse --short HEAD > /HEAD; exit 0
66

77
# BASE image
88
# ----BEGIN----
9-
FROM php:8-apache as hashtopolis-server-base
9+
FROM php:8-apache AS hashtopolis-server-base
1010

1111
# Enable possible build args for injecting user commands
1212
ARG CONTAINER_USER_CMD_PRE
@@ -96,7 +96,7 @@ ENTRYPOINT [ "docker-entrypoint.sh" ]
9696

9797
# DEVELOPMENT Image
9898
# ----BEGIN----
99-
FROM hashtopolis-server-base as hashtopolis-server-dev
99+
FROM hashtopolis-server-base AS hashtopolis-server-dev
100100

101101
# Setting up development requirements, install xdebug
102102
RUN yes | pecl install xdebug-3.4.0beta1 && docker-php-ext-enable xdebug \
@@ -143,10 +143,13 @@ USER vscode
143143

144144
# PRODUCTION Image
145145
# ----BEGIN----
146-
FROM hashtopolis-server-base as hashtopolis-server-prod
146+
FROM hashtopolis-server-base AS hashtopolis-server-prod
147147

148148
COPY --chown=www-data:www-data ./src/ $HASHTOPOLIS_DOCUMENT_ROOT
149149

150+
# protect install/update directory
151+
RUN echo "Order deny,allow\nDeny from all" > "${HASHTOPOLIS_DOCUMENT_ROOT}/install/.htaccess"
152+
150153
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" \
151154
&& touch "/usr/local/etc/php/conf.d/custom.ini" \
152155
&& echo "memory_limit = 256m" >> /usr/local/etc/php/conf.d/custom.ini \

ci/HashtopolisTestFramework.class.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use DBA\Factory;
4+
use Composer\Semver\Comparator;
45

56
class HashtopolisTestFramework {
67
const REQUEST_CLIENT = 0;
@@ -104,11 +105,11 @@ private function isTestIncluded($instance, $version, $testNames, $runType, $upgr
104105
if (!empty($testNames) && !in_array(get_class($instance), $testNames)) {
105106
return false;
106107
}
107-
if (!$upgrade && $version != 'master' && (Util::versionComparison($version, $instance->getMinVersion()) > 0 || $instance->getMinVersion() == 'master')) {
108+
if (!$upgrade && $version != 'master' && (Comparator::lessThan($version, $instance->getMinVersion()) > 0 || $instance->getMinVersion() == 'master')) {
108109
echo "Ignoring " . $instance->getTestName() . ": minimum " . $instance->getMinVersion() . " required, but testing $version...\n";
109110
return false;
110111
}
111-
if ($instance->getMaxVersion() != 'master' && (Util::versionComparison($version, $instance->getMaxVersion()) < 0 || $version == 'master')) {
112+
if ($instance->getMaxVersion() != 'master' && (Comparator::lessThan($version, $instance->getMaxVersion()) < 0 || $version == 'master')) {
112113
echo "Ignoring " . $instance->getTestName() . ": maximum " . $instance->getMaxVersion() . " required, but testing $version...\n";
113114
return false;
114115
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"slim/psr7": "^1.5",
2929
"slim/slim": "^4.10",
3030
"tuupola/slim-basic-auth": "^3.3",
31-
"tuupola/slim-jwt-auth": "^3.6"
31+
"tuupola/slim-jwt-auth": "^3.6",
32+
"composer/semver": "^3.4"
3233
},
3334
"require-dev": {
3435
"jangregor/phpstan-prophecy": "^1.0.0",

src/inc/Util.class.php

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use DBA\FileDelete;
2828
use DBA\Factory;
2929
use DBA\Speed;
30+
use Composer\Semver\Comparator;
3031

3132
/**
3233
*
@@ -198,7 +199,7 @@ public static function checkAgentVersion($type, $version, $silent = false) {
198199
}
199200
$binary = Factory::getAgentBinaryFactory()->filter([Factory::FILTER => $qF], true);
200201
if ($binary != null) {
201-
if (Util::versionComparison($binary->getVersion(), $version) == 1) {
202+
if (Comparator::lessThan($binary->getVersion(), $version) == 1) {
202203
if (!$silent) {
203204
echo "update $type version... ";
204205
}
@@ -941,35 +942,7 @@ public static function getStaticArray($val, $id) {
941942
* @return int
942943
*/
943944
public static function versionComparisonBinary($binary1, $binary2) {
944-
return Util::versionComparison($binary1->getVersion(), $binary2->getVersion());
945-
}
946-
947-
/**
948-
* @param string $version1
949-
* @param string $version2
950-
* @return int 1 if version2 is newer, 0 if equal and -1 if version1 is newer
951-
*/
952-
public static function versionComparison($version1, $version2) {
953-
$version1 = explode(".", $version1);
954-
$version2 = explode(".", $version2);
955-
956-
for ($i = 0; $i < sizeof($version1) && $i < sizeof($version2); $i++) {
957-
$num1 = (int)$version1[$i];
958-
$num2 = (int)$version2[$i];
959-
if ($num1 > $num2) {
960-
return -1;
961-
}
962-
else if ($num1 < $num2) {
963-
return 1;
964-
}
965-
}
966-
if (sizeof($version1) > sizeof($version2)) {
967-
return -1;
968-
}
969-
else if (sizeof($version1) < sizeof($version2)) {
970-
return 1;
971-
}
972-
return 0;
945+
return Comparator::lessThan($binary1->getVersion(), $binary2->getVersion());
973946
}
974947

975948
/**
@@ -984,7 +957,7 @@ public static function updateVersionComparison($versionString1, $versionString2)
984957
$version1 = substr($versionString1, 8, strpos($versionString1, "_", 7) - 8);
985958
$version2 = substr($versionString2, 8, strpos($versionString2, "_", 7) - 8);
986959

987-
return Util::versionComparison($version2, $version1);
960+
return Comparator::lessThan($version2, $version1);
988961
}
989962

990963
/**

src/inc/api/APICheckClientVersion.class.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use DBA\AgentBinary;
44
use DBA\QueryFilter;
55
use DBA\Factory;
6+
use Composer\Semver\Comparator;
67

78
class APICheckClientVersion extends APIBasic {
89
public function execute($QUERY = array()) {
@@ -23,7 +24,7 @@ public function execute($QUERY = array()) {
2324
}
2425

2526
$this->updateAgent(PActions::CHECK_CLIENT_VERSION);
26-
if (Util::versionComparison($result->getVersion(), $version) == -1) {
27+
if (Comparator::lessThan($result->getVersion(), $version) == -1) {
2728
DServerLog::log(DServerLog::DEBUG, "Agent " . $this->agent->getId() . " got notified about client update");
2829
$this->sendResponse(array(
2930
PResponseClientUpdate::ACTION => PActions::CHECK_CLIENT_VERSION,
@@ -42,4 +43,4 @@ public function execute($QUERY = array()) {
4243
);
4344
}
4445
}
45-
}
46+
}

src/inc/load.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
session_start();
1414

15+
require_once(dirname(__FILE__) . "/../../vendor/autoload.php");
16+
1517
require_once(dirname(__FILE__) . "/info.php");
1618

1719
include(dirname(__FILE__) . "/confv2.php");

src/inc/utils/AgentBinaryUtils.class.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
use DBA\QueryFilter;
55
use DBA\User;
66
use DBA\Factory;
7+
use Composer\Semver\Comparator;
8+
9+
require_once(__DIR__ . "/../apiv2/common/ErrorHandler.class.php");
710

8-
require_once __DIR__ . '/../apiv2/common/ErrorHandler.class.php';
911
class AgentBinaryUtils {
1012
/**
1113
* @param string $type
@@ -226,9 +228,9 @@ public static function getAgentUpdate($agent, $track) {
226228
if (strlen($latest) == 0) {
227229
throw new HTException("Failed to retrieve latest version!");
228230
}
229-
if (Util::versionComparison($agent->getVersion(), $latest) > 0) {
231+
if (Comparator::lessThan($agent->getVersion(), $latest) > 0) {
230232
return $latest;
231233
}
232234
return false;
233235
}
234-
}
236+
}

src/inc/utils/CrackerBinaryUtils.class.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use DBA\CrackerBinary;
44
use DBA\QueryFilter;
55
use DBA\Factory;
6+
use Composer\Semver\Comparator;
67

78
class CrackerBinaryUtils {
89
/**
@@ -16,7 +17,7 @@ public static function getNewestVersion($crackerBinaryTypeId) {
1617
/** @var $newest CrackerBinary */
1718
$newest = null;
1819
foreach ($binaries as $binary) {
19-
if ($newest == null || Util::versionComparison($binary->getVersion(), $newest->getVersion()) < 0) {
20+
if ($newest == null || Comparator::lessThan($binary->getVersion(), $newest->getVersion()) < 0) {
2021
$newest = $binary;
2122
}
2223
}
@@ -25,4 +26,4 @@ public static function getNewestVersion($crackerBinaryTypeId) {
2526
}
2627
return $newest;
2728
}
28-
}
29+
}

src/install/updates/update.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use DBA\LikeFilterInsensitive;
44
use DBA\StoredValue;
55
use DBA\Factory;
6+
use Composer\Semver\Comparator;
67

78
/*
89
* This script should automatically determine the current base function and go through
@@ -46,10 +47,9 @@
4647
usort($allFiles, array("Util", "updateVersionComparison"));
4748
foreach ($allFiles as $file) {
4849
if (Util::startsWith($file, "update_v")) {
49-
// check version
50-
$minor = Util::getMinorVersion(substr($file, 8, strpos($file, "_", 7) - 8));
51-
if (Util::versionComparison($minor, Util::getMinorVersion($storedVersion->getVal())) < 1) {
52-
// script needs to be checked
50+
$startVersion = substr($file, 8, strpos($file, "_", 7) - 8);
51+
if (Comparator::greaterThanOrEqualTo($startVersion, $storedVersion->getVal())) {
52+
// script needs to be executed
5353
include(dirname(__FILE__) . "/" . $file);
5454
}
5555
}

src/install/updates/update_v0.14.4_v0.14.5.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
$EXECUTED["v0.14.4_agentBinaries"] = true;
1111
}
1212

13+
if (!isset($PRESENT["v0.14.4_update_agent_binary"])) {
14+
if (Util::databaseColumnExists("AgentBinary", "type")) {
15+
Factory::getAgentFactory()->getDB()->query("ALTER TABLE `AgentBinary` RENAME COLUMN `type` to `binaryType`;");
16+
$EXECUTED["v0.14.4_update_agent_binary"] = true;
17+
}
18+
}
19+
1320
if (!isset($PRESENT["v0.14.4_update_hashtypes"])){
1421
$hashTypes = [
1522
new HashType( 1310, "sha224($pass.$salt)", 1, 0),

0 commit comments

Comments
 (0)