improve link building (QUERY_STRING and PATH_INFO are now allowed)
[GitHub/Stricted/Domain-Control-Panel.git] / lib / system / RequestHandler.class.php
1 <?php
2 namespace dns\system;
3 use dns\system\cache\builder\ControllerCacheBuilder;
4 use dns\system\route\Request;
5 use dns\system\route\Segment;
6 use Zend\Router\Http\RouteMatch;
7 use Zend\Router\SimpleRouteStack;
8
9 /**
10 * @author Jan Altensen (Stricted)
11 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
12 * @copyright 2013-2016 Jan Altensen (Stricted)
13 */
14 class RequestHandler extends SingletonFactory {
15 protected $router = null;
16 protected $apiModule = false;
17
18 /**
19 * init RequestHandler
20 */
21 protected function init () {
22 $this->router = new SimpleRouteStack();
23
24 if (DNS::getSession()->username !== null) {
25 DNS::getTPL()->assign(array("username" => DNS::getSession()->username));
26 }
27 else {
28 DNS::getTPL()->assign(array("username" => ''));
29 }
30 }
31
32 /**
33 * set the default rules
34 *
35 * @param string $module
36 **/
37 public function setRoutes($module='') {
38 if ($module == "api") {
39 $this->apiModule = true;
40 }
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) {
47 $routes[$name] = Segment::factory([ 'route' => '[/]' . $name . '[/][/:id[-:title]]', 'constraints' => [ 'id' => '[0-9]+', 'title' => '[a-zA-Z0-9_.-/]+' ], 'defaults' => [ 'controller' => $data ] ]);
48 }
49
50 $this->router->setRoutes($routes);
51 }
52
53 /**
54 * @see \Zend\Mvc\Router\SimpleRouteStack::addRoute()
55 *
56 * @param string $name
57 * @param mixed $route
58 */
59 public function addRoute ($name, $route) {
60 $this->router->addRoute($name, $route);
61 }
62
63 /**
64 * @see \Zend\Mvc\Router\SimpleRouteStack::addRoutes()
65 *
66 * @param array|Traversable $routes
67 */
68 public function addRoutes ($routes) {
69 $this->router->addRoutes($routes);
70 }
71
72 /**
73 * Get the added routes
74 *
75 * @return Traversable list of all routes
76 */
77 public function getRoutes() {
78 return $this->router->getRoutes();
79 }
80
81 /**
82 * handle the request
83 */
84 public function handle () {
85 $match = $this->router->match(new Request());
86 if ($match !== null) {
87 $this->registerRouteData($match);
88
89 $className = $match->getParam("controller");
90
91 if (!User::isLoggedIn() && $this->apiModule == false && $className != 'dns\page\LoginPage' && $className != 'dns\page\ApiPage') {
92 DNS::getTPL()->display('login.tpl');
93 exit;
94 }
95
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 }
109 }
110
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');
125 exit;
126 }
127 }
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.'));
131 DNS::getTPL()->display('error.tpl');
132 exit;
133 }
134 }
135
136 /**
137 * Registers route data within $_GET and $_REQUEST.
138 */
139 protected function registerRouteData(RouteMatch $route) {
140 foreach ($route->getParams() as $key => $value) {
141 $_GET[$key] = $value;
142 $_REQUEST[$key] = $value;
143 }
144 }
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 = '') {
157 $path_info = true; // TODO: add config constant for that
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'])) {
176 $url .= '/'.$params['id'].'-'.$params['title'];
177 }
178 else {
179 $url .= '/'.$params['id'];
180 }
181 }
182
183 if (!empty($query)) {
184 if ($path_info) {
185 if (strpos($query, '&') === 0) {
186 $query = '/' . substr($query, 1);
187 }
188 else if (strpos($query, '/') !== 0) {
189 $query = '/' . $query;
190 }
191 }
192 else {
193 if (strpos($query, '/') === 0) {
194 $query = '&' . substr($query, 1);
195 }
196 else if (strpos($query, '&') !== 0) {
197 $query = '&' . $query;
198 }
199 }
200
201 $url .= $query;
202 }
203
204 return $url;
205 }
206 }