48ca6e4704e19222d3928c0ddc7da00eb8883717
[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 $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'] ?? ''));
92 if (!$containsSlash
93 && trim($rootFileDat)
94 && (
95 @is_file(PATH_site . $rootFileDat)
96 || $fileExtension === 'php'
97 || $fileExtension === 'html'
98 || $fileExtension === 'htm'
99 )
100 ) {
101 $isLocalFile = 1;
102 } elseif ($containsSlash) {
103 // Adding this so realurl directories are linked right (non-existing).
104 $isLocalFile = 2;
105 }
106 }
107
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;
111 if (!$scheme) {
112 $result['url'] = 'http://' . $linkParameter;
113 } else {
114 $result['url'] = $linkParameter;
115 }
116 // file (internal) or folder
117 } elseif ($containsSlash || $isLocalFile) {
118 $result = $this->getFileOrFolderObjectFromMixedIdentifier($linkParameter);
119 } else {
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);
123 }
124 }
125
126 return $result;
127 }
128
129 /**
130 * Internal method to do some magic to get a page parts, additional params, fragment / section hash
131 *
132 * @param string $data the input variable, can be "mypage,23" with fragments, keys
133 *
134 * @return array the result array with the page type set
135 */
136 protected function resolvePageRelatedParameters(string $data): array
137 {
138 $result = ['type' => LinkService::TYPE_PAGE];
139 if (strpos($data, '#') !== false) {
140 list($data, $result['fragment']) = explode('#', $data, 2);
141 }
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);
147 }
148 if (empty($data)) {
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];
161 }
162 }
163 } else {
164 $result['pageuid'] = $data;
165 }
166
167 // expect an alias
168 if (!MathUtility::canBeInterpretedAsInteger($result['pageuid']) && $result['pageuid'] !== 'current') {
169 $result['pagealias'] = $result['pageuid'];
170 unset($result['pageuid']);
171 }
172
173 return $result;
174 }
175
176 /**
177 * Internal method that fetches a file or folder object based on the file or folder combined identifier
178 *
179 * @param string $mixedIdentifier can be something like "2" (file uid), "fileadmin/i/like.png" or "2:/myidentifier/"
180 *
181 * @return array the result with the type (file or folder) set
182 */
183 protected function getFileOrFolderObjectFromMixedIdentifier(string $mixedIdentifier): array
184 {
185 $result = [];
186 try {
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;
195 } else {
196 $result['type'] = LinkService::TYPE_UNKNOWN;
197 $result['file'] = $mixedIdentifier;
198 }
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;
207 }
208
209 return $result;
210 }
211
212 /**
213 * Initializes the resource factory (only once)
214 *
215 * @return ResourceFactory
216 */
217 protected function getResourceFactory(): ResourceFactory
218 {
219 if (!$this->resourceFactory) {
220 $this->resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
221 }
222 return $this->resourceFactory;
223 }
224 }