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