Code development platform for open source projects from the European Union institutions :large_blue_circle: EU Login authentication by SMS has been phased out. To see alternatives please check here

Skip to content
Snippets Groups Projects
Commit 734ca143 authored by Davis Ragels's avatar Davis Ragels
Browse files

More Drupal security advisory fixes

parent d9ac97ab
No related branches found
No related tags found
No related merge requests found
......@@ -85,11 +85,6 @@ class EtranslationCallbackController extends ControllerBase {
/**
* {@inheritdoc}
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* The Drupal service container.
*
* @return static
*/
public static function create(ContainerInterface $container) {
return new static(
......@@ -115,7 +110,7 @@ class EtranslationCallbackController extends ControllerBase {
$request_id = Html::escape($request->get('request-id'));
$translated_content = base64_decode(file_get_contents('php://input'));
$key = EtranslationUtils::getCacheKey($request_id);
$this->logger->info("Received response from eTranslation ($request_id)");
$this->logger->info("Received response from eTranslation (@request_id)", ['@request_id' => $request_id]);
if ($this->cache->get($key) && $this->cache->get($key)->data === EtranslationUtils::$timeoutValue) {
$this->logger->info("Received late translation (@request_id)", ['@request_id' => $request_id]);
$entity_id = Html::escape($request->get('external-reference'));
......@@ -144,7 +139,7 @@ class EtranslationCallbackController extends ControllerBase {
public function onError() {
$request = $this->requestStack->getCurrentRequest();
$request_id = Html::escape($request->get('request-id'));
$this->logger->error("eTranslation response error ($request_id). Please make sure eTranslation API is working as expected.");
$this->logger->error("eTranslation response error (@request_id). Please make sure eTranslation API is working as expected.", ['@request_id' => $request_id]);
$this->cache->set(EtranslationUtils::getCacheKey($request_id), EtranslationUtils::$errorValue);
return new Response();
}
......@@ -194,7 +189,7 @@ class EtranslationCallbackController extends ControllerBase {
}
}
}
$this->logger->error("Could not load entity by ID ($id). Please make sure such entity exists!");
$this->logger->error("Could not load entity by ID (@entity_id). Please make sure such entity exists!", ['@entity_id' => $id]);
return NULL;
}
......@@ -216,7 +211,7 @@ class EtranslationCallbackController extends ControllerBase {
return $langcode;
}
}
$this->logger->error("Could not find langcode for language '$language_lowercase'. Please check if the language code of this language is correct!");
$this->logger->error("Could not find langcode for language '@language'. Please check if the language code of this language is correct!", ['@language' => $language_lowercase]);
return NULL;
}
......
......@@ -61,11 +61,6 @@ class PretranslationProgressController extends ControllerBase {
/**
* {@inheritdoc}
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* The Drupal service container.
*
* @return static
*/
public static function create(ContainerInterface $container) {
return new static(
......
......@@ -6,7 +6,7 @@ use Drupal\content_translation\ContentTranslationManager;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Link;
......@@ -23,7 +23,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Pre-translation tab form.
*/
class PretranslationForm extends FormBase {
class PretranslationForm extends ConfigFormBase {
use LoggerChannelTrait;
......@@ -121,11 +121,6 @@ class PretranslationForm extends FormBase {
/**
* {@inheritdoc}
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* The Drupal service container.
*
* @return static
*/
public static function create(ContainerInterface $container) {
return new static(
......@@ -327,6 +322,15 @@ class PretranslationForm extends FormBase {
$this->messenger->addStatus($this->t('Affected content type selection saved'));
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'webt.settings',
];
}
/**
* Checks if translation is enabled in site settings for given content type.
*
......@@ -379,7 +383,7 @@ class PretranslationForm extends FormBase {
}
$this->pretranslationProgressController->clearTranslationProgress();
$this->pretranslationProgressController->updateTranslationMessage('Translation started');
$this->pretranslationProgressController->updateTranslationMessage($this->t('Translation started'));
$this->pretranslationProgressController->updateTranslationPercentage(0);
$selected_langcodes = array_filter($form_state->getValue('table'));
......@@ -419,7 +423,7 @@ class PretranslationForm extends FormBase {
* Target language codes.
*/
public function translateEntities($entity_type, $langcodes) {
$this->logger->debug("Translating entities of type: '$entity_type'");
$this->logger->debug("Translating entities of type: '@entity_type'", ['@entity_type' => $entity_type]);
$entities = $this->entityTypeManager
->getStorage($entity_type)
......@@ -520,7 +524,7 @@ class PretranslationForm extends FormBase {
* Target language codes.
*/
public function deleteEntityTranslations($entity_type, $langcodes) {
$this->logger->debug("Deleting entity translations of type: '$entity_type'");
$this->logger->debug("Deleting entity translations of type: '@entity_type'", ['@entity_type' => $entity_type]);
$entities = $this->entityTypeManager
->getStorage($entity_type)
......@@ -616,7 +620,7 @@ class PretranslationForm extends FormBase {
return;
default:
$this->logger->error("Invalid translation response '$response'. Please contact the developers of this module!");
$this->logger->error("Invalid translation response '@response'. Please contact the developers of this module!", ['@response' => $response]);
return;
}
}
......@@ -639,7 +643,7 @@ class PretranslationForm extends FormBase {
return;
default:
$this->logger->error("Invalid deletion response '$response'. Please contact the developers of this module!");
$this->logger->error("Invalid deletion response '@response'. Please contact the developers of this module!", ['@response' => $response]);
return;
}
}
......
......@@ -2,7 +2,6 @@
namespace Drupal\webt\Form;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
......@@ -95,11 +94,6 @@ class TranslationProviderForm extends ConfigFormBase {
/**
* {@inheritdoc}
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* The Drupal service container.
*
* @return static
*/
public static function create(ContainerInterface $container) {
return new static(
......@@ -196,10 +190,9 @@ class TranslationProviderForm extends ConfigFormBase {
],
'help_text' => [
'#type' => 'markup',
'#markup' => new FormattableMarkup(
$this->t("Set up an account for <a target='_blank' href='@link'>eTranslation</a>"),
'#markup' => $this->t('Set up an account for <a target="_blank" href=":link">eTranslation</a>',
[
'@link' => 'https://website-translation.language-tools.ec.europa.eu/automated-translation_en',
':link' => 'https://website-translation.language-tools.ec.europa.eu/automated-translation_en',
]
),
],
......@@ -233,10 +226,9 @@ class TranslationProviderForm extends ConfigFormBase {
],
'help_text' => [
'#type' => 'markup',
'#markup' => new FormattableMarkup(
$this->t("Get <a target='_blank' href='@link'>MT provider access</a>"),
'#markup' => $this->t('Get <a target="_blank" href=":link">MT provider access</a>',
[
'@link' => 'https://website-translation.language-tools.ec.europa.eu/automated-translation_en',
':link' => 'https://website-translation.language-tools.ec.europa.eu/automated-translation_en',
]
),
],
......@@ -397,7 +389,7 @@ class TranslationProviderForm extends ConfigFormBase {
break;
default:
$this->logger->error("Invalid translation engine: '$engine'. Please recheck your MT provider authorization settings!");
$this->logger->error("Invalid translation engine: '@engine'. Please recheck your MT provider authorization settings!", ['@engine' => $engine]);
return;
}
}
......@@ -525,7 +517,7 @@ class TranslationProviderForm extends ConfigFormBase {
}
if (count($unsupported_languages) > 0) {
$list_str = implode(', ', $unsupported_languages);
$this->messenger->addWarning($this->t('Current MT provider does not support following languages:') . ' ' . $list_str . '!');
$this->messenger->addWarning($this->t('Current MT provider does not support following languages: @languages!', ['@languages' => $list_str]));
}
return [
'#type' => 'container',
......
......@@ -221,7 +221,7 @@ class TranslationManager {
* If provided, uses given string translations, skips machine translation.
*/
public function translateEntity($entity, $src_lang, $trg_lang, $translations = NULL) {
$this->logger->info("Translating entity ($src_lang-$trg_lang)");
$this->logger->info("Translating entity (@src_lang-@trg_lang)", ['@src_lang' => $src_lang, '@trg_lang' => $trg_lang]);
$translatable_fields = $this->getTranslatableFields($entity);
if (count($translatable_fields) > 0 && $entity->isTranslatable()) {
......
......@@ -5,6 +5,7 @@ namespace Drupal\webt\translation_engines;
use Drupal\Component\Utility\Html;
use Drupal\Core\Http\ClientFactory;
use Drupal\Core\Logger\LoggerChannelTrait;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\webt\Model\TranslationResponse;
use Drupal\webt\Model\TranslationStatus;
use Drupal\webt\WebtConstInterface;
......@@ -15,6 +16,7 @@ use Drupal\webt\WebtConstInterface;
abstract class AbstractTranslationService {
use LoggerChannelTrait;
use StringTranslationTrait;
/**
* Regex patterns for non-translatable Drupal variables in UI strings.
......@@ -267,14 +269,22 @@ abstract class AbstractTranslationService {
}
// Translate XML[].
$this->logger->debug("Sending $lang_from-$lang_to translation request " . ((string) ($i + 1)) . '/' . (string) $request_count . ' (' . ((string) count($translatable_values)) . ' strings)');
$this->logger->debug("Sending @lang_from-@lang_to translation request @iteration/@total_iterations (@total_strings strings)",
[
'@lang_from' => $lang_from,
'@lang_to' => $lang_to,
'@iteration' => $i + 1,
'@total_iterations' => $request_count,
'@total_strings' => count($translatable_values),
]
);
$xml_translations = $this->sendTranslationRequest($lang_from, $lang_to, $translatable_values, $entity_id);
if (!$xml_translations || empty($xml_translations)) {
$retries_left = 2;
while ($retries_left > 0 && (!$entity_id || $this->retryOnEntityTranslationError)) {
$this->checkClientDisconnected($reference_id, $entity_id);
$this->logger->debug("Translation request failed, retrying... (retries left: $retries_left)");
$this->logger->debug("Translation request failed, retrying... (retries left: @retries_left)", ['@retries_left' => $retries_left]);
$xml_translations = $this->sendTranslationRequest($lang_from, $lang_to, $translatable_values, $entity_id);
$retries_left--;
}
......@@ -321,7 +331,7 @@ abstract class AbstractTranslationService {
}
$content_type = isset($response->getHeaders()['Content-Type']) ? $response->getHeaders()['Content-Type'][0] : NULL;
if ($content_type && !str_contains($content_type, 'application/json')) {
$this->logger->error("Invalid content type '$content_type' in language direction response!");
$this->logger->error("Invalid content type '@content_type' in language direction response!", ['@content_type' => $content_type]);
return FALSE;
}
return 200 === $response->getStatusCode();
......@@ -644,7 +654,13 @@ abstract class AbstractTranslationService {
private function updateProgressInfo($percentage, $type, $from, $to, $entity_id) {
$pretranslationProgressController = \Drupal::service('webt.pretranslation_progress_controller');
if (!$entity_id && $pretranslationProgressController->getValue()) {
$pretranslationProgressController->updateTranslationMessage("Translating strings of type '$type' ($from -> $to)");
$pretranslationProgressController->updateTranslationMessage($this->t("Translating strings of type '@type' (@from -> @to)",
[
'@type' => $type,
'@from' => $from,
'@to' => $to,
]
));
$pretranslationProgressController->updateTranslationPercentage($percentage);
return $pretranslationProgressController->getValue()->data->request_id;
}
......
......@@ -224,11 +224,17 @@ class EtranslationService extends AbstractTranslationService {
$status = $response->getStatusCode();
if (200 !== $status || $request_id < 0) {
$message = self::$errorMap[$request_id] ?? $body;
$this->logger->error("Invalid request response from eTranslation: $response [status: $status, message: $message]");
$this->logger->error("Invalid request response from eTranslation: @response [status: @status, message: @message]",
[
'@response' => $response,
'@status' => $status,
'@message' => $message,
]
);
return [];
}
$this->logger->info("eTranslation request successful ($request_id)");
$this->logger->info("eTranslation request successful (@request_id)", ['@request_id' => $request_id]);
// Wait for translation callback.
$translations = $this->awaitEtranslationResponse($request_id, NULL === $entity_id);
......@@ -245,11 +251,11 @@ class EtranslationService extends AbstractTranslationService {
if ($response && strlen($message) == 0) {
$message = Message::toString($response);
}
$this->logger->error('Error translating strings: ' . $message);
$this->logger->error('Error translating strings: @message', ['@message' => $e->getMessage()]);
return [];
}
catch (ConnectException $e) {
$this->logger->error('Could not establish connection with eTranslation. Error: ' . $e->getMessage());
$this->logger->error('Could not establish connection with eTranslation. Error: @message', ['@message' => $e->getMessage()]);
return [];
}
}
......@@ -349,7 +355,12 @@ class EtranslationService extends AbstractTranslationService {
usleep($check_interval_ms * 1000);
}
if (!$response) {
$this->logger->info("eTranslation response timeout ($request_id, $timeout s)");
$this->logger->info("eTranslation response timeout (@request_id, @timeout s)",
[
'@request_id' => $request_id,
'@timeout' => $timeout,
]
);
$this->cache->set($key, EtranslationUtils::$timeoutValue);
return NULL;
}
......@@ -382,11 +393,11 @@ class EtranslationService extends AbstractTranslationService {
if ($response && strlen($message) == 0) {
$message = Message::toString($response);
}
$this->logger->error('Error on language direction request: ' . $message);
$this->logger->error('Error on language direction request: @message', ['@message' => $message]);
return $response;
}
catch (ConnectException $e) {
$this->logger->error('Could not establish connection with eTranslation. Error: ' . $e->getMessage());
$this->logger->error('Could not establish connection with eTranslation. Error: @message', ['@message' => $e->getMessage()]);
return new Response(408);
}
}
......
......@@ -90,11 +90,11 @@ class GenericMTService extends AbstractTranslationService {
if ($response && strlen($message) == 0) {
$message = Message::toString($response);
}
$this->logger->error('Error translating strings: ' . $message);
$this->logger->error('Error translating strings: @message', ['@message' => $message]);
return [];
}
catch (ConnectException $e) {
$this->logger->error('Could not establish connection with the MT provider. Check your API URL or try again later! Error: ' . $e->getMessage());
$this->logger->error('Could not establish connection with the MT provider. Check your API URL or try again later! Error: @message', ['@message' => $e->getMessage()]);
return [];
}
}
......@@ -125,11 +125,11 @@ class GenericMTService extends AbstractTranslationService {
if ($response && strlen($message) == 0) {
$message = Message::toString($response);
}
$this->logger->error('Error on language direction request: ' . $message);
$this->logger->error('Error on language direction request: @message', ['@message' => $message]);
return $response;
}
catch (ConnectException $e) {
$this->logger->error('Could not establish connection with the MT provider. Check your API URL or try again later! Error: ' . $e->getMessage());
$this->logger->error('Could not establish connection with the MT provider. Check your API URL or try again later! Error: @message', ['@message' => $e->getMessage()]);
return new Response(408);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment