fix last commit
[GitHub/Stricted/Domain-Control-Panel.git] / lib / system / RequestHandler.class.php
CommitLineData
2aa91ff2
S
1<?php
2namespace dns\system;
ff49a0be 3use dns\system\cache\builder\ControllerCacheBuilder;
8ea73456 4use dns\system\route\Request;
f0f86c22 5use dns\system\route\Segment;
43bc7b62
S
6use Zend\Router\Http\RouteMatch;
7use Zend\Router\SimpleRouteStack;
2aa91ff2
S
8
9/**
10 * @author Jan Altensen (Stricted)
11 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
d4779364 12 * @copyright 2013-2016 Jan Altensen (Stricted)
2aa91ff2 13 */
d4f35691
S
14class RequestHandler extends SingletonFactory {
15 protected $router = null;
f0f86c22 16 protected $apiModule = false;
ff49a0be 17
2aa91ff2 18 /**
f0f86c22 19 * init RequestHandler
2aa91ff2 20 */
d4f35691
S
21 protected function init () {
22 $this->router = new SimpleRouteStack();
ac94d319 23
ff49a0be
S
24 if (DNS::getSession()->username !== null) {
25 DNS::getTPL()->assign(array("username" => DNS::getSession()->username));
2aa91ff2
S
26 }
27 else {
ff49a0be 28 DNS::getTPL()->assign(array("username" => ''));
2aa91ff2 29 }
d4f35691
S
30 }
31
f0f86c22
S
32 /**
33 * set the default rules
34 *
35 * @param string $module
36 **/
d4f35691 37 public function setRoutes($module='') {
616cffa8 38 if ($module == "api") {
f0f86c22 39 $this->apiModule = true;
d4f35691 40 }
616cffa8
S
41
42 /* load the controllers from cache and build routes */
43 $controllers = ControllerCacheBuilder::getInstance()->getData(array('module' => $module));
44 $routes = [];
45
46 foreach ($controllers as $name => $data) {
cff58c55 47 $routes[$name] = Segment::factory([ 'route' => '[/]' . $name . '[/][/:id[-:title]]', 'constraints' => [ 'id' => '[0-9]+', 'title' => '[a-zA-Z0-9_.-/]+' ], 'defaults' => [ 'controller' => $data ] ]);
f0f86c22 48 }
616cffa8
S
49
50 $this->router->setRoutes($routes);
f0f86c22 51 }
d4f35691 52
f0f86c22
S
53 /**
54 * @see \Zend\Mvc\Router\SimpleRouteStack::addRoute()
55 *
ec1c709b
S
56 * @param string $name
57 * @param mixed $route
f0f86c22
S
58 */
59 public function addRoute ($name, $route) {
60 $this->router->addRoute($name, $route);
61 }
62
63 /**
64 * @see \Zend\Mvc\Router\SimpleRouteStack::addRoutes()
65 *
ec1c709b 66 * @param array|Traversable $routes
f0f86c22
S
67 */
68 public function addRoutes ($routes) {
69 $this->router->addRoutes($routes);
70 }
71
ec1c709b
S
72 /**
73 * Get the added routes
74 *
75 * @return Traversable list of all routes
76 */
d4f35691 77 public function getRoutes() {
f0f86c22 78 return $this->router->getRoutes();
d4f35691
S
79 }
80
f0f86c22
S
81 /**
82 * handle the request
83 */
d4f35691
S
84 public function handle () {
85 $match = $this->router->match(new Request());
44d399bc 86 if ($match !== null) {
616cffa8 87 $this->registerRouteData($match);
44d399bc
S
88
89 $className = $match->getParam("controller");
90
f0f86c22 91 if (!User::isLoggedIn() && $this->apiModule == false && $className != 'dns\page\LoginPage' && $className != 'dns\page\ApiPage') {
44d399bc 92 DNS::getTPL()->display('login.tpl');
ff49a0be
S
93 exit;
94 }
2aa91ff2 95
44d399bc
S
96 if (defined('OFFLINE') && OFFLINE) {
97 $admin = User::isAdmin();
98 $available = false;
99
100 if (defined($className . '::AVAILABLE_DURING_OFFLINE_MODE') && constant($className . '::AVAILABLE_DURING_OFFLINE_MODE')) {
101 $available = true;
102 }
103
104 if (!$admin && !$available) {
105 @header('HTTP/1.1 503 Service Unavailable');
106 DNS::getTPL()->display('offline.tpl');
107 exit;
108 }
2aa91ff2
S
109 }
110
44d399bc
S
111 try {
112 new $className();
113 }
114 catch (\Exception $e) {
115 if ($e->getCode() == 404) {
116 @header('HTTP/1.0 404 Not Found');
117 }
118 else if ($e->getCode() == 403) {
119 @header('HTTP/1.0 403 Forbidden');
120 }
121
122 // show error page
123 DNS::getTPL()->assign(array("activeMenuItem" => '', "error" => $e->getMessage()));
124 DNS::getTPL()->display('error.tpl');
2aa91ff2
S
125 exit;
126 }
127 }
44d399bc
S
128 else {
129 @header('HTTP/1.0 404 Not Found');
130 DNS::getTPL()->assign(array("activeMenuItem" => '', "error" => 'The link you are trying to reach is no longer available or invalid.'));
2aa91ff2
S
131 DNS::getTPL()->display('error.tpl');
132 exit;
133 }
134 }
616cffa8
S
135
136 /**
ec1c709b
S
137 * Registers route data within $_GET and $_REQUEST.
138 */
616cffa8
S
139 protected function registerRouteData(RouteMatch $route) {
140 foreach ($route->getParams() as $key => $value) {
141 $_GET[$key] = $value;
142 $_REQUEST[$key] = $value;
143 }
144 }
cff58c55
S
145
146 public function getBaseUrl () {
147 $protocol = 'http://';
148
149 if ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443 || (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) {
150 $protocol = 'https://';
151 }
152
153 return $protocol . $_SERVER['HTTP_HOST'] . '/';
154 }
155
156 public function getLink (array $params = [], $query = '') {
f039270f 157 $path_info = false; // TODO: add config constant for that
cff58c55
S
158
159 $url = $this->getBaseUrl() . 'index.php';
160 if ($path_info) {
161 $url .= '/';
162 }
163 else {
164 $url .= '?';
165 }
166
167 if (!empty($params['controller'])) {
168 $url .= $params['controller'];
169 }
170 else {
171 // TODO: InvalidArgumentException?
172 }
173
174 if (!empty($params['id'])) {
175 if (!empty($params['title'])) {
f039270f 176 $url .= '/' . $params['id'] . '-' . $params['title'];
cff58c55
S
177 }
178 else {
f039270f 179 $url .= '/' . $params['id'];
cff58c55
S
180 }
181 }
182
183 if (!empty($query)) {
184 if ($path_info) {
185 if (strpos($query, '&') === 0) {
f039270f 186 $query = '?' . substr($query, 1);
cff58c55 187 }
f039270f
S
188 else if (strpos($query, '?') !== 0) {
189 $query = '?' . $query;
cff58c55 190 }
f039270f
S
191
192 $query = '/' . $query;
cff58c55
S
193 }
194 else {
f039270f 195 if (strpos($query, '?') === 0) {
cff58c55
S
196 $query = '&' . substr($query, 1);
197 }
198 else if (strpos($query, '&') !== 0) {
199 $query = '&' . $query;
200 }
201 }
202
203 $url .= $query;
204 }
205
206 return $url;
207 }
2aa91ff2 208}