Apply PSR-12 code style (#3886)
[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 * @package WoltLabSuite\Core\System\Request
17 * @since 3.0
18 */
19 class LookupRequestRoute implements IRequestRoute
20 {
21 /**
22 * list of parsed route information
23 * @var array
24 */
25 protected $routeData = [];
26
27 /**
28 * @inheritDoc
29 */
30 public function matches($requestURL)
31 {
32 $requestURL = FileUtil::removeLeadingSlash($requestURL);
33
34 if ($requestURL === '') {
35 // ignore empty urls and let them be handled by regular routes
36 return false;
37 }
38
39 $regex = '~^
40 (?P<controller>.+?)
41 (?:
42 /
43 (?P<id>[0-9]+)
44 (?:
45 -
46 (?P<title>[^/]+)
47 )?
48 /?
49 )?
50 $~x';
51
52 if (\preg_match($regex, $requestURL, $matches)) {
53 $application = ApplicationHandler::getInstance()->getActiveApplication()->getAbbreviation();
54 if (!empty($matches['id'])) {
55 // check for static controller URLs
56 $this->routeData = ControllerMap::getInstance()->resolveCustomController(
57 $application,
58 FileUtil::removeTrailingSlash($matches['controller'])
59 );
60
61 // lookup WCF controllers unless initial request targeted WCF itself
62 if (empty($this->routeData) && $application !== 'wcf') {
63 $this->routeData = ControllerMap::getInstance()->resolveCustomController(
64 'wcf',
65 FileUtil::removeTrailingSlash($matches['controller'])
66 );
67 }
68
69 if (!empty($this->routeData)) {
70 if (!empty($matches['id'])) {
71 $this->routeData['id'] = $matches['id'];
72
73 if (!empty($matches['title'])) {
74 $this->routeData['title'] = $matches['title'];
75 }
76 }
77 }
78 }
79
80 if (empty($this->routeData)) {
81 // try to match the entire url
82 $this->routeData = ControllerMap::getInstance()->resolveCustomController(
83 $application,
84 FileUtil::removeTrailingSlash($requestURL)
85 );
86
87 // lookup WCF controllers unless initial request targeted WCF itself
88 if (empty($this->routeData) && $application !== 'wcf') {
89 $this->routeData = ControllerMap::getInstance()->resolveCustomController(
90 'wcf',
91 FileUtil::removeTrailingSlash($requestURL)
92 );
93 }
94 }
95 }
96
97 if (!empty($this->routeData)) {
98 $this->routeData['isDefaultController'] = false;
99
100 return true;
101 }
102
103 return false;
104 }
105
106 /**
107 * @inheritDoc
108 */
109 public function getRouteData()
110 {
111 return $this->routeData;
112 }
113
114 /**
115 * @inheritDoc
116 */
117 public function setIsACP($isACP)
118 {
119 throw new \BadMethodCallException('lookups are not supported for ACP requests');
120 }
121
122 /**
123 * @inheritDoc
124 * @throws \BadMethodCallException
125 */
126 public function buildLink(array $components)
127 {
128 throw new \BadMethodCallException(
129 'LookupRequestRoute cannot build links, please verify capabilities by calling canHandle() first.'
130 );
131 }
132
133 /**
134 * @inheritDoc
135 */
136 public function canHandle(array $components)
137 {
138 // this route cannot build routes, it is a one-way resolver
139 return false;
140 }
141
142 /**
143 * @inheritDoc
144 */
145 public function isACP()
146 {
147 // lookups are not supported for ACP requests
148 return false;
149 }
150 }