Compare commits

...

2 Commits

5 changed files with 84 additions and 13 deletions

View File

@ -1,3 +1,6 @@
repair:
php backend/scripts/repair.php
setup:
php backend/scripts/setup.php

View File

@ -24,8 +24,15 @@ final class FingerprintDeleteController extends AbstractController
}
try {
$fingerprint = new Fingerprint($this->fingerprintId);
$db = new MySqlDatabase();
$db->startTransaction();
$fingerprint = new Fingerprint($this->fingerprintId, $db);
$qrCode = new QrCode($fingerprint->getFingerprintId(), $fingerprint->getFingerprint());
$fingerprint->Delete();
$qrCode->delete();
$db->commit();
$this->response = new ApiJsonResponse();
} catch (Throwable $e) {

View File

@ -20,16 +20,23 @@ final class QrCodeGetController extends AbstractController
$filename = Setting::PATH_QR_CODES . (string)$this->fingerprintId . '.svg';
if (!is_file($filename)) {
$this->response = new ApiJsonResponse();
$this->response = new ApiJsonResponse(ServerStatus::BAD_REQUEST);
$this->response->setParameter('success', false);
$this->response->setMessage('No QR code for fingerprint id %d found!');
$this->response->setMimeType(ApiResponse::MIME_TYPE_JSON);
$this->response->setMessage(sprintf('No QR code for fingerprint id %d found!', $this->fingerprintId));
return;
}
$this->response = new ApiSvgResponse();
try {
$this->response = new ApiSvgResponse();
$file = fopen($filename, 'r');
$this->response->setContent(fread($file, filesize($filename)));
fclose($file);
$file = @fopen($filename, 'r');
$this->response->setContent(@fread($file, @filesize($filename)));
@fclose($file);
} catch (Throwable $e) {
$this->response = new ApiJsonResponse(ServerStatus::INTERNAL_ERROR);
$this->response->setParameter('success', false);
$this->response->setMessage($e->getMessage());
}
}
}

View File

@ -24,11 +24,7 @@ final class QrCode
$returnCode = 0;
$path = substr(Setting::PATH_QR_CODES, -1) === '/'
? Setting::PATH_QR_CODES
: Setting::PATH_QR_CODES . '/';
$filename = $path . $this->fingerprintId . '.svg';
$filename = $this->getFilePath();
passthru(
sprintf('mv %s %s', $this->temporaryFilename, $filename),
@ -42,6 +38,19 @@ final class QrCode
}
}
public function delete(): void
{
$filepath = $this->getFilePath();
if (!is_file($filepath)) {
throw new QrCodeException(sprintf('Qr code file %s not found!', $filepath));
}
if (!unlink($filepath)) {
throw new QrCodeException('Couldn\'t delete %s!', $filepath);
}
}
public function generate(): bool
{
$returnCode = 0;
@ -58,10 +67,24 @@ final class QrCode
return !(bool)$returnCode;
}
public function hasFile(): bool
{
return is_file($this->getFilePath());
}
private function generateTemporaryFilename(): string
{
$hash = hash('md5', (new DateTime())->format('U') . $this->fingerprint);
return sprintf('%s.svg', $hash);
}
private function getFilePath(): string
{
$path = substr(Setting::PATH_QR_CODES, -1) === '/'
? Setting::PATH_QR_CODES
: Setting::PATH_QR_CODES . '/';
return $path . $this->fingerprintId . '.svg';
}
}

View File

@ -0,0 +1,31 @@
<?php
require 'backend/classes/core/Autoloader.php';
$autoloader = new Autoloader();
echo 'Checking for missing qr codes...' . PHP_EOL;
$db = new MySqlDatabase();
$countForMissing = 0;
foreach ($db->Select(Fingerprint::class, [Fingerprint::FIELD_ID]) as $record) {
$fingerprint = new Fingerprint((int)$record[Fingerprint::FIELD_ID], $db);
$qrCode = new QrCode($fingerprint->getFingerprintId(), $fingerprint->getFingerprint());
if (!$qrCode->hasFile()) {
$countForMissing++;
$qrCode->generate();
$qrCode->save();
echo sprintf("\t=> Missing QR code for fingerprint %d generated.\n", $fingerprint->getFingerprintId());
}
}
echo $countForMissing === 0
? 'No missing QR codes found.' . PHP_EOL
: sprintf('%d missing QR code(s) fixed.', $countForMissing) . PHP_EOL;