Skip to content

Commit 1c2cb4f

Browse files
committed
Verify that timestrings can be parsed
In case of "unknown" timezones we would silently set the starttime to 0 but leave the startTimeString so the user would not be aware that the starttime is set to a completely different value.
1 parent 540a41e commit 1c2cb4f

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

webapp/src/Entity/Contest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,17 @@ public function getFreezeData(): FreezeData
12601260
return new FreezeData($this);
12611261
}
12621262

1263+
public function checkValidTimeString(string $timeString): bool {
1264+
if (preg_match("/^[+-]?\d+:\d\d(:\d\d(\.\d{1,6})?)?$/", $timeString) !== 1) {
1265+
try {
1266+
$date = new DateTime($timeString);
1267+
} catch (Exception) {
1268+
return false;
1269+
}
1270+
}
1271+
return true;
1272+
}
1273+
12631274
#[ORM\PrePersist]
12641275
#[ORM\PreUpdate]
12651276
public function updateTimes(): void
@@ -1272,6 +1283,16 @@ public function updateTimes(): void
12721283
#[Assert\Callback]
12731284
public function validate(ExecutionContextInterface $context): void
12741285
{
1286+
foreach (['Activate', 'Deactivate', 'Start', 'End', 'Freeze', 'Unfreeze'] as $timeString) {
1287+
$tmpValue = $this->{'get' . $timeString . 'timeString'}();
1288+
if ($tmpValue === null) continue;
1289+
if (!$this->checkValidTimeString($tmpValue)) {
1290+
$context
1291+
->buildViolation("Can't parse this time.")
1292+
->atPath(strtolower($timeString) . "timeString")
1293+
->addViolation();
1294+
}
1295+
};
12751296
$this->updateTimes();
12761297
if (Utils::difftime((float)$this->getEndtime(), (float)$this->getStarttime(true)) <= 0) {
12771298
$context

0 commit comments

Comments
 (0)