ee3c19fef013aa69b736f0d71885433a84b313ba
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / request / route / LookupRequestRoute.class.php
1 <?php
2
3 namespace wcf\system\request\route;
4
5 use wcf\system\application\ApplicationHandler;
6 use wcf\system\request\ControllerMap;
7 use wcf\util\FileUtil;
8
9 /**
10 * Attempts to resolve arbitrary request URLs against the list of known custom
11 * controller URLs, optionally recognizing id and title parameter.
12 *
13 * @author Alexander Ebert
14 * @copyright 2001-2019 WoltLab GmbH
15 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
16 * @since 3.0
17 */
18 final class LookupRequestRoute implements IRequestRoute
19 {
20 /**
21 * list of parsed route information
22 * @var array
23 */
24 protected array $routeData = [];
25
26 /**
27 * @inheritDoc
28 */
29 public function matches($requestURL): bool
30 {
31 $requestURL = FileUtil::removeLeadingSlash($requestURL);
32
33 if ($requestURL === '') {
34 // ignore empty urls and let them be handled by regular routes
35 return false;
36 }
37
38 $regex = '~^
39 (?P<controller>.+?)
40 (?:
41 /
42 (?P<id>[0-9]+)
43 (?:
44 -
45 (?P<title>[^/]+)
46 )?
47 /?
48 )?
49 $~x';
50
51 if (\preg_match($regex, $requestURL, $matches)) {
52 $application = ApplicationHandler::getInstance()->getActiveApplication()->getAbbreviation();
53 if (!empty($matches['id'])) {
54 // check for static controller URLs
55 $this->routeData = ControllerMap::getInstance()->resolveCustomController(
56 $application,
57 FileUtil::removeTrailingSlash($matches['controller'])
58 );
59
60 if ($this->routeData !== []) {
61 if (!empty($matches['id'])) {
62 $this->routeData['id'] = $matches['id'];
63
64 if (!empty($matches['title'])) {
65 $this->routeData['title'] = $matches['title'];
66 }
67 }
68 }
69 }
70
71 if ($this->routeData === []) {
72 // try to match the entire url
73 $this->routeData = ControllerMap::getInstance()->resolveCustomController(
74 $application,
75 FileUtil::removeTrailingSlash($requestURL)
76 );
77 }
78 }
79
80 if ($this->routeData !== []) {
81 $this->routeData['isDefaultController'] = false;
82
83 return true;
84 }
85
86 return false;
87 }
88
89 /**
90 * @inheritDoc
91 */
92 public function getRouteData(): array
93 {
94 return $this->routeData;
95 }
96
97 /**
98 * @inheritDoc
99 * @throws \BadMethodCallException
100 */
101 public function buildLink(array $components): string
102 {
103 throw new \BadMethodCallException(
104 'LookupRequestRoute cannot build links, please verify capabilities by calling canHandle() first.'
105 );
106 }
107
108 /**
109 * @inheritDoc
110 */
111 public function canHandle(array $components): bool
112 {
113 // this route cannot build routes, it is a one-way resolver
114 return false;
115 }
116
117 /**
118 * @inheritDoc
119 */
120 public function isACP(): bool
121 {
122 // lookups are not supported for ACP requests
123 return false;
124 }
125 }