‪TYPO3CMS  ‪main
TypolinkTagSoftReferenceParser.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 
25 
31 {
32  public function ‪parse(string $table, string $field, int ‪$uid, string $content, string $structurePath = ''): ‪SoftReferenceParserResult
33  {
34  $this->‪setTokenIdBasePrefix($table, (string)‪$uid, $field, $structurePath);
35 
36  // Parse string for special TYPO3 <link> tag:
37  $htmlParser = GeneralUtility::makeInstance(HtmlParser::class);
38  $linkService = GeneralUtility::makeInstance(LinkService::class);
39  $linkTags = $htmlParser->splitTags('a', $content);
40  // Traverse result:
41  $elements = [];
42  foreach ($linkTags as $key => $foundValue) {
43  if ($key % 2 && preg_match('/href="([^"]+)"/', $foundValue, $matches)) {
44  try {
45  $linkDetails = $linkService->resolve($matches[1]);
46  if ($linkDetails['type'] === ‪LinkService::TYPE_FILE && preg_match('/file\?uid=(\d+)/', $matches[1], $fileIdMatch)) {
47  $token = $this->‪makeTokenID((string)$key);
48  $elements[$key]['matchString'] = $foundValue;
49  $linkTags[$key] = str_replace($matches[1], '{softref:' . $token . '}', $foundValue);
50  $elements[$key]['subst'] = [
51  'type' => 'db',
52  'recordRef' => 'sys_file:' . $fileIdMatch[1],
53  'tokenID' => $token,
54  'tokenValue' => 'file:' . ($linkDetails['file'] instanceof ‪File ? $linkDetails['file']->‪getUid() : $fileIdMatch[1]),
55  ];
56  } elseif ($linkDetails['type'] === ‪LinkService::TYPE_PAGE && preg_match('/page\?uid=(\d+)#?(\d+)?/', $matches[1], $pageAndAnchorMatches)) {
57  $token = $this->‪makeTokenID((string)$key);
58  $content = '{softref:' . $token . '}';
59  $elements[$key]['matchString'] = $foundValue;
60  $elements[$key]['subst'] = [
61  'type' => 'db',
62  'recordRef' => 'pages:' . ($linkDetails['pageuid'] ?? 0),
63  'tokenID' => $token,
64  'tokenValue' => $linkDetails['pageuid'] ?? '',
65  ];
66  if (isset($pageAndAnchorMatches[2]) && $pageAndAnchorMatches[2] !== '') {
67  // Anchor is assumed to point to a content elements:
68  if (‪MathUtility::canBeInterpretedAsInteger($pageAndAnchorMatches[2])) {
69  // Initialize a new entry because we have a new relation:
70  $newTokenID = $this->‪makeTokenID('setTypoLinkPartsElement:anchor:' . $key);
71  $elements[$newTokenID . ':' . $key] = [];
72  $elements[$newTokenID . ':' . $key]['matchString'] = 'Anchor Content Element: ' . $pageAndAnchorMatches[2];
73  $content .= '#{softref:' . $newTokenID . '}';
74  $elements[$newTokenID . ':' . $key]['subst'] = [
75  'type' => 'db',
76  'recordRef' => 'tt_content:' . $pageAndAnchorMatches[2],
77  'tokenID' => $newTokenID,
78  'tokenValue' => $pageAndAnchorMatches[2],
79  ];
80  } else {
81  // Anchor is a hardcoded string
82  $content .= '#' . $pageAndAnchorMatches[2];
83  }
84  }
85  $linkTags[$key] = str_replace($matches[1], $content, $foundValue);
86  } elseif ($linkDetails['type'] === ‪LinkService::TYPE_URL) {
87  $token = $this->‪makeTokenID((string)$key);
88  $elements[$key]['matchString'] = $foundValue;
89  $linkTags[$key] = str_replace($matches[1], '{softref:' . $token . '}', $foundValue);
90  $elements[$key]['subst'] = [
91  'type' => 'external',
92  'tokenID' => $token,
93  'tokenValue' => (string)($linkDetails['url'] ?? ''),
94  ];
95  } elseif ($linkDetails['type'] === ‪LinkService::TYPE_EMAIL) {
96  $token = $this->‪makeTokenID((string)$key);
97  $elements[$key]['matchString'] = $foundValue;
98  $linkTags[$key] = str_replace($matches[1], '{softref:' . $token . '}', $foundValue);
99  $elements[$key]['subst'] = [
100  'type' => 'string',
101  'tokenID' => $token,
102  'tokenValue' => (string)($linkDetails['email'] ?? ''),
103  ];
104  } elseif ($linkDetails['type'] === ‪LinkService::TYPE_TELEPHONE) {
105  $token = $this->‪makeTokenID((string)$key);
106  $elements[$key]['matchString'] = $foundValue;
107  $linkTags[$key] = str_replace($matches[1], '{softref:' . $token . '}', $foundValue);
108  $elements[$key]['subst'] = [
109  'type' => 'string',
110  'tokenID' => $token,
111  'tokenValue' => (string)($linkDetails['telephone'] ?? ''),
112  ];
113  }
114  } catch (\‪Exception $e) {
115  // skip invalid links
116  }
117  }
118  }
119  // Return output:
121  implode('', $linkTags),
122  $elements
123  );
124  }
125 }
‪TYPO3\CMS\Core\DataHandling\SoftReference\AbstractSoftReferenceParser\setTokenIdBasePrefix
‪setTokenIdBasePrefix(string $table, string $uid, string $field, string $structurePath)
Definition: AbstractSoftReferenceParser.php:55
‪TYPO3\CMS\Core\Html\HtmlParser
Definition: HtmlParser.php:26
‪TYPO3\CMS\Core\DataHandling\SoftReference\SoftReferenceParserResult\create
‪static create(string $content, array $elements)
Definition: SoftReferenceParserResult.php:48
‪TYPO3\CMS\Core\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger(mixed $var)
Definition: MathUtility.php:74
‪TYPO3\CMS\Core\DataHandling\SoftReference
Definition: AbstractSoftReferenceParser.php:18
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Core\Resource\AbstractFile\getUid
‪return MathUtility::canBeInterpretedAsInteger($size) ?(int) $size int getUid()
Definition: AbstractFile.php:195
‪TYPO3\CMS\Core\DataHandling\SoftReference\SoftReferenceParserResult
Definition: SoftReferenceParserResult.php:43
‪TYPO3\CMS\Webhooks\Message\$uid
‪identifier readonly int $uid
Definition: PageModificationMessage.php:35
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Core\DataHandling\SoftReference\AbstractSoftReferenceParser\makeTokenID
‪string makeTokenID(string $index='')
Definition: AbstractSoftReferenceParser.php:35
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\DataHandling\SoftReference\AbstractSoftReferenceParser
Definition: AbstractSoftReferenceParser.php:24