Commit 0db18a05 by Mohammad Izzat Johari

refactor

parent 3c97bed0
......@@ -52,14 +52,14 @@ class Pkcs11Command
return implode("\n", $output);
}
protected function logger(string $event, string $subject, string $response){
protected function logger(string $event, string $subject, string $response)
{
$hsm_log = new HsmLog();
$hsm_log->event = $event;
$hsm_log->subject = $subject;
$hsm_log->response = $response;
$hsm_log->causer = auth()->user()->id;
$hsm_log->save();
}
/**
......@@ -90,13 +90,13 @@ class Pkcs11Command
. ' --login'
. ' --pin "' . $this->pin . '"'
. ' --list-objects'
. ' --label "'. $label . '"';
. ' --label "' . $label . '"';
$keys = $this->runShellCommand($cmd, 'Failed to list objects in HSM');
$key_exists = str_contains($keys, $label);
if(!$key_exists){
if (!$key_exists) {
$latest_id = $this->getNewKeypairId();
$this->generateKeypair($latest_id, $label);
}
......@@ -122,7 +122,6 @@ class Pkcs11Command
$usedIds = collect($matches[1])->unique()->sort()->values();
$nextId = $usedIds->last() ? hexdec($usedIds->last()) + 1 : 1;
return dechex($nextId);
}
/**
......@@ -144,7 +143,6 @@ class Pkcs11Command
$nextId = hexdec($usedIds->last());
return dechex($nextId);
}
/**
......@@ -278,10 +276,10 @@ class Pkcs11Command
}
$key_pem = $storagePath . '/' . Str::beforeLast($filename, '.') . '.pem';
$extract_key = '"' . $this->pkcs11_tool . '" --module "' . $this->module . '" --slot ' . $this->slotId .' --read-object --type pubkey --id 01 > "'.$key_der.'"';
$extract_key = '"' . $this->pkcs11_tool . '" --module "' . $this->module . '" --slot ' . $this->slotId . ' --read-object --type pubkey --id 01 > "' . $key_der . '"';
$this->runShellCommand($extract_key, 'Failed to extract key in HSM');
$convert_key = 'openssl rsa -pubin -inform DER -in "'.$key_der.'" -outform PEM -out "'.$key_pem.'"';
$convert_key = 'openssl rsa -pubin -inform DER -in "' . $key_der . '" -outform PEM -out "' . $key_pem . '"';
$this->runShellCommand($convert_key, 'Failed to convert key');
$cmd = 'openssl pkeyutl -encrypt -pubin -inkey ' . escapeshellarg($key_pem) .
......@@ -408,14 +406,14 @@ class Pkcs11Command
// 🔹 Draw watermark first (goes behind)
$fpdi->SetFont('Helvetica', '', 10);
$fpdi->SetTextColor(200, 200, 200); // very light gray
$fpdi->SetTextColor(225, 225, 225); // very light gray
$watermarkText = auth()->user()->name . " | " . date("d/m/y") . " | " . date("h:i:s A");
$angle = 45; // diagonal
$spacing = 30; // distance between stripes
for ($x = -50; $x < $size['width'] + 200; $x += $spacing) {
for ($y = 0; $y < $size['height'] + 200; $y += $spacing) {
for ($x = -50; $x < $size['width'] + 200; $x += 30) {
for ($y = 0; $y < $size['height'] + 200; $y += 60) {
$fpdi->Rotate($angle, $x, $y);
$fpdi->Text($x, $y, $watermarkText);
$fpdi->Rotate(0); // reset rotation
......@@ -432,7 +430,7 @@ class Pkcs11Command
$response = response($watermarkedPdf, 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="'.$filename.'"',
'Content-Disposition' => 'inline; filename="' . $filename . '"',
'Cache-Control' => 'no-store, no-cache, must-revalidate',
'Pragma' => 'no-cache',
'X-Content-Type-Options' => 'nosniff',
......@@ -440,7 +438,6 @@ class Pkcs11Command
// 4️⃣ Stream PDF to browser (inline)
return $response;
} catch (\Throwable $e) {
Log::error('Decryption failed', [
'file' => $filename,
......@@ -452,10 +449,12 @@ class Pkcs11Command
}
}
class MyPdf extends Fpdi {
class MyPdf extends Fpdi
{
protected $angle = 0;
function Rotate($angle, $x = -1, $y = -1) {
function Rotate($angle, $x = -1, $y = -1)
{
if ($x == -1) $x = $this->x;
if ($y == -1) $y = $this->y;
if ($this->angle != 0) {
......@@ -463,19 +462,27 @@ class MyPdf extends Fpdi {
}
$this->angle = $angle;
if ($angle != 0) {
$angle *= M_PI/180;
$angle *= M_PI / 180;
$c = cos($angle);
$s = sin($angle);
$cx = $x * $this->k;
$cy = ($this->h - $y) * $this->k;
$this->_out(sprintf(
'q %.5f %.5f %.5f %.5f %.5f %.5f cm 1 0 0 1 %.5f %.5f cm',
$c, $s, -$s, $c, $cx, $cy, -$cx, -$cy
$c,
$s,
-$s,
$c,
$cx,
$cy,
-$cx,
-$cy
));
}
}
function _endpage() {
function _endpage()
{
if ($this->angle != 0) {
$this->angle = 0;
$this->_out('Q');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment