cf5c1504987ad7b1be329db600fc1200a447d2ca
[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($linkParameter)) {
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 } else {
74 // special handling without a scheme
75 $isLocalFile = 0;
76 $fileChar = (int)strpos($linkParameter, '/');
77 $urlChar = (int)strpos($linkParameter, '.');
78
79 $containsSlash = false;
80 if (!MathUtility::canBeInterpretedAsInteger($linkParameter)) {
81 // Detects if a file is found in site-root and if so it will be treated like a normal file.
82 list($rootFileDat) = explode('?', rawurldecode($linkParameter));
83 $containsSlash = strpos($rootFileDat, '/') !== false;
84 $pathInfo = pathinfo($rootFileDat);
85 $fileExtension = strtolower(($pathInfo['extension'] ?? ''));
86 if (!$containsSlash
87 && trim($rootFileDat)
88 && (
89 @is_file(PATH_site . $rootFileDat)
90 || $fileExtension === 'php'
91 || $fileExtension === 'html'
92 || $fileExtension === 'htm'
93 )
94 ) {
95 $isLocalFile = 1;
96 } elseif ($containsSlash) {
97 // Adding this so realurl directories are linked right (non-existing).
98 $isLocalFile = 2;
99 }
100 }
101
102 // url (external): If doubleSlash or if a '.' comes before a '/'.
103 if ($isLocalFile !== 1 && $urlChar && (!$containsSlash || $urlChar < $fileChar)) {
104 $result['type'] = LinkService::TYPE_URL;
105 if (!$scheme) {
106 $result['url'] = 'http://' . $linkParameter;
107 } else {
108 $result['url'] = $linkParameter;
109 }
110 // file (internal) or folder
111 } elseif ($containsSlash || $isLocalFile) {
112 $result = $this->getFileOrFolderObjectFromMixedIdentifier($linkParameter);
113 } else {
114 // Integer or alias (alias is without slashes or periods or commas, that is
115 // 'nospace,alphanum_x,lower,unique' according to definition in $GLOBALS['TCA']!)
116 $result = $this->resolvePageRelatedParameters($linkParameter);
117 }
118 }
119
120 return $result;
121 }
122
123 /**
124 * Internal method to do some magic to get a page parts, additional params, fragment / section hash
125 *
126 * @param string $data the input variable, can be "mypage,23" with fragments, keys
127 *
128 * @return array the result array with the page type set
129 */
130 protected function resolvePageRelatedParameters(string $data): array
131 {
132 $result = ['type' => LinkService::TYPE_PAGE];
133 if (strpos($data, '#') !== false) {
134 list($data, $result['fragment']) = explode('#', $data, 2);
135 }
136 // check for additional parameters
137 if (strpos($data, '?') !== false) {
138 list($data, $result['parameters']) = explode('?', $data, 2);
139 } elseif (strpos($data, '&') !== false) {
140 list($data, $result['parameters']) = explode('&', $data, 2);
141 }
142 if (empty($data)) {
143 $result['pageuid'] = 'current';
144 } elseif ($data{0} === '#') {
145 $result['pageuid'] = 'current';
146 $result['fragment'] = substr($data, 1);
147 } elseif (strpos($data, ',') !== false) {
148 list($result['pageuid'], $result['pagetype']) = explode(',', $data, 2);
149 } elseif (strpos($data, '/') !== false) {
150 $data = explode('/', trim($data, '/'));
151 $result['pageuid'] = array_shift($data);
152 foreach ($data as $k => $item) {
153 if ($data[$k] % 2 === 0 && !empty($data[$k + 1])) {
154 $result['page' . $data[$k]] = $data[$k + 1];
155 }
156 }
157 } else {
158 $result['pageuid'] = $data;
159 }
160
161 // expect an alias
162 if (!MathUtility::canBeInterpretedAsInteger($result['pageuid']) && $result['pageuid'] !== 'current') {
163 $result['pagealias'] = $result['pageuid'];
164 unset($result['pageuid']);
165 }
166
167 return $result;
168 }
169
170 /**
171 * Internal method that fetches a file or folder object based on the file or folder combined identifier
172 *
173 * @param string $mixedIdentifier can be something like "2" (file uid), "fileadmin/i/like.png" or "2:/myidentifier/"
174 *
175 * @return array the result with the type (file or folder) set
176 */
177 protected function getFileOrFolderObjectFromMixedIdentifier(string $mixedIdentifier): array
178 {
179 $result = [];
180 try {
181 $fileOrFolderObject = $this->getResourceFactory()->retrieveFileOrFolderObject($mixedIdentifier);
182 // Link to a folder or file
183 if ($fileOrFolderObject instanceof File) {
184 $result['type'] = LinkService::TYPE_FILE;
185 $result['file'] = $fileOrFolderObject;
186 } elseif ($fileOrFolderObject instanceof Folder) {
187 $result['type'] = LinkService::TYPE_FOLDER;
188 $result['folder'] = $fileOrFolderObject;
189 } else {
190 $result['type'] = LinkService::TYPE_UNKNOWN;
191 $result['file'] = $mixedIdentifier;
192 }
193 } catch (\RuntimeException $e) {
194 // Element wasn't found
195 $result['type'] = LinkService::TYPE_UNKNOWN;
196 $result['file'] = $mixedIdentifier;
197 } catch (ResourceDoesNotExistException $e) {
198 // Resource was not found
199 $result['type'] = LinkService::TYPE_UNKNOWN;
200 $result['file'] = $mixedIdentifier;
201 }
202
203 return $result;
204 }
205
206 /**
207 * Initializes the resource factory (only once)
208 *
209 * @return ResourceFactory
210 */
211 protected function getResourceFactory(): ResourceFactory
212 {
213 if (!$this->resourceFactory) {
214 $this->resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
215 }
216 return $this->resourceFactory;
217 }
218 }