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
;
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)
16 class RequestHandler
extends SingletonFactory
{
17 protected $router = null;
18 protected $apiModule = false;
23 protected function init () {
24 $this->router
= new SimpleRouteStack();
25 if (DNS
::getSession()->username
!== null) {
26 DNS
::getTPL()->assign(array("username" => DNS
::getSession()->username
));
29 DNS
::getTPL()->assign(array("username" => ''));
34 * set the default rules
36 * @param string $module
38 public function setRoutes($module='') {
39 if ($module == "api") {
40 $this->apiModule
= true;
43 /* load the controllers from cache and build routes */
44 $controllers = ControllerCacheBuilder
::getInstance()->getData(array('module' => $module));
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 ] ]);
51 $this->router
->setRoutes($routes);
55 * @see \Zend\Mvc\Router\SimpleRouteStack::addRoute()
60 public function addRoute ($name, $route) {
61 $this->router
->addRoute($name, $route);
65 * @see \Zend\Mvc\Router\SimpleRouteStack::addRoutes()
67 * @param array|Traversable $routes
69 public function addRoutes ($routes) {
70 $this->router
->addRoutes($routes);
74 * Get the added routes
76 * @return Traversable list of all routes
78 public function getRoutes() {
79 return $this->router
->getRoutes();
85 public function handle () {
86 $match = $this->router
->match(new Request());
87 if ($match !== null) {
88 $this->registerRouteData($match);
90 $className = $match->getParam("controller");
92 if (!User
::isLoggedIn() && $this->apiModule
== false && $className != 'dns\page\LoginPage' && $className != 'dns\page\ApiPage') {
93 DNS
::getTPL()->display('login.tpl');
97 if (defined('OFFLINE') && OFFLINE
) {
98 $admin = User
::isAdmin();
101 if (defined($className . '::AVAILABLE_DURING_OFFLINE_MODE') && constant($className . '::AVAILABLE_DURING_OFFLINE_MODE')) {
105 if (!$admin && !$available) {
106 @header
('HTTP/1.1 503 Service Unavailable');
107 DNS
::getTPL()->display('offline.tpl');
115 catch (\Exception
$e) {
116 if ($e->getCode() == 404) {
117 @header
('HTTP/1.0 404 Not Found');
119 else if ($e->getCode() == 403) {
120 @header
('HTTP/1.0 403 Forbidden');
124 DNS
::getTPL()->assign(array("activeMenuItem" => '', "error" => $e->getMessage()));
125 DNS
::getTPL()->display('error.tpl');
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');
138 * Registers route data within $_GET and $_REQUEST.
140 protected function registerRouteData(RouteMatch
$route) {
141 foreach ($route->getParams() as $key => $value) {
142 $_GET[$key] = $value;
143 $_REQUEST[$key] = $value;