‪TYPO3CMS  10.4
FormDataFactory.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
24 final class ‪FormDataFactory
25 {
26  public function ‪fromHtmlMarkupAndXpath(string $html, string $query = '//form'): ‪FormData
27  {
28  return new ‪FormData(
29  $this->‪buildFormData(
30  $this->‪extractFormFragment($html, $query)
31  )
32  );
33  }
34 
35  private function ‪buildFormData(\DOMDocument $document): array
36  {
37  $data = [
38  'actionQueryData' => [],
39  'actionUrl' => '',
40  'elementData' => [],
41  'DOMDocument' => $document,
42  ];
43  foreach ($document->getElementsByTagName('form') as $node) {
44  $action = $node->getAttribute('action');
45  $actionQuery = parse_url($action, PHP_URL_QUERY);
46  $queryArray = [];
47  parse_str($actionQuery, $queryArray);
48  $data['actionQueryData'] = $queryArray;
49 
50  [$actionUrl, ] = explode('?', $action);
51  $data['actionUrl'] = $actionUrl;
52 
53  break;
54  }
55 
56  $xpath = new \DomXPath($document);
57  $nodesWithName = $xpath->query('//*[@name]');
58  foreach ($nodesWithName as $node) {
59  $name = $node->getAttribute('name');
60  foreach ($node->attributes ?? [] as $attribute) {
61  $data['elementData'][$name][$attribute->nodeName] = $attribute->nodeValue;
62  }
63  $data['elementData'][$name]['__isHoneypot'] = $this->‪isHoneypot($node);
64  }
65 
66  return $data;
67  }
68 
69  private function ‪extractFormFragment(string $html, string $query): \DOMDocument
70  {
71  $document = new \DOMDocument();
72  $document->loadHTML($html, LIBXML_NOERROR);
73 
74  $xpath = new \DomXPath($document);
75  $fragment = new \DOMDocument();
76  foreach ($xpath->query($query) as $node) {
77  $fragment->appendChild($fragment->importNode($node, true));
78  }
79 
80  return $fragment;
81  }
82 
83  private function ‪isHoneypot(\DOMElement $node): bool
84  {
85  if (!$node->hasAttribute('id')) {
86  return false;
87  }
88  if (!$node->hasAttribute('autocomplete')) {
89  return false;
90  }
91 
92  return str_ends_with($node->getAttribute('id'), $node->getAttribute('autocomplete'));
93  }
94 }
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormDataFactory\fromHtmlMarkupAndXpath
‪fromHtmlMarkupAndXpath(string $html, string $query='//form')
Definition: FormDataFactory.php:26
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormDataFactory\isHoneypot
‪isHoneypot(\DOMElement $node)
Definition: FormDataFactory.php:83
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling
Definition: FormData.php:18
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormDataFactory\extractFormFragment
‪extractFormFragment(string $html, string $query)
Definition: FormDataFactory.php:69
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormDataFactory\buildFormData
‪buildFormData(\DOMDocument $document)
Definition: FormDataFactory.php:35
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData
Definition: FormData.php:28
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormDataFactory
Definition: FormDataFactory.php:25