Commit | Line | Data |
---|---|---|
1c456904 | 1 | <?php |
a9229942 | 2 | |
1c456904 | 3 | namespace wcf\system\request\route; |
a9229942 | 4 | |
2d38f346 | 5 | use wcf\system\application\ApplicationHandler; |
1c456904 AE |
6 | use wcf\system\request\ControllerMap; |
7 | use wcf\util\FileUtil; | |
8 | ||
2d38f346 AE |
9 | /** |
10 | * Attempts to resolve arbitrary request URLs against the list of known custom | |
11 | * controller URLs, optionally recognizing id and title parameter. | |
a9229942 TD |
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> | |
a9229942 | 16 | * @since 3.0 |
2d38f346 | 17 | */ |
2e859d68 | 18 | final class LookupRequestRoute implements IRequestRoute |
a9229942 TD |
19 | { |
20 | /** | |
21 | * list of parsed route information | |
22 | * @var array | |
23 | */ | |
a4b74211 | 24 | protected array $routeData = []; |
a9229942 TD |
25 | |
26 | /** | |
27 | * @inheritDoc | |
28 | */ | |
a4b74211 | 29 | public function matches($requestURL): bool |
a9229942 TD |
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 = '~^ | |
1c456904 AE |
39 | (?P<controller>.+?) |
40 | (?: | |
c18c47be | 41 | / |
1c456904 AE |
42 | (?P<id>[0-9]+) |
43 | (?: | |
44 | - | |
45 | (?P<title>[^/]+) | |
46 | )? | |
df05ba14 | 47 | /? |
1c456904 AE |
48 | )? |
49 | $~x'; | |
a9229942 TD |
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 | ||
4a1873fe | 60 | if ($this->routeData !== []) { |
a9229942 TD |
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 | ||
4a1873fe | 71 | if ($this->routeData === []) { |
a9229942 TD |
72 | // try to match the entire url |
73 | $this->routeData = ControllerMap::getInstance()->resolveCustomController( | |
74 | $application, | |
75 | FileUtil::removeTrailingSlash($requestURL) | |
76 | ); | |
a9229942 TD |
77 | } |
78 | } | |
79 | ||
4a1873fe | 80 | if ($this->routeData !== []) { |
a9229942 TD |
81 | $this->routeData['isDefaultController'] = false; |
82 | ||
83 | return true; | |
84 | } | |
85 | ||
86 | return false; | |
87 | } | |
88 | ||
89 | /** | |
90 | * @inheritDoc | |
91 | */ | |
a4b74211 | 92 | public function getRouteData(): array |
a9229942 TD |
93 | { |
94 | return $this->routeData; | |
95 | } | |
96 | ||
a9229942 TD |
97 | /** |
98 | * @inheritDoc | |
99 | * @throws \BadMethodCallException | |
100 | */ | |
a4b74211 | 101 | public function buildLink(array $components): string |
a9229942 TD |
102 | { |
103 | throw new \BadMethodCallException( | |
104 | 'LookupRequestRoute cannot build links, please verify capabilities by calling canHandle() first.' | |
105 | ); | |
106 | } | |
107 | ||
108 | /** | |
109 | * @inheritDoc | |
110 | */ | |
a4b74211 | 111 | public function canHandle(array $components): bool |
a9229942 TD |
112 | { |
113 | // this route cannot build routes, it is a one-way resolver | |
114 | return false; | |
115 | } | |
116 | ||
117 | /** | |
118 | * @inheritDoc | |
119 | */ | |
a4b74211 | 120 | public function isACP(): bool |
a9229942 TD |
121 | { |
122 | // lookups are not supported for ACP requests | |
123 | return false; | |
124 | } | |
041bf248 | 125 | } |