Skip to content

Commit

Permalink
12.4 support
Browse files Browse the repository at this point in the history
  • Loading branch information
dsimon-dsimon committed May 12, 2023
1 parent d1a07e7 commit f1db419
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 252 deletions.
203 changes: 21 additions & 182 deletions Classes/Domain/Finishers/GpgEmailFinisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

namespace SUDHAUS7\Sudhaus7Gpgadmin\Domain\Finishers;

use Doctrine\DBAL\Driver\Exception;
use Doctrine\DBAL\Driver\Result;
use SUDHAUS7\Sudhaus7Gpgadmin\Helper\PgpEncyptor;
use SUDHAUS7\Sudhaus7Gpgadmin\Helper\PgpEncryptor;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Information\Typo3Version;
use TYPO3\CMS\Core\Mail\FluidEmail;
use TYPO3\CMS\Core\Mail\Mailer;
use TYPO3\CMS\Core\Mail\MailMessage;
use TYPO3\CMS\Core\Mail\MailerInterface;
use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
Expand All @@ -22,18 +21,13 @@

class GpgEmailFinisher extends EmailFinisher
{

/**
* @inheritDoc
* @throws Exception|\Symfony\Component\Mailer\Exception\TransportExceptionInterface
*/
protected function executeInternal()
protected function executeInternal(): string
{
$version = GeneralUtility::makeInstance(Typo3Version::class);
if ($version->getBranch()==='11.5') {
$mail = $this->handle115();
} else {
$mail = $this->handle104();
}
$mail = $this->handle124();

$recipients = $mail->getTo();
foreach ($recipients as $recipient) {
Expand All @@ -58,10 +52,10 @@ protected function executeInternal()


if (is_array($pgprow) && !empty($pgprow) && isset($pgprow['pgp_public_key'])) {
$encryptor = new PgpEncyptor((string)$pgprow['pgp_public_key']);
$encryptor = new PgpEncryptor((string)$pgprow['pgp_public_key']);
$mailToEncode = $encryptor->encrypt($mailToEncode);
}
GeneralUtility::makeInstance(Mailer::class)->send($mailToEncode);
GeneralUtility::makeInstance(MailerInterface::class)->send($mailToEncode);
}

return '';
Expand All @@ -71,131 +65,7 @@ protected function executeInternal()
* @return Email
* @throws FinisherException
*/
private function handle104(): Email
{
$languageBackup = null;
// Flexform overrides write strings instead of integers so
// we need to cast the string '0' to false.
if (
isset($this->options['addHtmlPart'])
&& $this->options['addHtmlPart'] === '0'
) {
$this->options['addHtmlPart'] = false;
}

$subject = $this->parseOption('subject');
$recipients = $this->getRecipients('recipients', 'recipientAddress', 'recipientName'); /** @phpstan-ignore-line */
$senderAddress = $this->parseOption('senderAddress');
$senderAddress = is_string($senderAddress) ? $senderAddress : '';
$senderName = $this->parseOption('senderName');
$senderName = is_string($senderName) ? $senderName : '';
$replyToRecipients = $this->getRecipients('replyToRecipients', 'replyToAddress'); /** @phpstan-ignore-line */
$carbonCopyRecipients = $this->getRecipients('carbonCopyRecipients', 'carbonCopyAddress'); /** @phpstan-ignore-line */
$blindCarbonCopyRecipients = $this->getRecipients('blindCarbonCopyRecipients', 'blindCarbonCopyAddress'); /** @phpstan-ignore-line */
$addHtmlPart = $this->isHtmlPartAdded(); /** @phpstan-ignore-line */
$attachUploads = $this->parseOption('attachUploads');
$useFluidEmail = $this->parseOption('useFluidEmail');
$title = $this->parseOption('title');
$title = is_string($title) && $title !== '' ? $title : $subject;

if (empty($subject)) {
throw new FinisherException('The option "subject" must be set for the EmailFinisher.', 1327060320);
}
if (empty($recipients)) {
throw new FinisherException('The option "recipients" must be set for the EmailFinisher.', 1327060200);
}
if (empty($senderAddress)) {
throw new FinisherException('The option "senderAddress" must be set for the EmailFinisher.', 1327060210);
}

$formRuntime = $this->finisherContext->getFormRuntime();

$translationService = TranslationService::getInstance();
if (is_string($this->options['translation']['language'] ?? null) && $this->options['translation']['language'] !== '') {
$languageBackup = $translationService->getLanguage();
$translationService->setLanguage($this->options['translation']['language']);
}

$mail = $useFluidEmail
? $this
->initializeFluidEmail($formRuntime)
->format($addHtmlPart ? FluidEmail::FORMAT_BOTH : FluidEmail::FORMAT_PLAIN)
->assign('title', $title)
: GeneralUtility::makeInstance(MailMessage::class);

$mail
->from(new Address($senderAddress, $senderName))
->to(...$recipients)
->subject($subject);

if (!empty($replyToRecipients)) {
$mail->replyTo(...$replyToRecipients);
}

if (!empty($carbonCopyRecipients)) {
$mail->cc(...$carbonCopyRecipients);
}

if (!empty($blindCarbonCopyRecipients)) {
$mail->bcc(...$blindCarbonCopyRecipients);
}

if (!$useFluidEmail) {
$parts = [
[
'format' => 'Plaintext',
'contentType' => 'text/plain',
],
];

if ($addHtmlPart) {
$parts[] = [
'format' => 'Html',
'contentType' => 'text/html',
];
}

foreach ($parts as $i => $part) {
$standaloneView = $this->initializeStandaloneView($formRuntime, $part['format']);
$message = $standaloneView->render();

if ($part['contentType'] === 'text/plain') {
$mail->text($message);
} else {
$mail->html($message);
}
}
}

if (!empty($languageBackup)) {
$translationService->setLanguage($languageBackup);
}

$elements = $formRuntime->getFormDefinition()->getRenderablesRecursively();

if ($attachUploads) {
foreach ($elements as $element) {
if (!$element instanceof FileUpload) {
continue;
}
$file = $formRuntime[$element->getIdentifier()];
if ($file) {
if ($file instanceof FileReference) {
$file = $file->getOriginalResource();
}
/** @var File $file */
$mail->attach($file->getContents(), $file->getName(), $file->getMimeType());
}
}
}
return $mail;
}

/**
* @return Email
* @throws FinisherException
*/
private function handle115(): Email
private function handle124(): Email
{
$languageBackup = null;
// Flexform overrides write strings instead of integers so
Expand All @@ -216,13 +86,11 @@ private function handle115(): Email
$replyToRecipients = $this->getRecipients('replyToRecipients'); /** @phpstan-ignore-line */
$carbonCopyRecipients = $this->getRecipients('carbonCopyRecipients'); /** @phpstan-ignore-line */
$blindCarbonCopyRecipients = $this->getRecipients('blindCarbonCopyRecipients'); /** @phpstan-ignore-line */
$addHtmlPart = $this->parseOption('addHtmlPart') ? true : false;
$addHtmlPart = (bool)$this->parseOption('addHtmlPart'); /** @phpstan-ignore-line */
$attachUploads = $this->parseOption('attachUploads');
$useFluidEmail = $this->parseOption('useFluidEmail');
$title = $this->parseOption('title');
$title = is_string($title) && $title !== '' ? $title : $subject;
$title = (string)$this->parseOption('title') ?: $subject;

if (empty($subject)) {
if ($subject === '') {
throw new FinisherException('The option "subject" must be set for the EmailFinisher.', 1327060320);
}
if (empty($recipients)) {
Expand All @@ -240,17 +108,13 @@ private function handle115(): Email
$translationService->setLanguage($this->options['translation']['language']);
}

$mail = $useFluidEmail
? $this
->initializeFluidEmail($formRuntime)
->format($addHtmlPart ? FluidEmail::FORMAT_BOTH : FluidEmail::FORMAT_PLAIN)
->assign('title', $title)
: GeneralUtility::makeInstance(MailMessage::class);

$mail
$mail = $this
->initializeFluidEmail($formRuntime)
->from(new Address($senderAddress, $senderName))
->to(...$recipients)
->subject($subject);
->subject($subject)
->format($addHtmlPart ? FluidEmail::FORMAT_BOTH : FluidEmail::FORMAT_PLAIN)
->assign('title', $title);

if (!empty($replyToRecipients)) {
$mail->replyTo(...$replyToRecipients);
Expand All @@ -264,39 +128,14 @@ private function handle115(): Email
$mail->bcc(...$blindCarbonCopyRecipients);
}

if (!$useFluidEmail) {
$parts = [
[
'format' => 'Plaintext',
'contentType' => 'text/plain',
],
];

if ($addHtmlPart) {
$parts[] = [
'format' => 'Html',
'contentType' => 'text/html',
];
}

foreach ($parts as $i => $part) {
$standaloneView = $this->initializeStandaloneView($formRuntime, $part['format']);
$message = $standaloneView->render();

if ($part['contentType'] === 'text/plain') {
$mail->text($message);
} else {
$mail->html($message);
}
}
}

if (!empty($languageBackup)) {
$translationService->setLanguage($languageBackup);
}

$elements = $formRuntime->getFormDefinition()->getRenderablesRecursively();

if ($attachUploads) {
foreach ($formRuntime->getFormDefinition()->getRenderablesRecursively() as $element) {
foreach ($elements as $element) {
if (!$element instanceof FileUpload) {
continue;
}
Expand All @@ -305,11 +144,11 @@ private function handle115(): Email
if ($file instanceof FileReference) {
$file = $file->getOriginalResource();
}
/** @var File $file */
$mail->attach($file->getContents(), $file->getName(), $file->getMimeType());
}
}
}

return $mail;
}
}
1 change: 0 additions & 1 deletion Classes/Domain/Model/Gpgkey.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

class Gpgkey extends AbstractEntity
{

/**
* @var string
*/
Expand Down
2 changes: 1 addition & 1 deletion Classes/Domain/Service/PgpBinaryHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use UnexpectedValueException;

use function is_resource;
use function proc_close;
use function putenv;
Expand All @@ -17,7 +18,6 @@

class PgpBinaryHandler implements PgpHandlerInterface
{

/**
* @var string
*/
Expand Down
25 changes: 12 additions & 13 deletions Classes/Domain/Service/PgpExtensionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

class PgpExtensionHandler implements PgpHandlerInterface
{


/**
* @var string
*/
Expand All @@ -36,10 +34,10 @@ public function __construct()
/**
* @inheritDoc
*/
public function encode(string $message, KeyInformationImmutable $recpientKey): string
public function encode(string $message, KeyInformationImmutable $recipientKey): string
{
$this->gnupg->import($recpientKey->getKey());
$this->gnupg->addencryptkey($recpientKey->getFingerprint());
$this->gnupg->import($recipientKey->getKey());
$this->gnupg->addencryptkey($recipientKey->getFingerprint());
$this->gnupg->setarmor(1);
return $this->gnupg->encrypt($message);
}
Expand All @@ -59,15 +57,16 @@ public function sign(string $message, string $signerEmail): string
public function keyInformation(string $key): KeyInformationImmutable
{
$keyarray = $this->gnupg->import($key);
$kyconfig = $this->gnupg->keyinfo($keyarray['fingerprint']);
$keyconfig = $this->gnupg->keyinfo($keyarray['fingerprint'] ?? '');

return new KeyInformationImmutable(
$kyconfig[0]['uids'][0]['uid'],
$keyarray['fingerprint'],
new \DateTimeImmutable('@'.$kyconfig[0]['subkeys'][0]['timestamp']),
new \DateTimeImmutable('@'.$kyconfig[0]['subkeys'][0]['expires']),
$kyconfig[0]['subkeys'][0]['length'] ?? 0,
$kyconfig[0]['uids'][0]['email'],
$kyconfig[0]['uids'][0]['name'],
$kyconfig[0]['uids'][0]['uid'] ?? 0,
$keyarray['fingerprint'] ?? '',
new \DateTimeImmutable('@' . ($keyconfig[0]['subkeys'][0]['timestamp'] ?? 0)),
new \DateTimeImmutable('@' . ($keyconfig[0]['subkeys'][0]['expires'] ?? 0)),
$keyconfig[0]['subkeys'][0]['length'] ?? 0,
$keyconfig[0]['uids'][0]['email'] ?? '',
$keyconfig[0]['uids'][0]['name'] ?? '',
$key
);
}
Expand Down
1 change: 0 additions & 1 deletion Classes/Domain/Service/PgpHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

interface PgpHandlerInterface
{

/**
* @param string $message
* @param KeyInformationImmutable $recpientKey
Expand Down
4 changes: 3 additions & 1 deletion Classes/Form/FieldControl/GpgKeyInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use SUDHAUS7\Sudhaus7Gpgadmin\Domain\Service\PgpHandlerFactory;
use SUDHAUS7\Sudhaus7Gpgadmin\Domain\Service\PgpHandlerInterface;
use TYPO3\CMS\Backend\Form\Element\TextElement;

use function htmlentities;

class GpgKeyInfo extends TextElement
Expand All @@ -21,7 +22,8 @@ public function render(): array
try {
$key = $pgpHandler->keyInformation($row['pgp_public_key']);

$result = sprintf('%s<br/>', htmlentities($key->getUid()));
$result = sprintf('Id: %s<br/>', htmlentities($key->getUid()));
$result .= sprintf('%s &lt;%s&gt;<br/>', $key->getName(), $key->getEmail());
$result .= sprintf('Fingerprint: %s<br/>', $key->getFingerprint());
$result .= sprintf('Valid %s - %s', $key->getStart()->format('Y-m-d'), $key->getEnd()->format('Y-m-d'));
} catch (InvalidArgumentException $e) {
Expand Down
Loading

0 comments on commit f1db419

Please sign in to comment.