2 declare(strict_types
=1);
3 namespace TYPO3\CMS\Core\LinkHandling
;
6 * This file is part of the TYPO3 CMS project.
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.
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
15 * The TYPO3 project - inspiring people to share!
18 use TYPO3\CMS\Core\
Resource\Exception\ResourceDoesNotExistException
;
19 use TYPO3\CMS\Core\
Resource\File
;
20 use TYPO3\CMS\Core\
Resource\Folder
;
21 use TYPO3\CMS\Core\
Resource\ResourceFactory
;
22 use TYPO3\CMS\Core\Utility\GeneralUtility
;
23 use TYPO3\CMS\Core\Utility\MathUtility
;
26 * Class to resolve and convert the "old" link information (email, external url, file, page etc)
27 * to a URL or new format for migration
31 class LegacyLinkNotationConverter
35 * @var ResourceFactory
37 protected $resourceFactory;
40 * Part of the typolink construction functionality, called by typoLink()
41 * Used to resolve "legacy"-based typolinks.
43 * Tries to get the type of the link from the link parameter
45 * - "mailto" an email address
46 * - "url" external URL
47 * - "file" a local file (checked AFTER getPublicUrl() is called)
48 * - "page" a page (integer or alias)
50 * Does NOT check if the page exists or the file exists.
52 * @param string $linkParameter could be "fileadmin/myfile.jpg", "info@typo3.org", "13" or "http://www.typo3.org"
56 public function resolve(string $linkParameter): array
60 $scheme = parse_url($linkParameter, PHP_URL_SCHEME
);
62 // Resolve FAL-api "file:UID-of-sys_file-record" and "file:combined-identifier"
63 if (stripos($linkParameter, 'file:') === 0) {
64 $result = $this->getFileOrFolderObjectFromMixedIdentifier(substr($linkParameter, 5));
65 } elseif (GeneralUtility
::validEmail(parse_url($linkParameter, PHP_URL_PATH
))) {
66 $result['type'] = LinkService
::TYPE_EMAIL
;
67 $result['email'] = $linkParameter;
68 } elseif (strpos($linkParameter, ':') !== false) {
69 // Check for link-handler keyword
70 list($linkHandlerKeyword, $linkHandlerValue) = explode(':', $linkParameter, 2);
71 $result['type'] = strtolower(trim($linkHandlerKeyword));
72 $result['url'] = $linkHandlerValue;
73 if ($result['type'] === LinkService
::TYPE_RECORD
) {
74 list($a['identifier'], $a['table'], $a['uid']) = explode(':', $linkHandlerValue);
77 // @TODO add a hook for old typolinkLinkHandler to convert their values properly, forge #79647
80 // special handling without a scheme
82 $fileChar = (int)strpos($linkParameter, '/');
83 $urlChar = (int)strpos($linkParameter, '.');
85 $containsSlash = false;
86 if (!MathUtility
::canBeInterpretedAsInteger($linkParameter)) {
87 // Detects if a file is found in site-root and if so it will be treated like a normal file.
88 list($rootFileDat) = explode('?', rawurldecode($linkParameter));
89 $containsSlash = strpos($rootFileDat, '/') !== false;
90 $pathInfo = pathinfo($rootFileDat);
91 $fileExtension = strtolower(($pathInfo['extension'] ??
''));
95 @is_file
(PATH_site
. $rootFileDat)
96 ||
$fileExtension === 'php'
97 ||
$fileExtension === 'html'
98 ||
$fileExtension === 'htm'
102 } elseif ($containsSlash) {
103 // Adding this so realurl directories are linked right (non-existing).
108 // url (external): If doubleSlash or if a '.' comes before a '/'.
109 if ($isLocalFile !== 1 && $urlChar && (!$containsSlash ||
$urlChar < $fileChar)) {
110 $result['type'] = LinkService
::TYPE_URL
;
112 $result['url'] = 'http://' . $linkParameter;
114 $result['url'] = $linkParameter;
116 // file (internal) or folder
117 } elseif ($containsSlash ||
$isLocalFile) {
118 $result = $this->getFileOrFolderObjectFromMixedIdentifier($linkParameter);
120 // Integer or alias (alias is without slashes or periods or commas, that is
121 // 'nospace,alphanum_x,lower,unique' according to definition in $GLOBALS['TCA']!)
122 $result = $this->resolvePageRelatedParameters($linkParameter);
130 * Internal method to do some magic to get a page parts, additional params, fragment / section hash
132 * @param string $data the input variable, can be "mypage,23" with fragments, keys
134 * @return array the result array with the page type set
136 protected function resolvePageRelatedParameters(string $data): array
138 $result = ['type' => LinkService
::TYPE_PAGE
];
139 if (strpos($data, '#') !== false) {
140 list($data, $result['fragment']) = explode('#', $data, 2);
142 // check for additional parameters
143 if (strpos($data, '?') !== false) {
144 list($data, $result['parameters']) = explode('?', $data, 2);
145 } elseif (strpos($data, '&') !== false) {
146 list($data, $result['parameters']) = explode('&', $data, 2);
149 $result['pageuid'] = 'current';
150 } elseif ($data{0} === '#') {
151 $result['pageuid'] = 'current';
152 $result['fragment'] = substr($data, 1);
153 } elseif (strpos($data, ',') !== false) {
154 list($result['pageuid'], $result['pagetype']) = explode(',', $data, 2);
155 } elseif (strpos($data, '/') !== false) {
156 $data = explode('/', trim($data, '/'));
157 $result['pageuid'] = array_shift($data);
158 foreach ($data as $k => $item) {
159 if ($data[$k] %
2 === 0 && !empty($data[$k +
1])) {
160 $result['page' . $data[$k]] = $data[$k +
1];
164 $result['pageuid'] = $data;
168 if (!MathUtility
::canBeInterpretedAsInteger($result['pageuid']) && $result['pageuid'] !== 'current') {
169 $result['pagealias'] = $result['pageuid'];
170 unset($result['pageuid']);
177 * Internal method that fetches a file or folder object based on the file or folder combined identifier
179 * @param string $mixedIdentifier can be something like "2" (file uid), "fileadmin/i/like.png" or "2:/myidentifier/"
181 * @return array the result with the type (file or folder) set
183 protected function getFileOrFolderObjectFromMixedIdentifier(string $mixedIdentifier): array
187 $fileOrFolderObject = $this->getResourceFactory()->retrieveFileOrFolderObject($mixedIdentifier);
188 // Link to a folder or file
189 if ($fileOrFolderObject instanceof File
) {
190 $result['type'] = LinkService
::TYPE_FILE
;
191 $result['file'] = $fileOrFolderObject;
192 } elseif ($fileOrFolderObject instanceof Folder
) {
193 $result['type'] = LinkService
::TYPE_FOLDER
;
194 $result['folder'] = $fileOrFolderObject;
196 $result['type'] = LinkService
::TYPE_UNKNOWN
;
197 $result['file'] = $mixedIdentifier;
199 } catch (\RuntimeException
$e) {
200 // Element wasn't found
201 $result['type'] = LinkService
::TYPE_UNKNOWN
;
202 $result['file'] = $mixedIdentifier;
203 } catch (ResourceDoesNotExistException
$e) {
204 // Resource was not found
205 $result['type'] = LinkService
::TYPE_UNKNOWN
;
206 $result['file'] = $mixedIdentifier;
213 * Initializes the resource factory (only once)
215 * @return ResourceFactory
217 protected function getResourceFactory(): ResourceFactory
219 if (!$this->resourceFactory
) {
220 $this->resourceFactory
= GeneralUtility
::makeInstance(ResourceFactory
::class);
222 return $this->resourceFactory
;