Skip to content

Commit

Permalink
Updated email verifier.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentmuller committed Mar 19, 2024
1 parent ad3ea02 commit d46b749
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 66 deletions.
40 changes: 20 additions & 20 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 20 additions & 3 deletions src/Pdf/Traits/PdfMemoryImageTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,30 @@ public function imageMemory(
float $height = 0.0,
string|int $link = ''
): void {
$mimeType = $this->getMimeType($data);
$fileType = $this->getFileType($mimeType);
$filename = $this->getFileName($mimeType, $data);
$this->image($filename, $x, $y, $width, $height, $fileType, $link);
}

private function getFileName(string $mimeType, string $data): string
{
return \sprintf('data://%s;base64,%s', $mimeType, \base64_encode($data));
}

private function getFileType(string $mimeType): string
{
return \substr((string) \strrchr($mimeType, '/'), 1);
}

private function getMimeType(string $data): string
{
$info = new \finfo(\FILEINFO_MIME_TYPE);
$mime = $info->buffer($data);
if (!\is_string($mime) || !\str_contains($mime, '/')) {
throw new PdfException(\sprintf('Empty or incorrect mime type: "%s".', $mime));
}
$type = \substr((string) \strstr($mime, '/'), 1);
$filename = \sprintf('data://%s;base64,%s', $mime, \base64_encode($data));
$this->image($filename, $x, $y, $width, $height, $type, $link);

return $mime;
}
}
52 changes: 14 additions & 38 deletions src/Report/MemoryImageReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use App\Controller\AbstractController;
use App\Pdf\Traits\PdfMemoryImageTrait;
use App\Service\ImageService;
use fpdf\PdfException;

/**
Expand Down Expand Up @@ -41,56 +42,31 @@ public function render(): bool

private function addImageGD(): void
{
$image = \imagecreate(200, 150);
if (!$image instanceof \GdImage) {
return;
$service = ImageService::fromTrueColor(200, 150);
if (!$service instanceof ImageService) {
throw new PdfException('Unable to create image.');
}

$background = $this->allocateColor($image, 255, 255, 255);
\imagefilledrectangle($image, 0, 0, 199, 149, $background);
$service->fill((int) $service->allocateWhite());
$service->rectangle(0, 0, 199, 149, (int) $service->allocateBlack());
$service->fillRectangle(30, 100, 30, 48, (int) $service->allocate(255, 0, 0));
$service->fillRectangle(80, 80, 30, 68, (int) $service->allocate(0, 255, 0));
$service->fillRectangle(130, 40, 30, 108, (int) $service->allocate(0, 0, 255));

$border = $this->allocateColor($image, 169, 169, 169);
\imagerectangle($image, 0, 0, 199, 149, $border);

$color1 = $this->allocateColor($image, 255, 0, 0);
\imagefilledrectangle($image, 30, 100, 60, 148, $color1);

$color2 = $this->allocateColor($image, 0, 255, 0);
\imagefilledrectangle($image, 80, 80, 110, 148, $color2);

$color3 = $this->allocateColor($image, 0, 0, 255);
\imagefilledrectangle($image, 130, 40, 160, 148, $color3);

$this->imageGD($image, 160, 20, 40);

// free memory
\imagecolordeallocate($image, $background);
\imagecolordeallocate($image, $border);
\imagecolordeallocate($image, $color1);
\imagecolordeallocate($image, $color2);
\imagecolordeallocate($image, $color3);
\imagedestroy($image);
$this->imageGD($service->getImage(), 160, 20, 40);
}

private function addImageMemory(): void
{
if (!\file_exists($this->image)) {
return;
throw new PdfException('Unable to get image.');
}

$data = \file_get_contents($this->image);
if (!\is_string($data)) {
return;
}
$this->imageMemory($data, 10, 20, 30);
}

private function allocateColor(\GdImage $image, int $red, int $green, int $blue): int
{
$color = \imagecolorallocate($image, $red, $green, $blue);
if (!\is_int($color)) {
throw new PdfException('Unable to allocate color.');
throw new PdfException('Unable to get image content.');
}

return $color;
$this->imageMemory($data, 10, 20, 30);
}
}
3 changes: 1 addition & 2 deletions src/Service/EmailVerifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,8 @@ private function trans(string $id, array $parameters = [], ?string $domain = nul
*/
private function validateEmail(Request $request, User $user): void
{
$url = $request->getUri();
$id = (string) $user->getId();
$email = (string) $user->getEmail();
$this->helper->validateEmailConfirmation($url, $id, $email);
$this->helper->validateEmailConfirmationFromRequest($request, $id, $email);
}
}
40 changes: 37 additions & 3 deletions src/Service/ImageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,18 +177,36 @@ public function copyResampled(
* Fill this image's bounds with the given color.
*
* @param int $color the fill color. A color identifier created with allocate.
* @param int $x the x-coordinate of start point
* @param int $y the y-coordinate of start point
*
* @return bool true on success or false on failure
*
* @see ImageService::allocate()
*/
public function fill(int $color): bool
public function fill(int $color, int $x = 0, int $y = 0): bool
{
return \imagefill($this->image, 0, 0, $color);
return \imagefill($this->image, $x, $y, $color);
}

/**
* Create a new image handler from file or URL.
* Draw a filled rectangle with the give color.
*
* @param int $x the x-coordinate
* @param int $y the y-coordinate
* @param int $width the rectangle width
* @param int $height the rectangle height
* @param int $color the fill color. A color identifier created with allocate.
*
* @return bool true on success or false on failure
*/
public function fillRectangle(int $x, int $y, int $width, int $height, int $color): bool
{
return \imagefilledrectangle($this->image, $x, $y, $x + $width, $y + $height, $color);
}

/**
* Create a new image handler from a file or a URL.
*
* This method uses the file extension to create the handler.
*
Expand Down Expand Up @@ -267,6 +285,22 @@ public function line(int $x1, int $y1, int $x2, int $y2, int $color): bool
return \imageline($this->image, $x1, $y1, $x2, $y2, $color);
}

/**
* Draw a rectangle with the given border color.
*
* @param int $x the x-coordinate
* @param int $y the y-coordinate
* @param int $width the rectangle width
* @param int $height the rectangle height
* @param int $color the border color. A color identifier created with allocate.
*
* @return bool true on success or false on failure
*/
public function rectangle(int $x, int $y, int $width, int $height, int $color): bool
{
return \imagerectangle($this->image, $x, $y, $x + $width, $y + $height, $color);
}

/**
* Get the horizontal resolution of this image in dot per inch (DPI).
*
Expand Down

0 comments on commit d46b749

Please sign in to comment.