Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\system;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Transliteration\TransliterationInterface;
|
Chris@0
|
6 use Drupal\Component\Utility\Unicode;
|
Chris@0
|
7 use Drupal\Core\Access\CsrfTokenGenerator;
|
Chris@0
|
8 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
|
Chris@0
|
9 use Symfony\Component\HttpFoundation\JsonResponse;
|
Chris@0
|
10 use Symfony\Component\HttpFoundation\Request;
|
Chris@0
|
11 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
Chris@0
|
12 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * Controller routines for machine name transliteration routes.
|
Chris@0
|
16 */
|
Chris@0
|
17 class MachineNameController implements ContainerInjectionInterface {
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * The transliteration helper.
|
Chris@0
|
21 *
|
Chris@0
|
22 * @var \Drupal\Component\Transliteration\TransliterationInterface
|
Chris@0
|
23 */
|
Chris@0
|
24 protected $transliteration;
|
Chris@0
|
25
|
Chris@0
|
26 /**
|
Chris@0
|
27 * The token generator.
|
Chris@0
|
28 *
|
Chris@0
|
29 * @var \Drupal\Core\Access\CsrfTokenGenerator
|
Chris@0
|
30 */
|
Chris@0
|
31 protected $tokenGenerator;
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * Constructs a MachineNameController object.
|
Chris@0
|
35 *
|
Chris@0
|
36 * @param \Drupal\Component\Transliteration\TransliterationInterface $transliteration
|
Chris@0
|
37 * The transliteration helper.
|
Chris@0
|
38 * @param \Drupal\Core\Access\CsrfTokenGenerator $token_generator
|
Chris@0
|
39 * The token generator.
|
Chris@0
|
40 */
|
Chris@0
|
41 public function __construct(TransliterationInterface $transliteration, CsrfTokenGenerator $token_generator) {
|
Chris@0
|
42 $this->transliteration = $transliteration;
|
Chris@0
|
43 $this->tokenGenerator = $token_generator;
|
Chris@0
|
44 }
|
Chris@0
|
45
|
Chris@0
|
46 /**
|
Chris@0
|
47 * {@inheritdoc}
|
Chris@0
|
48 */
|
Chris@0
|
49 public static function create(ContainerInterface $container) {
|
Chris@0
|
50 return new static(
|
Chris@0
|
51 $container->get('transliteration'),
|
Chris@0
|
52 $container->get('csrf_token')
|
Chris@0
|
53 );
|
Chris@0
|
54 }
|
Chris@0
|
55
|
Chris@0
|
56 /**
|
Chris@0
|
57 * Transliterates a string in given language. Various postprocessing possible.
|
Chris@0
|
58 *
|
Chris@0
|
59 * @param \Symfony\Component\HttpFoundation\Request $request
|
Chris@0
|
60 * The input string and language for the transliteration.
|
Chris@0
|
61 * Optionally may contain the replace_pattern, replace, lowercase parameters.
|
Chris@0
|
62 *
|
Chris@0
|
63 * @return \Symfony\Component\HttpFoundation\JsonResponse
|
Chris@0
|
64 * The transliterated string.
|
Chris@0
|
65 */
|
Chris@0
|
66 public function transliterate(Request $request) {
|
Chris@0
|
67 $text = $request->query->get('text');
|
Chris@0
|
68 $langcode = $request->query->get('langcode');
|
Chris@0
|
69 $replace_pattern = $request->query->get('replace_pattern');
|
Chris@0
|
70 $replace_token = $request->query->get('replace_token');
|
Chris@0
|
71 $replace = $request->query->get('replace');
|
Chris@0
|
72 $lowercase = $request->query->get('lowercase');
|
Chris@0
|
73
|
Chris@0
|
74 $transliterated = $this->transliteration->transliterate($text, $langcode, '_');
|
Chris@0
|
75 if ($lowercase) {
|
Chris@0
|
76 $transliterated = Unicode::strtolower($transliterated);
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 if (isset($replace_pattern) && isset($replace)) {
|
Chris@0
|
80 if (!isset($replace_token)) {
|
Chris@0
|
81 throw new AccessDeniedHttpException("Missing 'replace_token' query parameter.");
|
Chris@0
|
82 }
|
Chris@0
|
83 elseif (!$this->tokenGenerator->validate($replace_token, $replace_pattern)) {
|
Chris@0
|
84 throw new AccessDeniedHttpException("Invalid 'replace_token' query parameter.");
|
Chris@0
|
85 }
|
Chris@0
|
86
|
Chris@0
|
87 // Quote the pattern delimiter and remove null characters to avoid the e
|
Chris@0
|
88 // or other modifiers being injected.
|
Chris@0
|
89 $transliterated = preg_replace('@' . strtr($replace_pattern, ['@' => '\@', chr(0) => '']) . '@', $replace, $transliterated);
|
Chris@0
|
90 }
|
Chris@0
|
91 return new JsonResponse($transliterated);
|
Chris@0
|
92 }
|
Chris@0
|
93
|
Chris@0
|
94 }
|