90b9553cdeac544a46c059c4577ad0bc36f05780
[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\Literal;
5 use dns\system\route\Regex;
6 use dns\system\route\Request;
7 use dns\system\route\Segment;
8 use Zend\Mvc\Router\SimpleRouteStack;
9 use Zend\Mvc\Router\Http\RouteMatch;
10
11 /**
12 * @author Jan Altensen (Stricted)
13 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
14 * @copyright 2013-2016 Jan Altensen (Stricted)
15 */
16 class RequestHandler extends SingletonFactory {
17 protected $router = null;
18 protected $apiModule = false;
19
20 /**
21 * init RequestHandler
22 */
23 protected function init () {
24 $this->router = new SimpleRouteStack();
25 if (DNS::getSession()->username !== null) {
26 DNS::getTPL()->assign(array("username" => DNS::getSession()->username));
27 }
28 else {
29 DNS::getTPL()->assign(array("username" => ''));
30 }
31 }
32
33 /**
34 * set the default rules
35 *
36 * @param string $module
37 **/
38 public function setRoutes($module='') {
39 if ($module == "api") {
40 $this->apiModule = true;
41 }
42
43 /* load the controllers from cache and build routes */
44 $controllers = ControllerCacheBuilder::getInstance()->getData(array('module' => $module));
45 $routes = [];
46
47 foreach ($controllers as $name => $data) {
48 $routes[$name] = Segment::factory([ 'route' => $name.'[/:id[-:title]]', 'constraints' => [ 'id' => '[0-9]+', 'title' => '[a-zA-Z0-9_.-/]+' ], 'defaults' => [ 'controller' => $data ] ]);
49 }
50
51 $this->router->setRoutes($routes);
52 }
53
54 /**
55 * @see \Zend\Mvc\Router\SimpleRouteStack::addRoute()
56 *
57 * @param string $name
58 * @param mixed $route
59 */
60 public function addRoute ($name, $route) {
61 $this->router->addRoute($name, $route);
62 }
63
64 /**
65 * @see \Zend\Mvc\Router\SimpleRouteStack::addRoutes()
66 *
67 * @param array|Traversable $routes
68 */
69 public function addRoutes ($routes) {
70 $this->router->addRoutes($routes);
71 }
72
73 /**
74 * Get the added routes
75 *
76 * @return Traversable list of all routes
77 */
78 public function getRoutes() {
79 return $this->router->getRoutes();
80 }
81
82 /**
83 * handle the request
84 */
85 public function handle () {
86 $match = $this->router->match(new Request());
87 if ($match !== null) {
88 $this->registerRouteData($match);
89
90 $className = $match->getParam("controller");
91
92 if (!User::isLoggedIn() && $this->apiModule == false && $className != 'dns\page\LoginPage' && $className != 'dns\page\ApiPage') {
93 DNS::getTPL()->display('login.tpl');
94 exit;
95 }
96
97 if (defined('OFFLINE') && OFFLINE) {
98 $admin = User::isAdmin();
99 $available = false;
100
101 if (defined($className . '::AVAILABLE_DURING_OFFLINE_MODE') && constant($className . '::AVAILABLE_DURING_OFFLINE_MODE')) {
102 $available = true;
103 }
104
105 if (!$admin && !$available) {
106 @header('HTTP/1.1 503 Service Unavailable');
107 DNS::getTPL()->display('offline.tpl');
108 exit;
109 }
110 }
111
112 try {
113 new $className();
114 }
115 catch (\Exception $e) {
116 if ($e->getCode() == 404) {
117 @header('HTTP/1.0 404 Not Found');
118 }
119 else if ($e->getCode() == 403) {
120 @header('HTTP/1.0 403 Forbidden');
121 }
122
123 // show error page
124 DNS::getTPL()->assign(array("activeMenuItem" => '', "error" => $e->getMessage()));
125 DNS::getTPL()->display('error.tpl');
126 exit;
127 }
128 }
129 else {
130 @header('HTTP/1.0 404 Not Found');
131 DNS::getTPL()->assign(array("activeMenuItem" => '', "error" => 'The link you are trying to reach is no longer available or invalid.'));
132 DNS::getTPL()->display('error.tpl');
133 exit;
134 }
135 }
136
137 /**
138 * Registers route data within $_GET and $_REQUEST.
139 */
140 protected function registerRouteData(RouteMatch $route) {
141 foreach ($route->getParams() as $key => $value) {
142 $_GET[$key] = $value;
143 $_REQUEST[$key] = $value;
144 }
145 }
146 }