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