mirror of
https://github.com/saccas/mjml-typo3.git
synced 2024-11-15 10:36:10 +01:00
Daniel Siepmann
c45dee45b0
Use one functional test to ensure everything works, no unit tests with heavy mocking. Update mjml to fix security issues and allow new options. Provide GitHub Actions. This is a public extension with free minutes which should ensure that existing setup still works.
58 lines
1.8 KiB
PHP
58 lines
1.8 KiB
PHP
<?php
|
|
namespace Saccas\Mjml\Domain\Renderer;
|
|
|
|
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
|
|
use TYPO3\CMS\Core\Utility\CommandUtility;
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
|
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
|
|
|
|
class Command implements RendererInterface
|
|
{
|
|
private ExtensionConfiguration $extensionConfiguration;
|
|
|
|
public function __construct(
|
|
ExtensionConfiguration $extensionConfiguration
|
|
) {
|
|
$this->extensionConfiguration = $extensionConfiguration;
|
|
}
|
|
|
|
public function getHtmlFromMjml($mjml): string
|
|
{
|
|
$conf = $this->extensionConfiguration->get('mjml');
|
|
|
|
$temporaryMjmlFileWithPath = GeneralUtility::tempnam('mjml_', '.mjml');
|
|
|
|
GeneralUtility::writeFileToTypo3tempDir($temporaryMjmlFileWithPath, $mjml);
|
|
|
|
$mjmlExtPath = ExtensionManagementUtility::extPath('mjml');
|
|
|
|
// see https://mjml.io/download and https://www.npmjs.com/package/mjml-cli
|
|
$cmd = $conf['nodeBinaryPath'] . ' ' . $mjmlExtPath . $conf['mjmlBinaryPath'] . $conf['mjmlBinary'];
|
|
$args = $temporaryMjmlFileWithPath . ' ' . $conf['mjmlParams'];
|
|
|
|
$result = [];
|
|
$returnValue = '';
|
|
|
|
CommandUtility::exec($this->getEscapedCommand($cmd, $args), $result, $returnValue);
|
|
|
|
GeneralUtility::unlink_tempfile($temporaryMjmlFileWithPath);
|
|
|
|
return implode('', $result);
|
|
}
|
|
|
|
/**
|
|
* @param string $cmd
|
|
* @param string $args
|
|
* @return string
|
|
*/
|
|
protected function getEscapedCommand(string $cmd, string $args): string
|
|
{
|
|
$escapedCmd = escapeshellcmd($cmd);
|
|
|
|
$argsArray = explode(' ', $args);
|
|
$escapedArgsArray = CommandUtility::escapeShellArguments($argsArray);
|
|
$escapedArgs = implode(' ', $escapedArgsArray);
|
|
|
|
return $escapedCmd . ' ' . $escapedArgs;
|
|
}
|
|
}
|