Newer
Older
<?php
declare(strict_types=1);
/*
* This file is part of the "gsb_metadata_cleaner" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/
namespace ITZBund\GsbMetadataCleaner\Configuration;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationExtensionNotConfiguredException;
use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationPathDoesNotExistException;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration as CoreExtensionConfiguration;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\PathUtility;
final public const EXIFTOOL_BINARY = 'exiftool';
final public const QPDF_BINARY = 'qpdf';
public function __construct(
private readonly CoreExtensionConfiguration $extensionConfiguration,
private readonly LoggerInterface $logger
) {}
/**
* Get the path to the exiftool executable, defined by extension configuration or default
*
* @return string the path to the exiftool executable
*/
public function getExifToolPath(): string
{
$exifToolPath = trim($this->extensionConfiguration->get('gsb_metadata_cleaner', 'exifToolPath'));
if (!$this->utilityExistsAndIsSafe($exifToolPath, self::EXIFTOOL_BINARY)) {
throw new RuntimeException('exitfool path not properly configured', 1710517552);
return $exifToolPath . self::EXIFTOOL_BINARY;
}
/**
* Get the path to the qpdf executable, defined by extension configuration or default
*
* @return string the path to the pdf executable
*/
public function getQpdfToolPath(): string
{
$qpdfToolPath = trim($this->extensionConfiguration->get('gsb_metadata_cleaner', 'qpdfToolPath'));
if (!$this->utilityExistsAndIsSafe($qpdfToolPath, self::QPDF_BINARY)) {
throw new RuntimeException('qpdf path not properly configured', 1710517551);
return $qpdfToolPath . self::QPDF_BINARY;
}
public function getUseQpdf(): bool
{
$useQpdf = true;
try {
$useQpdf = (bool)$this->extensionConfiguration->get('gsb_metadata_cleaner', 'useQpdf');
} catch (ExtensionConfigurationExtensionNotConfiguredException $ecence) {
$this->logger->log(LogLevel::INFO, 'Extension not configured');
} catch (ExtensionConfigurationPathDoesNotExistException $ecpdnee) {
$this->logger->log(LogLevel::INFO, 'useQpdf not configured');
/**
* @param string $path
* @param string $binaryName
* @return bool
*/
protected function utilityExistsAndIsSafe(string $path, string $binaryName): bool
{
if ($path === '' || !PathUtility::isAbsolutePath($path)
|| !GeneralUtility::validPathStr($path) || str_starts_with($path, Environment::getPublicPath())
|| !is_dir($path)
) {
return false;
}
if (file_exists($path . $binaryName)) {
return true;
}
return false;
}