2012-12-04 00:48:32 +01:00
|
|
|
<?php
|
|
|
|
|
2019-12-30 00:27:20 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2012-12-04 00:48:32 +01:00
|
|
|
namespace Gregwar\CaptchaBundle\Generator;
|
|
|
|
|
|
|
|
use Symfony\Component\Finder\Finder;
|
|
|
|
|
|
|
|
/**
|
2020-01-03 00:12:44 +01:00
|
|
|
* Handles actions related to captcha image files including saving and garbage collection.
|
2012-12-04 01:02:43 +01:00
|
|
|
*
|
|
|
|
* @author Gregwar <g.passault@gmail.com>
|
2012-12-04 00:48:32 +01:00
|
|
|
* @author Jeremy Livingston <jeremy@quizzle.com>
|
|
|
|
*/
|
|
|
|
class ImageFileHandler
|
|
|
|
{
|
|
|
|
/**
|
2020-01-03 00:12:44 +01:00
|
|
|
* Name of folder for captcha images.
|
|
|
|
*
|
2012-12-04 00:48:32 +01:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $imageFolder;
|
|
|
|
|
|
|
|
/**
|
2020-01-03 00:12:44 +01:00
|
|
|
* Absolute path to public web folder.
|
|
|
|
*
|
2012-12-04 00:48:32 +01:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $webPath;
|
|
|
|
|
|
|
|
/**
|
2020-01-03 00:12:44 +01:00
|
|
|
* Frequency of garbage collection in fractions of 1.
|
|
|
|
*
|
2012-12-04 00:48:32 +01:00
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $gcFreq;
|
|
|
|
|
|
|
|
/**
|
2020-01-03 00:12:44 +01:00
|
|
|
* Maximum age of images in minutes.
|
|
|
|
*
|
2012-12-04 00:48:32 +01:00
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $expiration;
|
|
|
|
|
|
|
|
/**
|
2019-12-30 00:27:20 +01:00
|
|
|
* @param string $imageFolder
|
|
|
|
* @param string $webPath
|
|
|
|
* @param string $gcFreq
|
|
|
|
* @param string $expiration
|
2012-12-04 00:48:32 +01:00
|
|
|
*/
|
2019-12-30 00:27:20 +01:00
|
|
|
public function __construct(string $imageFolder, string $webPath, string $gcFreq, string $expiration)
|
2012-12-04 00:48:32 +01:00
|
|
|
{
|
2019-12-30 00:27:20 +01:00
|
|
|
$this->imageFolder = $imageFolder;
|
2020-01-03 00:12:44 +01:00
|
|
|
$this->webPath = $webPath;
|
2019-12-30 00:27:20 +01:00
|
|
|
$this->gcFreq = $gcFreq;
|
|
|
|
$this->expiration = $expiration;
|
2012-12-04 00:48:32 +01:00
|
|
|
}
|
|
|
|
|
2020-01-03 00:12:44 +01:00
|
|
|
public function saveAsFile($contents): string
|
2012-12-04 00:48:32 +01:00
|
|
|
{
|
|
|
|
$this->createFolderIfMissing();
|
|
|
|
|
2020-01-03 00:12:44 +01:00
|
|
|
$filename = md5(uniqid()).'.jpg';
|
|
|
|
$filePath = $this->webPath.'/'.$this->imageFolder.'/'.$filename;
|
2012-12-04 00:48:32 +01:00
|
|
|
imagejpeg($contents, $filePath, 15);
|
|
|
|
|
2020-01-03 00:12:44 +01:00
|
|
|
return '/'.$this->imageFolder.'/'.$filename;
|
2012-12-04 00:48:32 +01:00
|
|
|
}
|
|
|
|
|
2019-12-30 00:27:20 +01:00
|
|
|
public function collectGarbage(): bool
|
2012-12-04 00:48:32 +01:00
|
|
|
{
|
2020-01-03 00:12:44 +01:00
|
|
|
if (1 == !mt_rand(1, $this->gcFreq)) {
|
2012-12-04 00:48:32 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->createFolderIfMissing();
|
|
|
|
|
|
|
|
$finder = new Finder();
|
|
|
|
$criteria = sprintf('<= now - %s minutes', $this->expiration);
|
2020-01-03 00:12:44 +01:00
|
|
|
$finder->in($this->webPath.'/'.$this->imageFolder)
|
2012-12-04 00:48:32 +01:00
|
|
|
->date($criteria);
|
|
|
|
|
2020-01-03 00:12:44 +01:00
|
|
|
foreach ($finder->files() as $file) {
|
2012-12-04 00:48:32 +01:00
|
|
|
unlink($file->getPathname());
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-12-30 00:27:20 +01:00
|
|
|
protected function createFolderIfMissing(): void
|
2012-12-04 00:48:32 +01:00
|
|
|
{
|
2020-01-03 00:12:44 +01:00
|
|
|
if (!file_exists($this->webPath.'/'.$this->imageFolder)) {
|
|
|
|
mkdir($this->webPath.'/'.$this->imageFolder, 0755);
|
2012-12-04 00:48:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|