Skip to content

Commit 2ccf56c

Browse files
authored
Update composer dependencies (hestiacp#2628)
* Update composer dependencies * Update 2FA dependencies
1 parent f23ea52 commit 2ccf56c

27 files changed

+1132
-430
lines changed

install/deb/filemanager/filegator/composer.lock

Lines changed: 238 additions & 237 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
3+
namespace RobThree\Auth\Providers\Qr;
4+
5+
use BaconQrCode\Writer;
6+
use BaconQrCode\Renderer\ImageRenderer;
7+
use BaconQrCode\Renderer\RendererStyle\RendererStyle;
8+
use BaconQrCode\Renderer\RendererStyle\Fill;
9+
use BaconQrCode\Renderer\Color\Rgb;
10+
use BaconQrCode\Renderer\RendererStyle\EyeFill;
11+
12+
use BaconQrCode\Renderer\Image\EpsImageBackEnd;
13+
use BaconQrCode\Renderer\Image\ImageBackEndInterface;
14+
use BaconQrCode\Renderer\Image\ImagickImageBackEnd;
15+
use BaconQrCode\Renderer\Image\SvgImageBackEnd;
16+
17+
class BaconQrCodeProvider implements IQRCodeProvider
18+
{
19+
private $borderWidth = 4; // default from Bacon QR Code
20+
private $backgroundColour;
21+
private $foregroundColour;
22+
private $format;
23+
24+
/**
25+
* Ensure we using the latest Bacon QR Code and specify default options
26+
*
27+
* @param int $borderWidth space around the QR code, 4 is the default from Bacon QR Code
28+
* @param string $backgroundColour hex reference for the background colour
29+
* @param string $foregroundColour hex reference for the foreground colour
30+
* @param string $format the desired output, png or svg
31+
*/
32+
public function __construct($borderWidth = 4, $backgroundColour = '#ffffff', $foregroundColour = '#000000', $format = 'png')
33+
{
34+
if (! class_exists(ImagickImageBackEnd::class)) {
35+
throw new \RuntimeException('Make sure you are using version 2 of Bacon QR Code');
36+
}
37+
38+
$this->borderWidth = $borderWidth;
39+
$this->backgroundColour = $this->handleColour($backgroundColour);
40+
$this->foregroundColour = $this->handleColour($foregroundColour);
41+
$this->format = strtolower($format);
42+
}
43+
44+
/**
45+
* Standard functions from IQRCodeProvider
46+
*/
47+
48+
public function getMimeType()
49+
{
50+
switch ($this->format) {
51+
case 'png':
52+
return 'image/png';
53+
case 'gif':
54+
return 'image/gif';
55+
case 'jpg':
56+
case 'jpeg':
57+
return 'image/jpeg';
58+
case 'svg':
59+
return 'image/svg+xml';
60+
case 'eps':
61+
return 'application/postscript';
62+
}
63+
64+
throw new \RuntimeException(sprintf('Unknown MIME-type: %s', $this->format));
65+
}
66+
67+
public function getQRCodeImage($qrText, $size)
68+
{
69+
switch ($this->format) {
70+
case 'svg':
71+
$backend = new SvgImageBackEnd;
72+
break;
73+
case 'eps':
74+
$backend = new EpsImageBackEnd;
75+
break;
76+
default:
77+
$backend = new ImagickImageBackEnd($this->format);
78+
}
79+
80+
$output = $this->getQRCodeByBackend($qrText, $size, $backend);
81+
82+
if ($this->format == 'svg') {
83+
$svg = explode("\n", $output);
84+
return $svg[1];
85+
}
86+
87+
return $output;
88+
}
89+
90+
/**
91+
* Abstract QR code generation function
92+
* providing colour changing support
93+
*/
94+
private function getQRCodeByBackend($qrText, $size, ImageBackEndInterface $backend)
95+
{
96+
$rendererStyleArgs = array($size, $this->borderWidth);
97+
98+
if (is_array($this->foregroundColour) && is_array($this->backgroundColour)) {
99+
$rendererStyleArgs = array_merge($rendererStyleArgs, array(
100+
null,
101+
null,
102+
Fill::withForegroundColor(
103+
new Rgb(...$this->backgroundColour),
104+
new Rgb(...$this->foregroundColour),
105+
new EyeFill(null, null),
106+
new EyeFill(null, null),
107+
new EyeFill(null, null)
108+
)
109+
));
110+
}
111+
112+
$writer = new Writer(new ImageRenderer(
113+
new RendererStyle(...$rendererStyleArgs),
114+
$backend
115+
));
116+
117+
return $writer->writeString($qrText);
118+
}
119+
120+
/**
121+
* Ensure colour is an array of three values but also
122+
* accept a string and assume its a 3 or 6 character hex
123+
*/
124+
private function handleColour($colour)
125+
{
126+
if (is_string($colour) && $colour[0] == '#') {
127+
$hexToRGB = function ($input) {
128+
// ensure input no longer has a # for more predictable division
129+
// PHP 8.1 does not like implicitly casting a float to an int
130+
$input = trim($input, '#');
131+
132+
if (strlen($input) != 3 && strlen($input) != 6) {
133+
throw new \RuntimeException('Colour should be a 3 or 6 character value after the #');
134+
}
135+
136+
// split the array into three chunks
137+
$split = str_split($input, strlen($input) / 3);
138+
139+
// cope with three character hex reference
140+
if (strlen($input) == 3) {
141+
array_walk($split, function (&$character) {
142+
$character = str_repeat($character, 2);
143+
});
144+
}
145+
146+
// convert hex to rgb
147+
return array_map('hexdec', $split);
148+
};
149+
150+
return $hexToRGB($colour);
151+
}
152+
153+
if (is_array($colour) && count($colour) == 3) {
154+
return $colour;
155+
}
156+
157+
throw new \RuntimeException('Invalid colour value');
158+
}
159+
}

web/inc/2fa/Providers/Qr/BaseHTTPQRCodeProvider.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@
44

55
abstract class BaseHTTPQRCodeProvider implements IQRCodeProvider
66
{
7+
/** @var bool */
78
protected $verifyssl;
89

10+
/**
11+
* @param string $url
12+
*
13+
* @return string|bool
14+
*/
915
protected function getContent($url)
1016
{
1117
$curlhandle = curl_init();
12-
18+
1319
curl_setopt_array($curlhandle, array(
1420
CURLOPT_URL => $url,
1521
CURLOPT_RETURNTRANSFER => true,
@@ -20,8 +26,8 @@ protected function getContent($url)
2026
CURLOPT_USERAGENT => 'TwoFactorAuth'
2127
));
2228
$data = curl_exec($curlhandle);
23-
29+
2430
curl_close($curlhandle);
2531
return $data;
2632
}
27-
}
33+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
namespace RobThree\Auth\Providers\Qr;
3+
4+
use Endroid\QrCode\Color\Color;
5+
use Endroid\QrCode\ErrorCorrectionLevel;
6+
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh;
7+
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow;
8+
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelMedium;
9+
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelQuartile;
10+
use Endroid\QrCode\QrCode;
11+
use Endroid\QrCode\Writer\PngWriter;
12+
13+
class EndroidQrCodeProvider implements IQRCodeProvider
14+
{
15+
public $bgcolor;
16+
public $color;
17+
public $margin;
18+
public $errorcorrectionlevel;
19+
20+
protected $endroid4 = false;
21+
22+
public function __construct($bgcolor = 'ffffff', $color = '000000', $margin = 0, $errorcorrectionlevel = 'H')
23+
{
24+
$this->endroid4 = method_exists(QrCode::class, 'create');
25+
26+
$this->bgcolor = $this->handleColor($bgcolor);
27+
$this->color = $this->handleColor($color);
28+
$this->margin = $margin;
29+
$this->errorcorrectionlevel = $this->handleErrorCorrectionLevel($errorcorrectionlevel);
30+
}
31+
32+
public function getMimeType()
33+
{
34+
return 'image/png';
35+
}
36+
37+
public function getQRCodeImage($qrtext, $size)
38+
{
39+
if (!$this->endroid4) {
40+
return $this->qrCodeInstance($qrtext, $size)->writeString();
41+
}
42+
43+
$writer = new PngWriter();
44+
return $writer->write($this->qrCodeInstance($qrtext, $size))->getString();
45+
}
46+
47+
protected function qrCodeInstance($qrtext, $size)
48+
{
49+
$qrCode = new QrCode($qrtext);
50+
$qrCode->setSize($size);
51+
52+
$qrCode->setErrorCorrectionLevel($this->errorcorrectionlevel);
53+
$qrCode->setMargin($this->margin);
54+
$qrCode->setBackgroundColor($this->bgcolor);
55+
$qrCode->setForegroundColor($this->color);
56+
57+
return $qrCode;
58+
}
59+
60+
private function handleColor($color)
61+
{
62+
$split = str_split($color, 2);
63+
$r = hexdec($split[0]);
64+
$g = hexdec($split[1]);
65+
$b = hexdec($split[2]);
66+
67+
return $this->endroid4 ? new Color($r, $g, $b, 0) : ['r' => $r, 'g' => $g, 'b' => $b, 'a' => 0];
68+
}
69+
70+
private function handleErrorCorrectionLevel($level)
71+
{
72+
switch ($level) {
73+
case 'L':
74+
return $this->endroid4 ? new ErrorCorrectionLevelLow() : ErrorCorrectionLevel::LOW();
75+
case 'M':
76+
return $this->endroid4 ? new ErrorCorrectionLevelMedium() : ErrorCorrectionLevel::MEDIUM();
77+
case 'Q':
78+
return $this->endroid4 ? new ErrorCorrectionLevelQuartile() : ErrorCorrectionLevel::QUARTILE();
79+
case 'H':
80+
default:
81+
return $this->endroid4 ? new ErrorCorrectionLevelHigh() : ErrorCorrectionLevel::HIGH();
82+
}
83+
}
84+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
namespace RobThree\Auth\Providers\Qr;
3+
4+
use Endroid\QrCode\Logo\Logo;
5+
use Endroid\QrCode\Writer\PngWriter;
6+
7+
class EndroidQrCodeWithLogoProvider extends EndroidQrCodeProvider
8+
{
9+
protected $logoPath;
10+
protected $logoSize;
11+
12+
/**
13+
* Adds an image to the middle of the QR Code.
14+
* @param string $path Path to an image file
15+
* @param array|int $size Just the width, or [width, height]
16+
*/
17+
public function setLogo($path, $size = null)
18+
{
19+
$this->logoPath = $path;
20+
$this->logoSize = (array)$size;
21+
}
22+
23+
public function getQRCodeImage($qrtext, $size)
24+
{
25+
if (!$this->endroid4) {
26+
return $this->qrCodeInstance($qrtext, $size)->writeString();
27+
}
28+
29+
$logo = null;
30+
if ($this->logoPath) {
31+
$logo = Logo::create($this->logoPath);
32+
if ($this->logoSize) {
33+
$logo->setResizeToWidth($this->logoSize[0]);
34+
if (isset($this->logoSize[1])) {
35+
$logo->setResizeToHeight($this->logoSize[1]);
36+
}
37+
}
38+
}
39+
$writer = new PngWriter();
40+
return $writer->write($this->qrCodeInstance($qrtext, $size), $logo)->getString();
41+
}
42+
43+
protected function qrCodeInstance($qrtext, $size) {
44+
$qrCode = parent::qrCodeInstance($qrtext, $size);
45+
46+
if (!$this->endroid4 && $this->logoPath) {
47+
$qrCode->setLogoPath($this->logoPath);
48+
if ($this->logoSize) {
49+
$qrCode->setLogoSize($this->logoSize[0], isset($this->logoSize[1]) ? $this->logoSize[1] : null);
50+
}
51+
}
52+
53+
return $qrCode;
54+
}
55+
}

0 commit comments

Comments
 (0)