2021-09-07 23:01:53 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use App\TwoFAccount;
|
|
|
|
use Zxing\QrReader;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
2021-10-15 23:46:21 +02:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2021-09-07 23:01:53 +02:00
|
|
|
use chillerlan\QRCode\{QRCode, QROptions};
|
|
|
|
|
|
|
|
class QrCodeService
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
//private $token;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
//$this->token = $otpType === TOTP::create($secret) : HOTP::create($secret);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode a string into a QR code image
|
|
|
|
*
|
|
|
|
* @param string $data The string to encode
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function encode(string $data)
|
|
|
|
{
|
|
|
|
$options = new QROptions([
|
|
|
|
'quietzoneSize' => 2,
|
|
|
|
'scale' => 8,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$qrcode = new QRCode($options);
|
|
|
|
|
2021-10-15 23:46:21 +02:00
|
|
|
Log::info('data encoded to QR code');
|
|
|
|
|
2021-09-07 23:01:53 +02:00
|
|
|
return $qrcode->render($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode an uploaded QR code image
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\UploadedFile $file
|
|
|
|
*/
|
|
|
|
public function decode(\Illuminate\Http\UploadedFile $file)
|
|
|
|
{
|
|
|
|
$qrcode = new QrReader($file->get(), QrReader::SOURCE_TYPE_BLOB);
|
|
|
|
$data = urldecode($qrcode->text());
|
|
|
|
|
|
|
|
if(!$data) {
|
|
|
|
throw new \App\Exceptions\InvalidQrCodeException;
|
|
|
|
}
|
2021-10-15 23:46:21 +02:00
|
|
|
|
|
|
|
Log::info('QR code decoded');
|
2021-09-07 23:01:53 +02:00
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
}
|