[BUGFIX] LinkService must handle old ?id=xyz links
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / LinkHandling / LegacyLinkNotationConverter.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Core\LinkHandling;
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
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;
24
25 /**
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
28 *
29 * @internal
30 */
31 class LegacyLinkNotationConverter
32 {
33
34 /**
35 * @var ResourceFactory
36 */
37 protected $resourceFactory;
38
39 /**
40 * Part of the typolink construction functionality, called by typoLink()
41 * Used to resolve "legacy"-based typolinks.
42 *
43 * Tries to get the type of the link from the link parameter
44 * could be
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)
49 *
50 * Does NOT check if the page exists or the file exists.
51 *
52 * @param string $linkParameter could be "fileadmin/myfile.jpg", "info@typo3.org", "13" or "http://www.typo3.org"
53 *
54 * @return array
55 */
56 public function resolve(string $linkParameter): array
57 {
58 $result = [];
59 // Parse URL scheme
60 $scheme = parse_url($linkParameter, PHP_URL_SCHEME);
61
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);
75 $result['url'] = $a;
76 } else {
77 // @TODO add a hook for old typolinkLinkHandler to convert their values properly, forge #79647
78 }
79 } else {
80 // special handling without a scheme
81 $isLocalFile = 0;
82 $fileChar = (int)strpos($linkParameter, '/');
83 $urlChar = (int)strpos($linkParameter, '.');
84
85 $isIdOrAlias = MathUtility::canBeInterpretedAsInteger($linkParameter);
86 $matches = [];
87 // capture old RTE links relative to TYPO3_mainDir
88 if (preg_match('#../\\?id=([^&]+)#', $linkParameter, $matches)) {
89 $linkParameter = $matches[1];
90 $isIdOrAlias = true;
91 }
92 $containsSlash = false;
93 if (!$isIdOrAlias) {
94 // Detects if a file is found in site-root and if so it will be treated like a normal file.
95 list($rootFileDat) = explode('?', rawurldecode($linkParameter));
96 $containsSlash = strpos($rootFileDat, '/') !== false;
97 $pathInfo = pathinfo($rootFileDat);
98 $fileExtension = strtolower(($pathInfo['extension'] ?? ''));
99 if (!$containsSlash
100 && trim($rootFileDat)
101 && (
102 @is_file(PATH_site . $rootFileDat)
103 || $fileExtension === 'php'
104 || $fileExtension === 'html'
105 || $fileExtension === 'htm'
106 )
107 ) {
108 $isLocalFile = 1;
109 } elseif ($containsSlash) {
110 // Adding this so realurl directories are linked right (non-existing).
111 $isLocalFile = 2;
112 }
113 }
114
115 // url (external): If doubleSlash or if a '.' comes before a '/'.
116 if (!$isIdOrAlias && $isLocalFile !== 1 && $urlChar && (!$containsSlash || $urlChar < $fileChar)) {
117 $result['type'] = LinkService::TYPE_URL;
118 if (!$scheme) {
119 $result['url'] = 'http://' . $linkParameter;
120 } else {
121 $result['url'] = $linkParameter;
122 }
123 // file (internal) or folder
124 } elseif ($containsSlash || $isLocalFile) {
125 $result = $this->getFileOrFolderObjectFromMixedIdentifier($linkParameter);
126 } else {
127 // Integer or alias (alias is without slashes or periods or commas, that is
128 // 'nospace,alphanum_x,lower,unique' according to definition in $GLOBALS['TCA']!)
129 $result = $this->resolvePageRelatedParameters($linkParameter);
130 }
131 }
132
133 return $result;
134 }
135
136 /**
137 * Internal method to do some magic to get a page parts, additional params, fragment / section hash
138 *
139 * @param string $data the input variable, can be "mypage,23" with fragments, keys
140 *
141 * @return array the result array with the page type set
142 */
143 protected function resolvePageRelatedParameters(string $data): array
144 {
145 $result = ['type' => LinkService::TYPE_PAGE];
146 if (strpos($data, '#') !== false) {
147 list($data, $result['fragment']) = explode('#', $data, 2);
148 }
149 // check for additional parameters
150 if (strpos($data, '?') !== false) {
151 list($data, $result['parameters']) = explode('?', $data, 2);
152 } elseif (strpos($data, '&') !== false) {
153 list($data, $result['parameters']) = explode('&', $data, 2);
154 }
155 if (empty($data)) {
156 $result['pageuid'] = 'current';
157 } elseif ($data{0} === '#') {
158 $result['pageuid'] = 'current';
159 $result['fragment'] = substr($data, 1);
160 } elseif (strpos($data, ',') !== false) {
161 list($result['pageuid'], $result['pagetype']) = explode(',', $data, 2);
162 } elseif (strpos($data, '/') !== false) {
163 $data = explode('/', trim($data, '/'));
164 $result['pageuid'] = array_shift($data);
165 foreach ($data as $k => $item) {
166 if ($data[$k] % 2 === 0 && !empty($data[$k + 1])) {
167 $result['page' . $data[$k]] = $data[$k + 1];
168 }
169 }
170 } else {
171 $result['pageuid'] = $data;
172 }
173
174 // expect an alias
175 if (!MathUtility::canBeInterpretedAsInteger($result['pageuid']) && $result['pageuid'] !== 'current') {
176 $result['pagealias'] = $result['pageuid'];
177 unset($result['pageuid']);
178 }
179
180 return $result;
181 }
182
183 /**
184 * Internal method that fetches a file or folder object based on the file or folder combined identifier
185 *
186 * @param string $mixedIdentifier can be something like "2" (file uid), "fileadmin/i/like.png" or "2:/myidentifier/"
187 *
188 * @return array the result with the type (file or folder) set
189 */
190 protected function getFileOrFolderObjectFromMixedIdentifier(string $mixedIdentifier): array
191 {
192 $result = [];
193 try {
194 $fileOrFolderObject = $this->getResourceFactory()->retrieveFileOrFolderObject($mixedIdentifier);
195 // Link to a folder or file
196 if ($fileOrFolderObject instanceof File) {
197 $result['type'] = LinkService::TYPE_FILE;
198 $result['file'] = $fileOrFolderObject;
199 } elseif ($fileOrFolderObject instanceof Folder) {
200 $result['type'] = LinkService::TYPE_FOLDER;
201 $result['folder'] = $fileOrFolderObject;
202 } else {
203 $result['type'] = LinkService::TYPE_UNKNOWN;
204 $result['file'] = $mixedIdentifier;
205 }
206 } catch (\RuntimeException $e) {
207 // Element wasn't found
208 $result['type'] = LinkService::TYPE_UNKNOWN;
209 $result['file'] = $mixedIdentifier;
210 } catch (ResourceDoesNotExistException $e) {
211 // Resource was not found
212 $result['type'] = LinkService::TYPE_UNKNOWN;
213 $result['file'] = $mixedIdentifier;
214 }
215
216 return $result;
217 }
218
219 /**
220 * Initializes the resource factory (only once)
221 *
222 * @return ResourceFactory
223 */
224 protected function getResourceFactory(): ResourceFactory
225 {
226 if (!$this->resourceFactory) {
227 $this->resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
228 }
229 return $this->resourceFactory;
230 }
231 }