TYPO3 CMS  TYPO3_8-7
LinkService.php
Go to the documentation of this file.
1 <?php
2 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 
20 
28 {
29  const TYPE_PAGE = 'page';
30  const TYPE_URL = 'url';
31  const TYPE_EMAIL = 'email';
32  const TYPE_FILE = 'file';
33  const TYPE_FOLDER = 'folder';
34  const TYPE_RECORD = 'record';
35  const TYPE_UNKNOWN = 'unknown';
36 
42  protected $handlers;
43 
47  public function __construct()
48  {
49  if (!empty($GLOBALS['TYPO3_CONF_VARS']['SYS']['linkHandler'])) {
50  foreach ($GLOBALS['TYPO3_CONF_VARS']['SYS']['linkHandler'] as $type => $handler) {
51  if (!is_object($this->handlers[$type])) {
52  $this->handlers[$type] = GeneralUtility::makeInstance($handler);
53  }
54  }
55  }
56  }
57 
74  public function resolve(string $linkParameter): array
75  {
76  try {
77  // Check if the new syntax with "t3://" is used
78  return $this->resolveByStringRepresentation($linkParameter);
79  } catch (Exception\UnknownUrnException $e) {
80  $legacyLinkNotationConverter = GeneralUtility::makeInstance(LegacyLinkNotationConverter::class);
81  return $legacyLinkNotationConverter->resolve($linkParameter);
82  }
83  }
84 
93  public function resolveByStringRepresentation(string $urn): array
94  {
95  // linking to any t3:// syntax
96  if (stripos($urn, 't3://') === 0) {
97  // lets parse the urn
98  $urnParsed = parse_url($urn);
99  $type = $urnParsed['host'];
100  if (isset($urnParsed['query'])) {
101  parse_str(htmlspecialchars_decode($urnParsed['query']), $data);
102  } else {
103  $data = [];
104  }
105  $fragment = $urnParsed['fragment'] ?? null;
106 
107  if (is_object($this->handlers[$type])) {
108  $result = $this->handlers[$type]->resolveHandlerData($data);
109  $result['type'] = $type;
110  } else {
111  throw new Exception\UnknownLinkHandlerException('LinkHandler for ' . $type . ' was not registered', 1460581769);
112  }
113  // this was historically named "section"
114  if ($fragment) {
115  $result['fragment'] = $fragment;
116  }
117  } elseif (stripos($urn, '://') && $this->handlers[self::TYPE_URL]) {
118  $result = $this->handlers[self::TYPE_URL]->resolveHandlerData(['url' => $urn]);
119  $result['type'] = self::TYPE_URL;
120  } elseif (stripos($urn, 'mailto:') === 0 && $this->handlers[self::TYPE_EMAIL]) {
121  $result = $this->handlers[self::TYPE_EMAIL]->resolveHandlerData(['email' => $urn]);
122  $result['type'] = self::TYPE_EMAIL;
123  } else {
124  $result = [];
125  if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['Link']['resolveByStringRepresentation'] ?? null)) {
126  $params = ['urn' => $urn, 'result' => &$result];
127  foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['Link']['resolveByStringRepresentation'] as $hookMethod) {
128  $fakeThis = false;
129  GeneralUtility::callUserFunction($hookMethod, $params, $fakeThis);
130  }
131  }
132  if (empty($result) || empty($result['type'])) {
133  throw new Exception\UnknownUrnException('No valid URN to resolve found', 1457177667);
134  }
135  }
136 
137  return $result;
138  }
139 
153  public function asString(array $parameters): string
154  {
155  if (is_object($this->handlers[$parameters['type']])) {
156  return $this->handlers[$parameters['type']]->asString($parameters);
157  }
158  if (isset($parameters['url']) && !empty($parameters['url'])) {
159  // This usually happens for tel: or other types where a URL is available and the
160  // legacy link service could resolve at least something
161  return $parameters['url'];
162  }
163  throw new Exception\UnknownLinkHandlerException('No valid handlers found for type: ' . $parameters['type'], 1460629247);
164  }
165 }
static callUserFunction($funcName, &$params, &$ref, $_='', $errorMode=0)
static makeInstance($className,... $constructorArguments)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']